-
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 apps (#2192)
Ref: SRX-CSV95I
- Loading branch information
Showing
12 changed files
with
513 additions
and
506 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
database/migrations/postgres/1720455590920137_resize_json_columns.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 |
---|---|---|
@@ -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; |
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
62 changes: 62 additions & 0 deletions
62
database/migrations/postgres/1736341539889915_separate_apps_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,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; |
23 changes: 23 additions & 0 deletions
23
database/migrations/sqlite/1736341539889915_separate_apps_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,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) | ||
); |
Oops, something went wrong.