Permalink
Browse files

DRY database configuration and create test database automatically

  • Loading branch information...
1 parent 2b78bc3 commit c870ad4180d5327d59b68c83003272430373b6ae @andreychernih andreychernih committed Feb 7, 2013
Showing with 24 additions and 28 deletions.
  1. +1 −15 features/support/activerecord.rb
  2. +1 −13 spec/orm/activerecord_spec.rb
  3. +22 −0 spec/support/activerecord.rb
View
16 features/support/activerecord.rb
@@ -1,21 +1,7 @@
# encoding: utf-8
-require 'mysql2'
-
-require 'active_record'
require 'carrierwave/mount'
-require 'carrierwave/orm/activerecord'
-
-# Change this if MySQL is unavailable
-dbconfig = {
- :adapter => 'mysql2',
- :database => 'carrierwave_test',
- :username => 'root',
- :encoding => 'utf8'
-}
-
-ActiveRecord::Base.establish_connection(dbconfig)
-ActiveRecord::Migration.verbose = false
+require File.join(File.dirname(__FILE__), '..', '..', 'spec', 'support', 'activerecord')
class TestMigration < ActiveRecord::Migration
def self.up
View
14 spec/orm/activerecord_spec.rb
@@ -1,19 +1,7 @@
# encoding: utf-8
require 'spec_helper'
-
-require 'carrierwave/orm/activerecord'
-
-# Change this if MySQL is unavailable
-dbconfig = {
- :adapter => 'mysql2',
- :database => 'carrierwave_test',
- :username => 'root',
- :encoding => 'utf8'
-}
-
-ActiveRecord::Base.establish_connection(dbconfig)
-ActiveRecord::Migration.verbose = false
+require 'support/activerecord'
class TestMigration < ActiveRecord::Migration
def self.up
View
22 spec/support/activerecord.rb
@@ -0,0 +1,22 @@
+require 'mysql2'
+require 'active_record'
+require 'carrierwave/orm/activerecord'
+
+# Change this if MySQL is unavailable
+dbconfig = {
+ :adapter => 'mysql2',
+ :database => 'carrierwave_test',
+ :username => 'root',
+ :encoding => 'utf8'
+}
+
+database = dbconfig.delete(:database)
+
+ActiveRecord::Base.establish_connection(dbconfig)
+begin
+ ActiveRecord::Base.connection.create_database database
+rescue ActiveRecord::StatementInvalid => e # database already exists
+end
+ActiveRecord::Base.establish_connection(dbconfig.merge(:database => database))
+
+ActiveRecord::Migration.verbose = false

0 comments on commit c870ad4

Please sign in to comment.