-
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): separate current from old environments in database (#2193)
Ref: SRX-7NCIGX * We move the previous things that happened to the environments to the environments_history table and only keep the current state in the environments table * The all_environments table is completely removed * All operations related to environments in database is moved from db.go to a new module db_environments.go
- Loading branch information
Showing
15 changed files
with
677 additions
and
675 deletions.
There are no files selected for viewing
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
73 changes: 73 additions & 0 deletions
73
database/migrations/postgres/1736501786775874_separate_envs_history.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,73 @@ | ||
-- rename environments table to environments_history if it doesn't exist | ||
DO $$ | ||
BEGIN | ||
IF EXISTS ( | ||
SELECT FROM information_schema.tables | ||
WHERE table_schema = 'public' AND table_name = 'environments' | ||
) AND NOT EXISTS ( | ||
SELECT FROM information_schema.tables | ||
WHERE table_schema = 'public' AND table_name = 'environments_history' | ||
) THEN | ||
ALTER TABLE environments RENAME TO environments_history; | ||
END IF; | ||
END | ||
$$; | ||
|
||
-- create apps table if it doesn't exist | ||
CREATE TABLE IF NOT EXISTS environments | ||
( | ||
created TIMESTAMP, | ||
name VARCHAR(255), | ||
json VARCHAR, | ||
applications VARCHAR, | ||
PRIMARY KEY(name) | ||
); | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS ( | ||
SELECT 1 | ||
FROM information_schema.table_constraints | ||
WHERE table_name = 'environments' | ||
AND constraint_type = 'PRIMARY KEY' | ||
) THEN | ||
ALTER TABLE environments | ||
ADD CONSTRAINT environments_pkey PRIMARY KEY (name); | ||
END IF; | ||
END $$; | ||
-- insert data into environments table from environments_history table if there's no data inside it | ||
DO $$ | ||
BEGIN | ||
IF EXISTS ( | ||
SELECT FROM information_schema.tables | ||
WHERE table_schema = 'public' AND table_name = 'environments' | ||
) AND NOT EXISTS ( | ||
SELECT 1 FROM environments LIMIT 1 | ||
) AND EXISTS ( | ||
SELECT FROM information_schema.tables | ||
WHERE table_schema = 'public' AND table_name = 'environments_history' | ||
) THEN | ||
INSERT INTO environments (created, name, json, applications) | ||
SELECT DISTINCT | ||
environments_history.created, | ||
environments_history.name, | ||
environments_history.json, | ||
environments_history.applications | ||
FROM ( | ||
SELECT | ||
MAX(version) AS latestVersion, | ||
name | ||
FROM | ||
"environments_history" | ||
GROUP BY | ||
name) AS latest | ||
JOIN | ||
environments_history AS environments_history | ||
ON | ||
latest.latestVersion=environments_history.version | ||
AND latest.name=environments_history.name; | ||
END IF; | ||
END | ||
$$; | ||
|
||
-- Remove all_environments table | ||
DROP TABLE IF EXISTS all_environments; |
24 changes: 24 additions & 0 deletions
24
database/migrations/sqlite/1736501786775874_separate_envs_history.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,24 @@ | ||
CREATE TABLE IF NOT EXISTS environments_history | ||
( | ||
created TIMESTAMP, | ||
name VARCHAR, | ||
json VARCHAR, | ||
applications VARCHAR, | ||
deleted BOOLEAN, | ||
version INTEGER PRIMARY KEY AUTOINCREMENT | ||
); | ||
|
||
INSERT INTO environments_history (created, name, json, applications, deleted) | ||
SELECT created, name, json, applications, deleted | ||
FROM environments | ||
ORDER BY version; | ||
DROP TABLE IF EXISTS environments; | ||
DROP TABLE IF EXISTS all_environments; | ||
CREATE TABLE IF NOT EXISTS environments | ||
( | ||
created TIMESTAMP, | ||
name VARCHAR, | ||
json VARCHAR, | ||
applications VARCHAR, | ||
PRIMARY KEY (name) | ||
); |
Oops, something went wrong.