From bca52b05af8ef757e9574f833217c0051ee21a50 Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Sun, 17 Jun 2012 17:39:12 +0200 Subject: db:drop and some of db:test:purge. --- .../lib/active_record/tasks/database_tasks.rb | 15 ++++++++++-- .../active_record/tasks/mysql_database_tasks.rb | 10 ++++++++ .../tasks/postgresql_database_tasks.rb | 28 +++++++++++++++++----- .../active_record/tasks/sqlite_database_tasks.rb | 10 ++++++++ 4 files changed, 55 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/tasks') diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 6b9115f214..1d70d6100c 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -7,11 +7,22 @@ class ActiveRecord::Tasks::DatabaseTasks def self.create(configuration) class_for_adapter(configuration['adapter']).new(configuration).create - rescue Exception => e - $stderr.puts e, *(e.backtrace) + rescue Exception => error + $stderr.puts error, *(error.backtrace) $stderr.puts "Couldn't create database for #{configuration.inspect}" end + def self.drop(configuration) + class_for_adapter(configuration['adapter']).new(configuration).drop + rescue Exception => error + $stderr.puts error, *(error.backtrace) + $stderr.puts "Couldn't drop #{configuration['database']}" + end + + def self.purge(configuration) + class_for_adapter(configuration['adapter']).new(configuration).purge + end + def self.class_for_adapter(adapter) key = TASKS_PATTERNS.keys.detect { |key| adapter[key] } TASKS_PATTERNS[key] diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index 16d606d6fb..a1d36f0855 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -27,6 +27,16 @@ class ActiveRecord::Tasks::MySQLDatabaseTasks $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['charset'] end + def drop + establish_connection configuration + connection.drop_database configuration['database'] + end + + def purge + establish_connection :test + connection.recreate_database configuration['database'], creation_options + end + private attr_reader :configuration diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb index 4e64a5e346..c3c5b2f2f5 100644 --- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb @@ -1,22 +1,31 @@ class ActiveRecord::Tasks::PostgreSQLDatabaseTasks DEFAULT_ENCODING = ENV['CHARSET'] || 'utf8' - delegate :connection, :establish_connection, :to => ActiveRecord::Base + delegate :connection, :establish_connection, :clear_active_connections!, + :to => ActiveRecord::Base def initialize(configuration) @configuration = configuration end - def create - establish_connection configuration.merge( - 'database' => 'postgres', - 'schema_search_path' => 'public' - ) + def create(master_established = false) + establish_master_connection unless master_established connection.create_database configuration['database'], configuration.merge('encoding' => encoding) establish_connection configuration end + def drop + establish_master_connection + connection.drop_database configuration['database'] + end + + def purge + clear_active_connections! + drop + create true + end + private attr_reader :configuration @@ -24,4 +33,11 @@ class ActiveRecord::Tasks::PostgreSQLDatabaseTasks def encoding configuration['encoding'] || DEFAULT_ENCODING end + + def establish_master_connection + establish_connection configuration.merge( + 'database' => 'postgres', + 'schema_search_path' => 'public' + ) + end end diff --git a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb index dd43414d79..fff763421b 100644 --- a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb @@ -15,6 +15,16 @@ class ActiveRecord::Tasks::SQLiteDatabaseTasks connection end + def drop + require 'pathname' + path = Pathname.new configuration['database'] + file = path.absolute? ? path.to_s : File.join(Rails.root, path) + + FileUtils.rm(file) + end + + alias :purge :drop + private attr_reader :configuration -- cgit v1.2.3