Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor schema migration table creation to the schema migration model
  • Loading branch information
tenderlove committed Jan 13, 2012
1 parent 2c667f6 commit e0f0afb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
Expand Up @@ -414,15 +414,7 @@ def dump_schema_information #:nodoc:
# Should not be called normally, but this operation is non-destructive. # Should not be called normally, but this operation is non-destructive.
# The migrations module handles this automatically. # The migrations module handles this automatically.
def initialize_schema_migrations_table def initialize_schema_migrations_table
sm_table = ActiveRecord::Migrator.schema_migrations_table_name ActiveRecord::SchemaMigration.create_table

unless table_exists?(sm_table)
create_table(sm_table, :id => false) do |schema_migrations_table|
schema_migrations_table.column :version, :string, :null => false
end
add_index sm_table, :version, :unique => true,
:name => "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}"
end
end end


def assume_migrated_upto_version(version, migrations_paths = ActiveRecord::Migrator.migrations_paths) def assume_migrated_upto_version(version, migrations_paths = ActiveRecord::Migrator.migrations_paths)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/migration.rb
Expand Up @@ -669,7 +669,7 @@ def initialize(direction, migrations, target_version = nil)


validate(@migrations) validate(@migrations)


Base.connection.initialize_schema_migrations_table ActiveRecord::SchemaMigration.create_table
end end


def current_version def current_version
Expand Down
18 changes: 18 additions & 0 deletions activerecord/lib/active_record/schema_migration.rb
Expand Up @@ -8,6 +8,24 @@ def self.table_name
Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
end end


def self.create_table
unless connection.table_exists?(table_name)
connection.create_table(table_name, :id => false) do |t|
t.column :version, :string, :null => false
end
connection.add_index table_name, :version, :unique => true,
:name => "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}"
end
end

def self.drop_table
if connection.table_exists?(table_name)
connection.remove_index table_name, :version, :unique => true,
:name => "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}"
connection.drop_table(table_name)
end
end

def version def version
super.to_i super.to_i
end end
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/cases/migrator_test.rb
Expand Up @@ -19,6 +19,7 @@ def down; @went_down = true; end


def setup def setup
super super
ActiveRecord::SchemaMigration.create_table
ActiveRecord::SchemaMigration.delete_all rescue nil ActiveRecord::SchemaMigration.delete_all rescue nil
end end


Expand Down

0 comments on commit e0f0afb

Please sign in to comment.