diff options
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/migration_test.rb | 36 |
2 files changed, 39 insertions, 7 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 4e770c37da..730b1c7425 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -422,9 +422,17 @@ module ActiveRecord def initialize_schema_migrations_table sm_table = ActiveRecord::Migrator.schema_migrations_table_name - unless table_exists?(sm_table) + if table_exists?(sm_table) + cols = columns(sm_table).collect { |col| col.name } + unless cols.include?("migrated_at") + add_column sm_table, :migrated_at, :datetime + update "UPDATE #{quote_table_name(sm_table)} SET migrated_at = '#{quoted_date(Time.now)}' WHERE migrated_at IS NULL" + change_column sm_table, :migrated_at, :datetime, :null => false + end + else create_table(sm_table, :id => false) do |schema_migrations_table| schema_migrations_table.column :version, :string, :null => false + schema_migrations_table.column :migrated_at, :datetime, :null => false end add_index sm_table, :version, :unique => true, :name => "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}" diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 3037d73a1b..55a24a94f3 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -27,22 +27,46 @@ if ActiveRecord::Base.connection.supports_migrations? end class MigrationTableAndIndexTest < ActiveRecord::TestCase - def test_add_schema_info_respects_prefix_and_suffix - conn = ActiveRecord::Base.connection + def setup + @conn = ActiveRecord::Base.connection + @conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if @conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) + end - conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) + def test_add_schema_migrations_respects_prefix_and_suffix # Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters ActiveRecord::Base.table_name_prefix = 'p_' ActiveRecord::Base.table_name_suffix = '_s' - conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) + @conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if @conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) - conn.initialize_schema_migrations_table + @conn.initialize_schema_migrations_table - assert_equal "p_unique_schema_migrations_s", conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name] + assert_equal "p_unique_schema_migrations_s", @conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name] ensure ActiveRecord::Base.table_name_prefix = "" ActiveRecord::Base.table_name_suffix = "" end + + def test_schema_migrations_columns + @conn.initialize_schema_migrations_table + + columns = @conn.columns(ActiveRecord::Migrator.schema_migrations_table_name).collect(&:name) + %w[version migrated_at].each { |col| assert columns.include?(col) } + end + + def test_add_migrated_at_to_exisiting_schema_migrations + sm_table = ActiveRecord::Migrator.schema_migrations_table_name + @conn.create_table(sm_table, :id => false) do |schema_migrations_table| + schema_migrations_table.column :version, :string, :null => false + end + @conn.insert "INSERT INTO #{@conn.quote_table_name(sm_table)} (version) VALUES (100)" + @conn.insert "INSERT INTO #{@conn.quote_table_name(sm_table)} (version) VALUES (200)" + + @conn.initialize_schema_migrations_table + + m_ats = @conn.select_values("SELECT migrated_at FROM #{@conn.quote_table_name(sm_table)}") + assert_equal 2, m_ats.length + assert_equal 2, m_ats.compact.length + end end class MigrationTest < ActiveRecord::TestCase |