pgkiln 0.36.0 → 0.38.0

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.
Files changed (54) hide show
  1. package/SECURITY.md +12 -9
  2. package/db/migrations/090_workspace_isolation.sql +512 -0
  3. package/db/migrations/091_workspace_service.sql +20 -0
  4. package/db/migrations/092_app_groups.sql +23 -0
  5. package/docs/apex-feature-parity.md +16 -11
  6. package/docs/guide/03-builder.md +125 -11
  7. package/docs/guide/12-development.md +12 -2
  8. package/examples/gallery/README.md +13 -0
  9. package/examples/gallery/gallery.json +20 -0
  10. package/examples/gallery/starters/asset-inventory.json +30 -0
  11. package/examples/gallery/starters/contacts.json +41 -0
  12. package/examples/gallery/starters/event-registrations.json +29 -0
  13. package/examples/gallery/starters/expense-claims.json +32 -0
  14. package/examples/gallery/starters/team-tasks.json +35 -0
  15. package/examples/hr/hr_40_sample_data.sql +1 -1
  16. package/package.json +1 -1
  17. package/public/builder.css +91 -0
  18. package/src/app.ts +24 -0
  19. package/src/automations.ts +3 -2
  20. package/src/builder/advisor.ts +11 -5
  21. package/src/builder/ai-builder.ts +27 -18
  22. package/src/builder/app-groups.ts +132 -0
  23. package/src/builder/blueprints.ts +21 -7
  24. package/src/builder/code-editor.ts +3 -2
  25. package/src/builder/cross-app.ts +106 -0
  26. package/src/builder/dataload.ts +24 -14
  27. package/src/builder/ddl-tools.ts +7 -4
  28. package/src/builder/documents.ts +2 -1
  29. package/src/builder/gallery.ts +154 -0
  30. package/src/builder/home.ts +33 -10
  31. package/src/builder/newapp.ts +21 -3
  32. package/src/builder/quicksql.ts +3 -2
  33. package/src/builder/report-settings.ts +4 -2
  34. package/src/builder/routes.ts +78 -26
  35. package/src/builder/sampledata.ts +22 -18
  36. package/src/builder/scripts.ts +31 -16
  37. package/src/builder/sql.ts +119 -15
  38. package/src/builder/ui.ts +30 -7
  39. package/src/builder/unload.ts +3 -2
  40. package/src/builder/workspaces.ts +291 -22
  41. package/src/cli/replace.ts +2 -0
  42. package/src/db.ts +36 -6
  43. package/src/ddl.ts +2 -1
  44. package/src/debug.ts +3 -2
  45. package/src/migrate.ts +3 -0
  46. package/src/process-jobs.ts +7 -2
  47. package/src/restsync.ts +3 -2
  48. package/src/runtime/calendar.ts +2 -1
  49. package/src/runtime/routes.ts +4 -1
  50. package/src/sampledata.ts +3 -3
  51. package/src/session.ts +18 -4
  52. package/src/websources.ts +3 -1
  53. package/src/workflow.ts +65 -9
  54. package/src/workspace-db.ts +166 -0
@@ -3,6 +3,7 @@ import { designSql } from './websources.ts';
3
3
  import pg from 'pg';
4
4
  import { applyBinds } from '../binds.ts';
5
5
  import { owner } from '../db.ts';
6
+ import { devDbForApp } from '../workspace-db.ts';
6
7
  import { html, raw } from '../html.ts';
7
8
  import { formatMaskError } from '../runtime/format.ts';
8
9
  import { heading } from '../runtime/items.ts';
@@ -19,12 +20,13 @@ const PAGE_SIZES = ['5', '10', '15', '25', '50', '100'];
19
20
  /** The column names of a report's query: run with limit 0, as the app's role, then rolled back. */
