public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix mysql 4.1 incompatibility in the active record schema tests.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#233 state:resolved ]
fcheung (author)
Wed May 21 13:03:38 -0700 2008
NZKoz (committer)
Wed May 21 16:02:27 -0700 2008
commit  dd9938a44ee3a7bb6c42527a1be6fcec70bf4772
tree    edc43d66fe1088e435f90efd50f5f7166194645b
parent  262d23d763c05bbe5f433a99cb9f02e48a8cdd4a
...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 
 
 
 
 
 
 
 
 
 
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
 
 
 
 
 
 
 
 
 
 
78
79
80
81
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
84
85
 
 
 
 
 
86
...
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
0
@@ -40,47 +40,56 @@ class ActiveSchemaTest < ActiveRecord::TestCase
0
   end
0
 
0
   def test_add_timestamps 
0
-    #we need to actually modify some data, so we make execute to point to the original method
0
-    ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do      
0
-      alias_method :execute_with_stub, :execute
0
-      alias_method :execute, :execute_without_stub
0
-    end  
0
-    ActiveRecord::Base.connection.create_table :delete_me do |t|        
0
-    end
0
-    ActiveRecord::Base.connection.add_timestamps :delete_me
0
-    assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='updated_at' AND TYPE='datetime'").num_rows, 1
0
-    assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='created_at' AND TYPE='datetime'").num_rows, 1
0
-  ensure    
0
-    ActiveRecord::Base.connection.drop_table :delete_me rescue nil  
0
-    #before finishing, we restore the alias to the mock-up method
0
-    ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do      
0
-      alias_method :execute, :execute_with_stub
0
+    with_real_execute do
0
+      begin
0
+        ActiveRecord::Base.connection.create_table :delete_me do |t|
0
+        end
0
+        ActiveRecord::Base.connection.add_timestamps :delete_me
0
+        assert column_present?('delete_me', 'updated_at', 'datetime')
0
+        assert column_present?('delete_me', 'created_at', 'datetime')
0
+      ensure
0
+        ActiveRecord::Base.connection.drop_table :delete_me rescue nil
0
+      end
0
     end
0
   end
0
   
0
   def test_remove_timestamps 
0
-    #we need to actually modify some data, so we make execute to point to the original method
0
-    ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do      
0
-      alias_method :execute_with_stub, :execute
0
-      alias_method :execute, :execute_without_stub
0
-    end  
0
-    ActiveRecord::Base.connection.create_table :delete_me do |t|        
0
-      t.timestamps
0
-    end
0
-    ActiveRecord::Base.connection.remove_timestamps :delete_me
0
-    assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='updated_at' AND TYPE='datetime'").num_rows, 0
0
-    assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='created_at' AND TYPE='datetime'").num_rows, 0
0
-  ensure    
0
-    ActiveRecord::Base.connection.drop_table :delete_me rescue nil  
0
-    #before finishing, we restore the alias to the mock-up method
0
-    ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do      
0
-      alias_method :execute, :execute_with_stub
0
+    with_real_execute do
0
+      begin
0
+        ActiveRecord::Base.connection.create_table :delete_me do |t|
0
+          t.timestamps
0
+        end
0
+        ActiveRecord::Base.connection.remove_timestamps :delete_me
0
+        assert !column_present?('delete_me', 'updated_at', 'datetime')
0
+        assert !column_present?('delete_me', 'created_at', 'datetime')
0
+      ensure
0
+        ActiveRecord::Base.connection.drop_table :delete_me rescue nil
0
+      end
0
     end
0
   end
0
 
0
-
0
   private
0
+    def with_real_execute
0
+      #we need to actually modify some data, so we make execute point to the original method
0
+      ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
0
+        alias_method :execute_with_stub, :execute
0
+        alias_method :execute, :execute_without_stub
0
+      end
0
+      yield
0
+    ensure
0
+      #before finishing, we restore the alias to the mock-up method
0
+      ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
0
+        alias_method :execute, :execute_with_stub
0
+      end
0
+    end
0
+
0
+
0
     def method_missing(method_symbol, *arguments)
0
       ActiveRecord::Base.connection.send(method_symbol, *arguments)
0
     end
0
+
0
+    def column_present?(table_name, column_name, type)
0
+      results = ActiveRecord::Base.connection.select_all("SHOW FIELDS FROM #{table_name} LIKE '#{column_name}'")
0
+      results.first && results.first['Type'] == type
0
+    end
0
 end

Comments