rectilinear-core 0.7.0

Core engine for Linear issue intelligence — search, sync, embeddings
Documentation
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use anyhow::Result;
use rusqlite::Connection;

pub fn run_migrations(conn: &Connection) -> Result<()> {
    conn.execute_batch(
        "CREATE TABLE IF NOT EXISTS schema_version (
            version INTEGER PRIMARY KEY
        );",
    )?;

    let current_version: i64 = conn
        .query_row(
            "SELECT COALESCE(MAX(version), 0) FROM schema_version",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0);

    if current_version < 1 {
        conn.execute_batch(MIGRATION_1)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (1)", [])?;
    }

    if current_version < 2 {
        conn.execute_batch(MIGRATION_2)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (2)", [])?;
    }

    if current_version < 3 {
        conn.execute_batch(MIGRATION_3)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (3)", [])?;
    }

    if current_version < 4 {
        conn.execute_batch(MIGRATION_4)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (4)", [])?;
    }

    if current_version < 5 {
        conn.execute_batch(MIGRATION_5)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (5)", [])?;
    }

    if current_version < 6 {
        conn.execute_batch(MIGRATION_6)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (6)", [])?;
    }

    if current_version < 7 {
        conn.execute_batch(MIGRATION_7)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (7)", [])?;
    }

    if current_version < 8 {
        conn.execute_batch(MIGRATION_8)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (8)", [])?;
    }

    if current_version < 9 {
        run_migration_9(conn)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (9)", [])?;
    }

    if current_version < 10 {
        run_migration_10(conn)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (10)", [])?;
    } else {
        // Development builds briefly recorded schema version 10 before all
        // project join tables were present. Re-running this idempotent migration
        // repairs those databases without requiring users to delete their cache.
        run_migration_10(conn)?;
    }

    if current_version < 11 {
        run_migration_11(conn)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (11)", [])?;
    }

    if current_version < 12 {
        run_migration_12(conn)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (12)", [])?;
    } else {
        // Repair databases created by development builds of schema 12 before
        // the latest-index scheduling token was added.
        run_migration_12(conn)?;
    }

    if current_version < 13 {
        run_migration_13(conn)?;
        conn.execute("INSERT INTO schema_version (version) VALUES (13)", [])?;
    } else {
        // Repair databases produced by development builds while embedding
        // source metadata was being introduced.
        run_migration_13(conn)?;
    }

    Ok(())
}

fn run_migration_13(conn: &Connection) -> Result<()> {
    add_column_if_missing(
        conn,
        "issues",
        "embedding_content_hash",
        "TEXT NOT NULL DEFAULT ''",
    )?;
    add_column_if_missing(
        conn,
        "chunks",
        "source_content_hash",
        "TEXT NOT NULL DEFAULT ''",
    )?;
    // Legacy content_hash includes all previously indexed embedding inputs.
    // It is a safe conservative seed: existing chunks have an empty source
    // hash and are therefore regenerated once, then replaced with the exact
    // title/description embedding hash.
    conn.execute(
        "UPDATE issues
         SET embedding_content_hash = content_hash
         WHERE embedding_content_hash = '' AND content_hash <> ''",
        [],
    )?;
    Ok(())
}

fn run_migration_12(conn: &Connection) -> Result<()> {
    add_column_if_missing(conn, "issues", "archived_at", "TEXT")?;
    add_column_if_missing(conn, "sync_state", "synced_through_at", "TEXT")?;
    conn.execute(
        "UPDATE sync_state
         SET synced_through_at = COALESCE(synced_through_at, last_updated_at)",
        [],
    )?;
    conn.execute_batch(MIGRATION_12)?;
    add_column_if_missing(conn, "issue_hydration_state", "index_sync_token", "TEXT")?;
    Ok(())
}

fn run_migration_11(conn: &Connection) -> Result<()> {
    add_column_if_missing(conn, "issues", "cycle_id", "TEXT")?;
    add_column_if_missing(conn, "issues", "cycle_name", "TEXT")?;
    add_column_if_missing(conn, "issues", "sync_token", "TEXT")?;
    add_column_if_missing(conn, "comments", "sync_token", "TEXT")?;
    add_column_if_missing(conn, "issue_relations", "sync_token", "TEXT")?;
    add_column_if_missing(conn, "labels", "sync_token", "TEXT")?;
    add_column_if_missing(conn, "issue_labels", "sync_token", "TEXT")?;
    add_column_if_missing(conn, "projects", "sync_token", "TEXT")?;
    add_column_if_missing(conn, "project_teams", "sync_token", "TEXT")?;
    add_column_if_missing(conn, "project_members", "sync_token", "TEXT")?;
    add_column_if_missing(conn, "project_labels", "sync_token", "TEXT")?;
    add_column_if_missing(conn, "project_milestones", "sync_token", "TEXT")?;
    conn.execute_batch(MIGRATION_11)?;
    Ok(())
}

