-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9371a01
commit 24f7094
Showing
14 changed files
with
482 additions
and
517 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Empty file.
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,6 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); | ||
DROP FUNCTION IF EXISTS diesel_set_updated_at(); |
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,36 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
|
||
|
||
|
||
-- Sets up a trigger for the given table to automatically set a column called | ||
-- `updated_at` whenever the row is modified (unless `updated_at` was included | ||
-- in the modified columns) | ||
-- | ||
-- # Example | ||
-- | ||
-- ```sql | ||
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); | ||
-- | ||
-- SELECT diesel_manage_updated_at('users'); | ||
-- ``` | ||
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ | ||
BEGIN | ||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s | ||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ | ||
BEGIN | ||
IF ( | ||
NEW IS DISTINCT FROM OLD AND | ||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at | ||
) THEN | ||
NEW.updated_at := current_timestamp; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,9 @@ | ||
-- 1. Drop the triggers | ||
DROP TRIGGER IF EXISTS tr_set_dates_after_insert ON vss_db; | ||
DROP TRIGGER IF EXISTS tr_set_dates_after_update ON vss_db; | ||
|
||
-- 2. Drop the trigger functions | ||
DROP FUNCTION IF EXISTS set_created_date(); | ||
DROP FUNCTION IF EXISTS set_updated_date(); | ||
|
||
DROP TABLE IF EXISTS vss_db; |
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,79 @@ | ||
CREATE TABLE vss_db | ||
( | ||
store_id TEXT NOT NULL CHECK (store_id != ''), | ||
key TEXT NOT NULL, | ||
value bytea, | ||
version BIGINT NOT NULL, | ||
created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
updated_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
PRIMARY KEY (store_id, key) | ||
); | ||
|
||
-- triggers to set dates automatically, generated by ChatGPT | ||
|
||
-- Function to set created_date and updated_date during INSERT | ||
CREATE OR REPLACE FUNCTION set_created_date() | ||
RETURNS TRIGGER AS | ||
$$ | ||
BEGIN | ||
NEW.created_date := CURRENT_TIMESTAMP; | ||
NEW.updated_date := CURRENT_TIMESTAMP; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
-- Function to set updated_date during UPDATE | ||
CREATE OR REPLACE FUNCTION set_updated_date() | ||
RETURNS TRIGGER AS | ||
$$ | ||
BEGIN | ||
NEW.updated_date := CURRENT_TIMESTAMP; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
-- Trigger for INSERT operation on vss_db | ||
CREATE TRIGGER tr_set_dates_after_insert | ||
BEFORE INSERT | ||
ON vss_db | ||
FOR EACH ROW | ||
EXECUTE FUNCTION set_created_date(); | ||
|
||
-- Trigger for UPDATE operation on vss_db | ||
CREATE TRIGGER tr_set_dates_after_update | ||
BEFORE UPDATE | ||
ON vss_db | ||
FOR EACH ROW | ||
EXECUTE FUNCTION set_updated_date(); | ||
|
||
CREATE OR REPLACE FUNCTION upsert_vss_db( | ||
p_store_id TEXT, | ||
p_key TEXT, | ||
p_value bytea, | ||
p_version BIGINT | ||
) RETURNS VOID AS | ||
$$ | ||
BEGIN | ||
|
||
WITH new_values (store_id, key, value, version) AS (VALUES (p_store_id, p_key, p_value, p_version)) | ||
INSERT | ||
INTO vss_db | ||
(store_id, key, value, version) | ||
SELECT new_values.store_id, | ||
new_values.key, | ||
new_values.value, | ||
new_values.version | ||
FROM new_values | ||
LEFT JOIN vss_db AS existing | ||
ON new_values.store_id = existing.store_id | ||
AND new_values.key = existing.key | ||
WHERE CASE | ||
WHEN new_values.version >= 4294967295 THEN new_values.version >= COALESCE(existing.version, -1) | ||
ELSE new_values.version > COALESCE(existing.version, -1) | ||
END | ||
ON CONFLICT (store_id, key) | ||
DO UPDATE SET value = excluded.value, | ||
version = excluded.version; | ||
|
||
END; | ||
$$ LANGUAGE plpgsql; |
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
Oops, something went wrong.