@myrialabs/clopen 0.4.47 → 0.4.48
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 +11 -11
- package/backend/artifacts/project/sources.ts +1 -1
- package/backend/auth/auth-service.ts +52 -358
- package/backend/auth/onboarding.test.ts +2 -3
- package/backend/auth/permissions.test.ts +10 -20
- package/backend/auth/permissions.ts +4 -290
- package/backend/auth/rate-limiter.test.ts +8 -9
- package/backend/auth/rate-limiter.ts +0 -2
- package/backend/auth/tokens.ts +1 -8
- package/backend/bootstrap.ts +7 -0
- package/backend/chat/attachments.test.ts +88 -0
- package/backend/chat/attachments.ts +159 -0
- package/backend/chat/engine-handoff.test.ts +0 -2
- package/backend/chat/stream-manager.ts +62 -62
- package/backend/chat/turn-answer.test.ts +64 -0
- package/backend/chat/turn-answer.ts +125 -0
- package/backend/chat-apps/awaiting.test.ts +39 -0
- package/backend/chat-apps/awaiting.ts +49 -0
- package/backend/chat-apps/conversation.ts +746 -0
- package/backend/chat-apps/inbound.ts +324 -0
- package/backend/chat-apps/index.ts +309 -0
- package/backend/chat-apps/link-events.ts +20 -0
- package/backend/chat-apps/media.test.ts +62 -0
- package/backend/chat-apps/media.ts +208 -0
- package/backend/chat-apps/messages.test.ts +229 -0
- package/backend/chat-apps/messages.ts +365 -0
- package/backend/chat-apps/outbound.ts +243 -0
- package/backend/chat-apps/pairing.test.ts +45 -0
- package/backend/chat-apps/pairing.ts +74 -0
- package/backend/chat-apps/platforms/index.ts +20 -0
- package/backend/chat-apps/platforms/telegram.test.ts +35 -0
- package/backend/chat-apps/platforms/telegram.ts +191 -0
- package/backend/chat-apps/platforms/types.ts +114 -0
- package/backend/chat-apps/questions.ts +133 -0
- package/backend/chat-apps/runtime.ts +572 -0
- package/backend/chat-apps/storage.test.ts +201 -0
- package/backend/chat-apps/turns.test.ts +45 -0
- package/backend/chat-apps/turns.ts +208 -0
- package/backend/containers/pty.ts +3 -11
- package/backend/control/actions/accounts.ts +89 -0
- package/backend/control/actions/files.ts +116 -0
- package/backend/control/actions/models.ts +188 -0
- package/backend/control/actions/projects.ts +272 -0
- package/backend/control/actions/sessions.ts +280 -0
- package/backend/control/actions/skills.ts +111 -0
- package/backend/control/control.test.ts +169 -0
- package/backend/control/define.ts +20 -0
- package/backend/control/folders.test.ts +67 -0
- package/backend/control/index.ts +213 -0
- package/backend/control/invoke.ts +44 -0
- package/backend/control/links.ts +35 -0
- package/backend/control/paging.ts +26 -0
- package/backend/control/types.ts +117 -0
- package/backend/control/workspace.ts +127 -0
- package/backend/database/migrations/084_create_channel_links.ts +71 -0
- package/backend/database/migrations/085_add_home_projects.ts +36 -0
- package/backend/database/migrations/086_single_owner.test.ts +170 -0
- package/backend/database/migrations/086_single_owner.ts +418 -0
- package/backend/database/migrations/index.ts +21 -0
- package/backend/database/queries/auth-queries.ts +7 -85
- package/backend/database/queries/channel-queries.ts +270 -0
- package/backend/database/queries/db-client-connection-queries.ts +18 -56
- package/backend/database/queries/file-share-queries.ts +5 -23
- package/backend/database/queries/git-identity-queries.ts +31 -42
- package/backend/database/queries/graph-queries.ts +1 -1
- package/backend/database/queries/index.ts +13 -1
- package/backend/database/queries/message-queries.test.ts +146 -0
- package/backend/database/queries/message-queries.ts +42 -1
- package/backend/database/queries/permission-set-queries.ts +1 -1
- package/backend/database/queries/profile-queries.ts +1 -1
- package/backend/database/queries/project-mcp-trust-queries.ts +1 -1
- package/backend/database/queries/project-queries.ts +50 -63
- package/backend/database/queries/session-queries.ts +28 -56
- package/backend/database/queries/ssh-connection-queries.ts +18 -41
- package/backend/db-client/integrations/projector.ts +0 -5
- package/backend/engine/adapters/claude/message-converter.test.ts +47 -0
- package/backend/engine/adapters/claude/message-converter.ts +25 -2
- package/backend/engine/adapters/cline/message-converter.ts +0 -1
- package/backend/engine/adapters/cline/stream.ts +11 -15
- package/backend/engine/adapters/codex/message-converter.ts +0 -2
- package/backend/engine/adapters/copilot/message-converter.ts +1 -2
- package/backend/engine/adapters/cursor/message-converter.ts +0 -1
- package/backend/engine/adapters/opencode/message-converter.ts +2 -4
- package/backend/engine/adapters/opencode/stream.ts +11 -0
- package/backend/engine/adapters/parent-scope.test.ts +38 -0
- package/backend/engine/adapters/parent-scope.ts +44 -0
- package/backend/engine/adapters/pi/message-converter.ts +0 -1
- package/backend/engine/adapters/pi/stream.ts +4 -5
- package/backend/engine/adapters/qwen/message-converter.ts +0 -2
- package/backend/engine/bootstrap-default-engine.ts +2 -2
- package/backend/engine/docs/adapter-contract.md +8 -1
- package/backend/engine/install-runner.ts +8 -17
- package/backend/files/file-shares.ts +20 -44
- package/backend/files/file-size-limit.ts +3 -3
- package/backend/files/image-processing.ts +1 -1
- package/backend/files/os-clipboard.ts +2 -2
- package/backend/git/git-executor.ts +25 -5
- package/backend/git/git-service.ts +420 -19
- package/backend/git/identity/credential-helper.test.ts +2 -7
- package/backend/git/identity/import-local.test.ts +11 -16
- package/backend/git/identity/import-local.ts +12 -12
- package/backend/git/identity/index.ts +10 -24
- package/backend/git/identity/resolve.ts +13 -14
- package/backend/git/identity/scope-config.ts +33 -28
- package/backend/git/identity/service.ts +44 -47
- package/backend/git/op-lock.test.ts +111 -0
- package/backend/git/op-lock.ts +180 -0
- package/backend/git/remote-sync.test.ts +249 -0
- package/backend/host/metrics.ts +1 -1
- package/backend/http/bearer-auth.ts +1 -2
- package/backend/http/files-download.test.ts +8 -9
- package/backend/http/files-download.ts +2 -2
- package/backend/http/files-shared.test.ts +62 -89
- package/backend/http/files-shared.ts +2 -2
- package/backend/http/files-upload.test.ts +3 -7
- package/backend/http/files-upload.ts +3 -3
- package/backend/http/notes-images.test.ts +15 -38
- package/backend/http/notes-images.ts +6 -8
- package/backend/http/ssh-sftp.ts +5 -10
- package/backend/index.ts +21 -7
- package/backend/integrations/accounts.ts +61 -3
- package/backend/integrations/index.ts +2 -2
- package/backend/integrations/registry/define.ts +15 -0
- package/backend/integrations/registry/index.ts +2 -0
- package/backend/integrations/registry/providers/telegram.ts +54 -0
- package/backend/integrations/registry/types.ts +21 -1
- package/backend/integrations/webhooks/gateway.ts +7 -6
- package/backend/integrations/webhooks/verify.test.ts +30 -0
- package/backend/integrations/webhooks/verify.ts +4 -0
- package/backend/mcp/external/project.ts +1 -1
- package/backend/mcp/internal/remote-server.test.ts +1 -3
- package/backend/mcp/internal/remote-server.ts +4 -4
- package/backend/mcp/internal/servers/browser-automation/actions/dialogs.ts +67 -0
- package/backend/mcp/internal/servers/browser-automation/actions/index.ts +3 -1
- package/backend/mcp/internal/servers/browser-automation/actions/input.ts +5 -5
- package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +157 -28
- package/backend/mcp/internal/servers/browser-automation/context.ts +1 -0
- package/backend/mcp/internal/servers/browser-automation/description.ts +5 -2
- package/backend/mcp/internal/servers/browser-automation/format.ts +24 -1
- package/backend/mcp/internal/servers/browser-automation/runner.ts +68 -3
- package/backend/memory/redact.ts +1 -1
- package/backend/notes/access.test.ts +28 -29
- package/backend/notes/access.ts +6 -11
- package/backend/permissions/service.ts +1 -1
- package/backend/preview/browser/browser-clipboard.test.ts +46 -0
- package/backend/preview/browser/browser-clipboard.ts +67 -0
- package/backend/preview/browser/browser-dialog-handler.ts +95 -6
- package/backend/preview/browser/browser-host-bridge.ts +9 -0
- package/backend/preview/browser/browser-interaction-handler.ts +316 -86
- package/backend/preview/browser/browser-mcp-control.ts +34 -0
- package/backend/preview/browser/browser-pool.ts +67 -6
- package/backend/preview/browser/browser-preview-service.ts +361 -25
- package/backend/preview/browser/browser-tab-manager.ts +66 -23
- package/backend/preview/browser/browser-video-capture.ts +92 -5
- package/backend/preview/browser/element-locator.ts +190 -17
- package/backend/preview/browser/keyboard-chords.test.ts +68 -0
- package/backend/preview/browser/keyboard-chords.ts +132 -0
- package/backend/preview/browser/paste.ts +81 -0
- package/backend/preview/browser/process-usage.ts +142 -0
- package/backend/preview/browser/profile-lock.ts +209 -0
- package/backend/preview/browser/scripts/host-bridge.ts +59 -0
- package/backend/preview/browser/scripts/video-stream.ts +58 -0
- package/backend/preview/browser/types.ts +40 -2
- package/backend/preview/browser/viewport.test.ts +45 -0
- package/backend/preview/browser/viewport.ts +96 -0
- package/backend/profiles/service.ts +2 -2
- package/backend/project/status-manager.ts +43 -172
- package/backend/projects/home.ts +61 -0
- package/backend/projects/project-cleanup.integration.test.ts +2 -2
- package/backend/projects/project-cleanup.ts +0 -8
- package/backend/skills/service.ts +2 -3
- package/backend/snapshot/helpers.ts +0 -1
- package/backend/snapshot/restore.ts +252 -0
- package/backend/ssh/ptykit.ts +1 -2
- package/backend/terminal/ptykit.ts +5 -8
- package/backend/tunnel/public-origin.ts +31 -20
- package/backend/utils/user-helpers.ts +0 -55
- package/backend/utils/ws.ts +61 -154
- package/backend/worktrees/branching/engine.ts +3 -16
- package/backend/worktrees/branching/index.ts +1 -1
- package/backend/worktrees/branching/projector.ts +1 -1
- package/backend/worktrees/branching/service.ts +13 -25
- package/backend/ws/access.ts +12 -21
- package/backend/ws/artifacts/generate.ts +2 -2
- package/backend/ws/auth/device.ts +16 -71
- package/backend/ws/auth/index.ts +4 -22
- package/backend/ws/auth/login.test.ts +14 -60
- package/backend/ws/auth/login.ts +24 -124
- package/backend/ws/auth/status.ts +0 -1
- package/backend/ws/chat/composer.ts +118 -118
- package/backend/ws/chat/stream.ts +64 -47
- package/backend/ws/chat-apps/index.ts +117 -0
- package/backend/ws/containers/actions.ts +11 -25
- package/backend/ws/containers/index.ts +3 -4
- package/backend/ws/containers/list.ts +10 -36
- package/backend/ws/containers/logs.ts +2 -2
- package/backend/ws/containers/tunnel.ts +1 -2
- package/backend/ws/db-client/access.ts +2 -16
- package/backend/ws/db-client/accounts.ts +5 -15
- package/backend/ws/db-client/connections.ts +17 -25
- package/backend/ws/db-client/context.ts +1 -1
- package/backend/ws/db-client/env.ts +7 -10
- package/backend/ws/db-client/index.ts +2 -2
- package/backend/ws/db-client/io.ts +5 -5
- package/backend/ws/db-client/query-history-retention.test.ts +8 -8
- package/backend/ws/db-client/query.ts +18 -17
- package/backend/ws/db-client/schema.ts +13 -13
- package/backend/ws/db-client/structure.ts +35 -35
- package/backend/ws/db-client/supabase.ts +4 -21
- package/backend/ws/deployments/context.ts +1 -1
- package/backend/ws/deployments/crud.ts +0 -5
- package/backend/ws/engine/access.ts +7 -20
- package/backend/ws/engine/claude/accounts.ts +4 -4
- package/backend/ws/engine/cline/accounts.ts +5 -5
- package/backend/ws/engine/codex/accounts.ts +3 -3
- package/backend/ws/engine/pi/accounts.ts +5 -5
- package/backend/ws/files/clipboard.ts +8 -27
- package/backend/ws/files/image-edit.ts +6 -6
- package/backend/ws/files/index.ts +1 -1
- package/backend/ws/files/path-access.test.ts +32 -131
- package/backend/ws/files/path-access.ts +8 -87
- package/backend/ws/files/read.ts +15 -19
- package/backend/ws/files/reveal.ts +3 -3
- package/backend/ws/files/search.ts +7 -7
- package/backend/ws/files/share.ts +9 -13
- package/backend/ws/files/state.ts +9 -12
- package/backend/ws/files/watch.ts +3 -3
- package/backend/ws/files/write.ts +30 -31
- package/backend/ws/git/branch.ts +80 -59
- package/backend/ws/git/commit.ts +63 -44
- package/backend/ws/git/conflict.ts +16 -18
- package/backend/ws/git/identity-context.ts +8 -17
- package/backend/ws/git/identity.ts +17 -27
- package/backend/ws/git/index.ts +3 -1
- package/backend/ws/git/op-state.ts +139 -0
- package/backend/ws/git/remote.ts +241 -141
- package/backend/ws/git/repo-context.ts +67 -0
- package/backend/ws/git/staging.ts +18 -28
- package/backend/ws/git/status.ts +5 -2
- package/backend/ws/index.ts +5 -1
- package/backend/ws/instructions/crud.ts +1 -1
- package/backend/ws/integrations/crud.ts +1 -3
- package/backend/ws/mcp/crud.ts +3 -5
- package/backend/ws/mcp/tools.ts +1 -1
- package/backend/ws/memory/crud.ts +14 -46
- package/backend/ws/memory/index.ts +2 -2
- package/backend/ws/messages/crud.ts +29 -8
- package/backend/ws/notes/index.ts +33 -39
- package/backend/ws/permissions/crud.ts +3 -4
- package/backend/ws/ports/index.ts +1 -2
- package/backend/ws/ports/kill.ts +5 -10
- package/backend/ws/ports/list.ts +4 -10
- package/backend/ws/preview/access.ts +1 -1
- package/backend/ws/preview/browser/interact.ts +52 -79
- package/backend/ws/preview/browser/native-ui.ts +24 -0
- package/backend/ws/preview/browser/stats.ts +98 -1
- package/backend/ws/preview/browser/tab-info.ts +15 -0
- package/backend/ws/preview/browser/tab.ts +19 -4
- package/backend/ws/preview/index.ts +27 -0
- package/backend/ws/profiles/crud.ts +6 -8
- package/backend/ws/project-artifacts/index.ts +30 -31
- package/backend/ws/projects/crud.ts +33 -35
- package/backend/ws/projects/index.ts +3 -8
- package/backend/ws/projects/info.ts +3 -3
- package/backend/ws/projects/overview.ts +19 -30
- package/backend/ws/projects/status.ts +17 -57
- package/backend/ws/sessions/crud.ts +34 -45
- package/backend/ws/sessions/index.ts +1 -1
- package/backend/ws/settings/crud.ts +1 -1
- package/backend/ws/skills/crud.ts +3 -4
- package/backend/ws/skills/index.ts +1 -1
- package/backend/ws/snapshot/changes.ts +7 -7
- package/backend/ws/snapshot/restore.ts +11 -295
- package/backend/ws/snapshot/timeline.ts +2 -4
- package/backend/ws/ssh/access.ts +6 -21
- package/backend/ws/ssh/connections.ts +21 -31
- package/backend/ws/ssh/forwards.ts +12 -12
- package/backend/ws/ssh/sftp.ts +34 -34
- package/backend/ws/ssh/tunnel.ts +1 -2
- package/backend/ws/stack/install.ts +7 -17
- package/backend/ws/subagents/crud.ts +1 -1
- package/backend/ws/tunnel/share.ts +0 -2
- package/backend/ws/user/crud.ts +1 -59
- package/backend/ws/user/index.ts +1 -1
- package/backend/ws/work/context.ts +1 -1
- package/backend/ws/work/crud.ts +0 -5
- package/backend/ws/workspace/state.ts +10 -13
- package/backend/ws/worktrees/branching.ts +7 -36
- package/backend/ws/worktrees/crud.ts +2 -2
- package/bin/clopen.ts +10 -11
- package/dist/assets/{index-CwuKQelV.js → index-CONlwAXZ.js} +1 -1
- package/dist/assets/{index-BTcR1byF.js → index-DJzwt-hq.js} +1 -1
- package/dist/assets/main-BUiUL3eZ.css +1 -0
- package/dist/assets/main-nBOb9YOA.js +1240 -0
- package/dist/index.html +2 -2
- package/package.json +2 -1
- package/shared/constants/attachments.ts +14 -0
- package/shared/constants/engine-tools.ts +1 -1
- package/shared/constants/preview.test.ts +259 -0
- package/shared/constants/preview.ts +1018 -57
- package/shared/constants/tool-icons.ts +5 -0
- package/shared/types/containers/container.ts +1 -1
- package/shared/types/database/schema.ts +9 -6
- package/shared/types/git-identity.ts +1 -1
- package/shared/types/git.ts +116 -0
- package/shared/types/integrations.ts +7 -0
- package/shared/types/stores/settings.ts +1 -1
- package/shared/types/unified/index.ts +1 -1
- package/shared/types/unified/message.ts +0 -7
- package/shared/types/unified/stream.ts +2 -4
- package/shared/types/unified/tool.ts +15 -1
- package/shared/utils/ask-user-question.test.ts +319 -0
- package/shared/utils/ask-user-question.ts +154 -0
- package/shared/utils/home-project.ts +25 -0
- package/shared/utils/logger.ts +2 -0
- package/shared/utils/ws-server.ts +32 -7
- package/backend/auth/session-invalidation.test.ts +0 -112
- package/backend/auth/session-invalidation.ts +0 -77
- package/backend/project/status-manager.test.ts +0 -32
- package/backend/ws/auth/invites.ts +0 -94
- package/backend/ws/auth/users.ts +0 -112
- package/backend/ws/projects/presence.ts +0 -46
- package/backend/ws/stack/access.ts +0 -31
- package/dist/assets/main-BiDNMWb3.css +0 -1
- package/dist/assets/main-BlhKWz2B.js +0 -1241
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 086 takes a multi-user install to a single owner. It runs the real
|
|
3
|
+
* migration chain up to 085 against an in-memory SQLite, seeds the shapes a
|
|
4
|
+
* team install actually had, then checks what 086 keeps and what it removes —
|
|
5
|
+
* above all that no credential of a removed account survives.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, test } from 'bun:test';
|
|
9
|
+
import { Database } from 'bun:sqlite';
|
|
10
|
+
import { mkdtempSync, mkdirSync, existsSync, rmSync } from 'fs';
|
|
11
|
+
import { join } from 'path';
|
|
12
|
+
import { tmpdir } from 'os';
|
|
13
|
+
import type { DatabaseConnection } from '$shared/types/database/connection';
|
|
14
|
+
import migrations from './index';
|
|
15
|
+
import { up, down } from './086_single_owner';
|
|
16
|
+
|
|
17
|
+
const NOW = '2026-01-01T00:00:00.000Z';
|
|
18
|
+
|
|
19
|
+
function migratedTo085(): Database {
|
|
20
|
+
const db = new Database(':memory:');
|
|
21
|
+
db.exec('PRAGMA foreign_keys = ON');
|
|
22
|
+
for (const migration of [...migrations].sort((a, b) => a.id.localeCompare(b.id))) {
|
|
23
|
+
if (migration.id >= '086') break;
|
|
24
|
+
migration.up(db as unknown as DatabaseConnection);
|
|
25
|
+
}
|
|
26
|
+
return db;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function addUser(db: Database, id: string, role: 'admin' | 'member', createdAt: string): void {
|
|
30
|
+
db.prepare(`INSERT INTO users (id, name, color, avatar, role, personal_access_token_hash, created_at, updated_at) VALUES (?, ?, '#000', 'X', ?, ?, ?, ?)`)
|
|
31
|
+
.run(id, id, role, `pat-${id}`, createdAt, createdAt);
|
|
32
|
+
db.prepare(`INSERT INTO auth_sessions (id, user_id, token_hash, expires_at, created_at, last_active_at) VALUES (?, ?, ?, '2099-01-01', ?, ?)`)
|
|
33
|
+
.run(`s-${id}`, id, `hash-${id}`, NOW, NOW);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function addProject(db: Database, id: string, path: string, kind: 'project' | 'home' = 'project', owner: string | null = null): void {
|
|
37
|
+
db.prepare(`INSERT INTO projects (id, name, path, created_at, last_opened_at, kind, owner_user_id) VALUES (?, ?, ?, ?, ?, ?, ?)`)
|
|
38
|
+
.run(id, id, path, NOW, NOW, kind, owner);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function seed(home: string): Database {
|
|
42
|
+
const db = migratedTo085();
|
|
43
|
+
// The oldest admin is the owner; a second admin and a member are removed.
|
|
44
|
+
addUser(db, 'owner', 'admin', '2025-01-01');
|
|
45
|
+
addUser(db, 'admin2', 'admin', '2025-02-01');
|
|
46
|
+
addUser(db, 'member', 'member', '2024-01-01');
|
|
47
|
+
|
|
48
|
+
addProject(db, 'shared', '/repo/shared');
|
|
49
|
+
addProject(db, 'removed', '/repo/removed');
|
|
50
|
+
addProject(db, 'home-owner', join(home, 'owner'), 'home', 'owner');
|
|
51
|
+
addProject(db, 'home-member', join(home, 'member'), 'home', 'member');
|
|
52
|
+
mkdirSync(join(home, 'member'), { recursive: true });
|
|
53
|
+
|
|
54
|
+
for (const [user, project, state] of [['owner', 'shared', 'owner-layout'], ['owner', 'home-owner', null], ['member', 'shared', 'member-layout'], ['member', 'home-member', null]] as const) {
|
|
55
|
+
db.prepare(`INSERT INTO user_projects (user_id, project_id, joined_at, workspace_state) VALUES (?, ?, ?, ?)`).run(user, project, NOW, state);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// A chat the member had in the shared project is project history; one in their Home is theirs.
|
|
59
|
+
db.prepare(`INSERT INTO chat_sessions (id, project_id, started_at) VALUES ('chat-shared', 'shared', ?)`).run(NOW);
|
|
60
|
+
db.prepare(`INSERT INTO chat_sessions (id, project_id, started_at) VALUES ('chat-home', 'home-member', ?)`).run(NOW);
|
|
61
|
+
db.prepare(`INSERT INTO user_unread_sessions (user_id, session_id, project_id, marked_at) VALUES ('owner', 'chat-shared', 'shared', ?), ('member', 'chat-shared', 'shared', ?)`).run(NOW, NOW);
|
|
62
|
+
|
|
63
|
+
db.prepare(`INSERT INTO invite_tokens (id, token_hash, role, created_by, created_at) VALUES ('inv', 'inv-hash', 'member', 'owner', ?)`).run(NOW);
|
|
64
|
+
db.prepare(`INSERT INTO device_codes (id, code_hash, user_id, expires_at, created_at) VALUES ('dev', 'dev-hash', 'member', '2099-01-01', ?)`).run(NOW);
|
|
65
|
+
|
|
66
|
+
for (const [id, owner] of [['db-owner', 'owner'], ['db-member', 'member'], ['db-legacy', null]] as const) {
|
|
67
|
+
db.prepare(`INSERT INTO db_client_connections (id, name, driver, created_at, updated_at, owner_user_id) VALUES (?, ?, 'postgres', ?, ?, ?)`).run(id, id, NOW, NOW, owner);
|
|
68
|
+
}
|
|
69
|
+
for (const [id, owner] of [['ssh-owner', 'owner'], ['ssh-member', 'member']] as const) {
|
|
70
|
+
db.prepare(`INSERT INTO ssh_connections (id, name, host, port, username, auth_method, created_at, updated_at, owner_user_id) VALUES (?, ?, 'h', 22, 'u', 'agent', ?, ?, ?)`).run(id, id, NOW, NOW, owner);
|
|
71
|
+
}
|
|
72
|
+
for (const [id, owner, isDefault] of [['gi-owner', 'owner', 1], ['gi-member', 'member', 1]] as const) {
|
|
73
|
+
db.prepare(`INSERT INTO git_identities (id, owner_user_id, label, name, email, is_default, created_at, updated_at) VALUES (?, ?, 'Work', 'N', 'e@x.y', ?, ?, ?)`).run(id, owner, isDefault, NOW, NOW);
|
|
74
|
+
}
|
|
75
|
+
db.prepare(`INSERT INTO git_identity_bindings (project_id, user_id, identity_id, created_at, updated_at) VALUES ('shared', 'owner', 'gi-owner', ?, ?), ('shared', 'member', 'gi-member', ?, ?)`).run(NOW, NOW, NOW, NOW);
|
|
76
|
+
|
|
77
|
+
db.prepare(`INSERT INTO integration_accounts (id, provider, label, auth_method, created_at, updated_at) VALUES ('bot', 'telegram', 'Bot', 'token', ?, ?)`).run(NOW, NOW);
|
|
78
|
+
db.prepare(`INSERT INTO channel_links (id, account_id, user_id, conversation_id, platform_user_id, status, created_at) VALUES ('link-owner', 'bot', 'owner', 'c1', 'p1', 'approved', ?), ('link-member', 'bot', 'member', 'c2', 'p2', 'pending', ?)`).run(NOW, NOW);
|
|
79
|
+
return db;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function ids(db: Database, sql: string): string[] {
|
|
83
|
+
return (db.prepare(sql).all() as { id: string }[]).map((row) => row.id);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function columns(db: Database, table: string): string[] {
|
|
87
|
+
return (db.prepare(`PRAGMA table_info(${table})`).all() as { name: string }[]).map((c) => c.name);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
describe('migration 086', () => {
|
|
91
|
+
// Homes live under `<data dir>/home/<userId>`; the migration only removes a
|
|
92
|
+
// directory of exactly that shape.
|
|
93
|
+
const dataDir = mkdtempSync(join(tmpdir(), 'clopen-086-'));
|
|
94
|
+
const home = join(dataDir, 'home');
|
|
95
|
+
|
|
96
|
+
test('keeps the oldest admin as the only account, and nothing a removed account could sign in with', () => {
|
|
97
|
+
const db = seed(home);
|
|
98
|
+
up(db as unknown as DatabaseConnection);
|
|
99
|
+
|
|
100
|
+
expect(ids(db, 'SELECT id FROM users')).toEqual(['owner']);
|
|
101
|
+
expect(ids(db, 'SELECT user_id AS id FROM auth_sessions')).toEqual(['owner']);
|
|
102
|
+
expect(ids(db, 'SELECT id FROM device_codes')).toEqual([]);
|
|
103
|
+
expect(ids(db, 'SELECT id FROM channel_links')).toEqual(['link-owner']);
|
|
104
|
+
expect(columns(db, 'users')).not.toContain('role');
|
|
105
|
+
expect(db.prepare(`SELECT name FROM sqlite_master WHERE name = 'invite_tokens'`).get()).toBeNull();
|
|
106
|
+
db.close();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('removes what was the others alone, keeps shared project history', () => {
|
|
110
|
+
const db = seed(home);
|
|
111
|
+
up(db as unknown as DatabaseConnection);
|
|
112
|
+
|
|
113
|
+
expect(ids(db, 'SELECT id FROM projects ORDER BY id')).toEqual(['home-owner', 'removed', 'shared']);
|
|
114
|
+
expect(ids(db, 'SELECT id FROM chat_sessions')).toEqual(['chat-shared']);
|
|
115
|
+
expect(existsSync(join(home, 'member'))).toBe(false);
|
|
116
|
+
expect(ids(db, 'SELECT id FROM db_client_connections ORDER BY id')).toEqual(['db-legacy', 'db-owner']);
|
|
117
|
+
expect(ids(db, 'SELECT id FROM ssh_connections')).toEqual(['ssh-owner']);
|
|
118
|
+
expect(ids(db, 'SELECT id FROM git_identities')).toEqual(['gi-owner']);
|
|
119
|
+
expect(ids(db, 'SELECT identity_id AS id FROM git_identity_bindings')).toEqual(['gi-owner']);
|
|
120
|
+
db.close();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("carries the owner's workspace list and state over, without a user key", () => {
|
|
124
|
+
const db = seed(home);
|
|
125
|
+
up(db as unknown as DatabaseConnection);
|
|
126
|
+
|
|
127
|
+
const rows = db.prepare('SELECT project_id, workspace_state FROM workspace_projects ORDER BY project_id').all();
|
|
128
|
+
expect(rows).toEqual([
|
|
129
|
+
{ project_id: 'home-owner', workspace_state: null },
|
|
130
|
+
{ project_id: 'shared', workspace_state: 'owner-layout' }
|
|
131
|
+
]);
|
|
132
|
+
// `removed` was never in the owner's list, so it stays removed.
|
|
133
|
+
expect(db.prepare(`SELECT session_id FROM unread_sessions`).all()).toEqual([{ session_id: 'chat-shared' }]);
|
|
134
|
+
for (const [table, column] of [['projects', 'owner_user_id'], ['db_client_connections', 'owner_user_id'], ['ssh_connections', 'owner_user_id'], ['git_identities', 'owner_user_id'], ['git_identity_bindings', 'user_id'], ['chat_sessions', 'sender_id'], ['channel_links', 'status']] as const) {
|
|
135
|
+
expect(columns(db, table)).not.toContain(column);
|
|
136
|
+
}
|
|
137
|
+
db.close();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('rolls back to the multi-user schema, the owner as its admin', () => {
|
|
141
|
+
const db = seed(home);
|
|
142
|
+
up(db as unknown as DatabaseConnection);
|
|
143
|
+
down(db as unknown as DatabaseConnection);
|
|
144
|
+
|
|
145
|
+
expect(db.prepare('SELECT id, role FROM users').all()).toEqual([{ id: 'owner', role: 'admin' }]);
|
|
146
|
+
expect(db.prepare('SELECT user_id, project_id FROM user_projects ORDER BY project_id').all()).toEqual([
|
|
147
|
+
{ user_id: 'owner', project_id: 'home-owner' },
|
|
148
|
+
{ user_id: 'owner', project_id: 'shared' }
|
|
149
|
+
]);
|
|
150
|
+
expect(db.prepare('SELECT owner_user_id FROM git_identities').all()).toEqual([{ owner_user_id: 'owner' }]);
|
|
151
|
+
expect(columns(db, 'chat_sessions')).toContain('sender_id');
|
|
152
|
+
db.close();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test('allows one Home and one default identity for the install', () => {
|
|
156
|
+
const db = seed(home);
|
|
157
|
+
up(db as unknown as DatabaseConnection);
|
|
158
|
+
|
|
159
|
+
expect(() => addProjectAfter(db, 'home-2')).toThrow();
|
|
160
|
+
expect(() =>
|
|
161
|
+
db.prepare(`INSERT INTO git_identities (id, label, name, email, is_default, created_at, updated_at) VALUES ('gi-2', 'Other', 'N', 'e@x.y', 1, ?, ?)`).run(NOW, NOW)
|
|
162
|
+
).toThrow();
|
|
163
|
+
db.close();
|
|
164
|
+
rmSync(dataDir, { recursive: true, force: true });
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
function addProjectAfter(db: Database, id: string): void {
|
|
169
|
+
db.prepare(`INSERT INTO projects (id, name, path, created_at, last_opened_at, kind) VALUES (?, ?, ?, ?, ?, 'home')`).run(id, id, `/${id}`, NOW, NOW);
|
|
170
|
+
}
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import { rmSync } from 'fs';
|
|
2
|
+
import { basename, dirname } from 'path';
|
|
3
|
+
import type { DatabaseConnection } from '$shared/types/database/connection';
|
|
4
|
+
|
|
5
|
+
import { debug } from '$shared/utils/logger';
|
|
6
|
+
|
|
7
|
+
export const description = 'Collapse multi-user collaboration into a single owner';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Clopen is one owner reaching their own machine from any number of devices.
|
|
11
|
+
* Multi-user collaboration (roles, invites, per-project membership, per-user
|
|
12
|
+
* scoping) is removed, and this migration takes an existing install there.
|
|
13
|
+
*
|
|
14
|
+
* The owner is the oldest admin — the account the setup wizard created — or,
|
|
15
|
+
* failing that, the oldest user. Everyone else is deleted together with the
|
|
16
|
+
* credentials and personal data that belonged to them. That part is a security
|
|
17
|
+
* requirement, not tidiness: once role checks are gone, any token a former
|
|
18
|
+
* member still holds would otherwise sign in as someone with full control.
|
|
19
|
+
*
|
|
20
|
+
* Shared history stays. Chats a member had in a regular project are project
|
|
21
|
+
* history and remain in that project; only what was theirs alone (their Home,
|
|
22
|
+
* their saved DB/SSH connections, git identities, paired chat apps, file
|
|
23
|
+
* shares, push subscriptions, sessions) goes.
|
|
24
|
+
*
|
|
25
|
+
* The per-user columns that only existed to separate people are dropped:
|
|
26
|
+
* `users.role`, `projects.owner_user_id`, `db_client_connections.owner_user_id`,
|
|
27
|
+
* `ssh_connections.owner_user_id`, `git_identities.owner_user_id`,
|
|
28
|
+
* `git_identity_bindings.user_id`, `chat_sessions.sender_*`,
|
|
29
|
+
* `channel_links.status` (a member's chat waited for an admin), and the user key
|
|
30
|
+
* of `user_projects` / `user_unread_sessions`, which become `workspace_projects`
|
|
31
|
+
* / `unread_sessions`. Columns that record WHO authenticated (auth sessions,
|
|
32
|
+
* device codes, push subscriptions, chat-app links, audit logs, `created_by`)
|
|
33
|
+
* keep their user id — the owner still has one.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
function tableExists(db: DatabaseConnection, name: string): boolean {
|
|
37
|
+
return Boolean(db.prepare(`SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?`).get(name));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function hasColumn(db: DatabaseConnection, table: string, column: string): boolean {
|
|
41
|
+
const columns = db.prepare(`PRAGMA table_info(${table})`).all() as { name: string }[];
|
|
42
|
+
return columns.some((c) => c.name === column);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Every chat session of a project and everything hanging off it, mirroring `sessionQueries.deleteAllByProjectId`. */
|
|
46
|
+
function deleteProjectSessions(db: DatabaseConnection, projectId: string): void {
|
|
47
|
+
const inProject = 'SELECT id FROM chat_sessions WHERE project_id = ?';
|
|
48
|
+
if (tableExists(db, 'branches')) db.prepare(`DELETE FROM branches WHERE session_id IN (${inProject})`).run(projectId);
|
|
49
|
+
if (tableExists(db, 'message_snapshots')) db.prepare('DELETE FROM message_snapshots WHERE project_id = ?').run(projectId);
|
|
50
|
+
if (tableExists(db, 'session_relationships')) {
|
|
51
|
+
db.prepare(`DELETE FROM session_relationships WHERE parent_session_id IN (${inProject}) OR child_session_id IN (${inProject})`)
|
|
52
|
+
.run(projectId, projectId);
|
|
53
|
+
}
|
|
54
|
+
db.prepare(`DELETE FROM messages WHERE session_id IN (${inProject})`).run(projectId);
|
|
55
|
+
if (tableExists(db, 'messages_fts')) db.prepare('DELETE FROM messages_fts WHERE project_id = ?').run(projectId);
|
|
56
|
+
db.prepare('DELETE FROM user_unread_sessions WHERE project_id = ?').run(projectId);
|
|
57
|
+
db.prepare('UPDATE user_projects SET current_session_id = NULL WHERE project_id = ?').run(projectId);
|
|
58
|
+
db.prepare('DELETE FROM chat_sessions WHERE project_id = ?').run(projectId);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Remove a project and every row that names it. FK cascades cover most tables; the rest are swept by column. */
|
|
62
|
+
function deleteProject(db: DatabaseConnection, projectId: string): void {
|
|
63
|
+
deleteProjectSessions(db, projectId);
|
|
64
|
+
const tables = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'`).all() as { name: string }[];
|
|
65
|
+
for (const { name } of tables) {
|
|
66
|
+
if (name === 'projects' || name === 'chat_sessions') continue;
|
|
67
|
+
if (!hasColumn(db, name, 'project_id')) continue;
|
|
68
|
+
db.prepare(`DELETE FROM ${name} WHERE project_id = ?`).run(projectId);
|
|
69
|
+
}
|
|
70
|
+
db.prepare('DELETE FROM projects WHERE id = ?').run(projectId);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A Home directory is `<data dir>/home/<userId>`. Only a path of exactly that
|
|
75
|
+
* shape for the deleted user is removed, so a hand-edited row can never point
|
|
76
|
+
* this at somebody's repository.
|
|
77
|
+
*/
|
|
78
|
+
function removeHomeDirectory(path: string, userId: string): void {
|
|
79
|
+
if (basename(path) !== userId || basename(dirname(path)) !== 'home') return;
|
|
80
|
+
try {
|
|
81
|
+
rmSync(path, { recursive: true, force: true });
|
|
82
|
+
} catch (error) {
|
|
83
|
+
debug.warn('migration', `Could not remove Home directory ${path}:`, error);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function pickOwner(db: DatabaseConnection): string | null {
|
|
88
|
+
const row = db.prepare(`
|
|
89
|
+
SELECT id FROM users
|
|
90
|
+
ORDER BY CASE WHEN role = 'admin' THEN 0 ELSE 1 END, created_at ASC, id ASC
|
|
91
|
+
LIMIT 1
|
|
92
|
+
`).get() as { id: string } | undefined;
|
|
93
|
+
return row?.id ?? null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function removeOtherUsers(db: DatabaseConnection, ownerId: string): string[] {
|
|
97
|
+
const others = (db.prepare('SELECT id FROM users WHERE id != ?').all(ownerId) as { id: string }[]).map((u) => u.id);
|
|
98
|
+
if (others.length === 0) return [];
|
|
99
|
+
const homeDirs: Array<{ path: string; userId: string }> = [];
|
|
100
|
+
|
|
101
|
+
for (const userId of others) {
|
|
102
|
+
const homes = db.prepare(`SELECT id, path FROM projects WHERE kind = 'home' AND owner_user_id = ?`).all(userId) as { id: string; path: string }[];
|
|
103
|
+
for (const home of homes) {
|
|
104
|
+
deleteProject(db, home.id);
|
|
105
|
+
homeDirs.push({ path: home.path, userId });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
db.prepare('DELETE FROM db_client_connections WHERE owner_user_id = ?').run(userId);
|
|
109
|
+
db.prepare('DELETE FROM ssh_connections WHERE owner_user_id = ?').run(userId);
|
|
110
|
+
db.prepare('DELETE FROM user_projects WHERE user_id = ?').run(userId);
|
|
111
|
+
db.prepare('DELETE FROM user_unread_sessions WHERE user_id = ?').run(userId);
|
|
112
|
+
if (tableExists(db, 'push_subscriptions')) db.prepare('DELETE FROM push_subscriptions WHERE user_id = ?').run(userId);
|
|
113
|
+
// Cascades: auth_sessions, device_codes, invite_tokens, file_shares,
|
|
114
|
+
// git_identities (+ bindings), git_identity_bindings, channel_links (+ messages).
|
|
115
|
+
db.prepare('DELETE FROM users WHERE id = ?').run(userId);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Rows a member could still reach through a cascade that did not fire
|
|
119
|
+
// (an older schema without the FK) are swept explicitly.
|
|
120
|
+
db.prepare('DELETE FROM auth_sessions WHERE user_id != ?').run(ownerId);
|
|
121
|
+
db.prepare('DELETE FROM device_codes WHERE user_id != ?').run(ownerId);
|
|
122
|
+
if (tableExists(db, 'channel_links')) db.prepare('DELETE FROM channel_links WHERE user_id != ?').run(ownerId);
|
|
123
|
+
if (tableExists(db, 'file_shares')) db.prepare('DELETE FROM file_shares WHERE created_by != ?').run(ownerId);
|
|
124
|
+
|
|
125
|
+
for (const { path, userId } of homeDirs) removeHomeDirectory(path, userId);
|
|
126
|
+
return others;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function rebuildGitIdentities(db: DatabaseConnection): void {
|
|
130
|
+
db.exec(`
|
|
131
|
+
CREATE TABLE git_identities_new (
|
|
132
|
+
id TEXT PRIMARY KEY,
|
|
133
|
+
label TEXT NOT NULL UNIQUE,
|
|
134
|
+
name TEXT NOT NULL,
|
|
135
|
+
email TEXT NOT NULL,
|
|
136
|
+
auth_method TEXT NOT NULL DEFAULT 'none'
|
|
137
|
+
CHECK (auth_method IN ('none','ssh-key','https-token','https-account')),
|
|
138
|
+
ssh_private_key TEXT,
|
|
139
|
+
ssh_public_key TEXT,
|
|
140
|
+
ssh_passphrase TEXT,
|
|
141
|
+
https_username TEXT,
|
|
142
|
+
https_token TEXT,
|
|
143
|
+
integration_account_id TEXT REFERENCES integration_accounts(id) ON DELETE SET NULL,
|
|
144
|
+
hosts TEXT NOT NULL DEFAULT '[]',
|
|
145
|
+
is_default INTEGER NOT NULL DEFAULT 0,
|
|
146
|
+
source TEXT NOT NULL DEFAULT 'manual'
|
|
147
|
+
CHECK (source IN ('manual','local-machine')),
|
|
148
|
+
created_at TEXT NOT NULL,
|
|
149
|
+
updated_at TEXT NOT NULL
|
|
150
|
+
)
|
|
151
|
+
`);
|
|
152
|
+
db.exec(`
|
|
153
|
+
INSERT INTO git_identities_new (
|
|
154
|
+
id, label, name, email, auth_method, ssh_private_key, ssh_public_key, ssh_passphrase,
|
|
155
|
+
https_username, https_token, integration_account_id, hosts, is_default, source, created_at, updated_at
|
|
156
|
+
)
|
|
157
|
+
SELECT
|
|
158
|
+
id, label, name, email, auth_method, ssh_private_key, ssh_public_key, ssh_passphrase,
|
|
159
|
+
https_username, https_token, integration_account_id, hosts, is_default, source, created_at, updated_at
|
|
160
|
+
FROM git_identities
|
|
161
|
+
`);
|
|
162
|
+
|
|
163
|
+
db.exec(`
|
|
164
|
+
CREATE TABLE git_identity_bindings_new (
|
|
165
|
+
project_id TEXT PRIMARY KEY REFERENCES projects(id) ON DELETE CASCADE,
|
|
166
|
+
identity_id TEXT NOT NULL REFERENCES git_identities_new(id) ON DELETE CASCADE,
|
|
167
|
+
created_at TEXT NOT NULL,
|
|
168
|
+
updated_at TEXT NOT NULL
|
|
169
|
+
)
|
|
170
|
+
`);
|
|
171
|
+
db.exec(`
|
|
172
|
+
INSERT OR IGNORE INTO git_identity_bindings_new (project_id, identity_id, created_at, updated_at)
|
|
173
|
+
SELECT project_id, identity_id, created_at, updated_at FROM git_identity_bindings
|
|
174
|
+
`);
|
|
175
|
+
|
|
176
|
+
db.exec('DROP TABLE git_identity_bindings');
|
|
177
|
+
db.exec('DROP TABLE git_identities');
|
|
178
|
+
db.exec('ALTER TABLE git_identities_new RENAME TO git_identities');
|
|
179
|
+
db.exec('ALTER TABLE git_identity_bindings_new RENAME TO git_identity_bindings');
|
|
180
|
+
|
|
181
|
+
db.exec('CREATE INDEX idx_git_identity_bindings_identity ON git_identity_bindings (identity_id)');
|
|
182
|
+
// At most one default and one mirrored machine identity, now install-wide.
|
|
183
|
+
db.exec('CREATE UNIQUE INDEX idx_git_identities_one_default ON git_identities (is_default) WHERE is_default = 1');
|
|
184
|
+
db.exec(`CREATE UNIQUE INDEX idx_git_identities_one_local ON git_identities (source) WHERE source = 'local-machine'`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export const up = (db: DatabaseConnection): void => {
|
|
188
|
+
debug.log('migration', '📋 Collapsing collaboration into a single owner...');
|
|
189
|
+
|
|
190
|
+
db.exec('BEGIN');
|
|
191
|
+
try {
|
|
192
|
+
const ownerId = pickOwner(db);
|
|
193
|
+
if (ownerId) {
|
|
194
|
+
const removed = removeOtherUsers(db, ownerId);
|
|
195
|
+
if (removed.length > 0) debug.log('migration', `Removed ${removed.length} non-owner account(s) and their data`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Invites only ever created members.
|
|
199
|
+
db.exec('DROP TABLE IF EXISTS invite_tokens');
|
|
200
|
+
|
|
201
|
+
db.exec('ALTER TABLE users DROP COLUMN role');
|
|
202
|
+
|
|
203
|
+
// Projects in the workspace list, with their saved workspace state. A
|
|
204
|
+
// project absent from here was "removed" (kept so its chats can be
|
|
205
|
+
// restored by adding the same folder again).
|
|
206
|
+
db.exec(`
|
|
207
|
+
CREATE TABLE workspace_projects (
|
|
208
|
+
project_id TEXT PRIMARY KEY REFERENCES projects(id) ON DELETE CASCADE,
|
|
209
|
+
joined_at TEXT NOT NULL,
|
|
210
|
+
current_session_id TEXT,
|
|
211
|
+
files_panel_state TEXT,
|
|
212
|
+
workspace_state TEXT
|
|
213
|
+
)
|
|
214
|
+
`);
|
|
215
|
+
if (ownerId) {
|
|
216
|
+
db.prepare(`
|
|
217
|
+
INSERT OR IGNORE INTO workspace_projects (project_id, joined_at, current_session_id, files_panel_state, workspace_state)
|
|
218
|
+
SELECT project_id, joined_at, current_session_id, files_panel_state, workspace_state
|
|
219
|
+
FROM user_projects
|
|
220
|
+
WHERE user_id = ? AND project_id IN (SELECT id FROM projects)
|
|
221
|
+
`).run(ownerId);
|
|
222
|
+
}
|
|
223
|
+
db.exec('DROP TABLE user_projects');
|
|
224
|
+
|
|
225
|
+
db.exec(`
|
|
226
|
+
CREATE TABLE unread_sessions (
|
|
227
|
+
session_id TEXT PRIMARY KEY REFERENCES chat_sessions(id) ON DELETE CASCADE,
|
|
228
|
+
project_id TEXT NOT NULL,
|
|
229
|
+
marked_at TEXT NOT NULL
|
|
230
|
+
)
|
|
231
|
+
`);
|
|
232
|
+
db.exec('CREATE INDEX idx_unread_sessions_project ON unread_sessions(project_id)');
|
|
233
|
+
db.exec(`
|
|
234
|
+
INSERT OR IGNORE INTO unread_sessions (session_id, project_id, marked_at)
|
|
235
|
+
SELECT session_id, project_id, MAX(marked_at) FROM user_unread_sessions
|
|
236
|
+
WHERE session_id IN (SELECT id FROM chat_sessions)
|
|
237
|
+
GROUP BY session_id
|
|
238
|
+
`);
|
|
239
|
+
db.exec('DROP TABLE user_unread_sessions');
|
|
240
|
+
|
|
241
|
+
// One Home for the install.
|
|
242
|
+
db.exec('DROP INDEX IF EXISTS idx_projects_home_owner');
|
|
243
|
+
db.exec('ALTER TABLE projects DROP COLUMN owner_user_id');
|
|
244
|
+
db.exec(`CREATE UNIQUE INDEX idx_projects_single_home ON projects (kind) WHERE kind = 'home'`);
|
|
245
|
+
|
|
246
|
+
db.exec('DROP INDEX IF EXISTS idx_db_client_conn_owner');
|
|
247
|
+
db.exec('ALTER TABLE db_client_connections DROP COLUMN owner_user_id');
|
|
248
|
+
db.exec('DROP INDEX IF EXISTS idx_ssh_conn_owner');
|
|
249
|
+
db.exec('ALTER TABLE ssh_connections DROP COLUMN owner_user_id');
|
|
250
|
+
|
|
251
|
+
if (hasColumn(db, 'chat_sessions', 'sender_id')) db.exec('ALTER TABLE chat_sessions DROP COLUMN sender_id');
|
|
252
|
+
if (hasColumn(db, 'chat_sessions', 'sender_name')) db.exec('ALTER TABLE chat_sessions DROP COLUMN sender_name');
|
|
253
|
+
|
|
254
|
+
rebuildGitIdentities(db);
|
|
255
|
+
|
|
256
|
+
if (tableExists(db, 'channel_links') && hasColumn(db, 'channel_links', 'status')) {
|
|
257
|
+
// Only a member's chat could still be waiting for approval.
|
|
258
|
+
db.exec(`DELETE FROM channel_links WHERE status != 'approved'`);
|
|
259
|
+
db.exec('ALTER TABLE channel_links DROP COLUMN status');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
db.exec('COMMIT');
|
|
263
|
+
} catch (error) {
|
|
264
|
+
db.exec('ROLLBACK');
|
|
265
|
+
throw error;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
debug.log('migration', '✅ Single owner in place');
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Restores the multi-user SCHEMA so older code can boot. The removed accounts
|
|
273
|
+
* and their data are gone for good; the remaining user becomes the admin.
|
|
274
|
+
*/
|
|
275
|
+
export const down = (db: DatabaseConnection): void => {
|
|
276
|
+
debug.log('migration', '🗑️ Restoring multi-user schema...');
|
|
277
|
+
const owner = db.prepare('SELECT id FROM users ORDER BY created_at ASC LIMIT 1').get() as { id: string } | undefined;
|
|
278
|
+
|
|
279
|
+
db.exec(`ALTER TABLE users ADD COLUMN role TEXT NOT NULL DEFAULT 'admin' CHECK(role IN ('admin', 'member'))`);
|
|
280
|
+
|
|
281
|
+
db.exec(`
|
|
282
|
+
CREATE TABLE IF NOT EXISTS invite_tokens (
|
|
283
|
+
id TEXT PRIMARY KEY,
|
|
284
|
+
token_hash TEXT NOT NULL UNIQUE,
|
|
285
|
+
role TEXT NOT NULL CHECK(role IN ('admin', 'member')),
|
|
286
|
+
label TEXT,
|
|
287
|
+
created_by TEXT NOT NULL,
|
|
288
|
+
max_uses INTEGER NOT NULL DEFAULT 1,
|
|
289
|
+
use_count INTEGER NOT NULL DEFAULT 0,
|
|
290
|
+
expires_at TEXT,
|
|
291
|
+
created_at TEXT NOT NULL,
|
|
292
|
+
project_ids TEXT,
|
|
293
|
+
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
|
|
294
|
+
)
|
|
295
|
+
`);
|
|
296
|
+
|
|
297
|
+
db.exec(`
|
|
298
|
+
CREATE TABLE user_projects (
|
|
299
|
+
user_id TEXT NOT NULL,
|
|
300
|
+
project_id TEXT NOT NULL,
|
|
301
|
+
joined_at TEXT NOT NULL,
|
|
302
|
+
current_session_id TEXT,
|
|
303
|
+
files_panel_state TEXT,
|
|
304
|
+
workspace_state TEXT,
|
|
305
|
+
PRIMARY KEY (user_id, project_id),
|
|
306
|
+
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
|
|
307
|
+
)
|
|
308
|
+
`);
|
|
309
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_user_projects_user_id ON user_projects(user_id)');
|
|
310
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_user_projects_project_id ON user_projects(project_id)');
|
|
311
|
+
if (owner) {
|
|
312
|
+
db.prepare(`
|
|
313
|
+
INSERT INTO user_projects (user_id, project_id, joined_at, current_session_id, files_panel_state, workspace_state)
|
|
314
|
+
SELECT ?, project_id, joined_at, current_session_id, files_panel_state, workspace_state FROM workspace_projects
|
|
315
|
+
`).run(owner.id);
|
|
316
|
+
}
|
|
317
|
+
db.exec('DROP TABLE workspace_projects');
|
|
318
|
+
|
|
319
|
+
db.exec(`
|
|
320
|
+
CREATE TABLE user_unread_sessions (
|
|
321
|
+
user_id TEXT NOT NULL,
|
|
322
|
+
session_id TEXT NOT NULL,
|
|
323
|
+
project_id TEXT NOT NULL,
|
|
324
|
+
marked_at TEXT NOT NULL,
|
|
325
|
+
PRIMARY KEY (user_id, session_id),
|
|
326
|
+
FOREIGN KEY (session_id) REFERENCES chat_sessions(id) ON DELETE CASCADE
|
|
327
|
+
)
|
|
328
|
+
`);
|
|
329
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_user_unread_sessions_user_project ON user_unread_sessions(user_id, project_id)');
|
|
330
|
+
if (owner) {
|
|
331
|
+
db.prepare(`
|
|
332
|
+
INSERT INTO user_unread_sessions (user_id, session_id, project_id, marked_at)
|
|
333
|
+
SELECT ?, session_id, project_id, marked_at FROM unread_sessions
|
|
334
|
+
`).run(owner.id);
|
|
335
|
+
}
|
|
336
|
+
db.exec('DROP TABLE unread_sessions');
|
|
337
|
+
|
|
338
|
+
db.exec('DROP INDEX IF EXISTS idx_projects_single_home');
|
|
339
|
+
db.exec('ALTER TABLE projects ADD COLUMN owner_user_id TEXT');
|
|
340
|
+
if (owner) db.prepare(`UPDATE projects SET owner_user_id = ? WHERE kind = 'home'`).run(owner.id);
|
|
341
|
+
db.exec(`CREATE UNIQUE INDEX idx_projects_home_owner ON projects (owner_user_id) WHERE kind = 'home'`);
|
|
342
|
+
|
|
343
|
+
db.exec('ALTER TABLE db_client_connections ADD COLUMN owner_user_id TEXT');
|
|
344
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_db_client_conn_owner ON db_client_connections(owner_user_id)');
|
|
345
|
+
db.exec('ALTER TABLE ssh_connections ADD COLUMN owner_user_id TEXT');
|
|
346
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_ssh_conn_owner ON ssh_connections(owner_user_id)');
|
|
347
|
+
if (owner) {
|
|
348
|
+
db.prepare('UPDATE db_client_connections SET owner_user_id = ?').run(owner.id);
|
|
349
|
+
db.prepare('UPDATE ssh_connections SET owner_user_id = ?').run(owner.id);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
db.exec(`ALTER TABLE channel_links ADD COLUMN status TEXT NOT NULL DEFAULT 'approved' CHECK (status IN ('pending', 'approved'))`);
|
|
353
|
+
|
|
354
|
+
db.exec('ALTER TABLE chat_sessions ADD COLUMN sender_id TEXT');
|
|
355
|
+
db.exec('ALTER TABLE chat_sessions ADD COLUMN sender_name TEXT');
|
|
356
|
+
|
|
357
|
+
// Git identities need an owner; without one there is nobody to give them to.
|
|
358
|
+
db.exec(`
|
|
359
|
+
CREATE TABLE git_identities_old (
|
|
360
|
+
id TEXT PRIMARY KEY,
|
|
361
|
+
owner_user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
362
|
+
label TEXT NOT NULL,
|
|
363
|
+
name TEXT NOT NULL,
|
|
364
|
+
email TEXT NOT NULL,
|
|
365
|
+
auth_method TEXT NOT NULL DEFAULT 'none'
|
|
366
|
+
CHECK (auth_method IN ('none','ssh-key','https-token','https-account')),
|
|
367
|
+
ssh_private_key TEXT,
|
|
368
|
+
ssh_public_key TEXT,
|
|
369
|
+
ssh_passphrase TEXT,
|
|
370
|
+
https_username TEXT,
|
|
371
|
+
https_token TEXT,
|
|
372
|
+
integration_account_id TEXT REFERENCES integration_accounts(id) ON DELETE SET NULL,
|
|
373
|
+
hosts TEXT NOT NULL DEFAULT '[]',
|
|
374
|
+
is_default INTEGER NOT NULL DEFAULT 0,
|
|
375
|
+
source TEXT NOT NULL DEFAULT 'manual'
|
|
376
|
+
CHECK (source IN ('manual','local-machine')),
|
|
377
|
+
created_at TEXT NOT NULL,
|
|
378
|
+
updated_at TEXT NOT NULL,
|
|
379
|
+
UNIQUE (owner_user_id, label)
|
|
380
|
+
)
|
|
381
|
+
`);
|
|
382
|
+
db.exec(`
|
|
383
|
+
CREATE TABLE git_identity_bindings_old (
|
|
384
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
385
|
+
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
386
|
+
identity_id TEXT NOT NULL REFERENCES git_identities_old(id) ON DELETE CASCADE,
|
|
387
|
+
created_at TEXT NOT NULL,
|
|
388
|
+
updated_at TEXT NOT NULL,
|
|
389
|
+
PRIMARY KEY (project_id, user_id)
|
|
390
|
+
)
|
|
391
|
+
`);
|
|
392
|
+
if (owner) {
|
|
393
|
+
db.prepare(`
|
|
394
|
+
INSERT INTO git_identities_old (
|
|
395
|
+
id, owner_user_id, label, name, email, auth_method, ssh_private_key, ssh_public_key, ssh_passphrase,
|
|
396
|
+
https_username, https_token, integration_account_id, hosts, is_default, source, created_at, updated_at
|
|
397
|
+
)
|
|
398
|
+
SELECT
|
|
399
|
+
id, ?, label, name, email, auth_method, ssh_private_key, ssh_public_key, ssh_passphrase,
|
|
400
|
+
https_username, https_token, integration_account_id, hosts, is_default, source, created_at, updated_at
|
|
401
|
+
FROM git_identities
|
|
402
|
+
`).run(owner.id);
|
|
403
|
+
db.prepare(`
|
|
404
|
+
INSERT INTO git_identity_bindings_old (project_id, user_id, identity_id, created_at, updated_at)
|
|
405
|
+
SELECT project_id, ?, identity_id, created_at, updated_at FROM git_identity_bindings
|
|
406
|
+
`).run(owner.id);
|
|
407
|
+
}
|
|
408
|
+
db.exec('DROP TABLE git_identity_bindings');
|
|
409
|
+
db.exec('DROP TABLE git_identities');
|
|
410
|
+
db.exec('ALTER TABLE git_identities_old RENAME TO git_identities');
|
|
411
|
+
db.exec('ALTER TABLE git_identity_bindings_old RENAME TO git_identity_bindings');
|
|
412
|
+
db.exec('CREATE INDEX idx_git_identities_owner ON git_identities (owner_user_id)');
|
|
413
|
+
db.exec('CREATE UNIQUE INDEX idx_git_identities_one_default ON git_identities (owner_user_id) WHERE is_default = 1');
|
|
414
|
+
db.exec(`CREATE UNIQUE INDEX idx_git_identities_one_local ON git_identities (owner_user_id) WHERE source = 'local-machine'`);
|
|
415
|
+
db.exec('CREATE INDEX idx_git_identity_bindings_identity ON git_identity_bindings (identity_id)');
|
|
416
|
+
|
|
417
|
+
debug.log('migration', '✅ Multi-user schema restored');
|
|
418
|
+
};
|
|
@@ -82,6 +82,9 @@ import * as migration080 from './080_create_file_shares';
|
|
|
82
82
|
import * as migration081 from './081_create_push_subscriptions';
|
|
83
83
|
import * as migration082 from './082_create_git_identities';
|
|
84
84
|
import * as migration083 from './083_create_project_mcp_trust';
|
|
85
|
+
import * as migration084 from './084_create_channel_links';
|
|
86
|
+
import * as migration085 from './085_add_home_projects';
|
|
87
|
+
import * as migration086 from './086_single_owner';
|
|
85
88
|
|
|
86
89
|
// Export all migrations in order
|
|
87
90
|
export const migrations = [
|
|
@@ -582,6 +585,24 @@ export const migrations = [
|
|
|
582
585
|
description: migration083.description,
|
|
583
586
|
up: migration083.up,
|
|
584
587
|
down: migration083.down
|
|
588
|
+
},
|
|
589
|
+
{
|
|
590
|
+
id: '084',
|
|
591
|
+
description: migration084.description,
|
|
592
|
+
up: migration084.up,
|
|
593
|
+
down: migration084.down
|
|
594
|
+
},
|
|
595
|
+
{
|
|
596
|
+
id: '085',
|
|
597
|
+
description: migration085.description,
|
|
598
|
+
up: migration085.up,
|
|
599
|
+
down: migration085.down
|
|
600
|
+
},
|
|
601
|
+
{
|
|
602
|
+
id: '086',
|
|
603
|
+
description: migration086.description,
|
|
604
|
+
up: migration086.up,
|
|
605
|
+
down: migration086.down
|
|
585
606
|
}
|
|
586
607
|
];
|
|
587
608
|
|