DEV: Less hacky way of rolling back DB changes

Some specs use psql to test database restores and dropping the table after the test needs to happen outside of rspec because of transactions. The previous attempt lead to some changes to be stored in the test database.
This commit is contained in:
Gerhard Schlager 2020-01-15 23:37:21 +01:00
parent 68a7ae3091
commit 5e3fc31f2c
2 changed files with 4 additions and 13 deletions

View File

@ -91,10 +91,10 @@ module BackupRestore
end
def restore_dump_command
"#{sed_command} #{@db_dump_path} | #{psql_command} 2>&1"
"#{sed_command} #{@db_dump_path} | #{self.class.psql_command} 2>&1"
end
def psql_command
def self.psql_command
db_conf = BackupRestore.database_configuration
password_argument = "PGPASSWORD='#{db_conf.password}'" if db_conf.password.present?

View File

@ -69,17 +69,8 @@ describe BackupRestore::DatabaseRestorer do
context "with real psql" do
after do
DB.exec <<~SQL
-- Drop table and execute a commit to make the drop stick,
-- otherwise rspec will rollback the drop at the end of each test.
-- The tests in this context do not change the DB, so this should be safe.
DROP TABLE IF EXISTS foo;
COMMIT;
-- Start a new transaction in order to suppress the
-- "there is no transaction in progress" warnings from rspec.
BEGIN TRANSACTION;
SQL
psql = BackupRestore::DatabaseRestorer.psql_command
system("#{psql} -c 'DROP TABLE IF EXISTS foo'", [:out, :err] => File::NULL)
end
def restore(filename, stub_migrate: true)