提交 4a370f9d 编写于 作者: K kennyj

Extract Firebird database tasks.

上级 196af50e
......@@ -143,6 +143,8 @@ module Tasks
autoload :MySQLDatabaseTasks, 'active_record/tasks/mysql_database_tasks'
autoload :PostgreSQLDatabaseTasks,
'active_record/tasks/postgresql_database_tasks'
autoload :FirebirdDatabaseTasks, 'active_record/tasks/firebird_database_tasks'
end
autoload :TestCase
......
......@@ -270,15 +270,6 @@ db_namespace = namespace :db do
end
namespace :structure do
def set_firebird_env(config)
ENV['ISC_USER'] = config['username'].to_s if config['username']
ENV['ISC_PASSWORD'] = config['password'].to_s if config['password']
end
def firebird_db_string(config)
FireRuby::Database.db_string_for(config.symbolize_keys)
end
desc 'Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql'
task :dump => [:environment, :load_config] do
filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
......@@ -289,10 +280,6 @@ db_namespace = namespace :db do
File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
when 'sqlserver'
`smoscript -s #{current_config['host']} -d #{current_config['database']} -u #{current_config['username']} -p #{current_config['password']} -f #{filename} -A -U`
when "firebird"
set_firebird_env(current_config)
db_string = firebird_db_string(current_config)
sh "isql -a #{db_string} > #{filename}"
else
ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename)
end
......@@ -317,10 +304,6 @@ db_namespace = namespace :db do
IO.read(filename).split(";\n\n").each do |ddl|
ActiveRecord::Base.connection.execute(ddl)
end
when 'firebird'
set_firebird_env(current_config)
db_string = firebird_db_string(current_config)
sh "isql -i #{filename} #{db_string}"
else
ActiveRecord::Tasks::DatabaseTasks.structure_load(current_config, filename)
end
......@@ -391,9 +374,6 @@ db_namespace = namespace :db do
ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
ActiveRecord::Base.connection.execute(ddl)
end
when 'firebird'
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.connection.recreate_database!
else
ActiveRecord::Tasks::DatabaseTasks.purge abcs['test']
end
......
......@@ -18,6 +18,7 @@ def register_task(pattern, task)
register_task(/mysql/, ActiveRecord::Tasks::MySQLDatabaseTasks)
register_task(/postgresql/, ActiveRecord::Tasks::PostgreSQLDatabaseTasks)
register_task(/sqlite/, ActiveRecord::Tasks::SQLiteDatabaseTasks)
register_task(/firebird/, ActiveRecord::Tasks::FirebirdDatabaseTasks)
def current_config(options = {})
options.reverse_merge! :env => Rails.env
......
module ActiveRecord
module Tasks # :nodoc:
class FirebirdDatabaseTasks # :nodoc:
delegate :connection, :establish_connection, to: ActiveRecord::Base
def initialize(configuration)
@configuration = configuration
end
def create
$stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
def drop
$stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
def purge
establish_connection(:test)
connection.recreate_database!
end
def charset
$stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end
def structure_dump(filename)
set_firebird_env(configuration)
db_string = firebird_db_string(configuration)
Kernel.system "isql -a #{db_string} > #{filename}"
end
def structure_load(filename)
set_firebird_env(configuration)
db_string = firebird_db_string(configuration)
Kernel.system "isql -i #{filename} #{db_string}"
end
private
def set_firebird_env(config)
ENV['ISC_USER'] = config['username'].to_s if config['username']
ENV['ISC_PASSWORD'] = config['password'].to_s if config['password']
end
def firebird_db_string(config)
FireRuby::Database.db_string_for(config.symbolize_keys)
end
def configuration
@configuration
end
end
end
end
require 'cases/helper'
unless defined?(FireRuby::Database)
module FireRuby
module Database; end
end
end
module ActiveRecord
module FirebirdSetupper
def setup
@database = 'db.firebird'
@connection = stub :connection
@configuration = {
'adapter' => 'firebird',
'database' => @database
}
ActiveRecord::Base.stubs(:connection).returns(@connection)
ActiveRecord::Base.stubs(:establish_connection).returns(true)
end
end
class FirebirdDBCreateTest < ActiveRecord::TestCase
include FirebirdSetupper
def test_db_retrieves_create
message = capture(:stderr) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
assert_match(/not supported/, message)
end
end
class FirebirdDBDropTest < ActiveRecord::TestCase
include FirebirdSetupper
def test_db_retrieves_drop
message = capture(:stderr) do
ActiveRecord::Tasks::DatabaseTasks.drop @configuration
end
assert_match(/not supported/, message)
end
end
class FirebirdDBCharsetAndCollationTest < ActiveRecord::TestCase
include FirebirdSetupper
def test_db_retrieves_collation
assert_raise NoMethodError do
ActiveRecord::Tasks::DatabaseTasks.collation @configuration
end
end
def test_db_retrieves_charset
message = capture(:stderr) do
ActiveRecord::Tasks::DatabaseTasks.charset @configuration
end
assert_match(/not supported/, message)
end
end
class FirebirdStructureDumpTest < ActiveRecord::TestCase
include FirebirdSetupper
def setup
super
FireRuby::Database.stubs(:db_string_for).returns(@database)
end
def test_structure_dump
filename = "filebird.sql"
Kernel.expects(:system).with("isql -a #{@database} > #{filename}")
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename)
end
end
class FirebirdStructureLoadTest < ActiveRecord::TestCase
include FirebirdSetupper
def setup
super
FireRuby::Database.stubs(:db_string_for).returns(@database)
end
def test_structure_load
filename = "firebird.sql"
Kernel.expects(:system).with("isql -i #{filename} #{@database}")
ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册