20
21
  export async function reportColumns(appId: number, source: string | null): Promise<{ columns: string[] } | { error: string }> {
21
22
  if (!source?.trim()) return { error: 'The region has no source query yet.' };
22
- const c = await owner.pool.connect();
23
+ // the role from pgkiln's tables (the owner); the query on the developer's connection (090)
24
+ const role = (await owner.one<{ db_role: string | null }>('select db_role from meta.app where id = $1', [appId]))?.db_role;
25
+ const c = await (await devDbForApp(appId)).pool.connect();
23
26
  try {
24
27
  await c.query('begin');
25
28
  await c.query(`set local statement_timeout = '5s'`);
26
29
  await c.query(`select set_config('pgkiln.app_id', $1, true)`, [String(appId)]);
27
- const role = (await c.query('select db_role from meta.app where id = $1', [appId])).rows[0]?.db_role;
28
30
  if (role) await c.query(`set local role ${pg.escapeIdentifier(role)}`);
29
31
  const sql = applyBinds(source.trim().replace(/;+\s*$/, ''), {});
30
32
  const res = await c.query(`select * from (\n${sql}\n) "__q" limit 0`);
@@ -17,7 +17,7 @@ import { APP_COLORS, appHeader, back, BASE, builderHead, csrf, developer, flash,
17
17
  import { appOr404 } from './forms.ts';
18
18
  import { docToFiles, filesToZip } from '../appfiles.ts';
19
19
  import { homeRoutes, rememberApp } from './home.ts';
20
- import { appAllowed, currentWorkspace, placeApp, workspaceRoutes } from './workspaces.ts';
20
+ import { appAllowed, currentWorkspace, placeApp, workshopDb, workspaceRoutes } from './workspaces.ts';
21
21
  import { instanceRoutes } from './instance.ts';
22
22
  import { saveTimeZoneSettings, timeZoneSettings } from './globalization.ts';
23
23
  import { WIZARD_KINDS, wizardRoutes, wizardTables } from './wizards.ts';
@@ -31,23 +31,61 @@ import { clearDevbar } from '../runtime/devbar.ts';
31
31
 
32
32
  export async function builderRoutes(app: FastifyInstance) {
33
33
  // ---------------------------------------------------------------- login
34
+ // (sprint 61, APEX: the workspace sign-in page) workspace (optional), username and password;
35
+ // "Remember workspace and username" keeps both in a cookie of the builder's path
36
+ const REMEMBER_COOKIE = 'pgkiln_bsignin';
37
+ const remembered = (req: Req): { w: string; u: string } | null => {
38
+ try {
39
+ const v = JSON.parse(Buffer.from(String(req.cookies?.[REMEMBER_COOKIE] ?? ''), 'base64url').toString('utf8'));
40
+ return typeof v?.w === 'string' && typeof v?.u === 'string' ? { w: v.w.slice(0, 60), u: v.u.slice(0, 100) } : null;
41
+ } catch {
42
+ return null;
43
+ }
44
+ };
45
+ /** Where to go after signing in: a builder page only. */
46
+ const safeNext = (v: unknown) => (typeof v === 'string' && /^\/builder(\/[A-Za-z0-9_/-]{0,200})?$/.test(v) ? v : BASE);
47
+
34
48
  app.get(`${BASE}/login`, async (req: Req, reply) => {
35
49
  const s = await getSession(req, reply, null, BASE);
36
50
  if (s.username) return reply.redirect(BASE);
37
51
  const error = takeFlash(s, '__ERROR');
38
52
  await saveState(s);
53
+ const r = remembered(req);
54
+ const next = safeNext(req.query?.next);
55
+ const field = (name: string, label: string, ic: string, value: string, opts: { type?: string; required?: boolean; auto?: string; placeholder?: string; reveal?: boolean }) =>
56
+ html`<div class="field signin-field"><label class="sr-only" for="f_${name}">${label}</label>
57
+ <div class="signin-input">${icon(ic)}<input id="f_${name}" name="${name}" type="${opts.type ?? 'text'}" value="${value}" placeholder="${opts.placeholder ?? label}"${opts.required ? raw(' required') : ''}${opts.auto ? raw(` autocomplete="${opts.auto}"`) : ''}>
58
+ ${opts.reveal ? html`<button type="button" class="signin-reveal" data-reveal="f_${name}" aria-controls="f_${name}" aria-pressed="false" data-hide="Hide" hidden>Show</button>` : ''}</div></div>`;
39
59
  return reply.type('text/html').send(
40
60
  documentShell(
41
61
  'Sign in · Builder',
42
- html`<main class="login"><form method="post" class="card login-card">
43
- <h1>pgkiln Builder</h1>
44
- ${error ? html`<div class="alert alert-error" role="alert">${error}</div>` : ''}
45
- ${csrf(s)}
46
- ${input('username', 'Username', '', { required: true, auto: 'username' })}
47
- ${input('password', 'Password', '', { type: 'password', required: true, auto: 'current-password' })}
48
- <button class="btn btn-hot">Sign in</button>
49
- </form></main>`,
50
- 'login-body ide-login',
62
+ html`<main class="signin">
63
+ <section class="signin-hero" aria-label="pgkiln">
64
+ <span class="signin-logo" aria-hidden="true">pg</span>
65
+ <h1 class="signin-title">pgkiln</h1>
66
+ <p class="signin-tagline">Build data applications on PostgreSQL, in SQL you can trust</p>
67
+ <p class="signin-foot">Open source under the Apache-2.0 license</p>
68
+ </section>
69
+ <section class="signin-panel">
70
+ <form method="post" class="signin-form">
71
+ <h2>Welcome</h2>
72
+ <p class="muted u-mt0">Sign in to your workspace to continue</p>
73
+ ${error ? html`<div class="alert alert-error" role="alert">${error}</div>` : ''}
74
+ ${csrf(s)}<input type="hidden" name="next" value="${next}">
75
+ ${field('workspace', 'Workspace', 'database', r?.w ?? '', { placeholder: 'Workspace (optional)', auto: 'organization' })}
76
+ ${field('username', 'Username', 'user', r?.u ?? '', { required: true, auto: 'username' })}
77
+ ${field('password', 'Password', 'key', '', { type: 'password', required: true, auto: 'current-password', reveal: true })}
78
+ <label class="check signin-remember"><input type="checkbox" name="remember" value="1"${r ? raw(' checked') : ''}> Remember workspace and username</label>
79
+ <button class="btn btn-hot signin-submit">Sign in</button>
80
+ <p class="muted small signin-hint">Leave the workspace empty to open your last one; you can switch later in your account menu.</p>
81
+ <nav class="signin-actions" aria-label="Quick actions">
82
+ <a href="${BASE}/login?next=${encodeURIComponent(`${BASE}/utilities`)}">${icon('settings')} Administration</a>
83
+ <a href="https://github.com/pgkiln/pgkiln/blob/main/docs/guide/03-builder.md#workspaces" target="_blank" rel="noopener">${icon('help')} About workspaces</a>
84
+ </nav>
85
+ </form>
86
+ </section>
87
+ </main>`,
88
+ 'login-body ide-login signin-body',
51
89
  {},
52
90
  builderHead(),
53
91
  { theme: validTheme(req.cookies?.[THEME_COOKIE]) ?? 'dark' },
@@ -58,15 +96,18 @@ export async function builderRoutes(app: FastifyInstance) {
58
96
  app.post(`${BASE}/login`, async (req: Req, reply) => {
59
97
  const s = await getSession(req, reply, null, BASE);
60
98
  const { username = '', password = '' } = req.body ?? {};
99
+ const workspace = String(req.body?.workspace ?? '').trim().slice(0, 60);
100
+ const next = safeNext(req.body?.next);
101
+ const again = next === BASE ? `${BASE}/login` : `${BASE}/login?next=${encodeURIComponent(next)}`;
61
102
  const ip = clientIp(req);
62
103
  if (req.body?.__csrf !== s.csrf_token) {
63
104
  flash(s, 'Your session expired. Please try again.', 'error');
64
- return back(reply, s, `${BASE}/login`);
105
+ return back(reply, s, again);
65
106
  }
66
107
  if (await loginThrottled(null, username, ip)) {
67
108
  logActivity({ username, event: 'login_locked', ip, detail: 'builder' });
68
109
  flash(s, 'Too many failed sign-in attempts. Try again later.', 'error');
69
- return back(reply, s, `${BASE}/login`);
110
+ return back(reply, s, again);
70
111
  }
71
112
  // bcrypt always runs (dummy salt for unknown users) so timing does not reveal accounts
72
113
  const dev = await owner.one(
@@ -76,25 +117,36 @@ export async function builderRoutes(app: FastifyInstance) {
76
117
  where crypt($2, coalesce(d.password_hash, gen_salt('bf', 10))) = d.password_hash`,
77
118
  [username, password],
78
119
  );
79
- if (!dev) {
80
- await logActivity({ username: username.slice(0, 100), event: 'login_failed', ip, detail: 'builder' });
81
- flash(s, 'Invalid username or password.', 'error');
82
- return back(reply, s, `${BASE}/login`);
120
+ // a workspace, when given, must be one of the developer's (administrators: any); the same answer otherwise
121
+ const ws = dev && workspace
122
+ ? await owner.one<{ id: number }>(
123
+ `select w.id from meta.workspace w
124
+ where lower(w.name) = lower($1)
125
+ and (exists (select 1 from meta.developer d where d.username = $2 and d.is_admin)
126
+ or exists (select 1 from meta.workspace_member m where m.workspace_id = w.id and m.username = $2))`,
127
+ [workspace, dev.username],
128
+ )
129
+ : undefined;
130
+ if (!dev || (workspace && !ws)) {
131
+ await logActivity({ username: username.slice(0, 100), event: 'login_failed', ip, detail: workspace ? 'builder (workspace)' : 'builder' });
132
+ flash(s, workspace ? 'Invalid workspace, username or password.' : 'Invalid username or password.', 'error');
133
+ return back(reply, s, again);
83
134
  }
84
135
  await destroySession(reply, s, BASE);
85
136
  const ns = await createSession(reply, null, BASE, dev.username);
86
137
  // the builder theme chosen earlier on this device
87
138
  const theme = validTheme(req.cookies?.[THEME_COOKIE]);
88
- if (theme) {
89
- ns.state.__BTHEME = theme;
90
- await saveState(ns);
91
- }
139
+ if (theme) ns.state.__BTHEME = theme;
140
+ if (ws) ns.state.__WS = String(ws.id);
141
+ if (password === 'admin' || password === dev.username) ns.state.__WEAK = '1';
142
+ await saveState(ns);
143
+ if (req.body?.remember === '1')
144
+ reply.setCookie(REMEMBER_COOKIE, Buffer.from(JSON.stringify({ w: workspace, u: dev.username })).toString('base64url'), {
145
+ path: BASE, httpOnly: true, sameSite: 'lax', maxAge: 365 * 24 * 3600, secure: process.env.COOKIE_SECURE === 'true',
146
+ });
147
+ else reply.clearCookie(REMEMBER_COOKIE, { path: BASE });
92
148
  logActivity({ username: dev.username, event: 'login', ip, detail: 'builder' });
93
- if (password === 'admin' || password === dev.username) {
94
- ns.state.__WEAK = '1';
95
- await saveState(ns);
96
- }
97
- return reply.redirect(BASE, 303);
149
+ return reply.redirect(next, 303);
98
150
  });
99
151
 
100
152
  app.post(`${BASE}/logout`, async (req: Req, reply) => {
@@ -170,7 +222,7 @@ export async function builderRoutes(app: FastifyInstance) {
170
222
  await placeApp(c, made.id, ws);
171
223
  // (sprint 51) the Advisor on the imported application, before the commit: errors refuse it
172
224
  if (req.body?.check === 'true') {
173
- const errors = (await advise(made.id, c)).findings.filter((f) => f.severity === 'error');
225
+ const errors = (await advise(made.id, c, { db: await workshopDb(s) })).findings.filter((f) => f.severity === 'error');
174
226
  if (errors.length)
175
227
  throw new Error(`the check found ${errors.length} error(s), nothing was imported: ${errors.slice(0, 5).map((f) =>
176
228
  `${f.entry ? `${f.entry.pageNo !== null ? `page ${f.entry.pageNo}, ` : ''}${f.entry.label}` : 'database'} (${f.field}): ${f.message}`).join('; ')}${errors.length > 5 ? '; …' : ''}. Fix them, or import without the check.`);
@@ -31,6 +31,7 @@ import {
31
31
  } from '../sampledata.ts';
32
32
  import { clientIp, logActivity, type Session } from '../session.ts';
33
33
  import { back, BASE, csrf, developer, flash, region, send, shell, workshopTabs, type Body, type Req } from './ui.ts';
34
+ import { currentWorkspace, workshopDb } from './workspaces.ts';
34
35
 
35
36
  // SQL Workshop → Sample Data (APEX 26.1: Data Generator): generate realistic
36
37
  // rows for one or more tables of a schema (src/sampledata.ts). Step 1 picks a
@@ -58,9 +59,9 @@ interface Saved {
58
59
  tables: TableSpec[];
59
60
  }
60
61
 
61
- async function saved(id: unknown) {
62
+ async function saved(s: Session, id: unknown) {
62
63
  if (!/^\d{1,9}$/.test(String(id ?? ''))) return undefined;
63
- return owner.one<Saved>('select * from meta.data_generator where id = $1', [id]);
64
+ return owner.one<Saved>('select * from meta.data_generator where id = $1 and workspace_id = $2', [id, currentWorkspace(s)]);
64
65
  }
65
66
 
66
67
  /** The definition posted by the generator form (never trusted: plan() checks it against the catalog). */
@@ -108,12 +109,13 @@ export async function sampleDataRoutes(app: FastifyInstance) {
108
109
 
109
110
  const intro = html`<p class="muted">Generate realistic rows for the tables of a schema: names, e-mail addresses, dates and numbers in a range, values from a list,
110
111
  foreign keys that pick existing parent rows. Preview them, insert them in one transaction (parents first) or download them as SQL or CSV.
111
- Runs as the builder's owner connection, like <a href="${BASE}/sql">SQL Commands</a>.</p>`;
112
+ Runs as the builder's owner connection (an isolated workspace's developer login for its developers), like <a href="${BASE}/sql">SQL Commands</a>.</p>`;
112
113
 
113
114
  // ------------------------------------------------------------ step 1 and 2: schema and tables
114
115
  const start = async (s: Session, reply: FastifyReply, schema: string | undefined) => {
115
- const all = await schemas(owner.pool);
116
- const gens = (await owner.query<Saved & { updated_at: string; updated_by: string }>('select id, name, description, schema_name, tables, updated_at, updated_by from meta.data_generator order by lower(name)')).rows;
116
+ const db = await workshopDb(s);
117
+ const all = (await schemas(db.pool)).filter((x) => db === owner || x.usable);
118
+ const gens = (await owner.query<Saved & { updated_at: string; updated_by: string }>('select id, name, description, schema_name, tables, updated_at, updated_by from meta.data_generator where workspace_id = $1 order by lower(name)', [currentWorkspace(s)])).rows;
117
119
  const savedList = gens.length
118
120
  ? html`<ul class="sd-saved">${gens.map(
119
121
  (g) => html`<li><a href="${sdUrl}/${g.id}">${g.name}</a> <span class="muted small">${g.schema_name}: ${g.tables.map((t) => `${t.table} (${t.rows})`).join(', ')}</span>
@@ -129,7 +131,7 @@ export async function sampleDataRoutes(app: FastifyInstance) {
129
131
  <button class="btn">Choose</button></form>`;
130
132
  let tablesForm: Raw | '' = '';
131
133
  if (chosen) {
132
- const tables = await schemaTables(owner.pool, chosen);
134
+ const tables = await schemaTables(db.pool, chosen);
133
135
  tablesForm = html`<form method="get" action="${sdUrl}" class="u-mt1"><input type="hidden" name="schema" value="${chosen}">
134
136
  <fieldset class="prop-group"><legend>Tables of ${chosen}</legend><div class="qb-pick">${tables.map(
135
137
  (t) => html`<label class="check"><input type="checkbox" name="t" value="${t.name}"> ${t.name} <span class="muted small">${t.estimate >= 0 ? `~${t.estimate.toLocaleString('en')} rows` : ''}</span></label>`,
@@ -155,23 +157,24 @@ export async function sampleDataRoutes(app: FastifyInstance) {
155
157
  }
156
158
 
157
159
  /** The tables of a definition with their catalog info; specs proposed for columns the definition doesn't mention. */
158
- const describeAll = async (def: GeneratorDef) => {
160
+ const describeAll = async (s: Session, def: GeneratorDef) => {
161
+ const db = (await workshopDb(s)).pool;
159
162
  const out: { info: TableInfo; spec: TableSpec }[] = [];
160
163
  const missing: string[] = [];
161
164
  for (const t of def.tables) {
162
- const info = await describe(owner.pool, def.schema, t.table);
165
+ const info = await describe(db, def.schema, t.table);
163
166
  if (!info) {
164
167
  missing.push(t.table);
165
168
  continue;
166
169
  }
167
- const spec = tableSpec(info, DEFAULT_ROWS, t, undefined, await nextValues(owner.pool, info));
170
+ const spec = tableSpec(info, DEFAULT_ROWS, t, undefined, await nextValues(db, info));
168
171
  out.push({ info, spec: { ...spec, rows: Number.isInteger(t.rows) ? t.rows : DEFAULT_ROWS } });
169
172
  }
170
173
  return { tables: out, missing };
171
174
  };
172
175
 
173
176
  const editor = async (s: Session, reply: FastifyReply, st: FormState, code = 200) => {
174
- const { tables, missing } = await describeAll(st.def);
177
+ const { tables, missing } = await describeAll(s, st.def);
175
178
  const title = st.id ? (st.name ?? 'Generator') : 'New generator';
176
179
  const columnRow = (i: number, j: number, c: ColumnInfo, spec: TableSpec) => {
177
180
  const cs = spec.columns.find((x) => x.column === c.name)!;
@@ -246,7 +249,7 @@ export async function sampleDataRoutes(app: FastifyInstance) {
246
249
  app.get(`${sdUrl}/:id`, async (req: Req, reply) => {
247
250
  const s = await developer(req, reply);
248
251
  if (!s) return;
249
- const g = await saved(req.params.id);
252
+ const g = await saved(s, req.params.id);
250
253
  if (!g) return reply.code(404).send('Not found');
251
254
  return editor(s, reply, { id: g.id, name: g.name, description: g.description ?? '', def: cleanDef({ schema: g.schema_name, seed: g.seed, tables: g.tables }) });
252
255
  });
@@ -256,7 +259,7 @@ export async function sampleDataRoutes(app: FastifyInstance) {
256
259
  if (!s) return;
257
260
  const b = req.body ?? {};
258
261
  const def = defFromBody(b);
259
- const g = b.id ? await saved(b.id) : undefined;
262
+ const g = b.id ? await saved(s, b.id) : undefined;
260
263
  if (b.id && !g) return reply.code(404).send('Not found');
261
264
  const st: FormState = { def, id: g?.id, name: (b.name ?? '').trim(), description: (b.description ?? '').trim() };
262
265
  const refuse = (error: string) => editor(s, reply, { ...st, error }, 422);
@@ -266,13 +269,13 @@ export async function sampleDataRoutes(app: FastifyInstance) {
266
269
  if (!st.name) return refuse('Give the generator a name to save it.');
267
270
  if (st.name.length > 100 || (st.description ?? '').length > 1000) return refuse('The name is at most 100 characters, the description 1000.');
268
271
  if (!allowedSchema(def.schema)) return refuse('Choose a schema of your own: not meta, information_schema or pg_*.');
269
- const { tables } = await describeAll(def);
272
+ const { tables } = await describeAll(s, def);
270
273
  if (!tables.length) return refuse('Choose at least one table.');
271
274
  const json = JSON.stringify(tables.map(({ spec }) => spec));
272
275
  try {
273
276
  const id = g
274
277
  ? (await owner.one('update meta.data_generator set name = $2, description = $3, schema_name = $4, seed = $5, tables = $6, updated_by = $7, updated_at = now() where id = $1 returning id', [g.id, st.name, st.description || null, def.schema, def.seed, json, s.username])).id
275
- : (await owner.one('insert into meta.data_generator (name, description, schema_name, seed, tables, created_by, updated_by) values ($1, $2, $3, $4, $5, $6, $6) returning id', [st.name, st.description || null, def.schema, def.seed, json, s.username])).id;
278
+ : (await owner.one('insert into meta.data_generator (name, description, schema_name, seed, tables, created_by, updated_by, workspace_id) values ($1, $2, $3, $4, $5, $6, $6, $7) returning id', [st.name, st.description || null, def.schema, def.seed, json, s.username, currentWorkspace(s)])).id;
276
279
  flash(s, `Generator ${st.name} saved.`);
277
280
  return back(reply, s, `${sdUrl}/${id}`);
278
281
  } catch (e) {
@@ -282,7 +285,8 @@ export async function sampleDataRoutes(app: FastifyInstance) {
282
285
  }
283
286
  if (!['preview', 'insert', 'sql', 'csv'].includes(action)) return refuse('Unknown action.');
284
287
 
285
- const p = await plan(owner.pool, def);
288
+ const db = await workshopDb(s);
289
+ const p = await plan(db.pool, def);
286
290
  if (p.problems.length) return refuse(p.problems.join(' '));
287
291
  const seed = def.seed ?? randomSeed();
288
292
  st.def = { ...def, seed };
@@ -290,7 +294,7 @@ export async function sampleDataRoutes(app: FastifyInstance) {
290
294
  if (action === 'sql' || action === 'csv') {
291
295
  let generated;
292
296
  try {
293
- generated = await generateAll(owner.pool, p.tables, seed);
297
+ generated = await generateAll(db.pool, p.tables, seed);
294
298
  } catch (e) {
295
299
  if (e instanceof SampleDataError) return refuse(e.message);
296
300
  throw e;
@@ -309,7 +313,7 @@ export async function sampleDataRoutes(app: FastifyInstance) {
309
313
 
310
314
  // preview and insert: the same run, rolled back or committed
311
315
  const started = performance.now();
312
- const c = await owner.pool.connect();
316
+ const c = await db.pool.connect();
313
317
  let result: RunResult;
314
318
  try {
315
319
  await c.query('begin');
@@ -340,7 +344,7 @@ export async function sampleDataRoutes(app: FastifyInstance) {
340
344
  app.post(`${sdUrl}/:id/delete`, async (req: Req, reply) => {
341
345
  const s = await developer(req, reply);
342
346
  if (!s) return;
343
- const g = await saved(req.params.id);
347
+ const g = await saved(s, req.params.id);
344
348
  if (!g) return reply.code(404).send('Not found');
345
349
  await owner.query('delete from meta.data_generator where id = $1', [g.id]);
346
350
  flash(s, `Generator ${g.name} deleted.`);
@@ -7,12 +7,14 @@ import { clientIp, logActivity, type Session } from '../session.ts';
7
7
  import { runScript, splitScript, type ScriptRun, type StatementResult } from '../sqlscript.ts';
8
8
  import { clearCompletions } from './code-editor.ts';
9
9
  import { back, BASE, csrf, developer, flash, region, send, shell, workshopTabs, type Body, type Req } from './ui.ts';
10
+ import { currentWorkspace, sessionSchema, workshopDb } from './workspaces.ts';
10
11
 
11
12
  // SQL Workshop → SQL Scripts: saved scripts (create, edit, upload, download,
12
13
  // delete), run with a result per statement (stop at the first error or go on,
13
14
  // optionally in one transaction), and the run history. Scripts run on the
14
15
  // owner connection, like SQL Commands, on a connection of their own that is
15
- // closed afterwards (a script may SET ROLE or change settings).
16
+ // closed afterwards (a script may SET ROLE or change settings). Scripts and runs belong
17
+ // to the current workspace; an isolated workspace's run on its developer login (090).
16
18
 
17
19
  /** Largest script (upload or editor), in MB. */
18
20
  export const SCRIPT_MAX_MB = 5;
@@ -30,9 +32,13 @@ export interface RunRequest {
30
32
  /** Run a script, record the run (history + activity log) and return the run's id. */
31
33
  export async function runAndRecord(s: Session, ip: string, r: RunRequest): Promise<{ id: string; run: ScriptRun }> {
32
34
  const statements = splitScript(r.content);
33
- const c = await owner.pool.connect();
35
+ const db = await workshopDb(s);
36
+ const schema = await sessionSchema(s, db);
37
+ const c = await db.pool.connect();
34
38
  let run: ScriptRun;
35
39
  try {
40
+ // (sprint 61) the session's schema first in the search path (the SQL Workshop home)
41
+ if (schema) await c.query(`select set_config('search_path', $1, false)`, [`"${schema.replace(/"/g, '""')}", public`]);
36
42
  run = await runScript(c, statements, { stopOnError: r.stopOnError, transaction: r.transaction });
37
43
  } finally {
38
44
  // never hand a connection back to the pool after arbitrary SQL (SET ROLE, search_path, temp tables)
@@ -40,12 +46,17 @@ export async function runAndRecord(s: Session, ip: string, r: RunRequest): Promi
40
46
  }
41
47
  clearCompletions(); // the script may have changed tables or grants
42
48
  const saved = await owner.one<{ id: string }>(
43
- `insert into meta.sql_script_run (script_id, script_name, run_by, elapsed_ms, stop_on_error, transactional, statements, succeeded, failed, rolled_back, results)
44
- values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) returning id`,
49
+ `insert into meta.sql_script_run (script_id, script_name, run_by, elapsed_ms, stop_on_error, transactional, statements, succeeded, failed, rolled_back, results, workspace_id)
50
+ values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) returning id`,
45
51
  [r.scriptId, r.name, s.username, run.ms, r.stopOnError, r.transaction, run.statements, run.succeeded, run.failed, run.rolledBack,
46
- JSON.stringify(run.commitError ? [...run.results, { n: 0, line: 0, sql: 'COMMIT', status: 'error', error: run.commitError, ms: 0 }] : run.results)],
52
+ JSON.stringify(run.commitError ? [...run.results, { n: 0, line: 0, sql: 'COMMIT', status: 'error', error: run.commitError, ms: 0 }] : run.results),
53
+ currentWorkspace(s)],
54
+ );
55
+ await owner.query(
56
+ `delete from meta.sql_script_run where workspace_id = $2
57
+ and id <= (select id from meta.sql_script_run where workspace_id = $2 order by id desc offset $1 limit 1)`,
58
+ [KEEP_RUNS, currentWorkspace(s)],
47
59
  );
48
- await owner.query(`delete from meta.sql_script_run where id <= (select id from meta.sql_script_run order by id desc offset $1 limit 1)`, [KEEP_RUNS]);
49
60
  await logActivity({
50
61
  username: s.username,
51
62
  event: 'sql_script',
@@ -121,10 +132,12 @@ export async function scriptRoutes(app: FastifyInstance) {
121
132
  r.started_at as last_run, r.failed as last_failed
122
133
  from meta.sql_script s
123
134
  left join lateral (select started_at, failed from meta.sql_script_run x where x.script_id = s.id order by x.id desc limit 1) r on true
135
+ where s.workspace_id = $1
124
136
  order by lower(s.name)`,
137
+ [currentWorkspace(s)],
125
138
  )
126
139
  ).rows;
127
- const runs = (await owner.query('select * from meta.sql_script_run order by id desc limit 20')).rows;
140
+ const runs = (await owner.query('select * from meta.sql_script_run where workspace_id = $1 order by id desc limit 20', [currentWorkspace(s)])).rows;
128
141
  const list = scripts.length
129
142
  ? html`<div class="table-wrap"><table class="report"><thead><tr><th>Script</th><th>Updated</th><th class="num">Size</th><th>Last run</th><th>File</th></tr></thead><tbody>
130
143
  ${scripts.map((x) => html`<tr><td><a href="${BASE}/sql/scripts/${x.id}">${x.name}</a>${x.description ? html`<br><span class="muted small">${x.description}</span>` : ''}</td>
@@ -193,7 +206,7 @@ export async function scriptRoutes(app: FastifyInstance) {
193
206
  app.get(`${BASE}/sql/scripts/:id`, async (req: Req, reply) => {
194
207
  const s = await developer(req, reply);
195
208
  if (!s) return;
196
- const row = /^\d+$/.test(req.params.id) ? await owner.one('select * from meta.sql_script where id = $1', [req.params.id]) : undefined;
209
+ const row = /^\d+$/.test(req.params.id) ? await owner.one('select * from meta.sql_script where id = $1 and workspace_id = $2', [req.params.id, currentWorkspace(s)]) : undefined;
197
210
  if (!row) return reply.code(404).send('Not found');
198
211
  return editor(req, reply, s, row);
199
212
  });
@@ -220,15 +233,15 @@ export async function scriptRoutes(app: FastifyInstance) {
220
233
  try {
221
234
  if (id) {
222
235
  const r = await owner.one(
223
- 'update meta.sql_script set name = $2, description = nullif($3, \'\'), content = $4, updated_by = $5, updated_at = now() where id = $1 returning id',
224
- [id, name, b.description ?? '', content, s.username],
236
+ 'update meta.sql_script set name = $2, description = nullif($3, \'\'), content = $4, updated_by = $5, updated_at = now() where id = $1 and workspace_id = $6 returning id',
237
+ [id, name, b.description ?? '', content, s.username, currentWorkspace(s)],
225
238
  );
226
239
  if (!r) return reply.code(404).send('Not found');
227
240
  scriptId = r.id;
228
241
  } else {
229
242
  const r = await owner.one(
230
- `insert into meta.sql_script (name, description, content, created_by, updated_by) values ($1, nullif($2, ''), $3, $4, $4) returning id`,
231
- [name, b.description ?? '', content, s.username],
243
+ `insert into meta.sql_script (name, description, content, created_by, updated_by, workspace_id) values ($1, nullif($2, ''), $3, $4, $4, $5) returning id`,
244
+ [name, b.description ?? '', content, s.username, currentWorkspace(s)],
232
245
  );
233
246
  scriptId = r!.id;
234
247
  }
@@ -260,6 +273,8 @@ export async function scriptRoutes(app: FastifyInstance) {
260
273
  if (!s) return;
261
274
  if (!/^\d+$/.test(req.params.id)) return reply.code(404).send('Not found');
262
275
  const r = await owner.tx(async (c) => {
276
+ const own = (await c.query('select 1 from meta.sql_script where id = $1 and workspace_id = $2', [req.params.id, currentWorkspace(s)])).rowCount;
277
+ if (!own) return undefined;
263
278
  await c.query('delete from meta.sql_script_run where script_id = $1', [req.params.id]);
264
279
  return (await c.query('delete from meta.sql_script where id = $1 returning name', [req.params.id])).rows[0];
265
280
  });
@@ -270,7 +285,7 @@ export async function scriptRoutes(app: FastifyInstance) {
270
285
  app.get(`${BASE}/sql/scripts/:id/download`, async (req: Req, reply) => {
271
286
  const s = await developer(req, reply);
272
287
  if (!s) return;
273
- const row = /^\d+$/.test(req.params.id) ? await owner.one('select name, content from meta.sql_script where id = $1', [req.params.id]) : undefined;
288
+ const row = /^\d+$/.test(req.params.id) ? await owner.one('select name, content from meta.sql_script where id = $1 and workspace_id = $2', [req.params.id, currentWorkspace(s)]) : undefined;
274
289
  if (!row) return reply.code(404).send('Not found');
275
290
  return reply
276
291
  .header('content-type', 'application/sql; charset=utf-8')
@@ -302,8 +317,8 @@ export async function scriptRoutes(app: FastifyInstance) {
302
317
  if (content.includes('\0')) return fail('The file is not a text file.');
303
318
  const base = file.filename.replace(/^.*[\\/]/, '').replace(/\.sql$/i, '').slice(0, 180) || 'script';
304
319
  let name = base;
305
- for (let n = 2; await owner.one('select 1 from meta.sql_script where name = $1', [name]); n++) name = `${base} (${n})`;
306
- const r = await owner.one(`insert into meta.sql_script (name, content, created_by, updated_by) values ($1, $2, $3, $3) returning id`, [name, content, s.username]);
320
+ for (let n = 2; await owner.one('select 1 from meta.sql_script where name = $1 and workspace_id = $2', [name, currentWorkspace(s)]); n++) name = `${base} (${n})`;
321
+ const r = await owner.one(`insert into meta.sql_script (name, content, created_by, updated_by, workspace_id) values ($1, $2, $3, $3, $4) returning id`, [name, content, s.username, currentWorkspace(s)]);
307
322
  flash(s, `Script “${name}” uploaded.`);
308
323
  return back(reply, s, `${BASE}/sql/scripts/${r!.id}`);
309
324
  });
@@ -312,7 +327,7 @@ export async function scriptRoutes(app: FastifyInstance) {
312
327
  app.get(`${BASE}/sql/scripts/runs/:run`, async (req: Req, reply) => {
313
328
  const s = await developer(req, reply);
314
329
  if (!s) return;
315
- const run = /^\d+$/.test(req.params.run) ? await owner.one('select * from meta.sql_script_run where id = $1', [req.params.run]) : undefined;
330
+ const run = /^\d+$/.test(req.params.run) ? await owner.one('select * from meta.sql_script_run where id = $1 and workspace_id = $2', [req.params.run, currentWorkspace(s)]) : undefined;
316
331
  if (!run) return reply.code(404).send('Not found');
317
332
  const exists = run.script_id ? await owner.one('select id from meta.sql_script where id = $1', [run.script_id]) : undefined;
318
333
  const options = `${run.stop_on_error ? 'stop on error' : 'continue on error'}${run.transactional ? ', one transaction' : ''}`;