1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/helper.rb
Piotr Sarnacki 75f8ac6ea7 Implemented ActiveRecord::Migrations#copy based on James Adam's idea
ActiveRecord::Migration#copy allows to copy migrations from one place
to another, changing migrations versions and adding scope to filename.

For example:
ActiveRecord::Migration.copy("db/migrate",
        :blog_engine => "vendor/gems/blog/db/migrate")

will copy all migrations from vendor/gems/blog/db/migrate to db/migrate
with such format:

Versions of copied migrations will be reversioned to be appended after
migrations that already exists in db/migrate
2010-09-03 22:59:09 +02:00

103 lines
2.7 KiB
Ruby

require File.expand_path('../../../../load_paths', __FILE__)
lib = File.expand_path("#{File.dirname(__FILE__)}/../../lib")
$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib)
require 'config'
require 'test/unit'
require 'stringio'
require 'mocha'
require 'active_record'
require 'active_support/dependencies'
require 'connection'
begin
require 'ruby-debug'
rescue LoadError
end
# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
# Quote "type" if it's a reserved word for the current connection.
QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type')
def current_adapter?(*types)
types.any? do |type|
ActiveRecord::ConnectionAdapters.const_defined?(type) &&
ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters.const_get(type))
end
end
ActiveRecord::Base.connection.class.class_eval do
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /SHOW FIELDS/]
def execute_with_query_record(sql, name = nil, &block)
$queries_executed ||= []
$queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r }
execute_without_query_record(sql, name, &block)
end
alias_method_chain :execute, :query_record
end
unless ENV['FIXTURE_DEBUG']
module ActiveRecord::TestFixtures::ClassMethods
def try_to_load_dependency_with_silence(*args)
ActiveRecord::Base.logger.silence { try_to_load_dependency_without_silence(*args)}
end
alias_method_chain :try_to_load_dependency, :silence
end
end
require "cases/validations_repair_helper"
class ActiveSupport::TestCase
include ActiveRecord::TestFixtures
include ActiveRecord::ValidationsRepairHelper
self.fixture_path = FIXTURES_ROOT
self.use_instantiated_fixtures = false
self.use_transactional_fixtures = true
def create_fixtures(*table_names, &block)
Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block)
end
end
# silence verbose schema loading
original_stdout = $stdout
$stdout = StringIO.new
begin
adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
adapter_specific_schema_file = SCHEMA_ROOT + "/#{adapter_name}_specific_schema.rb"
load SCHEMA_ROOT + "/schema.rb"
if File.exists?(adapter_specific_schema_file)
load adapter_specific_schema_file
end
ensure
$stdout = original_stdout
end
class << Time
unless method_defined? :now_before_time_travel
alias_method :now_before_time_travel, :now
end
def now
(@now ||= nil) || now_before_time_travel
end
def travel_to(time, &block)
@now = time
block.call
ensure
@now = nil
end
end