Skip to content

Commit 3ecce42

Browse files
committed
refactor(memory): hoist PORTABLE_TABLES to shared module
1 parent b08e78a commit 3ecce42

2 files changed

Lines changed: 62 additions & 48 deletions

File tree

src/memory/retrieval/store/Brain.ts

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
DDL_ARCHIVE_ACCESS_LOG_IDX,
4747
} from '../../archive/SqlStorageMemoryArchive.js';
4848
import { migrateV1ToV2 } from './migrations/v1-to-v2.js';
49+
import { PORTABLE_TABLES, PORTABLE_TABLE_PRIMARY_KEYS } from './portable-tables.js';
4950

5051
/**
5152
* Derive a stable brain identifier from the database file path.
@@ -942,51 +943,5 @@ export class Brain {
942943
}
943944
}
944945

945-
// ---------------------------------------------------------------------------
946-
// Portable-artifact constants
947-
// ---------------------------------------------------------------------------
948-
949-
/**
950-
* Tables exported and imported by `Brain.exportToSqlite` / `importFromSqlite`.
951-
* Order matters for import: parents before children to satisfy FKs.
952-
*/
953-
const PORTABLE_TABLES = [
954-
'brain_meta',
955-
'memory_traces',
956-
'knowledge_nodes',
957-
'knowledge_edges',
958-
'documents',
959-
'document_chunks',
960-
'document_images',
961-
'consolidation_log',
962-
'retrieval_feedback',
963-
'conversations',
964-
'messages',
965-
'prospective_items',
966-
'archived_traces',
967-
'archive_access_log',
968-
] as const;
969-
970-
/**
971-
* Composite primary key columns for each portable table, used by
972-
* `dialect.insertOrReplace` as the conflict target during merge import.
973-
*
974-
* Tables with `INTEGER PRIMARY KEY AUTOINCREMENT` (consolidation_log,
975-
* retrieval_feedback) use `id` alone since their PK is system-generated.
976-
*/
977-
const PORTABLE_TABLE_PRIMARY_KEYS: Record<string, string> = {
978-
brain_meta: 'brain_id, key',
979-
memory_traces: 'brain_id, id',
980-
knowledge_nodes: 'brain_id, id',
981-
knowledge_edges: 'brain_id, id',
982-
documents: 'brain_id, id',
983-
document_chunks: 'brain_id, id',
984-
document_images: 'brain_id, id',
985-
consolidation_log: 'id',
986-
retrieval_feedback: 'id',
987-
conversations: 'brain_id, id',
988-
messages: 'brain_id, id',
989-
prospective_items: 'brain_id, id',
990-
archived_traces: 'brain_id, trace_id',
991-
archive_access_log: 'brain_id, trace_id, accessed_at',
992-
};
946+
// PORTABLE_TABLES + PORTABLE_TABLE_PRIMARY_KEYS moved to ./portable-tables.ts
947+
// (single source of truth shared with v1-to-v2 migration + postgres test cleanup).
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @fileoverview Single source of truth for the list of brain-owned tables that
3+
* are exported, imported, and migrated as a portable artifact.
4+
*
5+
* Three call sites import from here:
6+
* 1. `Brain.exportToSqlite` / `importFromSqlite` (export/import row copies)
7+
* 2. `migrations/v1-to-v2.ts` (migration walk order: parents before children for FKs)
8+
* 3. `__tests__/Brain.postgres.test.ts` (cleanup between tests)
9+
*
10+
* Adding a new portable table requires editing only this file.
11+
*
12+
* @module memory/retrieval/store/portable-tables
13+
*/
14+
15+
/**
16+
* Order matters: parents before children to satisfy FK constraints during import.
17+
*/
18+
export const PORTABLE_TABLES = [
19+
'brain_meta',
20+
'memory_traces',
21+
'knowledge_nodes',
22+
'knowledge_edges',
23+
'documents',
24+
'document_chunks',
25+
'document_images',
26+
'consolidation_log',
27+
'retrieval_feedback',
28+
'conversations',
29+
'messages',
30+
'prospective_items',
31+
'archived_traces',
32+
'archive_access_log',
33+
] as const;
34+
35+
/**
36+
* Composite primary key columns for each portable table, used by
37+
* `dialect.insertOrReplace` as the conflict target during merge import.
38+
*
39+
* Tables with `INTEGER PRIMARY KEY AUTOINCREMENT` (consolidation_log,
40+
* retrieval_feedback) use `id` alone since their PK is system-generated.
41+
*/
42+
export const PORTABLE_TABLE_PRIMARY_KEYS: Record<string, string> = {
43+
brain_meta: 'brain_id, key',
44+
memory_traces: 'brain_id, id',
45+
knowledge_nodes: 'brain_id, id',
46+
knowledge_edges: 'brain_id, id',
47+
documents: 'brain_id, id',
48+
document_chunks: 'brain_id, id',
49+
document_images: 'brain_id, id',
50+
consolidation_log: 'id',
51+
retrieval_feedback: 'id',
52+
conversations: 'brain_id, id',
53+
messages: 'brain_id, id',
54+
prospective_items: 'brain_id, id',
55+
archived_traces: 'brain_id, trace_id',
56+
archive_access_log: 'brain_id, trace_id, accessed_at',
57+
};
58+
59+
export type PortableTable = typeof PORTABLE_TABLES[number];

0 commit comments

Comments
 (0)