Skip to content
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

fix(db): separate current from old deployments in db #2175

Merged
merged 16 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
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)
);
Loading
Loading