Преглед на файлове

fix: handle production DB migration edge cases

- Use existing shops.db path if it exists (was defaulting to sites.db,
  missing the old file entirely)
- Skip webhook_deliveries rename (has webhook_id, not shop_id)
- Skip shop_embeddings shop_id rename (only has shop_content_id)
- Guard migrateSiteTypeToPlatform against missing sites table on fresh installs

Tested against production DB backup — both migration stages succeed.
fszontagh преди 3 месеца
родител
ревизия
a152d7312e
променени са 1 файла, в които са добавени 18 реда и са изтрити 4 реда
  1. 18 4
      src/database/Database.ts

+ 18 - 4
src/database/Database.ts

@@ -154,9 +154,18 @@ export interface McpLog {
 export class SiteDatabase {
   private db: Database.Database;
 
-  constructor(dbPath: string = path.join(process.cwd(), 'data', 'sites.db')) {
-    // Ensure data directory exists
+  constructor(dbPath?: string) {
     const fs = require('fs');
+
+    // Resolve DB path: prefer existing shops.db for backward compat, else sites.db
+    if (!dbPath) {
+      const dataDir = path.join(process.cwd(), 'data');
+      const oldPath = path.join(dataDir, 'shops.db');
+      const newPath = path.join(dataDir, 'sites.db');
+      dbPath = fs.existsSync(oldPath) ? oldPath : newPath;
+    }
+
+    // Ensure data directory exists
     const dir = path.dirname(dbPath);
     if (!fs.existsSync(dir)) {
       fs.mkdirSync(dir, { recursive: true });
@@ -185,11 +194,10 @@ export class SiteDatabase {
     this.db.exec('ALTER TABLE sites RENAME COLUMN webshop_type TO site_type');
     this.db.exec('ALTER TABLE site_content RENAME COLUMN shop_id TO site_id');
     this.db.exec('ALTER TABLE site_analytics RENAME COLUMN shop_id TO site_id');
-    this.db.exec('ALTER TABLE site_embeddings RENAME COLUMN shop_id TO site_id');
     this.db.exec('ALTER TABLE site_embeddings RENAME COLUMN shop_content_id TO site_content_id');
     this.db.exec('ALTER TABLE custom_urls RENAME COLUMN shop_id TO site_id');
     this.db.exec('ALTER TABLE webhooks RENAME COLUMN shop_id TO site_id');
-    this.db.exec('ALTER TABLE webhook_deliveries RENAME COLUMN shop_id TO site_id');
+    // webhook_deliveries has webhook_id, not shop_id — no rename needed
     this.db.exec('ALTER TABLE scheduled_jobs RENAME COLUMN shop_id TO site_id');
     this.db.exec('ALTER TABLE scrape_history RENAME COLUMN shop_id TO site_id');
     this.db.exec('ALTER TABLE qdrant_deletion_queue RENAME COLUMN shop_id TO site_id');
@@ -199,6 +207,12 @@ export class SiteDatabase {
   }
 
   private migrateSiteTypeToPlatform(): void {
+    // Only run if sites table exists (might not on fresh install — initializeTables handles that)
+    const hasSitesTable = this.db.prepare(
+      "SELECT name FROM sqlite_master WHERE type='table' AND name='sites'"
+    ).get();
+    if (!hasSitesTable) return;
+
     // Check if site_platform column already exists
     const columns = this.db.pragma('table_info(sites)') as any[];
     const hasSitePlatform = columns.some((col: any) => col.name === 'site_platform');