Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Firebird / Sqlserver / Oracle database tasks, and They should be deprecated. #9971

Merged
merged 8 commits into from
Apr 2, 2013
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* Extract and deprecate Firebird / Sqlserver / Oracle database tasks, because
These tasks should be supported by 3rd-party adapter.

*kennyj*

* Allow `ActiveRecord::Base.connection_handler` to have thread affinity and be * Allow `ActiveRecord::Base.connection_handler` to have thread affinity and be
settable, this effectively allows Active Record to be used in a multi threaded settable, this effectively allows Active Record to be used in a multi threaded
setup with multiple connections to multiple dbs. setup with multiple connections to multiple dbs.
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ module Tasks
autoload :MySQLDatabaseTasks, 'active_record/tasks/mysql_database_tasks' autoload :MySQLDatabaseTasks, 'active_record/tasks/mysql_database_tasks'
autoload :PostgreSQLDatabaseTasks, autoload :PostgreSQLDatabaseTasks,
'active_record/tasks/postgresql_database_tasks' 'active_record/tasks/postgresql_database_tasks'

autoload :FirebirdDatabaseTasks, 'active_record/tasks/firebird_database_tasks'
autoload :SqlserverDatabaseTasks, 'active_record/tasks/sqlserver_database_tasks'
autoload :OracleDatabaseTasks, 'active_record/tasks/oracle_database_tasks'
end end


autoload :TestCase autoload :TestCase
Expand Down
61 changes: 4 additions & 57 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -270,32 +270,11 @@ db_namespace = namespace :db do
end end


namespace :structure do 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' 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 task :dump => [:environment, :load_config] do
filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql") filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
current_config = ActiveRecord::Tasks::DatabaseTasks.current_config current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
case current_config['adapter'] ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename)
when 'oci', 'oracle'
ActiveRecord::Base.establish_connection(current_config)
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


if ActiveRecord::Base.connection.supports_migrations? if ActiveRecord::Base.connection.supports_migrations?
File.open(filename, "a") do |f| File.open(filename, "a") do |f|
Expand All @@ -307,23 +286,9 @@ db_namespace = namespace :db do


# desc "Recreate the databases from the structure.sql file" # desc "Recreate the databases from the structure.sql file"
task :load => [:environment, :load_config] do task :load => [:environment, :load_config] do
current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql") filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
case current_config['adapter'] current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
when 'sqlserver' ActiveRecord::Tasks::DatabaseTasks.structure_load(current_config, filename)
`sqlcmd -S #{current_config['host']} -d #{current_config['database']} -U #{current_config['username']} -P #{current_config['password']} -i #{filename}`
when 'oci', 'oracle'
ActiveRecord::Base.establish_connection(current_config)
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
end end


task :load_if_sql => ['db:create', :environment] do task :load_if_sql => ['db:create', :environment] do
Expand Down Expand Up @@ -378,25 +343,7 @@ db_namespace = namespace :db do


# desc "Empty the test database" # desc "Empty the test database"
task :purge => [:environment, :load_config] do task :purge => [:environment, :load_config] do
abcs = ActiveRecord::Base.configurations ActiveRecord::Tasks::DatabaseTasks.purge ActiveRecord::Base.configurations['test']
case abcs['test']['adapter']
when 'sqlserver'
test = abcs.deep_dup['test']
test_database = test['database']
test['database'] = 'master'
ActiveRecord::Base.establish_connection(test)
ActiveRecord::Base.connection.recreate_database!(test_database)
when "oci", "oracle"
ActiveRecord::Base.establish_connection(:test)
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
end end


# desc 'Check for pending migrations and load the test schema' # desc 'Check for pending migrations and load the test schema'
Expand Down
10 changes: 7 additions & 3 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ def register_task(pattern, task)
@tasks[pattern] = task @tasks[pattern] = task
end end


