Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/postal/message_db/provisioner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ def create_table(table_name, options)
end

#
# Drop a table
# Drop a table. Uses IF EXISTS so that two workers running retention
# concurrently do not crash when the second one tries to drop a table
# the first has already removed.
#
def drop_table(table_name)
@database.query("DROP TABLE `#{@database.database_name}`.`#{table_name}`")
@database.query("DROP TABLE IF EXISTS `#{@database.database_name}`.`#{table_name}`")
end

#
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/postal/message_db/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,15 @@
end.to raise_error(Mysql2::Error)
end
end

describe "#drop_table" do
# The retention task's lock can be stolen after a few minutes, so two
# workers may call drop_table for the same table. Dropping a table that
# has already been removed must not raise (regression for #3595).
it "does not raise when the table no longer exists" do
expect { database.provisioner.drop_table("raw-does-not-exist") }
.not_to raise_error
end
end
end
end