diff --git a/src/main/infra/db.test.ts b/src/main/infra/db.test.ts index 6577918..544a8c1 100644 --- a/src/main/infra/db.test.ts +++ b/src/main/infra/db.test.ts @@ -72,6 +72,24 @@ describe('openDatabase', () => { expect(c?.c).toBe(2); }); + test('chunk_index table accepts insert + UNIQUE(item_id, chunk_seq)', () => { + db = openDatabase(path.join(tmpDir, 'chunk.db')); + db.run( + 'INSERT INTO chunk_index(item_id, chunk_seq, sha1_hex, bytes) VALUES (?, ?, ?, ?)', + ['item1', 0, 'abc123', 1024] + ); + const handle = db; + expect(() => { + handle.run( + 'INSERT INTO chunk_index(item_id, chunk_seq, sha1_hex, bytes) VALUES (?, ?, ?, ?)', + ['item1', 0, 'different', 2048] + ); + }).toThrow(); // UNIQUE violation + const rows = handle.all<{ sha1_hex: string }>('SELECT sha1_hex FROM chunk_index WHERE item_id = ?', ['item1']); + expect(rows).toHaveLength(1); + expect(rows[0].sha1_hex).toBe('abc123'); + }); + test('oauth_accounts table exists and accepts insert', () => { db = openDatabase(path.join(tmpDir, 'oauth.db')); db.run( diff --git a/src/main/infra/schema-v5.ts b/src/main/infra/schema-v5.ts index 0df1612..72c1a61 100644 --- a/src/main/infra/schema-v5.ts +++ b/src/main/infra/schema-v5.ts @@ -64,6 +64,19 @@ CREATE TABLE IF NOT EXISTS archive_files ( CREATE INDEX IF NOT EXISTS idx_archive_streamer ON archive_files(streamer_login); +CREATE TABLE IF NOT EXISTS chunk_index ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + item_id TEXT NOT NULL, + chunk_seq INTEGER NOT NULL, + sha1_hex TEXT NOT NULL, + bytes INTEGER NOT NULL, + created_at INTEGER NOT NULL DEFAULT (strftime('%s','now')), + UNIQUE(item_id, chunk_seq) +); + +CREATE INDEX IF NOT EXISTS idx_chunk_item ON chunk_index(item_id); +CREATE INDEX IF NOT EXISTS idx_chunk_sha1 ON chunk_index(sha1_hex); + CREATE TABLE IF NOT EXISTS oauth_accounts ( id INTEGER PRIMARY KEY AUTOINCREMENT, provider TEXT NOT NULL,