@myrialabs/clopen 0.4.37 → 0.4.39
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 +2 -2
- package/backend/auth/permissions.ts +39 -0
- package/backend/bootstrap.ts +16 -0
- package/backend/chat/stream-manager.ts +1 -1
- package/backend/database/crypto/envelope.test.ts +93 -0
- package/backend/database/crypto/envelope.ts +127 -0
- package/backend/database/crypto/index.ts +22 -0
- package/backend/database/crypto/master-key.ts +129 -0
- package/backend/database/crypto/secret-columns.ts +151 -0
- package/backend/database/migrations/071_create_notes_tables.ts +90 -0
- package/backend/database/migrations/072_encrypt_stored_secrets.ts +107 -0
- package/backend/database/migrations/073_create_integration_tables.ts +119 -0
- package/backend/database/migrations/074_create_work_tables.ts +94 -0
- package/backend/database/migrations/075_create_deploy_bindings.ts +54 -0
- package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
- package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
- package/backend/database/migrations/index.ts +49 -0
- package/backend/database/queries/db-client-connection-queries.ts +42 -12
- package/backend/database/queries/deploy-queries.ts +124 -0
- package/backend/database/queries/engine-queries.ts +23 -10
- package/backend/database/queries/graph-queries.ts +291 -247
- package/backend/database/queries/index.ts +10 -1
- package/backend/database/queries/integration-db-link-queries.ts +168 -0
- package/backend/database/queries/integration-queries.ts +325 -0
- package/backend/database/queries/mcp-server-queries.ts +38 -11
- package/backend/database/queries/note-queries.test.ts +190 -0
- package/backend/database/queries/note-queries.ts +269 -0
- package/backend/database/queries/profile-queries.ts +1 -1
- package/backend/database/queries/ssh-connection-queries.ts +19 -8
- package/backend/database/queries/work-queries.ts +210 -0
- package/backend/db-client/integrations/index.ts +37 -0
- package/backend/db-client/integrations/links.ts +533 -0
- package/backend/db-client/integrations/projector.test.ts +280 -0
- package/backend/db-client/integrations/projector.ts +256 -0
- package/backend/db-client/integrations/supabase-service.ts +266 -0
- package/backend/db-client/providers/registry.ts +27 -0
- package/backend/db-client/providers/supabase/adapter.ts +500 -0
- package/backend/db-client/providers/supabase/api-types.ts +92 -0
- package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
- package/backend/db-client/providers/supabase/client.ts +259 -0
- package/backend/db-client/providers/supabase/features.ts +502 -0
- package/backend/db-client/providers/supabase/local.test.ts +78 -0
- package/backend/db-client/providers/supabase/local.ts +137 -0
- package/backend/db-client/providers/types.ts +137 -0
- package/backend/deployments/bindings.test.ts +43 -0
- package/backend/deployments/bindings.ts +216 -0
- package/backend/deployments/index.ts +53 -0
- package/backend/deployments/log-streams.ts +214 -0
- package/backend/deployments/prompts.ts +70 -0
- package/backend/deployments/providers/vercel/adapter.ts +757 -0
- package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
- package/backend/deployments/providers/vercel/build-logs.ts +211 -0
- package/backend/deployments/providers/vercel/client.ts +203 -0
- package/backend/deployments/providers/vercel/list.test.ts +624 -0
- package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
- package/backend/deployments/providers/vercel/normalise.ts +113 -0
- package/backend/deployments/providers/vercel/types.ts +104 -0
- package/backend/deployments/registry.ts +27 -0
- package/backend/deployments/service.ts +458 -0
- package/backend/deployments/types.ts +278 -0
- package/backend/engine/docs/artifacts.md +22 -9
- package/backend/files/os-clipboard.test.ts +209 -0
- package/backend/files/os-clipboard.ts +567 -0
- package/backend/http/integration-hooks.ts +42 -0
- package/backend/http/notes-images.test.ts +228 -0
- package/backend/http/notes-images.ts +196 -0
- package/backend/index.ts +13 -0
- package/backend/integrations/accounts.ts +229 -0
- package/backend/integrations/health.ts +146 -0
- package/backend/integrations/index.ts +36 -0
- package/backend/integrations/projections/index.ts +0 -0
- package/backend/integrations/projections/mcp-projector.test.ts +204 -0
- package/backend/integrations/projections/mcp-projector.ts +163 -0
- package/backend/integrations/projections/types.ts +63 -0
- package/backend/integrations/registry/define.ts +38 -0
- package/backend/integrations/registry/index.ts +60 -0
- package/backend/integrations/registry/providers/context7.ts +44 -0
- package/backend/integrations/registry/providers/firecrawl.ts +41 -0
- package/backend/integrations/registry/providers/github.ts +78 -0
- package/backend/integrations/registry/providers/supabase.ts +61 -0
- package/backend/integrations/registry/providers/vercel.ts +46 -0
- package/backend/integrations/registry/types.ts +96 -0
- package/backend/integrations/webhooks/gateway.ts +160 -0
- package/backend/integrations/webhooks/verify.ts +65 -0
- package/backend/mcp/README.md +25 -3
- package/backend/mcp/external/config.ts +35 -1
- package/backend/mcp/index.ts +1 -1
- package/backend/mcp/internal/config.ts +1 -1
- package/backend/mcp/internal/servers/memory-graph/description.ts +3 -3
- package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
- package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
- package/backend/memory/README.md +57 -34
- package/backend/memory/compose.test.ts +3 -3
- package/backend/memory/compose.ts +6 -9
- package/backend/memory/config.ts +0 -5
- package/backend/memory/consolidate.ts +1 -2
- package/backend/memory/context.test.ts +8 -54
- package/backend/memory/context.ts +19 -25
- package/backend/memory/extract/episodic.ts +10 -12
- package/backend/memory/extract/index.ts +23 -52
- package/backend/memory/extract/scheduler.ts +3 -6
- package/backend/memory/index.ts +6 -5
- package/backend/memory/invalidate.ts +36 -48
- package/backend/memory/layout.ts +319 -130
- package/backend/memory/retention.ts +12 -42
- package/backend/memory/retrieval.eval.test.ts +2 -2
- package/backend/memory/retrieval.test.ts +228 -183
- package/backend/memory/retrieval.ts +22 -21
- package/backend/memory/revise.test.ts +62 -116
- package/backend/memory/revise.ts +0 -1
- package/backend/memory/view.ts +8 -5
- package/backend/notes/access.test.ts +82 -0
- package/backend/notes/access.ts +27 -0
- package/backend/notes/storage.test.ts +77 -0
- package/backend/notes/storage.ts +114 -0
- package/backend/permissions/service.ts +2 -2
- package/backend/profiles/service.ts +1 -1
- package/backend/projects/project-cleanup.ts +6 -0
- package/backend/snapshot/helpers.ts +35 -41
- package/backend/snapshot/snapshot-service.ts +90 -0
- package/backend/snapshot/turn-changes.test.ts +163 -0
- package/backend/snapshot/turn-changes.ts +131 -0
- package/backend/work/bindings.ts +124 -0
- package/backend/work/index.ts +42 -0
- package/backend/work/prompts.ts +150 -0
- package/backend/work/providers/github/actions-logs.ts +180 -0
- package/backend/work/providers/github/adapter.test.ts +88 -0
- package/backend/work/providers/github/adapter.ts +1255 -0
- package/backend/work/providers/github/client.ts +280 -0
- package/backend/work/providers/github/list-range.test.ts +158 -0
- package/backend/work/providers/github/token-kind.test.ts +42 -0
- package/backend/work/pull-requests.ts +242 -0
- package/backend/work/registry.ts +27 -0
- package/backend/work/service.ts +436 -0
- package/backend/work/start-work.test.ts +71 -0
- package/backend/work/start-work.ts +232 -0
- package/backend/work/types.ts +180 -0
- package/backend/ws/db-client/accounts.ts +225 -0
- package/backend/ws/db-client/connections.ts +34 -3
- package/backend/ws/db-client/context.ts +37 -0
- package/backend/ws/db-client/index.ts +20 -1
- package/backend/ws/db-client/supabase.ts +188 -0
- package/backend/ws/deployments/actions.ts +360 -0
- package/backend/ws/deployments/context.ts +43 -0
- package/backend/ws/deployments/crud.ts +152 -0
- package/backend/ws/deployments/index.ts +15 -0
- package/backend/ws/deployments/schemas.ts +183 -0
- package/backend/ws/files/clipboard.ts +78 -0
- package/backend/ws/files/index.ts +5 -0
- package/backend/ws/git/diff-budget.ts +70 -11
- package/backend/ws/index.ts +16 -2
- package/backend/ws/integrations/crud.ts +219 -0
- package/backend/ws/integrations/index.ts +22 -0
- package/backend/ws/mcp/crud.ts +25 -1
- package/backend/ws/memory/crud.ts +4 -16
- package/backend/ws/notes/index.ts +278 -0
- package/backend/ws/profiles/crud.ts +1 -1
- package/backend/ws/snapshot/changes.ts +279 -0
- package/backend/ws/snapshot/index.ts +4 -3
- package/backend/ws/work/actions.ts +206 -0
- package/backend/ws/work/context.ts +51 -0
- package/backend/ws/work/crud.ts +347 -0
- package/backend/ws/work/index.ts +17 -0
- package/backend/ws/work/pull-requests.ts +177 -0
- package/backend/ws/work/schemas.ts +226 -0
- package/dist/assets/{index-sJmS_CCm.js → index-BrF6nqld.js} +1 -1
- package/dist/assets/{index-Bik9TCBi.js → index-C8Gi-b6U.js} +1 -1
- package/dist/assets/main-Bd3gb79F.css +1 -0
- package/dist/assets/main-Bo1hD7mt.js +1191 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/constants/engine-tools.ts +1 -1
- package/shared/constants/tool-icons.ts +42 -0
- package/shared/types/database/schema.ts +41 -0
- package/shared/types/db-client/connection.ts +6 -0
- package/shared/types/db-client/index.ts +2 -0
- package/shared/types/db-client/integrations.ts +174 -0
- package/shared/types/db-client/supabase.ts +131 -0
- package/shared/types/deployments.ts +382 -0
- package/shared/types/integrations.ts +140 -0
- package/shared/types/memory.ts +34 -51
- package/shared/types/stores/settings.ts +8 -0
- package/shared/types/ui/components.ts +1 -1
- package/shared/types/work.ts +426 -0
- package/shared/utils/diff-calculator.test.ts +89 -0
- package/shared/utils/diff-calculator.ts +30 -65
- package/shared/utils/logger.ts +4 -0
- package/backend/memory/extract/languages.ts +0 -176
- package/backend/memory/extract/structural.test.ts +0 -215
- package/backend/memory/extract/structural.ts +0 -364
- package/backend/ws/snapshot/content.ts +0 -85
- package/dist/assets/main-DLqolBI2.css +0 -1
- package/dist/assets/main-Df1I3hnw.js +0 -1096
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the database projection.
|
|
3
|
+
*
|
|
4
|
+
* The cases here are the ones that destroy something when they are wrong, which
|
|
5
|
+
* is the same reason `mcp-projector.test.ts` covers what it covers:
|
|
6
|
+
*
|
|
7
|
+
* - ONE account must be able to own SEVERAL connections. That is the whole
|
|
8
|
+
* point of widening the projection key in migration 077, and a regression
|
|
9
|
+
* would silently drop every linked database but one.
|
|
10
|
+
* - Removing one link must release exactly that one. Releasing by capability
|
|
11
|
+
* would tear down the survivors and rebuild them, which for an adopted row
|
|
12
|
+
* means handing it back and re-adopting it — snapshotting our own credential
|
|
13
|
+
* in the process.
|
|
14
|
+
* - Adopting a hand-typed connection must produce ONE row, and disconnecting
|
|
15
|
+
* must hand it back with the password its user had on it, not with ours.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'bun:test';
|
|
19
|
+
|
|
20
|
+
import { initializeDatabase, closeDatabase } from '$backend/database';
|
|
21
|
+
import {
|
|
22
|
+
dbClientConnectionQueries,
|
|
23
|
+
integrationAccountQueries,
|
|
24
|
+
integrationDbLinkQueries,
|
|
25
|
+
integrationProjectionQueries
|
|
26
|
+
} from '$backend/database/queries';
|
|
27
|
+
import { integrationAccounts } from '$backend/integrations/accounts';
|
|
28
|
+
import { reproject } from '$backend/integrations';
|
|
29
|
+
import '../integrations';
|
|
30
|
+
|
|
31
|
+
const POOLER_HOST = 'aws-0-eu-west-2.pooler.supabase.com';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Create a link the way the link service does, minus the network call.
|
|
35
|
+
*
|
|
36
|
+
* `resolveEndpoint` is what talks to Supabase; storing its result on the link
|
|
37
|
+
* is what lets the projector stay synchronous, so a test can supply that result
|
|
38
|
+
* directly and exercise everything downstream of it.
|
|
39
|
+
*/
|
|
40
|
+
function linkTo(accountId: string, ref: string, password: string, label?: string) {
|
|
41
|
+
return integrationDbLinkQueries.create({
|
|
42
|
+
accountId,
|
|
43
|
+
remoteRef: ref,
|
|
44
|
+
label: label ?? ref,
|
|
45
|
+
driver: 'postgres',
|
|
46
|
+
mode: 'pooler-session',
|
|
47
|
+
secrets: { password },
|
|
48
|
+
config: {
|
|
49
|
+
endpoint: {
|
|
50
|
+
host: POOLER_HOST,
|
|
51
|
+
port: 5432,
|
|
52
|
+
username: `postgres.${ref}`,
|
|
53
|
+
database: 'postgres',
|
|
54
|
+
sslMode: 'require',
|
|
55
|
+
options: { supabase: { ref, mode: 'pooler-session' } }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function connect(label?: string) {
|
|
62
|
+
return integrationAccounts.connect({
|
|
63
|
+
provider: 'supabase',
|
|
64
|
+
label,
|
|
65
|
+
credentials: { accessToken: 'sbp_test' }
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function clearFixtures(): void {
|
|
70
|
+
for (const account of integrationAccountQueries.getAll()) {
|
|
71
|
+
integrationAccountQueries.remove(account.id);
|
|
72
|
+
}
|
|
73
|
+
for (const connection of dbClientConnectionQueries.list()) {
|
|
74
|
+
dbClientConnectionQueries.delete(connection.id);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
beforeAll(async () => {
|
|
79
|
+
await initializeDatabase();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
beforeEach(() => {
|
|
83
|
+
clearFixtures();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
afterAll(() => {
|
|
87
|
+
clearFixtures();
|
|
88
|
+
closeDatabase();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('database projection', () => {
|
|
92
|
+
it('projects one connection per linked database from a single account', () => {
|
|
93
|
+
const account = connect();
|
|
94
|
+
linkTo(account.id, 'prodprojectref00000', 'prod-pw', 'Production');
|
|
95
|
+
linkTo(account.id, 'stageprojectref0000', 'stage-pw', 'Staging');
|
|
96
|
+
reproject(account.id);
|
|
97
|
+
|
|
98
|
+
const connections = dbClientConnectionQueries.list();
|
|
99
|
+
expect(connections).toHaveLength(2);
|
|
100
|
+
expect(connections.map((entry) => entry.name).sort()).toEqual(['Production', 'Staging']);
|
|
101
|
+
|
|
102
|
+
// The narrow key this replaced could only hold one of these.
|
|
103
|
+
const projections = integrationProjectionQueries.getForAccount(account.id);
|
|
104
|
+
expect(projections).toHaveLength(2);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('carries the endpoint and the password onto the row', () => {
|
|
108
|
+
const account = connect();
|
|
109
|
+
linkTo(account.id, 'prodprojectref00000', 'prod-pw');
|
|
110
|
+
reproject(account.id);
|
|
111
|
+
|
|
112
|
+
const connection = dbClientConnectionQueries.list()[0];
|
|
113
|
+
expect(connection.host).toBe(POOLER_HOST);
|
|
114
|
+
expect(connection.port).toBe(5432);
|
|
115
|
+
expect(connection.username).toBe('postgres.prodprojectref00000');
|
|
116
|
+
expect(connection.sslMode).toBe('require');
|
|
117
|
+
expect(connection.password).toBe('prod-pw');
|
|
118
|
+
// The marker is what lets DB Client recognise a Supabase connection
|
|
119
|
+
// without learning that accounts exist.
|
|
120
|
+
expect((connection.options.supabase as { ref: string }).ref).toBe('prodprojectref00000');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('releases only the link that was removed', () => {
|
|
124
|
+
const account = connect();
|
|
125
|
+
const keep = linkTo(account.id, 'keepprojectref00000', 'keep-pw', 'Keep');
|
|
126
|
+
const drop = linkTo(account.id, 'dropprojectref00000', 'drop-pw', 'Drop');
|
|
127
|
+
reproject(account.id);
|
|
128
|
+
expect(dbClientConnectionQueries.list()).toHaveLength(2);
|
|
129
|
+
|
|
130
|
+
const keptId = integrationDbLinkQueries.configOf<{ connectionId?: string }>(
|
|
131
|
+
integrationDbLinkQueries.getById(keep.id)!
|
|
132
|
+
).connectionId;
|
|
133
|
+
|
|
134
|
+
integrationDbLinkQueries.remove(drop.id);
|
|
135
|
+
reproject(account.id);
|
|
136
|
+
|
|
137
|
+
const remaining = dbClientConnectionQueries.list();
|
|
138
|
+
expect(remaining).toHaveLength(1);
|
|
139
|
+
expect(remaining[0].name).toBe('Keep');
|
|
140
|
+
// The survivor keeps its identity: a rebuild would give it a new id and
|
|
141
|
+
// orphan every saved view and query-history row keyed to the old one.
|
|
142
|
+
expect(remaining[0].id).toBe(keptId!);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('rewrites the password when the link rotates it', () => {
|
|
146
|
+
const account = connect();
|
|
147
|
+
const link = linkTo(account.id, 'prodprojectref00000', 'old-pw');
|
|
148
|
+
reproject(account.id);
|
|
149
|
+
|
|
150
|
+
integrationDbLinkQueries.update(link.id, { secrets: { password: 'new-pw' } });
|
|
151
|
+
reproject(account.id);
|
|
152
|
+
|
|
153
|
+
expect(dbClientConnectionQueries.list()[0].password).toBe('new-pw');
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('adopts a hand-typed connection instead of creating a sibling', () => {
|
|
157
|
+
const mine = dbClientConnectionQueries.create({
|
|
158
|
+
name: 'My Supabase',
|
|
159
|
+
driver: 'postgres',
|
|
160
|
+
host: POOLER_HOST,
|
|
161
|
+
port: 5432,
|
|
162
|
+
username: 'postgres.prodprojectref00000',
|
|
163
|
+
password: 'typed-by-hand',
|
|
164
|
+
database: 'postgres',
|
|
165
|
+
sslMode: 'require'
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const account = connect();
|
|
169
|
+
linkTo(account.id, 'prodprojectref00000', 'from-account', 'Production');
|
|
170
|
+
reproject(account.id);
|
|
171
|
+
|
|
172
|
+
const connections = dbClientConnectionQueries.list();
|
|
173
|
+
expect(connections).toHaveLength(1);
|
|
174
|
+
expect(connections[0].id).toBe(mine.id);
|
|
175
|
+
// An adopted row keeps the name its user gave it.
|
|
176
|
+
expect(connections[0].name).toBe('My Supabase');
|
|
177
|
+
expect(connections[0].password).toBe('from-account');
|
|
178
|
+
|
|
179
|
+
const projection = integrationProjectionQueries.getByTarget('db_client_connection', mine.id);
|
|
180
|
+
expect(projection?.adopted).toBe(1);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('hands an adopted connection back with the password it arrived with', () => {
|
|
184
|
+
const mine = dbClientConnectionQueries.create({
|
|
185
|
+
name: 'My Supabase',
|
|
186
|
+
driver: 'postgres',
|
|
187
|
+
host: POOLER_HOST,
|
|
188
|
+
port: 5432,
|
|
189
|
+
username: 'postgres.prodprojectref00000',
|
|
190
|
+
password: 'typed-by-hand',
|
|
191
|
+
database: 'postgres',
|
|
192
|
+
sslMode: 'require'
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const account = connect();
|
|
196
|
+
linkTo(account.id, 'prodprojectref00000', 'from-account');
|
|
197
|
+
reproject(account.id);
|
|
198
|
+
integrationAccounts.disconnect(account.id);
|
|
199
|
+
|
|
200
|
+
const survivor = dbClientConnectionQueries.get(mine.id);
|
|
201
|
+
expect(survivor).not.toBe(null);
|
|
202
|
+
// Not ours, and not empty. Leaving our password behind would let a
|
|
203
|
+
// disconnected account keep working; clearing it would break a connection
|
|
204
|
+
// the user set up themselves.
|
|
205
|
+
expect(survivor!.password).toBe('typed-by-hand');
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('deletes a connection it created when the account is disconnected', () => {
|
|
209
|
+
const account = connect();
|
|
210
|
+
linkTo(account.id, 'prodprojectref00000', 'prod-pw');
|
|
211
|
+
reproject(account.id);
|
|
212
|
+
expect(dbClientConnectionQueries.list()).toHaveLength(1);
|
|
213
|
+
|
|
214
|
+
integrationAccounts.disconnect(account.id);
|
|
215
|
+
expect(dbClientConnectionQueries.list()).toHaveLength(0);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('projects nothing for a link whose endpoint is not resolved yet, and keeps its secret', () => {
|
|
219
|
+
const account = connect();
|
|
220
|
+
// The shape `createDatabase` writes: the secret is durable before the
|
|
221
|
+
// database is answering, because a generated password that is lost while
|
|
222
|
+
// provisioning is one only a reset can recover.
|
|
223
|
+
const link = integrationDbLinkQueries.create({
|
|
224
|
+
accountId: account.id,
|
|
225
|
+
remoteRef: 'freshprojectref0000',
|
|
226
|
+
label: 'Fresh',
|
|
227
|
+
driver: 'postgres',
|
|
228
|
+
mode: 'pooler-session',
|
|
229
|
+
secrets: { password: 'generated-pw' },
|
|
230
|
+
config: {}
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
reproject(account.id);
|
|
234
|
+
expect(dbClientConnectionQueries.list()).toHaveLength(0);
|
|
235
|
+
expect(integrationDbLinkQueries.secretsOf(integrationDbLinkQueries.getById(link.id)!).password)
|
|
236
|
+
.toBe('generated-pw');
|
|
237
|
+
|
|
238
|
+
// Once the endpoint lands the same link projects normally, with the
|
|
239
|
+
// password it has been holding all along.
|
|
240
|
+
integrationDbLinkQueries.update(link.id, {
|
|
241
|
+
config: {
|
|
242
|
+
endpoint: {
|
|
243
|
+
host: POOLER_HOST,
|
|
244
|
+
port: 5432,
|
|
245
|
+
username: 'postgres.freshprojectref0000',
|
|
246
|
+
database: 'postgres',
|
|
247
|
+
sslMode: 'require',
|
|
248
|
+
options: { supabase: { ref: 'freshprojectref0000', mode: 'pooler-session' } }
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
reproject(account.id);
|
|
253
|
+
|
|
254
|
+
const connections = dbClientConnectionQueries.list();
|
|
255
|
+
expect(connections).toHaveLength(1);
|
|
256
|
+
expect(connections[0].password).toBe('generated-pw');
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('re-adopting does not re-snapshot our own credential', () => {
|
|
260
|
+
const mine = dbClientConnectionQueries.create({
|
|
261
|
+
name: 'My Supabase',
|
|
262
|
+
driver: 'postgres',
|
|
263
|
+
host: POOLER_HOST,
|
|
264
|
+
port: 5432,
|
|
265
|
+
username: 'postgres.prodprojectref00000',
|
|
266
|
+
password: 'typed-by-hand',
|
|
267
|
+
database: 'postgres',
|
|
268
|
+
sslMode: 'require'
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const account = connect();
|
|
272
|
+
linkTo(account.id, 'prodprojectref00000', 'from-account');
|
|
273
|
+
reproject(account.id);
|
|
274
|
+
// A second pass — a capability toggle, a rename, any account mutation.
|
|
275
|
+
reproject(account.id);
|
|
276
|
+
integrationAccounts.disconnect(account.id);
|
|
277
|
+
|
|
278
|
+
expect(dbClientConnectionQueries.get(mine.id)!.password).toBe('typed-by-hand');
|
|
279
|
+
});
|
|
280
|
+
});
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `database` projector: an account owns one `db_client_connections` row per
|
|
3
|
+
* linked remote database.
|
|
4
|
+
*
|
|
5
|
+
* Nothing downstream knows accounts exist. The drivers, the schema tree, the
|
|
6
|
+
* query console and the connection manager read the row exactly as they read a
|
|
7
|
+
* hand-typed one — which is the point of keeping this layer off the path every
|
|
8
|
+
* connection already flows through.
|
|
9
|
+
*
|
|
10
|
+
* The projector is SYNCHRONOUS, so it never talks to a provider. Where a
|
|
11
|
+
* database answers was resolved when the link was created (see `links.ts`) and
|
|
12
|
+
* stored on the link; here it is only copied onto a row. That keeps a token
|
|
13
|
+
* rotation from waiting on a third-party API before the account can be saved.
|
|
14
|
+
*
|
|
15
|
+
* A projected row is created with NO OWNER, which in db-client's access model
|
|
16
|
+
* means admins see it and members do not. That matches who can create it:
|
|
17
|
+
* `integrations:*` is admin-gated, because connecting an account changes what
|
|
18
|
+
* the whole install can reach.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
dbClientConnectionQueries,
|
|
23
|
+
integrationDbLinkQueries,
|
|
24
|
+
integrationProjectionQueries,
|
|
25
|
+
type IntegrationDbLinkRow
|
|
26
|
+
} from '$backend/database/queries';
|
|
27
|
+
import type { DbClientConnection, DbSslMode } from '$shared/types/db-client';
|
|
28
|
+
import type { Projector, ProjectionContext, ProjectionResult } from '$backend/integrations';
|
|
29
|
+
import type { DbProviderEndpoint } from '../providers/types';
|
|
30
|
+
import { debug } from '$shared/utils/logger';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* What an adopted connection looked like before we took it over.
|
|
34
|
+
*
|
|
35
|
+
* Only the fields a projection rewrites are snapshotted. The row's name and
|
|
36
|
+
* colour are never touched on an adopted row, so putting them back is not this
|
|
37
|
+
* snapshot's job.
|
|
38
|
+
*/
|
|
39
|
+
interface DbRestoreSnapshot {
|
|
40
|
+
host: string | null;
|
|
41
|
+
port: number | null;
|
|
42
|
+
username: string | null;
|
|
43
|
+
password: string | null;
|
|
44
|
+
database: string | null;
|
|
45
|
+
sslMode: DbSslMode;
|
|
46
|
+
options: Record<string, unknown>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface LinkConfig {
|
|
50
|
+
endpoint?: DbProviderEndpoint;
|
|
51
|
+
connectionId?: string;
|
|
52
|
+
[key: string]: unknown;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The endpoint stored on a link, or null when it was never resolved. */
|
|
56
|
+
export function endpointOf(link: IntegrationDbLinkRow): DbProviderEndpoint | null {
|
|
57
|
+
const endpoint = integrationDbLinkQueries.configOf<LinkConfig>(link).endpoint;
|
|
58
|
+
if (!endpoint || typeof endpoint.host !== 'string' || !endpoint.host) return null;
|
|
59
|
+
return endpoint;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The connection a link currently owns, if it has been projected before. */
|
|
63
|
+
export function connectionIdOf(link: IntegrationDbLinkRow): string | null {
|
|
64
|
+
return integrationDbLinkQueries.configOf<LinkConfig>(link).connectionId ?? null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function rememberConnection(link: IntegrationDbLinkRow, connectionId: string): void {
|
|
68
|
+
const config = integrationDbLinkQueries.configOf<LinkConfig>(link);
|
|
69
|
+
if (config.connectionId === connectionId) return;
|
|
70
|
+
integrationDbLinkQueries.update(link.id, { config: { ...config, connectionId } });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function snapshotOf(connection: DbClientConnection): DbRestoreSnapshot {
|
|
74
|
+
return {
|
|
75
|
+
host: connection.host,
|
|
76
|
+
port: connection.port,
|
|
77
|
+
username: connection.username,
|
|
78
|
+
password: connection.password,
|
|
79
|
+
database: connection.database,
|
|
80
|
+
sslMode: connection.sslMode,
|
|
81
|
+
options: connection.options
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* A hand-typed connection pointing at the same place, if there is one.
|
|
87
|
+
*
|
|
88
|
+
* The database counterpart of adopting an MCP server by slug: a user who typed
|
|
89
|
+
* this connection in before connecting the account must end up with ONE entry,
|
|
90
|
+
* not two rows racing each other's `last_used_at`.
|
|
91
|
+
*
|
|
92
|
+
* Identity INCLUDES THE USERNAME, and that is not a detail. Every Supabase
|
|
93
|
+
* project in one region answers on the same pooler host, the same port and a
|
|
94
|
+
* database called `postgres` — the username (`postgres.<ref>`) is the only part
|
|
95
|
+
* of the endpoint that says which project. Matching without it made a second
|
|
96
|
+
* linked project adopt the first one's row, so an account with production and
|
|
97
|
+
* staging linked projected exactly one connection.
|
|
98
|
+
*
|
|
99
|
+
* Two rows are never adopted:
|
|
100
|
+
* - one owned by ANOTHER account, which would let two accounts fight over it,
|
|
101
|
+
* each release handing back the other's credential;
|
|
102
|
+
* - one already claimed by another link of THIS account, which is the same
|
|
103
|
+
* collision from the inside.
|
|
104
|
+
*/
|
|
105
|
+
function findAdoptable(
|
|
106
|
+
link: IntegrationDbLinkRow,
|
|
107
|
+
endpoint: DbProviderEndpoint,
|
|
108
|
+
accountId: string,
|
|
109
|
+
claimed: Set<string>
|
|
110
|
+
): DbClientConnection | null {
|
|
111
|
+
for (const connection of dbClientConnectionQueries.list()) {
|
|
112
|
+
if (claimed.has(connection.id)) continue;
|
|
113
|
+
if (connection.driver !== link.driver) continue;
|
|
114
|
+
if ((connection.host ?? '') !== endpoint.host) continue;
|
|
115
|
+
if ((connection.port ?? 0) !== endpoint.port) continue;
|
|
116
|
+
if ((connection.database ?? '') !== endpoint.database) continue;
|
|
117
|
+
if ((connection.username ?? '') !== endpoint.username) continue;
|
|
118
|
+
|
|
119
|
+
const owner = integrationProjectionQueries.getByTarget('db_client_connection', connection.id);
|
|
120
|
+
if (owner && owner.account_id !== accountId) continue;
|
|
121
|
+
return connection;
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The connection fields a link derives, minus the password (written separately). */
|
|
127
|
+
function fieldsFor(link: IntegrationDbLinkRow, endpoint: DbProviderEndpoint) {
|
|
128
|
+
return {
|
|
129
|
+
driver: link.driver,
|
|
130
|
+
host: endpoint.host,
|
|
131
|
+
port: endpoint.port,
|
|
132
|
+
username: endpoint.username,
|
|
133
|
+
database: endpoint.database,
|
|
134
|
+
sslMode: endpoint.sslMode,
|
|
135
|
+
options: { ...(endpoint.options ?? {}) }
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Write the derived state onto one row.
|
|
141
|
+
*
|
|
142
|
+
* The password goes through `replacePassword` rather than the patch, because a
|
|
143
|
+
* patch treats an empty password as "keep what is there" — right for a form,
|
|
144
|
+
* wrong for a derived row, which must equal what it was derived from even when
|
|
145
|
+
* that is nothing.
|
|
146
|
+
*/
|
|
147
|
+
function writeRow(
|
|
148
|
+
connectionId: string,
|
|
149
|
+
link: IntegrationDbLinkRow,
|
|
150
|
+
endpoint: DbProviderEndpoint,
|
|
151
|
+
options: { rename: boolean }
|
|
152
|
+
): void {
|
|
153
|
+
const patch = fieldsFor(link, endpoint);
|
|
154
|
+
dbClientConnectionQueries.update(connectionId, options.rename ? { name: link.label, ...patch } : patch);
|
|
155
|
+
dbClientConnectionQueries.replacePassword(connectionId, integrationDbLinkQueries.secretsOf(link).password ?? null);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export const dbConnectionProjector: Projector = {
|
|
159
|
+
capability: 'database',
|
|
160
|
+
targetKind: 'db_client_connection',
|
|
161
|
+
|
|
162
|
+
project(context: ProjectionContext): ProjectionResult[] {
|
|
163
|
+
const links = integrationDbLinkQueries.getForAccount(context.account.id);
|
|
164
|
+
const results: ProjectionResult[] = [];
|
|
165
|
+
// Rows this pass has already handed to a link. Two links of one account
|
|
166
|
+
// must never land on the same connection.
|
|
167
|
+
const claimed = new Set<string>(
|
|
168
|
+
links.map((entry) => connectionIdOf(entry)).filter((id): id is string => id !== null)
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
for (const link of links) {
|
|
172
|
+
const endpoint = endpointOf(link);
|
|
173
|
+
if (!endpoint) {
|
|
174
|
+
// A link whose endpoint was never resolved projects nothing. The link
|
|
175
|
+
// dialog resolves before it saves, so this is only reachable for a
|
|
176
|
+
// row written by a provider that resolves lazily — and skipping it
|
|
177
|
+
// beats writing a connection that points nowhere.
|
|
178
|
+
debug.warn('db-client', `Link ${link.id} has no resolved endpoint — nothing to project`);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const ownedId = connectionIdOf(link);
|
|
183
|
+
const owned = ownedId ? dbClientConnectionQueries.get(ownedId) : null;
|
|
184
|
+
const target = owned ?? findAdoptable(link, endpoint, context.account.id, claimed);
|
|
185
|
+
|
|
186
|
+
if (!target) {
|
|
187
|
+
const created = dbClientConnectionQueries.create({
|
|
188
|
+
name: link.label,
|
|
189
|
+
password: integrationDbLinkQueries.secretsOf(link).password ?? '',
|
|
190
|
+
...fieldsFor(link, endpoint)
|
|
191
|
+
});
|
|
192
|
+
rememberConnection(link, created.id);
|
|
193
|
+
claimed.add(created.id);
|
|
194
|
+
results.push({
|
|
195
|
+
targetKind: 'db_client_connection',
|
|
196
|
+
targetId: created.id,
|
|
197
|
+
adopted: false,
|
|
198
|
+
restore: null
|
|
199
|
+
});
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const prior = integrationProjectionQueries.getByTarget('db_client_connection', target.id);
|
|
204
|
+
// Re-projecting a row we already own must not re-snapshot it: the second
|
|
205
|
+
// snapshot would capture OUR credential, and release would then hand the
|
|
206
|
+
// user back the password we are about to delete.
|
|
207
|
+
const adopted = prior ? prior.adopted === 1 : !owned;
|
|
208
|
+
const restore = prior
|
|
209
|
+
? integrationProjectionQueries.restoreOf<DbRestoreSnapshot>(prior)
|
|
210
|
+
: adopted ? snapshotOf(target) : null;
|
|
211
|
+
|
|
212
|
+
if (!prior && adopted) {
|
|
213
|
+
debug.log('db-client', `Adopting existing connection ${target.id} for ${context.provider.id}`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// An adopted row keeps the name its user gave it. A row we created is
|
|
217
|
+
// ours to name, and follows the link's label.
|
|
218
|
+
writeRow(target.id, link, endpoint, { rename: !adopted });
|
|
219
|
+
rememberConnection(link, target.id);
|
|
220
|
+
claimed.add(target.id);
|
|
221
|
+
|
|
222
|
+
results.push({
|
|
223
|
+
targetKind: 'db_client_connection',
|
|
224
|
+
targetId: target.id,
|
|
225
|
+
adopted,
|
|
226
|
+
restore
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return results;
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
release(context: ProjectionContext, targetId: string, adopted: boolean, restore: unknown | null): void {
|
|
234
|
+
if (!adopted) {
|
|
235
|
+
dbClientConnectionQueries.delete(targetId);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// The row was the user's before it was ours. Put back exactly what we
|
|
240
|
+
// found, so what is left behind is their connection rather than a husk
|
|
241
|
+
// pointing at a password that no longer exists.
|
|
242
|
+
const snapshot = restore as DbRestoreSnapshot | null;
|
|
243
|
+
if (!snapshot || !dbClientConnectionQueries.get(targetId)) return;
|
|
244
|
+
|
|
245
|
+
dbClientConnectionQueries.update(targetId, {
|
|
246
|
+
host: snapshot.host ?? '',
|
|
247
|
+
port: snapshot.port ?? undefined,
|
|
248
|
+
username: snapshot.username ?? '',
|
|
249
|
+
database: snapshot.database ?? '',
|
|
250
|
+
sslMode: snapshot.sslMode ?? 'disable',
|
|
251
|
+
options: snapshot.options ?? {}
|
|
252
|
+
});
|
|
253
|
+
dbClientConnectionQueries.replacePassword(targetId, snapshot.password);
|
|
254
|
+
debug.log('db-client', `Released adopted connection ${targetId} back to the user (${context.provider.id})`);
|
|
255
|
+
}
|
|
256
|
+
};
|