-- Titano schema v1 (PostgreSQL). Instants are timestamptz, camera wall-clock times timestamp, JSON jsonb. -- Sort keys use the "C" collation so ordering equals SQLite's binary UTF-8 order. Mirrors sqlite/0001_initial.sql. CREATE TABLE archives ( id uuid NOT NULL PRIMARY KEY, name text NOT NULL, root_path text NOT NULL, folder_template text NOT NULL, file_template text NOT NULL, schema_version integer NOT NULL DEFAULT 1, created timestamptz NOT NULL, proxy_long_edge integer NOT NULL DEFAULT 1920 ); CREATE TABLE users ( id uuid NOT NULL PRIMARY KEY, username text NOT NULL, username_key text NOT NULL UNIQUE, display_name text, password_hash text NOT NULL, role integer NOT NULL, permissions bigint NOT NULL, quota_bytes bigint NOT NULL DEFAULT 0, used_bytes bigint NOT NULL DEFAULT 0, disabled boolean NOT NULL DEFAULT FALSE, must_change_password boolean NOT NULL DEFAULT FALSE, created timestamptz NOT NULL, last_login timestamptz ); CREATE TABLE sequences ( id uuid NOT NULL PRIMARY KEY, archive_id uuid NOT NULL REFERENCES archives (id) ON DELETE CASCADE, title text, title_key text COLLATE "C" NOT NULL, search_text text NOT NULL, folder text NOT NULL, camera_make text, camera_model text, camera_serial text, lens text, first_capture timestamp, last_capture timestamp, frame_count integer NOT NULL DEFAULT 0, median_interval double precision, total_bytes bigint NOT NULL DEFAULT 0, status integer NOT NULL DEFAULT 0, rendered_count integer NOT NULL DEFAULT 0, verified_at timestamptz, hero_frame_id uuid, rating integer NOT NULL DEFAULT 0, notes text, settings_json jsonb, analysis_json jsonb, origin integer NOT NULL DEFAULT 0, sync_state integer NOT NULL DEFAULT 0, settings_version integer NOT NULL DEFAULT 0, frames_version integer NOT NULL DEFAULT 0, owner_user_id uuid, warning_count integer NOT NULL DEFAULT 0, updated_at timestamptz NOT NULL ); CREATE INDEX ix_sequences_archive_capture ON sequences (archive_id, first_capture, id); CREATE INDEX ix_sequences_capture ON sequences (first_capture, id); CREATE INDEX ix_sequences_camera ON sequences (camera_model); CREATE TABLE frames ( id uuid NOT NULL PRIMARY KEY, sequence_id uuid NOT NULL REFERENCES sequences (id) ON DELETE CASCADE, idx integer NOT NULL, relative_path text NOT NULL, file_name text NOT NULL, original_file_name text, size bigint NOT NULL DEFAULT 0, sha256 text, captured_at timestamp, utc_offset_minutes integer, timestamp_source integer NOT NULL DEFAULT 0, exposure_time double precision, f_number double precision, iso integer, focal_length double precision, exposure_bias double precision, white_balance integer, orientation integer NOT NULL DEFAULT 1, width integer NOT NULL DEFAULT 0, height integer NOT NULL DEFAULT 0, format integer NOT NULL DEFAULT 0, preview_offset bigint, preview_length bigint, mean_log_luminance double precision, clip_low double precision, clip_high double precision, flags integer NOT NULL DEFAULT 0 ); CREATE UNIQUE INDEX ux_frames_sequence_idx ON frames (sequence_id, idx); CREATE INDEX ix_frames_sha256 ON frames (sha256); CREATE INDEX ix_frames_captured_at ON frames (captured_at); CREATE TABLE renders ( id uuid NOT NULL PRIMARY KEY, sequence_id uuid NOT NULL REFERENCES sequences (id) ON DELETE CASCADE, preset_name text NOT NULL, settings_json jsonb, output_path text NOT NULL, codec text, width integer NOT NULL DEFAULT 0, height integer NOT NULL DEFAULT 0, fps double precision NOT NULL DEFAULT 0, duration_seconds double precision NOT NULL DEFAULT 0, bytes bigint NOT NULL DEFAULT 0, created timestamptz NOT NULL, produced_by text ); CREATE INDEX ix_renders_sequence ON renders (sequence_id, created); CREATE TABLE jobs ( id uuid NOT NULL PRIMARY KEY, kind integer NOT NULL, target_ids text NOT NULL DEFAULT '', target_label text, parameters_json jsonb, state integer NOT NULL, priority integer NOT NULL, progress_done bigint NOT NULL DEFAULT 0, progress_total bigint NOT NULL DEFAULT 0, items_per_second double precision, eta_seconds double precision, checkpoint text, log_tail text, created_by uuid, node_id text, attempts integer NOT NULL DEFAULT 0, error text, created timestamptz NOT NULL, started timestamptz, finished timestamptz ); CREATE INDEX ix_jobs_queue ON jobs (state, priority DESC, created, id); CREATE INDEX ix_jobs_state_created ON jobs (state, created, id); CREATE INDEX ix_jobs_finished ON jobs (finished); CREATE TABLE media_cards ( id uuid NOT NULL PRIMARY KEY, identity text NOT NULL UNIQUE, volume_serial bigint NOT NULL, label text, capacity bigint NOT NULL DEFAULT 0, nickname text, camera_make text, camera_model text, camera_serial text, profile_json jsonb, auto_import boolean NOT NULL DEFAULT FALSE, first_seen timestamptz NOT NULL, last_seen timestamptz NOT NULL ); CREATE TABLE import_history ( card_id uuid NOT NULL REFERENCES media_cards (id) ON DELETE CASCADE, path text NOT NULL, size bigint NOT NULL, mtime timestamptz NOT NULL, sha256 text, frame_id uuid REFERENCES frames (id) ON DELETE SET NULL, job_id uuid, imported_at timestamptz NOT NULL, PRIMARY KEY (card_id, path, size, mtime) ); CREATE INDEX ix_import_history_frame ON import_history (frame_id); CREATE TABLE tokens ( token_hash text NOT NULL PRIMARY KEY, user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, client_name text, created timestamptz NOT NULL, expires timestamptz NOT NULL, last_used timestamptz NOT NULL, revoked boolean NOT NULL DEFAULT FALSE ); CREATE INDEX ix_tokens_user ON tokens (user_id); CREATE INDEX ix_tokens_expires ON tokens (expires); CREATE TABLE render_nodes ( id uuid NOT NULL PRIMARY KEY, user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, client_name text NOT NULL, capabilities_json jsonb, last_heartbeat timestamptz NOT NULL, state text NOT NULL ); CREATE INDEX ix_render_nodes_user ON render_nodes (user_id); CREATE INDEX ix_render_nodes_heartbeat ON render_nodes (last_heartbeat); CREATE TABLE settings ( scope text NOT NULL, scope_id text NOT NULL DEFAULT '', key text NOT NULL, value_json jsonb NOT NULL, updated_at timestamptz NOT NULL, PRIMARY KEY (scope, scope_id, key) ); CREATE TABLE tags ( id uuid NOT NULL PRIMARY KEY, name text NOT NULL, name_key text COLLATE "C" NOT NULL UNIQUE ); CREATE TABLE sequence_tags ( sequence_id uuid NOT NULL REFERENCES sequences (id) ON DELETE CASCADE, tag_id uuid NOT NULL REFERENCES tags (id) ON DELETE CASCADE, PRIMARY KEY (sequence_id, tag_id) ); CREATE INDEX ix_sequence_tags_tag ON sequence_tags (tag_id); CREATE TABLE audit_log ( id uuid NOT NULL PRIMARY KEY, at timestamptz NOT NULL, user_id uuid, username text, action text NOT NULL, target_kind text, target_id text, details text, remote_address text ); CREATE INDEX ix_audit_log_at ON audit_log (at, id); CREATE INDEX ix_audit_log_user ON audit_log (user_id, at); CREATE TABLE uploads ( id uuid NOT NULL PRIMARY KEY, user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, sequence_id uuid REFERENCES sequences (id) ON DELETE SET NULL, file_name text NOT NULL, size bigint NOT NULL, sha256 text NOT NULL, captured_at timestamp, chunk_size integer NOT NULL, received bytea NOT NULL, received_count integer NOT NULL DEFAULT 0, state integer NOT NULL, frame_id uuid, created timestamptz NOT NULL, updated_at timestamptz NOT NULL ); CREATE INDEX ix_uploads_user ON uploads (user_id, created); CREATE INDEX ix_uploads_state ON uploads (state, updated_at); CREATE INDEX ix_uploads_sha256 ON uploads (sha256); CREATE INDEX ix_uploads_sequence ON uploads (sequence_id);