register_task(/mysql/, ActiveRecord::Tasks::MySQLDatabaseTasks) register_task(/mysql/, ActiveRecord::Tasks::MySQLDatabaseTasks)
register_task(/postgresql/, ActiveRecord::Tasks::PostgreSQLDatabaseTasks) register_task(/postgresql/, ActiveRecord::Tasks::PostgreSQLDatabaseTasks)
register_task(/sqlite/, ActiveRecord::Tasks::SQLiteDatabaseTasks) register_task(/sqlite/, ActiveRecord::Tasks::SQLiteDatabaseTasks)

register_task(/firebird/, ActiveRecord::Tasks::FirebirdDatabaseTasks)
register_task(/sqlserver/, ActiveRecord::Tasks::SqlserverDatabaseTasks)
register_task(/(oci|oracle)/, ActiveRecord::Tasks::OracleDatabaseTasks)


def current_config(options = {}) def current_config(options = {})
options.reverse_merge! :env => Rails.env options.reverse_merge! :env => Rails.env
Expand Down
56 changes: 56 additions & 0 deletions activerecord/lib/active_record/tasks/firebird_database_tasks.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,56 @@
module ActiveRecord
module Tasks # :nodoc:
class FirebirdDatabaseTasks # :nodoc:
delegate :connection, :establish_connection, to: ActiveRecord::Base

def initialize(configuration)
ActiveSupport::Deprecation.warn "This database tasks were deprecated, because this tasks should be served by the 3rd party adapter."
@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
45 changes: 45 additions & 0 deletions activerecord/lib/active_record/tasks/oracle_database_tasks.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,45 @@
module ActiveRecord
module Tasks # :nodoc:
class OracleDatabaseTasks # :nodoc:
delegate :connection, :establish_connection, to: ActiveRecord::Base

def initialize(configuration)
ActiveSupport::Deprecation.warn "This database tasks were deprecated, because this tasks should be served by the 3rd party adapter."
@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.structure_drop.split(";\n\n").each { |ddl| connection.execute(ddl) }
end

def charset
$stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
end

def structure_dump(filename)
establish_connection(configuration)
File.open(filename, "w:utf-8") { |f| f << connection.structure_dump }
end

def structure_load(filename)
establish_connection(configuration)
IO.read(filename).split(";\n\n").each { |ddl| connection.execute(ddl) }
end

private

def configuration
@configuration
end
end
end
end
48 changes: 48 additions & 0 deletions activerecord/lib/active_record/tasks/sqlserver_database_tasks.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'shellwords'

module ActiveRecord
module Tasks # :nodoc:
class SqlserverDatabaseTasks # :nodoc:
delegate :connection, :establish_connection, to: ActiveRecord::Base

def initialize(configuration)
ActiveSupport::Deprecation.warn "This database tasks were deprecated, because this tasks should be served by the 3rd party adapter."
@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
test = configuration.deep_dup
test_database = test['database']
test['database'] = 'master'
establish_connection(test)
connection.recreate_database!(test_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)
Kernel.system("smoscript -s #{configuration['host']} -d #{configuration['database']} -u #{configuration['username']} -p #{configuration['password']} -f #{filename} -A -U")
end

def structure_load(filename)
Kernel.system("sqlcmd -S #{configuration['host']} -d #{configuration['database']} -U #{configuration['username']} -P #{configuration['password']} -i #{filename}")
end

private

def configuration
@configuration
end
end
end
end
100 changes: 100 additions & 0 deletions activerecord/test/cases/tasks/firebird_rake_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,100 @@
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)

@tasks = Class.new(ActiveRecord::Tasks::FirebirdDatabaseTasks) do
def initialize(configuration)
ActiveSupport::Deprecation.silence { super }
end
end
ActiveRecord::Tasks::DatabaseTasks.stubs(:class_for_adapter).returns(@tasks) unless defined? ActiveRecord::ConnectionAdapters::FirebirdAdapter
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
Loading