pgkiln 0.31.0 → 0.36.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 (131) hide show
  1. package/README.md +2 -2
  2. package/SECURITY.md +10 -0
  3. package/db/migrations/076_form_lost_update.sql +50 -0
  4. package/db/migrations/077_warn_unsaved.sql +41 -0
  5. package/db/migrations/078_url_region.sql +12 -0
  6. package/db/migrations/079_developer_toolbar.sql +53 -0
  7. package/db/migrations/080_search.sql +326 -0
  8. package/db/migrations/081_mail.sql +621 -0
  9. package/db/migrations/082_app_backups.sql +73 -0
  10. package/db/migrations/083_availability.sql +156 -0
  11. package/db/migrations/084_feedback.sql +46 -0
  12. package/db/migrations/085_page_behaviour.sql +65 -0
  13. package/db/migrations/086_data_settings.sql +599 -0
  14. package/db/migrations/087_login_page.sql +175 -0
  15. package/db/migrations/088_region_templates.sql +18 -0
  16. package/db/migrations/089_page_groups.sql +330 -0
  17. package/docs/apex-feature-parity.md +61 -39
  18. package/docs/guide/01-installation.md +1 -0
  19. package/docs/guide/03-builder.md +154 -4
  20. package/docs/guide/04-pages-and-regions.md +157 -3
  21. package/docs/guide/05-items.md +47 -0
  22. package/docs/guide/06-processing.md +210 -2
  23. package/docs/guide/07-dynamic-actions.md +60 -2
  24. package/docs/guide/08-security.md +56 -4
  25. package/docs/guide/09-reference.md +21 -0
  26. package/docs/guide/11-from-apex.md +9 -2
  27. package/docs/guide/12-development.md +40 -2
  28. package/docs/guide/14-globalization.md +15 -0
  29. package/docs/guide/15-extensions.md +1 -1
  30. package/docs/guide/17-mobile.md +3 -1
  31. package/docs/guide/18-cli.md +32 -1
  32. package/docs/guide/20-ai-agents.md +5 -2
  33. package/examples/hr/hr_50_search.sql +47 -0
  34. package/examples/hr/hr_51_mail.sql +70 -0
  35. package/examples/hr/hr_52_feedback.sql +15 -0
  36. package/examples/hr/hr_53_data_settings.sql +108 -0
  37. package/examples/hr/hr_54_region_templates.sql +67 -0
  38. package/examples/hr/hr_55_page_groups.sql +24 -0
  39. package/package.json +3 -1
  40. package/public/app.css +122 -3
  41. package/public/app.js +496 -13
  42. package/public/builder.css +22 -0
  43. package/public/builder.js +77 -0
  44. package/public/code-editor.css +3 -3
  45. package/public/sw.js +5 -3
  46. package/src/app.ts +10 -0
  47. package/src/appfiles.ts +13 -0
  48. package/src/automations.ts +12 -0
  49. package/src/builder/advisor.ts +68 -14
  50. package/src/builder/arrange.ts +2 -2
  51. package/src/builder/availability.ts +74 -0
  52. package/src/builder/backups.ts +134 -0
  53. package/src/builder/components.ts +123 -16
  54. package/src/builder/ddl-tools.ts +114 -0
  55. package/src/builder/designer.ts +35 -4
  56. package/src/builder/feedback.ts +82 -0
  57. package/src/builder/forms.ts +10 -5
  58. package/src/builder/home.ts +3 -0
  59. package/src/builder/locks.ts +15 -5
  60. package/src/builder/login-page.ts +82 -0
  61. package/src/builder/mail.ts +425 -0
  62. package/src/builder/newapp.ts +1 -1
  63. package/src/builder/region-help.ts +73 -0
  64. package/src/builder/region-settings.ts +19 -3
  65. package/src/builder/routes.ts +60 -5
  66. package/src/builder/search.ts +4 -4
  67. package/src/builder/shared.ts +3 -2
  68. package/src/builder/sql.ts +3 -0
  69. package/src/builder/ui.ts +9 -3
  70. package/src/cli/apps.ts +46 -5
  71. package/src/cli/main.ts +66 -5
  72. package/src/cli/mcp.ts +58 -4
  73. package/src/cli/replace.ts +40 -4
  74. package/src/ddl.ts +509 -0
  75. package/src/geocode.ts +44 -0
  76. package/src/i18n/ar.ts +56 -0
  77. package/src/i18n/cs.ts +56 -0
  78. package/src/i18n/da.ts +56 -0
  79. package/src/i18n/de.ts +56 -0
  80. package/src/i18n/el.ts +56 -0
  81. package/src/i18n/es.ts +56 -0
  82. package/src/i18n/fi.ts +56 -0
  83. package/src/i18n/fr.ts +56 -0
  84. package/src/i18n/he.ts +56 -0
  85. package/src/i18n/it.ts +56 -0
  86. package/src/i18n/ja.ts +56 -0
  87. package/src/i18n/ko.ts +56 -0
  88. package/src/i18n/nb.ts +56 -0
  89. package/src/i18n/pl.ts +56 -0
  90. package/src/i18n/pt.ts +56 -0
  91. package/src/i18n/ru.ts +56 -0
  92. package/src/i18n/sv.ts +56 -0
  93. package/src/i18n/tr.ts +56 -0
  94. package/src/i18n/uk.ts +56 -0
  95. package/src/i18n/zh.ts +56 -0
  96. package/src/i18n.ts +112 -0
  97. package/src/icons.ts +2 -1
  98. package/src/layout.ts +3 -2
  99. package/src/mail/mail.ts +148 -0
  100. package/src/mail/providers.ts +444 -0
  101. package/src/metadata.ts +34 -10
  102. package/src/runtime/ajax.ts +97 -0
  103. package/src/runtime/authz.ts +1 -1
  104. package/src/runtime/availability.ts +37 -0
  105. package/src/runtime/context.ts +46 -6
  106. package/src/runtime/data-export.ts +94 -0
  107. package/src/runtime/data-items.ts +194 -0
  108. package/src/runtime/devbar.ts +184 -0
  109. package/src/runtime/display-selector.ts +12 -4
  110. package/src/runtime/engine.ts +26 -1
  111. package/src/runtime/feedback.ts +128 -0
  112. package/src/runtime/grid.ts +170 -26
  113. package/src/runtime/items.ts +8 -0
  114. package/src/runtime/lists.ts +11 -1
  115. package/src/runtime/password-reset.ts +171 -0
  116. package/src/runtime/processes.ts +83 -2
  117. package/src/runtime/pwa.ts +7 -10
  118. package/src/runtime/regions.ts +41 -0
  119. package/src/runtime/render.ts +48 -7
  120. package/src/runtime/report-views.ts +4 -2
  121. package/src/runtime/report.ts +36 -22
  122. package/src/runtime/rest.ts +4 -0
  123. package/src/runtime/routes.ts +94 -37
  124. package/src/runtime/search.ts +252 -0
  125. package/src/runtime/signin.ts +80 -0
  126. package/src/runtime/url-region.ts +93 -0
  127. package/src/security.ts +11 -1
  128. package/src/server.ts +2 -0
  129. package/src/session.ts +8 -2
  130. package/src/static-version.ts +34 -0
  131. package/src/workingcopy.ts +2 -2
