-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(db): add auto increment feature to environments table (#2166)
Ref: SRX-JXABF5
- Loading branch information
Showing
8 changed files
with
79 additions
and
60 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
database/migrations/postgres/1733867056189286_environments_autoincrement.up.sql
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,32 @@ | ||
ALTER TABLE IF EXISTS environments ADD COLUMN IF NOT EXISTS row_version INTEGER; | ||
DO $$ | ||
BEGIN | ||
IF EXISTS (SELECT 1 | ||
FROM information_schema.columns | ||
WHERE table_name = 'environments' | ||
AND column_name = 'version') THEN | ||
EXECUTE 'WITH ordered_rows AS ( | ||
SELECT version, name, ROW_NUMBER() OVER (ORDER BY version) AS row_num | ||
FROM environments | ||
) | ||
UPDATE environments | ||
SET row_version = ordered_rows.row_num | ||
FROM ordered_rows | ||
WHERE environments.version = ordered_rows.version AND environments.name = ordered_rows.name;'; | ||
END IF; | ||
END $$; | ||
|
||
DROP SEQUENCE IF EXISTS environments_version_seq CASCADE; | ||
CREATE SEQUENCE IF NOT EXISTS environments_version_seq OWNED BY environments.row_version; | ||
|
||
SELECT setval('environments_version_seq', coalesce(max(row_version), 0) + 1, false) FROM environments; | ||
|
||
ALTER TABLE IF EXISTS environments | ||
ALTER COLUMN row_version SET DEFAULT nextval('environments_version_seq'); | ||
|
||
ALTER TABLE IF EXISTS environments DROP CONSTRAINT IF EXISTS environments_pkey; | ||
|
||
ALTER TABLE IF EXISTS environments ADD PRIMARY KEY (row_version, name); | ||
|
||
ALTER TABLE IF EXISTS environments DROP COLUMN IF EXISTS version; | ||
ALTER TABLE IF EXISTS environments RENAME COLUMN row_version TO version; |
17 changes: 17 additions & 0 deletions
17
database/migrations/sqlite/1733867056189286_environments_autoincrement.up.sql
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,17 @@ | ||
CREATE TABLE IF NOT EXISTS environments_new | ||
( | ||
version INTEGER PRIMARY KEY AUTOINCREMENT, | ||
created TIMESTAMP, | ||
name VARCHAR(255), | ||
json VARCHAR, | ||
deleted bool DEFAULT false NOT NULL, | ||
applications VARCHAR | ||
); | ||
|
||
INSERT INTO environments_new (created, name, json, deleted, applications) | ||
SELECT created, name, json, deleted, applications | ||
FROM environments | ||
ORDER BY version; | ||
|
||
DROP TABLE IF EXISTS environments; | ||
ALTER TABLE environments_new RENAME TO environments; |
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
Oops, something went wrong.