-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use a sequence for the verification number on postgres
- Loading branch information
Showing
5 changed files
with
101 additions
and
38 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
db/migrations/20201023_create_verification_number_sequence.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require_relative 'migration_helper' | ||
|
||
Sequel.migration do | ||
up do | ||
if PactBroker::MigrationHelper.postgres? | ||
row = from(:verification_sequence_number).select(:value).limit(1).first | ||
start = row ? row[:value] + 1 : 1 | ||
run("CREATE SEQUENCE verification_number_sequence START WITH #{start}") | ||
end | ||
end | ||
|
||
down do | ||
if PactBroker::MigrationHelper.postgres? | ||
nextval = execute("SELECT nextval('verification_number_sequence') as val") { |v| v.first["val"].to_i } | ||
# Add a safety margin! | ||
from(:verification_sequence_number).update(value: nextval + 100) | ||
run("DROP SEQUENCE verification_number_sequence") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters