Skip to content

Commit

Permalink
fix(db): separate current from old apps (#2192)
Browse files Browse the repository at this point in the history
Ref: SRX-CSV95I
  • Loading branch information
AminSlk authored Jan 10, 2025
1 parent fefd41f commit 5739eb5
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 506 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ALTER TABLE environments ALTER COLUMN json TYPE VARCHAR;
ALTER TABLE all_apps ALTER COLUMN json TYPE VARCHAR;
ALTER TABLE IF EXISTS all_apps ALTER COLUMN json TYPE VARCHAR;
ALTER TABLE all_env_locks ALTER COLUMN json TYPE VARCHAR;
ALTER TABLE all_app_locks ALTER COLUMN json TYPE VARCHAR;
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
ALTER TABLE IF EXISTS apps ADD COLUMN IF NOT EXISTS version INTEGER;

DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'apps'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS apps ADD COLUMN IF NOT EXISTS version INTEGER;
END IF;
END $$;
DO $$
BEGIN
IF EXISTS (SELECT 1
Expand All @@ -16,15 +25,63 @@ BEGIN
END IF;
END $$;

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

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

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

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

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

ALTER TABLE IF EXISTS apps DROP COLUMN IF EXISTS eslversion;
DO $$
BEGIN
IF EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'apps'
AND column_name = 'eslversion') THEN
ALTER TABLE IF EXISTS apps DROP COLUMN IF EXISTS eslversion;
END IF;
END $$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- rename apps table to apps_history if it doesn't exist
DO $$
BEGIN
IF EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'apps'
) AND NOT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'apps_history'
) THEN
ALTER TABLE apps RENAME TO apps_history;
END IF;
END
$$;

-- create apps table if it doesn't exist
CREATE TABLE IF NOT EXISTS apps
(
created TIMESTAMP,
appName VARCHAR,
stateChange VARCHAR,
metadata VARCHAR,
PRIMARY KEY(appName)
);

-- insert data into apps table from apps_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 = 'apps'
) AND NOT EXISTS (
SELECT 1 FROM apps LIMIT 1
) AND EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'apps_history'
) THEN
INSERT INTO apps (created, appName, stateChange, metadata)
SELECT DISTINCT
apps_history.created,
apps_history.appName,
apps_history.stateChange,
apps_history.metadata
FROM (
SELECT
MAX(version) AS latestVersion,
appname
FROM
"apps_history"
GROUP BY
appname) AS latest
JOIN
apps_history AS apps_history
ON
latest.latestVersion=apps_history.version
AND latest.appname=apps_history.appname;
END IF;
END
$$;

-- Remove all_apps table
DROP TABLE IF EXISTS all_apps;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE TABLE IF NOT EXISTS apps_history
(
created TIMESTAMP,
appName VARCHAR,
stateChange VARCHAR,
metadata VARCHAR,
version INTEGER PRIMARY KEY AUTOINCREMENT
);

INSERT INTO apps_history (created, appname, stateChange, metadata)
SELECT created, appname, stateChange, metadata
FROM apps
ORDER BY version;
DROP TABLE IF EXISTS apps;
DROP TABLE IF EXISTS all_apps;
CREATE TABLE IF NOT EXISTS apps
(
created TIMESTAMP,
appName VARCHAR,
stateChange VARCHAR,
metadata VARCHAR,
PRIMARY KEY (appname)
);
Loading

0 comments on commit 5739eb5

Please sign in to comment.