mirror of
https://github.com/tgdrive/teldrive.git
synced 2025-01-10 17:19:56 +08:00
104 lines
3.5 KiB
SQL
104 lines
3.5 KiB
SQL
-- +goose Up
|
|
-- +goose StatementBegin
|
|
|
|
CREATE TABLE IF NOT EXISTS teldrive.files (
|
|
id text NOT NULL DEFAULT teldrive.generate_uid(16) PRIMARY KEY,
|
|
"name" text NOT NULL,
|
|
"type" text NOT NULL,
|
|
mime_type text NOT NULL,
|
|
"path" text,
|
|
"size" bigint,
|
|
starred bool NOT NULL,
|
|
"depth" int,
|
|
user_id bigint NOT NULL,
|
|
parent_id text,
|
|
status text DEFAULT 'active'::text,
|
|
channel_id bigint,
|
|
parts jsonb,
|
|
created_at timestamp NOT NULL DEFAULT timezone('utc'::text, now()),
|
|
updated_at timestamp NOT NULL DEFAULT timezone('utc'::text, now())
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS teldrive.users (
|
|
user_id bigint NOT NULL PRIMARY KEY,
|
|
"name" text,
|
|
user_name text NOT NULL,
|
|
is_premium bool NOT NULL,
|
|
created_at timestamptz NOT NULL DEFAULT timezone('utc'::text, now()),
|
|
updated_at timestamptz NOT NULL DEFAULT timezone('utc'::text, now())
|
|
);
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS teldrive.uploads (
|
|
id text NOT NULL DEFAULT teldrive.generate_uid(16) PRIMARY KEY,
|
|
upload_id text NOT NULL,
|
|
"name" text NOT NULL,
|
|
user_id bigint,
|
|
part_no int NOT NULL,
|
|
part_id int NOT NULL,
|
|
total_parts int NOT NULL,
|
|
channel_id bigint NOT NULL,
|
|
"size" bigint NOT NULL,
|
|
created_at timestamp DEFAULT timezone('utc'::text, now()),
|
|
);
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS teldrive.channels (
|
|
channel_id bigint NOT NULL PRIMARY KEY,
|
|
channel_name text NOT NULL,
|
|
user_id bigint NOT NULL,
|
|
selected boolean DEFAULT false,
|
|
FOREIGN KEY (user_id) REFERENCES teldrive.users(user_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS teldrive.bots (
|
|
user_id bigint NOT NULL,
|
|
"token" text NOT NULL,
|
|
bot_user_name text NOT NULL,
|
|
bot_id bigint NOT NULL,
|
|
channel_id bigint NULL,
|
|
FOREIGN KEY (user_id) REFERENCES teldrive.users(user_id),
|
|
CONSTRAINT btoken_user_un UNIQUE (user_id,token)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS teldrive.sessions (
|
|
session text NOT NULL,
|
|
user_id bigint NOT NULL,
|
|
hash text NOT NULL,
|
|
created_at timestamp default timezone('utc'::text,now()),
|
|
PRIMARY KEY(session, hash),
|
|
FOREIGN KEY (user_id) REFERENCES teldrive.users(user_id)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS name_numeric_idx ON teldrive.files USING btree (name COLLATE "numeric" NULLS FIRST);
|
|
CREATE INDEX IF NOT EXISTS name_search_idx ON teldrive.files USING gin (teldrive.get_tsvector(name), updated_at);
|
|
CREATE INDEX IF NOT EXISTS parent_idx ON teldrive.files USING btree (parent_id);
|
|
CREATE INDEX IF NOT EXISTS parent_name_numeric_idx ON teldrive.files USING btree (parent_id, name COLLATE "numeric" DESC);
|
|
CREATE INDEX IF NOT EXISTS path_idx ON teldrive.files USING btree (path);
|
|
CREATE INDEX IF NOT EXISTS starred_updated_at_idx ON teldrive.files USING btree (starred, updated_at DESC);
|
|
CREATE INDEX IF NOT EXISTS status_idx ON teldrive.files USING btree (status);
|
|
CREATE UNIQUE IF NOT EXISTS INDEX unique_file ON teldrive.files USING btree (name, parent_id, user_id) WHERE (status = 'active'::text);
|
|
CREATE INDEX IF NOT EXISTS user_id_idx ON teldrive.files USING btree (user_id);
|
|
|
|
-- +goose StatementEnd
|
|
|
|
-- +goose Down
|
|
-- +goose StatementBegin
|
|
|
|
DROP INDEX IF EXISTS name_numeric_idx ;
|
|
DROP INDEX IF EXISTS name_search_idx ;
|
|
DROP INDEX IF EXISTS parent_idx ;
|
|
DROP INDEX IF EXISTS parent_name_numeric_idx ;
|
|
DROP INDEX IF EXISTS path_idx ;
|
|
DROP INDEX IF EXISTS starred_updated_at_idx ;
|
|
DROP INDEX IF EXISTS status_idx ;
|
|
DROP UNIQUE IF EXISTS INDEX unique_file;
|
|
DROP INDEX IF EXISTS user_id_idx;
|
|
|
|
DROP TABLE IF EXISTS teldrive.files;
|
|
DROP TABLE IF EXISTS teldrive.uploads;
|
|
DROP TABLE IF EXISTS teldrive.users;
|
|
DROP TABLE IF EXISTS teldrive.channels;
|
|
DROP TABLE IF EXISTS teldrive.bots;
|
|
DROP TABLE IF EXISTS teldrive.sessions;
|
|
-- +goose StatementEnd
|