LCOV - code coverage report
Current view: top level - dashql-app/src/storage/migration/session - v6_session_connections.ts Coverage Total Hit
Test: DashQL 915828b Lines: 97.3 % 37 36
Test Date: 2026-11-06 19:30:32

            Line data    Source code
       1              : import type { SessionMigrationStep } from '../session_migration.js';
       2              : 
       3              : /// The databases of a session become one list of connections with a default connection.
       4              : ///
       5              : /// Version 5 referenced a main database and a list of attached databases, the scripts named their
       6              : /// database by its databaseId. The main database is the default connection, the databases keep
       7              : /// their ids as connection ids and their order, main database first.
       8            1 : export const sessionConnections: SessionMigrationStep = {
       9            1 :     version: 6,
      10            1 :     description: 'List the databases of the session as connections with a default connection',
      11            1 :     async migrate(folder) {
      12            1 :         const text = await folder.readText('dashql-session.json');
      13            1 :         if (text == null) {
      14            0 :             return;
      15              :         }
      16            1 :         const data = JSON.parse(text) as Record<string, unknown>;
      17            1 :         if (Array.isArray(data.connections)) {
      18            1 :             return;
      19              :         }
      20            1 :         const mainDatabase = data.mainDatabase as Record<string, unknown> | undefined;
      21            1 :         const attachedDatabases = Array.isArray(data.attachedDatabases) ? data.attachedDatabases as unknown[] : [];
      22            1 :         const connections = [mainDatabase, ...attachedDatabases].flatMap(database => {
      23            1 :             if (database == null || typeof database !== 'object') return [];
      24            1 :             const { databaseId, ...rest } = database as Record<string, unknown>;
      25            1 :             return [{ connectionId: databaseId, ...rest }];
      26            1 :         });
      27            1 :         const fields = {
      28            1 :             defaultConnectionId: typeof mainDatabase?.databaseId === 'string' ? mainDatabase.databaseId : null,
      29            1 :             connections,
      30            1 :         };
      31              :         // The connections take the place of the main database in the metadata
      32            1 :         const migrated: Record<string, unknown> = {};
      33            1 :         for (const [key, value] of Object.entries(data)) {
      34            1 :             if (key === 'mainDatabase') {
      35            1 :                 Object.assign(migrated, fields);
      36            1 :             } else if (key === 'scripts' && Array.isArray(value)) {
      37            1 :                 migrated.scripts = value.map(script => {
      38            1 :                     if (script == null || typeof script !== 'object') return script;
      39            1 :                     const { databaseId, ...rest } = script as Record<string, unknown>;
      40            1 :                     return { ...rest, connectionId: databaseId };
      41            1 :                 });
      42            1 :             } else if (key !== 'attachedDatabases') {
      43            1 :                 migrated[key] = value;
      44              :             }
      45              :         }
      46            1 :         if (!('mainDatabase' in data)) Object.assign(migrated, fields);
      47            1 :         await folder.writeText('dashql-session.json', JSON.stringify(migrated, null, 2));
      48            1 :     },
      49            1 : };
        

Generated by: LCOV version 2.0-1