pgkiln 0.47.0 → 0.49.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 (69) hide show
  1. package/SECURITY.md +2 -2
  2. package/db/migrations/109_builder_pins.sql +19 -0
  3. package/db/migrations/110_sql_charts.sql +23 -0
  4. package/db/migrations/111_sql_diagram.sql +24 -0
  5. package/db/migrations/113_rest_objects.sql +49 -0
  6. package/db/migrations/116_dump_jobs.sql +41 -0
  7. package/db/migrations/117_rest_handlers.sql +63 -0
  8. package/db/migrations/118_change_log.sql +54 -0
  9. package/docs/apex-feature-parity.md +11 -11
  10. package/docs/guide/01-installation.md +4 -0
  11. package/docs/guide/03-builder.md +267 -7
  12. package/docs/guide/08-security.md +29 -0
  13. package/docs/guide/12-development.md +70 -6
  14. package/docs/guide/13-rest-api.md +133 -10
  15. package/docs/guide/15-extensions.md +1 -1
  16. package/examples/hr/hr_62_rest_module.sql +39 -0
  17. package/package.json +1 -1
  18. package/public/app.css +1 -1
  19. package/public/builder-icons.svg +2 -0
  20. package/public/builder.css +442 -173
  21. package/public/builder.js +48 -0
  22. package/public/data-modeler.css +56 -0
  23. package/public/data-modeler.js +367 -0
  24. package/public/palette.js +133 -0
  25. package/public/sql-monitor.js +73 -0
  26. package/public/worksheet.css +49 -0
  27. package/public/worksheet.js +4 -0
  28. package/src/app.ts +10 -0
  29. package/src/automations.ts +6 -0
  30. package/src/binds.ts +2 -1
  31. package/src/builder/advisor.ts +24 -2
  32. package/src/builder/autorest.ts +363 -0
  33. package/src/builder/changelog.ts +240 -0
  34. package/src/builder/components.ts +1 -1
  35. package/src/builder/data-modeler.ts +382 -0
  36. package/src/builder/datapump.ts +364 -0
  37. package/src/builder/db-users.ts +472 -0
  38. package/src/builder/ddl-tools.ts +13 -4
  39. package/src/builder/find-workspace.ts +2 -2
  40. package/src/builder/launchpad.ts +140 -0
  41. package/src/builder/object-actions.ts +1 -0
  42. package/src/builder/object-browser.ts +7 -1
  43. package/src/builder/plan-view.ts +25 -0
  44. package/src/builder/rest-modules.ts +629 -0
  45. package/src/builder/rest.ts +1 -0
  46. package/src/builder/routes.ts +4 -2
  47. package/src/builder/scheduling.ts +495 -0
  48. package/src/builder/sql-charts.ts +203 -0
  49. package/src/builder/sql-monitor.ts +397 -0
  50. package/src/builder/sql-tools.ts +53 -0
  51. package/src/builder/sql-utilities.ts +7 -39
  52. package/src/builder/sql.ts +20 -9
  53. package/src/builder/table-editor.ts +4 -1
  54. package/src/builder/top-sql.ts +24 -6
  55. package/src/builder/ui.ts +25 -17
  56. package/src/builder/worksheet.ts +35 -28
  57. package/src/builder/workspaces.ts +4 -2
  58. package/src/changelog.ts +109 -0
  59. package/src/cli/replace.ts +3 -1
  60. package/src/cron-schedule.ts +282 -0
  61. package/src/datamodel.ts +324 -0
  62. package/src/datapump.ts +546 -0
  63. package/src/dbroles.ts +215 -0
  64. package/src/dumpscript.ts +407 -0
  65. package/src/pgcron.ts +225 -0
  66. package/src/runtime/autorest.ts +336 -0
  67. package/src/runtime/rest-handlers.ts +75 -0
  68. package/src/runtime/rest.ts +120 -26
  69. package/src/session.ts +2 -0
