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 state of the releases from releases in history #2170

Merged
merged 10 commits into from
Dec 17, 2024
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
ALTER TABLE IF EXISTS releases ADD COLUMN IF NOT EXISTS version INTEGER;
DO $$
BEGIN
sven-urbanski-freiheit-com marked this conversation as resolved.
Show resolved Hide resolved
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'releases'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS releases ADD COLUMN IF NOT EXISTS version INTEGER;
END IF;
END $$;
DO $$
BEGIN
IF EXISTS (SELECT 1
Expand All @@ -16,15 +24,55 @@ BEGIN
END IF;
END $$;

CREATE SEQUENCE IF NOT EXISTS releases_version_seq OWNED BY releases.version;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'releases'
AND column_name = 'eslversion') THEN
CREATE SEQUENCE IF NOT EXISTS releases_version_seq OWNED BY releases.version;
END IF;
END $$;

SELECT setval('releases_version_seq', coalesce(max(version), 0) + 1, false) FROM releases;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'releases'
AND column_name = 'eslversion') THEN
PERFORM setval('releases_version_seq', coalesce(max(version), 0) + 1, false) FROM releases;
END IF;
END $$;

ALTER TABLE IF EXISTS releases
ALTER COLUMN version SET DEFAULT nextval('releases_version_seq');
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'releases'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS releases
ALTER COLUMN version SET DEFAULT nextval('releases_version_seq');
END IF;
END $$;

ALTER TABLE IF EXISTS releases DROP CONSTRAINT IF EXISTS releases_pkey;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'releases'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS releases DROP CONSTRAINT IF EXISTS releases_pkey;
END IF;
END $$;

ALTER TABLE IF EXISTS releases ADD PRIMARY KEY (version, appname, releaseversion);
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'releases'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS releases ADD PRIMARY KEY (version, appname, releaseversion);
END IF;
END $$;

ALTER TABLE IF EXISTS releases DROP COLUMN IF EXISTS eslversion;
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
ALTER TABLE IF EXISTS deployments ADD COLUMN IF NOT EXISTS version INTEGER;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'deployments'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS deployments ADD COLUMN IF NOT EXISTS version INTEGER;
END IF;
END $$;

DO $$
BEGIN
IF EXISTS (SELECT 1
Expand All @@ -16,15 +25,55 @@ BEGIN
END IF;
END $$;

CREATE SEQUENCE IF NOT EXISTS deployments_version_seq OWNED BY deployments.version;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'deployments'
AND column_name = 'eslversion') THEN
CREATE SEQUENCE IF NOT EXISTS deployments_version_seq OWNED BY deployments.version;
END IF;
END $$;

SELECT setval('deployments_version_seq', coalesce(max(version), 0) + 1, false) FROM deployments;

ALTER TABLE IF EXISTS deployments
ALTER COLUMN version SET DEFAULT nextval('deployments_version_seq');
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'deployments'
AND column_name = 'eslversion') THEN
SELECT setval('deployments_version_seq', coalesce(max(version), 0) + 1, false) FROM deployments;
END IF;
END $$;

ALTER TABLE IF EXISTS deployments DROP CONSTRAINT IF EXISTS deployments_pkey;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'deployments'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS deployments
ALTER COLUMN version SET DEFAULT nextval('deployments_version_seq');
END IF;
END $$;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'deployments'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS deployments DROP CONSTRAINT IF EXISTS deployments_pkey;
END IF;
END $$;

ALTER TABLE IF EXISTS deployments ADD PRIMARY KEY (version, appname, envname);
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'deployments'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS deployments ADD PRIMARY KEY (version, appname, envname);
END IF;
END $$;

ALTER TABLE IF EXISTS deployments DROP COLUMN IF EXISTS eslversion;
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,23 @@ SELECT setval('environments_version_seq', coalesce(max(row_version), 0) + 1, fal
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;
DO $$
sven-urbanski-freiheit-com marked this conversation as resolved.
Show resolved Hide resolved
DECLARE
cmd TEXT;
BEGIN
FOR cmd IN
SELECT format(
'ALTER TABLE %I DROP CONSTRAINT %I;',
relname, conname
)
FROM pg_constraint c
JOIN pg_class t ON c.conrelid = t.oid
WHERE conname LIKE 'environments_pkey%'
LOOP
EXECUTE cmd;
END LOOP;
END $$;


ALTER TABLE IF EXISTS environments ADD PRIMARY KEY (row_version, name);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
-- rename releases table to releases_history if it doesn't exist
DO $$
BEGIN
IF EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'releases'
) AND NOT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'releases_history'
) THEN
ALTER TABLE releases RENAME TO releases_history;
END IF;
END
$$;

-- create releases table if it doesn't exist
CREATE TABLE IF NOT EXISTS releases
(
releaseVersion INTEGER,
created TIMESTAMP,
appName VARCHAR,
manifests VARCHAR,
metadata VARCHAR,
environments VARCHAR,
PRIMARY KEY(releaseVersion, appName)
);

-- insert data into releases table from releases_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 = 'releases'
) AND NOT EXISTS (
SELECT 1 FROM releases LIMIT 1
) AND EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'releases_history'
) THEN
INSERT INTO releases (releaseVersion, created, appName, manifests, metadata, environments)
SELECT DISTINCT
releases_history.releaseVersion,
releases_history.created,
releases_history.appName,
releases_history.manifests,
releases_history.metadata,
releases_history.environments
FROM (
SELECT
MAX(version) AS latestRelease,
appname,
releaseversion
FROM
"releases_history"
GROUP BY
appname, releaseversion) AS latest
JOIN
releases_history AS releases_history
ON
latest.latestRelease=releases_history.version
AND latest.releaseVersion=releases_history.releaseVersion
AND latest.appname=releases_history.appname
WHERE releases_history.deleted=false;
END IF;
END
$$;

-- Remove all_releases table
DROP TABLE IF EXISTS all_releases;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

CREATE TABLE IF NOT EXISTS releases_history
(
releaseVersion INTEGER,
created TIMESTAMP,
appName VARCHAR,
manifests VARCHAR,
metadata VARCHAR,
deleted BOOLEAN,
environments VARCHAR,
version INTEGER PRIMARY KEY AUTOINCREMENT
);

INSERT INTO releases_history (releaseversion, created, appname, manifests, metadata, deleted)
SELECT releaseversion, created, appname, manifests, metadata, deleted
FROM releases
ORDER BY version;

DROP TABLE IF EXISTS releases;
DROP TABLE IF EXISTS all_releases;

CREATE TABLE IF NOT EXISTS releases
(
releaseVersion INTEGER,
created TIMESTAMP,
appName VARCHAR,
manifests VARCHAR,
metadata VARCHAR,
environments VARCHAR,
PRIMARY KEY (releaseVersion, appName)
);
Loading
Loading