summaryrefslogtreecommitdiff
path: root/makima/migrations/20241222000000_create_files_table.sql
blob: cf6f76c6ebceb91a036fab19b9e7a8822015b459 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- Create files table for storing transcription records
CREATE TABLE IF NOT EXISTS files (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    owner_id UUID NOT NULL DEFAULT '00000000-0000-0000-0000-000000000002',
    name VARCHAR(255) NOT NULL,
    description TEXT,
    transcript JSONB NOT NULL DEFAULT '[]'::jsonb,
    location VARCHAR(512),
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Create index on owner_id for efficient filtering
CREATE INDEX idx_files_owner_id ON files(owner_id);

-- Create index on created_at for sorting
CREATE INDEX idx_files_created_at ON files(created_at DESC);

-- Create trigger to auto-update updated_at
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ language 'plpgsql';

CREATE TRIGGER update_files_updated_at
    BEFORE UPDATE ON files
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();