@@ -22,9 +22,17 @@ export const REPLACED = [
22
22
  'list', 'list_entry', 'supporting_script',
23
23
  // (068) static application files, (069) plug-ins
24
24
  'static_file', 'plugin',
25
+ // (080) search configurations, (081) e-mail templates
26
+ 'search_config', 'email_template',
27
+ // (086) application settings (installed values kept, see below), dynamic translations
28
+ 'app_setting', 'dynamic_translation',
29
+ // (089) page groups
30
+ 'page_group',
25
31
  ];
26
32
  /** Tables of an application that belong to the installation: kept. */
27
33
  export const KEPT = ['app_access', 'api_client', 'session', 'sso_pending', 'saved_report', 'persistent_login', 'task', 'workflow', 'process_job',
34
+ // (087) password reset links of this installation's accounts
35
+ 'password_reset',
28
36
  // (042) builder state of this installation: locks and developer comments (by page number)
29
37
  'builder_lock', 'dev_comment',
30
38
  // (051) debug messages recorded by this installation
@@ -48,7 +56,13 @@ export const KEPT = ['app_access', 'api_client', 'session', 'sso_pending', 'save
48
56
  // (064) the workspace of the application
49
57
  'workspace_app',
50
58
  // (074) push notifications: the app's VAPID keys, users' devices, queued notifications
51
- 'push_key', 'push_subscription', 'push_message'];
59
+ 'push_key', 'push_subscription', 'push_message',
60
+ // (082) the application's backups, (084) users' feedback
61
+ 'app_backup', 'feedback',
62
+ // (081) the mail service an application sends through, and its queued and sent mail
63
+ 'app_mail_service', 'mail_queue',
64
+ // (086) users' preferences
65
+ 'user_preference'];
52
66
  /** Children of pages (replaced with their page). */
53
67
  const PAGE_CHILDREN = ['region', 'item', 'button', 'dynamic_action', 'validation', 'process', 'computation', 'branch'];
54
68
  /** References into replaced tables from kept data, repointed below: "table.column". */
@@ -85,12 +99,23 @@ export async function checkSchema(db: Db) {
85
99
  if (problems.length) throw new Error(`replace does not know these tables yet; update src/cli/replace.ts: ${problems.join(', ')}`);
86
100
  }
87
101
 
