FEATURE: Drop "backup" schema 7 days after restore

The "backup" schema is used to rollback a failed restore. It isn't useful after a longer period of time and turns into a waste of disk space.
This commit is contained in:
Gerhard Schlager
2020-01-15 23:19:13 +01:00
parent eeb2855a48
commit f216c6d60b
4 changed files with 102 additions and 0 deletions

View File

@ -67,6 +67,14 @@ describe BackupRestore::DatabaseRestorer do
subject.restore("foo.sql")
end
it "stores the date of the last restore" do
date_string = "2020-01-10T17:38:27Z"
freeze_time(Time.parse(date_string))
execute_stubbed_restore
expect(BackupMetadata.value_for(BackupMetadata::LAST_RESTORE_DATE)).to eq(date_string)
end
context "with real psql" do
after do
psql = BackupRestore::DatabaseRestorer.psql_command
@ -176,4 +184,53 @@ describe BackupRestore::DatabaseRestorer do
subject.clean_up
end
end
describe ".drop_backup_schema" do
subject { BackupRestore::DatabaseRestorer }
context "when no backup schema exists" do
it "doesn't do anything" do
ActiveRecord::Base.connection.expects(:schema_exists?).with("backup").returns(false)
ActiveRecord::Base.connection.expects(:drop_schema).never
subject.drop_backup_schema
end
end
context "when a backup schema exists" do
before do
ActiveRecord::Base.connection.expects(:schema_exists?).with("backup").returns(true)
end
it "drops the schema when the last restore was long ago" do
ActiveRecord::Base.connection.expects(:drop_schema).with("backup")
freeze_time(8.days.ago) do
subject.update_last_restore_date
end
subject.drop_backup_schema
end
it "doesn't drop the schema when the last restore was recently" do
ActiveRecord::Base.connection.expects(:drop_schema).with("backup").never
freeze_time(6.days.ago) do
subject.update_last_restore_date
end
subject.drop_backup_schema
end
it "stores the current date when there is no record of the last restore" do
ActiveRecord::Base.connection.expects(:drop_schema).with("backup").never
date_string = "2020-01-08T17:38:27Z"
freeze_time(Time.parse(date_string))
subject.drop_backup_schema
expect(BackupMetadata.value_for(BackupMetadata::LAST_RESTORE_DATE)).to eq(date_string)
end
end
end
end