@@ -0,0 +1,140 @@
1
+ import type { FastifyInstance, FastifyReply } from 'fastify';
2
+ import { owner } from '../db.ts';
3
+ import { html, type Raw } from '../html.ts';
4
+ import { icon } from '../icons.ts';
5
+ import type { Session } from '../session.ts';
6
+ import { back, BASE, csrf, designerUrl, developer, flash, send, shell, type Req } from './ui.ts';
7
+ import { SQL_TOOLS, TOOL_GROUPS } from './sql-tools.ts';
8
+ import { SYSTEM_SCHEMAS } from './object-browser.ts';
9
+ import { currentWorkspace, inWorkspaceSql, workshopDb } from './workspaces.ts';
10
+ import { isAdmin } from './locks.ts';
11
+
12
+ // (sprint 96, Database Actions DA-15) The SQL Workshop's Launchpad and the builder's search.
13
+ // - The home page starts with Pinned (per developer and workspace, meta.builder_pin) and Recently visited (kept
14
+ // in the browser by public/palette.js), then the tools in tabs: Development, Administration, Monitoring, each
15
+ // with a line of description and a pin.
16
+ // - POST /builder/pins pins or unpins a builder page (a tool, an object, a saved worksheet).
17
+ // - GET /builder/palette?q= finds tools, applications, pages and database objects (tables, views, functions) by
18
+ // name: JSON for the Ctrl+K search, or a page of results without JavaScript (the header's search field).
19
+
20
+ const MAX_PINS = 24;
21
+ const HREF_RE = /^\/builder\/[A-Za-z0-9/_\-.~%]*(\?[A-Za-z0-9_\-.~%=&+]*)?$/;
22
+
23
+ export interface Pin { href: string; label: string; icon: string }
24
+
25
+ export async function pinsOf(s: Session): Promise<Pin[]> {
26
+ return (await owner.query<Pin>(`select href, label, icon from meta.builder_pin where username = $1 and workspace_id = $2 order by at`, [s.username, currentWorkspace(s)])).rows;
27
+ }
28
+
29
+ /** A pin toggle (a small form): pinned → unpin, else pin. */
30
+ export function pinButton(s: Session, p: Pin, pinned: boolean, backTo: string): Raw {
31
+ return html`<form method="post" action="${BASE}/pins" class="pin-form">${csrf(s)}
32
+ <input type="hidden" name="href" value="${p.href}"><input type="hidden" name="label" value="${p.label}"><input type="hidden" name="icon" value="${p.icon}">
33
+ <input type="hidden" name="pin" value="${pinned ? '0' : '1'}"><input type="hidden" name="back" value="${backTo}">
34
+ <button class="pin-btn${pinned ? ' is-pinned' : ''}" title="${pinned ? 'Unpin' : 'Pin to the SQL Workshop home'}" aria-pressed="${pinned ? 'true' : 'false'}">${icon('pin')}<span class="sr-only">${pinned ? 'Unpin' : 'Pin'} ${p.label}</span></button></form>`;
35
+ }
36
+
37
+ /** The Launchpad part of the SQL Workshop home. */
38
+ export async function launchpad(s: Session, admin: boolean): Promise<Raw> {
39
+ const pins = await pinsOf(s);
40
+ const pinned = new Set(pins.map((p) => p.href));
41
+ const tools = SQL_TOOLS.filter((t) => admin || !t.admin);
42
+ const card = (t: (typeof SQL_TOOLS)[number]) => html`<li class="lp-tool">
43
+ <a href="${t.href}">${icon(t.icon)}<span><strong>${t.label}</strong><small class="muted">${t.description}</small></span></a>
44
+ ${pinButton(s, { href: t.href, label: t.label, icon: t.icon }, pinned.has(t.href), `${BASE}/sql`)}</li>`;
45
+ return html`<section class="lp-pins" aria-labelledby="lp-pins-title">
46
+ <h2 id="lp-pins-title">Pinned &amp; recently visited</h2>
47
+ ${pins.length ? html`<ul class="lp-chips">${pins.map((p) => html`<li><a href="${p.href}">${icon(p.icon)}<span>${p.label}</span></a>${pinButton(s, p, true, `${BASE}/sql`)}</li>`)}</ul>`
48
+ : html`<p class="muted small">Pin a tool, an object or a saved worksheet with ${icon('pin')} to keep it here.</p>`}
49
+ <ul class="lp-chips lp-recent" data-lp-recent aria-label="Recently visited" hidden></ul>
50
+ </section>
51
+ <div class="lp-groups">
52
+ <div class="lp-tabs" role="tablist" aria-label="Tools" hidden>${TOOL_GROUPS.map(([g, label], i) => html`<button type="button" role="tab" aria-selected="${i === 0 ? 'true' : 'false'}" aria-controls="lp-${g}" data-lp-tab="${g}">${label}</button>`)}</div>
53
+ ${TOOL_GROUPS.map(([g, label]) => html`<section class="lp-group" id="lp-${g}" data-lp-group="${g}" aria-label="${label}"><h2>${label}</h2><ul class="lp-list">${tools.filter((t) => t.group === g).map(card)}</ul></section>`)}
54
+ </div>`;
55
+ }
56
+
57
+ interface Result { kind: string; label: string; detail: string; href: string; icon: string }
58
+
59
+ /** What the builder's search finds for `q`. */
60
+ async function search(s: Session, q: string, admin: boolean): Promise<Result[]> {
61
+ const needle = q.toLowerCase();
62
+ const like = `%${q.replace(/[\\%_]/g, (c) => `\\${c}`)}%`;
63
+ const out: Result[] = SQL_TOOLS.filter((t) => (admin || !t.admin) && `${t.label} ${t.description}`.toLowerCase().includes(needle))
64
+ .map((t) => ({ kind: 'Tool', label: t.label, detail: t.description, href: t.href, icon: t.icon }));
65
+ const ws = currentWorkspace(s);
66
+ const apps = (await owner.query<{ id: number; name: string; alias: string }>(
67
+ `select a.id, a.name, a.alias from meta.app a where ${inWorkspaceSql('a', 1)} and (a.name ilike $2 or a.alias ilike $2) order by a.name limit 8`, [ws, like])).rows;
68
+ out.push(...apps.map((a) => ({ kind: 'Application', label: a.name, detail: a.alias, href: `${BASE}/apps/${a.id}`, icon: 'grid' })));
69
+ const pages = (await owner.query<{ app_id: number; page_no: number; name: string; app: string }>(
70
+ `select p.app_id, p.page_no, p.name, a.name as app from meta.page p join meta.app a on a.id = p.app_id
71
+ where ${inWorkspaceSql('a', 1)} and (p.name ilike $2 or p.page_no::text = $3) order by a.name, p.page_no limit 12`, [ws, like, q.trim()])).rows;
72
+ out.push(...pages.map((p) => ({ kind: 'Page', label: `${p.page_no}: ${p.name}`, detail: p.app, href: designerUrl(p.app_id, p.page_no), icon: 'file' })));
73
+ // a workspace without the SQL Workshop (its preference) finds no database objects
74
+ if (s.workspace?.sql_workshop === false) return out.filter((r) => r.kind !== 'Tool' || !r.href.startsWith(`${BASE}/sql`)).slice(0, 40);
75
+ const db = await workshopDb(s);
76
+ const objects = (await db.query<{ k: string; schema: string; name: string; ref: string }>(
77
+ `select * from (
78
+ select case c.relkind when 'v' then 'view' when 'm' then 'matview' else 'table' end as k, n.nspname as schema, c.relname as name, c.oid::regclass::text as ref
79
+ from pg_class c join pg_namespace n on n.oid = c.relnamespace
80
+ where c.relkind in ('r', 'p', 'v', 'm') and not c.relispartition and c.relname ilike $1
81
+ union all
82
+ select case p.prokind when 'p' then 'procedure' else 'function' end, n.nspname, p.proname || '(' || pg_get_function_identity_arguments(p.oid) || ')', p.oid::regprocedure::text
83
+ from pg_proc p join pg_namespace n on n.oid = p.pronamespace
84
+ where p.prokind in ('f', 'p') and p.proname ilike $1
85
+ ) x join pg_namespace n on n.nspname = x.schema
86
+ where x.schema !~ '^pg_' and x.schema <> 'information_schema' and not (x.schema = any ($2)) and has_schema_privilege(n.oid, 'USAGE')
87
+ and not exists (select 1 from pg_extension e join pg_depend d on d.refobjid = e.oid and d.deptype = 'e' where d.objid = case when x.k in ('function', 'procedure') then x.ref::regprocedure::oid else x.ref::regclass::oid end)
88
+ order by length(x.name), x.name limit 15`, [like, SYSTEM_SCHEMAS]).catch(() => ({ rows: [] }))).rows;
89
+ const KIND: Record<string, [string, string]> = { table: ['Table', 'table'], view: ['View', 'layers'], matview: ['Materialized view', 'layers'], function: ['Function', 'code'], procedure: ['Procedure', 'code'] };
90
+ out.push(...objects.map((o) => ({ kind: KIND[o.k][0], label: o.name, detail: o.schema, href: `${BASE}/sql/objects?${new URLSearchParams({ schema: o.schema, k: o.k, o: o.ref })}`, icon: KIND[o.k][1] })));
91
+ return out.slice(0, 40);
92
+ }
93
+
94
+ export async function launchpadRoutes(app: FastifyInstance) {
95
+ app.post(`${BASE}/pins`, async (req: Req, reply: FastifyReply) => {
96
+ const s = await developer(req, reply);
97
+ if (!s) return;
98
+ const b = req.body ?? {};
99
+ const href = String(b.href ?? b.pin_href ?? '');
100
+ const label = String(b.label ?? b.pin_label ?? '').trim().slice(0, 120);
101
+ const ic = /^[a-z0-9-]{1,40}$/.test(String(b.icon ?? '')) ? String(b.icon) : b.pin_href ? 'code' : 'bookmark';
102
+ const backTo = HREF_RE.test(String(b.back ?? '')) ? String(b.back) : `${BASE}/sql`;
103
+ if (!HREF_RE.test(href) || href.length > 400 || !label) return reply.code(400).send('Not a builder page');
104
+ const ws = currentWorkspace(s);
105
+ if (String(b.pin ?? '1') === '0') {
106
+ await owner.query('delete from meta.builder_pin where username = $1 and workspace_id = $2 and href = $3', [s.username, ws, href]);
107
+ flash(s, `Unpinned ${label}.`);
108
+ } else {
109
+ const n = Number((await owner.one<{ n: string }>('select count(*) as n from meta.builder_pin where username = $1 and workspace_id = $2', [s.username, ws]))?.n ?? 0);
110
+ if (n >= MAX_PINS) flash(s, `At most ${MAX_PINS} pins: unpin one first.`, 'error');
111
+ else {
112
+ await owner.query(
113
+ `insert into meta.builder_pin (username, workspace_id, href, label, icon) values ($1, $2, $3, $4, $5)
114
+ on conflict (username, workspace_id, href) do update set label = excluded.label, icon = excluded.icon`, [s.username, ws, href, label, ic]);
115
+ flash(s, `Pinned ${label} to the SQL Workshop home.`);
116
+ }
117
+ }
118
+ return back(reply, s, backTo);
119
+ });
120
+
121
+ app.get(`${BASE}/palette`, async (req: Req, reply: FastifyReply) => {
122
+ const s = await developer(req, reply);
123
+ if (!s) return;
124
+ const q = String(req.query?.q ?? '').trim().slice(0, 100);
125
+ const admin = await isAdmin(s.username);
126
+ const results = q ? await search(s, q, admin) : [];
127
+ if (req.query?.format === 'json') return reply.header('Cache-Control', 'private, no-store').send({ q, results });
128
+ const main = html`<h1 class="u-mb1">Search</h1>
129
+ <form method="get" action="${BASE}/palette" class="search u-mb1" role="search"><label class="sr-only" for="pal-q">Search</label>
130
+ <input id="pal-q" name="q" type="search" value="${q}" placeholder="Tables, views, functions, applications, pages, tools" autofocus><button class="btn">${icon('search')} Search</button></form>
131
+ ${q ? results.length ? html`<ul class="pal-results">${results.map((r) => html`<li><a href="${r.href}">${icon(r.icon)}<span><strong>${r.label}</strong> <small class="muted">${r.kind} · ${r.detail}</small></span></a></li>`)}</ul>`
132
+ : html`<p class="muted">Nothing found for “${q}”.</p>` : html`<p class="muted">Search by name. Ctrl+K opens this search on every builder page.</p>`}`;
133
+ return send(reply, s, shell(s, 'Search', [['Search']], main, 'apps'));
134
+ });
135
+ }
136
+
137
+ /** For pages that show a pin for themselves (an object, a worksheet). */
138
+ export async function isPinned(s: Session, href: string) {
139
+ return !!(await owner.one('select 1 as ok from meta.builder_pin where username = $1 and workspace_id = $2 and href = $3', [s.username, currentWorkspace(s), href]));
140
+ }
@@ -32,6 +32,7 @@ export function objectMenu(schema: string, o: MenuObj): Raw {
32
32
  ${table ? item(`${BASE}/sql/objects/table?${new URLSearchParams({ schema, template: o.ref })}`, 'copy', 'Use as template') : ''}
33
33
  ${table ? item(`${BASE}/sql/load`, 'upload', 'Load data') : ''}
34
34
  ${table ? html`${item(q('index'), 'list', 'Add index')}${item(q('trigger'), 'bolt', 'Add trigger')}${item(q('policy'), 'shield', 'Add policy')}` : ''}
35
+ ${RELATIONS.has(o.type) ? item(`${BASE}/sql/rest/object?${new URLSearchParams({ schema, name: o.name })}`, 'globe', 'REST › Enable / Disable') : ''}
35
36
  ${item(q('ddl'), 'code', 'Quick DDL to the worksheet')}
36
37
  ${item(q('download'), 'download', 'Quick DDL as a file')}
37
38
  ${item(q('drop'), 'trash', 'Drop…')}
@@ -6,6 +6,7 @@ import { readSchema } from '../ddl.ts';
6
6
  import { back, BASE, csrf, developer, flash, region, send, shell, workshopTabs, type Req } from './ui.ts';
7
7
  import { currentWorkspace, inWorkspaceSql, sessionSchema, workshopDb } from './workspaces.ts';
8
8
  import { objectMenu } from './object-actions.ts';
9
+ import { isPinned, pinButton } from './launchpad.ts';
9
10
  import { owner } from '../db.ts';
10
11
  import type { Session } from '../session.ts';
11
12
 
@@ -142,7 +143,12 @@ export async function objectBrowserRoutes(app: FastifyInstance) {
142
143
 
143
144
  let detail: Raw;
144
145
  if (!schema) detail = html`<p class="muted">No schema you may use.</p>`;
145
- else if (selected) detail = await objectDetail(db, s, schema, selected, String(q.tab ?? ''), Number(q.p) || 1, link);
146
+ else if (selected) {
147
+ // (sprint 96) pin the object to the SQL Workshop's home
148
+ const href = `${BASE}/sql/objects?${new URLSearchParams({ schema, k: selected.type, o: selected.ref })}`;
149
+ const pin = pinButton(s, { href, label: selected.name, icon: OB_TYPES.find(([t]) => t === selected.type)![2] }, await isPinned(s, href), href);
150
+ detail = html`<div class="ob-pin">${pin}</div>${await objectDetail(db, s, schema, selected, String(q.tab ?? ''), Number(q.p) || 1, link)}`;
151
+ }
146
152
  else
147
153
  detail = region(`Create database objects in ${schema}`, html`<div class="ob-tiles">
148
154
  <a class="create-source" href="${BASE}/sql/objects/table?${new URLSearchParams({ schema })}">${icon('table')}<span><b>Table (form)</b><small class="muted">Columns, keys, indexes and security in a form</small></span></a>${[
@@ -0,0 +1,25 @@
1
+ import { html, raw, type Raw } from '../html.ts';
2
+ import type { Plan, PlanNode } from '../sqlplan.ts';
3
+
4
+ // (sprint 92, DA-4; shared since sprint 102) A query plan (src/sqlplan.ts) drawn as a tree: the SQL Worksheet's
5
+ // Explain Plan and Autotrace tabs and the Database Monitor's statement page. Styles: .ws-plan-* in worksheet.css.
6
+
7
+ const fmtMs = (n: number) => (n >= 100 ? n.toFixed(0) : n >= 1 ? n.toFixed(1) : n.toFixed(3));
8
+
9
+ /** (sprint 92, DA-4) A plan as a tree of steps (open), with the text plan behind a toggle. */
10
+ export function planView(plan: Plan, text: string): Raw {
11
+ const step = (n: PlanNode): Raw => html`<li><details open${n.slowest ? raw(' class="is-slowest"') : ''}>
12
+ <summary><span class="ws-plan-op">${n.op}</span>${n.target ? html` <span class="ws-plan-target">${n.target}</span>` : ''}
13
+ <span class="ws-plan-nums">cost ${n.startupCost.toFixed(2)}..${n.totalCost.toFixed(2)} · rows ${n.planRows} · width ${n.width}${plan.analyze
14
+ ? n.neverRun ? html` · <em>never executed</em>` : html` · <strong>${fmtMs(n.actualTotal ?? 0)} ms</strong> × ${n.loops} · ${n.actualRows} row${n.actualRows === 1 ? '' : 's'}${n.selfMs !== undefined ? ` · self ${fmtMs(n.selfMs)} ms` : ''}${n.sharedHit || n.sharedRead ? ` · buffers ${n.sharedHit ?? 0} hit${n.sharedRead ? `, ${n.sharedRead} read` : ''}` : ''}`
15
+ : ''}</span>
16
+ ${n.slowest ? html`<span class="tag tag-warn">slowest step</span>` : ''}
17
+ ${n.misestimate ? html`<span class="tag tag-warn" title="Estimated ${n.planRows} rows, got ${n.actualRows}: ANALYZE the table, or look at the statistics">estimate ${n.misestimate >= 1 ? `${Math.round(n.misestimate)}× too high` : `${Math.round(1 / n.misestimate)}× too low`}</span>` : ''}
18
+ </summary>
19
+ ${n.details.length ? html`<dl class="ws-plan-details">${n.details.map(([k, v]) => html`<dt>${k}</dt><dd><code>${v}</code></dd>`)}</dl>` : ''}
20
+ ${n.children.length ? html`<ul class="ws-plan-tree">${n.children.map(step)}</ul>` : ''}
21
+ </details></li>`;
22
+ return html`${plan.planningMs !== undefined || plan.executionMs !== undefined ? html`<p class="muted small ws-info">${plan.planningMs !== undefined ? `Planning ${fmtMs(plan.planningMs)} ms` : ''}${plan.executionMs !== undefined ? ` · execution ${fmtMs(plan.executionMs)} ms (rolled back)` : ''}</p>` : ''}
23
+ <ul class="ws-plan-tree ws-plan-root">${step(plan.root)}</ul>
24
+ <details class="ws-plan-text"><summary>Text plan</summary><pre class="ws-log ws-plan">${text}</pre></details>`;
25
+ }