soulbridge 0.0.20 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
1
2
|
import type { SoulStore } from './store.ts';
|
|
2
3
|
export declare const SCHEMA_VERSION = 2;
|
|
3
4
|
export declare const AFFECT_RETENTION_MS: number;
|
|
@@ -6,4 +7,13 @@ export declare class DbNewerThanBinaryError extends Error {
|
|
|
6
7
|
readonly dbVersion: number;
|
|
7
8
|
constructor(dbVersion: number);
|
|
8
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Copy the database aside before a schema upgrade.
|
|
12
|
+
*
|
|
13
|
+
* In WAL mode the most recent commits can live in the `-wal` sidecar, so a copy
|
|
14
|
+
* of the main file alone can be missing data exactly when it matters. Checkpoint
|
|
15
|
+
* first (TRUNCATE moves the WAL back into the main file and empties it), then
|
|
16
|
+
* copy. Exported for the upgrade test.
|
|
17
|
+
*/
|
|
18
|
+
export declare function backupBeforeUpgrade(db: DatabaseSync, file: string, from: number): void;
|
|
9
19
|
export declare function openSqlite(file: string): SoulStore;
|
|
@@ -148,12 +148,22 @@ function upgradeToV3(db) {
|
|
|
148
148
|
)`);
|
|
149
149
|
db.exec('CREATE INDEX IF NOT EXISTS idx_corrections ON corrections(soul_id, user_id, at DESC)');
|
|
150
150
|
}
|
|
151
|
-
|
|
151
|
+
/**
|
|
152
|
+
* Copy the database aside before a schema upgrade.
|
|
153
|
+
*
|
|
154
|
+
* In WAL mode the most recent commits can live in the `-wal` sidecar, so a copy
|
|
155
|
+
* of the main file alone can be missing data exactly when it matters. Checkpoint
|
|
156
|
+
* first (TRUNCATE moves the WAL back into the main file and empties it), then
|
|
157
|
+
* copy. Exported for the upgrade test.
|
|
158
|
+
*/
|
|
159
|
+
export function backupBeforeUpgrade(db, file, from) {
|
|
152
160
|
if (file === ':memory:' || !existsSync(file))
|
|
153
161
|
return;
|
|
154
162
|
const backup = `${file}.bak-v${from}`;
|
|
155
|
-
if (
|
|
156
|
-
|
|
163
|
+
if (existsSync(backup))
|
|
164
|
+
return;
|
|
165
|
+
db.exec('PRAGMA wal_checkpoint(TRUNCATE);');
|
|
166
|
+
copyFileSync(file, backup);
|
|
157
167
|
}
|
|
158
168
|
export function openSqlite(file) {
|
|
159
169
|
const db = new DatabaseSync(file);
|
|
@@ -166,7 +176,7 @@ export function openSqlite(file) {
|
|
|
166
176
|
throw new DbNewerThanBinaryError(version);
|
|
167
177
|
}
|
|
168
178
|
if (version < SCHEMA_VERSION) {
|
|
169
|
-
backupBeforeUpgrade(file, version);
|
|
179
|
+
backupBeforeUpgrade(db, file, version);
|
|
170
180
|
if (version < 2)
|
|
171
181
|
upgradeToV2(db);
|
|
172
182
|
if (version < 3)
|