mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
Updates coder/customers#365 This PR updates our migration framework to run all migrations in a single transaction. This is the same behavior we had in v1 and ensures that failed migrations don't bring the whole deployment down. If a migration fails now, it will automatically be rolled back to the previous version, allowing the deployment to continue functioning.
40 lines
1.2 KiB
SQL
40 lines
1.2 KiB
SQL
-- Before dropping this table, we need to check if there exist any
|
|
-- foreign key references to it. We do this by checking the following:
|
|
-- user_links.oauth_access_token_key_id
|
|
-- user_links.oauth_refresh_token_key_id
|
|
-- git_auth_links.oauth_access_token_key_id
|
|
-- git_auth_links.oauth_refresh_token_key_id
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT *
|
|
FROM user_links
|
|
WHERE oauth_access_token_key_id IS NOT NULL
|
|
OR oauth_refresh_token_key_id IS NOT NULL
|
|
) THEN RAISE EXCEPTION 'Cannot drop dbcrypt_keys table as there are still foreign key references to it from user_links.';
|
|
END IF;
|
|
|
|
IF EXISTS (
|
|
SELECT *
|
|
FROM git_auth_links
|
|
WHERE oauth_access_token_key_id IS NOT NULL
|
|
OR oauth_refresh_token_key_id IS NOT NULL
|
|
) THEN RAISE EXCEPTION 'Cannot drop dbcrypt_keys table as there are still foreign key references to it from git_auth_links.';
|
|
END IF;
|
|
|
|
END
|
|
$$;
|
|
|
|
|
|
-- Drop the columns first.
|
|
ALTER TABLE git_auth_links
|
|
DROP COLUMN IF EXISTS oauth_access_token_key_id,
|
|
DROP COLUMN IF EXISTS oauth_refresh_token_key_id;
|
|
|
|
ALTER TABLE user_links
|
|
DROP COLUMN IF EXISTS oauth_access_token_key_id,
|
|
DROP COLUMN IF EXISTS oauth_refresh_token_key_id;
|
|
|
|
-- Finally, drop the table.
|
|
DROP TABLE IF EXISTS dbcrypt_keys;
|