Skip to content
This repository
Browse code

Add migrated_at column to schema_migrations table.

  • Loading branch information...
commit c283cdd63cafdb04784cfcc5094da41c9268c20c 1 parent 7148b93
Josh Susser authored March 15, 2010
10  activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -422,9 +422,17 @@ def dump_schema_information #:nodoc:
422 422
       def initialize_schema_migrations_table
423 423
         sm_table = ActiveRecord::Migrator.schema_migrations_table_name
424 424
 
425  
-        unless table_exists?(sm_table)
  425
+        if table_exists?(sm_table)
  426
+          cols = columns(sm_table).collect { |col| col.name }
  427
+          unless cols.include?("migrated_at")
  428
+            add_column sm_table, :migrated_at, :datetime
  429
+            update "UPDATE #{quote_table_name(sm_table)} SET migrated_at = '#{quoted_date(Time.now)}' WHERE migrated_at IS NULL"
  430
+            change_column sm_table, :migrated_at, :datetime, :null => false
  431
+          end
  432
+        else
426 433
           create_table(sm_table, :id => false) do |schema_migrations_table|
427 434
             schema_migrations_table.column :version, :string, :null => false
  435
+            schema_migrations_table.column :migrated_at, :datetime, :null => false
428 436
           end
429 437
           add_index sm_table, :version, :unique => true,
430 438
             :name => "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}"
36  activerecord/test/cases/migration_test.rb
@@ -27,22 +27,46 @@ def puts(text="")
27 27
   end
28 28
 
29 29
   class MigrationTableAndIndexTest < ActiveRecord::TestCase
30  
-    def test_add_schema_info_respects_prefix_and_suffix
31  
-      conn = ActiveRecord::Base.connection
  30
+    def setup
  31
+      @conn = ActiveRecord::Base.connection
  32
+      @conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if @conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
  33
+    end
32 34
 
33  
-      conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
  35
+    def test_add_schema_migrations_respects_prefix_and_suffix
34 36
       # Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters
35 37
       ActiveRecord::Base.table_name_prefix = 'p_'
36 38
       ActiveRecord::Base.table_name_suffix = '_s'
37  
-      conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
  39
+      @conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if @conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
38 40
 
39  
-      conn.initialize_schema_migrations_table
  41
+      @conn.initialize_schema_migrations_table
40 42
 
41  
-      assert_equal "p_unique_schema_migrations_s", conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name]
  43
+      assert_equal "p_unique_schema_migrations_s", @conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name]
42 44
     ensure
43 45
       ActiveRecord::Base.table_name_prefix = ""
44 46
       ActiveRecord::Base.table_name_suffix = ""
45 47
     end
  48
+
  49
+    def test_schema_migrations_columns
  50
+      @conn.initialize_schema_migrations_table
  51
+
  52
+      columns =  @conn.columns(ActiveRecord::Migrator.schema_migrations_table_name).collect(&:name)
  53
+      %w[version migrated_at].each { |col| assert columns.include?(col) }
  54
+    end
  55
+
  56
+    def test_add_migrated_at_to_exisiting_schema_migrations
  57
+      sm_table = ActiveRecord::Migrator.schema_migrations_table_name
  58
+      @conn.create_table(sm_table, :id => false) do |schema_migrations_table|
  59
+              schema_migrations_table.column :version, :string, :null => false
  60
+            end
  61
+      @conn.insert "INSERT INTO #{@conn.quote_table_name(sm_table)} (version) VALUES (100)"
  62
+      @conn.insert "INSERT INTO #{@conn.quote_table_name(sm_table)} (version) VALUES (200)"
  63
+
  64
+      @conn.initialize_schema_migrations_table
  65
+
  66
+      m_ats = @conn.select_values("SELECT migrated_at FROM #{@conn.quote_table_name(sm_table)}")
  67
+      assert_equal 2, m_ats.length
  68
+      assert_equal 2, m_ats.compact.length
  69
+    end
46 70
   end
47 71
 
48 72
   class MigrationTest < ActiveRecord::TestCase

0 notes on commit c283cdd

Please sign in to comment.
Something went wrong with that request. Please try again.