summaryrefslogtreecommitdiff
path: root/makima/migrations/20260508000000_add_contract_sequencing_columns.sql
blob: a2592855e76e2f478d1302bd2f8a903e074525fa (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
-- Add the contract sequencing columns to directive_documents.
--
-- Background: the directive_documents table was always intended to BE the
-- user-facing "contract" concept (see migration 20260502000000:
-- "the user calls these 'directive contracts'"). The unified directive
-- workflow now formalises that — a directive holds a sequence of
-- contracts; each contract is a spec body whose execution is driven by
-- tasks in the directive's shared worktree.
--
-- This migration is purely additive. It does NOT rename the table —
-- there is a legacy `contracts` table from the old contracts system
-- (still present, archived in 20260501000000), and renaming
-- `directive_documents → contracts` would collide with it. The actual
-- table rename lands once the legacy table is dropped (Phase 5).
--
-- For now: the Rust struct, API endpoints, and frontend types are all
-- renamed to "Contract" so the user-visible surface matches their model.
-- The DB column names (`directive_documents`, `directive_document_id`)
-- stay put.
--
-- Two new columns:
--   * position  — integer ordering inside the directive (queue position).
--                 Backfilled by created_at so existing data keeps the
--                 visual order users see today.
--   * merge_mode — 'shared' (commits go to the directive's branch) or
--                 'own_pr' (each contract gets its own branch + PR).
--                 Default 'shared'; per-contract toggle in the UI lifts
--                 to 'own_pr'.

ALTER TABLE directive_documents
    ADD COLUMN position INT NOT NULL DEFAULT 0;

WITH ranked AS (
    SELECT id,
           ROW_NUMBER() OVER (
               PARTITION BY directive_id
               ORDER BY created_at ASC, id ASC
           ) - 1 AS rn
      FROM directive_documents
)
UPDATE directive_documents d
   SET position = r.rn
  FROM ranked r
 WHERE d.id = r.id;

ALTER TABLE directive_documents
    ADD COLUMN merge_mode VARCHAR(16) NOT NULL DEFAULT 'shared'
    CHECK (merge_mode IN ('shared', 'own_pr'));