Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to be usable in Rails 3.x #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/migration_sql_generator/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 10 additions & 9 deletions lib/migration_sql_generator/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down