Drop Postgres database with AR connection

1. Works faster than issuing shell commands.
2. Attempts to drop the database before creating it.  This fixes some
ugly test output which warns that given database already exists.
This commit is contained in:
Sebastian Skałacki 2016-03-31 03:13:04 +02:00
parent f318781a77
commit 6c59ff2bac

View file

@ -14,6 +14,7 @@ module PostgreSQLHelper
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8' @encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
begin begin
establish_connection(default_config.merge('database' => 'postgres', 'schema_search_path' => 'public')) establish_connection(default_config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.drop_database(default_config['database']) rescue nil
ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding)) ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding))
rescue Exception => e rescue Exception => e
$stderr.puts e, *(e.backtrace) $stderr.puts e, *(e.backtrace)
@ -32,7 +33,6 @@ module PostgreSQLHelper
end end
def active_record_pg_migrate def active_record_pg_migrate
`dropdb #{default_config['database']}`
create_db create_db
establish_connection establish_connection
ActiveRecord::Migrator.migrate 'spec/support/active_record/migrations' ActiveRecord::Migrator.migrate 'spec/support/active_record/migrations'