diff options
author | Rick Olson <technoweenie@gmail.com> | 2008-04-09 16:20:15 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2008-04-09 16:20:15 +0000 |
commit | 8a5a9dcbf64843f064b6e8a0b9c6eea8f0b8536e (patch) | |
tree | 27064d2d5ebccdd40bf5c4f43fcf5b2e3e7e57f3 /activerecord/lib/active_record/connection_adapters/abstract | |
parent | 78c2d9fc223e7a9945aee65c838f7ce78e9ddb3e (diff) | |
download | rails-8a5a9dcbf64843f064b6e8a0b9c6eea8f0b8536e.tar.gz rails-8a5a9dcbf64843f064b6e8a0b9c6eea8f0b8536e.tar.bz2 rails-8a5a9dcbf64843f064b6e8a0b9c6eea8f0b8536e.zip |
Add support for interleaving migrations by storing which migrations have run in the new schema_migrations table. Closes #11493 [jordi]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9244 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 393d5c130e..c986f0c6f1 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -232,33 +232,41 @@ module ActiveRecord # Should not be called normally, but this operation is non-destructive. # The migrations module handles this automatically. - def initialize_schema_information(current_version=0) - begin - execute "CREATE TABLE #{quote_table_name(ActiveRecord::Migrator.schema_info_table_name)} (version #{type_to_sql(:string)})" - execute "INSERT INTO #{quote_table_name(ActiveRecord::Migrator.schema_info_table_name)} (version) VALUES(#{current_version})" - rescue ActiveRecord::StatementInvalid - # Schema has been initialized, make sure version is a string - version_column = columns(:schema_info).detect { |c| c.name == "version" } - - # can't just alter the table, since SQLite can't deal - unless version_column.type == :string - version = ActiveRecord::Migrator.current_version - execute "DROP TABLE #{quote_table_name(ActiveRecord::Migrator.schema_info_table_name)}" - initialize_schema_information(version) + def initialize_schema_migrations_table + sm_table = ActiveRecord::Migrator.schema_migrations_table_name + + unless tables.detect { |t| t == sm_table } + create_table(sm_table, :id => false) do |schema_migrations_table| + schema_migrations_table.column :version, :string, :null => false end - end - end + add_index sm_table, :version, :unique => true, + :name => 'unique_schema_migrations' + + # Backwards-compatibility: if we find schema_info, assume we've + # migrated up to that point: + si_table = Base.table_name_prefix + 'schema_info' + Base.table_name_suffix + + if tables.detect { |t| t == si_table } - def dump_schema_information #:nodoc: - begin - if (current_schema = ActiveRecord::Migrator.current_version) > 0 - return "INSERT INTO #{quote_table_name(ActiveRecord::Migrator.schema_info_table_name)} (version) VALUES (#{current_schema})" + old_version = select_value("SELECT version FROM #{quote_table_name(si_table)}").to_i + assume_migrated_upto_version(old_version) + drop_table(si_table) end - rescue ActiveRecord::StatementInvalid - # No Schema Info end end + def assume_migrated_upto_version(version) + sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name) + migrated = select_values("SELECT version FROM #{sm_table}").map(&:to_i) + versions = Dir['db/migrate/[0-9]*_*.rb'].map do |filename| + filename.split('/').last.split('_').first.to_i + end + + execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')" unless migrated.include?(version.to_i) + (versions - migrated).select { |v| v < version.to_i }.each do |v| + execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" + end + end def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc: if native = native_database_types[type] |