diff options
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] |