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: Fixed repeated names and added docs to SQL trigger and functions #86

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions db/sql/init.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
-- Providers
-- ## Tables definitions ##
-- Cloud Providers
CREATE TABLE IF NOT EXISTS providers (
name TEXT PRIMARY KEY
);

-- Default values for Cloud Providers table
INSERT INTO
providers(name)
VALUES
Expand All @@ -18,6 +20,7 @@ CREATE TABLE IF NOT EXISTS status (
value TEXT PRIMARY KEY
);

-- Default values for Status table
INSERT INTO
status(value)
VALUES
Expand Down Expand Up @@ -93,8 +96,8 @@ CREATE TABLE IF NOT EXISTS expenses (
PRIMARY KEY (instance_id, date)
);


-- Functions
-- ## Functions ##
-- Updates the total cost of an instance after a new expense record is inserted
CREATE OR REPLACE FUNCTION update_instance_total_costs_after_insert()
RETURNS TRIGGER
LANGUAGE PLPGSQL
Expand All @@ -108,6 +111,7 @@ BEGIN
END;
$$;

-- Updates the total cost of an instance after an expense record is deleted
CREATE OR REPLACE FUNCTION update_instance_total_costs_after_delete()
RETURNS TRIGGER
LANGUAGE PLPGSQL
Expand All @@ -121,7 +125,7 @@ BEGIN
END;
$$;


-- Updates the daily cost of an instance after a new expense record is inserted
CREATE OR REPLACE FUNCTION update_instance_daily_costs_after_insert()
RETURNS TRIGGER
LANGUAGE PLPGSQL
Expand All @@ -135,7 +139,7 @@ BEGIN
END;
$$;


-- Updates the daily cost of an instance after an expense record is deleted
CREATE OR REPLACE FUNCTION update_instance_daily_costs_after_delete()
RETURNS TRIGGER
LANGUAGE PLPGSQL
Expand All @@ -149,7 +153,7 @@ BEGIN
END;
$$;


-- Updates the total cost of a cluster based on its associated instances
CREATE OR REPLACE FUNCTION update_cluster_total_costs()
RETURNS TRIGGER
LANGUAGE PLPGSQL
Expand All @@ -163,7 +167,7 @@ BEGIN
END;
$$;


-- Updates the total cost of an account based on its associated clusters
CREATE OR REPLACE FUNCTION update_account_total_costs()
RETURNS TRIGGER
LANGUAGE PLPGSQL
Expand All @@ -177,64 +181,65 @@ BEGIN
END;
$$;

-- Triggers
-- ## Triggers ##
-- Trigger to update instance total cost after an expense is inserted
CREATE TRIGGER update_instance_total_cost_after_insert
AFTER INSERT
ON expenses
FOR EACH ROW
EXECUTE PROCEDURE update_instance_total_costs_after_insert();


CREATE TRIGGER update_instance_total_cost_after_insert
-- Trigger to update instance total cost after an expense is updated
CREATE TRIGGER update_instance_total_cost_after_update
AFTER UPDATE
ON expenses
FOR EACH ROW
EXECUTE PROCEDURE update_instance_total_costs_after_insert();


-- Trigger to update instance total cost after an expense is deleted
CREATE TRIGGER update_instance_total_cost_after_delete
AFTER DELETE
ON expenses
FOR EACH ROW
EXECUTE PROCEDURE update_instance_total_costs_after_delete();


-- Trigger to update instance daily cost after an expense is inserted
CREATE TRIGGER update_instance_daily_cost_after_insert
AFTER INSERT
ON expenses
FOR EACH ROW
EXECUTE PROCEDURE update_instance_daily_costs_after_insert();


CREATE TRIGGER update_instance_daily_cost_after_insert
-- Trigger to update instance daily cost after an expense is updated
CREATE TRIGGER update_instance_daily_cost_after_update
AFTER UPDATE
ON expenses
FOR EACH ROW
EXECUTE PROCEDURE update_instance_daily_costs_after_insert();


-- Trigger to update instance daily cost after an expense is deleted
CREATE TRIGGER update_instance_daily_cost_after_delete
AFTER DELETE
ON expenses
FOR EACH ROW
EXECUTE PROCEDURE update_instance_daily_costs_after_delete();


-- Trigger to update cluster total cost after an instance is updated
CREATE TRIGGER update_cluster_total_cost
AFTER UPDATE
ON instances
FOR EACH ROW
EXECUTE PROCEDURE update_cluster_total_costs();


-- Trigger to update account total cost after a cluster is updated
CREATE TRIGGER update_account_total_cost
AFTER UPDATE
ON clusters
FOR EACH ROW
EXECUTE PROCEDURE update_account_total_costs();


-- Removed Instances/Clusters
-- ## Maintenance Functions ##
-- Marks instances as 'Terminated' if they haven't been scanned in the last 24 hours
CREATE OR REPLACE FUNCTION check_terminated_instances()
RETURNS void AS $$
BEGIN
Expand All @@ -244,7 +249,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;


-- Marks clusters as 'Terminated' if they haven't been scanned in the last 24 hours
CREATE OR REPLACE FUNCTION check_terminated_clusters()
RETURNS void AS $$
BEGIN
Expand Down