fn run_migration_10(conn: &Connection) -> Result<()> {
    conn.execute_batch(MIGRATION_10)?;
    add_column_if_missing(conn, "issues", "project_id", "TEXT")?;
    add_column_if_missing(conn, "issues", "project_milestone_id", "TEXT")?;
    add_column_if_missing(conn, "issues", "project_milestone_name", "TEXT")?;
    conn.execute_batch(
        "CREATE INDEX IF NOT EXISTS idx_issues_project ON issues(workspace_id, project_id);
         CREATE INDEX IF NOT EXISTS idx_issues_project_milestone
             ON issues(workspace_id, project_milestone_id);",
    )?;
    Ok(())
}

fn run_migration_9(conn: &Connection) -> Result<()> {
    add_column_if_missing(conn, "comments", "updated_at", "TEXT")?;
    add_column_if_missing(conn, "comments", "parent_id", "TEXT")?;
    add_column_if_missing(conn, "comments", "url", "TEXT")?;
    conn.execute_batch(MIGRATION_9)?;
    Ok(())
}

fn add_column_if_missing(
    conn: &Connection,
    table: &str,
    column: &str,
    definition: &str,
) -> Result<()> {
    let mut stmt = conn.prepare(&format!("PRAGMA table_info({table})"))?;
    let columns = stmt.query_map([], |row| row.get::<_, String>(1))?;
    for existing in columns {
        if existing? == column {
            return Ok(());
        }
    }
    conn.execute_batch(&format!(
        "ALTER TABLE {table} ADD COLUMN {column} {definition};"
    ))?;
    Ok(())
}

