-
Notifications
You must be signed in to change notification settings - Fork 979
feat: run all migrations in a transaction #10966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b49ee44
feat: run all migrations in a transaction
coadler 31524f3
fix migrations
coadler 785ff59
lint
coadler 9a87f47
fmt
coadler d9b7db6
revert
coadler 307b999
add comments
coadler 0cb53fd
hold migration lock on migration table creation
coadler 480b99d
remove comment
coadler 3ca5ebf
document the correct way to create a new enum value
coadler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 +1,20 @@ | ||
ALTER TYPE login_type ADD VALUE IF NOT EXISTS 'token'; | ||
CREATE TYPE new_logintype AS ENUM ( | ||
'password', | ||
'github', | ||
'oidc', | ||
'token' | ||
); | ||
|
||
ALTER TABLE users | ||
ALTER COLUMN login_type DROP DEFAULT, | ||
ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype), | ||
ALTER COLUMN login_type SET DEFAULT 'password'::new_logintype; | ||
|
||
ALTER TABLE user_links | ||
ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype); | ||
|
||
ALTER TABLE api_keys | ||
ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype); | ||
|
||
DROP TYPE login_type; | ||
ALTER TYPE new_logintype RENAME TO login_type; |
This file contains hidden or 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,8 +1,4 @@ | ||
BEGIN; | ||
|
||
DROP TABLE group_members; | ||
DROP TABLE groups; | ||
ALTER TABLE templates DROP COLUMN group_acl; | ||
ALTER TABLE templates DROP COLUMN user_acl; | ||
|
||
COMMIT; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 +1 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE groups DROP COLUMN avatar_url; | ||
|
||
COMMIT; |
This file contains hidden or 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,5 +1 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE groups ADD COLUMN avatar_url text NOT NULL DEFAULT ''; | ||
|
||
COMMIT; |
This file contains hidden or 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,5 +1 @@ | ||
BEGIN; | ||
|
||
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'group'; | ||
|
||
COMMIT; | ||
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'group'; | ||
This file contains hidden or 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 hidden or 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 hidden or 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,9 +1,5 @@ | ||
BEGIN; | ||
|
||
-- rename column "name" to "display_name" on "workspace_apps" | ||
ALTER TABLE "workspace_apps" RENAME COLUMN "name" TO "display_name"; | ||
|
||
-- drop constraint "workspace_apps_agent_id_name_key" on "workspace_apps". | ||
ALTER TABLE ONLY workspace_apps DROP CONSTRAINT IF EXISTS workspace_apps_agent_id_name_key; | ||
|
||
COMMIT; |
This file contains hidden or 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
4 changes: 0 additions & 4 deletions
4
.../database/migrations/000072_add_agent_connection_timeout_and_troubleshooting_url.down.sql
This file contains hidden or 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,9 +1,5 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE workspace_agents | ||
DROP COLUMN connection_timeout_seconds; | ||
|
||
ALTER TABLE workspace_agents | ||
DROP COLUMN troubleshooting_url; | ||
|
||
COMMIT; |
This file contains hidden or 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
2 changes: 0 additions & 2 deletions
2
coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.down.sql
This file contains hidden or 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,5 +1,3 @@ | ||
BEGIN; | ||
ALTER TABLE parameter_schemas ALTER COLUMN default_source_scheme DROP NOT NULL; | ||
|
||
ALTER TABLE parameter_schemas ALTER COLUMN default_destination_scheme DROP NOT NULL; | ||
COMMIT; |
2 changes: 0 additions & 2 deletions
2
coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.up.sql
This file contains hidden or 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,7 +1,5 @@ | ||
BEGIN; | ||
UPDATE parameter_schemas SET default_source_scheme = 'none' WHERE default_source_scheme IS NULL; | ||
ALTER TABLE parameter_schemas ALTER COLUMN default_source_scheme SET NOT NULL; | ||
|
||
UPDATE parameter_schemas SET default_destination_scheme = 'none' WHERE default_destination_scheme IS NULL; | ||
ALTER TABLE parameter_schemas ALTER COLUMN default_destination_scheme SET NOT NULL; | ||
COMMIT; |
2 changes: 0 additions & 2 deletions
2
.../000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.down.sql
This file contains hidden or 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,8 +1,6 @@ | ||
BEGIN; | ||
ALTER TABLE workspace_agents RENAME COLUMN login_before_ready TO delay_login_until_ready; | ||
ALTER TABLE workspace_agents ALTER COLUMN delay_login_until_ready SET DEFAULT false; | ||
|
||
UPDATE workspace_agents SET delay_login_until_ready = NOT delay_login_until_ready; | ||
|
||
COMMENT ON COLUMN workspace_agents.delay_login_until_ready IS 'If true, the agent will delay logins until it is ready (e.g. executing startup script has ended).'; | ||
COMMIT; |
2 changes: 0 additions & 2 deletions
2
...ns/000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.up.sql
This file contains hidden or 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,8 +1,6 @@ | ||
BEGIN; | ||
ALTER TABLE workspace_agents RENAME COLUMN delay_login_until_ready TO login_before_ready; | ||
ALTER TABLE workspace_agents ALTER COLUMN login_before_ready SET DEFAULT true; | ||
|
||
UPDATE workspace_agents SET login_before_ready = NOT login_before_ready; | ||
|
||
COMMENT ON COLUMN workspace_agents.login_before_ready IS 'If true, the agent will not prevent login before it is ready (e.g. startup script is still executing).'; | ||
COMMIT; |
4 changes: 0 additions & 4 deletions
4
coderd/database/migrations/000096_agent_resolved_directory.down.sql
This file contains hidden or 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,6 +1,2 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE ONLY workspace_agents | ||
DROP COLUMN IF EXISTS expanded_directory; | ||
|
||
COMMIT; |
4 changes: 0 additions & 4 deletions
4
coderd/database/migrations/000096_agent_resolved_directory.up.sql
This file contains hidden or 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,9 +1,5 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE ONLY workspace_agents | ||
ADD COLUMN IF NOT EXISTS expanded_directory varchar(4096) DEFAULT '' NOT NULL; | ||
|
||
COMMENT ON COLUMN workspace_agents.expanded_directory | ||
IS 'The resolved path of a user-specified directory. e.g. ~/coder -> /home/coder/coder'; | ||
|
||
COMMIT; |
4 changes: 0 additions & 4 deletions
4
coderd/database/migrations/000097_license_not_null_uuid.up.sql
This file contains hidden or 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,8 +1,4 @@ | ||
BEGIN; | ||
|
||
-- We need to assign uuids to any existing licenses that don't have them. | ||
UPDATE licenses SET uuid = gen_random_uuid() WHERE uuid IS NULL; | ||
-- Assert no licenses have null uuids. | ||
ALTER TABLE ONLY licenses ALTER COLUMN uuid SET NOT NULL; | ||
|
||
COMMIT; |
This file contains hidden or 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,8 +1,4 @@ | ||
BEGIN; | ||
|
||
DROP INDEX idx_api_key_name; | ||
|
||
ALTER TABLE ONLY api_keys | ||
DROP COLUMN IF EXISTS token_name; | ||
|
||
COMMIT; |
This file contains hidden or 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 hidden or 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 hidden or 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,4 +1 @@ | ||
BEGIN; | ||
DROP TABLE workspace_proxies; | ||
|
||
COMMIT; |
This file contains hidden or 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
4 changes: 0 additions & 4 deletions
4
coderd/database/migrations/000118_workspace_proxy_token.down.sql
This file contains hidden or 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,6 +1,2 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE workspace_proxies | ||
DROP COLUMN token_hashed_secret; | ||
|
||
COMMIT; |
This file contains hidden or 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
4 changes: 0 additions & 4 deletions
4
coderd/database/migrations/000119_workspace_proxy_name_idx.down.sql
This file contains hidden or 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,8 +1,4 @@ | ||
BEGIN; | ||
|
||
DROP INDEX IF EXISTS workspace_proxies_lower_name_idx; | ||
|
||
-- Enforces no active proxies have the same name. | ||
CREATE UNIQUE INDEX ON workspace_proxies (name) WHERE deleted = FALSE; | ||
|
||
COMMIT; |
4 changes: 0 additions & 4 deletions
4
coderd/database/migrations/000119_workspace_proxy_name_idx.up.sql
This file contains hidden or 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,11 +1,7 @@ | ||
BEGIN; | ||
|
||
-- No one is using this feature yet as of writing this migration, so this is | ||
-- fine. Just delete all workspace proxies to prevent the new index from having | ||
-- conflicts. | ||
DELETE FROM workspace_proxies; | ||
|
||
DROP INDEX IF EXISTS workspace_proxies_name_idx; | ||
CREATE UNIQUE INDEX workspace_proxies_lower_name_idx ON workspace_proxies USING btree (lower(name)) WHERE deleted = FALSE; | ||
|
||
COMMIT; |
4 changes: 0 additions & 4 deletions
4
coderd/database/migrations/000120_trigger_delete_user_apikey.down.sql
This file contains hidden or 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,9 +1,5 @@ | ||
BEGIN; | ||
|
||
DROP TRIGGER IF EXISTS trigger_update_users ON users; | ||
DROP FUNCTION IF EXISTS delete_deleted_user_api_keys; | ||
|
||
DROP TRIGGER IF EXISTS trigger_insert_apikeys ON api_keys; | ||
DROP FUNCTION IF EXISTS insert_apikey_fail_if_user_deleted; | ||
|
||
COMMIT; |
This file contains hidden or 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
2 changes: 0 additions & 2 deletions
2
coderd/database/migrations/000122_add_template_cleanup_ttls.down.sql
This file contains hidden or 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,4 +1,2 @@ | ||
BEGIN; | ||
ALTER TABLE ONLY templates DROP COLUMN IF EXISTS failure_ttl; | ||
ALTER TABLE ONLY templates DROP COLUMN IF EXISTS inactivity_ttl; | ||
COMMIT; |
2 changes: 0 additions & 2 deletions
2
coderd/database/migrations/000122_add_template_cleanup_ttls.up.sql
This file contains hidden or 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,4 +1,2 @@ | ||
BEGIN; | ||
ALTER TABLE ONLY templates ADD COLUMN IF NOT EXISTS failure_ttl BIGINT NOT NULL DEFAULT 0; | ||
ALTER TABLE ONLY templates ADD COLUMN IF NOT EXISTS inactivity_ttl BIGINT NOT NULL DEFAULT 0; | ||
COMMIT; |
2 changes: 0 additions & 2 deletions
2
coderd/database/migrations/000123_workspace_agent_subsystem.down.sql
This file contains hidden or 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,4 +1,2 @@ | ||
BEGIN; | ||
ALTER TABLE workspace_agents DROP COLUMN subsystem; | ||
DROP TYPE workspace_agent_subsystem; | ||
COMMIT; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also do the new table + rename dance here as well?
Seems safer to just always do it that way instead of someone suddenly adding a new migration referencing this enum type and wondering why their new migration doesn't work.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only went back historically and fixed usage of
ADD VALUE
if we reference the newly added value later in a different migration. It is otherwise fine to use within a transaction.Edit: Ah sorry I didn't fully read. I see what you mean but I wasn't sure if it was worth going back to every type. It was really a pain to try and figure out for each enum what the exact values were at the time of the migration. I don't really mind following up though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix all of them in a follow up 👍