Skip to content

Commit

Permalink
Fix mysql 4.1 incompatibility in the active record schema tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#233 state:resolved ]
  • Loading branch information
fcheung authored and NZKoz committed May 21, 2008
1 parent 262d23d commit dd9938a
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions activerecord/test/cases/active_schema_test_mysql.rb
Expand Up @@ -40,47 +40,56 @@ def test_drop_table_with_specific_database
end

def test_add_timestamps
#we need to actually modify some data, so we make execute to point to the original method
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
alias_method :execute_with_stub, :execute
alias_method :execute, :execute_without_stub
end
ActiveRecord::Base.connection.create_table :delete_me do |t|
end
ActiveRecord::Base.connection.add_timestamps :delete_me
assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='updated_at' AND TYPE='datetime'").num_rows, 1
assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='created_at' AND TYPE='datetime'").num_rows, 1
ensure
ActiveRecord::Base.connection.drop_table :delete_me rescue nil
#before finishing, we restore the alias to the mock-up method
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
alias_method :execute, :execute_with_stub
with_real_execute do
begin
ActiveRecord::Base.connection.create_table :delete_me do |t|
end
ActiveRecord::Base.connection.add_timestamps :delete_me
assert column_present?('delete_me', 'updated_at', 'datetime')
assert column_present?('delete_me', 'created_at', 'datetime')
ensure
ActiveRecord::Base.connection.drop_table :delete_me rescue nil
end
end
end

def test_remove_timestamps
#we need to actually modify some data, so we make execute to point to the original method
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
alias_method :execute_with_stub, :execute
alias_method :execute, :execute_without_stub
end
ActiveRecord::Base.connection.create_table :delete_me do |t|
t.timestamps
end
ActiveRecord::Base.connection.remove_timestamps :delete_me
assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='updated_at' AND TYPE='datetime'").num_rows, 0
assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='created_at' AND TYPE='datetime'").num_rows, 0
ensure
ActiveRecord::Base.connection.drop_table :delete_me rescue nil
#before finishing, we restore the alias to the mock-up method
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
alias_method :execute, :execute_with_stub
with_real_execute do
begin
ActiveRecord::Base.connection.create_table :delete_me do |t|
t.timestamps
end
ActiveRecord::Base.connection.remove_timestamps :delete_me
assert !column_present?('delete_me', 'updated_at', 'datetime')
assert !column_present?('delete_me', 'created_at', 'datetime')
ensure
ActiveRecord::Base.connection.drop_table :delete_me rescue nil
end
end
end


private
def with_real_execute
#we need to actually modify some data, so we make execute point to the original method
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
alias_method :execute_with_stub, :execute
alias_method :execute, :execute_without_stub
end
yield
ensure
#before finishing, we restore the alias to the mock-up method
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
alias_method :execute, :execute_with_stub
end
end


def method_missing(method_symbol, *arguments)
ActiveRecord::Base.connection.send(method_symbol, *arguments)
end

def column_present?(table_name, column_name, type)
results = ActiveRecord::Base.connection.select_all("SHOW FIELDS FROM #{table_name} LIKE '#{column_name}'")
results.first && results.first['Type'] == type
end
end

0 comments on commit dd9938a

Please sign in to comment.