-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update cost model to use history
Signed-off-by: Gustavo Inacio <[email protected]>
- Loading branch information
Showing
4 changed files
with
74 additions
and
48 deletions.
There are no files selected for viewing
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
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,2 +1,8 @@ | ||
-- Add down migration script here | ||
DROP TABLE "CostModels"; | ||
DROP TRIGGER IF EXISTS cost_models_update ON "CostModelsHistory" CASCADE; | ||
|
||
DROP FUNCTION IF EXISTS cost_models_update_notify() CASCADE; | ||
|
||
DROP VIEW "CostModels"; | ||
|
||
DROP TABLE "CostModelsHistory"; |
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,8 +1,45 @@ | ||
CREATE TABLE IF NOT EXISTS "CostModels" | ||
CREATE TABLE IF NOT EXISTS "CostModelsHistory" | ||
( | ||
id INT, | ||
id SERIAL PRIMARY KEY, | ||
deployment VARCHAR NOT NULL, | ||
model TEXT, | ||
variables JSONB, | ||
PRIMARY KEY( deployment ) | ||
"createdAt" TIMESTAMP WITH TIME ZONE, | ||
"updatedAt" TIMESTAMP WITH TIME ZONE | ||
); | ||
|
||
CREATE VIEW "CostModels" AS SELECT id, | ||
deployment, | ||
model, | ||
variables, | ||
"createdAt", | ||
"updatedAt" | ||
FROM "CostModelsHistory" t1 | ||
JOIN | ||
( | ||
SELECT MAX(id) | ||
FROM "CostModelsHistory" | ||
GROUP BY deployment | ||
) t2 | ||
ON t1.id = t2.MAX; | ||
|
||
CREATE FUNCTION cost_models_update_notify() | ||
RETURNS trigger AS | ||
$$ | ||
BEGIN | ||
IF TG_OP = 'DELETE' THEN | ||
PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "DELETE", "deployment": "%s"}', OLD.deployment)); | ||
RETURN OLD; | ||
ELSIF TG_OP = 'INSERT' THEN | ||
PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "INSERT", "deployment": "%s", "model": "%s"}', NEW.deployment, NEW.model)); | ||
RETURN NEW; | ||
ELSE | ||
PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "%s", "deployment": "%s", "model": "%s"}', NEW.deployment, NEW.model)); | ||
RETURN NEW; | ||
END IF; | ||
END; | ||
$$ LANGUAGE 'plpgsql'; | ||
|
||
CREATE TRIGGER cost_models_update AFTER INSERT OR UPDATE OR DELETE | ||
ON "CostModelsHistory" | ||
FOR EACH ROW EXECUTE PROCEDURE cost_models_update_notify(); |
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