Added some triggers and formatting to the migration scripts
This commit is contained in:
parent
2572fcbffd
commit
cf607ae345
2 changed files with 68 additions and 20 deletions
|
@ -1,9 +1,31 @@
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
CREATE TABLE
|
||||||
id INTEGER PRIMARY KEY,
|
IF NOT EXISTS users (
|
||||||
username TEXT NOT NULL,
|
id INTEGER PRIMARY KEY NOT NULL,
|
||||||
password TEXT NOT NULL,
|
username TEXT NOT NULL UNIQUE,
|
||||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
password TEXT NOT NULL,
|
||||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
);
|
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
create index users_username_index on users (username);
|
-- 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);
|
|
@ -1,12 +1,38 @@
|
||||||
CREATE TABLE IF NOT EXISTS posts (
|
CREATE TABLE
|
||||||
id INTEGER PRIMARY KEY NOT NULL,
|
IF NOT EXISTS posts (
|
||||||
user_id INTEGER NOT NULL,
|
id INTEGER PRIMARY KEY NOT NULL,
|
||||||
content TEXT NOT NULL,
|
user_id INTEGER NOT NULL,
|
||||||
upvotes INT NOT NULL DEFAULT 0,
|
content TEXT NOT NULL,
|
||||||
downvotes INT NOT NULL DEFAULT 0,
|
upvotes INTEGER NOT NULL DEFAULT 0,
|
||||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
downvotes INTEGER NOT NULL DEFAULT 0,
|
||||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (user_id) REFERENCES users (id)
|
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);
|
|
||||||
|
-- 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);
|
Loading…
Reference in a new issue