fix: make files initial migration idempotent
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||||
|
|
||||||
CREATE TABLE files_nodes (
|
CREATE TABLE IF NOT EXISTS files_nodes (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
parent_id UUID REFERENCES files_nodes(id) ON DELETE CASCADE,
|
parent_id UUID REFERENCES files_nodes(id) ON DELETE CASCADE,
|
||||||
node_type TEXT NOT NULL CHECK (node_type IN ('folder', 'file', 'google_sheet', 'office_document')),
|
node_type TEXT NOT NULL CHECK (node_type IN ('folder', 'file', 'google_sheet', 'office_document')),
|
||||||
@@ -25,11 +25,11 @@ CREATE TABLE files_nodes (
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX files_nodes_parent_idx ON files_nodes(parent_id) WHERE deleted_at IS NULL;
|
CREATE INDEX IF NOT EXISTS files_nodes_parent_idx ON files_nodes(parent_id) WHERE deleted_at IS NULL;
|
||||||
CREATE INDEX files_nodes_owner_idx ON files_nodes(owner_user_id) WHERE deleted_at IS NULL;
|
CREATE INDEX IF NOT EXISTS files_nodes_owner_idx ON files_nodes(owner_user_id) WHERE deleted_at IS NULL;
|
||||||
CREATE INDEX files_nodes_type_idx ON files_nodes(node_type) WHERE deleted_at IS NULL;
|
CREATE INDEX IF NOT EXISTS files_nodes_type_idx ON files_nodes(node_type) WHERE deleted_at IS NULL;
|
||||||
|
|
||||||
CREATE TABLE files_access (
|
CREATE TABLE IF NOT EXISTS files_access (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
node_id UUID NOT NULL REFERENCES files_nodes(id) ON DELETE CASCADE,
|
node_id UUID NOT NULL REFERENCES files_nodes(id) ON DELETE CASCADE,
|
||||||
user_id UUID NOT NULL,
|
user_id UUID NOT NULL,
|
||||||
@@ -39,10 +39,10 @@ CREATE TABLE files_access (
|
|||||||
UNIQUE (node_id, user_id)
|
UNIQUE (node_id, user_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX files_access_node_idx ON files_access(node_id);
|
CREATE INDEX IF NOT EXISTS files_access_node_idx ON files_access(node_id);
|
||||||
CREATE INDEX files_access_user_idx ON files_access(user_id);
|
CREATE INDEX IF NOT EXISTS files_access_user_idx ON files_access(user_id);
|
||||||
|
|
||||||
CREATE TABLE files_public_links (
|
CREATE TABLE IF NOT EXISTS files_public_links (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
node_id UUID NOT NULL REFERENCES files_nodes(id) ON DELETE CASCADE,
|
node_id UUID NOT NULL REFERENCES files_nodes(id) ON DELETE CASCADE,
|
||||||
token_hash TEXT NOT NULL UNIQUE,
|
token_hash TEXT NOT NULL UNIQUE,
|
||||||
@@ -53,10 +53,10 @@ CREATE TABLE files_public_links (
|
|||||||
revoked_at TIMESTAMPTZ
|
revoked_at TIMESTAMPTZ
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX files_public_links_node_idx ON files_public_links(node_id);
|
CREATE INDEX IF NOT EXISTS files_public_links_node_idx ON files_public_links(node_id);
|
||||||
CREATE INDEX files_public_links_active_idx ON files_public_links(token_hash, expires_at) WHERE revoked_at IS NULL;
|
CREATE INDEX IF NOT EXISTS files_public_links_active_idx ON files_public_links(token_hash, expires_at) WHERE revoked_at IS NULL;
|
||||||
|
|
||||||
CREATE TABLE files_audit_events (
|
CREATE TABLE IF NOT EXISTS files_audit_events (
|
||||||
id BIGSERIAL PRIMARY KEY,
|
id BIGSERIAL PRIMARY KEY,
|
||||||
actor_user_id UUID,
|
actor_user_id UUID,
|
||||||
action TEXT NOT NULL,
|
action TEXT NOT NULL,
|
||||||
@@ -66,6 +66,5 @@ CREATE TABLE files_audit_events (
|
|||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX files_audit_events_actor_idx ON files_audit_events(actor_user_id, created_at DESC);
|
CREATE INDEX IF NOT EXISTS files_audit_events_actor_idx ON files_audit_events(actor_user_id, created_at DESC);
|
||||||
CREATE INDEX files_audit_events_entity_idx ON files_audit_events(entity_type, entity_id, created_at DESC);
|
CREATE INDEX IF NOT EXISTS files_audit_events_entity_idx ON files_audit_events(entity_type, entity_id, created_at DESC);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user