give up on dropping and recreating db for teardown and setup.

This commit is contained in:
Micah Geisel 2018-05-05 11:37:21 -07:00
parent 911f99d197
commit 9f2d7d2b5e
6 changed files with 18 additions and 30 deletions

View file

@ -30,3 +30,5 @@ sqlite3:
database: db/test.sqlite3
pool: 5
timeout: 5000
encoding: utf8

View file

@ -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

View file

@ -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"

View file

@ -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"

View file

@ -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

View file

@ -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