2016-11-20 20:04:52 +01:00
|
|
|
-- Tables
|
|
|
|
|
2019-12-09 16:27:01 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS image_collections (
|
2016-11-20 20:04:52 +01:00
|
|
|
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
|
2019-11-18 11:52:57 +01:00
|
|
|
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
deleted_at timestamptz DEFAULT NULL,
|
|
|
|
|
|
|
|
name text NOT NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS images (
|
|
|
|
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
|
2019-11-18 11:52:57 +01:00
|
|
|
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
deleted_at timestamptz DEFAULT NULL,
|
|
|
|
|
|
|
|
width int NOT NULL,
|
|
|
|
height int NOT NULL,
|
|
|
|
mimetype text NOT NULL,
|
2019-12-09 16:27:01 +01:00
|
|
|
collection_id uuid REFERENCES image_collections(id)
|
2019-11-18 11:52:57 +01:00
|
|
|
ON DELETE SET NULL
|
|
|
|
DEFAULT NULL,
|
2016-11-20 20:04:52 +01:00
|
|
|
name text NOT NULL,
|
|
|
|
path text NOT NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Indexes
|
|
|
|
|
2019-12-09 16:27:01 +01:00
|
|
|
CREATE INDEX image_collections__user_id__idx ON image_collections (user_id);
|
|
|
|
CREATE INDEX images__collection_id__idx ON images (collection_id);
|
|
|
|
CREATE INDEX images__user_id__idx ON images (user_id);
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
-- Triggers
|
|
|
|
|
2019-12-09 16:27:01 +01:00
|
|
|
CREATE TRIGGER image_collections__modified_at__tgr
|
|
|
|
BEFORE UPDATE ON image_collections
|
2016-11-20 20:04:52 +01:00
|
|
|
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
|
|
|
|
|
2019-12-09 16:27:01 +01:00
|
|
|
CREATE TRIGGER images__modified_at__tgr
|
|
|
|
BEFORE UPDATE ON images
|
|
|
|
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
|
2016-11-20 20:04:52 +01:00
|
|
|
|