@stackmemoryai/stackmemory 0.5.29 → 0.5.31
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.
- package/README.md +53 -32
- package/dist/core/database/batch-operations.js +29 -4
- package/dist/core/database/batch-operations.js.map +2 -2
- package/dist/core/database/connection-pool.js +13 -2
- package/dist/core/database/connection-pool.js.map +2 -2
- package/dist/core/database/migration-manager.js +130 -34
- package/dist/core/database/migration-manager.js.map +2 -2
- package/dist/core/database/paradedb-adapter.js +23 -7
- package/dist/core/database/paradedb-adapter.js.map +2 -2
- package/dist/core/database/query-router.js +8 -3
- package/dist/core/database/query-router.js.map +2 -2
- package/dist/core/database/sqlite-adapter.js +152 -33
- package/dist/core/database/sqlite-adapter.js.map +2 -2
- package/dist/integrations/linear/auth.js +34 -20
- package/dist/integrations/linear/auth.js.map +2 -2
- package/dist/integrations/linear/auto-sync.js +18 -8
- package/dist/integrations/linear/auto-sync.js.map +2 -2
- package/dist/integrations/linear/client.js +42 -9
- package/dist/integrations/linear/client.js.map +2 -2
- package/dist/integrations/linear/migration.js +94 -36
- package/dist/integrations/linear/migration.js.map +2 -2
- package/dist/integrations/linear/oauth-server.js +77 -34
- package/dist/integrations/linear/oauth-server.js.map +2 -2
- package/dist/integrations/linear/rest-client.js +13 -3
- package/dist/integrations/linear/rest-client.js.map +2 -2
- package/dist/integrations/linear/sync-service.js +18 -15
- package/dist/integrations/linear/sync-service.js.map +2 -2
- package/dist/integrations/linear/sync.js +12 -4
- package/dist/integrations/linear/sync.js.map +2 -2
- package/dist/integrations/linear/unified-sync.js +33 -8
- package/dist/integrations/linear/unified-sync.js.map +2 -2
- package/dist/integrations/linear/webhook-handler.js +5 -1
- package/dist/integrations/linear/webhook-handler.js.map +2 -2
- package/dist/integrations/linear/webhook-server.js +7 -7
- package/dist/integrations/linear/webhook-server.js.map +2 -2
- package/dist/integrations/linear/webhook.js +9 -2
- package/dist/integrations/linear/webhook.js.map +2 -2
- package/dist/integrations/mcp/schemas.js +147 -0
- package/dist/integrations/mcp/schemas.js.map +7 -0
- package/dist/integrations/mcp/server.js +19 -3
- package/dist/integrations/mcp/server.js.map +2 -2
- package/package.json +1 -1
|
@@ -4,6 +4,7 @@ const __filename = __fileURLToPath(import.meta.url);
|
|
|
4
4
|
const __dirname = __pathDirname(__filename);
|
|
5
5
|
import { EventEmitter } from "events";
|
|
6
6
|
import { logger } from "../monitoring/logger.js";
|
|
7
|
+
import { DatabaseError, ErrorCode, wrapError } from "../errors/index.js";
|
|
7
8
|
class MigrationManager extends EventEmitter {
|
|
8
9
|
config;
|
|
9
10
|
progress;
|
|
@@ -18,16 +19,32 @@ class MigrationManager extends EventEmitter {
|
|
|
18
19
|
}
|
|
19
20
|
validateConfig(config) {
|
|
20
21
|
if (!config.sourceAdapter || !config.targetAdapter) {
|
|
21
|
-
throw new
|
|
22
|
+
throw new DatabaseError(
|
|
23
|
+
"Source and target adapters are required",
|
|
24
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
25
|
+
{ reason: "missing_adapters" }
|
|
26
|
+
);
|
|
22
27
|
}
|
|
23
28
|
if (config.batchSize && (config.batchSize < 1 || config.batchSize > 1e4)) {
|
|
24
|
-
throw new
|
|
29
|
+
throw new DatabaseError(
|
|
30
|
+
"Batch size must be between 1 and 10000",
|
|
31
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
32
|
+
{ batchSize: config.batchSize }
|
|
33
|
+
);
|
|
25
34
|
}
|
|
26
35
|
if (config.retryAttempts && (config.retryAttempts < 0 || config.retryAttempts > 10)) {
|
|
27
|
-
throw new
|
|
36
|
+
throw new DatabaseError(
|
|
37
|
+
"Retry attempts must be between 0 and 10",
|
|
38
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
39
|
+
{ retryAttempts: config.retryAttempts }
|
|
40
|
+
);
|
|
28
41
|
}
|
|
29
42
|
if (config.retryDelayMs && (config.retryDelayMs < 0 || config.retryDelayMs > 3e4)) {
|
|
30
|
-
throw new
|
|
43
|
+
throw new DatabaseError(
|
|
44
|
+
"Retry delay must be between 0 and 30000ms",
|
|
45
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
46
|
+
{ retryDelayMs: config.retryDelayMs }
|
|
47
|
+
);
|
|
31
48
|
}
|
|
32
49
|
}
|
|
33
50
|
normalizeConfig(config) {
|
|
@@ -118,7 +135,11 @@ class MigrationManager extends EventEmitter {
|
|
|
118
135
|
fallbackOnError: true
|
|
119
136
|
}) {
|
|
120
137
|
if (this.isRunning) {
|
|
121
|
-
throw new
|
|
138
|
+
throw new DatabaseError(
|
|
139
|
+
"Migration already in progress",
|
|
140
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
141
|
+
{ reason: "already_running" }
|
|
142
|
+
);
|
|
122
143
|
}
|
|
123
144
|
this.isRunning = true;
|
|
124
145
|
this.abortController = new AbortController();
|
|
@@ -153,7 +174,12 @@ class MigrationManager extends EventEmitter {
|
|
|
153
174
|
logger.error("Rollback failed:", this.sanitizeError(rollbackError));
|
|
154
175
|
}
|
|
155
176
|
}
|
|
156
|
-
const userError = new
|
|
177
|
+
const userError = new DatabaseError(
|
|
178
|
+
"Migration failed. Check logs for details.",
|
|
179
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
180
|
+
{ phase: this.progress.phase },
|
|
181
|
+
error instanceof Error ? error : void 0
|
|
182
|
+
);
|
|
157
183
|
this.emit("failed", userError);
|
|
158
184
|
throw userError;
|
|
159
185
|
} finally {
|
|
@@ -167,13 +193,21 @@ class MigrationManager extends EventEmitter {
|
|
|
167
193
|
await this.config.sourceAdapter.connect();
|
|
168
194
|
}
|
|
169
195
|
if (!await this.config.sourceAdapter.ping()) {
|
|
170
|
-
throw new
|
|
196
|
+
throw new DatabaseError(
|
|
197
|
+
"Source adapter is not responding",
|
|
198
|
+
ErrorCode.DB_CONNECTION_FAILED,
|
|
199
|
+
{ adapter: "source" }
|
|
200
|
+
);
|
|
171
201
|
}
|
|
172
202
|
if (!this.config.targetAdapter.isConnected()) {
|
|
173
203
|
await this.config.targetAdapter.connect();
|
|
174
204
|
}
|
|
175
205
|
if (!await this.config.targetAdapter.ping()) {
|
|
176
|
-
throw new
|
|
206
|
+
throw new DatabaseError(
|
|
207
|
+
"Target adapter is not responding",
|
|
208
|
+
ErrorCode.DB_CONNECTION_FAILED,
|
|
209
|
+
{ adapter: "target" }
|
|
210
|
+
);
|
|
177
211
|
}
|
|
178
212
|
const sourceVersion = await this.config.sourceAdapter.getSchemaVersion();
|
|
179
213
|
const targetVersion = await this.config.targetAdapter.getSchemaVersion();
|
|
@@ -190,7 +224,12 @@ class MigrationManager extends EventEmitter {
|
|
|
190
224
|
await this.config.targetAdapter.initializeSchema();
|
|
191
225
|
} catch (error) {
|
|
192
226
|
logger.error("Failed to initialize target schema:", error);
|
|
193
|
-
throw new
|
|
227
|
+
throw new DatabaseError(
|
|
228
|
+
"Target schema initialization failed",
|
|
229
|
+
ErrorCode.DB_SCHEMA_ERROR,
|
|
230
|
+
{ operation: "initializeSchema" },
|
|
231
|
+
error instanceof Error ? error : void 0
|
|
232
|
+
);
|
|
194
233
|
}
|
|
195
234
|
}
|
|
196
235
|
async enableDualWrite() {
|
|
@@ -202,7 +241,11 @@ class MigrationManager extends EventEmitter {
|
|
|
202
241
|
async executeMigrationPlan(plan, strategy) {
|
|
203
242
|
for (const tablePlan of plan) {
|
|
204
243
|
if (this.abortController?.signal.aborted) {
|
|
205
|
-
throw new
|
|
244
|
+
throw new DatabaseError(
|
|
245
|
+
"Migration aborted by user",
|
|
246
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
247
|
+
{ reason: "user_abort" }
|
|
248
|
+
);
|
|
206
249
|
}
|
|
207
250
|
if (tablePlan.strategy === "skip") {
|
|
208
251
|
logger.info(`Skipping table: ${tablePlan.table}`);
|
|
@@ -212,7 +255,7 @@ class MigrationManager extends EventEmitter {
|
|
|
212
255
|
await this.migrateTable(tablePlan, strategy);
|
|
213
256
|
}
|
|
214
257
|
}
|
|
215
|
-
async migrateTable(plan,
|
|
258
|
+
async migrateTable(plan, _strategy) {
|
|
216
259
|
logger.info(`Migrating table: ${plan.table} (~${plan.estimatedRows} rows)`);
|
|
217
260
|
let offset = 0;
|
|
218
261
|
let migratedRows = 0;
|
|
@@ -240,7 +283,12 @@ class MigrationManager extends EventEmitter {
|
|
|
240
283
|
if (this.config.retryAttempts > 0) {
|
|
241
284
|
await this.retryBatch(plan.table, offset, this.config.batchSize);
|
|
242
285
|
} else {
|
|
243
|
-
throw
|
|
286
|
+
throw wrapError(
|
|
287
|
+
error,
|
|
288
|
+
`Batch migration failed for table ${plan.table}`,
|
|
289
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
290
|
+
{ table: plan.table, offset }
|
|
291
|
+
);
|
|
244
292
|
}
|
|
245
293
|
}
|
|
246
294
|
}
|
|
@@ -251,16 +299,16 @@ class MigrationManager extends EventEmitter {
|
|
|
251
299
|
async getBatch(table, offset, limit) {
|
|
252
300
|
const allowedTables = ["frames", "events", "anchors"];
|
|
253
301
|
if (!allowedTables.includes(table)) {
|
|
254
|
-
throw new
|
|
302
|
+
throw new DatabaseError(
|
|
303
|
+
`Invalid table name: ${table}`,
|
|
304
|
+
ErrorCode.DB_QUERY_FAILED,
|
|
305
|
+
{ table, allowedTables }
|
|
306
|
+
);
|
|
255
307
|
}
|
|
256
308
|
const safeLimit = Math.max(1, Math.min(limit, 1e4));
|
|
257
309
|
const safeOffset = Math.max(0, offset);
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
offset: safeOffset,
|
|
261
|
-
orderBy: "created_at",
|
|
262
|
-
orderDirection: "ASC"
|
|
263
|
-
};
|
|
310
|
+
void safeLimit;
|
|
311
|
+
void safeOffset;
|
|
264
312
|
switch (table) {
|
|
265
313
|
case "frames":
|
|
266
314
|
return [];
|
|
@@ -272,13 +320,21 @@ class MigrationManager extends EventEmitter {
|
|
|
272
320
|
return [];
|
|
273
321
|
// Placeholder
|
|
274
322
|
default:
|
|
275
|
-
throw new
|
|
323
|
+
throw new DatabaseError(
|
|
324
|
+
`Unsupported table: ${table}`,
|
|
325
|
+
ErrorCode.DB_QUERY_FAILED,
|
|
326
|
+
{ table }
|
|
327
|
+
);
|
|
276
328
|
}
|
|
277
329
|
}
|
|
278
330
|
async migrateBatch(table, batch) {
|
|
279
331
|
const allowedTables = ["frames", "events", "anchors"];
|
|
280
332
|
if (!allowedTables.includes(table)) {
|
|
281
|
-
throw new
|
|
333
|
+
throw new DatabaseError(
|
|
334
|
+
`Invalid table name: ${table}`,
|
|
335
|
+
ErrorCode.DB_INSERT_FAILED,
|
|
336
|
+
{ table }
|
|
337
|
+
);
|
|
282
338
|
}
|
|
283
339
|
await this.config.targetAdapter.inTransaction(async (adapter) => {
|
|
284
340
|
const operations = batch.map((row) => ({
|
|
@@ -291,7 +347,11 @@ class MigrationManager extends EventEmitter {
|
|
|
291
347
|
}
|
|
292
348
|
validateRowData(table, row) {
|
|
293
349
|
if (!row || typeof row !== "object") {
|
|
294
|
-
throw new
|
|
350
|
+
throw new DatabaseError(
|
|
351
|
+
`Invalid row data for table ${table}`,
|
|
352
|
+
ErrorCode.DB_INSERT_FAILED,
|
|
353
|
+
{ table, rowType: typeof row }
|
|
354
|
+
);
|
|
295
355
|
}
|
|
296
356
|
switch (table) {
|
|
297
357
|
case "frames":
|
|
@@ -301,7 +361,11 @@ class MigrationManager extends EventEmitter {
|
|
|
301
361
|
case "anchors":
|
|
302
362
|
return this.validateAnchorRow(row);
|
|
303
363
|
default:
|
|
304
|
-
throw new
|
|
364
|
+
throw new DatabaseError(
|
|
365
|
+
`Unknown table: ${table}`,
|
|
366
|
+
ErrorCode.DB_INSERT_FAILED,
|
|
367
|
+
{ table }
|
|
368
|
+
);
|
|
305
369
|
}
|
|
306
370
|
}
|
|
307
371
|
validateFrameRow(row) {
|
|
@@ -316,7 +380,11 @@ class MigrationManager extends EventEmitter {
|
|
|
316
380
|
];
|
|
317
381
|
for (const field of required) {
|
|
318
382
|
if (!(field in row)) {
|
|
319
|
-
throw new
|
|
383
|
+
throw new DatabaseError(
|
|
384
|
+
`Missing required field ${field} in frame row`,
|
|
385
|
+
ErrorCode.DB_CONSTRAINT_VIOLATION,
|
|
386
|
+
{ table: "frames", missingField: field }
|
|
387
|
+
);
|
|
320
388
|
}
|
|
321
389
|
}
|
|
322
390
|
return row;
|
|
@@ -325,7 +393,11 @@ class MigrationManager extends EventEmitter {
|
|
|
325
393
|
const required = ["event_id", "frame_id", "seq", "type", "text"];
|
|
326
394
|
for (const field of required) {
|
|
327
395
|
if (!(field in row)) {
|
|
328
|
-
throw new
|
|
396
|
+
throw new DatabaseError(
|
|
397
|
+
`Missing required field ${field} in event row`,
|
|
398
|
+
ErrorCode.DB_CONSTRAINT_VIOLATION,
|
|
399
|
+
{ table: "events", missingField: field }
|
|
400
|
+
);
|
|
329
401
|
}
|
|
330
402
|
}
|
|
331
403
|
return row;
|
|
@@ -334,7 +406,11 @@ class MigrationManager extends EventEmitter {
|
|
|
334
406
|
const required = ["anchor_id", "frame_id", "type", "text", "priority"];
|
|
335
407
|
for (const field of required) {
|
|
336
408
|
if (!(field in row)) {
|
|
337
|
-
throw new
|
|
409
|
+
throw new DatabaseError(
|
|
410
|
+
`Missing required field ${field} in anchor row`,
|
|
411
|
+
ErrorCode.DB_CONSTRAINT_VIOLATION,
|
|
412
|
+
{ table: "anchors", missingField: field }
|
|
413
|
+
);
|
|
338
414
|
}
|
|
339
415
|
}
|
|
340
416
|
return row;
|
|
@@ -353,8 +429,11 @@ class MigrationManager extends EventEmitter {
|
|
|
353
429
|
error
|
|
354
430
|
);
|
|
355
431
|
if (attempt === this.config.retryAttempts) {
|
|
356
|
-
throw new
|
|
357
|
-
`Failed after ${this.config.retryAttempts} retries
|
|
432
|
+
throw new DatabaseError(
|
|
433
|
+
`Failed after ${this.config.retryAttempts} retries`,
|
|
434
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
435
|
+
{ table, offset, attempts: this.config.retryAttempts },
|
|
436
|
+
error instanceof Error ? error : void 0
|
|
358
437
|
);
|
|
359
438
|
}
|
|
360
439
|
}
|
|
@@ -390,12 +469,17 @@ class MigrationManager extends EventEmitter {
|
|
|
390
469
|
}
|
|
391
470
|
}
|
|
392
471
|
if (this.progress.errors.length > 0) {
|
|
393
|
-
throw new
|
|
394
|
-
`Data integrity verification failed with ${this.progress.errors.length} errors
|
|
472
|
+
throw new DatabaseError(
|
|
473
|
+
`Data integrity verification failed with ${this.progress.errors.length} errors`,
|
|
474
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
475
|
+
{
|
|
476
|
+
errorCount: this.progress.errors.length,
|
|
477
|
+
errors: this.progress.errors
|
|
478
|
+
}
|
|
395
479
|
);
|
|
396
480
|
}
|
|
397
481
|
}
|
|
398
|
-
async completeMigration(
|
|
482
|
+
async completeMigration(_strategy) {
|
|
399
483
|
logger.info("Completing migration");
|
|
400
484
|
const sourceVersion = await this.config.sourceAdapter.getSchemaVersion();
|
|
401
485
|
await this.config.targetAdapter.migrateSchema(sourceVersion);
|
|
@@ -473,7 +557,11 @@ class MigrationManager extends EventEmitter {
|
|
|
473
557
|
}
|
|
474
558
|
pause() {
|
|
475
559
|
if (!this.isRunning) {
|
|
476
|
-
throw new
|
|
560
|
+
throw new DatabaseError(
|
|
561
|
+
"No migration in progress",
|
|
562
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
563
|
+
{ reason: "not_running" }
|
|
564
|
+
);
|
|
477
565
|
}
|
|
478
566
|
this.isPaused = true;
|
|
479
567
|
logger.info("Migration paused");
|
|
@@ -481,7 +569,11 @@ class MigrationManager extends EventEmitter {
|
|
|
481
569
|
}
|
|
482
570
|
resume() {
|
|
483
571
|
if (!this.isRunning) {
|
|
484
|
-
throw new
|
|
572
|
+
throw new DatabaseError(
|
|
573
|
+
"No migration in progress",
|
|
574
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
575
|
+
{ reason: "not_running" }
|
|
576
|
+
);
|
|
485
577
|
}
|
|
486
578
|
this.isPaused = false;
|
|
487
579
|
logger.info("Migration resumed");
|
|
@@ -489,7 +581,11 @@ class MigrationManager extends EventEmitter {
|
|
|
489
581
|
}
|
|
490
582
|
abort() {
|
|
491
583
|
if (!this.isRunning) {
|
|
492
|
-
throw new
|
|
584
|
+
throw new DatabaseError(
|
|
585
|
+
"No migration in progress",
|
|
586
|
+
ErrorCode.DB_MIGRATION_FAILED,
|
|
587
|
+
{ reason: "not_running" }
|
|
588
|
+
);
|
|
493
589
|
}
|
|
494
590
|
this.abortController?.abort();
|
|
495
591
|
logger.info("Migration aborted");
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/core/database/migration-manager.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Migration Manager for Dual-Write Strategy\n * Enables seamless migration between SQLite and ParadeDB with zero downtime\n */\n\nimport { EventEmitter } from 'events';\nimport { DatabaseAdapter } from './database-adapter.js';\nimport { logger } from '../monitoring/logger.js';\n\nexport interface MigrationConfig {\n sourceAdapter: DatabaseAdapter;\n targetAdapter: DatabaseAdapter;\n batchSize?: number;\n retryAttempts?: number;\n retryDelayMs?: number;\n verifyData?: boolean;\n enableDualWrite?: boolean;\n progressCallback?: (progress: MigrationProgress) => void;\n}\n\nexport interface MigrationProgress {\n phase:\n | 'initializing'\n | 'migrating'\n | 'verifying'\n | 'completing'\n | 'completed'\n | 'failed';\n totalRecords: number;\n processedRecords: number;\n percentage: number;\n startTime: Date;\n estimatedEndTime?: Date;\n currentTable?: string;\n errors: Array<{ table: string; error: string; timestamp: Date }>;\n warnings: Array<{ table: string; warning: string; timestamp: Date }>;\n}\n\nexport interface MigrationStrategy {\n type: 'online' | 'offline' | 'dual-write';\n allowWrites: boolean;\n verifyIntegrity: boolean;\n fallbackOnError: boolean;\n}\n\nexport interface TableMigrationPlan {\n table: string;\n priority: number;\n estimatedRows: number;\n dependencies: string[];\n strategy: 'full' | 'incremental' | 'skip';\n}\n\nexport class MigrationManager extends EventEmitter {\n private config: Required<MigrationConfig>;\n private progress: MigrationProgress;\n private isRunning = false;\n private isPaused = false;\n private abortController?: AbortController;\n\n constructor(config: MigrationConfig) {\n super();\n\n this.validateConfig(config);\n this.config = this.normalizeConfig(config);\n this.progress = this.initializeProgress();\n }\n\n private validateConfig(config: MigrationConfig): void {\n if (!config.sourceAdapter || !config.targetAdapter) {\n throw new Error('Source and target adapters are required');\n }\n\n if (\n config.batchSize &&\n (config.batchSize < 1 || config.batchSize > 10000)\n ) {\n throw new Error('Batch size must be between 1 and 10000');\n }\n\n if (\n config.retryAttempts &&\n (config.retryAttempts < 0 || config.retryAttempts > 10)\n ) {\n throw new Error('Retry attempts must be between 0 and 10');\n }\n\n if (\n config.retryDelayMs &&\n (config.retryDelayMs < 0 || config.retryDelayMs > 30000)\n ) {\n throw new Error('Retry delay must be between 0 and 30000ms');\n }\n }\n\n private normalizeConfig(config: MigrationConfig): Required<MigrationConfig> {\n return {\n ...config,\n batchSize: config.batchSize ?? 1000,\n retryAttempts: config.retryAttempts ?? 3,\n retryDelayMs: config.retryDelayMs ?? 1000,\n verifyData: config.verifyData ?? true,\n enableDualWrite: config.enableDualWrite ?? true,\n progressCallback: config.progressCallback ?? (() => {}),\n };\n }\n\n private initializeProgress(): MigrationProgress {\n return {\n phase: 'initializing',\n totalRecords: 0,\n processedRecords: 0,\n percentage: 0,\n startTime: new Date(),\n errors: [],\n warnings: [],\n };\n }\n\n async planMigration(): Promise<TableMigrationPlan[]> {\n logger.info('Planning migration strategy');\n\n const plan: TableMigrationPlan[] = [];\n const tables = ['frames', 'events', 'anchors'];\n\n for (const table of tables) {\n try {\n const stats = await this.config.sourceAdapter.getStats();\n const estimatedRows = this.estimateTableRows(table, stats);\n\n plan.push({\n table,\n priority: this.getTablePriority(table),\n estimatedRows,\n dependencies: this.getTableDependencies(table),\n strategy: 'full',\n });\n } catch (error: unknown) {\n logger.warn(`Failed to estimate rows for table ${table}:`, error);\n plan.push({\n table,\n priority: this.getTablePriority(table),\n estimatedRows: 0,\n dependencies: this.getTableDependencies(table),\n strategy: 'skip',\n });\n }\n }\n\n // Sort by priority (dependencies first)\n plan.sort((a, b) => a.priority - b.priority);\n\n const totalRecords = plan.reduce((sum, p) => sum + p.estimatedRows, 0);\n this.progress.totalRecords = totalRecords;\n\n logger.info(\n `Migration plan: ${plan.length} tables, ~${totalRecords} records`\n );\n return plan;\n }\n\n private estimateTableRows(table: string, stats: any): number {\n switch (table) {\n case 'frames':\n return stats.totalFrames || 0;\n case 'events':\n return stats.totalEvents || 0;\n case 'anchors':\n return stats.totalAnchors || 0;\n default:\n return 0;\n }\n }\n\n private getTablePriority(table: string): number {\n const priorities = { frames: 1, events: 2, anchors: 3 };\n return priorities[table as keyof typeof priorities] || 99;\n }\n\n private getTableDependencies(table: string): string[] {\n const dependencies = {\n frames: [],\n events: ['frames'],\n anchors: ['frames'],\n };\n return dependencies[table as keyof typeof dependencies] || [];\n }\n\n async migrate(\n strategy: MigrationStrategy = {\n type: 'online',\n allowWrites: true,\n verifyIntegrity: true,\n fallbackOnError: true,\n }\n ): Promise<void> {\n if (this.isRunning) {\n throw new Error('Migration already in progress');\n }\n\n this.isRunning = true;\n this.abortController = new AbortController();\n\n try {\n logger.info('Starting database migration', strategy);\n this.updateProgress({ phase: 'initializing' });\n\n // Validate adapters\n await this.validateAdapters();\n\n // Create migration plan\n const plan = await this.planMigration();\n\n // Initialize target schema\n await this.initializeTargetSchema();\n\n // Enable dual-write if requested\n if (strategy.type === 'dual-write' && this.config.enableDualWrite) {\n await this.enableDualWrite();\n }\n\n // Execute migration\n this.updateProgress({ phase: 'migrating' });\n await this.executeMigrationPlan(plan, strategy);\n\n // Verify data integrity\n if (strategy.verifyIntegrity) {\n this.updateProgress({ phase: 'verifying' });\n await this.verifyDataIntegrity(plan);\n }\n\n // Complete migration\n this.updateProgress({ phase: 'completing' });\n await this.completeMigration(strategy);\n\n this.updateProgress({ phase: 'completed', percentage: 100 });\n logger.info('Migration completed successfully');\n this.emit('completed', this.progress);\n } catch (error: unknown) {\n this.updateProgress({ phase: 'failed' });\n\n // Sanitize error for logging\n const sanitizedError = this.sanitizeError(error);\n logger.error('Migration failed:', sanitizedError);\n\n if (strategy.fallbackOnError) {\n try {\n await this.rollbackMigration();\n } catch (rollbackError: unknown) {\n logger.error('Rollback failed:', this.sanitizeError(rollbackError));\n }\n }\n\n // Create user-safe error message\n const userError = new Error('Migration failed. Check logs for details.');\n this.emit('failed', userError);\n throw userError;\n } finally {\n this.isRunning = false;\n this.abortController = undefined;\n }\n }\n\n private async validateAdapters(): Promise<void> {\n logger.debug('Validating database adapters');\n\n // Check source adapter\n if (!this.config.sourceAdapter.isConnected()) {\n await this.config.sourceAdapter.connect();\n }\n\n if (!(await this.config.sourceAdapter.ping())) {\n throw new Error('Source adapter is not responding');\n }\n\n // Check target adapter\n if (!this.config.targetAdapter.isConnected()) {\n await this.config.targetAdapter.connect();\n }\n\n if (!(await this.config.targetAdapter.ping())) {\n throw new Error('Target adapter is not responding');\n }\n\n // Verify schema compatibility\n const sourceVersion = await this.config.sourceAdapter.getSchemaVersion();\n const targetVersion = await this.config.targetAdapter.getSchemaVersion();\n\n if (sourceVersion !== targetVersion) {\n logger.warn(\n `Schema version mismatch: source=${sourceVersion}, target=${targetVersion}`\n );\n this.addWarning('Schema version mismatch detected');\n }\n }\n\n private async initializeTargetSchema(): Promise<void> {\n logger.debug('Initializing target schema');\n\n try {\n await this.config.targetAdapter.initializeSchema();\n } catch (error: unknown) {\n logger.error('Failed to initialize target schema:', error);\n throw new Error(`Target schema initialization failed: ${error}`);\n }\n }\n\n private async enableDualWrite(): Promise<void> {\n logger.info('Enabling dual-write mode');\n // This would typically involve configuring the application to write to both databases\n // For now, we'll just log the intention\n this.addWarning(\n 'Dual-write mode enabled - ensure application routes writes to both adapters'\n );\n }\n\n private async executeMigrationPlan(\n plan: TableMigrationPlan[],\n strategy: MigrationStrategy\n ): Promise<void> {\n for (const tablePlan of plan) {\n if (this.abortController?.signal.aborted) {\n throw new Error('Migration aborted by user');\n }\n\n if (tablePlan.strategy === 'skip') {\n logger.info(`Skipping table: ${tablePlan.table}`);\n continue;\n }\n\n this.updateProgress({ currentTable: tablePlan.table });\n await this.migrateTable(tablePlan, strategy);\n }\n }\n\n private async migrateTable(\n plan: TableMigrationPlan,\n strategy: MigrationStrategy\n ): Promise<void> {\n logger.info(`Migrating table: ${plan.table} (~${plan.estimatedRows} rows)`);\n\n let offset = 0;\n let migratedRows = 0;\n\n while (true) {\n if (this.abortController?.signal.aborted || this.isPaused) {\n break;\n }\n\n try {\n // Get batch of data from source\n const batch = await this.getBatch(\n plan.table,\n offset,\n this.config.batchSize\n );\n\n if (batch.length === 0) {\n break; // No more data\n }\n\n // Migrate batch to target\n await this.migrateBatch(plan.table, batch);\n\n migratedRows += batch.length;\n offset += this.config.batchSize;\n\n this.progress.processedRecords += batch.length;\n this.updateProgressPercentage();\n\n // Adaptive delay based on system resources\n await this.sleep(this.calculateAdaptiveDelay());\n } catch (error: unknown) {\n this.addError(plan.table, `Batch migration failed: ${error}`);\n\n if (this.config.retryAttempts > 0) {\n await this.retryBatch(plan.table, offset, this.config.batchSize);\n } else {\n throw error;\n }\n }\n }\n\n logger.info(\n `Completed migrating table ${plan.table}: ${migratedRows} rows`\n );\n }\n\n private async getBatch(\n table: string,\n offset: number,\n limit: number\n ): Promise<any[]> {\n // Validate table name against whitelist\n const allowedTables = ['frames', 'events', 'anchors'] as const;\n if (!allowedTables.includes(table as any)) {\n throw new Error(`Invalid table name: ${table}`);\n }\n\n // Validate and bound parameters\n const safeLimit = Math.max(1, Math.min(limit, 10000));\n const safeOffset = Math.max(0, offset);\n\n const options = {\n limit: safeLimit,\n offset: safeOffset,\n orderBy: 'created_at',\n orderDirection: 'ASC' as const,\n };\n\n switch (table) {\n case 'frames':\n // This would need to be implemented in the adapter\n return []; // Placeholder\n case 'events':\n return []; // Placeholder\n case 'anchors':\n return []; // Placeholder\n default:\n throw new Error(`Unsupported table: ${table}`);\n }\n }\n\n private async migrateBatch(table: string, batch: any[]): Promise<void> {\n // Validate table name\n const allowedTables = ['frames', 'events', 'anchors'] as const;\n if (!allowedTables.includes(table as any)) {\n throw new Error(`Invalid table name: ${table}`);\n }\n\n // Use transaction for batch safety\n await this.config.targetAdapter.inTransaction(async (adapter) => {\n const operations = batch.map((row) => ({\n type: 'insert' as const,\n table,\n data: this.validateRowData(table, row),\n }));\n\n await adapter.executeBulk(operations);\n });\n }\n\n private validateRowData(table: string, row: any): any {\n if (!row || typeof row !== 'object') {\n throw new Error(`Invalid row data for table ${table}`);\n }\n\n switch (table) {\n case 'frames':\n return this.validateFrameRow(row);\n case 'events':\n return this.validateEventRow(row);\n case 'anchors':\n return this.validateAnchorRow(row);\n default:\n throw new Error(`Unknown table: ${table}`);\n }\n }\n\n private validateFrameRow(row: any): any {\n const required = [\n 'frame_id',\n 'project_id',\n 'run_id',\n 'type',\n 'name',\n 'state',\n 'depth',\n ];\n for (const field of required) {\n if (!(field in row)) {\n throw new Error(`Missing required field ${field} in frame row`);\n }\n }\n return row;\n }\n\n private validateEventRow(row: any): any {\n const required = ['event_id', 'frame_id', 'seq', 'type', 'text'];\n for (const field of required) {\n if (!(field in row)) {\n throw new Error(`Missing required field ${field} in event row`);\n }\n }\n return row;\n }\n\n private validateAnchorRow(row: any): any {\n const required = ['anchor_id', 'frame_id', 'type', 'text', 'priority'];\n for (const field of required) {\n if (!(field in row)) {\n throw new Error(`Missing required field ${field} in anchor row`);\n }\n }\n return row;\n }\n\n private async retryBatch(\n table: string,\n offset: number,\n batchSize: number\n ): Promise<void> {\n for (let attempt = 1; attempt <= this.config.retryAttempts; attempt++) {\n try {\n await this.sleep(this.config.retryDelayMs * attempt);\n\n const batch = await this.getBatch(table, offset, batchSize);\n await this.migrateBatch(table, batch);\n\n logger.info(`Retry successful for table ${table} at offset ${offset}`);\n return;\n } catch (error: unknown) {\n logger.warn(\n `Retry ${attempt}/${this.config.retryAttempts} failed:`,\n error\n );\n\n if (attempt === this.config.retryAttempts) {\n throw new Error(\n `Failed after ${this.config.retryAttempts} retries: ${error}`\n );\n }\n }\n }\n }\n\n private async verifyDataIntegrity(plan: TableMigrationPlan[]): Promise<void> {\n logger.info('Verifying data integrity');\n\n for (const tablePlan of plan) {\n if (tablePlan.strategy === 'skip') continue;\n\n try {\n const sourceStats = await this.config.sourceAdapter.getStats();\n const targetStats = await this.config.targetAdapter.getStats();\n\n const sourceCount = this.estimateTableRows(\n tablePlan.table,\n sourceStats\n );\n const targetCount = this.estimateTableRows(\n tablePlan.table,\n targetStats\n );\n\n if (sourceCount !== targetCount) {\n this.addError(\n tablePlan.table,\n `Row count mismatch: source=${sourceCount}, target=${targetCount}`\n );\n } else {\n logger.debug(\n `Table ${tablePlan.table} verified: ${sourceCount} rows`\n );\n }\n } catch (error: unknown) {\n this.addError(tablePlan.table, `Verification failed: ${error}`);\n }\n }\n\n if (this.progress.errors.length > 0) {\n throw new Error(\n `Data integrity verification failed with ${this.progress.errors.length} errors`\n );\n }\n }\n\n private async completeMigration(strategy: MigrationStrategy): Promise<void> {\n logger.info('Completing migration');\n\n // Update target schema version if needed\n const sourceVersion = await this.config.sourceAdapter.getSchemaVersion();\n await this.config.targetAdapter.migrateSchema(sourceVersion);\n\n // Analyze target database for optimal performance\n await this.config.targetAdapter.analyze();\n\n logger.info('Migration completion tasks finished');\n }\n\n private async rollbackMigration(): Promise<void> {\n logger.warn('Rolling back migration');\n\n try {\n // This would typically involve cleaning up the target database\n // For now, we'll just log the intention\n logger.warn(\n 'Rollback would clean target database - implement based on strategy'\n );\n } catch (error: unknown) {\n logger.error('Rollback failed:', error);\n }\n }\n\n private updateProgress(updates: Partial<MigrationProgress>): void {\n Object.assign(this.progress, updates);\n this.updateProgressPercentage();\n\n if (this.progress.totalRecords > 0) {\n const elapsed = Date.now() - this.progress.startTime.getTime();\n const rate = this.progress.processedRecords / (elapsed / 1000);\n const remaining =\n this.progress.totalRecords - this.progress.processedRecords;\n\n if (rate > 0) {\n this.progress.estimatedEndTime = new Date(\n Date.now() + (remaining / rate) * 1000\n );\n }\n }\n\n this.config.progressCallback(this.progress);\n this.emit('progress', this.progress);\n }\n\n private updateProgressPercentage(): void {\n if (this.progress.totalRecords > 0) {\n this.progress.percentage = Math.min(\n 100,\n (this.progress.processedRecords / this.progress.totalRecords) * 100\n );\n }\n }\n\n private addError(table: string, error: string): void {\n this.progress.errors.push({\n table,\n error,\n timestamp: new Date(),\n });\n\n logger.error(`Migration error for table ${table}: ${error}`);\n }\n\n private addWarning(warning: string, table?: string): void {\n this.progress.warnings.push({\n table: table || 'general',\n warning,\n timestamp: new Date(),\n });\n\n logger.warn(`Migration warning: ${warning}`);\n }\n\n private sanitizeError(error: any): any {\n if (error instanceof Error) {\n return {\n name: error.name,\n message: error.message,\n // Exclude stack traces and sensitive data for security\n };\n }\n return { message: 'Unknown error occurred' };\n }\n\n private calculateAdaptiveDelay(): number {\n const memoryUsage = process.memoryUsage().heapUsed / 1024 / 1024;\n\n // Adaptive delay based on system resources\n if (memoryUsage > 400) return 100;\n if (memoryUsage > 300) return 50;\n return 10;\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n\n pause(): void {\n if (!this.isRunning) {\n throw new Error('No migration in progress');\n }\n\n this.isPaused = true;\n logger.info('Migration paused');\n this.emit('paused');\n }\n\n resume(): void {\n if (!this.isRunning) {\n throw new Error('No migration in progress');\n }\n\n this.isPaused = false;\n logger.info('Migration resumed');\n this.emit('resumed');\n }\n\n abort(): void {\n if (!this.isRunning) {\n throw new Error('No migration in progress');\n }\n\n this.abortController?.abort();\n logger.info('Migration aborted');\n this.emit('aborted');\n }\n\n getProgress(): MigrationProgress {\n return { ...this.progress };\n }\n\n isActive(): boolean {\n return this.isRunning;\n }\n\n async estimateDuration(): Promise<{\n estimatedMinutes: number;\n confidence: 'low' | 'medium' | 'high';\n }> {\n const plan = await this.planMigration();\n const totalRecords = plan.reduce((sum, p) => sum + p.estimatedRows, 0);\n\n // Rough estimate: 1000 records per second\n const estimatedSeconds = totalRecords / 1000;\n const estimatedMinutes = Math.ceil(estimatedSeconds / 60);\n\n let confidence: 'low' | 'medium' | 'high' = 'medium';\n if (totalRecords < 10000) confidence = 'high';\n if (totalRecords > 100000) confidence = 'low';\n\n return { estimatedMinutes, confidence };\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;AAKA,SAAS,oBAAoB;AAE7B,SAAS,cAAc;AA8ChB,MAAM,yBAAyB,aAAa;AAAA,EACzC;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ,WAAW;AAAA,EACX;AAAA,EAER,YAAY,QAAyB;AACnC,UAAM;AAEN,SAAK,eAAe,MAAM;AAC1B,SAAK,SAAS,KAAK,gBAAgB,MAAM;AACzC,SAAK,WAAW,KAAK,mBAAmB;AAAA,EAC1C;AAAA,EAEQ,eAAe,QAA+B;AACpD,QAAI,CAAC,OAAO,iBAAiB,CAAC,OAAO,eAAe;AAClD,YAAM,IAAI,MAAM,yCAAyC;AAAA,IAC3D;AAEA,QACE,OAAO,cACN,OAAO,YAAY,KAAK,OAAO,YAAY,MAC5C;AACA,YAAM,IAAI,MAAM,wCAAwC;AAAA,IAC1D;AAEA,QACE,OAAO,kBACN,OAAO,gBAAgB,KAAK,OAAO,gBAAgB,KACpD;AACA,YAAM,IAAI,MAAM,yCAAyC;AAAA,IAC3D;AAEA,QACE,OAAO,iBACN,OAAO,eAAe,KAAK,OAAO,eAAe,MAClD;AACA,YAAM,IAAI,MAAM,2CAA2C;AAAA,IAC7D;AAAA,EACF;AAAA,EAEQ,gBAAgB,QAAoD;AAC1E,WAAO;AAAA,MACL,GAAG;AAAA,MACH,WAAW,OAAO,aAAa;AAAA,MAC/B,eAAe,OAAO,iBAAiB;AAAA,MACvC,cAAc,OAAO,gBAAgB;AAAA,MACrC,YAAY,OAAO,cAAc;AAAA,MACjC,iBAAiB,OAAO,mBAAmB;A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A0B;AAAA,IAC5C;AAEA,SAAK,WAAW;AAChB,WAAO,KAAK,mBAAmB;AAC/B,SAAK,KAAK,SAAS;AAAA,EACrB;AAAA,EAEA,QAAc;AACZ,QAAI,CAAC,KAAK,WAAW;AACnB,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC5C;AAEA,SAAK,iBAAiB,MAAM;AAC5B,WAAO,KAAK,mBAAmB;AAC/B,SAAK,KAAK,SAAS;AAAA,EACrB;AAAA,EAEA,cAAiC;AAC/B,WAAO,EAAE,GAAG,KAAK,SAAS;AAAA,EAC5B;AAAA,EAEA,WAAoB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,mBAGH;AACD,UAAM,OAAO,MAAM,KAAK,cAAc;AACtC,UAAM,eAAe,KAAK,OAAO,CAAC,KAAK,MAAM,MAAM,EAAE,eAAe,CAAC;AAGrE,UAAM,mBAAmB,eAAe;AACxC,UAAM,mBAAmB,KAAK,KAAK,mBAAmB,EAAE;AAExD,QAAI,aAAwC;AAC5C,QAAI,eAAe,IAAO,cAAa;AACvC,QAAI,eAAe,IAAQ,cAAa;AAExC,WAAO,EAAE,kBAAkB,WAAW;AAAA,EACxC;AACF;",
|
|
4
|
+
"sourcesContent": ["/**\n * Migration Manager for Dual-Write Strategy\n * Enables seamless migration between SQLite and ParadeDB with zero downtime\n */\n\nimport { EventEmitter } from 'events';\nimport { DatabaseAdapter } from './database-adapter.js';\nimport { logger } from '../monitoring/logger.js';\nimport { DatabaseError, ErrorCode, wrapError } from '../errors/index.js';\n\nexport interface MigrationConfig {\n sourceAdapter: DatabaseAdapter;\n targetAdapter: DatabaseAdapter;\n batchSize?: number;\n retryAttempts?: number;\n retryDelayMs?: number;\n verifyData?: boolean;\n enableDualWrite?: boolean;\n progressCallback?: (progress: MigrationProgress) => void;\n}\n\nexport interface MigrationProgress {\n phase:\n | 'initializing'\n | 'migrating'\n | 'verifying'\n | 'completing'\n | 'completed'\n | 'failed';\n totalRecords: number;\n processedRecords: number;\n percentage: number;\n startTime: Date;\n estimatedEndTime?: Date;\n currentTable?: string;\n errors: Array<{ table: string; error: string; timestamp: Date }>;\n warnings: Array<{ table: string; warning: string; timestamp: Date }>;\n}\n\nexport interface MigrationStrategy {\n type: 'online' | 'offline' | 'dual-write';\n allowWrites: boolean;\n verifyIntegrity: boolean;\n fallbackOnError: boolean;\n}\n\nexport interface TableMigrationPlan {\n table: string;\n priority: number;\n estimatedRows: number;\n dependencies: string[];\n strategy: 'full' | 'incremental' | 'skip';\n}\n\nexport class MigrationManager extends EventEmitter {\n private config: Required<MigrationConfig>;\n private progress: MigrationProgress;\n private isRunning = false;\n private isPaused = false;\n private abortController?: AbortController;\n\n constructor(config: MigrationConfig) {\n super();\n\n this.validateConfig(config);\n this.config = this.normalizeConfig(config);\n this.progress = this.initializeProgress();\n }\n\n private validateConfig(config: MigrationConfig): void {\n if (!config.sourceAdapter || !config.targetAdapter) {\n throw new DatabaseError(\n 'Source and target adapters are required',\n ErrorCode.DB_MIGRATION_FAILED,\n { reason: 'missing_adapters' }\n );\n }\n\n if (\n config.batchSize &&\n (config.batchSize < 1 || config.batchSize > 10000)\n ) {\n throw new DatabaseError(\n 'Batch size must be between 1 and 10000',\n ErrorCode.DB_MIGRATION_FAILED,\n { batchSize: config.batchSize }\n );\n }\n\n if (\n config.retryAttempts &&\n (config.retryAttempts < 0 || config.retryAttempts > 10)\n ) {\n throw new DatabaseError(\n 'Retry attempts must be between 0 and 10',\n ErrorCode.DB_MIGRATION_FAILED,\n { retryAttempts: config.retryAttempts }\n );\n }\n\n if (\n config.retryDelayMs &&\n (config.retryDelayMs < 0 || config.retryDelayMs > 30000)\n ) {\n throw new DatabaseError(\n 'Retry delay must be between 0 and 30000ms',\n ErrorCode.DB_MIGRATION_FAILED,\n { retryDelayMs: config.retryDelayMs }\n );\n }\n }\n\n private normalizeConfig(config: MigrationConfig): Required<MigrationConfig> {\n return {\n ...config,\n batchSize: config.batchSize ?? 1000,\n retryAttempts: config.retryAttempts ?? 3,\n retryDelayMs: config.retryDelayMs ?? 1000,\n verifyData: config.verifyData ?? true,\n enableDualWrite: config.enableDualWrite ?? true,\n progressCallback: config.progressCallback ?? (() => {}),\n };\n }\n\n private initializeProgress(): MigrationProgress {\n return {\n phase: 'initializing',\n totalRecords: 0,\n processedRecords: 0,\n percentage: 0,\n startTime: new Date(),\n errors: [],\n warnings: [],\n };\n }\n\n async planMigration(): Promise<TableMigrationPlan[]> {\n logger.info('Planning migration strategy');\n\n const plan: TableMigrationPlan[] = [];\n const tables = ['frames', 'events', 'anchors'];\n\n for (const table of tables) {\n try {\n const stats = await this.config.sourceAdapter.getStats();\n const estimatedRows = this.estimateTableRows(table, stats);\n\n plan.push({\n table,\n priority: this.getTablePriority(table),\n estimatedRows,\n dependencies: this.getTableDependencies(table),\n strategy: 'full',\n });\n } catch (error: unknown) {\n logger.warn(`Failed to estimate rows for table ${table}:`, error);\n plan.push({\n table,\n priority: this.getTablePriority(table),\n estimatedRows: 0,\n dependencies: this.getTableDependencies(table),\n strategy: 'skip',\n });\n }\n }\n\n // Sort by priority (dependencies first)\n plan.sort((a, b) => a.priority - b.priority);\n\n const totalRecords = plan.reduce((sum, p) => sum + p.estimatedRows, 0);\n this.progress.totalRecords = totalRecords;\n\n logger.info(\n `Migration plan: ${plan.length} tables, ~${totalRecords} records`\n );\n return plan;\n }\n\n private estimateTableRows(table: string, stats: any): number {\n switch (table) {\n case 'frames':\n return stats.totalFrames || 0;\n case 'events':\n return stats.totalEvents || 0;\n case 'anchors':\n return stats.totalAnchors || 0;\n default:\n return 0;\n }\n }\n\n private getTablePriority(table: string): number {\n const priorities = { frames: 1, events: 2, anchors: 3 };\n return priorities[table as keyof typeof priorities] || 99;\n }\n\n private getTableDependencies(table: string): string[] {\n const dependencies = {\n frames: [],\n events: ['frames'],\n anchors: ['frames'],\n };\n return dependencies[table as keyof typeof dependencies] || [];\n }\n\n async migrate(\n strategy: MigrationStrategy = {\n type: 'online',\n allowWrites: true,\n verifyIntegrity: true,\n fallbackOnError: true,\n }\n ): Promise<void> {\n if (this.isRunning) {\n throw new DatabaseError(\n 'Migration already in progress',\n ErrorCode.DB_MIGRATION_FAILED,\n { reason: 'already_running' }\n );\n }\n\n this.isRunning = true;\n this.abortController = new AbortController();\n\n try {\n logger.info('Starting database migration', strategy);\n this.updateProgress({ phase: 'initializing' });\n\n // Validate adapters\n await this.validateAdapters();\n\n // Create migration plan\n const plan = await this.planMigration();\n\n // Initialize target schema\n await this.initializeTargetSchema();\n\n // Enable dual-write if requested\n if (strategy.type === 'dual-write' && this.config.enableDualWrite) {\n await this.enableDualWrite();\n }\n\n // Execute migration\n this.updateProgress({ phase: 'migrating' });\n await this.executeMigrationPlan(plan, strategy);\n\n // Verify data integrity\n if (strategy.verifyIntegrity) {\n this.updateProgress({ phase: 'verifying' });\n await this.verifyDataIntegrity(plan);\n }\n\n // Complete migration\n this.updateProgress({ phase: 'completing' });\n await this.completeMigration(strategy);\n\n this.updateProgress({ phase: 'completed', percentage: 100 });\n logger.info('Migration completed successfully');\n this.emit('completed', this.progress);\n } catch (error: unknown) {\n this.updateProgress({ phase: 'failed' });\n\n // Sanitize error for logging\n const sanitizedError = this.sanitizeError(error);\n logger.error('Migration failed:', sanitizedError);\n\n if (strategy.fallbackOnError) {\n try {\n await this.rollbackMigration();\n } catch (rollbackError: unknown) {\n logger.error('Rollback failed:', this.sanitizeError(rollbackError));\n }\n }\n\n // Create user-safe error message\n const userError = new DatabaseError(\n 'Migration failed. Check logs for details.',\n ErrorCode.DB_MIGRATION_FAILED,\n { phase: this.progress.phase },\n error instanceof Error ? error : undefined\n );\n this.emit('failed', userError);\n throw userError;\n } finally {\n this.isRunning = false;\n this.abortController = undefined;\n }\n }\n\n private async validateAdapters(): Promise<void> {\n logger.debug('Validating database adapters');\n\n // Check source adapter\n if (!this.config.sourceAdapter.isConnected()) {\n await this.config.sourceAdapter.connect();\n }\n\n if (!(await this.config.sourceAdapter.ping())) {\n throw new DatabaseError(\n 'Source adapter is not responding',\n ErrorCode.DB_CONNECTION_FAILED,\n { adapter: 'source' }\n );\n }\n\n // Check target adapter\n if (!this.config.targetAdapter.isConnected()) {\n await this.config.targetAdapter.connect();\n }\n\n if (!(await this.config.targetAdapter.ping())) {\n throw new DatabaseError(\n 'Target adapter is not responding',\n ErrorCode.DB_CONNECTION_FAILED,\n { adapter: 'target' }\n );\n }\n\n // Verify schema compatibility\n const sourceVersion = await this.config.sourceAdapter.getSchemaVersion();\n const targetVersion = await this.config.targetAdapter.getSchemaVersion();\n\n if (sourceVersion !== targetVersion) {\n logger.warn(\n `Schema version mismatch: source=${sourceVersion}, target=${targetVersion}`\n );\n this.addWarning('Schema version mismatch detected');\n }\n }\n\n private async initializeTargetSchema(): Promise<void> {\n logger.debug('Initializing target schema');\n\n try {\n await this.config.targetAdapter.initializeSchema();\n } catch (error: unknown) {\n logger.error('Failed to initialize target schema:', error);\n throw new DatabaseError(\n 'Target schema initialization failed',\n ErrorCode.DB_SCHEMA_ERROR,\n { operation: 'initializeSchema' },\n error instanceof Error ? error : undefined\n );\n }\n }\n\n private async enableDualWrite(): Promise<void> {\n logger.info('Enabling dual-write mode');\n // This would typically involve configuring the application to write to both databases\n // For now, we'll just log the intention\n this.addWarning(\n 'Dual-write mode enabled - ensure application routes writes to both adapters'\n );\n }\n\n private async executeMigrationPlan(\n plan: TableMigrationPlan[],\n strategy: MigrationStrategy\n ): Promise<void> {\n for (const tablePlan of plan) {\n if (this.abortController?.signal.aborted) {\n throw new DatabaseError(\n 'Migration aborted by user',\n ErrorCode.DB_MIGRATION_FAILED,\n { reason: 'user_abort' }\n );\n }\n\n if (tablePlan.strategy === 'skip') {\n logger.info(`Skipping table: ${tablePlan.table}`);\n continue;\n }\n\n this.updateProgress({ currentTable: tablePlan.table });\n await this.migrateTable(tablePlan, strategy);\n }\n }\n\n private async migrateTable(\n plan: TableMigrationPlan,\n _strategy: MigrationStrategy\n ): Promise<void> {\n logger.info(`Migrating table: ${plan.table} (~${plan.estimatedRows} rows)`);\n\n let offset = 0;\n let migratedRows = 0;\n\n while (true) {\n if (this.abortController?.signal.aborted || this.isPaused) {\n break;\n }\n\n try {\n // Get batch of data from source\n const batch = await this.getBatch(\n plan.table,\n offset,\n this.config.batchSize\n );\n\n if (batch.length === 0) {\n break; // No more data\n }\n\n // Migrate batch to target\n await this.migrateBatch(plan.table, batch);\n\n migratedRows += batch.length;\n offset += this.config.batchSize;\n\n this.progress.processedRecords += batch.length;\n this.updateProgressPercentage();\n\n // Adaptive delay based on system resources\n await this.sleep(this.calculateAdaptiveDelay());\n } catch (error: unknown) {\n this.addError(plan.table, `Batch migration failed: ${error}`);\n\n if (this.config.retryAttempts > 0) {\n await this.retryBatch(plan.table, offset, this.config.batchSize);\n } else {\n throw wrapError(\n error,\n `Batch migration failed for table ${plan.table}`,\n ErrorCode.DB_MIGRATION_FAILED,\n { table: plan.table, offset }\n );\n }\n }\n }\n\n logger.info(\n `Completed migrating table ${plan.table}: ${migratedRows} rows`\n );\n }\n\n private async getBatch(\n table: string,\n offset: number,\n limit: number\n ): Promise<any[]> {\n // Validate table name against whitelist\n const allowedTables = ['frames', 'events', 'anchors'] as const;\n if (!allowedTables.includes(table as any)) {\n throw new DatabaseError(\n `Invalid table name: ${table}`,\n ErrorCode.DB_QUERY_FAILED,\n { table, allowedTables }\n );\n }\n\n // Validate and bound parameters\n const safeLimit = Math.max(1, Math.min(limit, 10000));\n const safeOffset = Math.max(0, offset);\n\n // TODO: Use these options when adapter methods support pagination\n void safeLimit;\n void safeOffset;\n\n switch (table) {\n case 'frames':\n // This would need to be implemented in the adapter\n return []; // Placeholder\n case 'events':\n return []; // Placeholder\n case 'anchors':\n return []; // Placeholder\n default:\n throw new DatabaseError(\n `Unsupported table: ${table}`,\n ErrorCode.DB_QUERY_FAILED,\n { table }\n );\n }\n }\n\n private async migrateBatch(table: string, batch: any[]): Promise<void> {\n // Validate table name\n const allowedTables = ['frames', 'events', 'anchors'] as const;\n if (!allowedTables.includes(table as any)) {\n throw new DatabaseError(\n `Invalid table name: ${table}`,\n ErrorCode.DB_INSERT_FAILED,\n { table }\n );\n }\n\n // Use transaction for batch safety\n await this.config.targetAdapter.inTransaction(async (adapter) => {\n const operations = batch.map((row) => ({\n type: 'insert' as const,\n table,\n data: this.validateRowData(table, row),\n }));\n\n await adapter.executeBulk(operations);\n });\n }\n\n private validateRowData(table: string, row: any): any {\n if (!row || typeof row !== 'object') {\n throw new DatabaseError(\n `Invalid row data for table ${table}`,\n ErrorCode.DB_INSERT_FAILED,\n { table, rowType: typeof row }\n );\n }\n\n switch (table) {\n case 'frames':\n return this.validateFrameRow(row);\n case 'events':\n return this.validateEventRow(row);\n case 'anchors':\n return this.validateAnchorRow(row);\n default:\n throw new DatabaseError(\n `Unknown table: ${table}`,\n ErrorCode.DB_INSERT_FAILED,\n { table }\n );\n }\n }\n\n private validateFrameRow(row: any): any {\n const required = [\n 'frame_id',\n 'project_id',\n 'run_id',\n 'type',\n 'name',\n 'state',\n 'depth',\n ];\n for (const field of required) {\n if (!(field in row)) {\n throw new DatabaseError(\n `Missing required field ${field} in frame row`,\n ErrorCode.DB_CONSTRAINT_VIOLATION,\n { table: 'frames', missingField: field }\n );\n }\n }\n return row;\n }\n\n private validateEventRow(row: any): any {\n const required = ['event_id', 'frame_id', 'seq', 'type', 'text'];\n for (const field of required) {\n if (!(field in row)) {\n throw new DatabaseError(\n `Missing required field ${field} in event row`,\n ErrorCode.DB_CONSTRAINT_VIOLATION,\n { table: 'events', missingField: field }\n );\n }\n }\n return row;\n }\n\n private validateAnchorRow(row: any): any {\n const required = ['anchor_id', 'frame_id', 'type', 'text', 'priority'];\n for (const field of required) {\n if (!(field in row)) {\n throw new DatabaseError(\n `Missing required field ${field} in anchor row`,\n ErrorCode.DB_CONSTRAINT_VIOLATION,\n { table: 'anchors', missingField: field }\n );\n }\n }\n return row;\n }\n\n private async retryBatch(\n table: string,\n offset: number,\n batchSize: number\n ): Promise<void> {\n for (let attempt = 1; attempt <= this.config.retryAttempts; attempt++) {\n try {\n await this.sleep(this.config.retryDelayMs * attempt);\n\n const batch = await this.getBatch(table, offset, batchSize);\n await this.migrateBatch(table, batch);\n\n logger.info(`Retry successful for table ${table} at offset ${offset}`);\n return;\n } catch (error: unknown) {\n logger.warn(\n `Retry ${attempt}/${this.config.retryAttempts} failed:`,\n error\n );\n\n if (attempt === this.config.retryAttempts) {\n throw new DatabaseError(\n `Failed after ${this.config.retryAttempts} retries`,\n ErrorCode.DB_MIGRATION_FAILED,\n { table, offset, attempts: this.config.retryAttempts },\n error instanceof Error ? error : undefined\n );\n }\n }\n }\n }\n\n private async verifyDataIntegrity(plan: TableMigrationPlan[]): Promise<void> {\n logger.info('Verifying data integrity');\n\n for (const tablePlan of plan) {\n if (tablePlan.strategy === 'skip') continue;\n\n try {\n const sourceStats = await this.config.sourceAdapter.getStats();\n const targetStats = await this.config.targetAdapter.getStats();\n\n const sourceCount = this.estimateTableRows(\n tablePlan.table,\n sourceStats\n );\n const targetCount = this.estimateTableRows(\n tablePlan.table,\n targetStats\n );\n\n if (sourceCount !== targetCount) {\n this.addError(\n tablePlan.table,\n `Row count mismatch: source=${sourceCount}, target=${targetCount}`\n );\n } else {\n logger.debug(\n `Table ${tablePlan.table} verified: ${sourceCount} rows`\n );\n }\n } catch (error: unknown) {\n this.addError(tablePlan.table, `Verification failed: ${error}`);\n }\n }\n\n if (this.progress.errors.length > 0) {\n throw new DatabaseError(\n `Data integrity verification failed with ${this.progress.errors.length} errors`,\n ErrorCode.DB_MIGRATION_FAILED,\n {\n errorCount: this.progress.errors.length,\n errors: this.progress.errors,\n }\n );\n }\n }\n\n private async completeMigration(_strategy: MigrationStrategy): Promise<void> {\n logger.info('Completing migration');\n\n // Update target schema version if needed\n const sourceVersion = await this.config.sourceAdapter.getSchemaVersion();\n await this.config.targetAdapter.migrateSchema(sourceVersion);\n\n // Analyze target database for optimal performance\n await this.config.targetAdapter.analyze();\n\n logger.info('Migration completion tasks finished');\n }\n\n private async rollbackMigration(): Promise<void> {\n logger.warn('Rolling back migration');\n\n try {\n // This would typically involve cleaning up the target database\n // For now, we'll just log the intention\n logger.warn(\n 'Rollback would clean target database - implement based on strategy'\n );\n } catch (error: unknown) {\n logger.error('Rollback failed:', error);\n }\n }\n\n private updateProgress(updates: Partial<MigrationProgress>): void {\n Object.assign(this.progress, updates);\n this.updateProgressPercentage();\n\n if (this.progress.totalRecords > 0) {\n const elapsed = Date.now() - this.progress.startTime.getTime();\n const rate = this.progress.processedRecords / (elapsed / 1000);\n const remaining =\n this.progress.totalRecords - this.progress.processedRecords;\n\n if (rate > 0) {\n this.progress.estimatedEndTime = new Date(\n Date.now() + (remaining / rate) * 1000\n );\n }\n }\n\n this.config.progressCallback(this.progress);\n this.emit('progress', this.progress);\n }\n\n private updateProgressPercentage(): void {\n if (this.progress.totalRecords > 0) {\n this.progress.percentage = Math.min(\n 100,\n (this.progress.processedRecords / this.progress.totalRecords) * 100\n );\n }\n }\n\n private addError(table: string, error: string): void {\n this.progress.errors.push({\n table,\n error,\n timestamp: new Date(),\n });\n\n logger.error(`Migration error for table ${table}: ${error}`);\n }\n\n private addWarning(warning: string, table?: string): void {\n this.progress.warnings.push({\n table: table || 'general',\n warning,\n timestamp: new Date(),\n });\n\n logger.warn(`Migration warning: ${warning}`);\n }\n\n private sanitizeError(error: any): any {\n if (error instanceof Error) {\n return {\n name: error.name,\n message: error.message,\n // Exclude stack traces and sensitive data for security\n };\n }\n return { message: 'Unknown error occurred' };\n }\n\n private calculateAdaptiveDelay(): number {\n const memoryUsage = process.memoryUsage().heapUsed / 1024 / 1024;\n\n // Adaptive delay based on system resources\n if (memoryUsage > 400) return 100;\n if (memoryUsage > 300) return 50;\n return 10;\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n\n pause(): void {\n if (!this.isRunning) {\n throw new DatabaseError(\n 'No migration in progress',\n ErrorCode.DB_MIGRATION_FAILED,\n { reason: 'not_running' }\n );\n }\n\n this.isPaused = true;\n logger.info('Migration paused');\n this.emit('paused');\n }\n\n resume(): void {\n if (!this.isRunning) {\n throw new DatabaseError(\n 'No migration in progress',\n ErrorCode.DB_MIGRATION_FAILED,\n { reason: 'not_running' }\n );\n }\n\n this.isPaused = false;\n logger.info('Migration resumed');\n this.emit('resumed');\n }\n\n abort(): void {\n if (!this.isRunning) {\n throw new DatabaseError(\n 'No migration in progress',\n ErrorCode.DB_MIGRATION_FAILED,\n { reason: 'not_running' }\n );\n }\n\n this.abortController?.abort();\n logger.info('Migration aborted');\n this.emit('aborted');\n }\n\n getProgress(): MigrationProgress {\n return { ...this.progress };\n }\n\n isActive(): boolean {\n return this.isRunning;\n }\n\n async estimateDuration(): Promise<{\n estimatedMinutes: number;\n confidence: 'low' | 'medium' | 'high';\n }> {\n const plan = await this.planMigration();\n const totalRecords = plan.reduce((sum, p) => sum + p.estimatedRows, 0);\n\n // Rough estimate: 1000 records per second\n const estimatedSeconds = totalRecords / 1000;\n const estimatedMinutes = Math.ceil(estimatedSeconds / 60);\n\n let confidence: 'low' | 'medium' | 'high' = 'medium';\n if (totalRecords < 10000) confidence = 'high';\n if (totalRecords > 100000) confidence = 'low';\n\n return { estimatedMinutes, confidence };\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;AAKA,SAAS,oBAAoB;AAE7B,SAAS,cAAc;AACvB,SAAS,eAAe,WAAW,iBAAiB;AA8C7C,MAAM,yBAAyB,aAAa;AAAA,EACzC;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ,WAAW;AAAA,EACX;AAAA,EAER,YAAY,QAAyB;AACnC,UAAM;AAEN,SAAK,eAAe,MAAM;AAC1B,SAAK,SAAS,KAAK,gBAAgB,MAAM;AACzC,SAAK,WAAW,KAAK,mBAAmB;AAAA,EAC1C;AAAA,EAEQ,eAAe,QAA+B;AACpD,QAAI,CAAC,OAAO,iBAAiB,CAAC,OAAO,eAAe;AAClD,YAAM,IAAI;AAAA,QACR;AAAA,QACA,UAA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ACd;AAAA,EAEA,MAAM,mBAGH;AACD,UAAM,OAAO,MAAM,KAAK,cAAc;AACtC,UAAM,eAAe,KAAK,OAAO,CAAC,KAAK,MAAM,MAAM,EAAE,eAAe,CAAC;AAGrE,UAAM,mBAAmB,eAAe;AACxC,UAAM,mBAAmB,KAAK,KAAK,mBAAmB,EAAE;AAExD,QAAI,aAAwC;AAC5C,QAAI,eAAe,IAAO,cAAa;AACvC,QAAI,eAAe,IAAQ,cAAa;AAExC,WAAO,EAAE,kBAAkB,WAAW;AAAA,EACxC;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
FeatureAwareDatabaseAdapter
|
|
8
8
|
} from "./database-adapter.js";
|
|
9
9
|
import { logger } from "../monitoring/logger.js";
|
|
10
|
+
import { DatabaseError, ErrorCode, ValidationError } from "../errors/index.js";
|
|
10
11
|
class ParadeDBAdapter extends FeatureAwareDatabaseAdapter {
|
|
11
12
|
pool = null;
|
|
12
13
|
activeClient = null;
|
|
@@ -839,13 +840,21 @@ class ParadeDBAdapter extends FeatureAwareDatabaseAdapter {
|
|
|
839
840
|
await this.activeClient.query("BEGIN");
|
|
840
841
|
}
|
|
841
842
|
async commitTransaction() {
|
|
842
|
-
if (!this.activeClient)
|
|
843
|
+
if (!this.activeClient)
|
|
844
|
+
throw new DatabaseError(
|
|
845
|
+
"No active transaction",
|
|
846
|
+
ErrorCode.DB_TRANSACTION_FAILED
|
|
847
|
+
);
|
|
843
848
|
await this.activeClient.query("COMMIT");
|
|
844
849
|
this.activeClient.release();
|
|
845
850
|
this.activeClient = null;
|
|
846
851
|
}
|
|
847
852
|
async rollbackTransaction() {
|
|
848
|
-
if (!this.activeClient)
|
|
853
|
+
if (!this.activeClient)
|
|
854
|
+
throw new DatabaseError(
|
|
855
|
+
"No active transaction",
|
|
856
|
+
ErrorCode.DB_TRANSACTION_FAILED
|
|
857
|
+
);
|
|
849
858
|
await this.activeClient.query("ROLLBACK");
|
|
850
859
|
this.activeClient.release();
|
|
851
860
|
this.activeClient = null;
|
|
@@ -888,8 +897,10 @@ class ParadeDBAdapter extends FeatureAwareDatabaseAdapter {
|
|
|
888
897
|
}
|
|
889
898
|
return Buffer.from(chunks.join("\n\n"));
|
|
890
899
|
} else {
|
|
891
|
-
throw new
|
|
892
|
-
`Format ${format} not yet implemented for ParadeDB export
|
|
900
|
+
throw new ValidationError(
|
|
901
|
+
`Format ${format} not yet implemented for ParadeDB export`,
|
|
902
|
+
ErrorCode.VALIDATION_FAILED,
|
|
903
|
+
{ format, supportedFormats: ["json"] }
|
|
893
904
|
);
|
|
894
905
|
}
|
|
895
906
|
} finally {
|
|
@@ -927,8 +938,10 @@ class ParadeDBAdapter extends FeatureAwareDatabaseAdapter {
|
|
|
927
938
|
}
|
|
928
939
|
await client.query("COMMIT");
|
|
929
940
|
} else {
|
|
930
|
-
throw new
|
|
931
|
-
`Format ${format} not yet implemented for ParadeDB import
|
|
941
|
+
throw new ValidationError(
|
|
942
|
+
`Format ${format} not yet implemented for ParadeDB import`,
|
|
943
|
+
ErrorCode.VALIDATION_FAILED,
|
|
944
|
+
{ format, supportedFormats: ["json"] }
|
|
932
945
|
);
|
|
933
946
|
}
|
|
934
947
|
} catch (error) {
|
|
@@ -944,7 +957,10 @@ class ParadeDBAdapter extends FeatureAwareDatabaseAdapter {
|
|
|
944
957
|
return this.activeClient;
|
|
945
958
|
}
|
|
946
959
|
if (!this.pool) {
|
|
947
|
-
throw new
|
|
960
|
+
throw new DatabaseError(
|
|
961
|
+
"Database not connected",
|
|
962
|
+
ErrorCode.DB_CONNECTION_FAILED
|
|
963
|
+
);
|
|
948
964
|
}
|
|
949
965
|
return await this.pool.connect();
|
|
950
966
|
}
|