From 9f2d7d2b5ecbeded32d4614ad979fa2dbfa5b592 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Sat, 5 May 2018 11:37:21 -0700 Subject: [PATCH] give up on dropping and recreating db for teardown and setup. --- db/sample.config.yml | 2 ++ spec/support/active_record/base_helper.rb | 10 +++++++++- spec/support/active_record/mysql2_helper.rb | 7 ------- spec/support/active_record/mysql_helper.rb | 7 ------- spec/support/active_record/postgresql_helper.rb | 10 ++-------- spec/support/active_record/sqlite3_helper.rb | 12 +++++------- 6 files changed, 18 insertions(+), 30 deletions(-) diff --git a/db/sample.config.yml b/db/sample.config.yml index ee1e55b..1a295a7 100644 --- a/db/sample.config.yml +++ b/db/sample.config.yml @@ -30,3 +30,5 @@ sqlite3: database: db/test.sqlite3 pool: 5 timeout: 5000 + encoding: utf8 + diff --git a/spec/support/active_record/base_helper.rb b/spec/support/active_record/base_helper.rb index e3beb94..b6fc5fa 100644 --- a/spec/support/active_record/base_helper.rb +++ b/spec/support/active_record/base_helper.rb @@ -20,7 +20,10 @@ class BaseHelper end def teardown - ActiveRecord::Base.connection.drop_database default_config['database'] + tables = %w(users agents) + tables.each do |table| + connection.execute "DROP TABLE IF EXISTS #{table}" + end end private @@ -28,5 +31,10 @@ class BaseHelper def establish_connection(config = default_config) ActiveRecord::Base.establish_connection(config) end + + def create_db + establish_connection default_config.merge("database" => nil) + connection.execute "CREATE DATABASE IF NOT EXISTS #{default_config['database']}" + end end diff --git a/spec/support/active_record/mysql2_helper.rb b/spec/support/active_record/mysql2_helper.rb index 5bbb27a..df7222f 100644 --- a/spec/support/active_record/mysql2_helper.rb +++ b/spec/support/active_record/mysql2_helper.rb @@ -14,13 +14,6 @@ class MySQL2Helper < BaseHelper db_config['mysql2'] end - def create_db - establish_connection(default_config.merge("database" => nil)) - - ActiveRecord::Base.connection.drop_database default_config['database'] rescue nil - ActiveRecord::Base.connection.create_database default_config['database'] - end - def patch_mysql2_adapter # remove DEFAULT NULL from column definition, which is an error on primary keys in MySQL 5.7.3+ ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY" diff --git a/spec/support/active_record/mysql_helper.rb b/spec/support/active_record/mysql_helper.rb index eb9b8c1..5f9b3ee 100644 --- a/spec/support/active_record/mysql_helper.rb +++ b/spec/support/active_record/mysql_helper.rb @@ -14,13 +14,6 @@ class MySQLHelper < BaseHelper db_config['mysql'] end - def create_db - ActiveRecord::Base.establish_connection default_config.merge("database" => nil) - ActiveRecord::Base.connection.drop_database default_config['database'] rescue nil - ActiveRecord::Base.connection.create_database default_config['database'] - ActiveRecord::Base.establish_connection default_config - end - def patch_mysql_adapter # remove DEFAULT NULL from column definition, which is an error on primary keys in MySQL 5.7.3+ ActiveRecord::ConnectionAdapters::MysqlAdapter::NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY" diff --git a/spec/support/active_record/postgresql_helper.rb b/spec/support/active_record/postgresql_helper.rb index 6dd16cf..e82d50a 100644 --- a/spec/support/active_record/postgresql_helper.rb +++ b/spec/support/active_record/postgresql_helper.rb @@ -3,11 +3,6 @@ require 'support/active_record/base_helper' class PostgreSQLHelper < BaseHelper puts "Active Record #{ActiveRecord::VERSION::STRING}, pg" - def teardown - ActiveRecord::Base.connection.execute "DROP TABLE users, agents;" - rescue ActiveRecord::StatementInvalid - end - private def default_config @@ -15,9 +10,8 @@ class PostgreSQLHelper < BaseHelper end def create_db - @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8' - establish_connection(default_config.merge('database' => 'postgres', 'schema_search_path' => 'public')) - ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding)) + establish_connection default_config.merge('database' => 'postgres', 'schema_search_path' => 'public') + connection.execute "CREATE DATABASE #{default_config['database']}" rescue ActiveRecord::StatementInvalid end end diff --git a/spec/support/active_record/sqlite3_helper.rb b/spec/support/active_record/sqlite3_helper.rb index 5471e8e..366b89d 100644 --- a/spec/support/active_record/sqlite3_helper.rb +++ b/spec/support/active_record/sqlite3_helper.rb @@ -3,20 +3,18 @@ require 'support/active_record/base_helper' class SQLite3Helper < BaseHelper puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3" - def teardown - ActiveRecord::Base.connection.truncate_table('users') - ActiveRecord::Base.connection.truncate_table('agents') - end - private def default_config db_config['sqlite3'] end + def establish_connection + super default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public') + end + def create_db - @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8' - establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public')) + # NO-OP end end