Database Schema & Zero-Downtime Migration Plan


1. Entity-Relationship Diagram

[Diagram]

2. Indexing Strategy

Table Index Name Columns Type Query Pattern Served
documents idx_docs_workspace_updated (workspace_id, updated_at DESC) WHERE deleted_at IS NULL Partial B-Tree Dashboard recent files list
document_revisions uq_revisions_doc_ver (document_id, version_number) Unique B-Tree Optimistic concurrency & history lookup
share_links uq_shares_short_slug (short_slug) Unique B-Tree Public /s/:slug hot path
share_links idx_shares_expires (expires_at) WHERE expires_at IS NOT NULL Partial B-Tree Nightly expired-link cleanup job

3. Expand-and-Contract Migration Plan

To migrate documents.owner_id to documents.workspace_id with zero downtime, execute across four deploy phases:

Phase 1 — Expand (Safe Deploy)

SET lock_timeout = '3s';

ALTER TABLE documents
  ADD COLUMN workspace_id uuid;

CREATE INDEX CONCURRENTLY idx_docs_workspace_updated
  ON documents (workspace_id, updated_at DESC)
  WHERE deleted_at IS NULL;

Phase 2 — Dual-Write & Backfill

Phase 3 — Validate Constraint Without Table Lock

ALTER TABLE documents
  ADD CONSTRAINT fk_documents_workspace
  FOREIGN KEY (workspace_id) REFERENCES workspaces(id)
  NOT VALID;

ALTER TABLE documents
  VALIDATE CONSTRAINT fk_documents_workspace;

Phase 4 — Contract


4. Rollback & Verification Checklist