V1__create_uuid_schema.sql
paperless-backend/src/main/resources/db/migration/postgresql/V1__create_uuid_schema.sql
1-- Greenfield PostgreSQL schema matching the current Hibernate entities.2-- UUID primary keys are generated by Hibernate; there are intentionally no3-- database-side ID defaults.45CREATE TABLE users (6 id uuid NOT NULL,7 username varchar(50) NOT NULL,8 password varchar(255) NOT NULL,9 created_at timestamp(6) with time zone NOT NULL,10 updated_at timestamp(6) with time zone,11 CONSTRAINT pk_users PRIMARY KEY (id),12 CONSTRAINT uq_users_username UNIQUE (username)13);1415CREATE TABLE teams (16 id uuid NOT NULL,17 name varchar(100) NOT NULL,18 description varchar(255),19 created_at timestamp(6) with time zone NOT NULL,20 updated_at timestamp(6) with time zone,21 owner_id uuid NOT NULL,22 CONSTRAINT pk_teams PRIMARY KEY (id)23);2425CREATE TABLE document_types (26 id uuid NOT NULL,27 name varchar(100) NOT NULL,28 description varchar(255),29 CONSTRAINT pk_document_types PRIMARY KEY (id),30 CONSTRAINT uq_document_types_name UNIQUE (name)31);3233CREATE TABLE team_members (34 id uuid NOT NULL,35 team_id uuid NOT NULL,36 user_id uuid NOT NULL,37 role varchar(255) NOT NULL,38 joined_at timestamp(6) with time zone,39 CONSTRAINT pk_team_members PRIMARY KEY (id),40 CONSTRAINT uq_team_members_team_user UNIQUE (team_id, user_id),41 CONSTRAINT fk_team_members_team FOREIGN KEY (team_id) REFERENCES teams (id),42 CONSTRAINT fk_team_members_user FOREIGN KEY (user_id) REFERENCES users (id),43 CONSTRAINT ck_team_members_role CHECK (role IN ('ADMIN', 'READONLY', 'READ_WRITE'))44);4546CREATE TABLE documents (47 id uuid NOT NULL,48 title varchar(255) NOT NULL,49 original_filename varchar(255) NOT NULL,50 content_type varchar(255) NOT NULL,51 file_size bigint NOT NULL,52 owner_id uuid NOT NULL,53 team_id uuid,54 status varchar(255) NOT NULL,55 ocr_content text,56 summary text,57 storage_key varchar(255),58 document_type_id uuid,59 created_at timestamp(6) with time zone NOT NULL,60 updated_at timestamp(6) with time zone NOT NULL,61 version bigint NOT NULL,62 CONSTRAINT pk_documents PRIMARY KEY (id),63 CONSTRAINT fk_documents_owner FOREIGN KEY (owner_id) REFERENCES users (id),64 CONSTRAINT fk_documents_team FOREIGN KEY (team_id) REFERENCES teams (id),65 CONSTRAINT fk_documents_document_type FOREIGN KEY (document_type_id) REFERENCES document_types (id),66 CONSTRAINT ck_documents_status CHECK (67 status IN ('PENDING', 'OCR_IN_PROGRESS', 'GENAI_IN_PROGRESS', 'COMPLETED', 'FAILED')68 )69);7071CREATE TABLE access_tokens (72 digest varchar(64) NOT NULL,73 user_id uuid NOT NULL,74 expires_at timestamp(6) with time zone NOT NULL,75 CONSTRAINT pk_access_tokens PRIMARY KEY (digest),76 CONSTRAINT uq_access_tokens_user_id UNIQUE (user_id),77 CONSTRAINT fk_access_tokens_user FOREIGN KEY (user_id) REFERENCES users (id)78);79