-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
181e959
commit 442a3c6
Showing
8 changed files
with
110 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,7 @@ | |
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
.ruby-version | ||
.byebug_history | ||
/tmp/ | ||
.DS_STORE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/generators/active_reporting/mysql_function_migration_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails/generators/active_record' | ||
|
||
module ActiveReporting | ||
class MysqlFunctionMigrationGenerator < ActiveRecord::Generators::Base | ||
argument :name, type: :string, default: 'no_name', banner: 'This migration has default name' | ||
source_root File.expand_path("../templates", __FILE__) | ||
|
||
def copy_active_reporting_migration | ||
if mysql? && !migration_exists? | ||
migration_template( | ||
'migration.rb', | ||
"#{migration_path}/add_date_trunc_function_to_mysql.rb", | ||
migration_version: migration_version | ||
) | ||
end | ||
end | ||
|
||
def migration_exists? | ||
Dir.glob( | ||
"#{File.join(destination_root, migration_path)}/[0-9]*_*.rb" | ||
).grep(/\d+_add_date_trunc_function_to_mysql.rb$/).present? | ||
end | ||
|
||
def rails5_and_up? | ||
Rails::VERSION::MAJOR >= 5 | ||
end | ||
|
||
def mysql? | ||
config = ActiveRecord::Base.configurations[Rails.env] | ||
config && config['adapter'] == 'mysql2' | ||
end | ||
|
||
def migration_version | ||
if rails5_and_up? | ||
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" | ||
end | ||
end | ||
|
||
def migration_path | ||
if Rails.version >= '5.0.3' | ||
db_migrate_path | ||
else | ||
@migration_path ||= File.join('db', 'migrate') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddDateTruncFunctionToMysql < ActiveRecord::Migration[5.1] | ||
def up | ||
drop_function | ||
ActiveRecord::Base.connection.execute(date_trunc_function.strip.gsub(/\n/, ' ')) | ||
end | ||
|
||
def down | ||
drop_function | ||
end | ||
|
||
private | ||
|
||
def drop_function | ||
ActiveRecord::Base.connection.execute( | ||
<<-SQL | ||
DROP FUNCTION IF EXISTS date_trunc | ||
SQL | ||
) | ||
end | ||
|
||
def date_trunc_function | ||
func = <<-SQL | ||
CREATE FUNCTION date_trunc(vInterval varchar(7), vDate timestamp) | ||
RETURNS timestamp | ||
begin | ||
declare toReturn timestamp; | ||
IF vInterval = 'year' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(YEAR, '1900-01-01', vDate) YEAR); | ||
elseif vInterval = 'quarter' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(QUARTER, '1900-01-01', vDate) QUARTER); | ||
elseif vInterval = 'month' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(MONTH, '1900-01-01', vDate) MONTH); | ||
elseif vInterval = 'week' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(WEEK, '1900-01-01', vDate) WEEK); | ||
elseif vInterval = 'day' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(DAY, '1900-01-01', vDate) DAY); | ||
elseif vInterval = 'hour' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(HOUR, '1900-01-01', vDate) HOUR); | ||
elseif vInterval = 'minute' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(MINUTE, '1900-01-01', vDate) MINUTE); | ||
END IF; | ||
RETURN toReturn; | ||
END | ||
SQL | ||
func | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters