diff --git a/lib/migration_sql_generator/generator.rb b/lib/migration_sql_generator/generator.rb index ca11484..e655582 100644 --- a/lib/migration_sql_generator/generator.rb +++ b/lib/migration_sql_generator/generator.rb @@ -17,6 +17,10 @@ def redefine_execute_methods connection.class.send(:define_method, :column_for) {|*args| args.last } connection.class.send(:define_method, :tables) {|*args| [] } connection.class.send(:define_method, :select_all) {|*args| [] } + + # disable checking for indexes in DB + connection.class.send(:define_method, :index_name_for_remove) {|*args| index_name(*args) } + connection.class.send(:define_method, :index_name_exists?) {|*args| false} end def save_original_methods @@ -39,7 +43,7 @@ def generate_instead_of_executing(&block) end def migrations - Dir.glob(File.join(RAILS_ROOT, "db", "migrate", '*.rb')) + Dir.glob(File.join(Rails.root, "db", "migrate", '*.rb')) end def generate @@ -49,11 +53,11 @@ def generate def up_and_down(file) migration = MigrationSqlGenerator::Migration.new(file) MigrationSqlGenerator::Writer.file_name = "#{migration}.sql" - migration.up + migration.migrate(:up) begin MigrationSqlGenerator::Writer.file_name = "#{migration}_down.sql" - migration.down + migration.migrate(:down) rescue ActiveRecord::IrreversibleMigration end diff --git a/lib/migration_sql_generator/migration.rb b/lib/migration_sql_generator/migration.rb index 7640f0c..99cc639 100644 --- a/lib/migration_sql_generator/migration.rb +++ b/lib/migration_sql_generator/migration.rb @@ -4,7 +4,7 @@ class Migration def initialize(migration_file) self.full_name = File.basename(migration_file, ".rb") - require "db/migrate/#{full_name}" + require "#{Rails.root}/db/migrate/#{full_name}.rb" end def number @@ -23,14 +23,15 @@ def migration_class name.camelize.constantize end - def up - migration_class.up - connection.execute("INSERT INTO schema_migrations (version) VALUES (#{number})") - end - - def down - migration_class.down - connection.execute("DELETE FROM schema_migrations WHERE version = #{number}") + def migrate(direction) + migration_instance = migration_class.new + migration_instance.migrate(direction) + if direction == :up + connection.execute("INSERT INTO schema_migrations (version) VALUES (#{number})") + end + if(direction == :down) + connection.execute("DELETE FROM schema_migrations WHERE version = #{number}") + end end def connection