-
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 deployments in db (#2175)
Ref: SRX-5WTFDM
- Loading branch information
Showing
14 changed files
with
808 additions
and
928 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
database/migrations/postgres/1734008644556351_separate_deployments_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,68 @@ | ||
-- rename deployments table to deployments_history if it doesn't exist | ||
DO $$ | ||
BEGIN | ||
IF EXISTS ( | ||
SELECT FROM information_schema.tables | ||
WHERE table_schema = 'public' AND table_name = 'deployments' | ||
) AND NOT EXISTS ( | ||
SELECT FROM information_schema.tables | ||
WHERE table_schema = 'public' AND table_name = 'deployments_history' | ||
) THEN | ||
ALTER TABLE deployments RENAME TO deployments_history; | ||
END IF; | ||
END | ||
$$; | ||
-- create deployments table if it doesn't exist | ||
DROP INDEX IF EXISTS deployments_idx; | ||
DROP INDEX IF EXISTS deployments_version_idx; | ||
CREATE TABLE IF NOT EXISTS deployments( | ||
created timestamp without time zone, | ||
releaseversion bigint, | ||
appname varchar NOT NULL, | ||
envname varchar NOT NULL, | ||
metadata varchar, | ||
transformereslversion integer DEFAULT 0, | ||
PRIMARY KEY(appname,envname), | ||
CONSTRAINT fk_deployments_transformer_id FOREIGN key(transformereslversion) REFERENCES event_sourcing_light(eslversion) | ||
); | ||
CREATE INDEX IF NOT EXISTS deployments_idx ON deployments USING btree ("appname","envname"); | ||
CREATE INDEX IF NOT EXISTS deployments_version_idx ON deployments USING btree ("releaseversion","appname","envname"); | ||
|
||
-- insert data into deployments table from deployments_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 = 'deployments' | ||
) AND NOT EXISTS ( | ||
SELECT 1 FROM deployments LIMIT 1 | ||
) THEN | ||
INSERT INTO deployments (releaseVersion, created, appName, envname, metadata, transformereslversion) | ||
SELECT DISTINCT | ||
deployments_history.releaseVersion, | ||
deployments_history.created, | ||
deployments_history.appName, | ||
deployments_history.envname, | ||
deployments_history.metadata, | ||
deployments_history.transformereslversion | ||
FROM ( | ||
SELECT | ||
MAX(version) AS latestDeployment, | ||
appname, | ||
envname | ||
FROM | ||
"deployments_history" | ||
GROUP BY | ||
appname, envname) AS latest | ||
JOIN | ||
deployments_history AS deployments_history | ||
ON | ||
latest.latestDeployment=deployments_history.version | ||
AND latest.envname=deployments_history.envname | ||
AND latest.appname=deployments_history.appname; | ||
END IF; | ||
END | ||
$$; | ||
|
||
-- Remove all_deployments table | ||
DROP TABLE IF EXISTS all_deployments; |
29 changes: 29 additions & 0 deletions
29
database/migrations/sqlite/1734008644556351_separate_deployments_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,29 @@ | ||
CREATE TABLE IF NOT EXISTS deployments_history | ||
( | ||
created TIMESTAMP, | ||
releaseVersion BIGINT NULL, | ||
appName VARCHAR, | ||
envName VARCHAR, | ||
metadata VARCHAR, | ||
transformereslVersion INTEGER DEFAULT 0, | ||
version INTEGER PRIMARY KEY AUTOINCREMENT, | ||
FOREIGN KEY(transformereslVersion) REFERENCES event_sourcing_light(eslVersion) | ||
); | ||
|
||
INSERT INTO deployments_history (created, releaseversion, appname, envname, metadata, transformereslversion) | ||
SELECT created, releaseversion, appname, envname, metadata, transformereslversion | ||
FROM deployments | ||
ORDER BY version; | ||
DROP TABLE IF EXISTS deployments; | ||
DROP TABLE IF EXISTS all_deployments; | ||
CREATE TABLE IF NOT EXISTS deployments | ||
( | ||
created TIMESTAMP, | ||
releaseVersion BIGINT NULL, | ||
appName VARCHAR, | ||
envName VARCHAR, | ||
metadata VARCHAR, | ||
transformereslVersion INTEGER DEFAULT 0, | ||
PRIMARY KEY (appname, envname) | ||
FOREIGN KEY(transformereslVersion) REFERENCES event_sourcing_light(eslVersion) | ||
); |
Oops, something went wrong.