-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explicitly manage schema upgrade transactions (#4096)
- Previously, schema updates encouraged authors on each change to add their own transactions, validating that the "current" and "target" versions are correct. - This unfortunately is not handled particularly well in scripted SQL. I **incorrectly** thought that failing a transaction while `batch_execute`-ing it (e.g., via a `CAST` error) would cause the transaction to fail, and rollback. **This is not true**. In CockroachDB, an error is thrown, but the transaction is not closed. This was the cause of #4093 , where connections stuck in this mangled ongoing transaction state were placed back into the connection pool. - To fix this: Nexus now explicitly wraps each schema change in a transaction using Diesel, which ensures that "on success, they're committed, and on failure, they're rolled back"). - Additionally, this PR upgrades all existing schema changes to conform to this "implied transaction from Nexus" policy, and makes it possible to upgrade using multiple transactions in a single version change. Fixes #4093
- Loading branch information
Showing
17 changed files
with
148 additions
and
219 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,2 @@ | ||
-- CRDB documentation recommends the following: | ||
-- "Execute schema changes either as single statements (as an implicit transaction), | ||
-- or in an explicit transaction consisting of the single schema change statement." | ||
-- | ||
-- For each schema change, we transactionally: | ||
-- 1. Check the current version | ||
-- 2. Apply the idempotent update | ||
|
||
BEGIN; | ||
|
||
SELECT CAST( | ||
IF( | ||
( | ||
SELECT version = '1.0.0' and target_version = '2.0.0' | ||
FROM omicron.public.db_metadata WHERE singleton = true | ||
), | ||
'true', | ||
'Invalid starting version for schema change' | ||
) AS BOOL | ||
); | ||
|
||
ALTER TABLE omicron.public.instance | ||
ADD COLUMN IF NOT EXISTS boot_on_fault BOOL NOT NULL DEFAULT false; | ||
|
||
COMMIT; |
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 |
---|---|---|
@@ -1,36 +1,13 @@ | ||
-- CRDB documentation recommends the following: | ||
-- "Execute schema changes either as single statements (as an implicit transaction), | ||
-- or in an explicit transaction consisting of the single schema change statement." | ||
-- | ||
-- For each schema change, we transactionally: | ||
-- 1. Check the current version | ||
-- 2. Apply the idempotent update | ||
|
||
BEGIN; | ||
|
||
SELECT CAST( | ||
IF( | ||
( | ||
SELECT version = '2.0.0' and target_version = '3.0.0' | ||
FROM omicron.public.db_metadata WHERE singleton = true | ||
), | ||
'true', | ||
'Invalid starting version for schema change' | ||
) AS BOOL | ||
); | ||
|
||
ALTER TABLE omicron.public.ip_pool | ||
ADD COLUMN IF NOT EXISTS silo_ID UUID, | ||
ADD COLUMN IF NOT EXISTS project_id UUID, | ||
|
||
-- if silo_id is null, then project_id must be null | ||
ADD CONSTRAINT IF NOT EXISTS project_implies_silo CHECK ( | ||
NOT ((silo_id IS NULL) AND (project_id IS NOT NULL)) | ||
), | ||
|
||
-- if internal = true, non-null silo_id and project_id are not allowed | ||
-- if internal = true, non-null silo_id and project_id are not allowed | ||
ADD CONSTRAINT IF NOT EXISTS internal_pools_have_null_silo_and_project CHECK ( | ||
NOT (INTERNAL AND ((silo_id IS NOT NULL) OR (project_id IS NOT NULL))) | ||
); | ||
|
||
COMMIT; |
Oops, something went wrong.