memos/store/db/migration/10001__schema.sql

134 lines
3.3 KiB
MySQL
Raw Normal View History

2022-02-03 15:32:03 +08:00
-- user
CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
2022-05-17 21:21:13 +08:00
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
2022-05-19 18:32:04 +08:00
-- allowed row status are 'NORMAL', 'ARCHIVED'.
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
email TEXT NOT NULL UNIQUE,
role TEXT NOT NULL CHECK (role IN ('OWNER', 'USER')) DEFAULT 'USER',
2022-02-03 15:32:03 +08:00
name TEXT NOT NULL,
2022-02-06 16:19:20 +08:00
password_hash TEXT NOT NULL,
2022-05-17 21:21:13 +08:00
open_id TEXT NOT NULL UNIQUE
2022-02-03 15:32:03 +08:00
);
INSERT INTO
sqlite_sequence (name, seq)
VALUES
2022-02-05 11:43:25 +08:00
('user', 100);
2022-02-03 15:32:03 +08:00
CREATE TRIGGER IF NOT EXISTS `trigger_update_user_modification_time`
AFTER
UPDATE
ON `user` FOR EACH ROW BEGIN
UPDATE
`user`
SET
updated_ts = (strftime('%s', 'now'))
WHERE
rowid = old.rowid;
END;
-- memo
CREATE TABLE memo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
2022-05-17 21:21:13 +08:00
creator_id INTEGER NOT NULL,
2022-02-03 15:32:03 +08:00
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
2022-05-19 18:32:04 +08:00
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
2022-02-03 15:32:03 +08:00
content TEXT NOT NULL DEFAULT '',
2022-05-28 07:43:07 +08:00
FOREIGN KEY(creator_id) REFERENCES user(id) ON DELETE CASCADE
2022-02-03 15:32:03 +08:00
);
INSERT INTO
sqlite_sequence (name, seq)
VALUES
('memo', 100);
CREATE TRIGGER IF NOT EXISTS `trigger_update_memo_modification_time`
AFTER
UPDATE
ON `memo` FOR EACH ROW BEGIN
UPDATE
`memo`
SET
updated_ts = (strftime('%s', 'now'))
WHERE
rowid = old.rowid;
END;
2022-05-19 18:32:04 +08:00
-- memo_organizer
CREATE TABLE memo_organizer (
id INTEGER PRIMARY KEY AUTOINCREMENT,
memo_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
pinned INTEGER NOT NULL CHECK (pinned IN (0, 1)) DEFAULT 0,
2022-05-28 07:43:07 +08:00
FOREIGN KEY(memo_id) REFERENCES memo(id) ON DELETE CASCADE,
FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE,
2022-05-19 18:32:04 +08:00
UNIQUE(memo_id, user_id)
);
INSERT INTO
sqlite_sequence (name, seq)
VALUES
('memo_organizer', 100);
2022-02-03 15:32:03 +08:00
-- shortcut
CREATE TABLE shortcut (
id INTEGER PRIMARY KEY AUTOINCREMENT,
2022-05-17 21:21:13 +08:00
creator_id INTEGER NOT NULL,
2022-02-03 15:32:03 +08:00
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
2022-05-19 18:32:04 +08:00
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
2022-02-03 15:32:03 +08:00
title TEXT NOT NULL DEFAULT '',
2022-05-03 11:49:10 +08:00
payload TEXT NOT NULL DEFAULT '{}',
2022-05-28 07:43:07 +08:00
FOREIGN KEY(creator_id) REFERENCES user(id) ON DELETE CASCADE
2022-02-03 15:32:03 +08:00
);
INSERT INTO
sqlite_sequence (name, seq)
VALUES
2022-02-05 11:43:25 +08:00
('shortcut', 100);
2022-02-03 15:32:03 +08:00
CREATE TRIGGER IF NOT EXISTS `trigger_update_shortcut_modification_time`
AFTER
UPDATE
ON `shortcut` FOR EACH ROW BEGIN
UPDATE
`shortcut`
SET
updated_ts = (strftime('%s', 'now'))
WHERE
rowid = old.rowid;
END;
-- resource
CREATE TABLE resource (
id INTEGER PRIMARY KEY AUTOINCREMENT,
2022-05-17 21:21:13 +08:00
creator_id INTEGER NOT NULL,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
2022-02-03 15:32:03 +08:00
filename TEXT NOT NULL DEFAULT '',
blob BLOB NOT NULL,
type TEXT NOT NULL DEFAULT '',
size INTEGER NOT NULL DEFAULT 0,
2022-05-28 07:43:07 +08:00
FOREIGN KEY(creator_id) REFERENCES user(id) ON DELETE CASCADE
2022-02-03 15:32:03 +08:00
);
2022-02-05 11:43:25 +08:00
INSERT INTO
sqlite_sequence (name, seq)
VALUES
('resource', 100);
2022-02-03 15:32:03 +08:00
CREATE TRIGGER IF NOT EXISTS `trigger_update_resource_modification_time`
AFTER
UPDATE
ON `resource` FOR EACH ROW BEGIN
UPDATE
`resource`
SET
updated_ts = (strftime('%s', 'now'))
WHERE
rowid = old.rowid;
END;