Line data Source code
1 : import type { SessionMigrationStep } from '../session_migration.js';
2 :
3 : /// Sessions no longer have a default connection, they hold the connections their scripts use.
4 : ///
5 : /// Version 7 named a default connection that new scripts used and that the session held even when no
6 : /// script ran on it. The connections are reduced to the ones the scripts use, in the order the scripts
7 : /// first use them. A session without scripts keeps its default connection, or its first connection, its
8 : /// first script runs on it.
9 1 : export const connectionsOfScripts: SessionMigrationStep = {
10 1 : version: 8,
11 1 : description: 'Hold the connections of the scripts without a default connection',
12 1 : async migrate(folder) {
13 1 : const text = await folder.readText('dashql-session.json');
14 1 : if (text == null) {
15 0 : return;
16 : }
17 1 : const data = JSON.parse(text) as Record<string, unknown>;
18 1 : const scripts = Array.isArray(data.scripts) ? data.scripts as unknown[] : [];
19 1 : const connections = Array.isArray(data.connections) ? data.connections as unknown[] : [];
20 1 : const scriptConnectionIds = scripts.flatMap(script => {
21 1 : const connectionId = script != null && typeof script === 'object' ? (script as Record<string, unknown>).connectionId : null;
22 1 : return typeof connectionId === 'string' ? [connectionId] : [];
23 1 : });
24 1 : const byId = new Map<string, unknown>();
25 1 : for (const connection of connections) {
26 1 : const connectionId = connection != null && typeof connection === 'object' ? (connection as Record<string, unknown>).connectionId : null;
27 1 : if (typeof connectionId === 'string' && !byId.has(connectionId)) byId.set(connectionId, connection);
28 1 : }
29 1 : const emptyConnectionId = typeof data.defaultConnectionId === 'string' && byId.has(data.defaultConnectionId)
30 1 : ? data.defaultConnectionId
31 1 : : byId.keys().next().value;
32 1 : const used = scriptConnectionIds.length > 0 ? scriptConnectionIds : emptyConnectionId == null ? [] : [emptyConnectionId];
33 : // A session holds at least one connection, scripts on unknown connections run on it
34 1 : let kept: unknown[] = [...new Set(used)].flatMap(connectionId => byId.has(connectionId) ? [byId.get(connectionId)] : []);
35 1 : if (kept.length === 0 && emptyConnectionId != null) kept = [byId.get(emptyConnectionId)];
36 1 : const migrated: Record<string, unknown> = {};
37 1 : for (const [key, value] of Object.entries(data)) {
38 1 : if (key === 'defaultConnectionId') continue;
39 1 : migrated[key] = key === 'connections' ? kept : value;
40 1 : }
41 1 : await folder.writeText('dashql-session.json', JSON.stringify(migrated, null, 2));
42 1 : },
43 1 : };
|