diff options
author | Pat Allan <pat@freelancing-gods.com> | 2012-06-17 01:44:29 +0200 |
---|---|---|
committer | Pat Allan <pat@freelancing-gods.com> | 2012-06-17 01:51:33 +0200 |
commit | 619453d594187bc53b4c20d57e7821d8c166b71b (patch) | |
tree | 3b6e5d2492d3c93a619699bef65be022823ae401 /activerecord | |
parent | 08477a651648ba4417ded128aa37b9ae0dcbc9ce (diff) | |
download | rails-619453d594187bc53b4c20d57e7821d8c166b71b.tar.gz rails-619453d594187bc53b4c20d57e7821d8c166b71b.tar.bz2 rails-619453d594187bc53b4c20d57e7821d8c166b71b.zip |
A beginning of sorts.
Minimal implementation that supports db:create SQLite replacement
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/railties/databases.rake | 13 | ||||
-rw-r--r-- | activerecord/lib/active_record/tasks/database_tasks.rb | 14 | ||||
-rw-r--r-- | activerecord/test/cases/sqlite_rake_test.rb | 53 |
4 files changed, 74 insertions, 12 deletions
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index f8526bb691..c6dc7db2e8 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -137,6 +137,12 @@ module ActiveRecord end end + module Tasks + extend ActiveSupport::Autoload + + autoload :DatabaseTasks + end + autoload :TestCase autoload :TestFixtures, 'active_record/fixtures' end diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index d8d4834d22..b44d479fb6 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -50,18 +50,7 @@ db_namespace = namespace :db do def create_database(config) begin if config['adapter'] =~ /sqlite/ - if File.exist?(config['database']) - $stderr.puts "#{config['database']} already exists" - else - begin - # Create the SQLite database - ActiveRecord::Base.establish_connection(config) - ActiveRecord::Base.connection - rescue Exception => e - $stderr.puts e, *(e.backtrace) - $stderr.puts "Couldn't create database for #{config.inspect}" - end - end + ActiveRecord::Tasks::DatabaseTasks.create config return # Skip the else clause of begin/rescue else ActiveRecord::Base.establish_connection(config) diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb new file mode 100644 index 0000000000..a72ec0175b --- /dev/null +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -0,0 +1,14 @@ +class ActiveRecord::Tasks::DatabaseTasks + def self.create(configuration) + if File.exist?(configuration['database']) + $stderr.puts "#{configuration['database']} already exists" + return + end + + ActiveRecord::Base.establish_connection(configuration) + ActiveRecord::Base.connection + rescue Exception => e + $stderr.puts e, *(e.backtrace) + $stderr.puts "Couldn't create database for #{configuration.inspect}" + end +end diff --git a/activerecord/test/cases/sqlite_rake_test.rb b/activerecord/test/cases/sqlite_rake_test.rb new file mode 100644 index 0000000000..069c168952 --- /dev/null +++ b/activerecord/test/cases/sqlite_rake_test.rb @@ -0,0 +1,53 @@ +require 'cases/helper' + +module ActiveRecord + class SqliteRakeTest < ActiveRecord::TestCase + def setup + @database = "db_create.sqlite3" + @connection = stub :connection + @configuration = { + 'adapter' => 'sqlite3', + 'database' => @database + } + + File.stubs(:exist?).returns(false) + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + end + + def test_db_checks_database_exists + File.expects(:exist?).with(@database).returns(false) + + ActiveRecord::Tasks::DatabaseTasks.create @configuration + end + + def test_db_create_when_file_exists + File.stubs(:exist?).returns(true) + + $stderr.expects(:puts).with("#{@database} already exists") + + ActiveRecord::Tasks::DatabaseTasks.create @configuration + end + + def test_db_create_establishes_a_connection + ActiveRecord::Base.expects(:establish_connection).with(@configuration) + + ActiveRecord::Tasks::DatabaseTasks.create @configuration + end + + def test_db_create_returns_the_connection + assert_equal ActiveRecord::Tasks::DatabaseTasks.create(@configuration), + @connection + end + + def test_db_create_with_error_prints_message + ActiveRecord::Base.stubs(:establish_connection).raises(Exception) + + $stderr.stubs(:puts).returns(true) + $stderr.expects(:puts). + with("Couldn't create database for #{@configuration.inspect}") + + ActiveRecord::Tasks::DatabaseTasks.create(@configuration) + end + end +end |