use postgresql
This commit is contained in:
0
migrations/postgres/.gitkeep
Normal file
0
migrations/postgres/.gitkeep
Normal file
@@ -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();
|
||||
@@ -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;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
DROP TABLE posts;
|
||||
23
migrations/postgres/2022-03-21-155550_create_posts/up.sql
Normal file
23
migrations/postgres/2022-03-21-155550_create_posts/up.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
-- Your SQL goes here
|
||||
CREATE TABLE posts (
|
||||
id SERIAL PRIMARY KEY,
|
||||
author_hash VARCHAR NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
cw VARCHAR NOT NULL DEFAULT '',
|
||||
author_title VARCHAR NOT NULL DEFAULT '',
|
||||
is_tmp BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
n_attentions INTEGER NOT NULL DEFAULT 0,
|
||||
n_comments INTEGER NOT NULL DEFAULT 0,
|
||||
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
last_comment_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
is_reported BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
hot_score INTEGER NOT NULL DEFAULT 0,
|
||||
allow_search BOOLEAN NOT NULL DEFAULT FALSE
|
||||
);
|
||||
|
||||
CREATE INDEX posts_last_comment_time_idx ON posts (last_comment_time);
|
||||
CREATE INDEX posts_hot_idx ON posts (hot_score);
|
||||
CREATE INDEX posts_author_idx ON posts (author_title);
|
||||
CREATE INDEX posts_cw_idx ON posts (cw);
|
||||
CREATE INDEX posts_search_text_trgm_idx ON posts USING gin(content gin_trgm_ops);
|
||||
@@ -0,0 +1,2 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
DROP TABLE users;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- Your SQL goes here
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR NOT NULL UNIQUE,
|
||||
token VARCHAR NOT NULL UNIQUE,
|
||||
is_admin BOOLEAN NOT NULL DEFAULT FALSE
|
||||
);
|
||||
@@ -0,0 +1,2 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
DROP TABLE comments;
|
||||
14
migrations/postgres/2022-03-21-164718_create_comments/up.sql
Normal file
14
migrations/postgres/2022-03-21-164718_create_comments/up.sql
Normal file
@@ -0,0 +1,14 @@
|
||||
-- Your SQL goes here
|
||||
CREATE TABLE comments (
|
||||
id SERIAL PRIMARY KEY,
|
||||
author_hash VARCHAR NOT NULL,
|
||||
author_title VARCHAR NOT NULL DEFAULT '',
|
||||
is_tmp BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
content TEXT NOT NULL,
|
||||
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
allow_search BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
post_id INTEGER NOT NULL REFERENCES posts(id)
|
||||
);
|
||||
CREATE INDEX comments_postId_idx ON comments (post_id);
|
||||
|
||||
@@ -14,7 +14,7 @@ CREATE TABLE posts (
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
is_reported BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
hot_score INTEGER NOT NULL DEFAULT 0,
|
||||
allow_search BOOLEAN NOT NULL DEFAULT ''
|
||||
allow_search BOOLEAN NOT NULL DEFAULT FALSE
|
||||
);
|
||||
CREATE INDEX posts_last_comment_time_idx ON posts (`last_comment_time`);
|
||||
CREATE INDEX posts_hot_idx ON posts (`hot_score`)
|
||||
Reference in New Issue
Block a user