const MIGRATION_9: &str = "
-- Preserve Linear comment thread metadata and track whether comments were
-- actually refreshed for an issue.
CREATE TABLE IF NOT EXISTS comment_sync_state (
    issue_id TEXT PRIMARY KEY REFERENCES issues(id) ON DELETE CASCADE,
    workspace_id TEXT NOT NULL REFERENCES workspaces(id),
    status TEXT NOT NULL,
    sync_error TEXT,
    synced_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_comment_sync_workspace ON comment_sync_state(workspace_id);
";

const MIGRATION_10: &str = "
-- Projects and their milestones are first-class cached resources. Linear is
-- still the source of truth; these tables make the hierarchy importable by
-- downstream clients without reconstructing it from issue names.
CREATE TABLE IF NOT EXISTS projects (
    id TEXT PRIMARY KEY,
    workspace_id TEXT NOT NULL REFERENCES workspaces(id),
    slug_id TEXT NOT NULL DEFAULT '',
    name TEXT NOT NULL,
    description TEXT NOT NULL DEFAULT '',
    content TEXT,
    icon TEXT,
    color TEXT NOT NULL DEFAULT '',
    status_id TEXT NOT NULL DEFAULT '',
    status_name TEXT NOT NULL DEFAULT '',
    status_type TEXT NOT NULL DEFAULT '',
    status_color TEXT NOT NULL DEFAULT '',
    priority INTEGER NOT NULL DEFAULT 0,
    start_date TEXT,
    target_date TEXT,
    lead_id TEXT,
    lead_name TEXT,
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    archived_at TEXT,
    url TEXT NOT NULL DEFAULT '',
    progress REAL NOT NULL DEFAULT 0,
    synced_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_projects_workspace ON projects(workspace_id);
CREATE INDEX IF NOT EXISTS idx_projects_workspace_name
    ON projects(workspace_id, name COLLATE NOCASE);

CREATE TABLE IF NOT EXISTS project_teams (
    project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    team_id TEXT NOT NULL,
    team_key TEXT NOT NULL,
    team_name TEXT NOT NULL,
    PRIMARY KEY (project_id, team_id)
);
CREATE INDEX IF NOT EXISTS idx_project_teams_key ON project_teams(team_key);

CREATE TABLE IF NOT EXISTS project_members (
    project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    user_id TEXT NOT NULL,
    user_name TEXT NOT NULL,
    PRIMARY KEY (project_id, user_id)
);

CREATE TABLE IF NOT EXISTS project_labels (
    project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    label_id TEXT NOT NULL,
    label_name TEXT NOT NULL,
    color TEXT NOT NULL DEFAULT '',
    description TEXT,
    PRIMARY KEY (project_id, label_id)
);
CREATE INDEX IF NOT EXISTS idx_project_labels_name
    ON project_labels(label_name COLLATE NOCASE);

CREATE TABLE IF NOT EXISTS project_milestones (
    id TEXT PRIMARY KEY,
    workspace_id TEXT NOT NULL REFERENCES workspaces(id),
    project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    name TEXT NOT NULL,
    description TEXT,
    target_date TEXT,
    status TEXT NOT NULL DEFAULT '',
    progress REAL NOT NULL DEFAULT 0,
    sort_order REAL NOT NULL DEFAULT 0,
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    archived_at TEXT,
    synced_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_project_milestones_project
    ON project_milestones(project_id, sort_order, target_date);
CREATE INDEX IF NOT EXISTS idx_project_milestones_workspace
    ON project_milestones(workspace_id);
";

const MIGRATION_11: &str = "
-- Bounded synchronization persists pages under a run token. Stale rows are
-- reconciled only after an entity family completes, so interrupted refreshes
-- never erase the last complete local view.
CREATE TABLE IF NOT EXISTS cycles (
    id TEXT PRIMARY KEY,
    workspace_id TEXT NOT NULL REFERENCES workspaces(id),
    team_id TEXT NOT NULL,
    team_key TEXT NOT NULL,
    number INTEGER NOT NULL DEFAULT 0,
    name TEXT,
    starts_at TEXT,
    ends_at TEXT,
    completed_at TEXT,
    archived_at TEXT,
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    sync_token TEXT,
    synced_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_cycles_workspace_team
    ON cycles(workspace_id, team_key, number);
CREATE INDEX IF NOT EXISTS idx_issues_cycle ON issues(workspace_id, cycle_id);
CREATE INDEX IF NOT EXISTS idx_issues_sync_token
    ON issues(workspace_id, team_key, sync_token);
CREATE INDEX IF NOT EXISTS idx_comments_sync_token
    ON comments(issue_id, sync_token);
CREATE INDEX IF NOT EXISTS idx_relations_sync_token
    ON issue_relations(issue_id, sync_token);

CREATE TABLE IF NOT EXISTS sync_family_state (
    workspace_id TEXT NOT NULL REFERENCES workspaces(id),
    team_key TEXT NOT NULL,
    family TEXT NOT NULL,
    status TEXT NOT NULL,
    cursor TEXT,
    page_size INTEGER,
    sync_token TEXT,
    error TEXT,
    updated_at TEXT NOT NULL DEFAULT (datetime('now')),
    PRIMARY KEY (workspace_id, team_key, family)
);

-- Membership fields and cycle inventory require a fresh full traversal once.
UPDATE sync_state SET full_sync_done = 0, last_updated_at = '1970-01-01T00:00:00Z';
";

const MIGRATION_12: &str = "
-- Progressive issue synchronization separates the authoritative, bounded
-- issue index from independently retryable rich-resource hydration.
CREATE TABLE IF NOT EXISTS issue_hydration_state (
    workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
    issue_id TEXT NOT NULL REFERENCES issues(id) ON DELETE CASCADE,
    resource TEXT NOT NULL,
    status TEXT NOT NULL,
    source_updated_at TEXT NOT NULL,
    queue_reason TEXT NOT NULL DEFAULT 'initial',
    index_sync_token TEXT,
    last_attempted_at TEXT,
    hydrated_at TEXT,
    attempt_count INTEGER NOT NULL DEFAULT 0,
    next_retry_at TEXT,
    last_error TEXT,
    PRIMARY KEY (workspace_id, issue_id, resource)
);
CREATE INDEX IF NOT EXISTS idx_hydration_pending
    ON issue_hydration_state(workspace_id, status, next_retry_at);
CREATE INDEX IF NOT EXISTS idx_hydration_issue
    ON issue_hydration_state(issue_id, resource);

-- Databases produced by the legacy all-at-once synchronizer already contain
-- rich issue fields, labels, and relations. Treat those resources as hydrated
-- so upgrading does not cause a needless full refetch.
INSERT OR IGNORE INTO issue_hydration_state (
    workspace_id, issue_id, resource, status, source_updated_at,
    queue_reason, hydrated_at
)
SELECT workspace_id, id, resource, 'hydrated', updated_at, 'migration',
       COALESCE(synced_at, datetime('now'))
FROM issues
CROSS JOIN (SELECT 'details' AS resource UNION ALL SELECT 'labels' UNION ALL SELECT 'relations');

-- Preserve the existing per-issue comment evidence and retry intent. The
-- compatibility table remains in place for older callers.
INSERT OR IGNORE INTO issue_hydration_state (
    workspace_id, issue_id, resource, status, source_updated_at,
    queue_reason, hydrated_at, next_retry_at, last_error
)
SELECT i.workspace_id, i.id, 'comments',
       CASE c.status
           WHEN 'synced' THEN 'hydrated'
           WHEN 'none_found' THEN 'hydrated'
           WHEN 'permission_denied' THEN 'permission_denied'
           WHEN 'unavailable' THEN 'retryable'
           ELSE 'pending'
       END,
       i.updated_at,
       'migration',
       CASE WHEN c.status IN ('synced', 'none_found') THEN c.synced_at END,
       CASE WHEN c.status = 'unavailable' THEN datetime('now') END,
       c.sync_error
FROM issues i
LEFT JOIN comment_sync_state c ON c.issue_id = i.id;
";

const MIGRATION_8: &str = "
-- Workspace label catalog
CREATE TABLE IF NOT EXISTS labels (
    id TEXT PRIMARY KEY,
    workspace_id TEXT NOT NULL REFERENCES workspaces(id),
    name TEXT NOT NULL,
    color TEXT,
    parent_id TEXT,
    UNIQUE (workspace_id, name COLLATE NOCASE)
);
CREATE INDEX IF NOT EXISTS idx_labels_workspace ON labels(workspace_id);

-- Issue ↔ label join table
CREATE TABLE IF NOT EXISTS issue_labels (
    issue_id TEXT NOT NULL REFERENCES issues(id) ON DELETE CASCADE,
    label_id TEXT NOT NULL REFERENCES labels(id) ON DELETE CASCADE,
    PRIMARY KEY (issue_id, label_id)
);
CREATE INDEX IF NOT EXISTS idx_issue_labels_label ON issue_labels(label_id);

-- Force full re-sync so issue_labels gets populated for existing issues.
UPDATE sync_state SET full_sync_done = 0, last_updated_at = '1970-01-01T00:00:00Z';
";

const MIGRATION_7: &str = "
-- Workspace registry
CREATE TABLE IF NOT EXISTS workspaces (
    id TEXT PRIMARY KEY,
    linear_org_id TEXT,
    display_name TEXT,
    created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

-- Seed the default workspace for existing data
INSERT OR IGNORE INTO workspaces (id) VALUES ('default');

-- Add workspace_id to issues
ALTER TABLE issues ADD COLUMN workspace_id TEXT NOT NULL DEFAULT 'default';

-- Add workspace_id to sync_state (recreate since altering PK is not supported)
CREATE TABLE sync_state_new (
    workspace_id TEXT NOT NULL REFERENCES workspaces(id),
    team_key TEXT NOT NULL,
    last_updated_at TEXT NOT NULL,
    full_sync_done INTEGER NOT NULL DEFAULT 0,
    last_synced_at TEXT,
    PRIMARY KEY (workspace_id, team_key)
);
INSERT INTO sync_state_new (workspace_id, team_key, last_updated_at, full_sync_done, last_synced_at)
    SELECT 'default', team_key, last_updated_at, full_sync_done, last_synced_at FROM sync_state;
DROP TABLE sync_state;
ALTER TABLE sync_state_new RENAME TO sync_state;

-- Add workspace_id to comments
ALTER TABLE comments ADD COLUMN workspace_id TEXT NOT NULL DEFAULT 'default';

-- New indices
CREATE INDEX IF NOT EXISTS idx_issues_workspace ON issues(workspace_id);
CREATE INDEX IF NOT EXISTS idx_issues_workspace_team ON issues(workspace_id, team_key);
CREATE INDEX IF NOT EXISTS idx_comments_workspace ON comments(workspace_id);
";

// Add branch_name to issues, model_name to chunks
const MIGRATION_6: &str = "
ALTER TABLE issues ADD COLUMN branch_name TEXT;
ALTER TABLE chunks ADD COLUMN model_name TEXT NOT NULL DEFAULT '';
";

// Add last_synced_at to sync_state
const MIGRATION_5: &str = "
ALTER TABLE sync_state ADD COLUMN last_synced_at TEXT;
";

// Add issue_relations table
const MIGRATION_4: &str = "
CREATE TABLE IF NOT EXISTS issue_relations (
    id TEXT PRIMARY KEY,
    issue_id TEXT NOT NULL REFERENCES issues(id) ON DELETE CASCADE,
    related_issue_id TEXT NOT NULL,
    related_issue_identifier TEXT NOT NULL,
    relation_type TEXT NOT NULL,
    UNIQUE(issue_id, related_issue_id, relation_type)
);
CREATE INDEX IF NOT EXISTS idx_relations_issue ON issue_relations(issue_id);
CREATE INDEX IF NOT EXISTS idx_relations_related ON issue_relations(related_issue_id);
";

// Add url column to issues
const MIGRATION_3: &str = "
ALTER TABLE issues ADD COLUMN url TEXT NOT NULL DEFAULT '';
";

// Fix contentless FTS5 triggers: use 'delete' command instead of DELETE FROM
const MIGRATION_2: &str = "
DROP TRIGGER IF EXISTS issues_au;
DROP TRIGGER IF EXISTS issues_ad;

-- Rebuild FTS index from scratch (old triggers may have left it corrupt)
DELETE FROM issues_fts;
INSERT INTO issues_fts(rowid, title, description, labels_text)
    SELECT rowid, title, COALESCE(description, ''), labels_json FROM issues;

CREATE TRIGGER issues_au AFTER UPDATE ON issues BEGIN
    INSERT INTO issues_fts(issues_fts, rowid, title, description, labels_text)
    VALUES ('delete', old.rowid, old.title, COALESCE(old.description, ''), old.labels_json);
    INSERT INTO issues_fts(rowid, title, description, labels_text)
    VALUES (new.rowid, new.title, COALESCE(new.description, ''), new.labels_json);
END;

CREATE TRIGGER issues_ad AFTER DELETE ON issues BEGIN
    INSERT INTO issues_fts(issues_fts, rowid, title, description, labels_text)
    VALUES ('delete', old.rowid, old.title, COALESCE(old.description, ''), old.labels_json);
END;
";

const MIGRATION_1: &str = "
CREATE TABLE IF NOT EXISTS issues (
    id TEXT PRIMARY KEY,
    identifier TEXT NOT NULL UNIQUE,
    team_key TEXT NOT NULL,
    title TEXT NOT NULL,
    description TEXT,
    state_name TEXT NOT NULL DEFAULT '',
    state_type TEXT NOT NULL DEFAULT '',
    priority INTEGER NOT NULL DEFAULT 0,
    assignee_name TEXT,
    project_name TEXT,
    labels_json TEXT NOT NULL DEFAULT '[]',
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    content_hash TEXT NOT NULL DEFAULT '',
    synced_at TEXT
);

CREATE INDEX IF NOT EXISTS idx_issues_team ON issues(team_key);
CREATE INDEX IF NOT EXISTS idx_issues_updated ON issues(updated_at);
CREATE INDEX IF NOT EXISTS idx_issues_identifier ON issues(identifier);

CREATE VIRTUAL TABLE IF NOT EXISTS issues_fts USING fts5(
    title,
    description,
    labels_text,
    content='',
    tokenize='porter unicode61'
);

-- Triggers to keep FTS in sync
CREATE TRIGGER IF NOT EXISTS issues_ai AFTER INSERT ON issues BEGIN
    INSERT INTO issues_fts(rowid, title, description, labels_text)
    VALUES (new.rowid, new.title, COALESCE(new.description, ''), new.labels_json);
END;

CREATE TRIGGER IF NOT EXISTS issues_au AFTER UPDATE ON issues BEGIN
    INSERT INTO issues_fts(issues_fts, rowid, title, description, labels_text)
    VALUES ('delete', old.rowid, old.title, COALESCE(old.description, ''), old.labels_json);
    INSERT INTO issues_fts(rowid, title, description, labels_text)
    VALUES (new.rowid, new.title, COALESCE(new.description, ''), new.labels_json);
END;

CREATE TRIGGER IF NOT EXISTS issues_ad AFTER DELETE ON issues BEGIN
    INSERT INTO issues_fts(issues_fts, rowid, title, description, labels_text)
    VALUES ('delete', old.rowid, old.title, COALESCE(old.description, ''), old.labels_json);
END;

CREATE TABLE IF NOT EXISTS chunks (
    issue_id TEXT NOT NULL REFERENCES issues(id) ON DELETE CASCADE,
    chunk_index INTEGER NOT NULL,
    chunk_text TEXT NOT NULL,
    embedding BLOB NOT NULL,
    PRIMARY KEY (issue_id, chunk_index)
);

CREATE TABLE IF NOT EXISTS comments (
    id TEXT PRIMARY KEY,
    issue_id TEXT NOT NULL REFERENCES issues(id) ON DELETE CASCADE,
    body TEXT NOT NULL,
    user_name TEXT,
    created_at TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_comments_issue ON comments(issue_id);

CREATE TABLE IF NOT EXISTS sync_state (
    team_key TEXT PRIMARY KEY,
    last_updated_at TEXT NOT NULL,
    full_sync_done INTEGER NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS metadata (
    key TEXT PRIMARY KEY,
    value TEXT NOT NULL
);
";