88
- /** Replace the components of application `alias` with those of doc. Returns the application id. */
89
- export async function replaceApp(db: Db, doc: unknown, alias: string): Promise<number> {
102
+ /** Why the replaced version is backed up (082); null: no backup (a new application). */
103
+ export interface ReplaceOptions {
104
+ backup?: { reason: 'import' | 'merge' | 'restore'; user?: string | null; note?: string | null } | null;
105
+ }
106
+
107
+ /**
108
+ * Replace the components of application `alias` with those of doc. Returns the
109
+ * application id. The version it replaces is backed up first (meta.backup_app,
110
+ * migration 082) unless opts.backup is null.
111
+ */
112
+ export async function replaceApp(db: Db, doc: unknown, alias: string, opts: ReplaceOptions = {}): Promise<number> {
90
113
  await checkSchema(db);
91
114
  const target = (await db.query<{ id: number }>('select id from meta.app where alias = $1 for update', [alias])).rows[0];
92
115
  if (!target) throw new Error(`application ${alias} not found`);
93
116
  const old = target.id;
117
+ const backup = opts.backup === undefined ? { reason: 'import' as const } : opts.backup;
118
+ if (backup) await db.query('select meta.backup_app($1, $2, $3, $4)', [old, backup.reason, backup.user ?? null, backup.note ?? null]);
94
119
  const tmpAlias = `pgkiln-replace-${randomBytes(6).toString('hex')}`;
95
120
  const neu = (await db.query<{ id: number }>('select meta.import_app($1::jsonb, $2) as id', [JSON.stringify(doc), tmpAlias])).rows[0].id;
96
121
 
@@ -131,6 +156,15 @@ export async function replaceApp(db: Db, doc: unknown, alias: string): Promise<n
131
156
  from meta.automation o where o.app_id = $1 and n.app_id = $2 and n.name = o.name`,
132
157
  [old, neu],
133
158
  );
159
+ // (086) application settings keep the installed value (keep_on_upgrade), when it is still valid
160
+ await db.query(
161
+ `update meta.app_setting n set value = o.value
162
+ from meta.app_setting o
163
+ where o.app_id = $1 and n.app_id = $2 and n.name = o.name and n.keep_on_upgrade
164
+ and (o.value is null or n.valid_values is null or o.value = any(n.valid_values))
165
+ and (not n.required or coalesce(o.value, '') <> '')`,
166
+ [old, neu],
167
+ );
134
168
  const regions = async (appId: number) => {
135
169
  const rows = (
136
170
  await db.query<{ id: number; page_no: number; title: string; type: string; static_id: string | null }>(
@@ -174,7 +208,9 @@ export async function replaceApp(db: Db, doc: unknown, alias: string): Promise<n
174
208
  const cols = (
175
209
  await db.query<{ c: string }>(
176
210
  `select quote_ident(column_name) as c from information_schema.columns
177
- where table_schema = 'meta' and table_name = 'app' and column_name not in ('id', 'alias', 'created_at', 'updated_at')
211
+ where table_schema = 'meta' and table_name = 'app'
212
+ -- (083) availability and build status belong to this installation
213
+ and column_name not in ('id', 'alias', 'created_at', 'updated_at', 'availability', 'availability_message', 'availability_users', 'build_status')
178
214
  order by ordinal_position`,
179
215
  )
180
216
  ).rows.map((r) => r.c);
package/src/ddl.ts ADDED
@@ -0,0 +1,509 @@
1
+ import pg from 'pg';
2
+
3
+ // DDL of one schema, read from the catalog (APEX: SQL Workshop → Utilities →
4
+ // Generate DDL), and the comparison of two schemas (Schema Comparison).
5
+ //
6
+ // Every object becomes one entry: its kind, a key (kind and name, a
7
+ // function's name with its argument types, a constraint's or policy's table
8
+ // and name) and its DDL. readSchema() runs with search_path set to the
9
+ // schema: the server's pg_get_*def functions then leave the schema's own
10
+ // names unqualified, and so does this code, so the DDL of schema A also
11
+ // creates the same objects in schema B (with search_path = B), and two
12
+ // schemas compare by their DDL text. A script for one schema
13
+ // (ddlScript) puts the schema in search_path at its top.
14
+ //
15
+ // The order of a script: types and domains, sequences, procedural functions
16
+ // (check_function_bodies off), tables, SQL-standard functions (their bodies
17
+ // are parsed at once, so after the tables), views and materialized views in
18
+ // dependency order, constraints, foreign keys, indexes, triggers, row level
19
+ // security and policies, sequence ownership, comments, grants.
20
+ //
21
+ // Objects that belong to an extension are left out. Read-only: nothing is run.
22
+
23
+ export type Kind =
24
+ | 'type' | 'domain' | 'sequence' | 'table' | 'function' | 'view' | 'materialized view'
25
+ | 'constraint' | 'foreign key' | 'index' | 'trigger' | 'rls' | 'policy' | 'sequence owner' | 'comment' | 'grant';
26
+
27
+ export const KINDS: Kind[] = ['type', 'domain', 'sequence', 'table', 'function', 'view', 'materialized view', 'constraint', 'foreign key', 'index', 'trigger', 'rls', 'policy', 'sequence owner', 'comment', 'grant'];
28
+
29
+ /** The object types a developer picks (comments and grants are options of their own). */
30
+ export const OBJECT_TYPES = ['types', 'sequences', 'tables', 'views', 'functions', 'triggers', 'policies'] as const;
31
+ export type ObjectType = (typeof OBJECT_TYPES)[number];
32
+
33
+ const TYPE_OF: Record<Kind, ObjectType | 'comments' | 'grants'> = {
34
+ type: 'types', domain: 'types', sequence: 'sequences', 'sequence owner': 'sequences', table: 'tables', constraint: 'tables', 'foreign key': 'tables', index: 'tables',
35
+ function: 'functions', view: 'views', 'materialized view': 'views', trigger: 'triggers', rls: 'policies', policy: 'policies', comment: 'comments', grant: 'grants',
36
+ };
37
+
38
+ export interface DdlObject {
39
+ kind: Kind;
40
+ /** kind and name: unique in a schema, the same for the same object in another schema */
41
+ key: string;
42
+ /** the object's name as shown (a table, "table.constraint", "f(int)") */
43
+ name: string;
44
+ ddl: string;
45
+ /** the order within its kind (views: by dependency) */
46
+ order: number;
47
+ /** a table's columns, for column-level changes in a comparison */
48
+ columns?: Column[];
49
+ /** what a comparison looks at, when not the DDL itself (a table: its columns in name order) */
50
+ compareAs?: string;
51
+ /** the table a constraint, index, trigger, policy or comment belongs to */
52
+ table?: string;
53
+ }
54
+
55
+ interface Column {
56
+ name: string;
57
+ type: string;
58
+ notNull: boolean;
59
+ default: string | null;
60
+ definition: string;
61
+ }
62
+
63
+ // names are quoted like quote_ident(): only when needed (keywords from pg_get_keywords(), read once)
64
+ let keywords: Set<string> | null = null;
65
+ const id = (name: string) => (/^[a-z_][a-z0-9_$]*$/.test(name) && !keywords?.has(name) ? name : pg.escapeIdentifier(name));
66
+ const PLURAL: Partial<Record<Kind, string>> = { index: 'indexes', rls: 'row level security', policy: 'policies', 'sequence owner': 'sequence ownership', 'foreign key': 'foreign keys' };
67
+ const heading = (k: Kind) => PLURAL[k] ?? `${k}s`;
68
+ const lit = (s: string) => `'${s.replace(/'/g, "''")}'`;
69
+ const end = (s: string) => (s.trimEnd().endsWith(';') ? s.trimEnd() : `${s.trimEnd()};`);
70
+
71
+ /** The schemas a developer can pick: not PostgreSQL's own, not pgkiln's metadata. */
72
+ export async function userSchemas(c: { query: (sql: string) => Promise<{ rows: { name: string }[] }> }) {
73
+ const r = await c.query(
74
+ `select nspname as name from pg_namespace
75
+ where nspname !~ '^pg_' and nspname not in ('information_schema', 'meta')
76
+ and not exists (select 1 from pg_depend d where d.classid = 'pg_namespace'::regclass and d.objid = pg_namespace.oid and d.deptype = 'e')
77
+ order by 1`,
78
+ );
79
+ return r.rows.map((x) => x.name);
80
+ }
81
+
82
+ /** Every object of the schema (in a read-only transaction of its own: search_path is set for it). */
83
+ export async function readSchema(db: pg.Pool | pg.ClientBase, schema: string): Promise<DdlObject[]> {
84
+ const pooled = db instanceof pg.Pool;
85
+ const c = pooled ? await db.connect() : db;
86
+ try {
87
+ await c.query('begin read only');
88
+ keywords ??= new Set((await c.query<{ w: string }>(`select word as w from pg_get_keywords() where catcode <> 'U'`)).rows.map((x) => x.w));
89
+ const ns = (await c.query<{ oid: number }>('select oid from pg_namespace where nspname = $1', [schema])).rows[0];
90
+ if (!ns) throw new Error(`There is no schema "${schema}".`);
91
+ await c.query(`select set_config('search_path', $1, true)`, [`${id(schema)}, pg_catalog`]);
92
+ const out = await read(c, ns.oid, schema);
93
+ await c.query('rollback');
94
+ return out;
95
+ } catch (e) {
96
+ await c.query('rollback').catch(() => {});
97
+ throw e;
98
+ } finally {
99
+ if (pooled) (c as pg.PoolClient).release();
100
+ }
101
+ }
102
+
103
+ // not an extension's member
104
+ const notExt = (cls: string, col = 'oid') =>
105
+ `not exists (select 1 from pg_depend d where d.classid = '${cls}'::regclass and d.objid = ${col} and d.deptype = 'e')`;
106
+
107
+ async function read(c: pg.ClientBase, ns: number, schema: string): Promise<DdlObject[]> {
108
+ // pg_get_functiondef and pg_get_indexdef name the schema whatever search_path says: leave it out
109
+ const own = `${pg.escapeIdentifier(schema) === `"${schema}"` && /^[a-z_][a-z0-9_$]*$/.test(schema) ? schema : pg.escapeIdentifier(schema)}.`;
110
+ const unqualify = (sql: string, before: RegExp) => sql.replace(before, (m) => m.replace(own, ''));
111
+ const out: DdlObject[] = [];
112
+ const add = (o: Omit<DdlObject, 'order'> & { order?: number }) => out.push({ order: 0, ...o });
113
+ const q = async <T extends pg.QueryResultRow>(sql: string, params: unknown[] = [ns]) => (await c.query<T>(sql, params)).rows;
114
+
115
+ // ---- types: enums and composite types; domains
116
+ for (const t of await q<{ name: string; labels: string[] | null; attrs: string | null }>(
117
+ `select t.typname as name,
118
+ case when t.typtype = 'e' then array(select e.enumlabel::text from pg_enum e where e.enumtypid = t.oid order by e.enumsortorder) end as labels,
119
+ case when t.typtype = 'c' then (select string_agg(quote_ident(a.attname) || ' ' || format_type(a.atttypid, a.atttypmod), ', ' order by a.attnum)
120
+ from pg_attribute a where a.attrelid = t.typrelid and a.attnum > 0 and not a.attisdropped) end as attrs
121
+ from pg_type t
122
+ where t.typnamespace = $1 and (t.typtype = 'e' or (t.typtype = 'c' and (select relkind from pg_class where oid = t.typrelid) = 'c'))
123
+ and ${notExt('pg_type', 't.oid')}
124
+ order by 1`,
125
+ ))
126
+ add({ kind: 'type', key: `type:${t.name}`, name: t.name, ddl: t.labels ? `create type ${id(t.name)} as enum (${t.labels.map(lit).join(', ')});` : `create type ${id(t.name)} as (${t.attrs ?? ''});` });
127
+
128
+ for (const d of await q<{ name: string; base: string; collation: string | null; def: string | null; notnull: boolean; checks: string[] }>(
129
+ `select t.typname as name, format_type(t.typbasetype, t.typtypmod) as base,
130
+ case when t.typcollation <> 0 and t.typcollation <> (select typcollation from pg_type b where b.oid = t.typbasetype)
131
+ then (select quote_ident(collname) from pg_collation where oid = t.typcollation) end as collation,
132
+ t.typdefault as def, t.typnotnull as notnull,
133
+ array(select 'constraint ' || quote_ident(k.conname) || ' ' || pg_get_constraintdef(k.oid) from pg_constraint k where k.contypid = t.oid order by k.conname) as checks
134
+ from pg_type t where t.typnamespace = $1 and t.typtype = 'd' and ${notExt('pg_type', 't.oid')} order by 1`,
135
+ ))
136
+ add({
137
+ kind: 'domain', key: `domain:${d.name}`, name: d.name,
138
+ ddl: `create domain ${id(d.name)} as ${d.base}${d.collation ? ` collate ${d.collation}` : ''}${d.def !== null ? ` default ${d.def}` : ''}${d.notnull ? ' not null' : ''}${d.checks.map((k) => `\n ${k}`).join('')};`,
139
+ });
140
+
141
+ // ---- sequences: not those of identity columns (the column creates them)
142
+ for (const s of await q<{ name: string; type: string; inc: string; min: string; max: string; start: string; cache: string; cycle: boolean; owner: string | null }>(
143
+ `select c.relname as name, format_type(s.seqtypid, null) as type, s.seqincrement::text as inc, s.seqmin::text as min, s.seqmax::text as max,
144
+ s.seqstart::text as start, s.seqcache::text as cache, s.seqcycle as cycle,
145
+ (select quote_ident(t.relname) || '.' || quote_ident(a.attname)
146
+ from pg_depend d join pg_class t on t.oid = d.refobjid join pg_attribute a on a.attrelid = t.oid and a.attnum = d.refobjsubid
147
+ where d.classid = 'pg_class'::regclass and d.objid = c.oid and d.refclassid = 'pg_class'::regclass and d.deptype = 'a' and t.relnamespace = $1) as owner
148
+ from pg_class c join pg_sequence s on s.seqrelid = c.oid
149
+ where c.relnamespace = $1 and c.relkind = 'S' and ${notExt('pg_class', 'c.oid')}
150
+ and not exists (select 1 from pg_depend d where d.classid = 'pg_class'::regclass and d.objid = c.oid and d.deptype = 'i')
151
+ order by 1`,
152
+ )) {
153
+ add({ kind: 'sequence', key: `sequence:${s.name}`, name: s.name, ddl: `create sequence ${id(s.name)} as ${s.type} increment by ${s.inc} minvalue ${s.min} maxvalue ${s.max} start with ${s.start} cache ${s.cache}${s.cycle ? ' cycle' : ''};` });
154
+ if (s.owner) add({ kind: 'sequence owner', key: `sequence owner:${s.name}`, name: s.name, ddl: `alter sequence ${id(s.name)} owned by ${s.owner};` });
155
+ }
156
+
157
+ // ---- tables (partitioned ones before their partitions)
158
+ const tables = await q<{ oid: number; name: string; kind: string; unlogged: boolean; partkey: string | null; parent: string | null; bound: string | null; rls: boolean; force: boolean; options: string[] | null }>(
159
+ `select c.oid, c.relname as name, c.relkind as kind, c.relpersistence = 'u' as unlogged,
160
+ case when c.relkind = 'p' then pg_get_partkeydef(c.oid) end as partkey,
161
+ (select p.relname from pg_inherits i join pg_class p on p.oid = i.inhparent where i.inhrelid = c.oid and c.relispartition) as parent,
162
+ case when c.relispartition then pg_get_expr(c.relpartbound, c.oid) end as bound,
163
+ c.relrowsecurity as rls, c.relforcerowsecurity as force, c.reloptions as options
164
+ from pg_class c where c.relnamespace = $1 and c.relkind in ('r', 'p') and ${notExt('pg_class', 'c.oid')}
165
+ order by c.relispartition, c.relname`,
166
+ );
167
+ for (const t of tables) {
168
+ const cols = await q<{ name: string; type: string; notnull: boolean; def: string | null; identity: string; generated: string; collation: string | null; inherited: boolean }>(
169
+ `select a.attname as name, format_type(a.atttypid, a.atttypmod) as type, a.attnotnull as notnull,
170
+ pg_get_expr(d.adbin, d.adrelid) as def, a.attidentity as identity, a.attgenerated as generated, a.attislocal = false as inherited,
171
+ case when a.attcollation <> 0 and a.attcollation <> (select typcollation from pg_type where oid = a.atttypid)
172
+ then (select quote_ident(collname) from pg_collation where oid = a.attcollation) end as collation
173
+ from pg_attribute a left join pg_attrdef d on d.adrelid = a.attrelid and d.adnum = a.attnum
174
+ where a.attrelid = $1 and a.attnum > 0 and not a.attisdropped order by a.attnum`,
175
+ [t.oid],
176
+ );
177
+ const columns: Column[] = cols.map((x) => {
178
+ const def = x.generated === 's' ? null : x.def;
179
+ const parts = [
180
+ `${id(x.name)} ${x.type}`,
181
+ x.collation ? `collate ${x.collation}` : '',
182
+ x.generated === 's' ? `generated always as (${x.def}) stored` : '',
183
+ x.identity === 'a' ? 'generated always as identity' : x.identity === 'd' ? 'generated by default as identity' : '',
184
+ def !== null && !x.identity ? `default ${def}` : '',
185
+ x.notnull ? 'not null' : '',
186
+ ].filter(Boolean);
187
+ return { name: x.name, type: x.type, notNull: x.notnull, default: x.identity ? `identity ${x.identity}` : x.generated === 's' ? `generated ${x.def}` : def, definition: parts.join(' ') };
188
+ });
189
+ const opts = t.options?.length ? ` with (${t.options.join(', ')})` : '';
190
+ const ddl = t.parent
191
+ ? `create${t.unlogged ? ' unlogged' : ''} table ${id(t.name)} partition of ${id(t.parent)} ${t.bound}${t.partkey ? ` partition by ${t.partkey}` : ''}${opts};`
192
+ : `create${t.unlogged ? ' unlogged' : ''} table ${id(t.name)} (\n${columns.map((x) => ` ${x.definition}`).join(',\n')}\n)${t.partkey ? ` partition by ${t.partkey}` : ''}${opts};`;
193
+ // a comparison ignores the order of the columns
194
+ const compareAs = t.parent ? undefined : ddl.replace(/\(\n[\s\S]*\n\)/, `(\n${columns.map((x) => ` ${x.definition}`).sort().join(',\n')}\n)`);
195
+ add({ kind: 'table', key: `table:${t.name}`, name: t.name, ddl, columns: t.parent ? undefined : columns, compareAs });
196
+ if (t.rls || t.force)
197
+ add({ kind: 'rls', key: `rls:${t.name}`, name: t.name, table: t.name,
198
+ ddl: [t.rls ? `alter table ${id(t.name)} enable row level security;` : '', t.force ? `alter table ${id(t.name)} force row level security;` : ''].filter(Boolean).join('\n') });
199
+ }
200
+
201
+ // ---- constraints (a partition's inherited ones come with it)
202
+ for (const k of await q<{ table: string; name: string; type: string; def: string }>(
203
+ `select t.relname as table, k.conname as name, k.contype as type, pg_get_constraintdef(k.oid) as def
204
+ from pg_constraint k join pg_class t on t.oid = k.conrelid
205
+ where t.relnamespace = $1 and t.relkind in ('r', 'p') and k.contype in ('p', 'u', 'c', 'x', 'f') and k.conislocal and ${notExt('pg_class', 't.oid')}
206
+ and (k.conparentid = 0 or k.conparentid is null)
207
+ order by t.relname, k.contype, k.conname`,
208
+ ))
209
+ add({
210
+ kind: k.type === 'f' ? 'foreign key' : 'constraint', key: `${k.type === 'f' ? 'foreign key' : 'constraint'}:${k.table}.${k.name}`, name: `${k.table}.${k.name}`, table: k.table,
211
+ ddl: `alter table ${id(k.table)} add constraint ${id(k.name)} ${k.def};`,
212
+ });
213
+
214
+ // ---- indexes that no constraint makes, not on partitions (the partitioned table's index makes them)
215
+ for (const i of await q<{ table: string; name: string; def: string }>(
216
+ `select t.relname as table, ic.relname as name, pg_get_indexdef(ic.oid) as def
217
+ from pg_index x join pg_class ic on ic.oid = x.indexrelid join pg_class t on t.oid = x.indrelid
218
+ where t.relnamespace = $1 and t.relkind in ('r', 'p', 'm') and not exists (select 1 from pg_constraint k where k.conindid = ic.oid and k.contype in ('p', 'u', 'x'))
219
+ and not t.relispartition and ${notExt('pg_class', 't.oid')}
220
+ order by t.relname, ic.relname`,
221
+ ))
222
+ add({ kind: 'index', key: `index:${i.name}`, name: i.name, table: i.table, ddl: `${unqualify(i.def, / ON (ONLY )?\S+ USING /)};` });
223
+
224
+ // ---- functions and procedures (SQL-standard bodies after the tables)
225
+ for (const f of await q<{ sig: string; def: string; atomic: boolean }>(
226
+ `select p.proname || '(' || pg_get_function_identity_arguments(p.oid) || ')' as sig, pg_get_functiondef(p.oid) as def, p.prosqlbody is not null as atomic
227
+ from pg_proc p where p.pronamespace = $1 and p.prokind in ('f', 'p', 'w') and ${notExt('pg_proc', 'p.oid')}
228
+ order by 1`,
229
+ ))
230
+ add({ kind: 'function', key: `function:${f.sig}`, name: f.sig, ddl: end(unqualify(f.def, /^CREATE OR REPLACE (FUNCTION|PROCEDURE) \S+?\(/)), order: f.atomic ? 1 : 0 });
231
+
232
+ // ---- views and materialized views, in dependency order
233
+ const views = await q<{ oid: number; name: string; kind: string; def: string; options: string[] | null; deps: number[] }>(
234
+ `select c.oid, c.relname as name, c.relkind as kind, pg_get_viewdef(c.oid, true) as def, c.reloptions as options,
235
+ array(select distinct d.refobjid::int from pg_rewrite r join pg_depend d on d.classid = 'pg_rewrite'::regclass and d.objid = r.oid
236
+ where r.ev_class = c.oid and d.refobjid <> c.oid and d.refclassid = 'pg_class'::regclass) as deps
237
+ from pg_class c where c.relnamespace = $1 and c.relkind in ('v', 'm') and ${notExt('pg_class', 'c.oid')}
238
+ order by c.relname`,
239
+ );
240
+ const position = new Map<number, number>();
241
+ const visit = (v: (typeof views)[number], seen: Set<number>): number => {
242
+ if (position.has(v.oid)) return position.get(v.oid)!;
243
+ if (seen.has(v.oid)) return 0;
244
+ seen.add(v.oid);
245
+ const after = Math.max(0, ...v.deps.map((d) => views.find((x) => x.oid === d)).filter((x): x is (typeof views)[number] => !!x).map((x) => visit(x, seen) + 1));
246
+ position.set(v.oid, after);
247
+ return after;
248
+ };
249
+ for (const v of views) {
250
+ const opts = v.options?.length ? ` with (${v.options.join(', ')})` : '';
251
+ const body = v.def.trim().replace(/;$/, '');
252
+ add({
253
+ kind: v.kind === 'm' ? 'materialized view' : 'view', key: `${v.kind === 'm' ? 'materialized view' : 'view'}:${v.name}`, name: v.name, order: visit(v, new Set()),
254
+ ddl: v.kind === 'm' ? `create materialized view ${id(v.name)}${opts} as\n${body}\nwith no data;` : `create or replace view ${id(v.name)}${opts} as\n${body};`,
255
+ });
256
+ }
257
+
258
+ // ---- triggers
259
+ for (const t of await q<{ table: string; name: string; def: string }>(
260
+ `select c.relname as table, g.tgname as name, pg_get_triggerdef(g.oid, true) as def
261
+ from pg_trigger g join pg_class c on c.oid = g.tgrelid
262
+ where c.relnamespace = $1 and not g.tgisinternal and g.tgparentid = 0 and ${notExt('pg_class', 'c.oid')}
263
+ order by 1, 2`,
264
+ ))
265
+ add({ kind: 'trigger', key: `trigger:${t.table}.${t.name}`, name: `${t.table}.${t.name}`, table: t.table, ddl: `${t.def};` });
266
+
267
+ // ---- row level security policies
268
+ for (const p of await q<{ table: string; name: string; permissive: boolean; cmd: string; roles: string[]; using: string | null; check: string | null }>(
269
+ `select c.relname as table, p.polname as name, p.polpermissive as permissive, p.polcmd::text as cmd,
270
+ array(select case when r = 0 then 'public' else quote_ident(pg_get_userbyid(r)) end from unnest(p.polroles) r order by 1) as roles,
271
+ pg_get_expr(p.polqual, p.polrelid) as using, pg_get_expr(p.polwithcheck, p.polrelid) as check
272
+ from pg_policy p join pg_class c on c.oid = p.polrelid where c.relnamespace = $1 order by 1, 2`,
273
+ )) {
274
+ const cmd = { r: 'select', a: 'insert', w: 'update', d: 'delete', '*': 'all' }[p.cmd] ?? 'all';
275
+ add({
276
+ kind: 'policy', key: `policy:${p.table}.${p.name}`, name: `${p.table}.${p.name}`, table: p.table,
277
+ ddl: `create policy ${id(p.name)} on ${id(p.table)} as ${p.permissive ? 'permissive' : 'restrictive'} for ${cmd} to ${p.roles.join(', ')}${p.using ? `\n using (${p.using})` : ''}${p.check ? `\n with check (${p.check})` : ''};`,
278
+ });
279
+ }
280
+
281
+ // ---- comments
282
+ for (const m of await q<{ what: string; target: string; text: string }>(
283
+ `select case c.relkind when 'v' then 'view' when 'm' then 'materialized view' when 'S' then 'sequence' else 'table' end as what,
284
+ quote_ident(c.relname) as target, d.description as text
285
+ from pg_description d join pg_class c on c.oid = d.objoid and d.classoid = 'pg_class'::regclass and d.objsubid = 0
286
+ where c.relnamespace = $1 and c.relkind in ('r', 'p', 'v', 'm', 'S') and ${notExt('pg_class', 'c.oid')}
287
+ union all
288
+ select 'column', quote_ident(c.relname) || '.' || quote_ident(a.attname), d.description
289
+ from pg_description d join pg_class c on c.oid = d.objoid and d.classoid = 'pg_class'::regclass
290
+ join pg_attribute a on a.attrelid = c.oid and a.attnum = d.objsubid
291
+ where c.relnamespace = $1 and d.objsubid > 0 and ${notExt('pg_class', 'c.oid')}
292
+ union all
293
+ select case p.prokind when 'p' then 'procedure' else 'function' end, quote_ident(p.proname) || '(' || pg_get_function_identity_arguments(p.oid) || ')', d.description
294
+ from pg_description d join pg_proc p on p.oid = d.objoid and d.classoid = 'pg_proc'::regclass
295
+ where p.pronamespace = $1 and ${notExt('pg_proc', 'p.oid')}
296
+ union all
297
+ select case t.typtype when 'd' then 'domain' else 'type' end, quote_ident(t.typname), d.description
298
+ from pg_description d join pg_type t on t.oid = d.objoid and d.classoid = 'pg_type'::regclass
299
+ where t.typnamespace = $1 and ${notExt('pg_type', 't.oid')}
300
+ order by 1, 2`,
301
+ ))
302
+ add({ kind: 'comment', key: `comment:${m.what} ${m.target}`, name: `${m.what} ${m.target}`, ddl: `comment on ${m.what} ${m.target} is ${lit(m.text)};` });
303
+
304
+ // ---- functions that PUBLIC may not run (by default it may): the revoke comes first
305
+ for (const f of await q<{ what: string; sig: string }>(
306
+ `select case p.prokind when 'p' then 'procedure' else 'function' end as what,
307
+ quote_ident(p.proname) || '(' || pg_get_function_identity_arguments(p.oid) || ')' as sig
308
+ from pg_proc p
309
+ where p.pronamespace = $1 and p.proacl is not null and ${notExt('pg_proc', 'p.oid')}
310
+ and not exists (select 1 from aclexplode(p.proacl) a where a.grantee = 0 and a.privilege_type = 'EXECUTE')
311
+ order by 2`,
312
+ ))
313
+ add({ kind: 'grant', key: `grant:${f.what} ${f.sig} public`, name: `${f.what} ${f.sig} not to public`, ddl: `revoke execute on ${f.what} ${f.sig} from public;` });
314
+
315
+ // ---- grants (owners' own privileges are not repeated)
316
+ for (const g of await q<{ what: string; target: string; grantee: string; privs: string[]; grantable: boolean }>(
317
+ `select x.what, x.target, case when a.grantee = 0 then 'public' else quote_ident(pg_get_userbyid(a.grantee)) end as grantee,
318
+ array_agg(a.privilege_type order by a.privilege_type) as privs, bool_and(a.is_grantable) as grantable
319
+ from (select case when c.relkind = 'S' then 'sequence' else 'table' end as what, quote_ident(c.relname) as target, c.relacl as acl, c.relowner as owner
320
+ from pg_class c where c.relnamespace = $1 and c.relkind in ('r', 'p', 'v', 'm', 'S') and c.relacl is not null and ${notExt('pg_class', 'c.oid')}
321
+ union all
322
+ select case p.prokind when 'p' then 'procedure' else 'function' end, quote_ident(p.proname) || '(' || pg_get_function_identity_arguments(p.oid) || ')', p.proacl, p.proowner
323
+ from pg_proc p where p.pronamespace = $1 and p.proacl is not null and ${notExt('pg_proc', 'p.oid')}) x,
324
+ aclexplode(x.acl) a
325
+ where a.grantee <> x.owner
326
+ group by x.what, x.target, a.grantee
327
+ order by 1, 2, 3`,
328
+ ))
329
+ add({ kind: 'grant', key: `grant:${g.what} ${g.target} ${g.grantee}`, name: `${g.what} ${g.target} to ${g.grantee}`, ddl: `grant ${g.privs.join(', ').toLowerCase()} on ${g.what === 'table' ? 'table ' : g.what === 'sequence' ? 'sequence ' : `${g.what} `}${g.target} to ${g.grantee}${g.grantable ? ' with grant option' : ''};` });
330
+
331
+ return out;
332
+ }
333
+
334
+ /** Order objects for a script. */
335
+ export function scriptOrder(objects: DdlObject[]) {
336
+ const rank = (o: DdlObject) => {
337
+ if (o.kind === 'function') return o.order === 0 ? KINDS.indexOf('sequence') + 0.5 : KINDS.indexOf('table') + 0.5;
338
+ return KINDS.indexOf(o.kind);
339
+ };
340
+ return [...objects].sort((a, b) => rank(a) - rank(b) || a.order - b.order || a.key.localeCompare(b.key));
341
+ }
342
+
343
+ /** Keep the chosen object types (and comments, grants when asked). */
344
+ export function pick(objects: DdlObject[], types: Set<string>, o: { comments: boolean; grants: boolean }) {
345
+ return objects.filter((x) => {
346
+ const t = TYPE_OF[x.kind];
347
+ if (t === 'comments') return o.comments;
348
+ if (t === 'grants') return o.grants;
349
+ return types.has(t);
350
+ });
351
+ }
352
+
353
+ const HEADER = (title: string, schema: string) => `-- ${title}
354
+ -- Generated by pgkiln on ${new Date().toISOString().slice(0, 19).replace('T', ' ')} UTC. Review before running.
355
+ set check_function_bodies = off;
356
+ create schema if not exists ${id(schema)};
357
+ set search_path = ${id(schema)}, pg_catalog;
358
+ `;
359
+
360
+ /** A script that creates the objects in the schema. */
361
+ export function ddlScript(schema: string, objects: DdlObject[]) {
362
+ let last = '';
363
+ const parts = scriptOrder(objects).map((o) => {
364
+ const head = o.kind !== last ? `\n-- ${heading(o.kind)}\n` : '';
365
+ last = o.kind;
366
+ return `${head}${o.ddl}`;
367
+ });
368
+ return `${HEADER(`DDL of schema ${schema}`, schema)}${parts.join('\n')}\n`;
369
+ }
370
+
371
+ // ---------------------------------------------------------------- comparison
372
+
373
+ export interface Difference {
374
+ key: string;
375
+ kind: Kind;
376
+ name: string;
377
+ /** in the source only, the target only, or in both but different */
378
+ state: 'source' | 'target' | 'different';
379
+ source?: string;
380
+ target?: string;
381
+ }
382
+
383
+ export function compare(source: DdlObject[], target: DdlObject[]): { differences: Difference[]; same: number } {
384
+ const t = new Map(target.map((x) => [x.key, x]));
385
+ const s = new Map(source.map((x) => [x.key, x]));
386
+ const differences: Difference[] = [];
387
+ let same = 0;
388
+ for (const a of source) {
389
+ const b = t.get(a.key);
390
+ if (!b) differences.push({ key: a.key, kind: a.kind, name: a.name, state: 'source', source: a.ddl });
391
+ else if ((a.compareAs ?? a.ddl) !== (b.compareAs ?? b.ddl)) differences.push({ key: a.key, kind: a.kind, name: a.name, state: 'different', source: a.ddl, target: b.ddl });
392
+ else same++;
393
+ }
394
+ for (const b of target) if (!s.has(b.key)) differences.push({ key: b.key, kind: b.kind, name: b.name, state: 'target', target: b.ddl });
395
+ const rank = (d: Difference) => KINDS.indexOf(d.kind);
396
+ differences.sort((a, b) => rank(a) - rank(b) || a.key.localeCompare(b.key));
397
+ return { differences, same };
398
+ }
399
+
400
+ /** Drop statements of an object, for a change script. */
401
+ function dropOf(o: DdlObject): string | null {
402
+ switch (o.kind) {
403
+ case 'type': return `drop type ${id(o.name)};`;
404
+ case 'domain': return `drop domain ${id(o.name)};`;
405
+ case 'sequence': return `drop sequence ${id(o.name)};`;
406
+ case 'table': return `drop table ${id(o.name)};`;
407
+ case 'view': return `drop view ${id(o.name)};`;
408
+ case 'materialized view': return `drop materialized view ${id(o.name)};`;
409
+ case 'function': return `drop routine ${o.name.replace(/^([^(]+)/, (m) => id(m))};`;
410
+ case 'constraint':
411
+ case 'foreign key': return `alter table ${id(o.table!)} drop constraint ${id(o.name.slice(o.table!.length + 1))};`;
412
+ case 'index': return `drop index ${id(o.name)};`;
413
+ case 'trigger': return `drop trigger ${id(o.name.slice(o.table!.length + 1))} on ${id(o.table!)};`;
414
+ case 'policy': return `drop policy ${id(o.name.slice(o.table!.length + 1))} on ${id(o.table!)};`;
415
+ case 'rls': return `alter table ${id(o.table!)} disable row level security;\nalter table ${id(o.table!)} no force row level security;`;
416
+ case 'sequence owner': return `alter sequence ${id(o.name)} owned by none;`;
417
+ case 'comment': return `comment on ${o.name} is null;`;
418
+ case 'grant': {
419
+ const r = /^revoke (.+) on (.+) from public;$/s.exec(o.ddl);
420
+ if (r) return `grant ${r[1]} on ${r[2]} to public;`;
421
+ const m = /^grant (.+) on (.+) to (\S+?)( with grant option)?;$/s.exec(o.ddl);
422
+ return m ? `revoke ${m[1]} on ${m[2]} from ${m[3]};` : null;
423
+ }
424
+ }
425
+ return null;
426
+ }
427
+
428
+ /** A table's changes, column by column (the rest of a table: its constraints, indexes … are objects of their own). */
429
+ function alterTable(a: DdlObject, b: DdlObject): { sql: string[]; review: string[] } {
430
+ const sql: string[] = [];
431
+ const review: string[] = [];
432
+ const t = id(a.name);
433
+ if (!a.columns || !b.columns) return { sql, review: [`-- table ${a.name} differs (partitioning or options): compare by hand.`] };
434
+ const bc = new Map(b.columns.map((x) => [x.name, x]));
435
+ const ac = new Map(a.columns.map((x) => [x.name, x]));
436
+ for (const x of a.columns) {
437
+ const y = bc.get(x.name);
438
+ if (!y) {
439
+ sql.push(`alter table ${t} add column ${x.definition};`);
440
+ continue;
441
+ }
442
+ if (x.definition === y.definition) continue;
443
+ if (x.default?.startsWith('identity') || y.default?.startsWith('identity') || x.default?.startsWith('generated') || y.default?.startsWith('generated')) {
444
+ review.push(`-- column ${a.name}.${x.name}: ${y.definition} → ${x.definition} (identity or generated: change by hand)`);
445
+ continue;
446
+ }
447
+ if (x.type !== y.type) sql.push(`alter table ${t} alter column ${id(x.name)} type ${x.type};`);
448
+ if (x.default !== y.default) sql.push(x.default === null ? `alter table ${t} alter column ${id(x.name)} drop default;` : `alter table ${t} alter column ${id(x.name)} set default ${x.default};`);
449
+ if (x.notNull !== y.notNull) sql.push(`alter table ${t} alter column ${id(x.name)} ${x.notNull ? 'set' : 'drop'} not null;`);
450
+ if (x.definition.includes(' collate ') || y.definition.includes(' collate ')) review.push(`-- column ${a.name}.${x.name}: the collation differs`);
451
+ }
452
+ for (const y of b.columns) if (!ac.has(y.name)) review.push(`-- alter table ${t} drop column ${id(y.name)};`);
453
+ if (a.columns.map((x) => x.name).filter((n) => bc.has(n)).join() !== b.columns.map((x) => x.name).filter((n) => ac.has(n)).join())
454
+ review.push(`-- table ${a.name}: the columns are in another order (not changed)`);
455
+ return { sql, review };
456
+ }
457
+
458
+ /**
459
+ * A script that makes the target schema like the source: what the source has and the target hasn't
460
+ * is created, what differs is replaced (functions and views in place, constraints, indexes, triggers,
461
+ * policies, grants and materialized views dropped and created again, tables column by column), and
462
+ * what only the target has is dropped, commented out: review it. Types, domains and sequences that
463
+ * differ are left for review (tables may use them).
464
+ */
465
+ export function changeScript(sourceName: string, targetName: string, source: DdlObject[], target: DdlObject[]) {
466
+ const { differences } = compare(source, target);
467
+ const s = new Map(source.map((x) => [x.key, x]));
468
+ const t = new Map(target.map((x) => [x.key, x]));
469
+ const create: DdlObject[] = [];
470
+ const first: string[] = [];
471
+ const alters: string[] = [];
472
+ const reviews: string[] = [];
473
+ const gone: string[] = [];
474
+ const comment = (sql: string) => sql.split('\n').map((l) => `-- ${l}`).join('\n');
475
+ for (const d of differences) {
476
+ const a = s.get(d.key);
477
+ const b = t.get(d.key);
478
+ if (d.state === 'source') create.push(a!);
479
+ else if (d.state === 'target') {
480
+ const sql = dropOf(b!);
481
+ if (sql) gone.push(comment(sql));
482
+ } else if (a!.kind === 'table') {
483
+ const r = alterTable(a!, b!);
484
+ alters.push(...r.sql);
485
+ reviews.push(...r.review);
486
+ } else if (a!.kind === 'function' || a!.kind === 'view' || a!.kind === 'comment' || a!.kind === 'sequence owner') {
487
+ create.push(a!);
488
+ } else if (a!.kind === 'type' || a!.kind === 'domain' || a!.kind === 'sequence') {
489
+ reviews.push(`-- ${a!.kind} ${a!.name} differs; in ${targetName}:`, comment(b!.ddl), `-- in ${sourceName}:`, comment(a!.ddl));
490
+ } else {
491
+ const sql = dropOf(b!);
492
+ if (sql) first.push(sql);
493
+ create.push(a!);
494
+ }
495
+ }
496
+ const lines = [HEADER(`Make schema ${targetName} like ${sourceName}`, targetName).replace(/create schema if not exists [^\n]+\n/, '')];
497
+ if (!differences.length) lines.push('-- the schemas are the same: nothing to change');
498
+ if (first.length) lines.push('-- replaced objects: the old ones first', ...first);
499
+ if (alters.length) lines.push('', '-- changed tables', ...alters);
500
+ let last = '';
501
+ for (const o of scriptOrder(create)) {
502
+ if (o.kind !== last) lines.push('', `-- ${heading(o.kind)}`);
503
+ last = o.kind;
504
+ lines.push(o.ddl);
505
+ }
506
+ if (reviews.length) lines.push('', '-- review: not changed by this script', ...reviews);
507
+ if (gone.length) lines.push('', `-- only in ${targetName}: dropped when you remove the comment marks`, ...gone);
508
+ return `${lines.join('\n')}\n`;
509
+ }
package/src/geocode.ts ADDED
@@ -0,0 +1,44 @@
1
+ // Geocoding for address items (migration 086): the installation's geocoder,
2
+ // a Nominatim-compatible search API (GEOCODER_URL, e.g. your own Nominatim,
3
+ // https://nominatim.openstreetmap.org/search within its usage policy, or
4
+ // LocationIQ's https://eu1.locationiq.com/v1/search with GEOCODER_KEY).
5
+ // Without GEOCODER_URL address items are plain text items.
6
+
7
+ export const geocoderUrl = () => {
8
+ const u = process.env.GEOCODER_URL?.trim();
9
+ return u && /^https?:\/\//.test(u) ? u : null;
10
+ };
11
+
12
+ export interface Place {
13
+ label: string;
14
+ lat: number;
15
+ lon: number;
16
+ }
17
+
18
+ /** At most this many candidates come back. */
19
+ export const GEOCODE_MAX = 5;
20
+
21
+ /** The places that match an address (best first); throws when the geocoder can't be reached. */
22
+ export async function geocode(q: string, opts: { language?: string; country?: string | null } = {}): Promise<Place[]> {
23
+ const base = geocoderUrl();
24
+ if (!base) throw new Error('No geocoder: set GEOCODER_URL.');
25
+ const url = new URL(base);
26
+ url.searchParams.set('format', 'jsonv2');
27
+ url.searchParams.set('limit', String(GEOCODE_MAX));
28
+ url.searchParams.set('q', q);
29
+ if (opts.language) url.searchParams.set('accept-language', opts.language);
30
+ if (opts.country && /^[a-z]{2}(,[a-z]{2})*$/i.test(opts.country)) url.searchParams.set('countrycodes', opts.country.toLowerCase());
31
+ if (process.env.GEOCODER_KEY) url.searchParams.set('key', process.env.GEOCODER_KEY);
32
+ const res = await fetch(url, {
33
+ headers: { accept: 'application/json', 'user-agent': `pgkiln (${process.env.PUBLIC_URL ?? 'https://pgkiln.vargar.eu'})` },
34
+ signal: AbortSignal.timeout(8000),
35
+ redirect: 'error',
36
+ });
37
+ if (!res.ok) throw new Error(`geocoder answered ${res.status}`);
38
+ const rows = (await res.json()) as unknown;
39
+ if (!Array.isArray(rows)) return [];
40
+ return rows
41
+ .map((r: any) => ({ label: String(r?.display_name ?? '').slice(0, 300), lat: Number(r?.lat), lon: Number(r?.lon) }))
42
+ .filter((p) => p.label && Number.isFinite(p.lat) && Number.isFinite(p.lon) && Math.abs(p.lat) <= 90 && Math.abs(p.lon) <= 180)
43
+ .slice(0, GEOCODE_MAX);
44
+ }