Skip to content

Commit

Permalink
fix(db): separate current from old environments in database (#2193)
Browse files Browse the repository at this point in the history
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
AminSlk authored Jan 14, 2025
1 parent 507fcd7 commit 585fd41
Show file tree
Hide file tree
Showing 15 changed files with 677 additions and 675 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
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
ALTER TABLE IF EXISTS environments ADD COLUMN IF NOT EXISTS row_version INTEGER;
END IF;
END $$;

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

DROP SEQUENCE IF EXISTS environments_version_seq CASCADE;
CREATE SEQUENCE IF NOT EXISTS environments_version_seq OWNED BY environments.row_version;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'environments'
AND column_name = 'version') THEN
DROP SEQUENCE IF EXISTS environments_version_seq CASCADE;
END IF;
END $$;

SELECT setval('environments_version_seq', coalesce(max(row_version), 0) + 1, false) FROM environments;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'environments'
AND column_name = 'version') THEN
CREATE SEQUENCE IF NOT EXISTS environments_version_seq OWNED BY environments.row_version;
END IF;
END $$;

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

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

DO $$
DECLARE
Expand All @@ -41,8 +83,32 @@ BEGIN
END LOOP;
END $$;

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

ALTER TABLE IF EXISTS environments ADD PRIMARY KEY (row_version, name);
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'environments'
AND column_name = 'version') THEN
ALTER TABLE IF EXISTS environments DROP COLUMN IF EXISTS version;
END IF;
END $$;

ALTER TABLE IF EXISTS environments DROP COLUMN IF EXISTS version;
ALTER TABLE IF EXISTS environments RENAME COLUMN row_version TO version;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'environments'
AND column_name = 'row_version') THEN
ALTER TABLE IF EXISTS environments RENAME COLUMN row_version TO version;
END IF;
END $$;
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;
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)
);
Loading

0 comments on commit 585fd41

Please sign in to comment.