Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

A beginning of sorts.

Minimal implementation that supports db:create SQLite replacement
  • Loading branch information...
commit 619453d594187bc53b4c20d57e7821d8c166b71b 1 parent 08477a6
@pat pat authored
View
6 activerecord/lib/active_record.rb
@@ -137,6 +137,12 @@ module Scoping
end
end
+ module Tasks
+ extend ActiveSupport::Autoload
+
+ autoload :DatabaseTasks
+ end
+
autoload :TestCase
autoload :TestFixtures, 'active_record/fixtures'
end
View
13 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)
View
14 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
View
53 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
Please sign in to comment.
Something went wrong with that request. Please try again.