From cf607ae34556288bbee1839b0454841cd3a053ea Mon Sep 17 00:00:00 2001 From: Imbus Date: Sat, 21 Oct 2023 00:59:21 +0200 Subject: [PATCH] Added some triggers and formatting to the migration scripts --- server/migrations/0001_users_table.sql | 38 +++++++++++++++----- server/migrations/0002_posts_table.sql | 50 +++++++++++++++++++------- 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/server/migrations/0001_users_table.sql b/server/migrations/0001_users_table.sql index 76976d2..b9ccfdb 100644 --- a/server/migrations/0001_users_table.sql +++ b/server/migrations/0001_users_table.sql @@ -1,9 +1,31 @@ -CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY, - username TEXT NOT NULL, - password TEXT NOT NULL, - created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP -); +CREATE TABLE + IF NOT EXISTS users ( + id INTEGER PRIMARY KEY NOT NULL, + username TEXT NOT NULL UNIQUE, + password TEXT NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + ); -create index users_username_index on users (username); \ No newline at end of file +-- Create a trigger to set created_at and updated_at on INSERT +CREATE TRIGGER IF NOT EXISTS set_created_at AFTER INSERT ON users BEGIN +UPDATE users +SET + created_at = CURRENT_TIMESTAMP +WHERE + id = NEW.id; + +END; + +-- Create a trigger to set updated_at on UPDATE +CREATE TRIGGER IF NOT EXISTS set_updated_at AFTER +UPDATE ON users BEGIN +UPDATE users +SET + updated_at = CURRENT_TIMESTAMP +WHERE + id = NEW.id; + +END; + +CREATE INDEX users_username_index ON users (username); \ No newline at end of file diff --git a/server/migrations/0002_posts_table.sql b/server/migrations/0002_posts_table.sql index c5d05e5..3863b63 100644 --- a/server/migrations/0002_posts_table.sql +++ b/server/migrations/0002_posts_table.sql @@ -1,12 +1,38 @@ -CREATE TABLE IF NOT EXISTS posts ( - id INTEGER PRIMARY KEY NOT NULL, - user_id INTEGER NOT NULL, - content TEXT NOT NULL, - upvotes INT NOT NULL DEFAULT 0, - downvotes INT NOT NULL DEFAULT 0, - created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (user_id) REFERENCES users (id) -); -create index IF NOT EXISTS posts_user_id_index on posts (user_id); -create index IF NOT EXISTS posts_id_index on posts (id); \ No newline at end of file +CREATE TABLE + IF NOT EXISTS posts ( + id INTEGER PRIMARY KEY NOT NULL, + user_id INTEGER NOT NULL, + content TEXT NOT NULL, + upvotes INTEGER NOT NULL DEFAULT 0, + downvotes INTEGER NOT NULL DEFAULT 0, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users (id) + ); + +-- Create a trigger to set created_at and updated_at on INSERT +CREATE TRIGGER IF NOT EXISTS set_created_at AFTER INSERT ON posts BEGIN +UPDATE posts +SET + created_at = CURRENT_TIMESTAMP +WHERE + id = NEW.id; + +END; + +-- Create a trigger to set updated_at on UPDATE +CREATE TRIGGER IF NOT EXISTS set_updated_at AFTER +UPDATE ON posts BEGIN +UPDATE posts +SET + updated_at = CURRENT_TIMESTAMP +WHERE + id = NEW.id; + +END; + +create INDEX IF NOT EXISTS posts_user_id_index ON posts (user_id); + +create INDEX IF NOT EXISTS posts_id_index ON posts (id); + +CREATE INDEX idx_created_at_desc ON your_table (created_at DESC); \ No newline at end of file