palgres 0.6.2 → 0.8.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.
package/dist/index.js CHANGED
@@ -1,26 +1,26 @@
1
- /*! palgres 0.6.2 — Palbase SDK License (see LICENSE) */
2
- import{b as ds,c as He,l as An,m as On}from"./chunks/JO2IWQRS.js";import{$ as Ss,A as rn,B as Y,C as pt,D as xe,E as on,F as ft,G as ps,H as fs,I as gs,J as kn,K as gt,L as hs,M as Es,N as ws,O as Ln,P as ys,Q as wo,R as yo,S as To,T as $o,U as So,V as Ro,W as xn,X as Ts,Y as $s,Z as In,_ as vn,a as mo,aa as Rs,b as po,c as te,d as oe,e as be,f as fo,g as Ne,h as ut,i as go,j as We,k as je,l as dt,m as mt,n as Ke,o as Pe,p as ae,q as ls,r as cs,s as us,t as ce,u as Nn,v as sn,w as ms,x as ho,y as Eo,z as Ve}from"./chunks/LMOR2RYN.js";import{a as _e,b as lt,c as D,d as Re,e as tn,f as os,g as as,h as ct,i as ge,j as is,k as De,n as he}from"./chunks/R4DKVA6H.js";import{createHash as Ao}from"node:crypto";import{createHash as wt}from"node:crypto";var N=t=>`"${t.replace(/"/g,'""')}"`,P=(t,e)=>`${N(t)}.${N(e)}`,F=t=>`'${t.replace(/'/g,"''")}'`;function ue(t){if(t.includes("\0"))throw new TypeError("a NUL character cannot be sent to Postgres");return`E'${t.replace(/\\/g,"\\\\").replace(/'/g,"''")}'`}var ye=t=>t.map(N).join(", "),bo=new Set(["now","current_timestamp","current_date","current_time","localtimestamp","localtime","transaction_timestamp","statement_timestamp"]);function bs(t){return[...ht(t)].some(e=>e.call&&!bo.has(e.name.toLowerCase()))}function*ht(t){let e=0;for(;e<t.length;){let n=t[e];if(n==="'"){for(e++;e<t.length;)if(t[e]==="'"&&t[e+1]==="'")e+=2;else{if(t[e]==="'")break;e++}e++;continue}if(n==="$"){let s=/^\$[A-Za-z_]*\$/.exec(t.slice(e));if(s){let r=t.indexOf(s[0],e+s[0].length);e=r<0?t.length:r+s[0].length;continue}}if(n==='"'){let s=e+1,r="";for(;s<t.length;)if(t[s]==='"'&&t[s+1]==='"')r+='"',s+=2;else{if(t[s]==='"')break;r+=t[s++]}let o=s+1;yield{start:e,end:o,name:r,quoted:!0,call:/^\s*\(/.test(t.slice(o))},e=o;continue}if(/[A-Za-z_]/.test(n)&&(e===0||!/[A-Za-z0-9_$]/.test(t[e-1]))){let s=/^[A-Za-z_][A-Za-z0-9_$]*/.exec(t.slice(e)),r=e+s[0].length;yield{start:e,end:r,name:s[0],quoted:!1,call:/^\s*\(/.test(t.slice(r))},e=r;continue}e++}}function Ae(t,e){for(let n of ht(t))if(!n.call&&(n.quoted?n.name===e:n.name.toLowerCase()===e.toLowerCase()))return!0;return!1}function Oe(t,e){if(e.size===0)return t;let n="",s=0;for(let r of ht(t)){if(r.call)continue;let o=e.get(r.quoted?r.name:No(e,r.name)??"");o!==void 0&&(n+=t.slice(s,r.start)+N(o),s=r.end)}return n+t.slice(s)}function No(t,e){if(t.has(e))return e;let n=e.toLowerCase();for(let s of t.keys())if(s.toLowerCase()===n)return s}var Cn=t=>`(NOT EXISTS (SELECT 1 FROM unnest(i.indclass::oid[]) AS oc(oid) JOIN pg_catalog.pg_opclass o ON o.oid = oc.oid WHERE NOT o.opcdefault)
1
+ /*! palgres 0.8.0 — Palbase SDK License (see LICENSE) */
2
+ import{a as ws,b as ys,c as Ge,l as kn,m as Ln}from"./chunks/4XRD3YUA.js";import{$ as Is,A as an,B as Y,C as wt,D as xe,E as ln,F as yt,G as $s,H as Ss,I as Rs,J as In,K as Tt,L as bs,M as Ns,N as As,O as xn,P as Os,Q as No,R as Ao,S as Oo,T as ko,U as Lo,V as Io,W as vn,X as ks,Y as Ls,Z as Cn,_ as _n,a as yo,aa as xs,b as To,c as ne,d as oe,e as Ne,f as $o,g as Ae,h as gt,i as So,j as Ke,k as He,l as ht,m as Et,n as Ve,o as Me,p as ae,q as gs,r as hs,s as Es,t as ue,u as On,v as on,w as Ts,x as Ro,y as bo,z as Xe}from"./chunks/FRASIV2M.js";import{a as je,b as De,c as pt,d as D,e as be,f as rn,g as ms,h as ps,i as ft,j as ge,k as fs,l as Pe,o as he}from"./chunks/BVHAGKC4.js";import{createHash as Co}from"node:crypto";import{createHash as Rt}from"node:crypto";var b=t=>`"${t.replace(/"/g,'""')}"`,P=(t,e)=>`${b(t)}.${b(e)}`,M=t=>`'${t.replace(/'/g,"''")}'`;function de(t){if(t.includes("\0"))throw new TypeError("a NUL character cannot be sent to Postgres");return`E'${t.replace(/\\/g,"\\\\").replace(/'/g,"''")}'`}var ye=t=>t.map(b).join(", "),xo=new Set(["now","current_timestamp","current_date","current_time","localtimestamp","localtime","transaction_timestamp","statement_timestamp"]);function vs(t){return[...$t(t)].some(e=>e.call&&!xo.has(e.name.toLowerCase()))}function*$t(t){let e=0;for(;e<t.length;){let n=t[e];if(n==="'"){for(e++;e<t.length;)if(t[e]==="'"&&t[e+1]==="'")e+=2;else{if(t[e]==="'")break;e++}e++;continue}if(n==="$"){let s=/^\$[A-Za-z_]*\$/.exec(t.slice(e));if(s){let r=t.indexOf(s[0],e+s[0].length);e=r<0?t.length:r+s[0].length;continue}}if(n==='"'){let s=e+1,r="";for(;s<t.length;)if(t[s]==='"'&&t[s+1]==='"')r+='"',s+=2;else{if(t[s]==='"')break;r+=t[s++]}let o=s+1;yield{start:e,end:o,name:r,quoted:!0,call:/^\s*\(/.test(t.slice(o))},e=o;continue}if(/[A-Za-z_]/.test(n)&&(e===0||!/[A-Za-z0-9_$]/.test(t[e-1]))){let s=/^[A-Za-z_][A-Za-z0-9_$]*/.exec(t.slice(e)),r=e+s[0].length;yield{start:e,end:r,name:s[0],quoted:!1,call:/^\s*\(/.test(t.slice(r))},e=r;continue}e++}}function Oe(t,e){for(let n of $t(t))if(!n.call&&(n.quoted?n.name===e:n.name.toLowerCase()===e.toLowerCase()))return!0;return!1}function ke(t,e){if(e.size===0)return t;let n="",s=0;for(let r of $t(t)){if(r.call)continue;let o=e.get(r.quoted?r.name:vo(e,r.name)??"");o!==void 0&&(n+=t.slice(s,r.start)+b(o),s=r.end)}return n+t.slice(s)}function vo(t,e){if(t.has(e))return e;let n=e.toLowerCase();for(let s of t.keys())if(s.toLowerCase()===n)return s}var Dn=t=>`(NOT EXISTS (SELECT 1 FROM unnest(i.indclass::oid[]) AS oc(oid) JOIN pg_catalog.pg_opclass o ON o.oid = oc.oid WHERE NOT o.opcdefault)
3
3
  AND NOT i.indnullsnotdistinct AND ${t}.reloptions IS NULL
4
- AND NOT EXISTS (SELECT 1 FROM unnest(i.indcollation::oid[], i.indkey::int2[]) AS x(coll, att) JOIN pg_catalog.pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = x.att WHERE x.att > 0 AND x.coll <> a.attcollation))`;var J="palgres_search",_n=(t,e)=>t.model===e.model&&t.dimensions===e.dimensions&&t.metric===e.metric&&t.from.length===e.from.length&&t.from.every((n,s)=>n===e.from[s]),Ns=48;function Ie(t,e){let n=t.replaceAll(".","__");return`${n.length<=Ns?n:`${n.slice(0,32)}_${wt("md5").update(n).digest("hex").slice(0,12)}`}__g${e}`}var As=t=>({insert:`_palgres_search_g${t}_ins`,update:`_palgres_search_g${t}_upd`});function Dn(t,e){let n=wt("sha256").update(JSON.stringify([t.schema,t.name,e])).digest("hex").slice(0,8),s=`_palgres_search_${t.name}_${n}`;return Buffer.byteLength(s)<=63?s:`_palgres_search_${wt("sha256").update(`${t.schema}.${t.name}`).digest("hex").slice(0,16)}_${n}`}function Pn(t,e){let n=e===void 0?"":`${N(e)}.`;return t.map(s=>`coalesce(${n}${N(s)}::text, '')`).join(" || ' ' || ")}var Mn=(t,e)=>`to_tsvector('simple', ${Pn(t,e)})`;function an(t,e){let n=t>os?"halfvec":"vector",s={cosine:["cosine","<=>"],euclidean:["l2","<->"],inner_product:["ip","<#>"]}[e];return{type:n,opclass:`${n}_${s[0]}_ops`,operator:s[1]}}function Os(t){let e=`${t.table.schema}.${t.table.name}`,n=P(t.table.schema,t.table.name),s=Ie(e,t.gen),r=P(J,s),o=an(t.search.dimensions,t.search.metric),a=N(t.pk.name),l=[t.roles.anon,t.roles.authenticated].map(N).join(", ");return[`INSERT INTO ${J}.generations (table_key, gen, model, dimensions, metric, from_columns, status, promoted_at) VALUES (${F(e)}, ${t.gen}, ${F(t.search.model)}, ${t.search.dimensions}, ${F(t.search.metric)}, ARRAY[${t.search.from.map(F).join(", ")}]::text[], ${F(t.status)}, ${t.status==="active"?"now()":"NULL"})`,`CREATE TABLE ${r} (
4
+ AND NOT EXISTS (SELECT 1 FROM unnest(i.indcollation::oid[], i.indkey::int2[]) AS x(coll, att) JOIN pg_catalog.pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = x.att WHERE x.att > 0 AND x.coll <> a.attcollation))`;var J="palgres_search",Pn=(t,e)=>t.model===e.model&&t.dimensions===e.dimensions&&t.metric===e.metric&&t.from.length===e.from.length&&t.from.every((n,s)=>n===e.from[s]),Cs=48;function ve(t,e){let n=t.replaceAll(".","__");return`${n.length<=Cs?n:`${n.slice(0,32)}_${Rt("md5").update(n).digest("hex").slice(0,12)}`}__g${e}`}var _s=t=>({insert:`_palgres_search_g${t}_ins`,update:`_palgres_search_g${t}_upd`});function Mn(t,e){let n=Rt("sha256").update(JSON.stringify([t.schema,t.name,e])).digest("hex").slice(0,8),s=`_palgres_search_${t.name}_${n}`;return Buffer.byteLength(s)<=63?s:`_palgres_search_${Rt("sha256").update(`${t.schema}.${t.name}`).digest("hex").slice(0,16)}_${n}`}function qn(t,e){let n=e===void 0?"":`${b(e)}.`;return t.map(s=>`coalesce(${n}${b(s)}::text, '')`).join(" || ' ' || ")}var Fn=(t,e)=>`to_tsvector('simple', ${qn(t,e)})`;function cn(t,e){let n=t>ms?"halfvec":"vector",s={cosine:["cosine","<=>"],euclidean:["l2","<->"],inner_product:["ip","<#>"]}[e];return{type:n,opclass:`${n}_${s[0]}_ops`,operator:s[1]}}function Ds(t){let e=`${t.table.schema}.${t.table.name}`,n=P(t.table.schema,t.table.name),s=ve(e,t.gen),r=P(J,s),o=cn(t.search.dimensions,t.search.metric),a=b(t.pk.name),i=[t.roles.anon,t.roles.authenticated].map(b).join(", ");return[`INSERT INTO ${J}.generations (table_key, gen, model, dimensions, metric, from_columns, status, promoted_at) VALUES (${M(e)}, ${t.gen}, ${M(t.search.model)}, ${t.search.dimensions}, ${M(t.search.metric)}, ARRAY[${t.search.from.map(M).join(", ")}]::text[], ${M(t.status)}, ${t.status==="active"?"now()":"NULL"})`,`CREATE TABLE ${r} (
5
5
  id ${t.pk.type} PRIMARY KEY REFERENCES ${n} (${a}) ON DELETE CASCADE,
6
- embedding ${N(t.types)}.${o.type}(${t.search.dimensions}) NOT NULL,
6
+ embedding ${b(t.types)}.${o.type}(${t.search.dimensions}) NOT NULL,
7
7
  model text NOT NULL,
8
8
  embedded_at timestamptz NOT NULL DEFAULT now()
9
- )`,`CREATE INDEX ON ${r} USING hnsw (embedding ${N(t.types)}.${o.opclass})`,`ALTER TABLE ${r} ENABLE ROW LEVEL SECURITY`,`CREATE POLICY ${N("_palgres_search_parent")} ON ${r} FOR SELECT TO ${l} USING (EXISTS (SELECT 1 FROM ${n} p WHERE p.${a} = ${N(s)}.id))`,`GRANT SELECT ON TABLE ${r} TO ${l}`,`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ${r} TO ${N(t.roles.service)}`,`CREATE FUNCTION ${r}() RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $palgres$
9
+ )`,`CREATE INDEX ON ${r} USING hnsw (embedding ${b(t.types)}.${o.opclass})`,`ALTER TABLE ${r} ENABLE ROW LEVEL SECURITY`,`CREATE POLICY ${b("_palgres_search_parent")} ON ${r} FOR SELECT TO ${i} USING (EXISTS (SELECT 1 FROM ${n} p WHERE p.${a} = ${b(s)}.id))`,`GRANT SELECT ON TABLE ${r} TO ${i}`,`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ${r} TO ${b(t.roles.service)}`,`CREATE FUNCTION ${r}() RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $palgres$
10
10
  BEGIN
11
11
  -- A generation left behind keeps its triggers until the next migration takes them: they do nothing.
12
- IF NOT EXISTS (SELECT 1 FROM ${J}.generations g WHERE g.table_key = ${F(e)} AND g.gen = ${t.gen} AND g.status <> 'retired') THEN
12
+ IF NOT EXISTS (SELECT 1 FROM ${J}.generations g WHERE g.table_key = ${M(e)} AND g.gen = ${t.gen} AND g.status <> 'retired') THEN
13
13
  RETURN NULL;
14
14
  END IF;
15
15
  -- One pending row per row of the table: written again before it is embedded, it is embedded once, as it is then.
16
- INSERT INTO ${J}.queue (table_key, gen, row_id) VALUES (${F(e)}, ${t.gen}, NEW.${a}::text)
16
+ INSERT INTO ${J}.queue (table_key, gen, row_id) VALUES (${M(e)}, ${t.gen}, NEW.${a}::text)
17
17
  ON CONFLICT ON CONSTRAINT queue_one_pending DO UPDATE SET enqueued_at = clock_timestamp(), attempts = 0, next_attempt_at = now(), last_error = NULL;
18
18
  -- After the queue: a worker writing this row's vector holds its queue row until it commits, so what it wrote from the text before this change is what goes here.
19
19
  IF TG_OP = 'UPDATE' AND TG_ARGV[0] = 'null' THEN
20
20
  DELETE FROM ${r} WHERE id = NEW.${a};
21
21
  END IF;
22
22
  RETURN NULL;
23
- END $palgres$`,`REVOKE ALL ON FUNCTION ${r}() FROM PUBLIC`,...qn({table:t.table,gen:t.gen,from:t.search.from,staleness:t.search.staleness},!1)]}function qn(t,e){let n=P(t.table.schema,t.table.name),s=`${P(J,Ie(`${t.table.schema}.${t.table.name}`,t.gen))}(${F(t.staleness)})`,r=As(t.gen),o=e?"CREATE OR REPLACE TRIGGER":"CREATE TRIGGER",a=t.from.map(l=>`OLD.${N(l)} IS DISTINCT FROM NEW.${N(l)}`).join(" OR ");return[`${o} ${N(r.insert)} AFTER INSERT ON ${n} FOR EACH ROW EXECUTE FUNCTION ${s}`,`${o} ${N(r.update)} AFTER UPDATE OF ${t.from.map(N).join(", ")} ON ${n} FOR EACH ROW WHEN (${a}) EXECUTE FUNCTION ${s}`]}function ks(t,e,n){return`INSERT INTO ${J}.queue (table_key, gen, row_id) SELECT ${F(`${t.schema}.${t.name}`)}, ${e}, ${N(n)}::text FROM ${P(t.schema,t.name)} ON CONFLICT ON CONSTRAINT queue_one_pending DO NOTHING`}function Fn(t,e){let n=`${t.schema}.${t.name}`,s=P(t.schema,t.name),r=P(J,Ie(n,e)),o=As(e);return[`DROP TRIGGER IF EXISTS ${N(o.insert)} ON ${s}`,`DROP TRIGGER IF EXISTS ${N(o.update)} ON ${s}`,`DROP TABLE IF EXISTS ${r}`,`DROP FUNCTION IF EXISTS ${r}()`,`DELETE FROM ${J}.queue WHERE table_key = ${F(n)} AND gen = ${e}`,`DELETE FROM ${J}.generations WHERE table_key = ${F(n)} AND gen = ${e}`]}var Un=t=>`palgres search 1 (${t.anon}, ${t.authenticated}, ${t.service})`;function Ls(t){let e=[t.anon,t.authenticated,t.service].map(N).join(", "),n=N(t.service),s=J;return[`CREATE SCHEMA IF NOT EXISTS ${s}`,`CREATE TABLE IF NOT EXISTS ${s}.generations (
23
+ END $palgres$`,`REVOKE ALL ON FUNCTION ${r}() FROM PUBLIC`,...Un({table:t.table,gen:t.gen,from:t.search.from,staleness:t.search.staleness},!1)]}function Un(t,e){let n=P(t.table.schema,t.table.name),s=`${P(J,ve(`${t.table.schema}.${t.table.name}`,t.gen))}(${M(t.staleness)})`,r=_s(t.gen),o=e?"CREATE OR REPLACE TRIGGER":"CREATE TRIGGER",a=t.from.map(i=>`OLD.${b(i)} IS DISTINCT FROM NEW.${b(i)}`).join(" OR ");return[`${o} ${b(r.insert)} AFTER INSERT ON ${n} FOR EACH ROW EXECUTE FUNCTION ${s}`,`${o} ${b(r.update)} AFTER UPDATE OF ${t.from.map(b).join(", ")} ON ${n} FOR EACH ROW WHEN (${a}) EXECUTE FUNCTION ${s}`]}function Ps(t,e,n){return`INSERT INTO ${J}.queue (table_key, gen, row_id) SELECT ${M(`${t.schema}.${t.name}`)}, ${e}, ${b(n)}::text FROM ${P(t.schema,t.name)} ON CONFLICT ON CONSTRAINT queue_one_pending DO NOTHING`}function Bn(t,e){let n=`${t.schema}.${t.name}`,s=P(t.schema,t.name),r=P(J,ve(n,e)),o=_s(e);return[`DROP TRIGGER IF EXISTS ${b(o.insert)} ON ${s}`,`DROP TRIGGER IF EXISTS ${b(o.update)} ON ${s}`,`DROP TABLE IF EXISTS ${r}`,`DROP FUNCTION IF EXISTS ${r}()`,`DELETE FROM ${J}.queue WHERE table_key = ${M(n)} AND gen = ${e}`,`DELETE FROM ${J}.generations WHERE table_key = ${M(n)} AND gen = ${e}`]}var Wn=t=>`palgres search 1 (${t.anon}, ${t.authenticated}, ${t.service})`;function Ms(t){let e=[t.anon,t.authenticated,t.service].map(b).join(", "),n=b(t.service),s=J;return[`CREATE SCHEMA IF NOT EXISTS ${s}`,`CREATE TABLE IF NOT EXISTS ${s}.generations (
24
24
  table_key text NOT NULL,
25
25
  gen integer NOT NULL,
26
26
  model text NOT NULL,
@@ -43,7 +43,7 @@ END $palgres$`,`REVOKE ALL ON FUNCTION ${r}() FROM PUBLIC`,...qn({table:t.table,
43
43
  CONSTRAINT queue_one_pending UNIQUE (table_key, gen, row_id),
44
44
  CONSTRAINT queue_due UNIQUE (next_attempt_at, enqueued_at, id)
45
45
  )`,`GRANT USAGE ON SCHEMA ${s} TO ${e}`,`GRANT SELECT ON TABLE ${s}.generations TO ${e}`,`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ${s}.queue TO ${n}`,`GRANT USAGE, SELECT ON SEQUENCE ${s}.queue_id_seq TO ${n}`,`CREATE OR REPLACE FUNCTION ${s}.vectors(of_table text, gen integer) RETURNS text LANGUAGE sql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $palgres$
46
- SELECT CASE WHEN length(b) <= ${Ns} THEN b ELSE left(b, 32) || '_' || left(md5(b), 12) END || '__g' || gen FROM (SELECT replace(of_table, '.', '__') AS b) x
46
+ SELECT CASE WHEN length(b) <= ${Cs} THEN b ELSE left(b, 32) || '_' || left(md5(b), 12) END || '__g' || gen FROM (SELECT replace(of_table, '.', '__') AS b) x
47
47
  $palgres$`,`CREATE OR REPLACE FUNCTION ${s}.promote(of_table text) RETURNS integer LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $palgres$
48
48
  DECLARE
49
49
  g ${s}.generations%ROWTYPE;
@@ -69,75 +69,75 @@ BEGIN
69
69
  UPDATE ${s}.generations SET status = 'retired' WHERE table_key = of_table AND status = 'active';
70
70
  UPDATE ${s}.generations SET status = 'active', promoted_at = now() WHERE table_key = of_table AND gen = g.gen;
71
71
  RETURN g.gen;
72
- END $palgres$`,`REVOKE ALL ON FUNCTION ${s}.promote(text) FROM PUBLIC`,`GRANT EXECUTE ON FUNCTION ${s}.promote(text) TO ${n}`,`COMMENT ON SCHEMA ${s} IS ${F(Un(t))}`]}var Et=new Set,Bn={listen(t){return Et.add(t),()=>{Et.delete(t)}},committed(){for(let t of Et)t()}};var Te=(t,e)=>`palgres_${t}_${e}`,Ds=t=>Ao("sha256").update(t).digest("hex").slice(0,8),yt=(t,e)=>`${t.name}_${e}_${Ds(JSON.stringify([t.schema,t.name,e])).slice(0,6)}`,ie=(t,e,n)=>{let s=`_palgres_${t}_${e}_${n}`;return Buffer.byteLength(s)<=63?s:`_palgres_${t}_${e}_${Ds(n)}`},Tt={anon:"anon",backend_anon:"anon",authenticated:"authenticated",backend_authenticated:"authenticated",service_role:"service",backend_service_role:"service",public:"public"},xs=t=>!t.nullable&&t.default===void 0&&!t.identity&&!t.generated,Is="the version serving now reads that value in the rows the new version writes";function bt(t){return new Rt(t).run()}var Rt=class{from;to;fromN;n;input;changes=[];refusals=[];matches=new Map;fromMatched=new Set;prelude=[];creates=[];createFks=[];alters=[];backfills=[];online=[];settle=[];lateFks=[];searchStart=[];searchComplete=[];undoSearch=[];viewsAtComplete=null;namesAtComplete=!1;dropsColumnsAtComplete=!1;completeUnitDrops=[];raws=[];completeTables=[];completeIndexes=[];completeDrops=[];completeTypes=[];undoTables=[];undoOnline=[];fkDrops=[];undoFksGone=[];undoIndexesGone=[];undoFksBack=[];undoRawsGone=[];undoRawsBack=[];undoCreates=[];undoNewTables=[];layout={tables:[]};constructor(e){this.input=e,this.from=ge(e.from?.schema??lt());let n=new Map((e.from?.schema.raw??[]).map((r,o)=>[r.name,o]));this.from.raw.sort((r,o)=>(n.get(r.name)??0)-(n.get(o.name)??0)),this.to=ge(e.to);let s=new Map(e.to.raw.map((r,o)=>[r.name,o]));this.to.raw.sort((r,o)=>(s.get(r.name)??0)-(s.get(o.name)??0)),this.fromN=e.from?.n??null,this.n=e.next}run(){this.extensions(),this.schemas(),this.enums(),this.rawUnitsBefore(),this.matchTables();for(let r of this.to.tables){let o=this.matches.get(D(r));o?this.alterTable(o):this.createTable(r),this.search(r,o)}for(let r of this.from.tables)this.fromMatched.has(r)||this.dropTable(r);this.rawUnits(),this.enumDrops();let e=[...this.prelude,...this.creates.length+this.createFks.length>0?[{kind:"tx",sql:[...this.creates,...this.createFks],why:"the new tables and types"}]:[],...this.fkDrops,...this.alters,...this.backfills,...this.online,...this.settle,...this.lateFks,...this.searchStart,...this.raws,{kind:"tx",sql:this.views(),why:`version ${this.n}'s views`}],n=[...this.fromN!==null?[{kind:"tx",sql:this.dropViews(this.fromN,this.from),why:`version ${this.fromN}'s views`}]:[],...this.searchComplete,...this.viewsAtComplete?[{kind:"tx",sql:["SET LOCAL check_function_bodies = off",...this.completeUnitDrops,...this.viewsAtComplete.drops,...this.completeTables.flatMap(r=>r.kind==="tx"?r.sql:[]),...this.viewsAtComplete.creates],why:"the tables, and the views that read them, at once"}]:[...this.completeUnitDrops.length>0?[{kind:"tx",sql:this.completeUnitDrops,why:"the triggers and rules gone"}]:[],...this.completeTables],...this.completeIndexes,...this.completeDrops.length>0?[{kind:"tx",sql:[`DROP TABLE IF EXISTS ${this.completeDrops.join(", ")}`],why:"the dropped tables"}]:[],...this.completeTypes.length>0?[{kind:"tx",sql:this.completeTypes,why:"the dropped types"}]:[]],s=[{kind:"tx",sql:this.dropViews(this.n,this.to),why:`version ${this.n}'s views`},...this.undoSearch,...this.undoFksGone,...this.undoRawsGone.length>0?[{kind:"tx",sql:["SET LOCAL check_function_bodies = off",...this.undoRawsGone],why:"the units start made, taken back"}]:[],...[...this.undoTables].reverse(),...this.undoIndexesGone,...this.undoFksBack,...this.undoRawsBack.length>0?[{kind:"tx",sql:["SET LOCAL check_function_bodies = off",...this.undoRawsBack],why:"the units start replaced, back"}]:[],...this.undoOnline,...this.undoNewTables.length+this.undoCreates.length>0?[{kind:"tx",sql:[...this.undoNewTables.length>0?[`DROP TABLE IF EXISTS ${this.undoNewTables.join(", ")}`]:[],...[...this.undoCreates].reverse()],why:"the new tables and types"}]:[]];return{from:this.fromN,to:this.n,changes:this.changes,refusals:this.refusals,start:e,complete:n,rollback:s,layout:this.layout}}refuse(e,n){this.refusals.push(`${e}: ${n}`)}extensions(){for(let e of this.to.extensions)this.from.extensions.includes(e)||(this.changes.push(`extension ${e}`),this.prelude.push({kind:"statement",sql:`CREATE EXTENSION IF NOT EXISTS ${N(e)}`,why:`extension ${e}`}))}schemas(){let e=new Set([...this.from.tables.map(a=>a.schema),...this.from.enums.map(a=>a.schema),...this.from.raw.flatMap(a=>a.schema?[a.schema]:[]),"public"]),n=[...new Set([...this.to.tables.map(a=>a.schema),...this.to.enums.map(a=>a.schema),...this.to.raw.flatMap(a=>a.schema?[a.schema]:[])])].filter(a=>!e.has(a)),{anon:s,authenticated:r,service:o}=this.input.roles;for(let a of n)this.changes.push(`schema ${a}`),this.creates.push(`CREATE SCHEMA IF NOT EXISTS ${N(a)}`,`GRANT USAGE ON SCHEMA ${N(a)} TO ${[s,r,o].map(N).join(", ")}`)}enums(){for(let e of this.to.enums){let n=this.from.enums.find(o=>o.schema===e.schema&&o.name===e.name),s=P(e.schema,e.name);if(!n){this.changes.push(`type ${e.schema}.${e.name}`),this.creates.push(`CREATE TYPE ${s} AS ENUM (${e.values.map(F).join(", ")})`),this.undoCreates.push(`DROP TYPE IF EXISTS ${s}`);continue}let r=n.values.filter(o=>!e.values.includes(o));r.length>0&&this.refuse(`type ${e.schema}.${e.name} loses ${r.join(", ")}`,"Postgres cannot take a value out of an enum; add a new type and move the column to it");for(let o of e.values)n.values.includes(o)||(this.changes.push(`type ${e.schema}.${e.name} gains '${o}'`),this.prelude.push({kind:"statement",sql:`ALTER TYPE ${s} ADD VALUE IF NOT EXISTS ${F(o)}`,why:`${e.name} gains ${o}`}))}}enumDrops(){for(let e of this.from.enums)this.to.enums.some(n=>n.schema===e.schema&&n.name===e.name)||(this.changes.push(`type ${e.schema}.${e.name} goes (at complete)`),this.completeTypes.push(`DROP TYPE IF EXISTS ${P(e.schema,e.name)}`))}matchTables(){let e=new Map(this.from.tables.map(n=>[D(n),n]));for(let n of this.to.tables)n.renamedFrom===void 0||n.renamedFrom===n.name||!e.has(`${n.schema}.${n.renamedFrom}`)||this.to.tables.some(s=>s!==n&&s.schema===n.schema&&s.name===n.renamedFrom)&&this.refuse(`table ${n.schema}.${n.renamedFrom} is renamed ${n.name}, and a new table takes the name ${n.renamedFrom} in the same deploy`,`rename it in this deploy, and make the new ${n.renamedFrom} in the next`);for(let n of this.to.tables){let s=e.get(D(n));if(n.renamedFrom!==void 0&&n.renamedFrom!==n.name){let r=e.get(`${n.schema}.${n.renamedFrom}`);s&&r&&this.refuse(`table ${D(n)} is renamed from ${n.renamedFrom}, but ${D(n)} is there already`,"remove renamedFrom, or rename to a free name"),!s&&r&&(s=r)}!s||this.fromMatched.has(s)||(this.fromMatched.add(s),this.matches.set(D(n),this.matchColumns(n,s)))}}matchColumns(e,n){let s=D(e),r=new Map(n.columns.map(c=>[c.name,c])),o={to:e,from:n,physical:n.name,renamed:new Map,cols:new Map,retyped:new Map,parked:new Map},a=new Set,l=new Set(e.columns.filter(c=>c.renamedFrom!==void 0&&c.renamedFrom!==c.name&&r.has(c.renamedFrom)&&!r.has(c.name)).map(c=>c.renamedFrom));for(let c of e.columns){let i=l.has(c.name)?void 0:r.get(c.name);if(c.renamedFrom!==void 0&&c.renamedFrom!==c.name&&r.has(c.renamedFrom)&&(r.has(c.name)?this.refuse(`column ${s}.${c.name} is renamed from ${c.renamedFrom}, but ${c.name} is there already`,"remove renamedFrom, or rename to a free name"):i=r.get(c.renamedFrom)),!i&&l.has(c.name)){let d=ie(this.n,"add",c.name);o.parked.set(c.name,d),o.cols.set(c.name,d);continue}if(!i||a.has(i.name)){o.cols.set(c.name,c.name);continue}if(a.add(i.name),i.name!==c.name&&o.renamed.set(i.name,c.name),Re(i.type)!==Re(c.type)){let d=ie(this.n,"new",c.name);o.retyped.set(c.name,{shadow:d,from:i,to:c}),o.cols.set(c.name,d)}else o.cols.set(c.name,i.name)}return o}inNewNames(e){let s=[...this.matches.values()].find(a=>a.from===e)?.renamed??new Map,r=a=>s.get(a)??a,o=a=>Oe(a,s);return{...e,uniques:e.uniques.map(a=>({...a,columns:a.columns.map(r)})),indexes:e.indexes.map(a=>({...a,keys:a.keys.map(l=>"column"in l?{...l,column:r(l.column)}:{...l,expr:o(l.expr)}),include:a.include?.map(r),where:a.where===void 0?void 0:o(a.where)})),checks:e.checks.map(a=>({...a,expr:o(a.expr)})),foreignKeys:e.foreignKeys.map(a=>this.fkInNewNames(a,r)),policies:e.policies.map(a=>({...a,using:a.using===void 0?void 0:o(a.using),withCheck:a.withCheck===void 0?void 0:o(a.withCheck)})),primaryKey:e.primaryKey.map(r)}}fkInNewNames(e,n){let s=this.from.tables.find(o=>o.schema===e.refSchema&&o.name===e.refTable),r=s&&[...this.matches.values()].find(o=>o.from===s);return{...e,columns:e.columns.map(n),refTable:r?r.to.name:e.refTable,refColumns:e.refColumns.map(o=>r?.renamed.get(o)??o)}}where(e){let n=this.matches.get(D(e));return n?{table:n.physical,cols:n.cols}:{table:e.name,cols:new Map(e.columns.map(s=>[s.name,s.name]))}}grants(e){let{anon:n,authenticated:s,service:r}=this.input.roles;return[`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ${e} TO ${[n,s,r].map(N).join(", ")}`]}typeSql(e){let n=/^([a-z_][a-z0-9_]*)\.([a-z_][a-z0-9_]*)((?:\[\])*)$/i.exec(e);return n?`${P(n[1],n[2])}${n[3]}`:e}columnSql(e){let n=`${N(e.name)} ${this.typeSql(e.type)}`;return e.identity&&(n+=` GENERATED ${e.identity==="always"?"ALWAYS":"BY DEFAULT"} AS IDENTITY`),e.generated&&(n+=` GENERATED ALWAYS AS (${e.generated.expr}) ${e.generated.stored?"STORED":"VIRTUAL"}`),e.default!==void 0&&(n+=` DEFAULT ${e.default}`),e.nullable||(n+=" NOT NULL"),n}sequences(e,n,s,r){let{anon:o,authenticated:a,service:l}=this.input.roles;for(let c of Oo(n.default)){let i=new RegExp(String.raw`\bCREATE\s+SEQUENCE\s+(?:IF\s+NOT\s+EXISTS\s+)?${c.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}(?![\w$"])`,"i");if(this.to.raw.some(d=>i.test(d.sql))){r.push(`GRANT USAGE, SELECT ON SEQUENCE ${c} TO ${[o,a,l].map(N).join(", ")}`);continue}s.push(`CREATE SEQUENCE IF NOT EXISTS ${c}`),r.push(`ALTER SEQUENCE ${c} OWNED BY ${P(e.schema,e.name)}.${N(n.name)}`,`GRANT USAGE, SELECT ON SEQUENCE ${c} TO ${[o,a,l].map(N).join(", ")}`)}}createTable(e){let n=P(e.schema,e.name),s=new Map(e.columns.map(c=>[c.name,c.name]));this.changes.push(`table ${D(e)}`);let r=[];for(let c of e.columns)this.sequences(e,c,this.creates,r);let o=e.columns.map(c=>this.columnSql(c)),a=new Set,l=[];e.primaryKey.length>0&&(o.push(`PRIMARY KEY (${ye(e.primaryKey)})`),a.add(e.primaryKey.join("\0")));for(let c of e.uniques){let i=c.columns.join("\0");a.has(i)?l.push(`ALTER TABLE ${n} ADD CONSTRAINT ${N(c.name)} UNIQUE (${ye(c.columns)})`):o.push(`CONSTRAINT ${N(c.name)} UNIQUE (${ye(c.columns)})`),a.add(i)}for(let c of e.checks)o.push(`CONSTRAINT ${N(c.name)} CHECK (${c.expr})`);this.creates.push(`CREATE TABLE ${n} (
72
+ END $palgres$`,`REVOKE ALL ON FUNCTION ${s}.promote(text) FROM PUBLIC`,`GRANT EXECUTE ON FUNCTION ${s}.promote(text) TO ${n}`,`COMMENT ON SCHEMA ${s} IS ${M(Wn(t))}`]}var St=new Set,jn={listen(t){return St.add(t),()=>{St.delete(t)}},committed(){for(let t of St)t()}};var Ws=new Set(["tx","statement","backfill"]),Gn=1,Te=(t,e)=>`palgres_${t}_${e}`,js=t=>Co("sha256").update(t).digest("hex").slice(0,8),bt=(t,e)=>`${t.name}_${e}_${js(JSON.stringify([t.schema,t.name,e])).slice(0,6)}`,ie=(t,e,n)=>{let s=`_palgres_${t}_${e}_${n}`;return Buffer.byteLength(s)<=63?s:`_palgres_${t}_${e}_${js(n)}`},Nt={anon:"anon",backend_anon:"anon",authenticated:"authenticated",backend_authenticated:"authenticated",service_role:"service",backend_service_role:"service",public:"public"},qs=t=>!t.nullable&&t.default===void 0&&!t.identity&&!t.generated,Fs="the version serving now reads that value in the rows the new version writes";function Lt(t){return new kt(t).run()}var kt=class{from;to;fromN;n;input;changes=[];refusals=[];matches=new Map;fromMatched=new Set;prelude=[];creates=[];createFks=[];alters=[];backfills=[];online=[];settle=[];lateFks=[];searchStart=[];searchComplete=[];undoSearch=[];viewsAtComplete=null;namesAtComplete=!1;dropsColumnsAtComplete=!1;completeUnitDrops=[];raws=[];completeTables=[];completeIndexes=[];completeDrops=[];completeTypes=[];undoTables=[];undoOnline=[];fkDrops=[];undoFksGone=[];undoIndexesGone=[];undoFksBack=[];undoRawsGone=[];undoRawsBack=[];undoCreates=[];undoNewTables=[];layout={tables:[]};constructor(e){this.input=e,this.from=ge(e.from?.schema??pt());let n=new Map((e.from?.schema.raw??[]).map((r,o)=>[r.name,o]));this.from.raw.sort((r,o)=>(n.get(r.name)??0)-(n.get(o.name)??0)),this.to=ge(e.to);let s=new Map(e.to.raw.map((r,o)=>[r.name,o]));this.to.raw.sort((r,o)=>(s.get(r.name)??0)-(s.get(o.name)??0)),this.fromN=e.from?.n??null,this.n=e.next}run(){this.extensions(),this.schemas(),this.enums(),this.rawUnitsBefore(),this.matchTables();for(let r of this.to.tables){let o=this.matches.get(D(r));o?this.alterTable(o):this.createTable(r),this.search(r,o)}for(let r of this.from.tables)this.fromMatched.has(r)||this.dropTable(r);this.rawUnits(),this.enumDrops();let e=[...this.prelude,...this.creates.length+this.createFks.length>0?[{kind:"tx",sql:[...this.creates,...this.createFks],why:"the new tables and types"}]:[],...this.fkDrops,...this.alters,...this.backfills,...this.online,...this.settle,...this.lateFks,...this.searchStart,...this.raws,{kind:"tx",sql:this.views(),why:`version ${this.n}'s views`}],n=[...this.fromN!==null?[{kind:"tx",sql:this.dropViews(this.fromN,this.from),why:`version ${this.fromN}'s views`}]:[],...this.searchComplete,...this.viewsAtComplete?[{kind:"tx",sql:["SET LOCAL check_function_bodies = off",...this.completeUnitDrops,...this.viewsAtComplete.drops,...this.completeTables.flatMap(r=>r.kind==="tx"?r.sql:[]),...this.viewsAtComplete.creates],why:"the tables, and the views that read them, at once"}]:[...this.completeUnitDrops.length>0?[{kind:"tx",sql:this.completeUnitDrops,why:"the triggers and rules gone"}]:[],...this.completeTables],...this.completeIndexes,...this.completeDrops.length>0?[{kind:"tx",sql:[`DROP TABLE IF EXISTS ${this.completeDrops.join(", ")}`],why:"the dropped tables"}]:[],...this.completeTypes.length>0?[{kind:"tx",sql:this.completeTypes,why:"the dropped types"}]:[]],s=[{kind:"tx",sql:this.dropViews(this.n,this.to),why:`version ${this.n}'s views`},...this.undoSearch,...this.undoFksGone,...this.undoRawsGone.length>0?[{kind:"tx",sql:["SET LOCAL check_function_bodies = off",...this.undoRawsGone],why:"the units start made, taken back"}]:[],...[...this.undoTables].reverse(),...this.undoIndexesGone,...this.undoFksBack,...this.undoRawsBack.length>0?[{kind:"tx",sql:["SET LOCAL check_function_bodies = off",...this.undoRawsBack],why:"the units start replaced, back"}]:[],...this.undoOnline,...this.undoNewTables.length+this.undoCreates.length>0?[{kind:"tx",sql:[...this.undoNewTables.length>0?[`DROP TABLE IF EXISTS ${this.undoNewTables.join(", ")}`]:[],...[...this.undoCreates].reverse()],why:"the new tables and types"}]:[]];return{format:Gn,from:this.fromN,to:this.n,changes:this.changes,refusals:this.refusals,start:e,complete:n,rollback:s,layout:this.layout}}refuse(e,n){this.refusals.push(`${e}: ${n}`)}extensions(){for(let e of this.to.extensions)this.from.extensions.includes(e)||(this.changes.push(`extension ${e}`),this.prelude.push({kind:"statement",sql:`CREATE EXTENSION IF NOT EXISTS ${b(e)}`,why:`extension ${e}`}))}schemas(){let e=new Set([...this.from.tables.map(a=>a.schema),...this.from.enums.map(a=>a.schema),...this.from.raw.flatMap(a=>a.schema?[a.schema]:[]),"public"]),n=[...new Set([...this.to.tables.map(a=>a.schema),...this.to.enums.map(a=>a.schema),...this.to.raw.flatMap(a=>a.schema?[a.schema]:[])])].filter(a=>!e.has(a)),{anon:s,authenticated:r,service:o}=this.input.roles;for(let a of n)this.changes.push(`schema ${a}`),this.creates.push(`CREATE SCHEMA IF NOT EXISTS ${b(a)}`,`GRANT USAGE ON SCHEMA ${b(a)} TO ${[s,r,o].map(b).join(", ")}`)}enums(){for(let e of this.to.enums){let n=this.from.enums.find(o=>o.schema===e.schema&&o.name===e.name),s=P(e.schema,e.name);if(!n){this.changes.push(`type ${e.schema}.${e.name}`),this.creates.push(`CREATE TYPE ${s} AS ENUM (${e.values.map(M).join(", ")})`),this.undoCreates.push(`DROP TYPE IF EXISTS ${s}`);continue}let r=n.values.filter(o=>!e.values.includes(o));r.length>0&&this.refuse(`type ${e.schema}.${e.name} loses ${r.join(", ")}`,"Postgres cannot take a value out of an enum; add a new type and move the column to it");for(let o of e.values)n.values.includes(o)||(this.changes.push(`type ${e.schema}.${e.name} gains '${o}'`),this.prelude.push({kind:"statement",sql:`ALTER TYPE ${s} ADD VALUE IF NOT EXISTS ${M(o)}`,why:`${e.name} gains ${o}`}))}}enumDrops(){for(let e of this.from.enums)this.to.enums.some(n=>n.schema===e.schema&&n.name===e.name)||(this.changes.push(`type ${e.schema}.${e.name} goes (at complete)`),this.completeTypes.push(`DROP TYPE IF EXISTS ${P(e.schema,e.name)}`))}matchTables(){let e=new Map(this.from.tables.map(n=>[D(n),n]));for(let n of this.to.tables)n.renamedFrom===void 0||n.renamedFrom===n.name||!e.has(`${n.schema}.${n.renamedFrom}`)||this.to.tables.some(s=>s!==n&&s.schema===n.schema&&s.name===n.renamedFrom)&&this.refuse(`table ${n.schema}.${n.renamedFrom} is renamed ${n.name}, and a new table takes the name ${n.renamedFrom} in the same deploy`,`rename it in this deploy, and make the new ${n.renamedFrom} in the next`);for(let n of this.to.tables){let s=e.get(D(n));if(n.renamedFrom!==void 0&&n.renamedFrom!==n.name){let r=e.get(`${n.schema}.${n.renamedFrom}`);s&&r&&this.refuse(`table ${D(n)} is renamed from ${n.renamedFrom}, but ${D(n)} is there already`,"remove renamedFrom, or rename to a free name"),!s&&r&&(s=r)}!s||this.fromMatched.has(s)||(this.fromMatched.add(s),this.matches.set(D(n),this.matchColumns(n,s)))}}matchColumns(e,n){let s=D(e),r=new Map(n.columns.map(c=>[c.name,c])),o={to:e,from:n,physical:n.name,renamed:new Map,cols:new Map,retyped:new Map,parked:new Map},a=new Set,i=new Set(e.columns.filter(c=>c.renamedFrom!==void 0&&c.renamedFrom!==c.name&&r.has(c.renamedFrom)&&!r.has(c.name)).map(c=>c.renamedFrom));for(let c of e.columns){let l=i.has(c.name)?void 0:r.get(c.name);if(c.renamedFrom!==void 0&&c.renamedFrom!==c.name&&r.has(c.renamedFrom)&&(r.has(c.name)?this.refuse(`column ${s}.${c.name} is renamed from ${c.renamedFrom}, but ${c.name} is there already`,"remove renamedFrom, or rename to a free name"):l=r.get(c.renamedFrom)),!l&&i.has(c.name)){let u=ie(this.n,"add",c.name);o.parked.set(c.name,u),o.cols.set(c.name,u);continue}if(!l||a.has(l.name)){o.cols.set(c.name,c.name);continue}if(a.add(l.name),l.name!==c.name&&o.renamed.set(l.name,c.name),be(l.type)!==be(c.type)){let u=ie(this.n,"new",c.name);o.retyped.set(c.name,{shadow:u,from:l,to:c}),o.cols.set(c.name,u)}else o.cols.set(c.name,l.name)}return o}inNewNames(e){let s=[...this.matches.values()].find(a=>a.from===e)?.renamed??new Map,r=a=>s.get(a)??a,o=a=>ke(a,s);return{...e,uniques:e.uniques.map(a=>({...a,columns:a.columns.map(r)})),indexes:e.indexes.map(a=>({...a,keys:a.keys.map(i=>"column"in i?{...i,column:r(i.column)}:{...i,expr:o(i.expr)}),include:a.include?.map(r),where:a.where===void 0?void 0:o(a.where)})),checks:e.checks.map(a=>({...a,expr:o(a.expr)})),foreignKeys:e.foreignKeys.map(a=>this.fkInNewNames(a,r)),policies:e.policies.map(a=>({...a,using:a.using===void 0?void 0:o(a.using),withCheck:a.withCheck===void 0?void 0:o(a.withCheck)})),primaryKey:e.primaryKey.map(r)}}fkInNewNames(e,n){let s=this.from.tables.find(o=>o.schema===e.refSchema&&o.name===e.refTable),r=s&&[...this.matches.values()].find(o=>o.from===s);return{...e,columns:e.columns.map(n),refTable:r?r.to.name:e.refTable,refColumns:e.refColumns.map(o=>r?.renamed.get(o)??o)}}where(e){let n=this.matches.get(D(e));return n?{table:n.physical,cols:n.cols}:{table:e.name,cols:new Map(e.columns.map(s=>[s.name,s.name]))}}grants(e){let{anon:n,authenticated:s,service:r}=this.input.roles;return[`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ${e} TO ${[n,s,r].map(b).join(", ")}`]}typeSql(e){let n=/^([a-z_][a-z0-9_]*)\.([a-z_][a-z0-9_]*)((?:\[\])*)$/i.exec(e);return n?`${P(n[1],n[2])}${n[3]}`:e}columnSql(e){let n=`${b(e.name)} ${this.typeSql(e.type)}`;return e.identity&&(n+=` GENERATED ${e.identity==="always"?"ALWAYS":"BY DEFAULT"} AS IDENTITY`),e.generated&&(n+=` GENERATED ALWAYS AS (${e.generated.expr}) ${e.generated.stored?"STORED":"VIRTUAL"}`),e.default!==void 0&&(n+=` DEFAULT ${e.default}`),e.nullable||(n+=" NOT NULL"),n}sequences(e,n,s,r){let{anon:o,authenticated:a,service:i}=this.input.roles;for(let c of _o(n.default)){let l=new RegExp(String.raw`\bCREATE\s+SEQUENCE\s+(?:IF\s+NOT\s+EXISTS\s+)?${c.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}(?![\w$"])`,"i");if(this.to.raw.some(u=>l.test(u.sql))){r.push(`GRANT USAGE, SELECT ON SEQUENCE ${c} TO ${[o,a,i].map(b).join(", ")}`);continue}s.push(`CREATE SEQUENCE IF NOT EXISTS ${c}`),r.push(`ALTER SEQUENCE ${c} OWNED BY ${P(e.schema,e.name)}.${b(n.name)}`,`GRANT USAGE, SELECT ON SEQUENCE ${c} TO ${[o,a,i].map(b).join(", ")}`)}}createTable(e){let n=P(e.schema,e.name),s=new Map(e.columns.map(c=>[c.name,c.name]));this.changes.push(`table ${D(e)}`);let r=[];for(let c of e.columns)this.sequences(e,c,this.creates,r);let o=e.columns.map(c=>this.columnSql(c)),a=new Set,i=[];e.primaryKey.length>0&&(o.push(`PRIMARY KEY (${ye(e.primaryKey)})`),a.add(e.primaryKey.join("\0")));for(let c of e.uniques){let l=c.columns.join("\0");a.has(l)?i.push(`ALTER TABLE ${n} ADD CONSTRAINT ${b(c.name)} UNIQUE (${ye(c.columns)})`):o.push(`CONSTRAINT ${b(c.name)} UNIQUE (${ye(c.columns)})`),a.add(l)}for(let c of e.checks)o.push(`CONSTRAINT ${b(c.name)} CHECK (${c.expr})`);this.creates.push(`CREATE TABLE ${n} (
73
73
  ${o.join(`,
74
74
  `)}
75
- )`,...l,...r);for(let c of e.indexes)this.creates.push(this.indexSql(e.schema,e.name,c,s,!1));for(let c of e.foreignKeys){let i=`${c.refSchema}.${c.refTable}`;this.from.tables.some(d=>D(d)===i)&&i!==D(e)?(this.lateFks.push({kind:"tx",sql:[`ALTER TABLE ${n} ADD ${this.fkSql(c,s)} NOT VALID`],why:`foreign key ${c.name}`}),this.lateFks.push({kind:"statement",sql:`ALTER TABLE ${n} VALIDATE CONSTRAINT ${N(c.name)}`,why:`foreign key ${c.name}`}),this.undoFksGone.push({kind:"tx",sql:[`ALTER TABLE IF EXISTS ${n} DROP CONSTRAINT IF EXISTS ${N(c.name)}`],why:`foreign key ${c.name}`})):this.createFks.push(`ALTER TABLE ${n} ADD ${this.fkSql(c,s)}`)}this.creates.push(...$t(n,e.rls,!1));for(let c of e.policies)this.readsOthersRenamed(e,c,D(e))||this.createFks.push(this.policySql(n,c,s));this.creates.push(...this.grants(n)),this.undoNewTables.push(n),this.addLayout(e,e.name,s)}dropTable(e){if(!e.ignored&&this.input.hasRows(D(e))){this.refuse(`table ${D(e)} is gone from the schema and has rows`,"mark it ignored first (defineTable(\u2026, { ignored: true })) and deploy; remove it in the deploy after that, when its data may go");return}this.changes.push(`table ${D(e)} goes (at complete)`),this.searchGone(e,e.name),this.completeDrops.push(P(e.schema,e.name))}addLayout(e,n,s){e.ignored||this.layout.tables.push({schema:e.schema,name:e.name,physical:n,columns:e.columns.filter(r=>!r.ignored).map(r=>({name:r.name,physical:s.get(r.name)}))})}alterTable(e){let{to:n,from:s}=e,r=P(n.schema,e.physical),o=D(n),a={tx:[],undo:[],undoColumns:[],complete:[]},l=this.input.hasRows(D(s));n.name!==s.name&&this.changes.push(`table ${n.schema}.${s.name} is renamed ${n.name} (at complete)`);let c=new Map(s.columns.map(u=>[u.name,u])),i=new Set;for(let u of n.columns){let f=e.cols.get(u.name),g=e.retyped.get(u.name),p=g?.from??(e.parked.has(u.name)?void 0:c.get(f));if(!p){this.addColumn(n,r,f===u.name?u:{...u,name:f},a);continue}if(i.add(p.name),g){this.retype(e,r,u.name,g,a);continue}p.name!==u.name&&(this.changes.push(`column ${o}.${p.name} is renamed ${u.name} (at complete)`),a.complete.push(`ALTER TABLE ${r} RENAME COLUMN ${N(p.name)} TO ${N(u.name)}`),a.names=!0),u.ignored&&!p.ignored&&this.insertsWithout(o,u),this.alterColumn(n,r,p,u,a)}for(let u of s.columns)if(!i.has(u.name)){if(!u.ignored&&l){let f=xs(u)?` \u2014 and, being NOT NULL with no default, with one (.default(\u2026)): ${Is}`:"";this.refuse(`column ${D(s)}.${u.name} is gone from the schema and the table has rows`,`mark it ignored first (.ignored())${f}, and deploy; remove it in the deploy after that, when its data may go`);continue}u.ignored||this.insertsWithout(o,u),this.changes.push(`column ${D(s)}.${u.name} goes (at complete)`),a.complete.push(`ALTER TABLE ${r} DROP COLUMN IF EXISTS ${N(u.name)}`),a.names=!0,a.dropsColumns=!0}this.constraints(e,r,a),this.policies(e,r,a),s.rls!==n.rls&&(this.changes.push(`table ${o}: row-level security ${n.rls?"on":"off"}`),a.tx.push(...$t(r,n.rls,!0)),a.undo.push(...$t(r,s.rls,!0))),this.inNewNames(s).primaryKey.join()!==n.primaryKey.join()&&this.refuse(`the primary key of ${o} changes`,"a primary key cannot change while both versions write the table; add a unique constraint instead"),a.tx.length>0&&this.alters.push({kind:"tx",sql:a.tx,why:`table ${o}`});let d=[...a.undo,...a.undoColumns];d.length>0&&this.undoTables.push({kind:"tx",sql:d,why:`table ${o}`});for(let[u,f]of e.parked)this.changes.push(`column ${o}.${u} is made as ${f}, and takes its name at complete`),a.complete.push(`ALTER TABLE ${r} RENAME COLUMN ${N(f)} TO ${N(u)}`),a.names=!0;n.name!==s.name&&(a.complete.push(`ALTER TABLE ${r} RENAME TO ${N(n.name)}`),a.names=!0),a.names&&(this.namesAtComplete=!0),a.dropsColumns&&(this.dropsColumnsAtComplete=!0),a.complete.length>0&&this.completeTables.push({kind:"tx",sql:a.complete,why:`table ${o}`}),this.addLayout(n,e.physical,e.cols)}sequenceRenamed(e,n){let[s,r]=jn(e.default),[o,a]=jn(n.default);if(s===void 0||o===void 0||r!==void 0||a!==void 0||(s.schema??null)!==(o.schema??null)||s.name===o.name)return;let l=g=>g.replace(Ms,"nextval()");if(l(e.default)!==l(n.default))return;let c=g=>p=>p.name===g.name&&(p.schema??null)===(g.schema??null),i=g=>this.from.tables.flatMap(p=>p.columns).filter(p=>jn(p.default).some(c(g))).length;if(i(s)!==1||i(o)!==0)return;let d=g=>g.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),u=o.schema===void 0?String.raw`[^".\s]+`:d(o.schema),f=new RegExp(String.raw`\bSEQUENCE\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:"?${u}"?\.)?"?${d(o.name)}"?(?![\w$"])`,"i");if(!this.from.raw.some(g=>f.test(g.sql)))return{from:s.schema===void 0?N(s.name):P(s.schema,s.name),to:o.name}}insertsWithout(e,n){xs(n)&&this.refuse(`column ${e}.${n.name} is NOT NULL with no default, and the new version inserts rows without it`,`give it a default (.default(\u2026)): ${Is} \u2014 or make it nullable in this deploy, if that version can read it empty`)}addColumn(e,n,s,r){let o=[];this.sequences(e,s,r.tx,o),this.addColumnItself(e,n,s,r),r.tx.push(...o)}addColumnItself(e,n,s,r){let o=D(e),a=N(s.name);if(this.changes.push(`column ${o}.${s.name}`),r.undoColumns.push(`ALTER TABLE ${n} DROP COLUMN IF EXISTS ${a}`),s.identity){this.input.hasRows(D(this.matches.get(o).from))?this.refuse(`column ${o}.${s.name} is an identity column added to a table with rows`,"Postgres would rewrite the table under a lock; add a bigint column with a default and fill it"):r.tx.push(`ALTER TABLE ${n} ADD COLUMN ${this.columnSql(s)}`);return}if(s.generated){s.generated.stored&&this.input.hasRows(D(this.matches.get(o).from))?this.refuse(`column ${o}.${s.name} is a stored generated column added to a table with rows`,"Postgres would rewrite the table under a lock: add it virtual (.generated(expr)), or as a plain column a trigger keeps"):r.tx.push(`ALTER TABLE ${n} ADD COLUMN ${this.columnSql(s)}`);return}if(this.input.hasRows(D(this.matches.get(o).from))&&this.checkedDomain(s.type)){this.refuse(`column ${o}.${s.name} is of ${s.type}, a domain with a CHECK, added to a table with rows`,"Postgres would rewrite the table under a lock to check every row: add it as the domain's base type with a CHECK (added NOT VALID, then validated), or before the table has rows");return}if(!(s.default!==void 0&&bs(s.default))&&(s.nullable||s.default!==void 0)){r.tx.push(`ALTER TABLE ${n} ADD COLUMN ${this.columnSql(s)}`);return}if(!s.nullable&&s.default===void 0&&s.fill===void 0){this.refuse(`column ${o}.${s.name} is NOT NULL with no default`,"the version serving now inserts rows without it: give it a default (.default(\u2026)) or the value for those rows (.fillWith(sql))");return}r.tx.push(`ALTER TABLE ${n} ADD COLUMN ${a} ${this.typeSql(s.type)}`),s.default!==void 0?(r.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${a} SET DEFAULT ${s.default}`),this.backfills.push({kind:"backfill",table:n,key:this.keyOf(e),set:`${a} = ${s.default}`,where:`${a} IS NULL`,why:`${o}.${s.name} takes its default`})):this.fill(e,n,s.name,s.fill,r),s.nullable||this.notNull(e,n,s.name,r)}fill(e,n,s,r,o){let a=D(e),l=P(e.schema,ie(this.n,"fill",yt(e,s))),c=N(ie(this.n,"fill",s)),i=N(s),d=this.where(e),u=Oe(r,ln(d.cols));o.tx.push(`CREATE OR REPLACE FUNCTION ${l}() RETURNS trigger LANGUAGE plpgsql AS $palgres$
75
+ )`,...i,...r);for(let c of e.indexes)this.creates.push(this.indexSql(e.schema,e.name,c,s,!1));for(let c of e.foreignKeys){let l=`${c.refSchema}.${c.refTable}`;this.from.tables.some(u=>D(u)===l)&&l!==D(e)?(this.lateFks.push({kind:"tx",sql:[`ALTER TABLE ${n} ADD ${this.fkSql(c,s)} NOT VALID`],why:`foreign key ${c.name}`}),this.lateFks.push({kind:"statement",sql:`ALTER TABLE ${n} VALIDATE CONSTRAINT ${b(c.name)}`,why:`foreign key ${c.name}`}),this.undoFksGone.push({kind:"tx",sql:[`ALTER TABLE IF EXISTS ${n} DROP CONSTRAINT IF EXISTS ${b(c.name)}`],why:`foreign key ${c.name}`})):this.createFks.push(`ALTER TABLE ${n} ADD ${this.fkSql(c,s)}`)}this.creates.push(...At(n,e.rls,!1));for(let c of e.policies)this.readsOthersRenamed(e,c,D(e))||this.createFks.push(this.policySql(n,c,s));this.creates.push(...this.grants(n)),this.undoNewTables.push(n),this.addLayout(e,e.name,s)}dropTable(e){if(!e.ignored&&this.input.hasRows(D(e))){this.refuse(`table ${D(e)} is gone from the schema and has rows`,"mark it ignored first (defineTable(\u2026, { ignored: true })) and deploy; remove it in the deploy after that, when its data may go");return}this.changes.push(`table ${D(e)} goes (at complete)`),this.searchGone(e,e.name),this.completeDrops.push(P(e.schema,e.name))}addLayout(e,n,s){e.ignored||this.layout.tables.push({schema:e.schema,name:e.name,physical:n,columns:e.columns.filter(r=>!r.ignored).map(r=>({name:r.name,physical:s.get(r.name)}))})}alterTable(e){let{to:n,from:s}=e,r=P(n.schema,e.physical),o=D(n),a={tx:[],undo:[],undoColumns:[],complete:[]},i=this.input.hasRows(D(s));n.name!==s.name&&this.changes.push(`table ${n.schema}.${s.name} is renamed ${n.name} (at complete)`);let c=new Map(s.columns.map(g=>[g.name,g])),l=new Set;for(let g of n.columns){let p=e.cols.get(g.name),h=e.retyped.get(g.name),m=h?.from??(e.parked.has(g.name)?void 0:c.get(p));if(!m){this.addColumn(n,r,p===g.name?g:{...g,name:p},a);continue}if(l.add(m.name),h){this.retype(e,r,g.name,h,a);continue}m.name!==g.name&&(this.changes.push(`column ${o}.${m.name} is renamed ${g.name} (at complete)`),a.complete.push(`ALTER TABLE ${r} RENAME COLUMN ${b(m.name)} TO ${b(g.name)}`),a.names=!0),g.ignored&&!m.ignored&&this.insertsWithout(o,g),this.alterColumn(n,r,m,g,a)}for(let g of s.columns)if(!l.has(g.name)){if(!g.ignored&&i){let p=qs(g)?` \u2014 and, being NOT NULL with no default, with one (.default(\u2026)): ${Fs}`:"";this.refuse(`column ${D(s)}.${g.name} is gone from the schema and the table has rows`,`mark it ignored first (.ignored())${p}, and deploy; remove it in the deploy after that, when its data may go`);continue}g.ignored||this.insertsWithout(o,g),this.changes.push(`column ${D(s)}.${g.name} goes (at complete)`),a.complete.push(`ALTER TABLE ${r} DROP COLUMN IF EXISTS ${b(g.name)}`),a.names=!0,a.dropsColumns=!0}this.constraints(e,r,a),this.policies(e,r,a),s.rls!==n.rls&&(this.changes.push(`table ${o}: row-level security ${n.rls?"on":"off"}`),a.tx.push(...At(r,n.rls,!0)),a.undo.push(...At(r,s.rls,!0))),this.inNewNames(s).primaryKey.join()!==n.primaryKey.join()&&this.refuse(`the primary key of ${o} changes`,"a primary key cannot change while both versions write the table; add a unique constraint instead"),a.tx.length>0&&this.alters.push({kind:"tx",sql:a.tx,why:`table ${o}`});let u=[...a.undo,...a.undoColumns];u.length>0&&this.undoTables.push({kind:"tx",sql:u,why:`table ${o}`});for(let[g,p]of e.parked)this.changes.push(`column ${o}.${g} is made as ${p}, and takes its name at complete`),a.complete.push(`ALTER TABLE ${r} RENAME COLUMN ${b(p)} TO ${b(g)}`),a.names=!0;n.name!==s.name&&(a.complete.push(`ALTER TABLE ${r} RENAME TO ${b(n.name)}`),a.names=!0),a.names&&(this.namesAtComplete=!0),a.dropsColumns&&(this.dropsColumnsAtComplete=!0),a.complete.length>0&&this.completeTables.push({kind:"tx",sql:a.complete,why:`table ${o}`}),this.addLayout(n,e.physical,e.cols)}sequenceRenamed(e,n){let[s,r]=Vn(e.default),[o,a]=Vn(n.default);if(s===void 0||o===void 0||r!==void 0||a!==void 0||(s.schema??null)!==(o.schema??null)||s.name===o.name)return;let i=h=>h.replace(Hs,"nextval()");if(i(e.default)!==i(n.default))return;let c=h=>m=>m.name===h.name&&(m.schema??null)===(h.schema??null),l=h=>this.from.tables.flatMap(m=>m.columns).filter(m=>Vn(m.default).some(c(h))).length;if(l(s)!==1||l(o)!==0)return;let u=h=>h.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),g=o.schema===void 0?String.raw`[^".\s]+`:u(o.schema),p=new RegExp(String.raw`\bSEQUENCE\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:"?${g}"?\.)?"?${u(o.name)}"?(?![\w$"])`,"i");if(!this.from.raw.some(h=>p.test(h.sql)))return{from:s.schema===void 0?b(s.name):P(s.schema,s.name),to:o.name}}insertsWithout(e,n){qs(n)&&this.refuse(`column ${e}.${n.name} is NOT NULL with no default, and the new version inserts rows without it`,`give it a default (.default(\u2026)): ${Fs} \u2014 or make it nullable in this deploy, if that version can read it empty`)}addColumn(e,n,s,r){let o=[];this.sequences(e,s,r.tx,o),this.addColumnItself(e,n,s,r),r.tx.push(...o)}addColumnItself(e,n,s,r){let o=D(e),a=b(s.name);if(this.changes.push(`column ${o}.${s.name}`),r.undoColumns.push(`ALTER TABLE ${n} DROP COLUMN IF EXISTS ${a}`),s.identity){this.input.hasRows(D(this.matches.get(o).from))?this.refuse(`column ${o}.${s.name} is an identity column added to a table with rows`,"Postgres would rewrite the table under a lock; add a bigint column with a default and fill it"):r.tx.push(`ALTER TABLE ${n} ADD COLUMN ${this.columnSql(s)}`);return}if(s.generated){s.generated.stored&&this.input.hasRows(D(this.matches.get(o).from))?this.refuse(`column ${o}.${s.name} is a stored generated column added to a table with rows`,"Postgres would rewrite the table under a lock: add it virtual (.generated(expr)), or as a plain column a trigger keeps"):r.tx.push(`ALTER TABLE ${n} ADD COLUMN ${this.columnSql(s)}`);return}if(this.input.hasRows(D(this.matches.get(o).from))&&this.checkedDomain(s.type)){this.refuse(`column ${o}.${s.name} is of ${s.type}, a domain with a CHECK, added to a table with rows`,"Postgres would rewrite the table under a lock to check every row: add it as the domain's base type with a CHECK (added NOT VALID, then validated), or before the table has rows");return}if(!(s.default!==void 0&&vs(s.default))&&(s.nullable||s.default!==void 0)){r.tx.push(`ALTER TABLE ${n} ADD COLUMN ${this.columnSql(s)}`);return}if(!s.nullable&&s.default===void 0&&s.fill===void 0){this.refuse(`column ${o}.${s.name} is NOT NULL with no default`,"the version serving now inserts rows without it: give it a default (.default(\u2026)) or the value for those rows (.fillWith(sql))");return}r.tx.push(`ALTER TABLE ${n} ADD COLUMN ${a} ${this.typeSql(s.type)}`),s.default!==void 0?(r.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${a} SET DEFAULT ${s.default}`),this.backfills.push({kind:"backfill",table:n,key:this.keyOf(e),set:`${a} = ${s.default}`,where:`${a} IS NULL`,why:`${o}.${s.name} takes its default`})):this.fill(e,n,s.name,s.fill,r),s.nullable||this.notNull(e,n,s.name,r)}fill(e,n,s,r,o){let a=D(e),i=P(e.schema,ie(this.n,"fill",bt(e,s))),c=b(ie(this.n,"fill",s)),l=b(s),u=this.where(e),g=ke(r,un(u.cols));o.tx.push(`CREATE OR REPLACE FUNCTION ${i}() RETURNS trigger LANGUAGE plpgsql AS $palgres$
76
76
  BEGIN
77
- IF NEW.${i} IS NULL THEN
78
- NEW.${i} := (SELECT (
79
- ${u}
80
- ) FROM (SELECT (NEW).*) AS ${N(d.table)});
77
+ IF NEW.${l} IS NULL THEN
78
+ NEW.${l} := (SELECT (
79
+ ${g}
80
+ ) FROM (SELECT (NEW).*) AS ${b(u.table)});
81
81
  END IF;
82
82
  RETURN NEW;
83
- END $palgres$`,`CREATE TRIGGER ${c} BEFORE INSERT OR UPDATE ON ${n} FOR EACH ROW EXECUTE FUNCTION ${l}()`);let f=[`DROP TRIGGER IF EXISTS ${c} ON ${n}`,`DROP FUNCTION IF EXISTS ${l}()`];o.undo.push(...f),o.complete.unshift(...f),this.backfills.push({kind:"backfill",table:n,key:this.keyOf(e),set:`${i} = (
84
- ${u}
85
- )`,where:`${i} IS NULL`,why:`${a}.${s} is filled`})}relaxNotNull(e,n,s,r){let o=N(s);r.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${o} DROP NOT NULL`);let a=N(ie(this.n,"nn",s)),l=`EXISTS (SELECT 1 FROM pg_constraint WHERE conname = ${F(a.slice(1,-1))} AND conrelid = ${F(n)}::regclass`;this.undoOnline.push({kind:"tx",sql:[`DO $palgres$ BEGIN IF NOT ${l}) THEN ALTER TABLE ${n} ADD CONSTRAINT ${a} CHECK (${o} IS NOT NULL) NOT VALID; END IF; END $palgres$`],why:`${e}.${s} NOT NULL again`},{kind:"statement",sql:`DO $palgres$ BEGIN IF ${l}) THEN ALTER TABLE ${n} VALIDATE CONSTRAINT ${a}; END IF; EXCEPTION WHEN check_violation THEN ALTER TABLE ${n} DROP CONSTRAINT ${a}; RAISE WARNING 'palgres: % stays nullable: NULL was written since', ${F(`${e}.${s}`)}; END $palgres$`,why:`${e}.${s} has no NULL`},{kind:"tx",sql:[`DO $palgres$ BEGIN IF ${l} AND convalidated) THEN ALTER TABLE ${n} ALTER COLUMN ${o} SET NOT NULL; END IF; END $palgres$`,`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${a}`],why:`${e}.${s} is NOT NULL`})}notNull(e,n,s,r){let o=N(ie(this.n,"nn",s)),a=N(s);this.online.push({kind:"tx",sql:[`ALTER TABLE ${n} ADD CONSTRAINT ${o} CHECK (${a} IS NOT NULL) NOT VALID`],why:`${D(e)}.${s} is held NOT NULL`}),r.undo.push(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${o}`),this.online.push({kind:"statement",sql:`ALTER TABLE ${n} VALIDATE CONSTRAINT ${o}`,why:`${D(e)}.${s} has no NULL`}),this.settle.push({kind:"tx",sql:[`ALTER TABLE ${n} ALTER COLUMN ${a} SET NOT NULL`,`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${o}`],why:`${D(e)}.${s} is NOT NULL`})}alterColumn(e,n,s,r,o){let a=D(e),l=N(s.name);if(s.nullable&&!r.nullable){let c=r.fill??r.default;c===void 0?this.refuse(`column ${a}.${r.name} becomes NOT NULL`,"the version serving now may still write NULL: give the value for those rows and writes (.fillWith(sql))"):(this.changes.push(`column ${a}.${r.name} becomes NOT NULL`),this.fill(e,n,s.name,c,o),this.notNull(e,n,s.name,o),o.undo.push(`ALTER TABLE ${n} ALTER COLUMN ${l} DROP NOT NULL`))}if(!s.nullable&&r.nullable&&(this.changes.push(`column ${a}.${r.name} becomes nullable`),this.relaxNotNull(a,n,s.name,o)),(s.default??null)!==(r.default??null)){let c=this.sequenceRenamed(s,r);c!==void 0?(this.changes.push(`sequence ${c.from} becomes ${c.to} (at complete)`),o.complete.push(`ALTER SEQUENCE ${c.from} RENAME TO ${N(c.to)}`)):r.default!==void 0?(this.changes.push(`column ${a}.${r.name} defaults to ${r.default}`),o.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${l} SET DEFAULT ${r.default}`),o.undo.push(s.default!==void 0?`ALTER TABLE ${n} ALTER COLUMN ${l} SET DEFAULT ${s.default}`:`ALTER TABLE ${n} ALTER COLUMN ${l} DROP DEFAULT`)):(this.changes.push(`column ${a}.${r.name} loses its default (at complete)`),o.complete.push(`ALTER TABLE ${n} ALTER COLUMN ${N(r.name)} DROP DEFAULT`))}(s.identity??null)!==(r.identity??null)&&this.refuse(`column ${a}.${r.name} changes identity`,"an identity cannot change while both versions write; add a new column"),this.regenerate(e,n,s,r,o)}regenerate(e,n,s,r,o){let a=D(e),l=N(s.name),c=s.generated,i=r.generated;if(!(!c&&!i)&&!(c&&i&&c.expr===i.expr&&c.stored===i.stored)){if(!c&&i){this.refuse(`column ${a}.${r.name} becomes a generated column`,"Postgres cannot make a column generated: add a new generated column and ignore the old one");return}if(c&&!i){if(!c.stored){this.refuse(`column ${a}.${r.name} stops being a virtual generated column`,"a virtual column holds no values to keep: add a plain column filled with .fillWith(expr) and ignore this one");return}this.changes.push(`column ${a}.${r.name} is no longer generated (it keeps its values; the old version's writes are computed until complete, and a rollback leaves it plain, computed so)`);let d=P(e.schema,ie(this.n,"gen",yt(e,s.name))),u=N(ie(this.n,"gen",s.name)),f=this.where(e);o.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${l} DROP EXPRESSION IF EXISTS`,`CREATE OR REPLACE FUNCTION ${d}() RETURNS trigger LANGUAGE plpgsql AS $palgres$
83
+ END $palgres$`,`CREATE TRIGGER ${c} BEFORE INSERT OR UPDATE ON ${n} FOR EACH ROW EXECUTE FUNCTION ${i}()`);let p=[`DROP TRIGGER IF EXISTS ${c} ON ${n}`,`DROP FUNCTION IF EXISTS ${i}()`];o.undo.push(...p),o.complete.unshift(...p),this.backfills.push({kind:"backfill",table:n,key:this.keyOf(e),set:`${l} = (
84
+ ${g}
85
+ )`,where:`${l} IS NULL`,why:`${a}.${s} is filled`})}relaxNotNull(e,n,s,r){let o=b(s);r.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${o} DROP NOT NULL`);let a=b(ie(this.n,"nn",s)),i=`EXISTS (SELECT 1 FROM pg_constraint WHERE conname = ${M(a.slice(1,-1))} AND conrelid = ${M(n)}::regclass`;this.undoOnline.push({kind:"tx",sql:[`DO $palgres$ BEGIN IF NOT ${i}) THEN ALTER TABLE ${n} ADD CONSTRAINT ${a} CHECK (${o} IS NOT NULL) NOT VALID; END IF; END $palgres$`],why:`${e}.${s} NOT NULL again`},{kind:"statement",sql:`DO $palgres$ BEGIN IF ${i}) THEN ALTER TABLE ${n} VALIDATE CONSTRAINT ${a}; END IF; EXCEPTION WHEN check_violation THEN ALTER TABLE ${n} DROP CONSTRAINT ${a}; RAISE WARNING 'palgres: % stays nullable: NULL was written since', ${M(`${e}.${s}`)}; END $palgres$`,why:`${e}.${s} has no NULL`},{kind:"tx",sql:[`DO $palgres$ BEGIN IF ${i} AND convalidated) THEN ALTER TABLE ${n} ALTER COLUMN ${o} SET NOT NULL; END IF; END $palgres$`,`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${a}`],why:`${e}.${s} is NOT NULL`})}notNull(e,n,s,r){let o=b(ie(this.n,"nn",s)),a=b(s);this.online.push({kind:"tx",sql:[`ALTER TABLE ${n} ADD CONSTRAINT ${o} CHECK (${a} IS NOT NULL) NOT VALID`],why:`${D(e)}.${s} is held NOT NULL`}),r.undo.push(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${o}`),this.online.push({kind:"statement",sql:`ALTER TABLE ${n} VALIDATE CONSTRAINT ${o}`,why:`${D(e)}.${s} has no NULL`}),this.settle.push({kind:"tx",sql:[`ALTER TABLE ${n} ALTER COLUMN ${a} SET NOT NULL`,`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${o}`],why:`${D(e)}.${s} is NOT NULL`})}alterColumn(e,n,s,r,o){let a=D(e),i=b(s.name);if(s.nullable&&!r.nullable){let c=r.fill??r.default;c===void 0?this.refuse(`column ${a}.${r.name} becomes NOT NULL`,"the version serving now may still write NULL: give the value for those rows and writes (.fillWith(sql))"):(this.changes.push(`column ${a}.${r.name} becomes NOT NULL`),this.fill(e,n,s.name,c,o),this.notNull(e,n,s.name,o),o.undo.push(`ALTER TABLE ${n} ALTER COLUMN ${i} DROP NOT NULL`))}if(!s.nullable&&r.nullable&&(this.changes.push(`column ${a}.${r.name} becomes nullable`),this.relaxNotNull(a,n,s.name,o)),(s.default??null)!==(r.default??null)){let c=this.sequenceRenamed(s,r);c!==void 0?(this.changes.push(`sequence ${c.from} becomes ${c.to} (at complete)`),o.complete.push(`ALTER SEQUENCE ${c.from} RENAME TO ${b(c.to)}`)):r.default!==void 0?(this.changes.push(`column ${a}.${r.name} defaults to ${r.default}`),o.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${i} SET DEFAULT ${r.default}`),o.undo.push(s.default!==void 0?`ALTER TABLE ${n} ALTER COLUMN ${i} SET DEFAULT ${s.default}`:`ALTER TABLE ${n} ALTER COLUMN ${i} DROP DEFAULT`)):(this.changes.push(`column ${a}.${r.name} loses its default (at complete)`),o.complete.push(`ALTER TABLE ${n} ALTER COLUMN ${b(r.name)} DROP DEFAULT`))}(s.identity??null)!==(r.identity??null)&&this.refuse(`column ${a}.${r.name} changes identity`,"an identity cannot change while both versions write; add a new column"),this.regenerate(e,n,s,r,o)}regenerate(e,n,s,r,o){let a=D(e),i=b(s.name),c=s.generated,l=r.generated;if(!(!c&&!l)&&!(c&&l&&c.expr===l.expr&&c.stored===l.stored)){if(!c&&l){this.refuse(`column ${a}.${r.name} becomes a generated column`,"Postgres cannot make a column generated: add a new generated column and ignore the old one");return}if(c&&!l){if(!c.stored){this.refuse(`column ${a}.${r.name} stops being a virtual generated column`,"a virtual column holds no values to keep: add a plain column filled with .fillWith(expr) and ignore this one");return}this.changes.push(`column ${a}.${r.name} is no longer generated (it keeps its values; the old version's writes are computed until complete, and a rollback leaves it plain, computed so)`);let u=P(e.schema,ie(this.n,"gen",bt(e,s.name))),g=b(ie(this.n,"gen",s.name)),p=this.where(e);o.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${i} DROP EXPRESSION IF EXISTS`,`CREATE OR REPLACE FUNCTION ${u}() RETURNS trigger LANGUAGE plpgsql AS $palgres$
86
86
  BEGIN
87
- IF position(${F(Te(this.n,""))} in replace(current_setting('search_path'), '"', '')) <> 1 THEN
88
- NEW.${l} := (SELECT (
89
- ${Oe(c.expr,ln(f.cols))}
90
- ) FROM (SELECT (NEW).*) AS ${N(f.table)});
87
+ IF position(${M(Te(this.n,""))} in replace(current_setting('search_path'), '"', '')) <> 1 THEN
88
+ NEW.${i} := (SELECT (
89
+ ${ke(c.expr,un(p.cols))}
90
+ ) FROM (SELECT (NEW).*) AS ${b(p.table)});
91
91
  END IF;
92
92
  RETURN NEW;
93
- END $palgres$`,`CREATE TRIGGER ${u} BEFORE INSERT OR UPDATE ON ${n} FOR EACH ROW EXECUTE FUNCTION ${d}()`),o.complete.unshift(`DROP TRIGGER IF EXISTS ${u} ON ${n}`,`DROP FUNCTION IF EXISTS ${d}()`);return}if(c.stored!==i.stored){this.refuse(`column ${a}.${r.name} changes between stored and virtual`,"Postgres would compute every row again: add a new generated column and ignore the old one");return}if(i.stored&&this.input.hasRows(D(this.matches.get(a).from))){this.refuse(`column ${a}.${r.name} changes its stored expression on a table with rows`,"Postgres would compute every row again under a lock: add a new generated column and ignore the old one");return}this.changes.push(`column ${a}.${r.name} is generated as ${i.expr}`),o.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${l} SET EXPRESSION AS (${i.expr})`),o.undo.push(`ALTER TABLE ${n} ALTER COLUMN ${l} SET EXPRESSION AS (${c.expr})`)}}retype(e,n,s,r,o){let a=e.to,l=D(a),c=[...a.checks.filter(p=>Ae(p.expr,s)).map(p=>`check ${p.name}`),...a.policies.filter(p=>Ae(`${p.using??""} ${p.withCheck??""}`,s)).map(p=>`policy ${p.name}`),...a.indexes.filter(p=>p.keys.some(y=>"expr"in y&&Ae(y.expr,s))||p.where!==void 0&&Ae(p.where,s)).map(p=>`index ${p.name}`),...a.primaryKey.includes(s)?["the primary key"]:[],...this.to.tables.flatMap(p=>p.foreignKeys.filter(y=>y.refSchema===a.schema&&y.refTable===a.name&&y.refColumns.includes(s)).map(y=>`foreign key ${D(p)}.${y.name}`))];if(c.length>0){this.refuse(`column ${l}.${s} changes type from ${r.from.type} to ${r.to.type}, and ${c.join(", ")} use it`,`add a column of the new type with .fillWith("${s}::${r.to.type}"), move them to it, and ignore the old one`);return}let i=this.conversion(r.from.type,r.to.type);if("refused"in i){this.refuse(`column ${l}.${s} changes type from ${r.from.type} to ${r.to.type}: ${i.refused}`,"add a column of the new type with .fillWith(\u2026) saying the value, move to it, and ignore the old one");return}if(r.from.nullable&&!r.to.nullable){this.refuse(`column ${l}.${s} changes type and becomes NOT NULL in one deploy`,"change its type in this deploy and make it NOT NULL (with .fillWith) in the next");return}this.changes.push(`column ${l}.${s} changes type from ${r.from.type} to ${r.to.type}`);let d=N(r.from.name),u=N(r.shadow),f=P(a.schema,ie(this.n,"sync",yt(a,s))),g=N(ie(this.n,"sync",s));o.tx.push(`ALTER TABLE ${n} ADD COLUMN ${u} ${this.typeSql(r.to.type)}`,`CREATE OR REPLACE FUNCTION ${f}() RETURNS trigger LANGUAGE plpgsql AS $palgres$
93
+ END $palgres$`,`CREATE TRIGGER ${g} BEFORE INSERT OR UPDATE ON ${n} FOR EACH ROW EXECUTE FUNCTION ${u}()`),o.complete.unshift(`DROP TRIGGER IF EXISTS ${g} ON ${n}`,`DROP FUNCTION IF EXISTS ${u}()`);return}if(c.stored!==l.stored){this.refuse(`column ${a}.${r.name} changes between stored and virtual`,"Postgres would compute every row again: add a new generated column and ignore the old one");return}if(l.stored&&this.input.hasRows(D(this.matches.get(a).from))){this.refuse(`column ${a}.${r.name} changes its stored expression on a table with rows`,"Postgres would compute every row again under a lock: add a new generated column and ignore the old one");return}this.changes.push(`column ${a}.${r.name} is generated as ${l.expr}`),o.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${i} SET EXPRESSION AS (${l.expr})`),o.undo.push(`ALTER TABLE ${n} ALTER COLUMN ${i} SET EXPRESSION AS (${c.expr})`)}}retype(e,n,s,r,o){let a=e.to,i=D(a),c=[...a.checks.filter(m=>Oe(m.expr,s)).map(m=>`check ${m.name}`),...a.policies.filter(m=>Oe(`${m.using??""} ${m.withCheck??""}`,s)).map(m=>`policy ${m.name}`),...a.indexes.filter(m=>m.keys.some(y=>"expr"in y&&Oe(y.expr,s))||m.where!==void 0&&Oe(m.where,s)).map(m=>`index ${m.name}`),...a.primaryKey.includes(s)?["the primary key"]:[],...this.to.tables.flatMap(m=>m.foreignKeys.filter(y=>y.refSchema===a.schema&&y.refTable===a.name&&y.refColumns.includes(s)).map(y=>`foreign key ${D(m)}.${y.name}`))];if(c.length>0){this.refuse(`column ${i}.${s} changes type from ${r.from.type} to ${r.to.type}, and ${c.join(", ")} use it`,`add a column of the new type with .fillWith("${s}::${r.to.type}"), move them to it, and ignore the old one`);return}let l=this.conversion(r.from.type,r.to.type);if("refused"in l){this.refuse(`column ${i}.${s} changes type from ${r.from.type} to ${r.to.type}: ${l.refused}`,"add a column of the new type with .fillWith(\u2026) saying the value, move to it, and ignore the old one");return}if(r.from.nullable&&!r.to.nullable){this.refuse(`column ${i}.${s} changes type and becomes NOT NULL in one deploy`,"change its type in this deploy and make it NOT NULL (with .fillWith) in the next");return}this.changes.push(`column ${i}.${s} changes type from ${r.from.type} to ${r.to.type}`);let u=b(r.from.name),g=b(r.shadow),p=P(a.schema,ie(this.n,"sync",bt(a,s))),h=b(ie(this.n,"sync",s));o.tx.push(`ALTER TABLE ${n} ADD COLUMN ${g} ${this.typeSql(r.to.type)}`,`CREATE OR REPLACE FUNCTION ${p}() RETURNS trigger LANGUAGE plpgsql AS $palgres$
94
94
  DECLARE
95
- next boolean := position(${F(Te(this.n,""))} in replace(current_setting('search_path'), '"', '')) = 1;
95
+ next boolean := position(${M(Te(this.n,""))} in replace(current_setting('search_path'), '"', '')) = 1;
96
96
  BEGIN
97
97
  -- The writer's own column, taken across \u2014 on an update only when it
98
98
  -- changed: a value the other version wrote is not cast back over (a cast
99
99
  -- may lose: a shorter varchar, an integer).
100
100
  IF TG_OP = 'INSERT' THEN
101
101
  IF next THEN
102
- NEW.${d} := ${i.back(`NEW.${u}`)};
102
+ NEW.${u} := ${l.back(`NEW.${g}`)};
103
103
  ELSE
104
- NEW.${u} := ${i.on(`NEW.${d}`)};
104
+ NEW.${g} := ${l.on(`NEW.${u}`)};
105
105
  END IF;
106
106
  ELSIF next THEN
107
- IF NEW.${u} IS DISTINCT FROM OLD.${u} THEN
108
- NEW.${d} := ${i.back(`NEW.${u}`)};
107
+ IF NEW.${g} IS DISTINCT FROM OLD.${g} THEN
108
+ NEW.${u} := ${l.back(`NEW.${g}`)};
109
109
  END IF;
110
- ELSIF NEW.${d} IS DISTINCT FROM OLD.${d} OR (NEW.${u} IS NULL AND NEW.${d} IS NOT NULL) THEN
110
+ ELSIF NEW.${u} IS DISTINCT FROM OLD.${u} OR (NEW.${g} IS NULL AND NEW.${u} IS NOT NULL) THEN
111
111
  -- the old version's write, or the backfill reaching a row
112
- NEW.${u} := ${i.on(`NEW.${d}`)};
112
+ NEW.${g} := ${l.on(`NEW.${u}`)};
113
113
  END IF;
114
114
  RETURN NEW;
115
- END $palgres$`,`CREATE TRIGGER ${g} BEFORE INSERT OR UPDATE ON ${n} FOR EACH ROW EXECUTE FUNCTION ${f}()`),r.to.default!==void 0&&o.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${u} SET DEFAULT ${r.to.default}`),!r.from.nullable&&r.to.nullable&&this.relaxNotNull(l,n,r.from.name,o),o.undo.push(`DROP TRIGGER IF EXISTS ${g} ON ${n}`,`DROP FUNCTION IF EXISTS ${f}()`),o.undoColumns.push(`ALTER TABLE ${n} DROP COLUMN IF EXISTS ${u}`),this.backfills.push({kind:"backfill",table:n,key:this.keyOf(a),set:`${d} = ${d}`,where:`${u} IS NULL AND ${d} IS NOT NULL`,why:`${l}.${s} in its new type`}),r.to.nullable||this.notNull(a,n,r.shadow,o),o.complete.push(`DROP TRIGGER IF EXISTS ${g} ON ${n}`,`DROP FUNCTION IF EXISTS ${f}()`,`ALTER TABLE ${n} DROP COLUMN ${d}`,`ALTER TABLE ${n} RENAME COLUMN ${u} TO ${N(s)}`),o.names=!0,o.dropsColumns=!0}checkedDomain(e){if(this.input.checkedDomain(this.typeSql(e))===!0)return!0;let n=Re(e).replace(/^public\./,""),s=o=>o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),r=new RegExp(String.raw`\bCREATE\s+DOMAIN\s+(?:"?public"?\.)?"?${s(n.replace(/^"|"$/g,""))}"?\s[\s\S]*\b(?:CHECK|NOT\s+NULL)\b`,"i");return this.to.raw.some(o=>r.test(o.sql))}conversion(e,n){let s=g=>/(\[\])*$/.exec(g)[0],r=g=>g.slice(0,g.length-s(g).length);if(s(e)!==s(n))return{refused:"one is an array and the other is not, or of another depth"};let o=s(e),a=g=>{let p=Re(r(g));return[...this.to.enums,...this.from.enums].find(y=>tn(y.schema,y.name)===p||y.schema==="public"&&tn(y.schema,y.name)===`public.${p}`)},l=g=>/^(text|citext|public\.citext|character varying(\(\d+\))?|character(\(\d+\))?)$/.test(Re(r(g))),c=g=>p=>`${p}::${this.typeSql(g)}`,i=a(e),d=a(n);if(i&&d){let g=(h,m)=>h.filter(T=>!m.includes(T)),p=g(i.values,d.values),y=g(d.values,i.values);if(p.length>0||y.length>0)return{refused:`the enums' labels differ (${[...p.map(h=>`-${h}`),...y.map(h=>`+${h}`)].join(", ")}), and a label one version writes the other's column cannot hold`};let S=h=>m=>`${m}::text${o}::${this.typeSql(h)}`;return{on:S(n),back:S(e)}}if(i||d)return l(i?n:e)?{on:c(n),back:c(e)}:{refused:`Postgres has no cast between an enum and ${i?n:e}`};let u=this.input.castable(this.typeSql(e),this.typeSql(n)),f=this.input.castable(this.typeSql(n),this.typeSql(e));return u===!1||f===!1?{refused:`Postgres has no cast from ${u===!1?e:n} to ${u===!1?n:e}`}:{on:c(n),back:c(e)}}constraints(e,n,s){let{to:r}=e,o=this.inNewNames(e.from),a=e.from,l=D(r),c=E=>E.some(w=>e.retyped.has(w)),i=E=>E.some(w=>e.retyped.has(w)),d=(E,w)=>c(w)?ie(this.n,"c",E):E,u=(E,w,A)=>{E!==w&&s.complete.push(A?`ALTER TABLE ${n} RENAME CONSTRAINT ${N(E)} TO ${N(w)}`:`ALTER INDEX ${P(r.schema,E)} RENAME TO ${N(w)}`)},f=(E,w,A,k)=>{let R=[],b=[];for(let O of w){let L=E.find($=>$.name===O.name);L?(!k(L,O)||c(A(O)))&&(i(A(L))||b.push(L),R.push(O)):R.push(O)}for(let O of E)!w.some(L=>L.name===O.name)&&!i(A(O))&&b.push(O);return{added:R,gone:b}},g=(E,w)=>E.find(A=>A.name===w),p=E=>a.uniques.some(w=>w.name===E)||a.indexes.some(w=>w.name===E),y=f(o.uniques,r.uniques,E=>E.columns,(E,w)=>E.columns.join()===w.columns.join());this.renamedTwins(y,(E,w)=>E.columns.join()===w.columns.join()&&!c(w.columns),(E,w)=>{this.changes.push(`unique ${l}.${E.name} is renamed ${w.name} (at complete)`),s.complete.push(`ALTER TABLE ${n} RENAME CONSTRAINT ${N(E.name)} TO ${N(w.name)}`)});for(let E of y.gone)this.changes.push(`unique ${l}.${E.name} goes (at complete)`),s.complete.push(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${N(E.name)}`);for(let E of y.added){let w=p(E.name)?ie(this.n,"c",E.name):d(E.name,E.columns),A=P(r.schema,w);this.changes.push(`unique ${l}.${E.name}`),this.online.push({kind:"statement",sql:`CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS ${N(w)} ON ${n} (${ye(E.columns.map(k=>e.cols.get(k)))})`,why:`unique ${E.name}`}),this.settle.push({kind:"tx",sql:[`DO $palgres$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = ${F(w)} AND conrelid = ${F(n)}::regclass) THEN ALTER TABLE ${n} ADD CONSTRAINT ${N(w)} UNIQUE USING INDEX ${N(w)}; END IF; END $palgres$`],why:`unique ${E.name}`}),s.undo.unshift(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${N(w)}`,`DROP INDEX IF EXISTS ${A}`),u(w,E.name,!0)}let S=f(o.checks,r.checks,E=>r.columns.filter(w=>Ae(E.expr,w.name)).map(w=>w.name),(E,w)=>E.expr.trim()===w.expr.trim());for(let E of S.gone)this.changes.push(`check ${l}.${E.name} goes`),s.tx.push(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${N(E.name)}`),s.undo.push(`ALTER TABLE ${n} ADD CONSTRAINT ${N(E.name)} CHECK (${g(a.checks,E.name).expr}) NOT VALID`);for(let E of S.added)this.changes.push(`check ${l}.${E.name}`),s.tx.push(`ALTER TABLE ${n} ADD CONSTRAINT ${N(E.name)} CHECK (${Oe(E.expr,ln(e.cols))}) NOT VALID`),this.online.push({kind:"statement",sql:`ALTER TABLE ${n} VALIDATE CONSTRAINT ${N(E.name)}`,why:`check ${E.name}`}),s.undo.unshift(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${N(E.name)}`);let h=f(o.foreignKeys,r.foreignKeys,E=>E.columns,(E,w)=>Cs(E)===Cs(w));for(let E of h.gone)this.changes.push(`foreign key ${l}.${E.name} goes`),this.fkDrops.push({kind:"tx",sql:[`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${N(E.name)}`],why:`foreign key ${E.name} goes`}),this.undoFksBack.push({kind:"tx",sql:[`ALTER TABLE ${n} ADD ${this.fkSqlOld(g(a.foreignKeys,E.name))} NOT VALID`],why:`foreign key ${E.name} again`});for(let E of h.added){let w=d(E.name,E.columns);this.changes.push(`foreign key ${l}.${E.name}`),this.lateFks.push({kind:"tx",sql:[`ALTER TABLE ${n} ADD ${this.fkSql({...E,name:w},e.cols)} NOT VALID`],why:`foreign key ${E.name}`}),this.lateFks.push({kind:"statement",sql:`ALTER TABLE ${n} VALIDATE CONSTRAINT ${N(w)}`,why:`foreign key ${E.name}`}),this.undoFksGone.push({kind:"tx",sql:[`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${N(w)}`],why:`foreign key ${E.name}`}),u(w,E.name,!0)}let m=E=>[...E.keys.flatMap(w=>"column"in w?[w.column]:[]),...E.include??[]],T=f(o.indexes,r.indexes,m,(E,w)=>Wn(E)===Wn(w));this.renamedTwins(T,(E,w)=>Wn(E)===Wn(w)&&!c(m(w)),(E,w)=>{this.changes.push(`index ${l}.${E.name} is renamed ${w.name} (at complete)`),s.complete.push(`ALTER INDEX ${P(r.schema,E.name)} RENAME TO ${N(w.name)}`)});for(let E of T.added){if(!E.unique)continue;let w=m(E).slice(0,E.keys.length),A=T.gone.find(R=>R.unique&&m(R).slice(0,R.keys.length).join()===w.join());if(!A)continue;let k=this.to.tables.flatMap(R=>R.foreignKeys.filter(b=>b.refSchema===r.schema&&b.refTable===r.name&&b.refColumns.join()===w.join()).map(b=>`${D(R)}.${b.name}`));k.length>0&&this.refuse(`unique index ${l}.${A.name} is built again as ${E.name}, and foreign key ${k.join(", ")} reference its columns`,"Postgres binds a foreign key to the first unique index over its columns \u2014 the old one, which then cannot go: change the index in one deploy and the foreign keys in another, or give the new index other key columns")}for(let E of T.gone)T.added.some(w=>w.name===E.name)?(this.changes.push(`index ${l}.${E.name} is built again (the old one goes at complete)`),s.complete.push(`DROP INDEX IF EXISTS ${P(r.schema,E.name)}`)):(this.changes.push(`index ${l}.${E.name} goes (at complete)`),this.completeIndexes.push({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(r.schema,E.name)}`,why:`index ${E.name}`}));for(let E of T.added){let w=p(E.name)?ie(this.n,"i",E.name):d(E.name,m(E));this.changes.push(`index ${l}.${E.name}`),this.online.push({kind:"statement",sql:this.indexSql(r.schema,e.physical,{...E,name:w},e.cols,!0),why:`index ${E.name}`}),this.undoIndexesGone.unshift({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(r.schema,w)}`,why:`index ${E.name}`}),u(w,E.name,!1)}}renamedTwins(e,n,s){for(let r of[...e.gone]){let o=e.added.find(a=>a.name!==r.name&&n(r,a)&&!e.gone.some(l=>l.name===a.name));o&&(e.gone.splice(e.gone.indexOf(r),1),e.added.splice(e.added.indexOf(o),1),s(r,o))}}indexSql(e,n,s,r,o){let a=ln(r),l=s.keys.map(i=>{let d="column"in i?N(r.get(i.column)??i.column):`(${Oe(i.expr,a)})`;return i.desc&&(d+=" DESC"),i.nulls&&(d+=` NULLS ${i.nulls.toUpperCase()}`),d}),c=`CREATE ${s.unique?"UNIQUE ":""}INDEX ${o?"CONCURRENTLY ":""}IF NOT EXISTS ${N(s.name)} ON ${P(e,n)}`;return s.method&&s.method!=="btree"&&(c+=` USING ${s.method}`),c+=` (${l.join(", ")})`,s.include?.length&&(c+=` INCLUDE (${ye(s.include.map(i=>r.get(i)??i))})`),s.where!==void 0&&(c+=` WHERE ${Oe(s.where,a)}`),c}fkSql(e,n){let s=this.to.tables.find(o=>o.schema===e.refSchema&&o.name===e.refTable),r=s?this.where(s):{table:e.refTable,cols:new Map};return`CONSTRAINT ${N(e.name)} FOREIGN KEY (${ye(e.columns.map(o=>n.get(o)??o))}) REFERENCES ${P(e.refSchema,r.table)} (${ye(e.refColumns.map(o=>r.cols.get(o)??o))})${e.match==="full"?" MATCH FULL":""} ON DELETE ${e.onDelete.toUpperCase()} ON UPDATE ${e.onUpdate.toUpperCase()}`}fkSqlOld(e){return`CONSTRAINT ${N(e.name)} FOREIGN KEY (${ye(e.columns)}) REFERENCES ${P(e.refSchema,e.refTable)} (${ye(e.refColumns)})${e.match==="full"?" MATCH FULL":""} ON DELETE ${e.onDelete.toUpperCase()} ON UPDATE ${e.onUpdate.toUpperCase()}`}generations(e){return[...this.input.generations?.(e)??[]].sort((n,s)=>n.gen-s.gen)}search(e,n){let s=D(e),r=e.search,o=n?.from.search;if(r===void 0){n&&this.searchGone(n.from,n.physical);return}let a=as(`table ${s}`,e,r);if(a.length>0){this.refusals.push(...a);return}let l=e.columns.find(T=>T.name===e.primaryKey[0]),c=this.generations(n?D(n.from):s),i=c.filter(T=>T.status!=="retired"),d=i.find(T=>T.status==="active"),u=i.at(-1),f=u===void 0?[]:_n(u,r)?i:d?[d]:[],g=u===void 0?"active":_n(u,r)||d!==void 0&&_n(d,r)?void 0:"filling";if(n){let T="take search out of the table and deploy, make the change in the next deploy, and declare search again in the one after \u2014 every row is embedded again then";e.name!==n.from.name&&this.refuse(`table ${e.schema}.${n.from.name} is renamed ${e.name} while it declares search`,T);let E=new Set;for(let w of new Set([l.name,...r.from])){let A=w===l.name?"search keeps each row's vector by it":"search reads it";if(n.retyped.has(w))this.refuse(`column ${s}.${w} changes type, and ${A}`,T);else if((n.cols.get(w)??w)!==w)this.refuse(`column ${s}.${w} ${n.parked.has(w)?"takes a name another column leaves in this deploy":`is renamed from ${n.cols.get(w)}`}, and ${A}`,T);else continue;E.add(n.retyped.get(w)?.from.name??n.cols.get(w)??w)}for(let w of f)for(let A of w.from){if(E.has(A))continue;e.columns.some(R=>R.name===A)&&!n.retyped.has(A)&&(n.cols.get(A)??A)===A||this.refuse(`column ${s}.${A} goes or changes, and the vectors that serve search of ${s} now (generation ${w.gen}) are made from it`,"they serve until the new ones are complete: leave the column as it is in this deploy, and change it in a deploy after that")}}let p={schema:e.schema,name:n?n.physical:e.name},y=P(p.schema,p.name);if(g!==void 0){let T=(c.at(-1)?.gen??0)+1;this.changes.push(g==="active"?`search of ${s}: its rows are embedded with ${r.model} (${r.dimensions} dimensions) in the background; found by their words until then`:`search of ${s}: every row is embedded again with ${r.model} (${r.dimensions} dimensions) in the background; the vectors there serve until the new ones are complete`),this.searchStart.push({kind:"tx",sql:Os({table:p,gen:T,search:r,status:g,pk:{name:l.name,type:this.typeSql(l.type)},roles:this.input.roles,types:this.input.vectorSchema??"public"}),why:`search of ${s}: generation ${T}`}),n&&this.searchStart.push({kind:"statement",sql:ks(p,T,l.name),why:`search of ${s}: its rows queued`}),this.undoSearch.push({kind:"tx",sql:Fn(p,T),why:`search of ${s}: generation ${T}`})}if(o!==void 0&&o.staleness!==r.staleness&&f.length>0){this.changes.push(`search of ${s}: a changed row's old vector ${r.staleness==="null"?"goes":"stays"} until the row is embedded again`);for(let T of f)this.searchStart.push({kind:"tx",sql:qn({table:p,gen:T.gen,from:T.from,staleness:r.staleness},!0),why:`search of ${s}: generation ${T.gen}'s triggers`}),this.undoSearch.push({kind:"tx",sql:qn({table:p,gen:T.gen,from:T.from,staleness:o.staleness},!0),why:`search of ${s}: generation ${T.gen}'s triggers`})}for(let T of c)f.includes(T)||(this.changes.push(`search of ${s}: generation ${T.gen}'s vectors, ${T.status==="retired"?"left behind":"never full"}, go (at complete)`),this.searchComplete.push({kind:"tx",sql:Fn(p,T.gen),why:`search of ${s}: generation ${T.gen}`}));let S=Dn(p,r.from),h=o===void 0?void 0:Dn(p,o.from);if(S===h)return;let m=T=>`CREATE INDEX ${T?"CONCURRENTLY ":""}IF NOT EXISTS ${N(S)} ON ${y} USING gin (${Mn(r.from)})`;o!==void 0&&this.changes.push(`search of ${s} reads ${r.from.join(", ")}`),n?(this.online.push({kind:"statement",sql:m(!0),why:`search of ${s}: its words`}),this.undoIndexesGone.unshift({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(p.schema,S)}`,why:`search of ${s}: its words`})):this.creates.push(m(!1)),h!==void 0&&this.completeIndexes.push({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(p.schema,h)}`,why:`search of ${s}: the words it read`})}searchGone(e,n){let s=this.generations(D(e));if(e.search===void 0&&s.length===0)return;let r={schema:e.schema,name:n},o=D(e);this.changes.push(`search of ${o} goes (at complete)`);for(let a of s)this.searchComplete.push({kind:"tx",sql:Fn(r,a.gen),why:`search of ${o}: generation ${a.gen}`});e.search!==void 0&&this.searchComplete.push({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(r.schema,Dn(r,e.search.from))}`,why:`search of ${o}: the words it read`})}policyRoles(e,n){let s=new Set;for(let r of e.to){if(r.startsWith("role:")&&r.length>5){s.add(N(r.slice(5)));continue}let o=Tt[r];if(!o){this.refuse(`policy ${n}.${e.name} is for role ${r}`,`a policy is for ${Object.keys(Tt).join(", ")}`);continue}s.add(o==="public"?"PUBLIC":N(this.input.roles[o]))}return[...s].join(", ")}policySql(e,n,s){let r=ln(s),o=`CREATE POLICY ${N(n.name)} ON ${e} AS ${n.restrictive?"RESTRICTIVE":"PERMISSIVE"} FOR ${n.for.toUpperCase()} TO ${this.policyRoles(n,e)}`;return n.using!==void 0&&(o+=` USING (${Oe(n.using,r)})`),n.withCheck!==void 0&&(o+=` WITH CHECK (${Oe(n.withCheck,r)})`),o}policies(e,n,s){let{to:r}=e,o=this.inNewNames(e.from),a=D(r),l=i=>JSON.stringify([i.for,[...new Set(i.to.map(d=>Tt[d]??d))].sort(),i.using??null,i.withCheck??null,i.restrictive??!1]),c=i=>e.from.policies.find(d=>d.name===i);for(let i of r.policies){let d=o.policies.find(u=>u.name===i.name);d&&l(d)===l(i)||this.readsOthersRenamed(r,i,a)||(this.changes.push(`policy ${a}.${i.name}${d?" changes":""}`),s.undo.push(`DROP POLICY IF EXISTS ${N(i.name)} ON ${n}`),d&&(s.tx.push(`DROP POLICY IF EXISTS ${N(i.name)} ON ${n}`),s.undo.push(this.policySql(n,c(i.name),vs(e.from)))),s.tx.push(this.policySql(n,i,e.cols)))}for(let i of o.policies)r.policies.some(d=>d.name===i.name)||(this.changes.push(`policy ${a}.${i.name} goes`),s.tx.push(`DROP POLICY IF EXISTS ${N(i.name)} ON ${n}`),s.undo.push(this.policySql(n,c(i.name),vs(e.from))))}readsOthersRenamed(e,n,s){let r=`${n.using??""} ${n.withCheck??""}`;for(let o of this.to.tables){if(o===e)continue;let a=this.from.tables.find(c=>c.schema===o.schema&&c.name===(o.renamedFrom??o.name));if(!a)continue;let l=[...o.name!==a.name&&Ae(r,o.name)?[`${o.schema}.${o.name}`]:[],...Ae(r,o.name)?o.columns.filter(c=>c.renamedFrom!==void 0&&c.renamedFrom!==c.name&&a.columns.some(i=>i.name===c.renamedFrom)&&Ae(r,c.name)).map(c=>`${o.schema}.${o.name}.${c.name}`):[]];if(l.length>0)return this.refuse(`policy ${s}.${n.name} reads ${l.join(", ")}, renamed in this deploy`,"Postgres has the new name only from complete on: rename in this deploy, change the policy in the next"),!0}return!1}rawUnitsBefore(){let e=!0;for(let n of this.to.raw){if(!n.before)continue;let s=this.from.raw.find(r=>r.name===n.name);if(!(s&&s.sql.trim()===n.sql.trim())){if(s&&/^\s*CREATE\s+(?:DOMAIN|TYPE|SEQUENCE)\b/i.test(s.sql)){this.refuse(`raw ${n.name} changes`,"Postgres cannot drop a domain or a type a column uses, and a sequence made again starts over: say the change as an ALTER (ALTER DOMAIN \u2026 ADD CONSTRAINT \u2026 NOT VALID, ALTER TYPE, ALTER SEQUENCE) in a unit of its own, or make a new one and move the columns to it");continue}this.changes.push(`raw ${n.name}${s?" changes":""}`),e&&this.creates.push("SET LOCAL check_function_bodies = off"),e=!1,this.creates.push(...this.rawApply(n,s)),!s&&n.drop!==void 0&&this.undoCreates.push(n.drop),s&&this.undoRawsBack.push(...this.rawApply(s,n))}}}rawApply(e,n){return!n||/^\s*CREATE\s+OR\s+REPLACE\b/i.test(e.sql)||n.drop===void 0?[e.sql]:[n.drop,e.sql]}rawUnits(){let e=this.to.raw.filter(i=>!i.before),n=i=>/^\s*CREATE\s+(?:OR\s+REPLACE\s+)?(?:MATERIALIZED\s+)?VIEW\b/i.test(i),s=this.namesAtComplete,r=this.dropsColumnsAtComplete,o=e.findIndex(i=>{let d=this.from.raw.find(u=>u.name===i.name);return n(i.sql)?r&&d!==void 0?!0:d===void 0?s:d.sql.trim()!==i.sql.trim():!1}),a=new Set,l=new Set,c=null;if(o>=0){let i=e.slice(o),d=f=>n(f)||/^\s*CREATE\s+(?:OR\s+REPLACE\s+)?(?:FUNCTION|PROCEDURE)\b/i.test(f)||St.test(f),u=[];for(let f of[...this.from.raw].reverse()){if(f.before||f.drop===void 0)continue;let g=i.some(y=>y.name===f.name),p=!this.to.raw.some(y=>y.name===f.name);(g&&d(f.sql)||p&&n(f.sql))&&u.push(f.drop),p&&n(f.sql)&&l.add(f.name)}for(let f of i){let g=this.from.raw.find(p=>p.name===f.name);(!g||g.sql.trim()!==f.sql.trim())&&this.changes.push(`raw ${f.name}${g?" changes":""}`),a.add(f.name)}if(s){this.viewsAtComplete={drops:u,creates:i.map(f=>f.sql)};for(let f of i){let g=this.changes.findIndex(p=>p===`raw ${f.name}`||p===`raw ${f.name} changes`);g>=0&&(this.changes[g]+=" (at complete)")}}else{c={kind:"tx",sql:["SET LOCAL check_function_bodies = off",...u,...i.map(f=>f.sql)],why:`raw ${i[0].name} and what follows it, made again`};for(let f of[...i].reverse())f.drop!==void 0&&this.undoRawsGone.push(f.drop);for(let f of this.from.raw){if(f.before||f.drop===void 0)continue;let g=i.some(y=>y.name===f.name),p=!this.to.raw.some(y=>y.name===f.name);(g&&d(f.sql)||p&&n(f.sql))&&this.undoRawsBack.push(f.sql)}}}for(let i of this.to.raw){if(i.before||(c!==null&&i.name===e[o].name&&this.raws.push(c),a.has(i.name)))continue;let d=this.from.raw.find(f=>f.name===i.name),u=_s(i.sql);if(!(d&&d.sql.trim()===i.sql.trim())){if(d&&u){this.refuse(`raw index ${i.name} changes under its name`,"an index is built CONCURRENTLY next to the one serving, so under a name of its own: give the new one its own name (the old one goes at complete)");continue}if(this.changes.push(`raw ${i.name}${d?" changes":""}`),u)this.raws.push({kind:"statement",sql:u.create,why:`raw ${i.name}`}),this.undoIndexesGone.unshift({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${u.qualified(i,this.to)}`,why:`raw ${i.name}`});else{if(i.drop===void 0&&(St.test(i.sql)||n(i.sql))){this.refuse(`raw ${i.name} has no drop`,"a rollback could not take it back, and the old version would write and read past it: give it its drop (raw(name, up, { drop }))");continue}this.raws.push({kind:"tx",sql:["SET LOCAL check_function_bodies = off",...this.rawApply(i,d)],why:`raw ${i.name}`}),i.drop!==void 0&&this.undoRawsGone.unshift(i.drop),d&&this.undoRawsBack.push(...i.drop!==void 0?[d.sql]:this.rawApply(d,i))}}}for(let i of[...this.from.raw].reverse()){if(this.to.raw.some(u=>u.name===i.name))continue;if(l.has(i.name)){this.changes.push(`raw ${i.name} goes (with the views made again)`);continue}let d=_s(i.sql);if(!d){if(i.drop===void 0){this.refuse(`raw ${i.name} is gone from the schema`,"Palgres does not know how to take back what its SQL made: keep it, or give the unit its drop (raw(name, up, { drop })) and remove it in the deploy after that");continue}this.changes.push(`raw ${i.name} goes (at complete)`),St.test(i.sql)?this.completeUnitDrops.push(i.drop):this.completeIndexes.push({kind:"tx",sql:[i.drop],why:`raw ${i.name}`});continue}this.changes.push(`raw index ${i.name} goes (at complete)`),this.completeIndexes.push({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${d.qualified(i,this.from)}`,why:`raw ${i.name}`})}}views(){let{anon:e,authenticated:n,service:s}=this.input.roles,r=[e,n,s].map(N).join(", "),o=[];for(let a of[...new Set(this.layout.tables.map(l=>l.schema))]){let l=N(Te(this.n,a));o.push(`DROP SCHEMA IF EXISTS ${l} CASCADE`,`CREATE SCHEMA ${l}`,`GRANT USAGE ON SCHEMA ${l} TO ${r}`)}for(let a of this.layout.tables){let l=a.columns.map(i=>i.physical===i.name?N(i.name):`${N(i.physical)} AS ${N(i.name)}`).join(", "),c=P(Te(this.n,a.schema),a.name);o.push(`CREATE VIEW ${c} WITH (security_invoker = true) AS SELECT ${l} FROM ${P(a.schema,a.physical)}`),o.push(`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ${c} TO ${r}`)}return o}dropViews(e,n){let s=[...new Set(n.tables.map(r=>r.schema))];return s.length>0?s.map(r=>`DROP SCHEMA IF EXISTS ${N(Te(e,r))} CASCADE`):["SELECT 1"]}keyOf(e){let n=this.where(e);return e.primaryKey.map(s=>{let r=this.matches.get(D(e))?.retyped.get(s);return r?r.from.name:n.cols.get(s)??s})}};function vs(t){return new Map(t.columns.map(e=>[e.name,e.name]))}function ln(t){return new Map([...t].filter(([e,n])=>e!==n))}function $t(t,e,n){return e?[`ALTER TABLE ${t} ENABLE ROW LEVEL SECURITY`]:n?[`ALTER TABLE ${t} DISABLE ROW LEVEL SECURITY`]:[]}function Cs(t){return JSON.stringify([t.columns,t.refSchema,t.refTable,t.refColumns,t.onDelete,t.onUpdate,t.match??"simple"])}function Wn(t){return JSON.stringify([t.keys,t.unique,t.where??null,t.method??"btree",t.include??[]])}var St=/^\s*(?:CREATE\s+(?:OR\s+REPLACE\s+)?(?:TRIGGER|CONSTRAINT\s+TRIGGER|RULE)\b|DO\s+\$\w*\$\s+BEGIN\s+IF\s+NOT\s+\(EXISTS\s+\(SELECT\s+1\s+FROM\s+pg_catalog\.pg_trigger)/i,Ps=/^\s*CREATE\s+(UNIQUE\s+)?INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+NOT\s+EXISTS\s+)?("[^"]+"|[A-Za-z_][A-Za-z0-9_$]*)\s+ON\s+/i;function Oo(t){return jn(t).map(e=>e.schema===void 0?N(e.name):P(e.schema,e.name))}var Ms=/\bnextval\('((?:[^']|'')+)'(?:::regclass)?\)/gi;function jn(t){if(t===void 0)return[];let e=[];for(let n of t.matchAll(Ms)){let s=n[1].replace(/''/g,"'"),r='("(?:[^"]|"")+"|[A-Za-z_][A-Za-z0-9_$]*)',o=new RegExp(`^(?:${r}\\.)?${r}$`).exec(s);if(!o)continue;let a=l=>l.startsWith('"')?l.slice(1,-1).replace(/""/g,'"'):l.toLowerCase();e.push(o[1]===void 0?{name:a(o[2])}:{schema:a(o[1]),name:a(o[2])})}return e}function Nt(t){let e=t.sql.trim().replace(/;\s*$/,""),n=Ps.exec(e);return!n||e.includes(";")?void 0:n[2].replace(/^"|"$/g,"")}function _s(t){let e=t.trim().replace(/;\s*$/,""),n=Ps.exec(e);if(!n||e.includes(";"))return;let s=n[2].replace(/^"|"$/g,""),r=e.slice(n[0].length);return{unique:!!n[1],create:`CREATE ${n[1]?"UNIQUE ":""}INDEX CONCURRENTLY IF NOT EXISTS ${N(s)} ON ${r}`,qualified:(o,a)=>{let l=/^\s*(?:ONLY\s+)?("[^"]+"|[A-Za-z_][A-Za-z0-9_$]*)(?:\.("[^"]+"|[A-Za-z_][A-Za-z0-9_$]*))?/i.exec(r),c=d=>d.replace(/^"|"$/g,""),i=l?.[2]?c(l[1]):o.table?.split(".")[0]??a.tables.find(d=>d.name===c(l?.[1]??""))?.schema??"public";return P(i,s)}}}import{randomBytes as Mo}from"node:crypto";var Me=(t,e)=>JSON.stringify(t)===JSON.stringify(e),cn=t=>new Map(t.map(e=>[e.name,e])),Ge=t=>({type:t.type,nullable:t.nullable,default:t.default??null,identity:t.identity??null,generated:t.generated??null});function ko(t,e){if(t.name===e)return t;let{renamedFrom:n,...s}=t;return{...s,name:e}}function At(t,e,n){let s=ge(n),r=[],o=[...t.unmanaged],a=new Map(t.schema.tables.map(h=>[D(h),h])),l=new Map(e.schema.tables.map(h=>[D(h),h])),c=new Set,i=[],d=new Set(s.raw.map(h=>Nt(h)).filter(h=>h!==void 0));for(let h of s.tables){let m=a.get(D(h))??(h.renamedFrom!==void 0?a.get(`${h.schema}.${h.renamedFrom}`):void 0);if(!m)continue;c.add(D(m));let T=l.get(D(h));if(!T)throw new Error(`palgres: the shadow database has no ${D(h)}: the schema did not make it`);let E=cn(T.columns),w=m.columns.map(k=>{let R=h.columns.find(O=>O.name===k.name)??h.columns.find(O=>O.renamedFrom===k.name&&!m.columns.some(L=>L.name===O.name)),b=R&&E.get(R.name);return R&&b&&Me(Ge(k),Ge(b))?ko(R,k.name):k}),A=(k,R,b)=>{let O=cn(R),L=cn(b);return k.map($=>{let _=O.get($.name),M=L.get($.name);return M&&_&&Me($,_)?M:$})};i.push({schema:m.schema,name:m.name,...h.ignored!==void 0?{ignored:h.ignored}:{},...h.renamedFrom!==void 0&&h.name===m.name?{renamedFrom:h.renamedFrom}:{},columns:w,primaryKey:m.primaryKey,uniques:A(m.uniques,T.uniques,h.uniques),indexes:A(m.indexes.filter(k=>!d.has(k.name)),T.indexes,h.indexes),checks:A(m.checks,T.checks,h.checks),foreignKeys:A(m.foreignKeys.filter(k=>a.has(`${k.refSchema}.${k.refTable}`)&&s.tables.some(R=>R.schema===k.refSchema&&R.name===k.refTable||R.schema===k.refSchema&&R.renamedFrom===k.refTable)?!0:(o.push(`foreign key ${D(m)}.${k.name} references ${k.refSchema}.${k.refTable}, outside the schema \u2014 kept as it is; drop it when its target goes`),!1)),T.foreignKeys,h.foreignKeys),rls:m.rls,policies:A(m.policies,T.policies,h.policies)})}for(let h of a.keys())c.has(h)||o.push(`table ${h}: not in the schema \u2014 kept as it is`);for(let h of t.forced)c.has(h)||o.push(`table ${h}: row-level security FORCEd on its owner \u2014 kept as it is`);let u=new Map(t.schema.tables.flatMap(h=>h.indexes.map(m=>[`${h.schema}.${m.name}`,{t:h,i:m}]))),f=new Map(e.schema.tables.flatMap(h=>h.indexes.map(m=>[`${h.schema}.${m.name}`,{t:h,i:m}]))),g=[],p=!0;for(let[h,m]of e.objects){let T=t.objects.get(h);T===void 0?p=!1:T!==m&&r.push(`${h} is not what the schema's raw SQL makes: drop it, and the migration makes it as the schema says`)}for(let h of s.raw){let m=Nt(h);if(m===void 0){p&&g.push(h);continue}let T=[...f.keys()].find(A=>A.endsWith(`.${m}`)),E=T&&f.get(T),w=T&&u.get(T);if(!E||!w){let A=[...e.objects.keys()].find(k=>k.startsWith("index ")&&k.endsWith(`.${m}`));A!==void 0&&t.objects.get(A)===e.objects.get(A)&&g.push(h);continue}Me(w.i,E.i)&&D(w.t)===D(E.t)?g.push(h):r.push(`index ${T} is not what the schema's raw unit ${h.name} makes: drop it, and the migration makes it as the schema says`)}let y=[];for(let h of s.enums){let m=t.schema.enums.find(T=>T.schema===h.schema&&T.name===h.name);m&&y.push(Me(m.values,h.values)?h:m)}let S=s.extensions.filter(h=>t.schema.extensions.includes(h));return{schema:ge({tables:i,enums:y,extensions:S,raw:g}),unforce:t.forced.filter(h=>c.has(h)),refusals:r,unmanaged:o}}function Lo(t,e){let n=[],s=new Map(t.schema.tables.map(r=>[D(r),r]));for(let r of e.schema.tables){let o=D(r),a=s.get(o);if(!a){n.push(`table ${o} is missing`);continue}for(let d of r.columns)a.columns.some(u=>u.name===d.name)||n.push(`column ${o}.${d.name} is missing`);for(let d of a.columns)r.columns.some(u=>u.name===d.name)||n.push(`column ${o}.${d.name}: not declared`);for(let d of r.columns){let u=a.columns.find(f=>f.name===d.name);u&&!Me(Ge(u),Ge(d))&&n.push(`column ${o}.${d.name}: ${JSON.stringify(Ge(u))}, declared ${JSON.stringify(Ge(d))}`)}Me(a.primaryKey,r.primaryKey)||n.push(`table ${o}: primary key (${a.primaryKey.join(", ")}), declared (${r.primaryKey.join(", ")})`),a.rls!==r.rls&&n.push(`table ${o}: row-level security ${a.rls?"on":"off"}, declared ${r.rls?"on":"off"}`);let l={uniques:"unique",indexes:"index",checks:"check",foreignKeys:"foreign key",policies:"policy"},c=(d,u)=>e.schema.tables.some(f=>f.schema===d&&f.name===u),i={...a,foreignKeys:a.foreignKeys.filter(d=>c(d.refSchema,d.refTable))};for(let[d,u]of Object.entries(l)){let f=cn(i[d]),g=cn(r[d]);for(let[p,y]of g)Me(f.get(p),y)||n.push(`${u} ${o}.${p}: ${f.has(p)?`${JSON.stringify(f.get(p))}, declared ${JSON.stringify(y)}`:"missing"}`);for(let p of f.keys())g.has(p)||n.push(`${u} ${o}.${p}: not declared`)}}return n}function ve(t,e){let n=t.join("_").replace(/[^A-Za-z0-9_]/g,"_").replace(/^([^A-Za-z_])/,"_$1");if(Buffer.byteLength(n)>55){let r=0;for(let o of t.join("."))r=Math.imul(r,31)+o.charCodeAt(0)>>>0;n=`${n.slice(0,46)}_${r.toString(36)}`}let s=n;for(let r=2;e.has(s);r++)s=`${n}_${r}`;return e.add(s),s}function xo(t){let e="$palgres$";for(let n=1;t.includes(e);n++)e=`$palgres${n}$`;return e}var Ot=(t,e)=>{let n=xo(e+t);return`DO ${n} BEGIN IF NOT (${t}) THEN ${e}; END IF; END ${n}`};async function qs(t,e,n,s){let r=new Set,o=[],a=[],l=[],c=await t(`SELECT n.nspname AS schema, t.typname AS name, pg_catalog.format_type(t.typbasetype, t.typtypmod) AS base,
115
+ END $palgres$`,`CREATE TRIGGER ${h} BEFORE INSERT OR UPDATE ON ${n} FOR EACH ROW EXECUTE FUNCTION ${p}()`),r.to.default!==void 0&&o.tx.push(`ALTER TABLE ${n} ALTER COLUMN ${g} SET DEFAULT ${r.to.default}`),!r.from.nullable&&r.to.nullable&&this.relaxNotNull(i,n,r.from.name,o),o.undo.push(`DROP TRIGGER IF EXISTS ${h} ON ${n}`,`DROP FUNCTION IF EXISTS ${p}()`),o.undoColumns.push(`ALTER TABLE ${n} DROP COLUMN IF EXISTS ${g}`),this.backfills.push({kind:"backfill",table:n,key:this.keyOf(a),set:`${u} = ${u}`,where:`${g} IS NULL AND ${u} IS NOT NULL`,why:`${i}.${s} in its new type`}),r.to.nullable||this.notNull(a,n,r.shadow,o),o.complete.push(`DROP TRIGGER IF EXISTS ${h} ON ${n}`,`DROP FUNCTION IF EXISTS ${p}()`,`ALTER TABLE ${n} DROP COLUMN ${u}`,`ALTER TABLE ${n} RENAME COLUMN ${g} TO ${b(s)}`),o.names=!0,o.dropsColumns=!0}checkedDomain(e){if(this.input.checkedDomain(this.typeSql(e))===!0)return!0;let n=be(e).replace(/^public\./,""),s=o=>o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),r=new RegExp(String.raw`\bCREATE\s+DOMAIN\s+(?:"?public"?\.)?"?${s(n.replace(/^"|"$/g,""))}"?\s[\s\S]*\b(?:CHECK|NOT\s+NULL)\b`,"i");return this.to.raw.some(o=>r.test(o.sql))}conversion(e,n){let s=h=>/(\[\])*$/.exec(h)[0],r=h=>h.slice(0,h.length-s(h).length);if(s(e)!==s(n))return{refused:"one is an array and the other is not, or of another depth"};let o=s(e),a=h=>{let m=be(r(h));return[...this.to.enums,...this.from.enums].find(y=>rn(y.schema,y.name)===m||y.schema==="public"&&rn(y.schema,y.name)===`public.${m}`)},i=h=>/^(text|citext|public\.citext|character varying(\(\d+\))?|character(\(\d+\))?)$/.test(be(r(h))),c=h=>m=>`${m}::${this.typeSql(h)}`,l=a(e),u=a(n);if(l&&u){let h=(f,d)=>f.filter(T=>!d.includes(T)),m=h(l.values,u.values),y=h(u.values,l.values);if(m.length>0||y.length>0)return{refused:`the enums' labels differ (${[...m.map(f=>`-${f}`),...y.map(f=>`+${f}`)].join(", ")}), and a label one version writes the other's column cannot hold`};let S=f=>d=>`${d}::text${o}::${this.typeSql(f)}`;return{on:S(n),back:S(e)}}if(l||u)return i(l?n:e)?{on:c(n),back:c(e)}:{refused:`Postgres has no cast between an enum and ${l?n:e}`};let g=this.input.castable(this.typeSql(e),this.typeSql(n)),p=this.input.castable(this.typeSql(n),this.typeSql(e));return g===!1||p===!1?{refused:`Postgres has no cast from ${g===!1?e:n} to ${g===!1?n:e}`}:{on:c(n),back:c(e)}}constraints(e,n,s){let{to:r}=e,o=this.inNewNames(e.from),a=e.from,i=D(r),c=E=>E.some(w=>e.retyped.has(w)),l=E=>E.some(w=>e.retyped.has(w)),u=(E,w)=>c(w)?ie(this.n,"c",E):E,g=(E,w,O)=>{E!==w&&s.complete.push(O?`ALTER TABLE ${n} RENAME CONSTRAINT ${b(E)} TO ${b(w)}`:`ALTER INDEX ${P(r.schema,E)} RENAME TO ${b(w)}`)},p=(E,w,O,k)=>{let N=[],A=[];for(let R of w){let L=E.find($=>$.name===R.name);L?(!k(L,R)||c(O(R)))&&(l(O(L))||A.push(L),N.push(R)):N.push(R)}for(let R of E)!w.some(L=>L.name===R.name)&&!l(O(R))&&A.push(R);return{added:N,gone:A}},h=(E,w)=>E.find(O=>O.name===w),m=E=>a.uniques.some(w=>w.name===E)||a.indexes.some(w=>w.name===E),y=p(o.uniques,r.uniques,E=>E.columns,(E,w)=>E.columns.join()===w.columns.join());this.renamedTwins(y,(E,w)=>E.columns.join()===w.columns.join()&&!c(w.columns),(E,w)=>{this.changes.push(`unique ${i}.${E.name} is renamed ${w.name} (at complete)`),s.complete.push(`ALTER TABLE ${n} RENAME CONSTRAINT ${b(E.name)} TO ${b(w.name)}`)});for(let E of y.gone)this.changes.push(`unique ${i}.${E.name} goes (at complete)`),s.complete.push(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${b(E.name)}`);for(let E of y.added){let w=m(E.name)?ie(this.n,"c",E.name):u(E.name,E.columns),O=P(r.schema,w);this.changes.push(`unique ${i}.${E.name}`),this.online.push({kind:"statement",sql:`CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS ${b(w)} ON ${n} (${ye(E.columns.map(k=>e.cols.get(k)))})`,why:`unique ${E.name}`}),this.settle.push({kind:"tx",sql:[`DO $palgres$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = ${M(w)} AND conrelid = ${M(n)}::regclass) THEN ALTER TABLE ${n} ADD CONSTRAINT ${b(w)} UNIQUE USING INDEX ${b(w)}; END IF; END $palgres$`],why:`unique ${E.name}`}),s.undo.unshift(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${b(w)}`,`DROP INDEX IF EXISTS ${O}`),g(w,E.name,!0)}let S=p(o.checks,r.checks,E=>r.columns.filter(w=>Oe(E.expr,w.name)).map(w=>w.name),(E,w)=>E.expr.trim()===w.expr.trim());for(let E of S.gone)this.changes.push(`check ${i}.${E.name} goes`),s.tx.push(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${b(E.name)}`),s.undo.push(`ALTER TABLE ${n} ADD CONSTRAINT ${b(E.name)} CHECK (${h(a.checks,E.name).expr}) NOT VALID`);for(let E of S.added)this.changes.push(`check ${i}.${E.name}`),s.tx.push(`ALTER TABLE ${n} ADD CONSTRAINT ${b(E.name)} CHECK (${ke(E.expr,un(e.cols))}) NOT VALID`),this.online.push({kind:"statement",sql:`ALTER TABLE ${n} VALIDATE CONSTRAINT ${b(E.name)}`,why:`check ${E.name}`}),s.undo.unshift(`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${b(E.name)}`);let f=p(o.foreignKeys,r.foreignKeys,E=>E.columns,(E,w)=>Kn(E)===Kn(w));for(let E of f.gone)this.changes.push(`foreign key ${i}.${E.name} goes`),this.fkDrops.push({kind:"tx",sql:[`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${b(E.name)}`],why:`foreign key ${E.name} goes`}),this.undoFksBack.push({kind:"tx",sql:[`ALTER TABLE ${n} ADD ${this.fkSqlOld(h(a.foreignKeys,E.name))} NOT VALID`],why:`foreign key ${E.name} again`});for(let E of f.added){let w=u(E.name,E.columns);this.changes.push(`foreign key ${i}.${E.name}`),this.lateFks.push({kind:"tx",sql:[`ALTER TABLE ${n} ADD ${this.fkSql({...E,name:w},e.cols)} NOT VALID`],why:`foreign key ${E.name}`}),this.lateFks.push({kind:"statement",sql:`ALTER TABLE ${n} VALIDATE CONSTRAINT ${b(w)}`,why:`foreign key ${E.name}`}),this.undoFksGone.push({kind:"tx",sql:[`ALTER TABLE ${n} DROP CONSTRAINT IF EXISTS ${b(w)}`],why:`foreign key ${E.name}`}),g(w,E.name,!0)}for(let E of r.foreignKeys){let w=o.foreignKeys.find(O=>O.name===E.name);!w||Kn(w)!==Kn(E)||c(E.columns)||(w.deferrable??!1)===(E.deferrable??!1)||(this.changes.push(`foreign key ${i}.${E.name} is ${E.deferrable?"":"not "}deferrable (in the catalog, no row read again; ${i} locked a moment, the lock given way to while others hold it and tried again)`),s.tx.push(`ALTER TABLE ${n} ALTER CONSTRAINT ${b(E.name)} ${E.deferrable?"DEFERRABLE":"NOT DEFERRABLE"}`),s.undo.push(`ALTER TABLE ${n} ALTER CONSTRAINT ${b(E.name)} ${E.deferrable?"NOT DEFERRABLE":"DEFERRABLE"}`))}let d=E=>[...E.keys.flatMap(w=>"column"in w?[w.column]:[]),...E.include??[]],T=p(o.indexes,r.indexes,d,(E,w)=>Hn(E)===Hn(w));this.renamedTwins(T,(E,w)=>Hn(E)===Hn(w)&&!c(d(w)),(E,w)=>{this.changes.push(`index ${i}.${E.name} is renamed ${w.name} (at complete)`),s.complete.push(`ALTER INDEX ${P(r.schema,E.name)} RENAME TO ${b(w.name)}`)});for(let E of T.added){if(!E.unique)continue;let w=d(E).slice(0,E.keys.length),O=T.gone.find(N=>N.unique&&d(N).slice(0,N.keys.length).join()===w.join());if(!O)continue;let k=this.to.tables.flatMap(N=>N.foreignKeys.filter(A=>A.refSchema===r.schema&&A.refTable===r.name&&A.refColumns.join()===w.join()).map(A=>`${D(N)}.${A.name}`));k.length>0&&this.refuse(`unique index ${i}.${O.name} is built again as ${E.name}, and foreign key ${k.join(", ")} reference its columns`,"Postgres binds a foreign key to the first unique index over its columns \u2014 the old one, which then cannot go: change the index in one deploy and the foreign keys in another, or give the new index other key columns")}for(let E of T.gone)T.added.some(w=>w.name===E.name)?(this.changes.push(`index ${i}.${E.name} is built again (the old one goes at complete)`),s.complete.push(`DROP INDEX IF EXISTS ${P(r.schema,E.name)}`)):(this.changes.push(`index ${i}.${E.name} goes (at complete)`),this.completeIndexes.push({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(r.schema,E.name)}`,why:`index ${E.name}`}));for(let E of T.added){let w=m(E.name)?ie(this.n,"i",E.name):u(E.name,d(E));this.changes.push(`index ${i}.${E.name}`),this.online.push({kind:"statement",sql:this.indexSql(r.schema,e.physical,{...E,name:w},e.cols,!0),why:`index ${E.name}`}),this.undoIndexesGone.unshift({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(r.schema,w)}`,why:`index ${E.name}`}),g(w,E.name,!1)}}renamedTwins(e,n,s){for(let r of[...e.gone]){let o=e.added.find(a=>a.name!==r.name&&n(r,a)&&!e.gone.some(i=>i.name===a.name));o&&(e.gone.splice(e.gone.indexOf(r),1),e.added.splice(e.added.indexOf(o),1),s(r,o))}}indexSql(e,n,s,r,o){let a=un(r),i=s.keys.map(l=>{let u="column"in l?b(r.get(l.column)??l.column):`(${ke(l.expr,a)})`;return l.desc&&(u+=" DESC"),l.nulls&&(u+=` NULLS ${l.nulls.toUpperCase()}`),u}),c=`CREATE ${s.unique?"UNIQUE ":""}INDEX ${o?"CONCURRENTLY ":""}IF NOT EXISTS ${b(s.name)} ON ${P(e,n)}`;return s.method&&s.method!=="btree"&&(c+=` USING ${s.method}`),c+=` (${i.join(", ")})`,s.include?.length&&(c+=` INCLUDE (${ye(s.include.map(l=>r.get(l)??l))})`),s.where!==void 0&&(c+=` WHERE ${ke(s.where,a)}`),c}fkSql(e,n){let s=this.to.tables.find(o=>o.schema===e.refSchema&&o.name===e.refTable),r=s?this.where(s):{table:e.refTable,cols:new Map};return`CONSTRAINT ${b(e.name)} FOREIGN KEY (${ye(e.columns.map(o=>n.get(o)??o))}) REFERENCES ${P(e.refSchema,r.table)} (${ye(e.refColumns.map(o=>r.cols.get(o)??o))})${e.match==="full"?" MATCH FULL":""} ON DELETE ${e.onDelete.toUpperCase()} ON UPDATE ${e.onUpdate.toUpperCase()}${e.deferrable?" DEFERRABLE":""}`}fkSqlOld(e){return`CONSTRAINT ${b(e.name)} FOREIGN KEY (${ye(e.columns)}) REFERENCES ${P(e.refSchema,e.refTable)} (${ye(e.refColumns)})${e.match==="full"?" MATCH FULL":""} ON DELETE ${e.onDelete.toUpperCase()} ON UPDATE ${e.onUpdate.toUpperCase()}${e.deferrable?" DEFERRABLE":""}`}generations(e){return[...this.input.generations?.(e)??[]].sort((n,s)=>n.gen-s.gen)}search(e,n){let s=D(e),r=e.search,o=n?.from.search;if(r===void 0){n&&this.searchGone(n.from,n.physical);return}let a=ps(`table ${s}`,e,r);if(a.length>0){this.refusals.push(...a);return}let i=e.columns.find(T=>T.name===e.primaryKey[0]),c=this.generations(n?D(n.from):s),l=c.filter(T=>T.status!=="retired"),u=l.find(T=>T.status==="active"),g=l.at(-1),p=g===void 0?[]:Pn(g,r)?l:u?[u]:[],h=g===void 0?"active":Pn(g,r)||u!==void 0&&Pn(u,r)?void 0:"filling";if(n){let T="take search out of the table and deploy, make the change in the next deploy, and declare search again in the one after \u2014 every row is embedded again then";e.name!==n.from.name&&this.refuse(`table ${e.schema}.${n.from.name} is renamed ${e.name} while it declares search`,T);let E=new Set;for(let w of new Set([i.name,...r.from])){let O=w===i.name?"search keeps each row's vector by it":"search reads it";if(n.retyped.has(w))this.refuse(`column ${s}.${w} changes type, and ${O}`,T);else if((n.cols.get(w)??w)!==w)this.refuse(`column ${s}.${w} ${n.parked.has(w)?"takes a name another column leaves in this deploy":`is renamed from ${n.cols.get(w)}`}, and ${O}`,T);else continue;E.add(n.retyped.get(w)?.from.name??n.cols.get(w)??w)}for(let w of p)for(let O of w.from){if(E.has(O))continue;e.columns.some(N=>N.name===O)&&!n.retyped.has(O)&&(n.cols.get(O)??O)===O||this.refuse(`column ${s}.${O} goes or changes, and the vectors that serve search of ${s} now (generation ${w.gen}) are made from it`,"they serve until the new ones are complete: leave the column as it is in this deploy, and change it in a deploy after that")}}let m={schema:e.schema,name:n?n.physical:e.name},y=P(m.schema,m.name);if(h!==void 0){let T=(c.at(-1)?.gen??0)+1;this.changes.push(h==="active"?`search of ${s}: its rows are embedded with ${r.model} (${r.dimensions} dimensions) in the background; found by their words until then`:`search of ${s}: every row is embedded again with ${r.model} (${r.dimensions} dimensions) in the background; the vectors there serve until the new ones are complete`),this.searchStart.push({kind:"tx",sql:Ds({table:m,gen:T,search:r,status:h,pk:{name:i.name,type:this.typeSql(i.type)},roles:this.input.roles,types:this.input.vectorSchema??"public"}),why:`search of ${s}: generation ${T}`}),n&&this.searchStart.push({kind:"statement",sql:Ps(m,T,i.name),why:`search of ${s}: its rows queued`}),this.undoSearch.push({kind:"tx",sql:Bn(m,T),why:`search of ${s}: generation ${T}`})}if(o!==void 0&&o.staleness!==r.staleness&&p.length>0){this.changes.push(`search of ${s}: a changed row's old vector ${r.staleness==="null"?"goes":"stays"} until the row is embedded again`);for(let T of p)this.searchStart.push({kind:"tx",sql:Un({table:m,gen:T.gen,from:T.from,staleness:r.staleness},!0),why:`search of ${s}: generation ${T.gen}'s triggers`}),this.undoSearch.push({kind:"tx",sql:Un({table:m,gen:T.gen,from:T.from,staleness:o.staleness},!0),why:`search of ${s}: generation ${T.gen}'s triggers`})}for(let T of c)p.includes(T)||(this.changes.push(`search of ${s}: generation ${T.gen}'s vectors, ${T.status==="retired"?"left behind":"never full"}, go (at complete)`),this.searchComplete.push({kind:"tx",sql:Bn(m,T.gen),why:`search of ${s}: generation ${T.gen}`}));let S=Mn(m,r.from),f=o===void 0?void 0:Mn(m,o.from);if(S===f)return;let d=T=>`CREATE INDEX ${T?"CONCURRENTLY ":""}IF NOT EXISTS ${b(S)} ON ${y} USING gin (${Fn(r.from)})`;o!==void 0&&this.changes.push(`search of ${s} reads ${r.from.join(", ")}`),n?(this.online.push({kind:"statement",sql:d(!0),why:`search of ${s}: its words`}),this.undoIndexesGone.unshift({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(m.schema,S)}`,why:`search of ${s}: its words`})):this.creates.push(d(!1)),f!==void 0&&this.completeIndexes.push({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(m.schema,f)}`,why:`search of ${s}: the words it read`})}searchGone(e,n){let s=this.generations(D(e));if(e.search===void 0&&s.length===0)return;let r={schema:e.schema,name:n},o=D(e);this.changes.push(`search of ${o} goes (at complete)`);for(let a of s)this.searchComplete.push({kind:"tx",sql:Bn(r,a.gen),why:`search of ${o}: generation ${a.gen}`});e.search!==void 0&&this.searchComplete.push({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${P(r.schema,Mn(r,e.search.from))}`,why:`search of ${o}: the words it read`})}policyRoles(e,n){let s=new Set;for(let r of e.to){if(r.startsWith("role:")&&r.length>5){s.add(b(r.slice(5)));continue}let o=Nt[r];if(!o){this.refuse(`policy ${n}.${e.name} is for role ${r}`,`a policy is for ${Object.keys(Nt).join(", ")}`);continue}s.add(o==="public"?"PUBLIC":b(this.input.roles[o]))}return[...s].join(", ")}policySql(e,n,s){let r=un(s),o=`CREATE POLICY ${b(n.name)} ON ${e} AS ${n.restrictive?"RESTRICTIVE":"PERMISSIVE"} FOR ${n.for.toUpperCase()} TO ${this.policyRoles(n,e)}`;return n.using!==void 0&&(o+=` USING (${ke(n.using,r)})`),n.withCheck!==void 0&&(o+=` WITH CHECK (${ke(n.withCheck,r)})`),o}policies(e,n,s){let{to:r}=e,o=this.inNewNames(e.from),a=D(r),i=l=>JSON.stringify([l.for,[...new Set(l.to.map(u=>Nt[u]??u))].sort(),l.using??null,l.withCheck??null,l.restrictive??!1]),c=l=>e.from.policies.find(u=>u.name===l);for(let l of r.policies){let u=o.policies.find(g=>g.name===l.name);u&&i(u)===i(l)||this.readsOthersRenamed(r,l,a)||(this.changes.push(`policy ${a}.${l.name}${u?" changes":""}`),s.undo.push(`DROP POLICY IF EXISTS ${b(l.name)} ON ${n}`),u&&(s.tx.push(`DROP POLICY IF EXISTS ${b(l.name)} ON ${n}`),s.undo.push(this.policySql(n,c(l.name),Us(e.from)))),s.tx.push(this.policySql(n,l,e.cols)))}for(let l of o.policies)r.policies.some(u=>u.name===l.name)||(this.changes.push(`policy ${a}.${l.name} goes`),s.tx.push(`DROP POLICY IF EXISTS ${b(l.name)} ON ${n}`),s.undo.push(this.policySql(n,c(l.name),Us(e.from))))}readsOthersRenamed(e,n,s){let r=`${n.using??""} ${n.withCheck??""}`;for(let o of this.to.tables){if(o===e)continue;let a=this.from.tables.find(c=>c.schema===o.schema&&c.name===(o.renamedFrom??o.name));if(!a)continue;let i=[...o.name!==a.name&&Oe(r,o.name)?[`${o.schema}.${o.name}`]:[],...Oe(r,o.name)?o.columns.filter(c=>c.renamedFrom!==void 0&&c.renamedFrom!==c.name&&a.columns.some(l=>l.name===c.renamedFrom)&&Oe(r,c.name)).map(c=>`${o.schema}.${o.name}.${c.name}`):[]];if(i.length>0)return this.refuse(`policy ${s}.${n.name} reads ${i.join(", ")}, renamed in this deploy`,"Postgres has the new name only from complete on: rename in this deploy, change the policy in the next"),!0}return!1}rawUnitsBefore(){let e=!0;for(let n of this.to.raw){if(!n.before)continue;let s=this.from.raw.find(r=>r.name===n.name);if(!(s&&s.sql.trim()===n.sql.trim())){if(s&&/^\s*CREATE\s+(?:DOMAIN|TYPE|SEQUENCE)\b/i.test(s.sql)){this.refuse(`raw ${n.name} changes`,"Postgres cannot drop a domain or a type a column uses, and a sequence made again starts over: say the change as an ALTER (ALTER DOMAIN \u2026 ADD CONSTRAINT \u2026 NOT VALID, ALTER TYPE, ALTER SEQUENCE) in a unit of its own, or make a new one and move the columns to it");continue}this.changes.push(`raw ${n.name}${s?" changes":""}`),e&&this.creates.push("SET LOCAL check_function_bodies = off"),e=!1,this.creates.push(...this.rawApply(n,s)),!s&&n.drop!==void 0&&this.undoCreates.push(n.drop),s&&this.undoRawsBack.push(...this.rawApply(s,n))}}}rawApply(e,n){return!n||/^\s*CREATE\s+OR\s+REPLACE\b/i.test(e.sql)||n.drop===void 0?[e.sql]:[n.drop,e.sql]}rawUnits(){let e=this.to.raw.filter(l=>!l.before),n=l=>/^\s*CREATE\s+(?:OR\s+REPLACE\s+)?(?:MATERIALIZED\s+)?VIEW\b/i.test(l),s=this.namesAtComplete,r=this.dropsColumnsAtComplete,o=e.findIndex(l=>{let u=this.from.raw.find(g=>g.name===l.name);return n(l.sql)?r&&u!==void 0?!0:u===void 0?s:u.sql.trim()!==l.sql.trim():!1}),a=new Set,i=new Set,c=null;if(o>=0){let l=e.slice(o),u=p=>n(p)||/^\s*CREATE\s+(?:OR\s+REPLACE\s+)?(?:FUNCTION|PROCEDURE)\b/i.test(p)||Ot.test(p),g=[];for(let p of[...this.from.raw].reverse()){if(p.before||p.drop===void 0)continue;let h=l.some(y=>y.name===p.name),m=!this.to.raw.some(y=>y.name===p.name);(h&&u(p.sql)||m&&n(p.sql))&&g.push(p.drop),m&&n(p.sql)&&i.add(p.name)}for(let p of l){let h=this.from.raw.find(m=>m.name===p.name);(!h||h.sql.trim()!==p.sql.trim())&&this.changes.push(`raw ${p.name}${h?" changes":""}`),a.add(p.name)}if(s){this.viewsAtComplete={drops:g,creates:l.map(p=>p.sql)};for(let p of l){let h=this.changes.findIndex(m=>m===`raw ${p.name}`||m===`raw ${p.name} changes`);h>=0&&(this.changes[h]+=" (at complete)")}}else{c={kind:"tx",sql:["SET LOCAL check_function_bodies = off",...g,...l.map(p=>p.sql)],why:`raw ${l[0].name} and what follows it, made again`};for(let p of[...l].reverse())p.drop!==void 0&&this.undoRawsGone.push(p.drop);for(let p of this.from.raw){if(p.before||p.drop===void 0)continue;let h=l.some(y=>y.name===p.name),m=!this.to.raw.some(y=>y.name===p.name);(h&&u(p.sql)||m&&n(p.sql))&&this.undoRawsBack.push(p.sql)}}}for(let l of this.to.raw){if(l.before||(c!==null&&l.name===e[o].name&&this.raws.push(c),a.has(l.name)))continue;let u=this.from.raw.find(p=>p.name===l.name),g=Bs(l.sql);if(!(u&&u.sql.trim()===l.sql.trim())){if(u&&g){this.refuse(`raw index ${l.name} changes under its name`,"an index is built CONCURRENTLY next to the one serving, so under a name of its own: give the new one its own name (the old one goes at complete)");continue}if(this.changes.push(`raw ${l.name}${u?" changes":""}`),g)this.raws.push({kind:"statement",sql:g.create,why:`raw ${l.name}`}),this.undoIndexesGone.unshift({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${g.qualified(l,this.to)}`,why:`raw ${l.name}`});else{if(l.drop===void 0&&(Ot.test(l.sql)||n(l.sql))){this.refuse(`raw ${l.name} has no drop`,"a rollback could not take it back, and the old version would write and read past it: give it its drop (raw(name, up, { drop }))");continue}this.raws.push({kind:"tx",sql:["SET LOCAL check_function_bodies = off",...this.rawApply(l,u)],why:`raw ${l.name}`}),l.drop!==void 0&&this.undoRawsGone.unshift(l.drop),u&&this.undoRawsBack.push(...l.drop!==void 0?[u.sql]:this.rawApply(u,l))}}}for(let l of[...this.from.raw].reverse()){if(this.to.raw.some(g=>g.name===l.name))continue;if(i.has(l.name)){this.changes.push(`raw ${l.name} goes (with the views made again)`);continue}let u=Bs(l.sql);if(!u){if(l.drop===void 0){this.refuse(`raw ${l.name} is gone from the schema`,"Palgres does not know how to take back what its SQL made: keep it, or give the unit its drop (raw(name, up, { drop })) and remove it in the deploy after that");continue}this.changes.push(`raw ${l.name} goes (at complete)`),Ot.test(l.sql)?this.completeUnitDrops.push(l.drop):this.completeIndexes.push({kind:"tx",sql:[l.drop],why:`raw ${l.name}`});continue}this.changes.push(`raw index ${l.name} goes (at complete)`),this.completeIndexes.push({kind:"statement",sql:`DROP INDEX CONCURRENTLY IF EXISTS ${u.qualified(l,this.from)}`,why:`raw ${l.name}`})}}views(){let{anon:e,authenticated:n,service:s}=this.input.roles,r=[e,n,s].map(b).join(", "),o=[];for(let a of[...new Set(this.layout.tables.map(i=>i.schema))]){let i=b(Te(this.n,a));o.push(`DROP SCHEMA IF EXISTS ${i} CASCADE`,`CREATE SCHEMA ${i}`,`GRANT USAGE ON SCHEMA ${i} TO ${r}`)}for(let a of this.layout.tables){let i=a.columns.map(l=>l.physical===l.name?b(l.name):`${b(l.physical)} AS ${b(l.name)}`).join(", "),c=P(Te(this.n,a.schema),a.name);o.push(`CREATE VIEW ${c} WITH (security_invoker = true) AS SELECT ${i} FROM ${P(a.schema,a.physical)}`),o.push(`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ${c} TO ${r}`)}return o}dropViews(e,n){let s=[...new Set(n.tables.map(r=>r.schema))];return s.length>0?s.map(r=>`DROP SCHEMA IF EXISTS ${b(Te(e,r))} CASCADE`):["SELECT 1"]}keyOf(e){let n=this.where(e);return e.primaryKey.map(s=>{let r=this.matches.get(D(e))?.retyped.get(s);return r?r.from.name:n.cols.get(s)??s})}};function Us(t){return new Map(t.columns.map(e=>[e.name,e.name]))}function un(t){return new Map([...t].filter(([e,n])=>e!==n))}function At(t,e,n){return e?[`ALTER TABLE ${t} ENABLE ROW LEVEL SECURITY`]:n?[`ALTER TABLE ${t} DISABLE ROW LEVEL SECURITY`]:[]}function Kn(t){return JSON.stringify([t.columns,t.refSchema,t.refTable,t.refColumns,t.onDelete,t.onUpdate,t.match??"simple"])}function Hn(t){return JSON.stringify([t.keys,t.unique,t.where??null,t.method??"btree",t.include??[]])}var Ot=/^\s*(?:CREATE\s+(?:OR\s+REPLACE\s+)?(?:TRIGGER|CONSTRAINT\s+TRIGGER|RULE)\b|DO\s+\$\w*\$\s+BEGIN\s+IF\s+NOT\s+\(EXISTS\s+\(SELECT\s+1\s+FROM\s+pg_catalog\.pg_trigger)/i,Ks=/^\s*CREATE\s+(UNIQUE\s+)?INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+NOT\s+EXISTS\s+)?("[^"]+"|[A-Za-z_][A-Za-z0-9_$]*)\s+ON\s+/i;function _o(t){return Vn(t).map(e=>e.schema===void 0?b(e.name):P(e.schema,e.name))}var Hs=/\bnextval\('((?:[^']|'')+)'(?:::regclass)?\)/gi;function Vn(t){if(t===void 0)return[];let e=[];for(let n of t.matchAll(Hs)){let s=n[1].replace(/''/g,"'"),r='("(?:[^"]|"")+"|[A-Za-z_][A-Za-z0-9_$]*)',o=new RegExp(`^(?:${r}\\.)?${r}$`).exec(s);if(!o)continue;let a=i=>i.startsWith('"')?i.slice(1,-1).replace(/""/g,'"'):i.toLowerCase();e.push(o[1]===void 0?{name:a(o[2])}:{schema:a(o[1]),name:a(o[2])})}return e}function It(t){let e=t.sql.trim().replace(/;\s*$/,""),n=Ks.exec(e);return!n||e.includes(";")?void 0:n[2].replace(/^"|"$/g,"")}function Bs(t){let e=t.trim().replace(/;\s*$/,""),n=Ks.exec(e);if(!n||e.includes(";"))return;let s=n[2].replace(/^"|"$/g,""),r=e.slice(n[0].length);return{unique:!!n[1],create:`CREATE ${n[1]?"UNIQUE ":""}INDEX CONCURRENTLY IF NOT EXISTS ${b(s)} ON ${r}`,qualified:(o,a)=>{let i=/^\s*(?:ONLY\s+)?("[^"]+"|[A-Za-z_][A-Za-z0-9_$]*)(?:\.("[^"]+"|[A-Za-z_][A-Za-z0-9_$]*))?/i.exec(r),c=u=>u.replace(/^"|"$/g,""),l=i?.[2]?c(i[1]):o.table?.split(".")[0]??a.tables.find(u=>u.name===c(i?.[1]??""))?.schema??"public";return P(l,s)}}}import{randomBytes as Ko}from"node:crypto";var qe=(t,e)=>JSON.stringify(t)===JSON.stringify(e),dn=t=>new Map(t.map(e=>[e.name,e])),Ye=t=>({type:t.type,nullable:t.nullable,default:t.default??null,identity:t.identity??null,generated:t.generated??null});function Do(t,e){if(t.name===e)return t;let{renamedFrom:n,...s}=t;return{...s,name:e}}function xt(t,e,n){let s=ge(n),r=[],o=[...t.unmanaged],a=new Map(t.schema.tables.map(f=>[D(f),f])),i=new Map(e.schema.tables.map(f=>[D(f),f])),c=new Set,l=[],u=new Set(s.raw.map(f=>It(f)).filter(f=>f!==void 0));for(let f of s.tables){let d=a.get(D(f))??(f.renamedFrom!==void 0?a.get(`${f.schema}.${f.renamedFrom}`):void 0);if(!d)continue;c.add(D(d));let T=i.get(D(f));if(!T)throw new Error(`palgres: the shadow database has no ${D(f)}: the schema did not make it`);let E=dn(T.columns),w=d.columns.map(k=>{let N=f.columns.find(R=>R.name===k.name)??f.columns.find(R=>R.renamedFrom===k.name&&!d.columns.some(L=>L.name===R.name)),A=N&&E.get(N.name);return N&&A&&qe(Ye(k),Ye(A))?Do(N,k.name):k}),O=(k,N,A)=>{let R=dn(N),L=dn(A);return k.map($=>{let C=R.get($.name),q=L.get($.name);return q&&C&&qe($,C)?q:$})};l.push({schema:d.schema,name:d.name,...f.ignored!==void 0?{ignored:f.ignored}:{},...f.renamedFrom!==void 0&&f.name===d.name?{renamedFrom:f.renamedFrom}:{},columns:w,primaryKey:d.primaryKey,uniques:O(d.uniques,T.uniques,f.uniques),indexes:O(d.indexes.filter(k=>!u.has(k.name)),T.indexes,f.indexes),checks:O(d.checks,T.checks,f.checks),foreignKeys:O(d.foreignKeys.filter(k=>a.has(`${k.refSchema}.${k.refTable}`)&&s.tables.some(N=>N.schema===k.refSchema&&N.name===k.refTable||N.schema===k.refSchema&&N.renamedFrom===k.refTable)?!0:(o.push(`foreign key ${D(d)}.${k.name} references ${k.refSchema}.${k.refTable}, outside the schema \u2014 kept as it is; drop it when its target goes`),!1)),T.foreignKeys,f.foreignKeys),rls:d.rls,policies:O(d.policies,T.policies,f.policies)})}for(let f of a.keys())c.has(f)||o.push(`table ${f}: not in the schema \u2014 kept as it is`);for(let f of t.forced)c.has(f)||o.push(`table ${f}: row-level security FORCEd on its owner \u2014 kept as it is`);let g=new Map(t.schema.tables.flatMap(f=>f.indexes.map(d=>[`${f.schema}.${d.name}`,{t:f,i:d}]))),p=new Map(e.schema.tables.flatMap(f=>f.indexes.map(d=>[`${f.schema}.${d.name}`,{t:f,i:d}]))),h=[],m=!0;for(let[f,d]of e.objects){let T=t.objects.get(f);T===void 0?m=!1:T!==d&&r.push(`${f} is not what the schema's raw SQL makes: drop it, and the migration makes it as the schema says`)}for(let f of s.raw){let d=It(f);if(d===void 0){m&&h.push(f);continue}let T=[...p.keys()].find(O=>O.endsWith(`.${d}`)),E=T&&p.get(T),w=T&&g.get(T);if(!E||!w){let O=[...e.objects.keys()].find(k=>k.startsWith("index ")&&k.endsWith(`.${d}`));O!==void 0&&t.objects.get(O)===e.objects.get(O)&&h.push(f);continue}qe(w.i,E.i)&&D(w.t)===D(E.t)?h.push(f):r.push(`index ${T} is not what the schema's raw unit ${f.name} makes: drop it, and the migration makes it as the schema says`)}let y=[];for(let f of s.enums){let d=t.schema.enums.find(T=>T.schema===f.schema&&T.name===f.name);d&&y.push(qe(d.values,f.values)?f:d)}let S=s.extensions.filter(f=>t.schema.extensions.includes(f));return{schema:ge({tables:l,enums:y,extensions:S,raw:h}),unforce:t.forced.filter(f=>c.has(f)),refusals:r,unmanaged:o}}function Po(t,e){let n=[],s=new Map(t.schema.tables.map(r=>[D(r),r]));for(let r of e.schema.tables){let o=D(r),a=s.get(o);if(!a){n.push(`table ${o} is missing`);continue}for(let u of r.columns)a.columns.some(g=>g.name===u.name)||n.push(`column ${o}.${u.name} is missing`);for(let u of a.columns)r.columns.some(g=>g.name===u.name)||n.push(`column ${o}.${u.name}: not declared`);for(let u of r.columns){let g=a.columns.find(p=>p.name===u.name);g&&!qe(Ye(g),Ye(u))&&n.push(`column ${o}.${u.name}: ${JSON.stringify(Ye(g))}, declared ${JSON.stringify(Ye(u))}`)}qe(a.primaryKey,r.primaryKey)||n.push(`table ${o}: primary key (${a.primaryKey.join(", ")}), declared (${r.primaryKey.join(", ")})`),a.rls!==r.rls&&n.push(`table ${o}: row-level security ${a.rls?"on":"off"}, declared ${r.rls?"on":"off"}`);let i={uniques:"unique",indexes:"index",checks:"check",foreignKeys:"foreign key",policies:"policy"},c=(u,g)=>e.schema.tables.some(p=>p.schema===u&&p.name===g),l={...a,foreignKeys:a.foreignKeys.filter(u=>c(u.refSchema,u.refTable))};for(let[u,g]of Object.entries(i)){let p=dn(l[u]),h=dn(r[u]);for(let[m,y]of h)qe(p.get(m),y)||n.push(`${g} ${o}.${m}: ${p.has(m)?`${JSON.stringify(p.get(m))}, declared ${JSON.stringify(y)}`:"missing"}`);for(let m of p.keys())h.has(m)||n.push(`${g} ${o}.${m}: not declared`)}}return n}function Ce(t,e){let n=t.join("_").replace(/[^A-Za-z0-9_]/g,"_").replace(/^([^A-Za-z_])/,"_$1");if(Buffer.byteLength(n)>55){let r=0;for(let o of t.join("."))r=Math.imul(r,31)+o.charCodeAt(0)>>>0;n=`${n.slice(0,46)}_${r.toString(36)}`}let s=n;for(let r=2;e.has(s);r++)s=`${n}_${r}`;return e.add(s),s}function Mo(t){let e="$palgres$";for(let n=1;t.includes(e);n++)e=`$palgres${n}$`;return e}var vt=(t,e)=>{let n=Mo(e+t);return`DO ${n} BEGIN IF NOT (${t}) THEN ${e}; END IF; END ${n}`};async function Vs(t,e,n,s){let r=new Set,o=[],a=[],i=[],c=await t(`SELECT n.nspname AS schema, t.typname AS name, pg_catalog.format_type(t.typbasetype, t.typtypmod) AS base,
116
116
  CASE WHEN t.typcollation <> 0 AND t.typcollation <> (SELECT b.typcollation FROM pg_catalog.pg_type b WHERE b.oid = t.typbasetype) THEN (SELECT format('%I.%I', cn.nspname, co.collname) FROM pg_catalog.pg_collation co JOIN pg_catalog.pg_namespace cn ON cn.oid = co.collnamespace WHERE co.oid = t.typcollation) END AS collation,
117
117
  pg_catalog.pg_get_expr(t.typdefaultbin, 0) AS default, t.typnotnull AS notnull,
118
118
  (SELECT json_agg(json_build_object('name', c.conname, 'def', pg_catalog.pg_get_constraintdef(c.oid, true)) ORDER BY c.conname) FROM pg_catalog.pg_constraint c WHERE c.contypid = t.oid AND c.contype = 'c') AS checks
119
119
  FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
120
120
  WHERE n.nspname IN ${e} AND t.typtype = 'd'
121
121
  AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d WHERE d.objid = t.oid AND d.deptype = 'e')
122
- ORDER BY t.oid`);for(let m of c){let T=[`AS ${m.base}`];m.collation!==null&&T.push(`COLLATE ${m.collation}`),m.default!==null&&T.push(`DEFAULT ${m.default}`),m.notnull&&T.push("NOT NULL");for(let w of m.checks??[])T.push(`CONSTRAINT ${N(w.name)} ${w.def}`);let E=`domain ${m.schema}.${m.name}`;s.set(E,T.join(" ")),o.push({schema:m.schema,key:E,listed:`type ${m.schema}.${m.name} (`,kind:"domain",name:ve(["domain",m.schema==="public"?m.name:`${m.schema}_${m.name}`],r),before:!0,create:Ot(`EXISTS (SELECT 1 FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace WHERE n.nspname = ${qe(m.schema)} AND t.typname = ${qe(m.name)})`,`CREATE DOMAIN ${P(m.schema,m.name)} ${T.join(" ")}`),drop:`DROP DOMAIN IF EXISTS ${P(m.schema,m.name)}`})}let i=await t(`SELECT n.nspname AS schema, t.typname AS name,
122
+ ORDER BY t.oid`);for(let d of c){let T=[`AS ${d.base}`];d.collation!==null&&T.push(`COLLATE ${d.collation}`),d.default!==null&&T.push(`DEFAULT ${d.default}`),d.notnull&&T.push("NOT NULL");for(let w of d.checks??[])T.push(`CONSTRAINT ${b(w.name)} ${w.def}`);let E=`domain ${d.schema}.${d.name}`;s.set(E,T.join(" ")),o.push({schema:d.schema,key:E,listed:`type ${d.schema}.${d.name} (`,kind:"domain",name:Ce(["domain",d.schema==="public"?d.name:`${d.schema}_${d.name}`],r),before:!0,create:vt(`EXISTS (SELECT 1 FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace WHERE n.nspname = ${Fe(d.schema)} AND t.typname = ${Fe(d.name)})`,`CREATE DOMAIN ${P(d.schema,d.name)} ${T.join(" ")}`),drop:`DROP DOMAIN IF EXISTS ${P(d.schema,d.name)}`})}let l=await t(`SELECT n.nspname AS schema, t.typname AS name,
123
123
  (SELECT json_agg(json_build_object('name', a.attname, 'type', pg_catalog.format_type(a.atttypid, a.atttypmod)) ORDER BY a.attnum) FROM pg_catalog.pg_attribute a WHERE a.attrelid = t.typrelid AND a.attnum > 0 AND NOT a.attisdropped) AS attrs,
124
124
  EXISTS (SELECT 1 FROM pg_catalog.pg_attribute a JOIN pg_catalog.pg_type at ON at.oid = a.atttypid JOIN pg_catalog.pg_class rc ON rc.oid = at.typrelid WHERE a.attrelid = t.typrelid AND rc.relkind <> 'c') AS "usesTable"
125
125
  FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace JOIN pg_catalog.pg_class c ON c.oid = t.typrelid
126
126
  WHERE n.nspname IN ${e} AND t.typtype = 'c' AND c.relkind = 'c'
127
127
  AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d WHERE d.objid = t.oid AND d.deptype = 'e')
128
- ORDER BY t.oid`);for(let m of i){let T=(m.attrs??[]).map(A=>`${N(A.name)} ${A.type}`).join(", "),E=`type ${m.schema}.${m.name}`;s.set(E,T);let w={schema:m.schema,key:E,listed:`type ${m.schema}.${m.name} (`,kind:"type",name:ve(["type",m.schema==="public"?m.name:`${m.schema}_${m.name}`],r),before:!m.usesTable,create:Ot(`EXISTS (SELECT 1 FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace WHERE n.nspname = ${qe(m.schema)} AND t.typname = ${qe(m.name)})`,`CREATE TYPE ${P(m.schema,m.name)} AS (${T})`),drop:`DROP TYPE IF EXISTS ${P(m.schema,m.name)}`};(w.before?o:a).push(w)}let d=await t(`SELECT n.nspname AS schema, c.relname AS name, pg_catalog.format_type(s.seqtypid, NULL) AS type, s.seqstart::text AS start, s.seqincrement::text AS increment,
128
+ ORDER BY t.oid`);for(let d of l){let T=(d.attrs??[]).map(O=>`${b(O.name)} ${O.type}`).join(", "),E=`type ${d.schema}.${d.name}`;s.set(E,T);let w={schema:d.schema,key:E,listed:`type ${d.schema}.${d.name} (`,kind:"type",name:Ce(["type",d.schema==="public"?d.name:`${d.schema}_${d.name}`],r),before:!d.usesTable,create:vt(`EXISTS (SELECT 1 FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace WHERE n.nspname = ${Fe(d.schema)} AND t.typname = ${Fe(d.name)})`,`CREATE TYPE ${P(d.schema,d.name)} AS (${T})`),drop:`DROP TYPE IF EXISTS ${P(d.schema,d.name)}`};(w.before?o:a).push(w)}let u=await t(`SELECT n.nspname AS schema, c.relname AS name, pg_catalog.format_type(s.seqtypid, NULL) AS type, s.seqstart::text AS start, s.seqincrement::text AS increment,
129
129
  s.seqmin::text AS min, s.seqmax::text AS max, s.seqcache::text AS cache, s.seqcycle AS cycle
130
130
  FROM pg_catalog.pg_sequence s JOIN pg_catalog.pg_class c ON c.oid = s.seqrelid JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
131
131
  WHERE n.nspname IN ${e}
132
132
  AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d WHERE d.objid = c.oid AND d.deptype IN ('a', 'i', 'e'))
133
- ORDER BY c.oid`);for(let m of d){let T=`AS ${m.type} INCREMENT BY ${m.increment} MINVALUE ${m.min} MAXVALUE ${m.max} START WITH ${m.start} CACHE ${m.cache}${m.cycle?" CYCLE":" NO CYCLE"}`,E=`sequence ${m.schema}.${m.name}`;s.set(E,T),o.push({schema:m.schema,key:E,listed:`sequence ${m.schema}.${m.name} (`,kind:"sequence",name:ve(["sequence",m.schema==="public"?m.name:`${m.schema}_${m.name}`],r),before:!0,create:`CREATE SEQUENCE IF NOT EXISTS ${P(m.schema,m.name)} ${T}`,drop:`DROP SEQUENCE IF EXISTS ${P(m.schema,m.name)}`})}let u=await t(`SELECT n.nspname AS schema, p.proname AS name, pg_catalog.pg_get_function_identity_arguments(p.oid) AS args, p.prokind AS kind, pg_catalog.pg_get_functiondef(p.oid) AS def,
133
+ ORDER BY c.oid`);for(let d of u){let T=`AS ${d.type} INCREMENT BY ${d.increment} MINVALUE ${d.min} MAXVALUE ${d.max} START WITH ${d.start} CACHE ${d.cache}${d.cycle?" CYCLE":" NO CYCLE"}`,E=`sequence ${d.schema}.${d.name}`;s.set(E,T),o.push({schema:d.schema,key:E,listed:`sequence ${d.schema}.${d.name} (`,kind:"sequence",name:Ce(["sequence",d.schema==="public"?d.name:`${d.schema}_${d.name}`],r),before:!0,create:`CREATE SEQUENCE IF NOT EXISTS ${P(d.schema,d.name)} ${T}`,drop:`DROP SEQUENCE IF EXISTS ${P(d.schema,d.name)}`})}let g=await t(`SELECT n.nspname AS schema, p.proname AS name, pg_catalog.pg_get_function_identity_arguments(p.oid) AS args, p.prokind AS kind, pg_catalog.pg_get_functiondef(p.oid) AS def,
134
134
  EXISTS (SELECT 1 FROM pg_catalog.pg_type t JOIN pg_catalog.pg_class rc ON rc.oid = t.typrelid
135
135
  WHERE rc.relkind <> 'c' AND (t.oid = p.prorettype OR t.oid = ANY (p.proargtypes::oid[]) OR t.oid = ANY (coalesce(p.proallargtypes, '{}'::oid[]))
136
136
  OR t.typarray = p.prorettype)) AS "usesRelation"
137
137
  FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
138
138
  WHERE n.nspname IN ${e} AND p.proname NOT LIKE '\\_palgres\\_%' AND p.prokind IN ('f', 'p', 'w')
139
139
  AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d WHERE d.objid = p.oid AND d.deptype = 'e')
140
- ORDER BY p.oid`);for(let m of u){let T=`function ${m.schema}.${m.name}(${m.args})`,E={schema:m.schema,key:T,listed:`function ${m.schema}.${m.name}(${m.args})`,kind:"function",name:ve(["fn",m.schema==="public"?m.name:`${m.schema}_${m.name}`,m.args===""?"":m.args.replace(/\s+/g,"_")].filter(Boolean),r),before:!m.usesRelation,create:m.def.trim(),drop:`DROP ${m.kind==="p"?"PROCEDURE":"FUNCTION"} IF EXISTS ${P(m.schema,m.name)}(${m.args})`};(E.before?o:l).push(E)}let f=await t(`SELECT n.nspname AS schema, p.proname AS name, pg_catalog.pg_get_function_identity_arguments(p.oid) AS args,
140
+ ORDER BY p.oid`);for(let d of g){let T=`function ${d.schema}.${d.name}(${d.args})`,E={schema:d.schema,key:T,listed:`function ${d.schema}.${d.name}(${d.args})`,kind:"function",name:Ce(["fn",d.schema==="public"?d.name:`${d.schema}_${d.name}`,d.args===""?"":d.args.replace(/\s+/g,"_")].filter(Boolean),r),before:!d.usesRelation,create:d.def.trim(),drop:`DROP ${d.kind==="p"?"PROCEDURE":"FUNCTION"} IF EXISTS ${P(d.schema,d.name)}(${d.args})`};(E.before?o:i).push(E)}let p=await t(`SELECT n.nspname AS schema, p.proname AS name, pg_catalog.pg_get_function_identity_arguments(p.oid) AS args,
141
141
  a.aggtransfn::regprocedure::text AS sfunc, pg_catalog.format_type(a.aggtranstype, NULL) AS stype,
142
142
  CASE WHEN a.aggfinalfn <> 0 THEN a.aggfinalfn::regproc::text END AS final, CASE WHEN a.aggcombinefn <> 0 THEN a.aggcombinefn::regproc::text END AS combine,
143
143
  a.agginitval AS init, CASE WHEN a.aggsortop <> 0 THEN a.aggsortop::regoperator::text END AS sortop, p.proparallel AS parallel,
@@ -145,65 +145,65 @@ END $palgres$`,`CREATE TRIGGER ${g} BEFORE INSERT OR UPDATE ON ${n} FOR EACH ROW
145
145
  FROM pg_catalog.pg_aggregate a JOIN pg_catalog.pg_proc p ON p.oid = a.aggfnoid JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
146
146
  WHERE n.nspname IN ${e} AND a.aggkind = 'n' AND a.aggmtransfn = 0
147
147
  AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d WHERE d.objid = p.oid AND d.deptype = 'e')
148
- ORDER BY p.oid`);for(let m of f){let T=m.sfunc.replace(/\(.*\)$/,""),E=[`SFUNC = ${T}`,`STYPE = ${m.stype}`];m.final!==null&&E.push(`FINALFUNC = ${m.final}`),m.combine!==null&&E.push(`COMBINEFUNC = ${m.combine}`),m.init!==null&&E.push(`INITCOND = ${qe(m.init)}`),m.sortop!==null&&E.push(`SORTOP = ${m.sortop.replace(/\(.*\)$/,"")}`),E.push(`PARALLEL = ${m.parallel==="s"?"SAFE":m.parallel==="r"?"RESTRICTED":"UNSAFE"}`);let w=`aggregate ${m.schema}.${m.name}(${m.args})`;s.set(w,E.join(", "));let A={schema:m.schema,key:w,listed:`function ${m.schema}.${m.name}(${m.args})`,kind:"function",name:ve(["agg",m.schema==="public"?m.name:`${m.schema}_${m.name}`,m.args.replace(/\s+/g,"_")].filter(Boolean),r),before:!m.usesRelation&&!l.some(k=>k.key.startsWith(`function ${T}(`)||m.final!==null&&k.key.startsWith(`function ${m.final}(`)),create:`CREATE OR REPLACE AGGREGATE ${P(m.schema,m.name)}(${m.args}) (${E.join(", ")})`,drop:`DROP AGGREGATE IF EXISTS ${P(m.schema,m.name)}(${m.args})`};(A.before?o:l).push(A)}let g=await t(`SELECT c.oid::int AS oid, n.nspname AS schema, c.relname AS name, c.relkind AS kind, pg_catalog.pg_get_viewdef(c.oid, true) AS def, c.reloptions AS options,
148
+ ORDER BY p.oid`);for(let d of p){let T=d.sfunc.replace(/\(.*\)$/,""),E=[`SFUNC = ${T}`,`STYPE = ${d.stype}`];d.final!==null&&E.push(`FINALFUNC = ${d.final}`),d.combine!==null&&E.push(`COMBINEFUNC = ${d.combine}`),d.init!==null&&E.push(`INITCOND = ${Fe(d.init)}`),d.sortop!==null&&E.push(`SORTOP = ${d.sortop.replace(/\(.*\)$/,"")}`),E.push(`PARALLEL = ${d.parallel==="s"?"SAFE":d.parallel==="r"?"RESTRICTED":"UNSAFE"}`);let w=`aggregate ${d.schema}.${d.name}(${d.args})`;s.set(w,E.join(", "));let O={schema:d.schema,key:w,listed:`function ${d.schema}.${d.name}(${d.args})`,kind:"function",name:Ce(["agg",d.schema==="public"?d.name:`${d.schema}_${d.name}`,d.args.replace(/\s+/g,"_")].filter(Boolean),r),before:!d.usesRelation&&!i.some(k=>k.key.startsWith(`function ${T}(`)||d.final!==null&&k.key.startsWith(`function ${d.final}(`)),create:`CREATE OR REPLACE AGGREGATE ${P(d.schema,d.name)}(${d.args}) (${E.join(", ")})`,drop:`DROP AGGREGATE IF EXISTS ${P(d.schema,d.name)}(${d.args})`};(O.before?o:i).push(O)}let h=await t(`SELECT c.oid::int AS oid, n.nspname AS schema, c.relname AS name, c.relkind AS kind, pg_catalog.pg_get_viewdef(c.oid, true) AS def, c.reloptions AS options,
149
149
  (SELECT array_agg(DISTINCT format('%s.%s', rn.nspname, rc.relname)) FROM pg_catalog.pg_rewrite r JOIN pg_catalog.pg_depend d ON d.classid = 'pg_catalog.pg_rewrite'::regclass AND d.objid = r.oid
150
150
  JOIN pg_catalog.pg_class rc ON rc.oid = d.refobjid JOIN pg_catalog.pg_namespace rn ON rn.oid = rc.relnamespace
151
151
  WHERE r.ev_class = c.oid AND d.refclassid = 'pg_catalog.pg_class'::regclass AND d.refobjid <> c.oid) AS reads
152
152
  FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
153
153
  WHERE n.nspname IN ${e} AND c.relkind IN ('v', 'm')
154
154
  AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d WHERE d.objid = c.oid AND d.deptype = 'e')
155
- ORDER BY c.oid`),p=new Set(n),y=[...g];for(let m=!0;m&&y.length>0;){m=!1;for(let T=0;T<y.length;T++){let E=y[T];if(!(E.reads??[]).every(b=>p.has(b)))continue;y.splice(T--,1),m=!0,p.add(`${E.schema}.${E.name}`);let w=E.def.trim().replace(/;$/,""),A=E.options?.length?` WITH (${E.options.join(", ")})`:"",k=E.kind==="m",R=`${k?"materialized view":"view"} ${E.schema}.${E.name}`;s.set(R,JSON.stringify({options:E.options??[],query:w})),a.push({schema:E.schema,key:R,listed:`${k?"materialized view":"view"} ${E.schema}.${E.name}`,kind:k?"materialized view":"view",name:ve([k?"matview":"view",E.schema==="public"?E.name:`${E.schema}_${E.name}`],r),before:!1,create:k?`CREATE MATERIALIZED VIEW IF NOT EXISTS ${P(E.schema,E.name)}${A} AS ${w}`:`CREATE OR REPLACE VIEW ${P(E.schema,E.name)}${A} AS ${w}`,drop:`DROP ${k?"MATERIALIZED VIEW":"VIEW"} IF EXISTS ${P(E.schema,E.name)}`})}}a.push(...l);let S=await t(`SELECT n.nspname AS schema, c.relname AS table, ic.relname AS name, pg_catalog.pg_get_indexdef(i.indexrelid) AS def
155
+ ORDER BY c.oid`),m=new Set(n),y=[...h];for(let d=!0;d&&y.length>0;){d=!1;for(let T=0;T<y.length;T++){let E=y[T];if(!(E.reads??[]).every(A=>m.has(A)))continue;y.splice(T--,1),d=!0,m.add(`${E.schema}.${E.name}`);let w=E.def.trim().replace(/;$/,""),O=E.options?.length?` WITH (${E.options.join(", ")})`:"",k=E.kind==="m",N=`${k?"materialized view":"view"} ${E.schema}.${E.name}`;s.set(N,JSON.stringify({options:E.options??[],query:w})),a.push({schema:E.schema,key:N,listed:`${k?"materialized view":"view"} ${E.schema}.${E.name}`,kind:k?"materialized view":"view",name:Ce([k?"matview":"view",E.schema==="public"?E.name:`${E.schema}_${E.name}`],r),before:!1,create:k?`CREATE MATERIALIZED VIEW IF NOT EXISTS ${P(E.schema,E.name)}${O} AS ${w}`:`CREATE OR REPLACE VIEW ${P(E.schema,E.name)}${O} AS ${w}`,drop:`DROP ${k?"MATERIALIZED VIEW":"VIEW"} IF EXISTS ${P(E.schema,E.name)}`})}}a.push(...i);let S=await t(`SELECT n.nspname AS schema, c.relname AS table, ic.relname AS name, pg_catalog.pg_get_indexdef(i.indexrelid) AS def
156
156
  FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class ic ON ic.oid = i.indexrelid JOIN pg_catalog.pg_class c ON c.oid = i.indrelid
157
157
  JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace JOIN pg_catalog.pg_am am ON am.oid = ic.relam
158
158
  WHERE n.nspname IN ${e} AND i.indisvalid AND ic.relname NOT LIKE '\\_palgres\\_search\\_%' AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_constraint k WHERE k.conindid = i.indexrelid AND k.contype IN ('p', 'u', 'x'))
159
- AND (c.relkind = 'm' OR (c.relkind = 'r' AND (am.amname NOT IN ('btree', 'hash', 'gin', 'gist', 'brin') OR NOT ${Cn("ic")})))
160
- ORDER BY ic.oid`);for(let m of S){if(!p.has(`${m.schema}.${m.table}`))continue;let T=`index ${m.schema}.${m.name}`;s.set(T,m.def),a.push({schema:m.schema,key:T,listed:`index ${m.schema}.${m.name} `,kind:"index",name:ve(["index",m.schema==="public"?m.name:`${m.schema}_${m.name}`],r),before:!1,create:m.def.replace(/^CREATE (UNIQUE )?INDEX /,"CREATE $1INDEX IF NOT EXISTS "),drop:`DROP INDEX IF EXISTS ${P(m.schema,m.name)}`})}let h=await t(`SELECT n.nspname AS schema, c.relname AS table, t.tgname AS name, pg_catalog.pg_get_triggerdef(t.oid, true) AS def, t.tgconstraint <> 0 AS constraint
159
+ AND (c.relkind = 'm' OR (c.relkind = 'r' AND (am.amname NOT IN ('btree', 'hash', 'gin', 'gist', 'brin') OR NOT ${Dn("ic")})))
160
+ ORDER BY ic.oid`);for(let d of S){if(!m.has(`${d.schema}.${d.table}`))continue;let T=`index ${d.schema}.${d.name}`;s.set(T,d.def),a.push({schema:d.schema,key:T,listed:`index ${d.schema}.${d.name} `,kind:"index",name:Ce(["index",d.schema==="public"?d.name:`${d.schema}_${d.name}`],r),before:!1,create:d.def.replace(/^CREATE (UNIQUE )?INDEX /,"CREATE $1INDEX IF NOT EXISTS "),drop:`DROP INDEX IF EXISTS ${P(d.schema,d.name)}`})}let f=await t(`SELECT n.nspname AS schema, c.relname AS table, t.tgname AS name, pg_catalog.pg_get_triggerdef(t.oid, true) AS def, t.tgconstraint <> 0 AS constraint
161
161
  FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
162
162
  WHERE n.nspname IN ${e} AND NOT t.tgisinternal AND t.tgname NOT LIKE '\\_palgres\\_%'
163
- ORDER BY t.oid`);for(let m of h){if(!p.has(`${m.schema}.${m.table}`))continue;let T=`trigger ${m.schema}.${m.table}.${m.name}`;a.push({schema:m.schema,key:T,listed:`trigger ${m.name} on ${m.schema}.${m.table} `,kind:"trigger",name:ve(["trigger",m.schema==="public"?m.table:`${m.schema}_${m.table}`,m.name],r),before:!1,create:m.constraint?Ot(`EXISTS (SELECT 1 FROM pg_catalog.pg_trigger x WHERE x.tgrelid = ${qe(`${N(m.schema)}.${N(m.table)}`)}::regclass AND x.tgname = ${qe(m.name)})`,m.def):m.def.replace(/^CREATE TRIGGER /,"CREATE OR REPLACE TRIGGER "),drop:`DROP TRIGGER IF EXISTS ${N(m.name)} ON ${P(m.schema,m.table)}`})}return[...o,...a]}var qe=t=>`'${t.replace(/'/g,"''")}'`;var Fs={a:"no action",r:"restrict",c:"cascade",n:"set null",d:"set default"},Io={ALL:"all",SELECT:"select",INSERT:"insert",UPDATE:"update",DELETE:"delete"};async function Kn(t,e,n){let s=await t.reserve();try{return await vo(async r=>await s.unsafe(r),e,n)}finally{s.release()}}async function vo(t,e,n){let s=[],r=[],o=new Map,a=`(${e.map(F).join(", ")})`;await t("BEGIN READ ONLY");try{await t("SET LOCAL search_path = ''");let l=await t(`SELECT c.oid::int AS oid, n.nspname AS schema, c.relname AS name, c.relkind AS kind, c.relrowsecurity AS rls, c.relforcerowsecurity AS forced, c.relispartition AS partition,
163
+ ORDER BY t.oid`);for(let d of f){if(!m.has(`${d.schema}.${d.table}`))continue;let T=`trigger ${d.schema}.${d.table}.${d.name}`;a.push({schema:d.schema,key:T,listed:`trigger ${d.name} on ${d.schema}.${d.table} `,kind:"trigger",name:Ce(["trigger",d.schema==="public"?d.table:`${d.schema}_${d.table}`,d.name],r),before:!1,create:d.constraint?vt(`EXISTS (SELECT 1 FROM pg_catalog.pg_trigger x WHERE x.tgrelid = ${Fe(`${b(d.schema)}.${b(d.table)}`)}::regclass AND x.tgname = ${Fe(d.name)})`,d.def):d.def.replace(/^CREATE TRIGGER /,"CREATE OR REPLACE TRIGGER "),drop:`DROP TRIGGER IF EXISTS ${b(d.name)} ON ${P(d.schema,d.table)}`})}return[...o,...a]}var Fe=t=>`'${t.replace(/'/g,"''")}'`;var Xn={a:"no action",r:"restrict",c:"cascade",n:"set null",d:"set default"},qo={ALL:"all",SELECT:"select",INSERT:"insert",UPDATE:"update",DELETE:"delete"};async function Yn(t,e,n){let s=await t.reserve();try{return await Fo(async r=>await s.unsafe(r),e,n)}finally{s.release()}}async function Fo(t,e,n){let s=[],r=[],o=new Map,a=`(${e.map(M).join(", ")})`;await t("BEGIN READ ONLY");try{await t("SET LOCAL search_path = ''");let i=await t(`SELECT c.oid::int AS oid, n.nspname AS schema, c.relname AS name, c.relkind AS kind, c.relrowsecurity AS rls, c.relforcerowsecurity AS forced, c.relispartition AS partition,
164
164
  EXISTS (SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhrelid = c.oid OR i.inhparent = c.oid) AS inherits,
165
165
  (SELECT e.extname FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_extension e ON e.oid = d.refobjid
166
166
  WHERE d.classid = 'pg_catalog.pg_class'::regclass AND d.objid = c.oid AND d.refclassid = 'pg_catalog.pg_extension'::regclass AND d.deptype = 'e' LIMIT 1) AS extension
167
167
  FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
168
168
  WHERE n.nspname IN ${a} AND c.relkind IN ('r', 'p', 'v', 'm', 'f')
169
- ORDER BY n.nspname, c.relname`),c=[],i=new Map;for(let h of l){let m=`${h.schema}.${h.name}`;if(h.extension!==null){s.push(`table ${m}: the extension ${h.extension}'s own \u2014 left to it`);continue}if(h.kind==="v"||h.kind==="m"||h.kind==="f"){s.push(`${h.kind==="v"?"view":h.kind==="m"?"materialized view":"foreign table"} ${m}`);continue}if(h.kind==="p"||h.partition||h.inherits){s.push(`table ${m}: partitioned or inherited \u2014 Palgres does not migrate it`);continue}h.forced&&r.push(m);let T={schema:h.schema,name:h.name,columns:[],primaryKey:[],uniques:[],indexes:[],checks:[],foreignKeys:[],rls:h.rls,policies:[]};c.push(T),i.set(h.oid,T)}let d=[...i.keys()];if(d.length>0){let h=`(${d.join(", ")})`,m=await t(`SELECT a.attrelid::int AS rel, a.attname AS name, pg_catalog.format_type(a.atttypid, a.atttypmod) AS type, a.attnotnull AS notnull,
169
+ ORDER BY n.nspname, c.relname`),c=[],l=new Map;for(let f of i){let d=`${f.schema}.${f.name}`;if(f.extension!==null){s.push(`table ${d}: the extension ${f.extension}'s own \u2014 left to it`);continue}if(f.kind==="v"||f.kind==="m"||f.kind==="f"){s.push(`${f.kind==="v"?"view":f.kind==="m"?"materialized view":"foreign table"} ${d}`);continue}if(f.kind==="p"||f.partition||f.inherits){s.push(`table ${d}: partitioned or inherited \u2014 Palgres does not migrate it`);continue}f.forced&&r.push(d);let T={schema:f.schema,name:f.name,columns:[],primaryKey:[],uniques:[],indexes:[],checks:[],foreignKeys:[],rls:f.rls,policies:[]};c.push(T),l.set(f.oid,T)}let u=[...l.keys()];if(u.length>0){let f=`(${u.join(", ")})`,d=await t(`SELECT a.attrelid::int AS rel, a.attname AS name, pg_catalog.format_type(a.atttypid, a.atttypmod) AS type, a.attnotnull AS notnull,
170
170
  pg_catalog.pg_get_expr(d.adbin, d.adrelid) AS default, a.attidentity AS identity, a.attgenerated AS generated,
171
171
  CASE WHEN a.attcollation <> t.typcollation THEN co.collname END AS collation
172
172
  FROM pg_catalog.pg_attribute a
173
173
  JOIN pg_catalog.pg_type t ON t.oid = a.atttypid
174
174
  LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attnum
175
175
  LEFT JOIN pg_catalog.pg_collation co ON co.oid = a.attcollation
176
- WHERE a.attrelid IN ${h} AND a.attnum > 0 AND NOT a.attisdropped
177
- ORDER BY a.attrelid, a.attnum`);for(let $ of m){let _=i.get($.rel),M=`${_.schema}.${_.name}.${$.name}`,U=$.generated==="s"||$.generated==="v"?{expr:$.default??"",stored:$.generated==="s"}:void 0;$.collation!==null&&s.push(`column ${M}: collation ${$.collation} \u2014 kept, the model does not say it`);let x={name:$.name,type:Re($.type),nullable:!$.notnull};U?x.generated=U:$.identity==="a"||$.identity==="d"?x.identity=$.identity==="a"?"always":"by default":$.default!==null&&(x.default=$.default),_.columns.push(x)}let T=await t(`SELECT c.conrelid::int AS rel, c.conname AS name, c.contype AS type, pg_catalog.pg_get_constraintdef(c.oid, true) AS def,
176
+ WHERE a.attrelid IN ${f} AND a.attnum > 0 AND NOT a.attisdropped
177
+ ORDER BY a.attrelid, a.attnum`);for(let $ of d){let C=l.get($.rel),q=`${C.schema}.${C.name}.${$.name}`,U=$.generated==="s"||$.generated==="v"?{expr:$.default??"",stored:$.generated==="s"}:void 0;$.collation!==null&&s.push(`column ${q}: collation ${$.collation} \u2014 kept, the model does not say it`);let v={name:$.name,type:be($.type),nullable:!$.notnull};U?v.generated=U:$.identity==="a"||$.identity==="d"?v.identity=$.identity==="a"?"always":"by default":$.default!==null&&(v.default=$.default),C.columns.push(v)}let T=await t(`SELECT c.conrelid::int AS rel, c.conname AS name, c.contype AS type, pg_catalog.pg_get_constraintdef(c.oid, true) AS def,
178
178
  (SELECT array_agg(a.attname ORDER BY k.i) FROM unnest(c.conkey) WITH ORDINALITY AS k(attnum, i) JOIN pg_catalog.pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = k.attnum) AS cols,
179
179
  c.confrelid::int AS refrel,
180
180
  (SELECT array_agg(a.attname ORDER BY k.i) FROM unnest(c.confkey) WITH ORDINALITY AS k(attnum, i) JOIN pg_catalog.pg_attribute a ON a.attrelid = c.confrelid AND a.attnum = k.attnum) AS refcols,
181
- c.confdeltype AS del, c.confupdtype AS upd, c.confmatchtype AS match, c.condeferrable AS deferrable, i.relname AS index
181
+ c.confdeltype AS del, c.confupdtype AS upd, c.confmatchtype AS match, c.condeferrable AS deferrable, c.condeferred AS deferred, i.relname AS index
182
182
  FROM pg_catalog.pg_constraint c LEFT JOIN pg_catalog.pg_class i ON i.oid = c.conindid
183
- WHERE c.conrelid IN ${h}
184
- ORDER BY c.conrelid, c.conname`),E=new Map,w=[...new Set(T.filter($=>$.refrel).map($=>$.refrel))];if(w.length>0)for(let $ of await t(`SELECT c.oid::int AS oid, n.nspname AS schema, c.relname AS name FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.oid IN (${w.join(", ")})`))E.set($.oid,{schema:$.schema,name:$.name});let A=new Set;for(let $ of T){let _=i.get($.rel),M=`${_.schema}.${_.name}`;if($.index&&($.type==="p"||$.type==="u"||$.type==="x")&&A.add(`${_.schema}.${$.index}`),$.deferrable&&$.type!=="n"){s.push(`constraint ${M}.${$.name}: deferrable \u2014 kept as it is, the model does not say it`);continue}switch($.type){case"p":_.primaryKey=$.cols??[];break;case"u":if(/NULLS NOT DISTINCT/i.test($.def)){s.push(`constraint ${M}.${$.name}: UNIQUE NULLS NOT DISTINCT \u2014 kept as it is`);break}_.uniques.push({name:$.name,columns:$.cols??[]});break;case"c":{let U=/^CHECK \((.*)\)( NOT VALID)?$/s.exec($.def);if(!U||/\bNO INHERIT\b/.test($.def)){s.push(`constraint ${M}.${$.name}: ${$.def} \u2014 kept as it is`);break}_.checks.push({name:$.name,expr:U[1]});break}case"f":{let U=$.refrel===null?void 0:E.get($.refrel);if(!U||$.match==="p"){s.push(`constraint ${M}.${$.name}: ${$.def} \u2014 kept as it is`);break}let x={name:$.name,columns:$.cols??[],refSchema:U.schema,refTable:U.name,refColumns:$.refcols??[],onDelete:Fs[$.del],onUpdate:Fs[$.upd]};$.match==="f"&&(x.match="full"),_.foreignKeys.push(x);break}case"n":break;default:s.push(`constraint ${M}.${$.name}: ${$.def} \u2014 kept as it is`)}}let k=await t(`SELECT i.indrelid::int AS rel, n.nspname AS schema, c.relname AS name, i.indisunique AS unique, am.amname AS method, i.indnkeyatts::int AS keyatts,
183
+ WHERE c.conrelid IN ${f}
184
+ ORDER BY c.conrelid, c.conname`),E=new Map,w=[...new Set(T.filter($=>$.refrel).map($=>$.refrel))];if(w.length>0)for(let $ of await t(`SELECT c.oid::int AS oid, n.nspname AS schema, c.relname AS name FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.oid IN (${w.join(", ")})`))E.set($.oid,{schema:$.schema,name:$.name});let O=new Set;for(let $ of T){let C=l.get($.rel),q=`${C.schema}.${C.name}`;$.index&&($.type==="p"||$.type==="u"||$.type==="x")&&O.add(`${C.schema}.${$.index}`);let U=$.type==="f"&&!$.deferred&&je(Xn[$.del],Xn[$.upd]);if($.deferrable&&$.type!=="n"&&!U){s.push(`constraint ${q}.${$.name}: deferrable \u2014 kept as it is, the model does not say it`);continue}switch($.type){case"p":C.primaryKey=$.cols??[];break;case"u":if(/NULLS NOT DISTINCT/i.test($.def)){s.push(`constraint ${q}.${$.name}: UNIQUE NULLS NOT DISTINCT \u2014 kept as it is`);break}C.uniques.push({name:$.name,columns:$.cols??[]});break;case"c":{let v=/^CHECK \((.*)\)( NOT VALID)?$/s.exec($.def);if(!v||/\bNO INHERIT\b/.test($.def)){s.push(`constraint ${q}.${$.name}: ${$.def} \u2014 kept as it is`);break}C.checks.push({name:$.name,expr:v[1]});break}case"f":{let v=$.refrel===null?void 0:E.get($.refrel);if(!v||$.match==="p"){s.push(`constraint ${q}.${$.name}: ${$.def} \u2014 kept as it is`);break}let I={name:$.name,columns:$.cols??[],refSchema:v.schema,refTable:v.name,refColumns:$.refcols??[],onDelete:Xn[$.del],onUpdate:Xn[$.upd]};$.match==="f"&&(I.match="full"),$.deferrable&&(I.deferrable=!0),C.foreignKeys.push(I);break}case"n":break;default:s.push(`constraint ${q}.${$.name}: ${$.def} \u2014 kept as it is`)}}let k=await t(`SELECT i.indrelid::int AS rel, n.nspname AS schema, c.relname AS name, i.indisunique AS unique, am.amname AS method, i.indnkeyatts::int AS keyatts,
185
185
  -- indkey and indoption count from 0, and Bun cannot read an array that does not start at 1 ("[0:1]={\u2026}").
186
186
  ARRAY(SELECT k::int FROM unnest(i.indkey::int2[]) AS k) AS attnums, ARRAY(SELECT o::int FROM unnest(i.indoption::int2[]) AS o) AS options,
187
187
  ARRAY(SELECT pg_catalog.pg_get_indexdef(i.indexrelid, k, true) FROM generate_series(1, i.indnatts) AS k) AS exprs,
188
188
  pg_catalog.pg_get_expr(i.indpred, i.indrelid, true) AS where,
189
- ${Cn("c")} AS "defaultOps",
189
+ ${Dn("c")} AS "defaultOps",
190
190
  i.indisvalid AS valid
191
191
  FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class c ON c.oid = i.indexrelid JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace JOIN pg_catalog.pg_am am ON am.oid = c.relam
192
- WHERE i.indrelid IN ${h} AND c.relname NOT LIKE '\\_palgres\\_search\\_%'
193
- ORDER BY i.indrelid, c.relname`);for(let $ of k){let _=i.get($.rel);if(A.has(`${$.schema}.${$.name}`))continue;let M=`${_.schema}.${_.name}`;if(!$.valid){s.push(`index ${$.schema}.${$.name}: INVALID (a build that did not finish) \u2014 drop it, or Palgres would take it for built`);continue}if(!["btree","hash","gin","gist","brin"].includes($.method)||!$.defaultOps){s.push(`index ${$.schema}.${$.name} of ${M}: ${$.method}${$.defaultOps?"":" with an operator class, a collation, NULLS NOT DISTINCT or storage options of its own"} \u2014 kept as it is`);continue}let U=[];for(let I=0;I<$.keyatts;I++){let v=($.options[I]&1)!==0,B=($.options[I]&2)!==0,q={};v&&(q.desc=!0),B!==v&&(q.nulls=B?"first":"last");let W=$.attnums[I];U.push(W===0?{expr:$.exprs[I],...q}:{column:$.exprs[I].replace(/^"(.*)"$/,"$1").replace(/""/g,'"'),...q})}let x={name:$.name,keys:U,unique:$.unique};$.method!=="btree"&&(x.method=$.method),$.where!==null&&(x.where=$.where),$.keyatts<$.attnums.length&&(x.include=$.exprs.slice($.keyatts).map(I=>I.replace(/^"(.*)"$/,"$1").replace(/""/g,'"'))),_.indexes.push(x)}let R=await t(`SELECT schemaname AS schema, tablename AS table, policyname AS name, permissive, roles::text[] AS roles, cmd, qual, with_check AS check
194
- FROM pg_catalog.pg_policies WHERE schemaname IN ${a} ORDER BY schemaname, tablename, policyname`),b=new Map([[n.anon,"anon"],[n.authenticated,"authenticated"],[n.service,"service_role"],["public","public"]]);for(let $ of R){let _=c.find(x=>x.schema===$.schema&&x.name===$.table);if(!_)continue;let M=$.roles.map(x=>b.get(x)??`role:${x}`),U={name:$.name,for:Io[$.cmd],to:M};$.qual!==null&&(U.using=$.qual),$.check!==null&&(U.withCheck=$.check),$.permissive==="RESTRICTIVE"&&(U.restrictive=!0),_.policies.push(U)}let O=await t(`SELECT format('%s.%s', n.nspname, c.relname) AS table, t.tgname AS name, pg_catalog.pg_get_triggerdef(t.oid, true) AS def FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
195
- WHERE t.tgrelid IN ${h} AND NOT t.tgisinternal AND t.tgname NOT LIKE '\\_palgres\\_%' ORDER BY 1, 2`);for(let $ of O)s.push(`trigger ${$.name} on ${$.table} \u2014 kept as it is`),o.set(`trigger ${$.table}.${$.name}`,$.def);let L=await t(`SELECT format('%s.%s', schemaname, tablename) AS table, rulename AS name FROM pg_catalog.pg_rules WHERE schemaname IN ${a} ORDER BY 1, 2`);for(let $ of L)s.push(`rule ${$.name} on ${$.table} \u2014 kept as it is`)}let u=await t(`SELECT n.nspname AS schema, t.typname AS name, t.typtype AS kind,
192
+ WHERE i.indrelid IN ${f} AND c.relname NOT LIKE '\\_palgres\\_search\\_%'
193
+ ORDER BY i.indrelid, c.relname`);for(let $ of k){let C=l.get($.rel);if(O.has(`${$.schema}.${$.name}`))continue;let q=`${C.schema}.${C.name}`;if(!$.valid){s.push(`index ${$.schema}.${$.name}: INVALID (a build that did not finish) \u2014 drop it, or Palgres would take it for built`);continue}if(!["btree","hash","gin","gist","brin"].includes($.method)||!$.defaultOps){s.push(`index ${$.schema}.${$.name} of ${q}: ${$.method}${$.defaultOps?"":" with an operator class, a collation, NULLS NOT DISTINCT or storage options of its own"} \u2014 kept as it is`);continue}let U=[];for(let I=0;I<$.keyatts;I++){let x=($.options[I]&1)!==0,B=($.options[I]&2)!==0,F={};x&&(F.desc=!0),B!==x&&(F.nulls=B?"first":"last");let W=$.attnums[I];U.push(W===0?{expr:$.exprs[I],...F}:{column:$.exprs[I].replace(/^"(.*)"$/,"$1").replace(/""/g,'"'),...F})}let v={name:$.name,keys:U,unique:$.unique};$.method!=="btree"&&(v.method=$.method),$.where!==null&&(v.where=$.where),$.keyatts<$.attnums.length&&(v.include=$.exprs.slice($.keyatts).map(I=>I.replace(/^"(.*)"$/,"$1").replace(/""/g,'"'))),C.indexes.push(v)}let N=await t(`SELECT schemaname AS schema, tablename AS table, policyname AS name, permissive, roles::text[] AS roles, cmd, qual, with_check AS check
194
+ FROM pg_catalog.pg_policies WHERE schemaname IN ${a} ORDER BY schemaname, tablename, policyname`),A=new Map([[n.anon,"anon"],[n.authenticated,"authenticated"],[n.service,"service_role"],["public","public"]]);for(let $ of N){let C=c.find(v=>v.schema===$.schema&&v.name===$.table);if(!C)continue;let q=$.roles.map(v=>A.get(v)??`role:${v}`),U={name:$.name,for:qo[$.cmd],to:q};$.qual!==null&&(U.using=$.qual),$.check!==null&&(U.withCheck=$.check),$.permissive==="RESTRICTIVE"&&(U.restrictive=!0),C.policies.push(U)}let R=await t(`SELECT format('%s.%s', n.nspname, c.relname) AS table, t.tgname AS name, pg_catalog.pg_get_triggerdef(t.oid, true) AS def FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
195
+ WHERE t.tgrelid IN ${f} AND NOT t.tgisinternal AND t.tgname NOT LIKE '\\_palgres\\_%' ORDER BY 1, 2`);for(let $ of R)s.push(`trigger ${$.name} on ${$.table} \u2014 kept as it is`),o.set(`trigger ${$.table}.${$.name}`,$.def);let L=await t(`SELECT format('%s.%s', schemaname, tablename) AS table, rulename AS name FROM pg_catalog.pg_rules WHERE schemaname IN ${a} ORDER BY 1, 2`);for(let $ of L)s.push(`rule ${$.name} on ${$.table} \u2014 kept as it is`)}let g=await t(`SELECT n.nspname AS schema, t.typname AS name, t.typtype AS kind,
196
196
  CASE WHEN t.typtype = 'e' THEN ARRAY(SELECT e.enumlabel::text FROM pg_catalog.pg_enum e WHERE e.enumtypid = t.oid ORDER BY e.enumsortorder) END AS values
197
197
  FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
198
198
  WHERE n.nspname IN ${a} AND t.typtype IN ('e', 'd', 'c', 'r', 'm')
199
199
  AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_class c WHERE c.reltype = t.oid)
200
- ORDER BY 1, 2`),f=[];for(let h of u)h.kind==="e"?f.push({schema:h.schema,name:h.name,values:h.values??[]}):s.push(`type ${h.schema}.${h.name} (${h.kind==="d"?"domain":h.kind==="c"?"composite":"range"}) \u2014 kept as it is`);let g=await t(`SELECT format('%s.%s(%s)', n.nspname, p.proname, pg_catalog.pg_get_function_identity_arguments(p.oid)) AS name, CASE WHEN p.prokind IN ('f', 'p', 'w') THEN pg_catalog.pg_get_functiondef(p.oid) ELSE '' END AS def FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
200
+ ORDER BY 1, 2`),p=[];for(let f of g)f.kind==="e"?p.push({schema:f.schema,name:f.name,values:f.values??[]}):s.push(`type ${f.schema}.${f.name} (${f.kind==="d"?"domain":f.kind==="c"?"composite":"range"}) \u2014 kept as it is`);let h=await t(`SELECT format('%s.%s(%s)', n.nspname, p.proname, pg_catalog.pg_get_function_identity_arguments(p.oid)) AS name, CASE WHEN p.prokind IN ('f', 'p', 'w') THEN pg_catalog.pg_get_functiondef(p.oid) ELSE '' END AS def FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
201
201
  WHERE n.nspname IN ${a} AND p.proname NOT LIKE '\\_palgres\\_%'
202
202
  AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d WHERE d.objid = p.oid AND d.deptype = 'e')
203
- ORDER BY 1`);for(let h of g)s.push(`function ${h.name} \u2014 kept as it is`),o.set(`function ${h.name}`,h.def);let p=await t(`SELECT format('%s.%s', n.nspname, c.relname) AS name FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
203
+ ORDER BY 1`);for(let f of h)s.push(`function ${f.name} \u2014 kept as it is`),o.set(`function ${f.name}`,f.def);let m=await t(`SELECT format('%s.%s', n.nspname, c.relname) AS name FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
204
204
  WHERE n.nspname IN ${a} AND c.relkind = 'S'
205
205
  AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend d WHERE d.objid = c.oid AND d.deptype IN ('a', 'i'))
206
- ORDER BY 1`);for(let h of p)s.push(`sequence ${h.name} (owned by no column) \u2014 kept as it is`);let y=(await t("SELECT extname AS name FROM pg_catalog.pg_extension WHERE extname <> 'plpgsql' ORDER BY 1")).map(h=>h.name),S=await qs(t,a,new Set(c.map(h=>`${h.schema}.${h.name}`)),o);return{schema:{tables:c,enums:f,extensions:y,raw:[]},unmanaged:s,forced:r,objects:o,units:S}}finally{await t("ROLLBACK")}}var ke={SHARE:1,"SHARE ROW EXCLUSIVE":2,"ACCESS EXCLUSIVE":3},pe=String.raw`(?:"(?:[^"]|"")+"|[A-Za-z_][A-Za-z0-9_$]*)`,de=String.raw`${pe}(?:\s*\.\s*${pe}){0,2}`,z=t=>new RegExp(t,"i");function Bs(t){let e={needs:[],made:[]};for(let n of t)for(let s of Vn(n))Ws(s,e);return e}function Us(t){return t.startsWith('"')?t.slice(1,-1).replace(/""/g,'"'):t.toLowerCase()}var me=t=>[...t.matchAll(new RegExp(pe,"g"))].map(e=>e[0]).join("."),Co=z(String.raw`\b(?:ALTER\s+TABLE|DROP\s+TABLE|CREATE\s+(?:OR\s+REPLACE\s+)?(?:CONSTRAINT\s+)?TRIGGER|DROP\s+TRIGGER|(?:CREATE|ALTER|DROP)\s+POLICY|CREATE\s+(?:UNIQUE\s+)?INDEX|DROP\s+INDEX|CREATE\s+(?:OR\s+REPLACE\s+)?RULE|DROP\s+RULE|TRUNCATE|CREATE\s+(?:UNLOGGED\s+|TEMP(?:ORARY)?\s+)?TABLE|DROP\s+VIEW|CREATE\s+OR\s+REPLACE\s+VIEW|ALTER\s+VIEW)\b`);function Ws(t,e){let n;if(n=/^DO\s+(?:LANGUAGE\s+\w+\s+)?(\$[A-Za-z_0-9]*\$)([\s\S]*?)\1/i.exec(t)){for(let s of Vn(n[2],!0)){let r=Co.exec(s);r&&Ws(s.slice(r.index),e)}return}if(n=z(String.raw`^ALTER\s+TABLE\s+(?:IF\s+EXISTS\s+)?(?:ONLY\s+)?(${de})\s*\*?\s*([\s\S]*)$`).exec(t))return Do(me(n[1]),n[2],e);if(n=z(String.raw`^DROP\s+TABLE\s+(?:IF\s+EXISTS\s+)?([\s\S]*?)(?:\s+(?:CASCADE|RESTRICT))?\s*$`).exec(t)){for(let s of Hn(n[1]))e.needs.push({of:"relation",name:s,mode:"ACCESS EXCLUSIVE"},{of:"table",name:s});return}if(n=z(String.raw`^DROP\s+VIEW\s+(?:IF\s+EXISTS\s+)?([\s\S]*?)(?:\s+(?:CASCADE|RESTRICT))?\s*$`).exec(t)){for(let s of Hn(n[1]))e.needs.push({of:"view",name:s});return}if(n=z(String.raw`^(?:CREATE\s+OR\s+REPLACE\s+(?:(?:TEMP|TEMPORARY)\s+)?(?:RECURSIVE\s+)?VIEW|ALTER\s+VIEW\s+(?:IF\s+EXISTS\s+)?)\s*(${de})`).exec(t)){e.needs.push({of:"view",name:me(n[1])});return}if(n=z(String.raw`^CREATE\s+(?:OR\s+REPLACE\s+)?(?:CONSTRAINT\s+)?TRIGGER\s+${pe}\s+[\s\S]*?\bON\s+(?:ONLY\s+)?(${de})`).exec(t)){e.needs.push({of:"relation",name:me(n[1]),mode:"SHARE ROW EXCLUSIVE"});return}if(n=z(String.raw`^(?:DROP\s+TRIGGER|(?:CREATE|ALTER|DROP)\s+POLICY|DROP\s+RULE)\s+(?:IF\s+EXISTS\s+)?${pe}\s+ON\s+(${de})`).exec(t)){e.needs.push({of:"relation",name:me(n[1]),mode:"ACCESS EXCLUSIVE"});return}if(n=z(String.raw`^CREATE\s+(?:OR\s+REPLACE\s+)?RULE\s+${pe}\s+AS\s+ON\s+\w+\s+TO\s+(${de})`).exec(t)){e.needs.push({of:"relation",name:me(n[1]),mode:"ACCESS EXCLUSIVE"});return}if(n=z(String.raw`^CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?!CONCURRENTLY\b)(?:IF\s+NOT\s+EXISTS\s+)?(?:${pe}\s+)?ON\s+(?:ONLY\s+)?(${de})`).exec(t)){e.needs.push({of:"relation",name:me(n[1]),mode:"SHARE"});return}if(n=z(String.raw`^DROP\s+INDEX\s+(?!CONCURRENTLY\b)(?:IF\s+EXISTS\s+)?([\s\S]*?)(?:\s+(?:CASCADE|RESTRICT))?\s*$`).exec(t)){for(let s of Hn(n[1]))e.needs.push({of:"index",name:s});return}if(n=z(String.raw`^TRUNCATE\s+(?:TABLE\s+)?([\s\S]*?)(?:\s+(?:RESTART|CONTINUE|CASCADE|RESTRICT)\b[\s\S]*)?$`).exec(t)){for(let s of Hn(n[1]))e.needs.push({of:"relation",name:s,mode:"ACCESS EXCLUSIVE"});return}if(n=z(String.raw`^CREATE\s+(?:(?:GLOBAL|LOCAL)\s+)?(?:TEMP(?:ORARY)?\s+|UNLOGGED\s+)?TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(${de})([\s\S]*)$`).exec(t)){e.made.push(me(n[1]));let s=z(String.raw`^\s*PARTITION\s+OF\s+(${de})`).exec(n[2]);s&&e.needs.push({of:"relation",name:me(s[1]),mode:"ACCESS EXCLUSIVE"});for(let r of js(n[2]))e.needs.push({of:"relation",name:r,mode:"SHARE ROW EXCLUSIVE"})}}var _o=[/^VALIDATE\s+CONSTRAINT\b/i,z(String.raw`^(?:ALTER\s+(?:COLUMN\s+)?${pe}\s+)?SET\s+STATISTICS\b`),z(String.raw`^(?:ALTER\s+(?:COLUMN\s+)?${pe}\s+)?(?:SET|RESET)\s*\(`),/^CLUSTER\s+ON\b/i,/^SET\s+WITHOUT\s+CLUSTER\b/i];function Do(t,e,n){let s=null,r=[];for(let o of Vn(e,!1,",")){let a,l="ACCESS EXCLUSIVE";z(String.raw`^ADD\s+(?:CONSTRAINT\s+${pe}\s+)?FOREIGN\s+KEY\b`).test(o)||/^(?:ENABLE|DISABLE)\s+(?:REPLICA\s+|ALWAYS\s+)?TRIGGER\b/i.test(o)?l="SHARE ROW EXCLUSIVE":_o.some(c=>c.test(o))?l=null:(a=z(String.raw`^ATTACH\s+PARTITION\s+(${de})`).exec(o))?(l=null,r.push({of:"relation",name:me(a[1]),mode:"ACCESS EXCLUSIVE"})):(a=z(String.raw`^DETACH\s+PARTITION\s+(${de})`).exec(o))?r.push({of:"relation",name:me(a[1]),mode:"ACCESS EXCLUSIVE"}):(a=z(String.raw`^DROP\s+CONSTRAINT\s+(?:IF\s+EXISTS\s+)?(${pe})`).exec(o))?r.push({of:"constraint",table:t,name:Us(a[1])}):(a=z(String.raw`^DROP\s+(?:COLUMN\s+)?(?:IF\s+EXISTS\s+)?(${pe})`).exec(o))&&r.push({of:"column",table:t,name:Us(a[1])});for(let c of js(o))r.push({of:"relation",name:c,mode:"SHARE ROW EXCLUSIVE"});l!==null&&(s===null||ke[l]>ke[s])&&(s=l)}s!==null&&n.needs.push({of:"relation",name:t,mode:s}),n.needs.push(...r)}function js(t){return[...t.matchAll(new RegExp(String.raw`\bREFERENCES\s+(${de})`,"gi"))].map(e=>me(e[1]))}function Hn(t){return Vn(t,!1,",").map(e=>z(String.raw`^(?:ONLY\s+)?(${de})\s*\*?$`).exec(e)?.[1]).filter(e=>e!==void 0).map(me)}async function Ks(t,e){if(e.length===0)return[];let n=e.map((c,i)=>c.of==="relation"?{i,of:c.of,rel:c.name,mode:c.mode}:c.of==="constraint"||c.of==="column"?{i,of:c.of,rel:c.table,name:c.name}:{i,of:c.of,rel:c.name}),s=await t(`WITH r AS (SELECT n.*, to_regclass(n.rel)::oid AS oid FROM jsonb_to_recordset(${F(JSON.stringify(n))}::jsonb) AS n(i int, "of" text, rel text, name text, mode text)),
206
+ ORDER BY 1`);for(let f of m)s.push(`sequence ${f.name} (owned by no column) \u2014 kept as it is`);let y=(await t("SELECT extname AS name FROM pg_catalog.pg_extension WHERE extname <> 'plpgsql' ORDER BY 1")).map(f=>f.name),S=await Vs(t,a,new Set(c.map(f=>`${f.schema}.${f.name}`)),o);return{schema:{tables:c,enums:p,extensions:y,raw:[]},unmanaged:s,forced:r,objects:o,units:S}}finally{await t("ROLLBACK")}}var Le={SHARE:1,"SHARE ROW EXCLUSIVE":2,"ACCESS EXCLUSIVE":3},fe=String.raw`(?:"(?:[^"]|"")+"|[A-Za-z_][A-Za-z0-9_$]*)`,me=String.raw`${fe}(?:\s*\.\s*${fe}){0,2}`,z=t=>new RegExp(t,"i");function Xs(t){let e={needs:[],made:[]};for(let n of t)for(let s of zn(n))Ys(s,e);return e}function Gs(t){return t.startsWith('"')?t.slice(1,-1).replace(/""/g,'"'):t.toLowerCase()}var pe=t=>[...t.matchAll(new RegExp(fe,"g"))].map(e=>e[0]).join("."),Uo=z(String.raw`\b(?:ALTER\s+TABLE|DROP\s+TABLE|CREATE\s+(?:OR\s+REPLACE\s+)?(?:CONSTRAINT\s+)?TRIGGER|DROP\s+TRIGGER|(?:CREATE|ALTER|DROP)\s+POLICY|CREATE\s+(?:UNIQUE\s+)?INDEX|DROP\s+INDEX|CREATE\s+(?:OR\s+REPLACE\s+)?RULE|DROP\s+RULE|TRUNCATE|CREATE\s+(?:UNLOGGED\s+|TEMP(?:ORARY)?\s+)?TABLE|DROP\s+VIEW|CREATE\s+OR\s+REPLACE\s+VIEW|ALTER\s+VIEW)\b`);function Ys(t,e){let n;if(n=/^DO\s+(?:LANGUAGE\s+\w+\s+)?(\$[A-Za-z_0-9]*\$)([\s\S]*?)\1/i.exec(t)){for(let s of zn(n[2],!0)){let r=Uo.exec(s);r&&Ys(s.slice(r.index),e)}return}if(n=z(String.raw`^ALTER\s+TABLE\s+(?:IF\s+EXISTS\s+)?(?:ONLY\s+)?(${me})\s*\*?\s*([\s\S]*)$`).exec(t))return Wo(pe(n[1]),n[2],e);if(n=z(String.raw`^DROP\s+TABLE\s+(?:IF\s+EXISTS\s+)?([\s\S]*?)(?:\s+(?:CASCADE|RESTRICT))?\s*$`).exec(t)){for(let s of Jn(n[1]))e.needs.push({of:"relation",name:s,mode:"ACCESS EXCLUSIVE"},{of:"table",name:s});return}if(n=z(String.raw`^DROP\s+VIEW\s+(?:IF\s+EXISTS\s+)?([\s\S]*?)(?:\s+(?:CASCADE|RESTRICT))?\s*$`).exec(t)){for(let s of Jn(n[1]))e.needs.push({of:"view",name:s});return}if(n=z(String.raw`^(?:CREATE\s+OR\s+REPLACE\s+(?:(?:TEMP|TEMPORARY)\s+)?(?:RECURSIVE\s+)?VIEW|ALTER\s+VIEW\s+(?:IF\s+EXISTS\s+)?)\s*(${me})`).exec(t)){e.needs.push({of:"view",name:pe(n[1])});return}if(n=z(String.raw`^CREATE\s+(?:OR\s+REPLACE\s+)?(?:CONSTRAINT\s+)?TRIGGER\s+${fe}\s+[\s\S]*?\bON\s+(?:ONLY\s+)?(${me})`).exec(t)){e.needs.push({of:"relation",name:pe(n[1]),mode:"SHARE ROW EXCLUSIVE"});return}if(n=z(String.raw`^(?:DROP\s+TRIGGER|(?:CREATE|ALTER|DROP)\s+POLICY|DROP\s+RULE)\s+(?:IF\s+EXISTS\s+)?${fe}\s+ON\s+(${me})`).exec(t)){e.needs.push({of:"relation",name:pe(n[1]),mode:"ACCESS EXCLUSIVE"});return}if(n=z(String.raw`^CREATE\s+(?:OR\s+REPLACE\s+)?RULE\s+${fe}\s+AS\s+ON\s+\w+\s+TO\s+(${me})`).exec(t)){e.needs.push({of:"relation",name:pe(n[1]),mode:"ACCESS EXCLUSIVE"});return}if(n=z(String.raw`^CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?!CONCURRENTLY\b)(?:IF\s+NOT\s+EXISTS\s+)?(?:${fe}\s+)?ON\s+(?:ONLY\s+)?(${me})`).exec(t)){e.needs.push({of:"relation",name:pe(n[1]),mode:"SHARE"});return}if(n=z(String.raw`^DROP\s+INDEX\s+(?!CONCURRENTLY\b)(?:IF\s+EXISTS\s+)?([\s\S]*?)(?:\s+(?:CASCADE|RESTRICT))?\s*$`).exec(t)){for(let s of Jn(n[1]))e.needs.push({of:"index",name:s});return}if(n=z(String.raw`^TRUNCATE\s+(?:TABLE\s+)?([\s\S]*?)(?:\s+(?:RESTART|CONTINUE|CASCADE|RESTRICT)\b[\s\S]*)?$`).exec(t)){for(let s of Jn(n[1]))e.needs.push({of:"relation",name:s,mode:"ACCESS EXCLUSIVE"});return}if(n=z(String.raw`^CREATE\s+(?:(?:GLOBAL|LOCAL)\s+)?(?:TEMP(?:ORARY)?\s+|UNLOGGED\s+)?TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(${me})([\s\S]*)$`).exec(t)){e.made.push(pe(n[1]));let s=z(String.raw`^\s*PARTITION\s+OF\s+(${me})`).exec(n[2]);s&&e.needs.push({of:"relation",name:pe(s[1]),mode:"ACCESS EXCLUSIVE"});for(let r of Js(n[2]))e.needs.push({of:"relation",name:r,mode:"SHARE ROW EXCLUSIVE"})}}var Bo=[/^VALIDATE\s+CONSTRAINT\b/i,z(String.raw`^(?:ALTER\s+(?:COLUMN\s+)?${fe}\s+)?SET\s+STATISTICS\b`),z(String.raw`^(?:ALTER\s+(?:COLUMN\s+)?${fe}\s+)?(?:SET|RESET)\s*\(`),/^CLUSTER\s+ON\b/i,/^SET\s+WITHOUT\s+CLUSTER\b/i];function Wo(t,e,n){let s=null,r=[];for(let o of zn(e,!1,",")){let a,i="ACCESS EXCLUSIVE";z(String.raw`^ADD\s+(?:CONSTRAINT\s+${fe}\s+)?FOREIGN\s+KEY\b`).test(o)||/^(?:ENABLE|DISABLE)\s+(?:REPLICA\s+|ALWAYS\s+)?TRIGGER\b/i.test(o)?i="SHARE ROW EXCLUSIVE":Bo.some(c=>c.test(o))?i=null:(a=z(String.raw`^ATTACH\s+PARTITION\s+(${me})`).exec(o))?(i=null,r.push({of:"relation",name:pe(a[1]),mode:"ACCESS EXCLUSIVE"})):(a=z(String.raw`^DETACH\s+PARTITION\s+(${me})`).exec(o))?r.push({of:"relation",name:pe(a[1]),mode:"ACCESS EXCLUSIVE"}):(a=z(String.raw`^DROP\s+CONSTRAINT\s+(?:IF\s+EXISTS\s+)?(${fe})`).exec(o))?r.push({of:"constraint",table:t,name:Gs(a[1])}):(a=z(String.raw`^DROP\s+(?:COLUMN\s+)?(?:IF\s+EXISTS\s+)?(${fe})`).exec(o))&&r.push({of:"column",table:t,name:Gs(a[1])});for(let c of Js(o))r.push({of:"relation",name:c,mode:"SHARE ROW EXCLUSIVE"});i!==null&&(s===null||Le[i]>Le[s])&&(s=i)}s!==null&&n.needs.push({of:"relation",name:t,mode:s}),n.needs.push(...r)}function Js(t){return[...t.matchAll(new RegExp(String.raw`\bREFERENCES\s+(${me})`,"gi"))].map(e=>pe(e[1]))}function Jn(t){return zn(t,!1,",").map(e=>z(String.raw`^(?:ONLY\s+)?(${me})\s*\*?$`).exec(e)?.[1]).filter(e=>e!==void 0).map(pe)}async function zs(t,e){if(e.length===0)return[];let n=e.map((c,l)=>c.of==="relation"?{i:l,of:c.of,rel:c.name,mode:c.mode}:c.of==="constraint"||c.of==="column"?{i:l,of:c.of,rel:c.table,name:c.name}:{i:l,of:c.of,rel:c.name}),s=await t(`WITH r AS (SELECT n.*, to_regclass(n.rel)::oid AS oid FROM jsonb_to_recordset(${M(JSON.stringify(n))}::jsonb) AS n(i int, "of" text, rel text, name text, mode text)),
207
207
  got AS (
208
208
  SELECT r.i, r.oid AS rel, r.mode FROM r WHERE r.of IN ('relation', 'view') AND r.oid IS NOT NULL
209
209
  UNION ALL
@@ -224,37 +224,48 @@ END $palgres$`,`CREATE TRIGGER ${g} BEFORE INSERT OR UPDATE ON ${n} FOR EACH ROW
224
224
  FROM got g JOIN pg_catalog.pg_class k ON k.oid = g.rel
225
225
  WHERE k.relkind IN ('r', 'p', 'v')
226
226
  AND (pg_catalog.pg_has_role(k.relowner, 'USAGE') OR pg_catalog.has_table_privilege(g.rel, 'UPDATE, DELETE, TRUNCATE, MAINTAIN'))
227
- ORDER BY g.i`),r=new Map,o=new Map;for(let c of s){let i=String(c.oid),d=c.mode;if(c.kind==="v"){o.has(i)||o.set(i,{lock:{oid:i,name:String(c.name),mode:"ACCESS EXCLUSIVE"},reads:c.reads.map(String)});continue}let u=r.get(i);u?ke[d]>ke[u.mode]&&(u.mode=d):r.set(i,{oid:i,name:String(c.name),mode:d})}let a=c=>r.get(c)?.mode==="ACCESS EXCLUSIVE";return[...[...o.values()].filter(c=>c.reads.length>0&&c.reads.every(a)).map(c=>c.lock),...r.values()]}async function Hs(t,e,n){if(e.length===0)return new Set;let s=e.map(o=>`(${F(o.oid)}::oid, ${F(o.mode)})`).join(", "),r=await t(`SELECT DISTINCT h.relation::text AS oid
227
+ ORDER BY g.i`),r=new Map,o=new Map;for(let c of s){let l=String(c.oid),u=c.mode;if(c.kind==="v"){o.has(l)||o.set(l,{lock:{oid:l,name:String(c.name),mode:"ACCESS EXCLUSIVE"},reads:c.reads.map(String)});continue}let g=r.get(l);g?Le[u]>Le[g.mode]&&(g.mode=u):r.set(l,{oid:l,name:String(c.name),mode:u})}let a=c=>r.get(c)?.mode==="ACCESS EXCLUSIVE";return[...[...o.values()].filter(c=>c.reads.length>0&&c.reads.every(a)).map(c=>c.lock),...r.values()]}async function Qs(t,e,n){if(e.length===0)return new Set;let s=e.map(o=>`(${M(o.oid)}::oid, ${M(o.mode)})`).join(", "),r=await t(`SELECT DISTINCT h.relation::text AS oid
228
228
  FROM pg_catalog.pg_locks h JOIN (VALUES ${s}) AS want(rel, mode) ON want.rel = h.relation
229
229
  JOIN pg_catalog.pg_locks w ON w.pid = h.pid AND NOT w.granted
230
230
  WHERE h.locktype = 'relation' AND h.granted AND h.pid <> pg_backend_pid()
231
231
  AND (want.mode = 'ACCESS EXCLUSIVE' OR h.mode NOT IN ('AccessShareLock', 'RowShareLock'))
232
- AND w.waitstart < clock_timestamp() - make_interval(secs => ${Math.max(0,n)/1e3})`);return new Set(r.map(o=>String(o.oid)))}var Po={ShareLock:"SHARE",ShareRowExclusiveLock:"SHARE ROW EXCLUSIVE",ExclusiveLock:"ACCESS EXCLUSIVE",AccessExclusiveLock:"ACCESS EXCLUSIVE"};async function Vs(t,e){let n=e.length===0?"":`AND l.relation NOT IN (SELECT o FROM unnest(ARRAY[${e.map(o=>`to_regclass(${F(o)})::oid`).join(", ")}]) AS o WHERE o IS NOT NULL)`,s=await t(`SELECT l.relation::text AS oid, l.relation::regclass::text AS name, l.mode
232
+ AND w.waitstart < clock_timestamp() - make_interval(secs => ${Math.max(0,n)/1e3})`);return new Set(r.map(o=>String(o.oid)))}var jo={ShareLock:"SHARE",ShareRowExclusiveLock:"SHARE ROW EXCLUSIVE",ExclusiveLock:"ACCESS EXCLUSIVE",AccessExclusiveLock:"ACCESS EXCLUSIVE"};async function Zs(t,e){let n=e.length===0?"":`AND l.relation NOT IN (SELECT o FROM unnest(ARRAY[${e.map(o=>`to_regclass(${M(o)})::oid`).join(", ")}]) AS o WHERE o IS NOT NULL)`,s=await t(`SELECT l.relation::text AS oid, l.relation::regclass::text AS name, l.mode
233
233
  FROM pg_catalog.pg_locks l JOIN pg_catalog.pg_class c ON c.oid = l.relation
234
234
  WHERE l.pid = pg_backend_pid() AND l.locktype = 'relation' AND l.granted AND c.relkind IN ('r', 'p')
235
- AND l.mode IN ('ShareLock', 'ShareRowExclusiveLock', 'ExclusiveLock', 'AccessExclusiveLock') ${n}`),r=new Map;for(let o of s){let a=String(o.oid),l=Po[String(o.mode)],c=r.get(a);c?ke[l]>ke[c.mode]&&(c.mode=l):r.set(a,{oid:a,name:String(o.name),mode:l})}return[...r.values()]}function Vn(t,e=!1,n=";"){let s=[],r="",o=0,a=0,l=t.length;for(;a<l;){let c=t[a];if(c==="-"&&t[a+1]==="-"){for(;a<l&&t[a]!==`
236
- `;)a++;r+=" ";continue}if(c==="/"&&t[a+1]==="*"){let i=0;do t[a]==="/"&&t[a+1]==="*"?(i++,a+=2):t[a]==="*"&&t[a+1]==="/"?(i--,a+=2):a++;while(a<l&&i>0);r+=" ";continue}if(c==="'"){let i=/[Ee]/.test(t[a-1]??"")&&!/[A-Za-z0-9_$]/.test(t[a-2]??""),d=a+1;for(;d<l;)if(i&&t[d]==="\\")d+=2;else if(t[d]==="'"&&t[d+1]==="'")d+=2;else{if(t[d]==="'")break;d++}r+=e?`'${" ".repeat(Math.max(0,d-a-1))}'`:t.slice(a,d+1),a=d+1;continue}if(c==='"'){let i=a+1;for(;i<l&&!(t[i]==='"'&&t[i+1]!=='"');)i+=t[i]==='"'?2:1;r+=t.slice(a,i+1),a=i+1;continue}if(c==="$"&&!/[A-Za-z0-9_$]/.test(t[a-1]??"")){let i=/^\$[A-Za-z_][A-Za-z0-9_]*\$|^\$\$/.exec(t.slice(a));if(i){let d=t.indexOf(i[0],a+i[0].length),u=d<0?l:d+i[0].length;r+=t.slice(a,u),a=u;continue}}if(c==="("?o++:c===")"&&o--,c===n&&o===0){r.trim()&&s.push(r.trim()),r="",a++;continue}r+=c,a++}return r.trim()&&s.push(r.trim()),s}var Fe=class extends Error{refusals;constructor(e){super(`palgres: the migration is refused:
235
+ AND l.mode IN ('ShareLock', 'ShareRowExclusiveLock', 'ExclusiveLock', 'AccessExclusiveLock') ${n}`),r=new Map;for(let o of s){let a=String(o.oid),i=jo[String(o.mode)],c=r.get(a);c?Le[i]>Le[c.mode]&&(c.mode=i):r.set(a,{oid:a,name:String(o.name),mode:i})}return[...r.values()]}function zn(t,e=!1,n=";"){let s=[],r="",o=0,a=0,i=t.length;for(;a<i;){let c=t[a];if(c==="-"&&t[a+1]==="-"){for(;a<i&&t[a]!==`
236
+ `;)a++;r+=" ";continue}if(c==="/"&&t[a+1]==="*"){let l=0;do t[a]==="/"&&t[a+1]==="*"?(l++,a+=2):t[a]==="*"&&t[a+1]==="/"?(l--,a+=2):a++;while(a<i&&l>0);r+=" ";continue}if(c==="'"){let l=/[Ee]/.test(t[a-1]??"")&&!/[A-Za-z0-9_$]/.test(t[a-2]??""),u=a+1;for(;u<i;)if(l&&t[u]==="\\")u+=2;else if(t[u]==="'"&&t[u+1]==="'")u+=2;else{if(t[u]==="'")break;u++}r+=e?`'${" ".repeat(Math.max(0,u-a-1))}'`:t.slice(a,u+1),a=u+1;continue}if(c==='"'){let l=a+1;for(;l<i&&!(t[l]==='"'&&t[l+1]!=='"');)l+=t[l]==='"'?2:1;r+=t.slice(a,l+1),a=l+1;continue}if(c==="$"&&!/[A-Za-z0-9_$]/.test(t[a-1]??"")){let l=/^\$[A-Za-z_][A-Za-z0-9_]*\$|^\$\$/.exec(t.slice(a));if(l){let u=t.indexOf(l[0],a+l[0].length),g=u<0?i:u+l[0].length;r+=t.slice(a,g),a=g;continue}}if(c==="("?o++:c===")"&&o--,c===n&&o===0){r.trim()&&s.push(r.trim()),r="",a++;continue}r+=c,a++}return r.trim()&&s.push(r.trim()),s}var $e=class extends Error{refusals;constructor(e){super(`palgres: the migration is refused:
237
237
  - ${e.join(`
238
- - `)}`),this.name="MigrationRefused",this.refusals=e}},dn=class extends Error{from;to;openedBy;constructor(e,n,s=null){super(`palgres: the migration from version ${e??"none"} to ${n}${s?`, opened by ${s},`:""} is still open: complete it or roll it back first`),this.name="MigrationOpen",this.from=e,this.to=n,this.openedBy=s}},mn=class extends Error{version;constructor(e,n,s){super(`palgres: ${n} connection(s) of version ${e} are still open after ${s} ms`),this.name="VersionStillConnected",this.version=e}},kt=1e4,un=class extends Error{constructor(e,n){super(`palgres: ${e} was not had in the step's time`,{cause:n}),this.name="LockNotHad"}},pn=class extends Error{constructor(e,n){super(`palgres: rows of ${e} to fill were held by writers through ${n} passes`),this.name="RowsHeld"}},qo=new Set(["55P03","40P01","40001","57014","25006"]);function Fo(t){if(ce(t)!==void 0)return!0;let e=t;for(let n=0;n<4&&typeof e=="object"&&e!==null;n++){if(e instanceof mn||e instanceof pn)return!0;let s=ae(e);if(s!==void 0)return qo.has(s)||s.startsWith("53");e=e.cause}return!1}var Xe={anon:"anon",authenticated:"authenticated",service:"service_role"},Gn=t=>`palgres:v${t}`,Gs="palgres.migrate",xt=class t{pool;roles;openedBy;lockTimeoutMs;statementWaitMs;lockTimeoutFor(e){return e>=3?Math.max(this.lockTimeoutMs,1500):this.lockTimeoutMs}attempts;batchSize;disconnectTimeoutMs;log;shadow;constructor(e,n={}){this.pool=e,this.roles={...Xe,...n.roles},this.lockTimeoutMs=n.lockTimeoutMs??500,this.statementWaitMs=n.statementWaitMs??6e5,this.attempts=n.attempts??10,this.batchSize=n.batchSize??1e3,this.disconnectTimeoutMs=n.disconnectTimeoutMs??6e4,this.log=n.log??(()=>{}),this.shadow=n.shadow,this.openedBy=n.openedBy??"deploy"}async status(){return this.session(async e=>{let n=await this.active(e),s=await this.open(e);return{active:n&&{n:n.n,digest:n.digest,state:"active"},open:s&&{from:s.from_n,to:s.to_n,phase:s.phase,opened_by:s.opened_by}}},!1)}async plan(e){Xs(e);let n=De(e),s=async(r,o)=>{let a;try{a=await this.adopt(r,e)}catch(i){if(!(i instanceof Fe))throw i;return{from:null,to:1,changes:[],refusals:i.refusals,start:[],complete:[],rollback:[],layout:{tables:[]}}}let l=o?[]:await this.authRefusals(r);if(!a){let i=await this.planFrom(r,null,e);return{...i,refusals:[...l,...i.refusals]}}if(a.version.digest===n)return{from:null,to:a.version.n,changes:[],refusals:l,start:[],complete:[],rollback:[],layout:a.layout,adoption:a.adoption};let c=await this.planFrom(r,a.version,e);return{...c,refusals:[...l,...c.refusals],adoption:a.adoption}};return this.session(async r=>{let o=await this.active(r);return o?this.planFrom(r,o,e):s(r,!0)},!1,r=>s(r,!1))}async start(e){Xs(e);let n=De(e);return this.session(async s=>{let r=await this.open(s);if(r){if((await this.version(s,r.to_n))?.digest!==n)throw new dn(r.from_n,r.to_n,r.opened_by);if(r.phase==="started")return{state:"resumed",from:r.from_n,to:r.to_n,changes:r.plan.changes,completed:!1};if(r.phase!=="starting")throw new dn(r.from_n,r.to_n);return this.log(`palgres: resuming the start of version ${r.to_n}`),await this.runStart(s,r),{state:"resumed",from:r.from_n,to:r.to_n,changes:r.plan.changes,completed:!1}}let o=await this.active(s);if(o?.digest===n)return{state:"in-sync",version:o.n};let a=o?null:await this.adopt(s,e),l=async()=>{s.ledger===!1&&(await this.ownAuth(s),await this.ensure(s),s.ledger=!0)};if(a){if(a.version.digest===n)return await l(),await a.record(),{state:"adopted",version:a.version.n,adoption:a.adoption};o=a.version}let c=a?.adoption,i=await this.planFrom(s,o,e);if(i.refusals.length>0)throw new Fe(i.refusals);await l(),await a?.record(),await this.tx(s,["ALTER TABLE palgres.migration ADD COLUMN IF NOT EXISTS opened_by text",`INSERT INTO palgres.versions (n, digest, schema, layout, state, direct) VALUES (${i.to}, ${F(n)}, ${F(JSON.stringify(is(e)))}::jsonb, ${F(JSON.stringify(i.layout))}::jsonb, 'starting', ${Js(i.layout)})`,`INSERT INTO palgres.migration (from_n, to_n, plan, phase, opened_by) VALUES (${i.from??"NULL"}, ${i.to}, ${F(JSON.stringify(i))}::jsonb, 'starting', ${F(this.openedBy)})`]),this.log(`palgres: version ${i.from??"none"} \u2192 ${i.to}:
239
- ${i.changes.join(`
240
- `)||"(no change but the views)"}`);let d={from_n:i.from,to_n:i.to,plan:i,done:{},phase:"starting",opened_by:this.openedBy};return await this.runStart(s,d),i.from===null?(await this.runComplete(s,{...d,phase:"started"}),{state:"started",from:null,to:i.to,changes:i.changes,completed:!0}):{state:"started",from:i.from,to:i.to,changes:i.changes,completed:!1,...c?{adoption:c}:{}}})}async complete(){return this.session(async e=>{let n=await this.open(e);if(!n)return{state:"none"};if(n.phase==="starting"||n.phase==="rolling_back")throw new Error(`palgres: version ${n.to_n} is ${n.phase==="starting"?"still starting: run start again to finish it":"being rolled back: run rollback again"}`);return n.from_n!==null&&await this.waitGone(e,n.from_n),await this.runComplete(e,n),{state:"completed",version:n.to_n}})}async rollback(){return this.session(async e=>{let n=await this.open(e);if(!n)return{state:"none"};if(n.phase==="completing")throw new Error(`palgres: version ${n.to_n} is completing: what the old version used is going; run complete again`);return await this.waitGone(e,n.to_n),n.phase!=="rolling_back"&&(await this.tx(e,["UPDATE palgres.migration SET phase = 'rolling_back', updated_at = now()"]),n.phase="rolling_back"),await this.runSteps(e,n,"rollback",[`UPDATE palgres.versions SET state = 'abandoned', retired_at = now() WHERE n = ${n.to_n}`,"DELETE FROM palgres.migration"]),this.log(`palgres: version ${n.to_n} rolled back; ${n.from_n??"no version"} serves`),{state:"rolled-back",version:n.from_n}})}async runStart(e,n){await this.runSteps(e,n,"start",[`UPDATE palgres.versions SET state = 'active', ready_at = now() WHERE n = ${n.to_n}`,"UPDATE palgres.migration SET phase = 'started', updated_at = now()"])}async runComplete(e,n){n.phase!=="completing"&&(await this.tx(e,["UPDATE palgres.migration SET phase = 'completing', updated_at = now()"]),n.phase="completing"),await this.runSteps(e,n,"complete",[...n.from_n!==null?[`UPDATE palgres.versions SET state = 'retired', retired_at = now() WHERE n = ${n.from_n}`]:[],`UPDATE palgres.versions SET direct = true WHERE n = ${n.to_n}`,"DELETE FROM palgres.migration"]),this.log(`palgres: version ${n.to_n} serves alone`)}async runSteps(e,n,s,r){let o=n.plan[s],a=n.done[s]??0;for(let l=a;l<o.length;l++){let c=o[l],i=`UPDATE palgres.migration SET done = done || ${F(JSON.stringify({[s]:l+1}))}::jsonb, updated_at = now()`,d=l===o.length-1;if(this.log(`palgres: ${s} ${l+1}/${o.length}: ${c.why}`),c.kind==="tx"){await this.tx(e,[...c.sql,...d?r:[i]]);continue}c.kind==="statement"?await this.statement(e,c.sql):await this.backfill(e,n,l,c),await this.tx(e,d?r:[i])}o.length===0&&await this.tx(e,r)}async tx(e,n){let s=n.every(d=>/^(?:UPDATE|INSERT\s+INTO|DELETE\s+FROM)\s+palgres\./i.test(d)),{needs:r,made:o}=s?{needs:[],made:[]}:Bs(n),a=await Ks(e.query,r),{holding:l,statementTimeout:c}=await this.settings(e),i=0;await this.retrying(e,async d=>{await e.query("BEGIN");try{let u=this.lockTimeoutFor(d);await e.query(`SET LOCAL lock_timeout = ${u}`);let f=Date.now()+l,g=p=>e.query(`SELECT set_config('lock_timeout', '${Math.max(1,Math.floor(p))}', true)`);if(!s&&a.length>0){let p=Date.now();f=p+l;let y=[a[i],...a.filter((h,m)=>m!==i)],S=new Set;for(let[h,m]of y.entries()){let T=Math.max(1,Math.floor(f-Date.now())),E=h>0&&(f-Date.now()<1||S.has(m.oid));h>0&&await e.query(`SELECT set_config('lock_timeout', '${T}', true), set_config('statement_timeout', '${T}', true)`);try{await e.query(`LOCK TABLE ${m.name} IN ${m.mode} MODE${E?" NOWAIT":""}`)}catch(w){let A=ae(w);throw(A==="55P03"||A==="57014")&&(i=a.indexOf(m),this.log(`palgres: ${m.name} is held by others (${m.mode} not had${E?" at once":` in ${h===0?u:T} ms`}): the step gives way`)),A==="57014"?new un(m.name,w):w}h===0&&y.length>1&&(S=await Hs(e.query,y.slice(1),Date.now()-p))}await e.query(`SET LOCAL statement_timeout = ${F(c)}`)}for(let[p,y]of n.entries())!s&&a.length===0&&p===0?f=Date.now()+l:s||await g(f-Date.now()),await e.query(y);s||await this.notTakenFirst(e,a,o),await e.query("COMMIT")}catch(u){throw await e.query("ROLLBACK").catch(()=>{}),u}})}async settings(e){if(e.deadlockMs===void 0||e.statementTimeout===void 0){let[n]=await e.query("SELECT (SELECT setting::int FROM pg_catalog.pg_settings WHERE name = 'deadlock_timeout') AS ms, current_setting('statement_timeout') AS st");e.deadlockMs=Number(n?.ms??1e3),e.statementTimeout=String(n?.st??"0")}return{holding:Math.max(1,Math.min(this.lockTimeoutMs,Math.floor(e.deadlockMs/2))),statementTimeout:e.statementTimeout}}async notTakenFirst(e,n,s){for(let r of await Vs(e.query,s)){let o=n.find(a=>a.oid===r.oid);o&&ke[o.mode]>=ke[r.mode]||this.log(`palgres: ${r.name} was locked ${r.mode} by a statement, ${o?`more strongly than the step took it first (${o.mode})`:"not taken first"}`)}}async statement(e,n){let s=/^CREATE (?:UNIQUE )?INDEX CONCURRENTLY IF NOT EXISTS ("(?:[^"]|"")+") ON /.exec(n);await this.retrying(e,async()=>{if(s){let r=await e.query(`SELECT n.nspname AS schema FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = ${F(Ys(s[1]))} AND NOT i.indisvalid`);for(let o of r)await e.query(`DROP INDEX CONCURRENTLY IF EXISTS ${P(String(o.schema),Ys(s[1]))}`)}await e.query(`SET lock_timeout = ${this.statementWaitMs}`);try{await e.query(n)}finally{await e.query("RESET lock_timeout")}})}async backfill(e,n,s,r){let{holding:o}=await this.settings(e),a=r.key.length>0,l=n.done.cursor?.step===s?n.done.cursor:void 0,c=l?.last??(a?null:0),i=l?.inclusive??!1,d=a?0:await this.pages(e,r.table),u=a?await this.highest(e,r):null,f=!1,g=l?.skippedFrom??null,p=g===null?[]:null,y=null,S=0,h=0,m=0;for(;;){let T=y?.splice(0,this.batchSize),E=T!==void 0,w=E?T.length===0:f?!1:a?u===null:Number(c)>=d,A;if(w||await this.retrying(e,async()=>{await e.query("BEGIN");try{await e.query(`SET LOCAL lock_timeout = ${o}`),await e.query("SET LOCAL statement_timeout = 30000");let R=(await e.query(a?this.keyedBatch(r,T??null,c,i,u,f):this.pageBatch(r,T??null,f?d:Number(c),f)))[0],b=R.left??[],O=E?!0:a?Number(R.seen)>0:!f,L={gone:Number(R.gone),seen:O,last:E||f&&!a?c:a?R.last??c:Number(c)+this.pagesPerBatch(),left:b},$=g??(b.length>0?a?b[0]:!0:y||T?a?T[0]??null:!0:null);await e.query(`UPDATE palgres.migration SET done = done || ${F(JSON.stringify({cursor:{step:s,last:L.last,inclusive:T?i:!1,skippedFrom:$}}))}::jsonb, updated_at = now()`),await e.query("COMMIT"),A=L}catch(R){throw await e.query("ROLLBACK").catch(()=>{}),R}}),A&&(S+=A.gone,h+=A.gone,E||(c=A.last,i=!1),A.left.length>0&&(g??=a?A.left[0]:!0,p!==null&&(p=p.length+A.left.length>kt?null:[...p,...A.left])),A.seen))continue;if(!E&&!f){f=!0;continue}if(g===null)break;if(h>0)m=0;else if(++m>=this.attempts)throw new pn(r.table,m);let k=Math.min(250*2**Math.max(0,m-1),5e3);this.log(`palgres: ${p===null?"rows":`${p.length} row(s)`} of ${r.table} held by writers; gone over again in ${k} ms`),await new Promise(R=>setTimeout(R,k)),p!==null?y=p:a?(y=null,c=g,i=!0,u=await this.highest(e,r)):(y=null,c=0,d=await this.pages(e,r.table)),g=null,p=[],h=0,f=!1}this.log(`palgres: ${S} row(s)`)}async highest(e,n){let s=n.key.map(N).join(", ");return(await e.query(`SELECT (SELECT to_jsonb(x) FROM (SELECT ${s} FROM ${n.table} ORDER BY ${s} DESC LIMIT 1) x) AS k`))[0].k??null}pagesPerBatch(){return Math.max(1,Math.ceil(this.batchSize/50))}async pages(e,n){return Number((await e.query(`SELECT (pg_catalog.pg_relation_size(${F(n)}::regclass) / current_setting('block_size')::int)::int AS n`))[0].n)}keyedBatch(e,n,s,r,o,a){let l="_palgres_t",c=e.key.map(N).join(", "),i=e.key.map(h=>`${l}.${N(h)}`).join(", "),d=e.key.map((h,m)=>N(`_palgres_k${m}`)),u=e.key.map((h,m)=>`${l}.${N(h)} AS ${d[m]}`).join(", "),f=h=>e.key.map((m,T)=>`${h}.${d[T]} AS ${N(m)}`).join(", "),g=h=>`(SELECT ${c} FROM jsonb_populate_record(NULL::${e.table}, ${F(JSON.stringify(h))}::jsonb))`,p=s===null?"true":`(${i}) ${r?">=":">"} ${g(s)}`,y=n!==null?`(${i}) IN (SELECT ${c} FROM jsonb_populate_recordset(NULL::${e.table}, ${F(JSON.stringify(n))}::jsonb))`:a?`${p} AND (${e.where})`:`${p} AND (${i}) <= ${g(o)}`,S=n!==null?y:`${p} AND (${i}) <= (SELECT ${d.join(", ")} FROM edge)`;return`WITH win AS (SELECT ${u} FROM ${e.table} ${l} WHERE ${y} ORDER BY ${i} LIMIT ${this.batchSize}),
241
- edge AS (SELECT * FROM win ORDER BY ${d.join(" DESC, ")} DESC LIMIT 1),
242
- batch AS (SELECT ${u} FROM ${e.table} ${l} WHERE ${S} AND (${e.where}) FOR NO KEY UPDATE OF ${l} SKIP LOCKED),
243
- done AS (UPDATE ${e.table} SET ${e.set} FROM batch WHERE (${e.key.map(h=>`${e.table}.${N(h)}`).join(", ")}) = (${d.map(h=>`batch.${h}`).join(", ")}) AND (${e.where}) RETURNING NOT (${e.where}) AS gone),
244
- left_ AS (SELECT ${u} FROM ${e.table} ${l} WHERE ${S} AND (${e.where}) AND NOT EXISTS (SELECT 1 FROM batch b WHERE (${d.map(h=>`b.${h}`).join(", ")}) = (${i})))
238
+ - `)}`),this.name="MigrationRefused",this.refusals=e}},pn=class extends Error{from;to;openedBy;constructor(e,n,s=null){super(`palgres: the migration from version ${e??"none"} to ${n}${s?`, opened by ${s},`:""} is still open: complete it or roll it back first`),this.name="MigrationOpen",this.from=e,this.to=n,this.openedBy=s}},fn=class extends Error{version;constructor(e,n,s){super(`palgres: ${n} connection(s) of version ${e} are still open after ${s} ms`),this.name="VersionStillConnected",this.version=e}},Ct=1e4,er=`CREATE OR REPLACE FUNCTION palgres.defer_keys(keys text[], deferred boolean) RETURNS void
239
+ LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp AS $palgres$
240
+ DECLARE
241
+ named text;
242
+ BEGIN
243
+ SELECT string_agg(format('%I.%I', n.nspname, c.conname), ', ' ORDER BY n.nspname, c.conname) INTO named
244
+ FROM pg_catalog.pg_constraint c JOIN pg_catalog.pg_namespace n ON n.oid = c.connamespace
245
+ WHERE c.contype = 'f' AND c.condeferrable AND n.nspname || '.' || c.conname = ANY (keys);
246
+ IF named IS NOT NULL THEN
247
+ EXECUTE 'SET CONSTRAINTS ' || named || CASE WHEN deferred THEN ' DEFERRED' ELSE ' IMMEDIATE' END;
248
+ END IF;
249
+ END $palgres$`,mn=class extends Error{constructor(e,n){super(`palgres: ${e} was not had in the step's time`,{cause:n}),this.name="LockNotHad"}},gn=class extends Error{constructor(e,n){super(`palgres: rows of ${e} to fill were held by writers through ${n} passes`),this.name="RowsHeld"}},Ho=new Set(["55P03","40P01","40001","57014","25006"]);function Vo(t){if(ue(t)!==void 0)return!0;let e=t;for(let n=0;n<4&&typeof e=="object"&&e!==null;n++){if(e instanceof fn||e instanceof gn)return!0;let s=ae(e);if(s!==void 0)return Ho.has(s)||s.startsWith("53");e=e.cause}return!1}var Je={anon:"anon",authenticated:"authenticated",service:"service_role"},Qn=t=>`palgres:v${t}`,nr="palgres.migrate",Dt=class t{pool;roles;openedBy;lockTimeoutMs;statementWaitMs;lockTimeoutFor(e){return e>=3?Math.max(this.lockTimeoutMs,1500):this.lockTimeoutMs}attempts;batchSize;disconnectTimeoutMs;log;shadow;constructor(e,n={}){this.pool=e,this.roles={...Je,...n.roles},this.lockTimeoutMs=n.lockTimeoutMs??500,this.statementWaitMs=n.statementWaitMs??6e5,this.attempts=n.attempts??10,this.batchSize=n.batchSize??1e3,this.disconnectTimeoutMs=n.disconnectTimeoutMs??6e4,this.log=n.log??(()=>{}),this.shadow=n.shadow,this.openedBy=n.openedBy??"deploy"}async status(){return this.session(async e=>{let n=await this.active(e),s=await this.open(e);return{active:n&&{n:n.n,digest:n.digest,state:"active"},open:s&&{from:s.from_n,to:s.to_n,phase:s.phase,opened_by:s.opened_by}}},!1)}async plan(e){tr(e);let n=Pe(e),s=async(r,o)=>{let a;try{a=await this.adopt(r,e)}catch(l){if(!(l instanceof $e))throw l;return{from:null,to:1,changes:[],refusals:l.refusals,start:[],complete:[],rollback:[],layout:{tables:[]}}}let i=o?[]:await this.authRefusals(r);if(!a){let l=await this.planFrom(r,null,e);return{...l,refusals:[...i,...l.refusals]}}if(a.version.digest===n)return{from:null,to:a.version.n,changes:[],refusals:i,start:[],complete:[],rollback:[],layout:a.layout,adoption:a.adoption};let c=await this.planFrom(r,a.version,e);return{...c,refusals:[...i,...c.refusals],adoption:a.adoption}};return this.session(async r=>{let o=await this.active(r);return o?this.planFrom(r,o,e):s(r,!0)},!1,r=>s(r,!1))}async start(e){tr(e);let n=Pe(e);return this.session(async s=>{let r=await this.open(s);if(r){if((await this.version(s,r.to_n))?.digest!==n)throw new pn(r.from_n,r.to_n,r.opened_by);if(r.phase==="started")return{state:"resumed",from:r.from_n,to:r.to_n,changes:r.plan.changes,completed:!1};if(r.phase!=="starting")throw new pn(r.from_n,r.to_n);return this.log(`palgres: resuming the start of version ${r.to_n}`),await this.runStart(s,r),{state:"resumed",from:r.from_n,to:r.to_n,changes:r.plan.changes,completed:!1}}let o=await this.active(s);if(o?.digest===n)return{state:"in-sync",version:o.n};let a=o?null:await this.adopt(s,e),i=async()=>{s.ledger===!1&&(await this.ownAuth(s),await this.ensure(s),s.ledger=!0)};if(a){if(a.version.digest===n)return await i(),await a.record(),{state:"adopted",version:a.version.n,adoption:a.adoption};o=a.version}let c=a?.adoption,l=await this.planFrom(s,o,e);if(l.refusals.length>0)throw new $e(l.refusals);await i(),await a?.record(),await this.tx(s,["ALTER TABLE palgres.migration ADD COLUMN IF NOT EXISTS opened_by text",er,`INSERT INTO palgres.versions (n, digest, schema, layout, state, direct) VALUES (${l.to}, ${M(n)}, ${M(JSON.stringify(fs(e)))}::jsonb, ${M(JSON.stringify(l.layout))}::jsonb, 'starting', ${rr(l.layout)})`,`INSERT INTO palgres.migration (from_n, to_n, plan, phase, opened_by) VALUES (${l.from??"NULL"}, ${l.to}, ${M(JSON.stringify(l))}::jsonb, 'starting', ${M(this.openedBy)})`]),this.log(`palgres: version ${l.from??"none"} \u2192 ${l.to}:
250
+ ${l.changes.join(`
251
+ `)||"(no change but the views)"}`);let u={from_n:l.from,to_n:l.to,plan:l,done:{},phase:"starting",opened_by:this.openedBy};return await this.runStart(s,u),l.from===null?(await this.runComplete(s,{...u,phase:"started"}),{state:"started",from:null,to:l.to,changes:l.changes,completed:!0}):{state:"started",from:l.from,to:l.to,changes:l.changes,completed:!1,...c?{adoption:c}:{}}})}async complete(){return this.session(async e=>{let n=await this.open(e);if(!n)return{state:"none"};if(n.phase==="starting"||n.phase==="rolling_back")throw new Error(`palgres: version ${n.to_n} is ${n.phase==="starting"?"still starting: run start again to finish it":"being rolled back: run rollback again"}`);return n.from_n!==null&&await this.waitGone(e,n.from_n),await this.runComplete(e,n),{state:"completed",version:n.to_n}})}async rollback(){return this.session(async e=>{let n=await this.open(e);if(!n)return{state:"none"};if(n.phase==="completing")throw new Error(`palgres: version ${n.to_n} is completing: what the old version used is going; run complete again`);return await this.waitGone(e,n.to_n),n.phase!=="rolling_back"&&(await this.tx(e,["UPDATE palgres.migration SET phase = 'rolling_back', updated_at = now()"]),n.phase="rolling_back"),await this.runSteps(e,n,"rollback",[`UPDATE palgres.versions SET state = 'abandoned', retired_at = now() WHERE n = ${n.to_n}`,"DELETE FROM palgres.migration"]),this.log(`palgres: version ${n.to_n} rolled back; ${n.from_n??"no version"} serves`),{state:"rolled-back",version:n.from_n}})}async runStart(e,n){await this.runSteps(e,n,"start",[`UPDATE palgres.versions SET state = 'active', ready_at = now() WHERE n = ${n.to_n}`,"UPDATE palgres.migration SET phase = 'started', updated_at = now()"])}async runComplete(e,n){n.phase!=="completing"&&(await this.tx(e,["UPDATE palgres.migration SET phase = 'completing', updated_at = now()"]),n.phase="completing"),await this.runSteps(e,n,"complete",[...n.from_n!==null?[`UPDATE palgres.versions SET state = 'retired', retired_at = now() WHERE n = ${n.from_n}`]:[],`UPDATE palgres.versions SET direct = true WHERE n = ${n.to_n}`,"DELETE FROM palgres.migration"]),this.log(`palgres: version ${n.to_n} serves alone`)}async runSteps(e,n,s,r){let o=n.plan[s],a=n.done[s]??0;for(let i=a;i<o.length;i++){let c=o[i],l=`UPDATE palgres.migration SET done = done || ${M(JSON.stringify({[s]:i+1}))}::jsonb, updated_at = now()`,u=i===o.length-1;if(this.log(`palgres: ${s} ${i+1}/${o.length}: ${c.why}`),c.kind==="tx"){await this.tx(e,[...c.sql,...u?r:[l]]);continue}if(c.kind==="statement")await this.statement(e,c.sql);else if(c.kind==="backfill")await this.backfill(e,n,i,c);else throw new $e([`version ${n.to_n}'s migration has a step of a kind this Palgres does not run (${JSON.stringify(c.kind)})`]);await this.tx(e,u?r:[l])}o.length===0&&await this.tx(e,r)}async tx(e,n){let s=n.every(u=>/^(?:UPDATE|INSERT\s+INTO|DELETE\s+FROM)\s+palgres\./i.test(u)),{needs:r,made:o}=s?{needs:[],made:[]}:Xs(n),a=await zs(e.query,r),{holding:i,statementTimeout:c}=await this.settings(e),l=0;await this.retrying(e,async u=>{await e.query("BEGIN");try{let g=this.lockTimeoutFor(u);await e.query(`SET LOCAL lock_timeout = ${g}`);let p=Date.now()+i,h=m=>e.query(`SELECT set_config('lock_timeout', '${Math.max(1,Math.floor(m))}', true)`);if(!s&&a.length>0){let m=Date.now();p=m+i;let y=[a[l],...a.filter((f,d)=>d!==l)],S=new Set;for(let[f,d]of y.entries()){let T=Math.max(1,Math.floor(p-Date.now())),E=f>0&&(p-Date.now()<1||S.has(d.oid));f>0&&await e.query(`SELECT set_config('lock_timeout', '${T}', true), set_config('statement_timeout', '${T}', true)`);try{await e.query(`LOCK TABLE ${d.name} IN ${d.mode} MODE${E?" NOWAIT":""}`)}catch(w){let O=ae(w);throw(O==="55P03"||O==="57014")&&(l=a.indexOf(d),this.log(`palgres: ${d.name} is held by others (${d.mode} not had${E?" at once":` in ${f===0?g:T} ms`}): the step gives way`)),O==="57014"?new mn(d.name,w):w}f===0&&y.length>1&&(S=await Qs(e.query,y.slice(1),Date.now()-m))}await e.query(`SET LOCAL statement_timeout = ${M(c)}`)}for(let[m,y]of n.entries())!s&&a.length===0&&m===0?p=Date.now()+i:s||await h(p-Date.now()),await e.query(y);s||await this.notTakenFirst(e,a,o),await e.query("COMMIT")}catch(g){throw await e.query("ROLLBACK").catch(()=>{}),g}})}async settings(e){if(e.deadlockMs===void 0||e.statementTimeout===void 0){let[n]=await e.query("SELECT (SELECT setting::int FROM pg_catalog.pg_settings WHERE name = 'deadlock_timeout') AS ms, current_setting('statement_timeout') AS st");e.deadlockMs=Number(n?.ms??1e3),e.statementTimeout=String(n?.st??"0")}return{holding:Math.max(1,Math.min(this.lockTimeoutMs,Math.floor(e.deadlockMs/2))),statementTimeout:e.statementTimeout}}async notTakenFirst(e,n,s){for(let r of await Zs(e.query,s)){let o=n.find(a=>a.oid===r.oid);o&&Le[o.mode]>=Le[r.mode]||this.log(`palgres: ${r.name} was locked ${r.mode} by a statement, ${o?`more strongly than the step took it first (${o.mode})`:"not taken first"}`)}}async statement(e,n){let s=/^CREATE (?:UNIQUE )?INDEX CONCURRENTLY IF NOT EXISTS ("(?:[^"]|"")+") ON /.exec(n);await this.retrying(e,async()=>{if(s){let r=await e.query(`SELECT n.nspname AS schema FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = ${M(sr(s[1]))} AND NOT i.indisvalid`);for(let o of r)await e.query(`DROP INDEX CONCURRENTLY IF EXISTS ${P(String(o.schema),sr(s[1]))}`)}await e.query(`SET lock_timeout = ${this.statementWaitMs}`);try{await e.query(n)}finally{await e.query("RESET lock_timeout")}})}async backfill(e,n,s,r){let{holding:o}=await this.settings(e),a=r.key.length>0,i=n.done.cursor?.step===s?n.done.cursor:void 0,c=i?.last??(a?null:0),l=i?.inclusive??!1,u=a?0:await this.pages(e,r.table),g=a?await this.highest(e,r):null,p=!1,h=i?.skippedFrom??null,m=h===null?[]:null,y=null,S=0,f=0,d=0;for(;;){let T=y?.splice(0,this.batchSize),E=T!==void 0,w=E?T.length===0:p?!1:a?g===null:Number(c)>=u,O;if(w||await this.retrying(e,async()=>{await e.query("BEGIN");try{await e.query(`SET LOCAL lock_timeout = ${o}`),await e.query("SET LOCAL statement_timeout = 30000");let N=(await e.query(a?this.keyedBatch(r,T??null,c,l,g,p):this.pageBatch(r,T??null,p?u:Number(c),p)))[0],A=N.left??[],R=E?!0:a?Number(N.seen)>0:!p,L={gone:Number(N.gone),seen:R,last:E||p&&!a?c:a?N.last??c:Number(c)+this.pagesPerBatch(),left:A},$=h??(A.length>0?a?A[0]:!0:y||T?a?T[0]??null:!0:null);await e.query(`UPDATE palgres.migration SET done = done || ${M(JSON.stringify({cursor:{step:s,last:L.last,inclusive:T?l:!1,skippedFrom:$}}))}::jsonb, updated_at = now()`),await e.query("COMMIT"),O=L}catch(N){throw await e.query("ROLLBACK").catch(()=>{}),N}}),O&&(S+=O.gone,f+=O.gone,E||(c=O.last,l=!1),O.left.length>0&&(h??=a?O.left[0]:!0,m!==null&&(m=m.length+O.left.length>Ct?null:[...m,...O.left])),O.seen))continue;if(!E&&!p){p=!0;continue}if(h===null)break;if(f>0)d=0;else if(++d>=this.attempts)throw new gn(r.table,d);let k=Math.min(250*2**Math.max(0,d-1),5e3);this.log(`palgres: ${m===null?"rows":`${m.length} row(s)`} of ${r.table} held by writers; gone over again in ${k} ms`),await new Promise(N=>setTimeout(N,k)),m!==null?y=m:a?(y=null,c=h,l=!0,g=await this.highest(e,r)):(y=null,c=0,u=await this.pages(e,r.table)),h=null,m=[],f=0,p=!1}this.log(`palgres: ${S} row(s)`)}async highest(e,n){let s=n.key.map(b).join(", ");return(await e.query(`SELECT (SELECT to_jsonb(x) FROM (SELECT ${s} FROM ${n.table} ORDER BY ${s} DESC LIMIT 1) x) AS k`))[0].k??null}pagesPerBatch(){return Math.max(1,Math.ceil(this.batchSize/50))}async pages(e,n){return Number((await e.query(`SELECT (pg_catalog.pg_relation_size(${M(n)}::regclass) / current_setting('block_size')::int)::int AS n`))[0].n)}keyedBatch(e,n,s,r,o,a){let i="_palgres_t",c=e.key.map(b).join(", "),l=e.key.map(f=>`${i}.${b(f)}`).join(", "),u=e.key.map((f,d)=>b(`_palgres_k${d}`)),g=e.key.map((f,d)=>`${i}.${b(f)} AS ${u[d]}`).join(", "),p=f=>e.key.map((d,T)=>`${f}.${u[T]} AS ${b(d)}`).join(", "),h=f=>`(SELECT ${c} FROM jsonb_populate_record(NULL::${e.table}, ${M(JSON.stringify(f))}::jsonb))`,m=s===null?"true":`(${l}) ${r?">=":">"} ${h(s)}`,y=n!==null?`(${l}) IN (SELECT ${c} FROM jsonb_populate_recordset(NULL::${e.table}, ${M(JSON.stringify(n))}::jsonb))`:a?`${m} AND (${e.where})`:`${m} AND (${l}) <= ${h(o)}`,S=n!==null?y:`${m} AND (${l}) <= (SELECT ${u.join(", ")} FROM edge)`;return`WITH win AS (SELECT ${g} FROM ${e.table} ${i} WHERE ${y} ORDER BY ${l} LIMIT ${this.batchSize}),
252
+ edge AS (SELECT * FROM win ORDER BY ${u.join(" DESC, ")} DESC LIMIT 1),
253
+ batch AS (SELECT ${g} FROM ${e.table} ${i} WHERE ${S} AND (${e.where}) FOR NO KEY UPDATE OF ${i} SKIP LOCKED),
254
+ done AS (UPDATE ${e.table} SET ${e.set} FROM batch WHERE (${e.key.map(f=>`${e.table}.${b(f)}`).join(", ")}) = (${u.map(f=>`batch.${f}`).join(", ")}) AND (${e.where}) RETURNING NOT (${e.where}) AS gone),
255
+ left_ AS (SELECT ${g} FROM ${e.table} ${i} WHERE ${S} AND (${e.where}) AND NOT EXISTS (SELECT 1 FROM batch b WHERE (${u.map(f=>`b.${f}`).join(", ")}) = (${l})))
245
256
  SELECT (SELECT count(*) FROM win)::int AS seen, (SELECT count(*) FROM done WHERE gone)::int AS gone,
246
- (SELECT to_jsonb(x) FROM (SELECT ${f("edge")} FROM edge) x) AS last,
247
- (SELECT jsonb_agg(to_jsonb(x)) FROM (SELECT ${f("left_")} FROM left_ ORDER BY ${d.join(", ")} LIMIT ${kt+1}) x) AS left`}pageBatch(e,n,s,r){let o=n!==null?`ctid = ANY (${F(`{${n.map(a=>`"${String(a)}"`).join(",")}}`)}::tid[])`:r?`ctid >= '(${s},0)'::tid`:`ctid >= '(${s},0)'::tid AND ctid < '(${s+this.pagesPerBatch()},0)'::tid`;return`WITH batch AS (SELECT ctid AS c FROM ${e.table} WHERE ${o} AND (${e.where}) FOR NO KEY UPDATE SKIP LOCKED),
257
+ (SELECT to_jsonb(x) FROM (SELECT ${p("edge")} FROM edge) x) AS last,
258
+ (SELECT jsonb_agg(to_jsonb(x)) FROM (SELECT ${p("left_")} FROM left_ ORDER BY ${u.join(", ")} LIMIT ${Ct+1}) x) AS left`}pageBatch(e,n,s,r){let o=n!==null?`ctid = ANY (${M(`{${n.map(a=>`"${String(a)}"`).join(",")}}`)}::tid[])`:r?`ctid >= '(${s},0)'::tid`:`ctid >= '(${s},0)'::tid AND ctid < '(${s+this.pagesPerBatch()},0)'::tid`;return`WITH batch AS (SELECT ctid AS c FROM ${e.table} WHERE ${o} AND (${e.where}) FOR NO KEY UPDATE SKIP LOCKED),
248
259
  done AS (UPDATE ${e.table} SET ${e.set} WHERE ctid = ANY (ARRAY(SELECT c FROM batch)) AND (${e.where}) RETURNING NOT (${e.where}) AS gone),
249
260
  left_ AS (SELECT ctid AS c FROM ${e.table} WHERE ${o} AND (${e.where}) AND ctid <> ALL (ARRAY(SELECT c FROM batch)))
250
- SELECT (SELECT count(*) FROM done WHERE gone)::int AS gone, (SELECT array_agg(c::text) FROM (SELECT c FROM left_ LIMIT ${kt+1}) x) AS left`}async retrying(e,n){for(let s=1;;s++)try{await n(s);return}catch(r){let o=r instanceof un?"57014":ae(r);if(o!=="55P03"&&o!=="40P01"&&!(r instanceof un)||s>=this.attempts)throw r;let a=Math.min(250*2**(s-1),5e3);this.log(`palgres: a lock was busy (${o}); again in ${a} ms`),await new Promise(l=>setTimeout(l,a))}}async waitGone(e,n){let s=Date.now()+this.disconnectTimeoutMs;for(;;){let r=await e.query(`SELECT count(*)::int AS n FROM pg_stat_activity WHERE application_name = ${F(Gn(n))} AND datname = current_database() AND pid <> pg_backend_pid()`),o=Number(r[0].n);if(o===0)return;if(Date.now()>=s)throw new mn(n,o,this.disconnectTimeoutMs);await new Promise(a=>setTimeout(a,500))}}async adopt(e,n){let s=[...new Set([...n.tables.map(g=>g.schema),...n.raw.flatMap(g=>g.schema?[g.schema]:[])])],r=await Kn(this.pool,s,this.roles);if(!n.tables.some(g=>r.schema.tables.some(p=>p.schema===g.schema&&(p.name===g.name||p.name===g.renamedFrom))))return null;if(!this.shadow)throw new Error("palgres: the database holds tables of the schema and no Palgres version: taking it over needs a shadow database (MigratorOptions.shadow) to read the schema as Postgres writes it");let a=await this.made(e,n,s,r),l=At(r,a,n);if(l.refusals.length>0)throw new Fe(l.refusals);let c=[this.roles.anon,this.roles.authenticated,this.roles.service].map(N).join(", "),i={tables:l.schema.tables.filter(g=>!g.ignored).map(g=>({schema:g.schema,name:g.name,physical:g.name,columns:g.columns.filter(p=>!p.ignored).map(p=>({name:p.name,physical:p.name}))}))},d=l.schema.tables.map(g=>P(g.schema,g.name)),u=De(l.schema),f=async()=>{await this.tx(e,[...l.unforce.map(g=>`ALTER TABLE ${P(g.slice(0,g.indexOf(".")),g.slice(g.indexOf(".")+1))} NO FORCE ROW LEVEL SECURITY`),...[...new Set(l.schema.tables.map(g=>g.schema))].map(g=>`GRANT USAGE ON SCHEMA ${N(g)} TO ${c}`),...d.map(g=>`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ${g} TO ${c}`),`DO $palgres$ DECLARE s regclass; BEGIN
261
+ SELECT (SELECT count(*) FROM done WHERE gone)::int AS gone, (SELECT array_agg(c::text) FROM (SELECT c FROM left_ LIMIT ${Ct+1}) x) AS left`}async retrying(e,n){for(let s=1;;s++)try{await n(s);return}catch(r){let o=r instanceof mn?"57014":ae(r);if(o!=="55P03"&&o!=="40P01"&&!(r instanceof mn)||s>=this.attempts)throw r;let a=Math.min(250*2**(s-1),5e3);this.log(`palgres: a lock was busy (${o}); again in ${a} ms`),await new Promise(i=>setTimeout(i,a))}}async waitGone(e,n){let s=Date.now()+this.disconnectTimeoutMs;for(;;){let r=await e.query(`SELECT count(*)::int AS n FROM pg_stat_activity WHERE application_name = ${M(Qn(n))} AND datname = current_database() AND pid <> pg_backend_pid()`),o=Number(r[0].n);if(o===0)return;if(Date.now()>=s)throw new fn(n,o,this.disconnectTimeoutMs);await new Promise(a=>setTimeout(a,500))}}async adopt(e,n){let s=[...new Set([...n.tables.map(h=>h.schema),...n.raw.flatMap(h=>h.schema?[h.schema]:[])])],r=await Yn(this.pool,s,this.roles);if(!n.tables.some(h=>r.schema.tables.some(m=>m.schema===h.schema&&(m.name===h.name||m.name===h.renamedFrom))))return null;if(!this.shadow)throw new Error("palgres: the database holds tables of the schema and no Palgres version: taking it over needs a shadow database (MigratorOptions.shadow) to read the schema as Postgres writes it");let a=await this.made(e,n,s,r),i=xt(r,a,n);if(i.refusals.length>0)throw new $e(i.refusals);let c=[this.roles.anon,this.roles.authenticated,this.roles.service].map(b).join(", "),l={tables:i.schema.tables.filter(h=>!h.ignored).map(h=>({schema:h.schema,name:h.name,physical:h.name,columns:h.columns.filter(m=>!m.ignored).map(m=>({name:m.name,physical:m.name}))}))},u=i.schema.tables.map(h=>P(h.schema,h.name)),g=Pe(i.schema),p=async()=>{await this.tx(e,[...i.unforce.map(h=>`ALTER TABLE ${P(h.slice(0,h.indexOf(".")),h.slice(h.indexOf(".")+1))} NO FORCE ROW LEVEL SECURITY`),...[...new Set(i.schema.tables.map(h=>h.schema))].map(h=>`GRANT USAGE ON SCHEMA ${b(h)} TO ${c}`),...u.map(h=>`GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE ${h} TO ${c}`),`DO $palgres$ DECLARE s regclass; BEGIN
251
262
  FOR s IN SELECT d.objid::regclass FROM pg_depend d JOIN pg_class c ON c.oid = d.objid AND c.relkind = 'S'
252
- WHERE d.deptype = 'a' AND d.refobjid = ANY (ARRAY[${d.length>0?d.map(g=>`${F(g)}::regclass`).join(", "):"NULL::regclass"}]::oid[]) LOOP
263
+ WHERE d.deptype = 'a' AND d.refobjid = ANY (ARRAY[${u.length>0?u.map(h=>`${M(h)}::regclass`).join(", "):"NULL::regclass"}]::oid[]) LOOP
253
264
  EXECUTE format('GRANT USAGE, SELECT ON SEQUENCE %s TO ${c.replace(/'/g,"''")}', s);
254
- END LOOP; END $palgres$`,`INSERT INTO palgres.versions (n, digest, schema, layout, state, direct, ready_at) VALUES (1, ${F(u)}, ${F(JSON.stringify(l.schema))}::jsonb, ${F(JSON.stringify(i))}::jsonb, 'active', true, now())`]),this.log(`palgres: takes the database over as version 1 (${l.schema.tables.length} table(s))${l.unforce.length>0?`; FORCE row-level security off on ${l.unforce.join(", ")}`:""}${l.unmanaged.length>0?`
265
+ END LOOP; END $palgres$`,`INSERT INTO palgres.versions (n, digest, schema, layout, state, direct, ready_at) VALUES (1, ${M(g)}, ${M(JSON.stringify(i.schema))}::jsonb, ${M(JSON.stringify(l))}::jsonb, 'active', true, now())`]),this.log(`palgres: takes the database over as version 1 (${i.schema.tables.length} table(s))${i.unforce.length>0?`; FORCE row-level security off on ${i.unforce.join(", ")}`:""}${i.unmanaged.length>0?`
255
266
  left as it is:
256
- ${l.unmanaged.join(`
257
- `)}`:""}`)};return{version:{n:1,digest:u,state:"active",schema:l.schema},adoption:{unforced:l.unforce,unmanaged:l.unmanaged},layout:i,record:f}}async made(e,n,s,r){let o=`palgres_shadow_${Mo(6).toString("hex")}`;await e.query(`CREATE DATABASE ${N(o)}`);try{let a=this.shadow(o);try{await new t(a,{roles:this.roles,lockTimeoutMs:this.lockTimeoutMs}).start(n);let l=await Kn(a,s,this.roles);return await Bo(a,r,l),l}finally{await a.end()}}finally{await e.query(`DROP DATABASE IF EXISTS ${N(o)} WITH (FORCE)`)}}async planFrom(e,n,s){let r=new Set;for(let h of n?.schema.tables??[])(await e.query(`SELECT EXISTS (SELECT 1 FROM ${P(h.schema,h.name)}) AS any`))[0].any&&r.add(D(h));let o=new Map,a=[],l=(h,m)=>{let T=`${h}\0${m}`;return o.has(T)||(o.set(T,void 0),a.push([h,m])),o.get(T)},i=(await e.query("SELECT to_regclass('palgres.versions') IS NOT NULL AS there"))[0]?.there===!0?Number((await e.query("SELECT coalesce(max(n), 0) AS n FROM palgres.versions"))[0].n):0,d=Math.max(i,n?.n??0)+1,u=new Map,f=h=>(u.has(h)||u.set(h,void 0),u.get(h)),g=new Map;if((await e.query(`SELECT to_regclass('${J}.generations') IS NOT NULL AS there`))[0]?.there===!0)for(let h of await e.query(`SELECT table_key, gen, model, dimensions, metric, from_columns, status FROM ${J}.generations ORDER BY table_key, gen`)){let m=String(h.table_key);g.has(m)||g.set(m,[]),g.get(m).push({gen:Number(h.gen),model:String(h.model),dimensions:Number(h.dimensions),metric:h.metric,from:h.from_columns.map(String),status:h.status})}let p=(await e.query("SELECT n.nspname AS schema FROM pg_catalog.pg_extension e JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace WHERE e.extname = 'vector'"))[0]?.schema,y=()=>bt({from:n&&{n:n.n,schema:n.schema},next:d,to:s,hasRows:h=>r.has(h),castable:l,checkedDomain:f,roles:this.roles,generations:h=>g.get(h)??[],vectorSchema:typeof p=="string"?p:"public"}),S=y();if(a.length===0&&u.size===0)return S;for(let[h,m]of a)o.set(`${h}\0${m}`,await this.casts(e,h,m));for(let h of u.keys())u.set(h,await this.checkedDomain(e,h));return y()}async checkedDomain(e,n){let s=await e.query(`SELECT t.typtype = 'd' AND (t.typnotnull OR EXISTS (SELECT 1 FROM pg_catalog.pg_constraint c WHERE c.contypid = t.oid)) AS checked FROM pg_catalog.pg_type t WHERE t.oid = pg_catalog.to_regtype(${F(n)})`);return s.length===0?void 0:s[0].checked===!0}async casts(e,n,s){try{return await e.query(`SELECT CAST(NULL::${n} AS ${s})`),!0}catch(r){let o=ae(r);if(o==="42846")return!1;if(o==="42704")return;throw r}}async active(e){if(e.ledger===!1)return null;let n=await e.query("SELECT n, digest, schema FROM palgres.versions WHERE state = 'active' ORDER BY n DESC LIMIT 1"),s=await this.open(e),o=(s?await e.query(`SELECT n, digest, schema FROM palgres.versions WHERE n = ${s.from_n??-1}`):n)[0];return o?{n:Number(o.n),digest:String(o.digest),state:"active",schema:Lt(o.schema)}:null}async version(e,n){if(e.ledger===!1)return null;let s=await e.query(`SELECT n, digest, state FROM palgres.versions WHERE n = ${n}`);return s[0]?{n,digest:String(s[0].digest),state:s[0].state}:null}async open(e){if(e.ledger===!1)return null;let s=(await e.query("SELECT from_n, to_n, plan, done, phase, to_jsonb(m) ->> 'opened_by' AS opened_by FROM palgres.migration m"))[0];return s?{from_n:s.from_n===null?null:Number(s.from_n),to_n:Number(s.to_n),plan:Lt(s.plan),done:Lt(s.done),phase:s.phase,opened_by:typeof s.opened_by=="string"?s.opened_by:null}:null}async session(e,n=!0,s){let r=await this.pool.reserve(),o={query:async a=>await r.unsafe(a)};try{if(await o.query("SET application_name = 'palgres:migrate'"),await o.query("SET search_path = public, pg_temp"),n){await o.query(`SELECT pg_advisory_lock(hashtextextended(${F(Gs)}, 0))`);let a=(await o.query(`SELECT to_regclass('palgres.versions') IS NOT NULL AS there, (SELECT pg_catalog.obj_description(n.oid, 'pg_namespace') FROM pg_catalog.pg_namespace n WHERE n.nspname = '${J}') AS search`))[0];o.ledger=a.there===!0,o.searchLedger=typeof a.search=="string"?a.search:null,o.ledger&&await this.ensure(o)}else if((await o.query("SELECT to_regclass('palgres.migration') IS NULL AS none"))[0].none)return await(s?s(o):e(Uo));return await e(o)}finally{n&&await o.query(`SELECT pg_advisory_unlock(hashtextextended(${F(Gs)}, 0))`).catch(()=>{}),r.release()}}async ownAuth(e){let n=await this.authRefusals(e);if(n.length>0)throw new Fe(n)}async authRefusals(e){let n={jwt:["SELECT coalesce(nullif(current_setting('request.jwt.claims', true), '')::jsonb, '{}'::jsonb)","SELECT coalesce(nullif(current_setting('request.jwt.claims', true),'')::jsonb, '{}'::jsonb)"],uid:["SELECT nullif(auth.jwt() ->> 'sub', '')","SELECT current_setting('request.jwt.claims', true)::jsonb ->> 'sub'","SELECT nullif(current_setting('request.jwt.claims', true)::jsonb ->> 'sub','')::text"],role:["SELECT nullif(auth.jwt() ->> 'role', '')","SELECT nullif(current_setting('request.jwt.claims', true)::jsonb ->> 'role','')::text"]},s=a=>a.replace(/\s+/g," ").trim();return(await e.query("SELECT p.proname AS name, p.prosrc AS src, pg_catalog.format_type(p.prorettype, NULL) AS returns FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace WHERE n.nspname = 'auth' AND p.proname IN ('jwt', 'uid', 'role') AND p.pronargs = 0")).filter(a=>!(n[String(a.name)]??[]).some(l=>s(l)===s(String(a.src)))).map(a=>`auth.${a.name}() is the database's own (returns ${a.returns}: ${s(String(a.src)).slice(0,120)}): Palgres makes it read the request's claims (request.jwt.claims), and the app there now would read its caller otherwise \u2014 rename yours, or make it read the claims as Palgres does, before the take-over`)}async ensure(e){let n=[this.roles.anon,this.roles.authenticated,this.roles.service],s=await e.query(`SELECT r FROM unnest(ARRAY[${n.map(F).join(", ")}]) AS r WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = r)`);if(s.length>0)throw new Error(`palgres: the database has no role ${s.map(o=>o.r).join(", ")} \u2014 the app's roles are made with the database`);let r=n.map(N).join(", ");await this.tx(e,["CREATE SCHEMA IF NOT EXISTS palgres",`CREATE TABLE IF NOT EXISTS palgres.versions (
267
+ ${i.unmanaged.join(`
268
+ `)}`:""}`)};return{version:{n:1,digest:g,state:"active",schema:i.schema},adoption:{unforced:i.unforce,unmanaged:i.unmanaged},layout:l,record:p}}async made(e,n,s,r){let o=`palgres_shadow_${Ko(6).toString("hex")}`;await e.query(`CREATE DATABASE ${b(o)}`);try{let a=this.shadow(o);try{await new t(a,{roles:this.roles,lockTimeoutMs:this.lockTimeoutMs}).start(n);let i=await Yn(a,s,this.roles);return await Yo(a,r,i),i}finally{await a.end()}}finally{await e.query(`DROP DATABASE IF EXISTS ${b(o)} WITH (FORCE)`)}}async planFrom(e,n,s){let r=new Set;for(let f of n?.schema.tables??[])(await e.query(`SELECT EXISTS (SELECT 1 FROM ${P(f.schema,f.name)}) AS any`))[0].any&&r.add(D(f));let o=new Map,a=[],i=(f,d)=>{let T=`${f}\0${d}`;return o.has(T)||(o.set(T,void 0),a.push([f,d])),o.get(T)},l=(await e.query("SELECT to_regclass('palgres.versions') IS NOT NULL AS there"))[0]?.there===!0?Number((await e.query("SELECT coalesce(max(n), 0) AS n FROM palgres.versions"))[0].n):0,u=Math.max(l,n?.n??0)+1,g=new Map,p=f=>(g.has(f)||g.set(f,void 0),g.get(f)),h=new Map;if((await e.query(`SELECT to_regclass('${J}.generations') IS NOT NULL AS there`))[0]?.there===!0)for(let f of await e.query(`SELECT table_key, gen, model, dimensions, metric, from_columns, status FROM ${J}.generations ORDER BY table_key, gen`)){let d=String(f.table_key);h.has(d)||h.set(d,[]),h.get(d).push({gen:Number(f.gen),model:String(f.model),dimensions:Number(f.dimensions),metric:f.metric,from:f.from_columns.map(String),status:f.status})}let m=(await e.query("SELECT n.nspname AS schema FROM pg_catalog.pg_extension e JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace WHERE e.extname = 'vector'"))[0]?.schema,y=()=>Lt({from:n&&{n:n.n,schema:n.schema},next:u,to:s,hasRows:f=>r.has(f),castable:i,checkedDomain:p,roles:this.roles,generations:f=>h.get(f)??[],vectorSchema:typeof m=="string"?m:"public"}),S=y();if(a.length===0&&g.size===0)return S;for(let[f,d]of a)o.set(`${f}\0${d}`,await this.casts(e,f,d));for(let f of g.keys())g.set(f,await this.checkedDomain(e,f));return y()}async checkedDomain(e,n){let s=await e.query(`SELECT t.typtype = 'd' AND (t.typnotnull OR EXISTS (SELECT 1 FROM pg_catalog.pg_constraint c WHERE c.contypid = t.oid)) AS checked FROM pg_catalog.pg_type t WHERE t.oid = pg_catalog.to_regtype(${M(n)})`);return s.length===0?void 0:s[0].checked===!0}async casts(e,n,s){try{return await e.query(`SELECT CAST(NULL::${n} AS ${s})`),!0}catch(r){let o=ae(r);if(o==="42846")return!1;if(o==="42704")return;throw r}}async active(e){if(e.ledger===!1)return null;let n=await e.query("SELECT n, digest, schema FROM palgres.versions WHERE state = 'active' ORDER BY n DESC LIMIT 1"),s=await this.open(e),o=(s?await e.query(`SELECT n, digest, schema FROM palgres.versions WHERE n = ${s.from_n??-1}`):n)[0];return o?{n:Number(o.n),digest:String(o.digest),state:"active",schema:_t(o.schema)}:null}async version(e,n){if(e.ledger===!1)return null;let s=await e.query(`SELECT n, digest, state FROM palgres.versions WHERE n = ${n}`);return s[0]?{n,digest:String(s[0].digest),state:s[0].state}:null}async open(e){if(e.ledger===!1)return null;let s=(await e.query("SELECT from_n, to_n, plan, done, phase, to_jsonb(m) ->> 'opened_by' AS opened_by FROM palgres.migration m"))[0];return s?{from_n:s.from_n===null?null:Number(s.from_n),to_n:Number(s.to_n),plan:Xo(_t(s.plan),Number(s.to_n)),done:_t(s.done),phase:s.phase,opened_by:typeof s.opened_by=="string"?s.opened_by:null}:null}async session(e,n=!0,s){let r=await this.pool.reserve(),o={query:async a=>await r.unsafe(a)};try{if(await o.query("SET application_name = 'palgres:migrate'"),await o.query("SET search_path = public, pg_temp"),n){await o.query(`SELECT pg_advisory_lock(hashtextextended(${M(nr)}, 0))`);let a=(await o.query(`SELECT to_regclass('palgres.versions') IS NOT NULL AS there, (SELECT pg_catalog.obj_description(n.oid, 'pg_namespace') FROM pg_catalog.pg_namespace n WHERE n.nspname = '${J}') AS search`))[0];o.ledger=a.there===!0,o.searchLedger=typeof a.search=="string"?a.search:null,o.ledger&&await this.ensure(o)}else if((await o.query("SELECT to_regclass('palgres.migration') IS NULL AS none"))[0].none)return await(s?s(o):e(Go));return await e(o)}finally{n&&await o.query(`SELECT pg_advisory_unlock(hashtextextended(${M(nr)}, 0))`).catch(()=>{}),r.release()}}async ownAuth(e){let n=await this.authRefusals(e);if(n.length>0)throw new $e(n)}async authRefusals(e){let n={jwt:["SELECT coalesce(nullif(current_setting('request.jwt.claims', true), '')::jsonb, '{}'::jsonb)","SELECT coalesce(nullif(current_setting('request.jwt.claims', true),'')::jsonb, '{}'::jsonb)"],uid:["SELECT nullif(auth.jwt() ->> 'sub', '')","SELECT current_setting('request.jwt.claims', true)::jsonb ->> 'sub'","SELECT nullif(current_setting('request.jwt.claims', true)::jsonb ->> 'sub','')::text"],role:["SELECT nullif(auth.jwt() ->> 'role', '')","SELECT nullif(current_setting('request.jwt.claims', true)::jsonb ->> 'role','')::text"]},s=a=>a.replace(/\s+/g," ").trim();return(await e.query("SELECT p.proname AS name, p.prosrc AS src, pg_catalog.format_type(p.prorettype, NULL) AS returns FROM pg_catalog.pg_proc p JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace WHERE n.nspname = 'auth' AND p.proname IN ('jwt', 'uid', 'role') AND p.pronargs = 0")).filter(a=>!(n[String(a.name)]??[]).some(i=>s(i)===s(String(a.src)))).map(a=>`auth.${a.name}() is the database's own (returns ${a.returns}: ${s(String(a.src)).slice(0,120)}): Palgres makes it read the request's claims (request.jwt.claims), and the app there now would read its caller otherwise \u2014 rename yours, or make it read the claims as Palgres does, before the take-over`)}async ensure(e){let n=[this.roles.anon,this.roles.authenticated,this.roles.service],s=await e.query(`SELECT r FROM unnest(ARRAY[${n.map(M).join(", ")}]) AS r WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = r)`);if(s.length>0)throw new Error(`palgres: the database has no role ${s.map(o=>o.r).join(", ")} \u2014 the app's roles are made with the database`);let r=n.map(b).join(", ");await this.tx(e,["CREATE SCHEMA IF NOT EXISTS palgres",`CREATE TABLE IF NOT EXISTS palgres.versions (
258
269
  n integer PRIMARY KEY,
259
270
  digest text NOT NULL,
260
271
  schema jsonb NOT NULL,
@@ -273,76 +284,60 @@ END $palgres$`,`CREATE TRIGGER ${g} BEFORE INSERT OR UPDATE ON ${n} FOR EACH ROW
273
284
  phase text NOT NULL CHECK (phase IN ('starting', 'started', 'completing', 'rolling_back')),
274
285
  updated_at timestamptz NOT NULL DEFAULT now(),
275
286
  opened_by text
276
- )`,"GRANT USAGE ON SCHEMA palgres TO PUBLIC","GRANT SELECT (n, digest, state, direct) ON palgres.versions TO PUBLIC","CREATE SCHEMA IF NOT EXISTS auth","CREATE OR REPLACE FUNCTION auth.jwt() RETURNS jsonb LANGUAGE sql STABLE AS $palgres$ SELECT coalesce(nullif(current_setting('request.jwt.claims', true), '')::jsonb, '{}'::jsonb) $palgres$","CREATE OR REPLACE FUNCTION auth.uid() RETURNS text LANGUAGE sql STABLE AS $palgres$ SELECT nullif(auth.jwt() ->> 'sub', '') $palgres$","CREATE OR REPLACE FUNCTION auth.role() RETURNS text LANGUAGE sql STABLE AS $palgres$ SELECT nullif(auth.jwt() ->> 'role', '') $palgres$",`GRANT USAGE ON SCHEMA auth TO ${r}`,`GRANT EXECUTE ON FUNCTION auth.jwt(), auth.uid(), auth.role() TO ${r}`,...e.searchLedger===Un(this.roles)?[]:Ls(this.roles)]),e.searchLedger=Un(this.roles)}},Uo={query:async()=>[]};function Xs(t){let e=ct(ge(t));if(e.length>0)throw new _e(e)}function Lt(t){return typeof t=="string"?JSON.parse(t):t}function Ys(t){return t.slice(1,-1).replace(/""/g,'"')}function It(t,e="public"){return`${N(Te(t,e))}, ${N(e)}, pg_temp`}function Js(t){return t.tables.every(e=>e.physical===e.name&&e.columns.every(n=>n.physical===n.name))}async function Bo(t,e,n){for(let[s,r]of n.objects){if(!s.startsWith("view ")&&!s.startsWith("materialized view "))continue;let o=e.objects.get(s);if(o===void 0||o===r)continue;let{options:a,query:l}=JSON.parse(o),c=await t.reserve();try{await c.unsafe("BEGIN"),await c.unsafe("SET LOCAL search_path = ''"),await c.unsafe(`CREATE TEMP VIEW palgres_fixpoint AS ${l}`);let i=await c.unsafe("SELECT pg_catalog.pg_get_viewdef('pg_temp.palgres_fixpoint'::regclass, true) AS def");JSON.stringify({options:a,query:i[0].def.trim().replace(/;$/,"")})===r&&e.objects.set(s,r)}catch{}finally{await Promise.resolve(c.unsafe("ROLLBACK")).catch(()=>{}),c.release()}}}function Qs(t,e){return{version:t,direct:e,searchPath:e?`${N("public")}, pg_temp`:It(t),applicationName:Gn(t),schema:n=>e?n:Te(t,n)}}var Xn=class extends Error{digest;constructor(e){super(`palgres: this schema (${e}) has no version in the database yet \u2014 the deploy migrates it before the app serves (PALBASE_DB_MIGRATE=start)`),this.name="VersionNotMigrated",this.digest=e}};async function Wo(t,e,n={}){let s=De(e),r=Date.now()+(n.waitMs??0);for(;;){let o;try{o=await t.unsafe("SELECT n, direct FROM palgres.versions WHERE digest = $1 AND state = 'active'",[s])}catch(a){let l=ae(a);if(l!=="42P01"&&l!=="3F000"){if(ce(a)===void 0||Date.now()>=r)throw a;n.onUnreachable?.(a),await new Promise(c=>setTimeout(c,1e3));continue}o=[]}if(o.length>0)return Qs(Number(o[0].n),o[0].direct===!0);if(Date.now()>=r)throw new Xn(s);await new Promise(a=>setTimeout(a,500))}}function fn(t,e,n){let s=zs.get(t);s||zs.set(t,s=new Map);let r=s.get(e);return r===void 0&&(r=`SELECT set_config('role', ${ue(e)}, true), set_config('search_path', ${ue(t.searchPath)}, true), set_config('application_name', ${ue(t.applicationName)}, false), set_config('request.jwt.claims', `,s.set(e,r)),`${r}${ue(JSON.stringify(n))}, true)`}var zs=new WeakMap;async function jo(t,e,n,s,r="service_role"){if(typeof s!="string"||s==="")throw new TypeError("forgetUser: the user id is a non-empty string");let o=[];for(let a of ge(n).tables)for(let l of a.columns){if(!l.user)continue;let c=a.ignored===!0||l.ignored===!0;o.push({view:P(c?a.schema:e.schema(a.schema),a.name),column:l.name,onDelete:l.user.onDelete})}return o.length===0?{deleted:0,detached:0}:t.begin(async a=>{await a.unsafe(fn(e,r,{role:r}));let l=0,c=0;for(let i of o.filter(d=>d.onDelete==="set null")){let d=await a.unsafe(`WITH t AS (UPDATE ${i.view} SET ${N(i.column)} = NULL WHERE ${N(i.column)} = $1 RETURNING 1) SELECT count(*)::int AS n FROM t`,[s]);c+=Number(d[0].n)}for(let i of o.filter(d=>d.onDelete==="cascade")){let d=await a.unsafe(`WITH t AS (DELETE FROM ${i.view} WHERE ${N(i.column)} = $1 RETURNING 1) SELECT count(*)::int AS n FROM t`,[s]);l+=Number(d[0].n)}return{deleted:l,detached:c}})}var Ko=new Set("break case catch class const continue debugger default delete do else enum export extends false finally for function if import in instanceof new null return super switch this throw true try typeof var void while with yield let static implements interface package private protected public await arguments eval".split(" ")),Ho=new Set(["defineSchema","defineTable","uuid","text","integer","bigint","numeric","boolean","timestamp","date","jsonb","policy","raw","foreignKey","enumType","sqlType","AnyColumn","TableHandle"]),vt=t=>/^[A-Za-z_][A-Za-z0-9_]*$/.test(t),er=/^[A-Za-z_$][A-Za-z0-9_$]*$/,j=t=>JSON.stringify(t).replace(/\u2028/g,"\\u2028").replace(/\u2029/g,"\\u2029"),Vo=t=>t.replace(/[\r\n\u2028\u2029]+/g," "),nr=t=>t==="__proto__"?`[${j(t)}]`:er.test(t)?t:j(t),Zs={uuid:"uuid()",text:"text()",integer:"integer()",bigint:"bigint()",numeric:"numeric()",boolean:"boolean()","timestamp with time zone":"timestamp()",date:"date()",jsonb:"jsonb()"},Go=new Set(["uuid","text","integer","bigint","numeric","boolean","timestamp with time zone","date"]);function Xo(t){let e=/^'((?:[^']|'')*)'(?:::[a-z_ ."]+(?:\[\])?)?$/i.exec(t);return e?e[1].replace(/''/g,"'"):void 0}function Yo(t,e={}){let n=e.from??"palbase/backend",s=[],r=new Map,o=new Map(t.enums.map(p=>[tn(p.schema,p.name),p])),a=new Map,l=new Set;for(let p of t.tables){let y=er.test(p.name)&&!Ko.has(p.name)&&!Ho.has(p.name)?p.name:`t_${p.name.replace(/[^A-Za-z0-9_$]/g,"_")}`;for(p.schema!=="public"&&l.has(y)&&(y=`${p.schema}_${y}`);l.has(y);)y=`${y}_`;l.add(y),a.set(D(p),y)}let c=e.units??[],i=zo(t),d=Jo(t),u=[...new Set([...t.tables.map(p=>p.schema),...c.map(p=>p.schema)])].sort(),f=new Map,g=new Set;for(let p of u.filter(vt)){let y=p;for(let S=2;g.has(y.toLowerCase());S++)y=`${p}_${S}`;g.add(y.toLowerCase()),f.set(p,y)}for(let p of u){if(!vt(p)){s.push(`schema ${JSON.stringify(p)} is not a plain name: not pulled`);continue}let y=t.tables.filter(b=>b.schema===p),S=new Set(["defineSchema","defineTable"]),h=new Map,m=y.map(b=>Qo(b,t,o,a,S,h,s,i,d)),T=c.filter(b=>b.schema===p);T.length>0&&S.add("raw");let E=T.map(b=>` raw(${j(b.name)}, ${j(b.create)}, { drop: ${j(b.drop)}${b.before?", before: true":""} }),`),w=[`import { ${[...S].sort().join(", ")} } from ${j(n)};`];for(let[b,O]of[...h].sort())w.push(`import { ${[...O].sort().join(", ")} } from ${j(`./${f.get(b)}.ts`)};`);let A=p==="public"&&t.extensions.length>0?`, extensions: [${t.extensions.map(j).join(", ")}]`:"",k=(e.unmanaged??[]).filter(b=>b.includes(` ${p}.`)&&!c.some(O=>b.startsWith(O.listed))),R=k.length>0?`// Left as it is in the database, outside this file:
277
- ${k.map(b=>`// ${Vo(b)}`).join(`
287
+ )`,"GRANT USAGE ON SCHEMA palgres TO PUBLIC","GRANT SELECT (n, digest, state, direct) ON palgres.versions TO PUBLIC",er,"CREATE SCHEMA IF NOT EXISTS auth","CREATE OR REPLACE FUNCTION auth.jwt() RETURNS jsonb LANGUAGE sql STABLE AS $palgres$ SELECT coalesce(nullif(current_setting('request.jwt.claims', true), '')::jsonb, '{}'::jsonb) $palgres$","CREATE OR REPLACE FUNCTION auth.uid() RETURNS text LANGUAGE sql STABLE AS $palgres$ SELECT nullif(auth.jwt() ->> 'sub', '') $palgres$","CREATE OR REPLACE FUNCTION auth.role() RETURNS text LANGUAGE sql STABLE AS $palgres$ SELECT nullif(auth.jwt() ->> 'role', '') $palgres$",`GRANT USAGE ON SCHEMA auth TO ${r}`,`GRANT EXECUTE ON FUNCTION auth.jwt(), auth.uid(), auth.role() TO ${r}`,...e.searchLedger===Wn(this.roles)?[]:Ms(this.roles)]),e.searchLedger=Wn(this.roles)}},Go={query:async()=>[]};function Xo(t,e){let n=t.format??1;if(!Number.isInteger(n)||n<1||n>Gn)throw new $e([`version ${e}'s migration was planned by a newer Palgres (plan format ${String(t.format)}; this one runs ${Gn} and before): release the version that planned it, or one after, to go on with it`]);for(let s of["start","complete","rollback"])for(let r of t[s]??[])if(!Ws.has(r.kind))throw new $e([`version ${e}'s migration has a step of a kind this Palgres does not run (${JSON.stringify(r.kind)}, in its ${s}): release the version that planned it, or one after, to go on with it`]);return t}function tr(t){let e=ft(ge(t));if(e.length>0)throw new De(e)}function _t(t){return typeof t=="string"?JSON.parse(t):t}function sr(t){return t.slice(1,-1).replace(/""/g,'"')}function Pt(t,e="public"){return`${b(Te(t,e))}, ${b(e)}, pg_temp`}function rr(t){return t.tables.every(e=>e.physical===e.name&&e.columns.every(n=>n.physical===n.name))}async function Yo(t,e,n){for(let[s,r]of n.objects){if(!s.startsWith("view ")&&!s.startsWith("materialized view "))continue;let o=e.objects.get(s);if(o===void 0||o===r)continue;let{options:a,query:i}=JSON.parse(o),c=await t.reserve();try{await c.unsafe("BEGIN"),await c.unsafe("SET LOCAL search_path = ''"),await c.unsafe(`CREATE TEMP VIEW palgres_fixpoint AS ${i}`);let l=await c.unsafe("SELECT pg_catalog.pg_get_viewdef('pg_temp.palgres_fixpoint'::regclass, true) AS def");JSON.stringify({options:a,query:l[0].def.trim().replace(/;$/,"")})===r&&e.objects.set(s,r)}catch{}finally{await Promise.resolve(c.unsafe("ROLLBACK")).catch(()=>{}),c.release()}}}function ar(t,e){return{version:t,direct:e,searchPath:e?`${b("public")}, pg_temp`:Pt(t),applicationName:Qn(t),schema:n=>e?n:Te(t,n)}}var Zn=class extends Error{digest;constructor(e){super(`palgres: this schema (${e}) has no version in the database yet \u2014 the deploy migrates it before the app serves (PALBASE_DB_MIGRATE=start)`),this.name="VersionNotMigrated",this.digest=e}};async function Jo(t,e,n={}){let s=Pe(e),r=Date.now()+(n.waitMs??0);for(;;){let o;try{o=await t.unsafe("SELECT n, direct FROM palgres.versions WHERE digest = $1 AND state = 'active'",[s])}catch(a){let i=ae(a);if(i!=="42P01"&&i!=="3F000"){if(ue(a)===void 0||Date.now()>=r)throw a;n.onUnreachable?.(a),await new Promise(c=>setTimeout(c,1e3));continue}o=[]}if(o.length>0)return ar(Number(o[0].n),o[0].direct===!0);if(Date.now()>=r)throw new Zn(s);await new Promise(a=>setTimeout(a,500))}}function hn(t,e,n){let s=or.get(t);s||or.set(t,s=new Map);let r=s.get(e);return r===void 0&&(r=`SELECT set_config('role', ${de(e)}, true), set_config('search_path', ${de(t.searchPath)}, true), set_config('application_name', ${de(t.applicationName)}, false), set_config('request.jwt.claims', `,s.set(e,r)),`${r}${de(JSON.stringify(n))}, true)`}var or=new WeakMap;async function zo(t,e,n,s,r="service_role"){if(typeof s!="string"||s==="")throw new TypeError("forgetUser: the user id is a non-empty string");let o=[];for(let a of ge(n).tables)for(let i of a.columns){if(!i.user)continue;let c=a.ignored===!0||i.ignored===!0;o.push({view:P(c?a.schema:e.schema(a.schema),a.name),column:i.name,onDelete:i.user.onDelete})}return o.length===0?{deleted:0,detached:0}:t.begin(async a=>{await a.unsafe(hn(e,r,{role:r})),await a.unsafe("SET CONSTRAINTS ALL DEFERRED");let i=0,c=0;for(let l of o.filter(u=>u.onDelete==="set null")){let u=await a.unsafe(`WITH t AS (UPDATE ${l.view} SET ${b(l.column)} = NULL WHERE ${b(l.column)} = $1 RETURNING 1) SELECT count(*)::int AS n FROM t`,[s]);c+=Number(u[0].n)}for(let l of o.filter(u=>u.onDelete==="cascade")){let u=await a.unsafe(`WITH t AS (DELETE FROM ${l.view} WHERE ${b(l.column)} = $1 RETURNING 1) SELECT count(*)::int AS n FROM t`,[s]);i+=Number(u[0].n)}return{deleted:i,detached:c}})}var Qo=new Set("break case catch class const continue debugger default delete do else enum export extends false finally for function if import in instanceof new null return super switch this throw true try typeof var void while with yield let static implements interface package private protected public await arguments eval".split(" ")),Zo=new Set(["defineSchema","defineTable","uuid","text","integer","bigint","numeric","boolean","timestamp","date","jsonb","policy","raw","foreignKey","enumType","sqlType","AnyColumn","TableHandle"]),Mt=t=>/^[A-Za-z_][A-Za-z0-9_]*$/.test(t),lr=/^[A-Za-z_$][A-Za-z0-9_$]*$/,j=t=>JSON.stringify(t).replace(/\u2028/g,"\\u2028").replace(/\u2029/g,"\\u2029"),ea=t=>t.replace(/[\r\n\u2028\u2029]+/g," "),cr=t=>t==="__proto__"?`[${j(t)}]`:lr.test(t)?t:j(t),ir={uuid:"uuid()",text:"text()",integer:"integer()",bigint:"bigint()",numeric:"numeric()",boolean:"boolean()","timestamp with time zone":"timestamp()",date:"date()",jsonb:"jsonb()"},na=new Set(["uuid","text","integer","bigint","numeric","boolean","timestamp with time zone","date"]);function ta(t){let e=/^'((?:[^']|'')*)'(?:::[a-z_ ."]+(?:\[\])?)?$/i.exec(t);return e?e[1].replace(/''/g,"'"):void 0}function sa(t,e={}){let n=e.from??"palbase/backend",s=[],r=new Map,o=new Map(t.enums.map(m=>[rn(m.schema,m.name),m])),a=new Map,i=new Set;for(let m of t.tables){let y=lr.test(m.name)&&!Qo.has(m.name)&&!Zo.has(m.name)?m.name:`t_${m.name.replace(/[^A-Za-z0-9_$]/g,"_")}`;for(m.schema!=="public"&&i.has(y)&&(y=`${m.schema}_${y}`);i.has(y);)y=`${y}_`;i.add(y),a.set(D(m),y)}let c=e.units??[],l=oa(t),u=ra(t),g=[...new Set([...t.tables.map(m=>m.schema),...c.map(m=>m.schema)])].sort(),p=new Map,h=new Set;for(let m of g.filter(Mt)){let y=m;for(let S=2;h.has(y.toLowerCase());S++)y=`${m}_${S}`;h.add(y.toLowerCase()),p.set(m,y)}for(let m of g){if(!Mt(m)){s.push(`schema ${JSON.stringify(m)} is not a plain name: not pulled`);continue}let y=t.tables.filter(A=>A.schema===m),S=new Set(["defineSchema","defineTable"]),f=new Map,d=y.map(A=>aa(A,t,o,a,S,f,s,l,u)),T=c.filter(A=>A.schema===m);T.length>0&&S.add("raw");let E=T.map(A=>` raw(${j(A.name)}, ${j(A.create)}, { drop: ${j(A.drop)}${A.before?", before: true":""} }),`),w=[`import { ${[...S].sort().join(", ")} } from ${j(n)};`];for(let[A,R]of[...f].sort())w.push(`import { ${[...R].sort().join(", ")} } from ${j(`./${p.get(A)}.ts`)};`);let O=m==="public"&&t.extensions.length>0?`, extensions: [${t.extensions.map(j).join(", ")}]`:"",k=(e.unmanaged??[]).filter(A=>A.includes(` ${m}.`)&&!c.some(R=>A.startsWith(R.listed))),N=k.length>0?`// Left as it is in the database, outside this file:
288
+ ${k.map(A=>`// ${ea(A)}`).join(`
278
289
  `)}
279
290
 
280
- `:"";r.set(`db/${f.get(p)}.ts`,`// Written by palgres pull from the database, ${new Date().toISOString().slice(0,10)}.
281
- ${R}${w.join(`
291
+ `:"";r.set(`db/${p.get(m)}.ts`,`// Written by palgres pull from the database, ${new Date().toISOString().slice(0,10)}.
292
+ ${N}${w.join(`
282
293
  `)}
283
294
 
284
- ${m.join(`
295
+ ${d.join(`
285
296
 
286
- `)}${m.length>0?`
297
+ `)}${d.length>0?`
287
298
 
288
- `:""}export default defineSchema(${j(p)}, {
289
- tables: [${y.map(b=>a.get(D(b))).join(", ")}],${A?`
290
- ${A.slice(2)},`:""}${E.length>0?`
299
+ `:""}export default defineSchema(${j(m)}, {
300
+ tables: [${y.map(A=>a.get(D(A))).join(", ")}],${O?`
301
+ ${O.slice(2)},`:""}${E.length>0?`
291
302
  raw: [
292
303
  ${E.join(`
293
304
  `)}
294
305
  ],`:""}
295
306
  });
296
- `)}return{files:r,notes:s}}function tr(t,e){let n=r=>`${t.schema==="public"?t.name:`${t.schema}_${t.name}`}_${r}_fkey`,s=new Map;for(let r of t.foreignKeys)r.columns.length===1&&r.name===n(r.columns[0])&&r.match!=="full"&&!Ct(e,r)&&s.set(r.columns[0],r);return s}var Ct=(t,e)=>!vt(e.refSchema)||!t.tables.some(n=>n.schema===e.refSchema&&n.name===e.refTable);function Jo(t){let e=new Map(t.tables.map(d=>[D(d),d.foreignKeys.filter(u=>!Ct(t,u)).map(u=>`${u.refSchema}.${u.refTable}`)])),n=new Map,s=new Map,r=new Map,o=[],a=new Set,l=0,c=0,i=d=>{n.set(d,l),s.set(d,l),l++,o.push(d),a.add(d);for(let f of e.get(d)??[])n.has(f)?a.has(f)&&s.set(d,Math.min(s.get(d),n.get(f))):(i(f),s.set(d,Math.min(s.get(d),s.get(f))));if(s.get(d)!==n.get(d))return;let u;do u=o.pop(),a.delete(u),r.set(u,c);while(u!==d);c++};for(let d of e.keys())n.has(d)||i(d);return(d,u,f)=>r.get(D(d))===r.get(`${u}.${f}`)}function zo(t){let e=l=>l.endsWith("_id")?l.slice(0,-3):l,n=new Map,s=[];for(let l of t.tables)for(let[c,i]of tr(l,t))s.push({fk:`${D(l)}.${c}`,child:D(l),column:c,target:`${i.refSchema}.${i.refTable}`,childName:l.name});let r=new Map,o=(l,c)=>r.set(`${l}\0${c}`,(r.get(`${l}\0${c}`)??0)+1);for(let l of s)o(l.child,e(l.column)),l.target!==l.child&&o(l.target,l.childName);let a=(l,c)=>(r.get(`${l}\0${c}`)??0)>1;for(let l of s){let c={};a(l.child,e(l.column))&&(c.as=l.column),l.target!==l.child&&a(l.target,l.childName)&&(c.reverseAs=`${l.childName}_${e(l.column)}`),(c.as!==void 0||c.reverseAs!==void 0)&&n.set(l.fk,c)}return n}function Qo(t,e,n,s,r,o,a,l,c){let i=D(t),d=[],u=t.primaryKey.length===1?t.primaryKey[0]:void 0,f=new Set(t.uniques.filter(w=>w.columns.length===1&&w.name===`${t.name}_${w.columns[0]}_key`).map(w=>w.columns[0])),g=tr(t,e),p=w=>Ct(e,w);for(let w of t.foreignKeys)p(w)&&a.push(`foreign key ${i}.${w.name} references ${w.refSchema}.${w.refTable}, outside the pulled schemas: left out, kept in the database as it is`);let y=new Set(t.indexes.map(w=>w.name)),S={name(w,A){let k=s.get(`${w}.${A}`);if(w!==t.schema){let R=o.get(w)??new Set;R.add(k),o.set(w,R)}return k},arrow(w,A,k){return c(t,w,A)?(r.add(`type ${k}`),`(): ${k} =>`):"() =>"}};d.push(" columns: {");for(let w of t.columns)d.push(` ${nr(w.name)}: ${Zo(t,w,n,r,a,u===w.name,f.has(w.name),g.get(w.name),y,S,l.get(`${i}.${w.name}`))},`);d.push(" },"),t.primaryKey.length>1&&d.push(` primaryKey: [${t.primaryKey.map(j).join(", ")}],`);let h=t.uniques.filter(w=>!(w.columns.length===1&&f.has(w.columns[0])&&w.name===`${t.name}_${w.columns[0]}_key`));h.length>0&&d.push(` unique: [${h.map(w=>`{ name: ${j(w.name)}, columns: [${w.columns.map(j).join(", ")}] }`).join(", ")}],`),t.checks.length>0&&d.push(` checks: [${t.checks.map(w=>`{ name: ${j(w.name)}, expr: ${j(w.expr)} }`).join(", ")}],`);let m=[],T=[];for(let w of t.indexes)(na(w)?m:T).push(w);m.length>0&&d.push(` indexes: () => [${m.map(w=>ta(w)).join(", ")}],`);let E=t.foreignKeys.filter(w=>!p(w)&&g.get(w.columns[0])!==w);return E.length>0&&(r.add("foreignKey"),d.push(` foreignKeys: (c) => [${E.map(w=>ra(t,w,S)).join(", ")}],`)),t.policies.length>0&&!t.rls?a.push(`table ${i} has ${t.policies.length} polic${t.policies.length===1?"y":"ies"} with row-level security off: they do nothing, and the files leave them out \u2014 the take-over drops them; to have them hold, declare rls: true and the policies`):t.policies.length>0&&(r.add("policy"),d.push(` policies: () => [${t.policies.map(w=>oa(t,w)).join(", ")}],`)),t.rls||d.push(" rls: false,"),T.length>0&&(r.add("raw"),d.push(` raw: [${T.map(w=>`raw(${j(w.name)}, ${j(sa(t,w))})`).join(", ")}],`)),`export const ${s.get(i)} = defineTable(${j(t.name)}, {
297
- ${d.join(`
307
+ `)}return{files:r,notes:s}}function ur(t,e){let n=r=>`${t.schema==="public"?t.name:`${t.schema}_${t.name}`}_${r}_fkey`,s=new Map;for(let r of t.foreignKeys)r.columns.length===1&&r.name===n(r.columns[0])&&r.match!=="full"&&!qt(e,r)&&s.set(r.columns[0],r);return s}var qt=(t,e)=>!Mt(e.refSchema)||!t.tables.some(n=>n.schema===e.refSchema&&n.name===e.refTable);function ra(t){let e=new Map(t.tables.map(u=>[D(u),u.foreignKeys.filter(g=>!qt(t,g)).map(g=>`${g.refSchema}.${g.refTable}`)])),n=new Map,s=new Map,r=new Map,o=[],a=new Set,i=0,c=0,l=u=>{n.set(u,i),s.set(u,i),i++,o.push(u),a.add(u);for(let p of e.get(u)??[])n.has(p)?a.has(p)&&s.set(u,Math.min(s.get(u),n.get(p))):(l(p),s.set(u,Math.min(s.get(u),s.get(p))));if(s.get(u)!==n.get(u))return;let g;do g=o.pop(),a.delete(g),r.set(g,c);while(g!==u);c++};for(let u of e.keys())n.has(u)||l(u);return(u,g,p)=>r.get(D(u))===r.get(`${g}.${p}`)}function oa(t){let e=i=>i.endsWith("_id")?i.slice(0,-3):i,n=new Map,s=[];for(let i of t.tables)for(let[c,l]of ur(i,t))s.push({fk:`${D(i)}.${c}`,child:D(i),column:c,target:`${l.refSchema}.${l.refTable}`,childName:i.name});let r=new Map,o=(i,c)=>r.set(`${i}\0${c}`,(r.get(`${i}\0${c}`)??0)+1);for(let i of s)o(i.child,e(i.column)),i.target!==i.child&&o(i.target,i.childName);let a=(i,c)=>(r.get(`${i}\0${c}`)??0)>1;for(let i of s){let c={};a(i.child,e(i.column))&&(c.as=i.column),i.target!==i.child&&a(i.target,i.childName)&&(c.reverseAs=`${i.childName}_${e(i.column)}`),(c.as!==void 0||c.reverseAs!==void 0)&&n.set(i.fk,c)}return n}function aa(t,e,n,s,r,o,a,i,c){let l=D(t),u=[],g=t.primaryKey.length===1?t.primaryKey[0]:void 0,p=new Set(t.uniques.filter(w=>w.columns.length===1&&w.name===`${t.name}_${w.columns[0]}_key`).map(w=>w.columns[0])),h=ur(t,e),m=w=>qt(e,w);for(let w of t.foreignKeys)m(w)&&a.push(`foreign key ${l}.${w.name} references ${w.refSchema}.${w.refTable}, outside the pulled schemas: left out, kept in the database as it is`);for(let w of t.foreignKeys)m(w)||!je(w.onDelete,w.onUpdate)||w.deferrable||a.push(`foreign key ${l}.${w.name} is checked at once, so the files say deferrable: false \u2014 remove it to let the next migration make the key deferrable: forgetting a user needs that where the user's rows cascade onto a row with more than one key with an action`);let y=new Set(t.indexes.map(w=>w.name)),S={name(w,O){let k=s.get(`${w}.${O}`);if(w!==t.schema){let N=o.get(w)??new Set;N.add(k),o.set(w,N)}return k},arrow(w,O,k){return c(t,w,O)?(r.add(`type ${k}`),`(): ${k} =>`):"() =>"}};u.push(" columns: {");for(let w of t.columns)u.push(` ${cr(w.name)}: ${ia(t,w,n,r,a,g===w.name,p.has(w.name),h.get(w.name),y,S,i.get(`${l}.${w.name}`))},`);u.push(" },"),t.primaryKey.length>1&&u.push(` primaryKey: [${t.primaryKey.map(j).join(", ")}],`);let f=t.uniques.filter(w=>!(w.columns.length===1&&p.has(w.columns[0])&&w.name===`${t.name}_${w.columns[0]}_key`));f.length>0&&u.push(` unique: [${f.map(w=>`{ name: ${j(w.name)}, columns: [${w.columns.map(j).join(", ")}] }`).join(", ")}],`),t.checks.length>0&&u.push(` checks: [${t.checks.map(w=>`{ name: ${j(w.name)}, expr: ${j(w.expr)} }`).join(", ")}],`);let d=[],T=[];for(let w of t.indexes)(ca(w)?d:T).push(w);d.length>0&&u.push(` indexes: () => [${d.map(w=>ua(w)).join(", ")}],`);let E=t.foreignKeys.filter(w=>!m(w)&&h.get(w.columns[0])!==w);return E.length>0&&(r.add("foreignKey"),u.push(` foreignKeys: (c) => [${E.map(w=>ma(t,w,S)).join(", ")}],`)),t.policies.length>0&&!t.rls?a.push(`table ${l} has ${t.policies.length} polic${t.policies.length===1?"y":"ies"} with row-level security off: they do nothing, and the files leave them out \u2014 the take-over drops them; to have them hold, declare rls: true and the policies`):t.policies.length>0&&(r.add("policy"),u.push(` policies: () => [${t.policies.map(w=>pa(t,w)).join(", ")}],`)),t.rls||u.push(" rls: false,"),T.length>0&&(r.add("raw"),u.push(` raw: [${T.map(w=>`raw(${j(w.name)}, ${j(da(t,w))})`).join(", ")}],`)),`export const ${s.get(l)} = defineTable(${j(t.name)}, {
308
+ ${u.join(`
298
309
  `)}
299
- });`}function Zo(t,e,n,s,r,o,a,l,c,i,d){let u=`${D(t)}.${e.name}`,f,g=e.type,p=!1;g.endsWith("[]")&&(Go.has(g.slice(0,-2))||n.has(g.slice(0,-2)))&&(g=g.slice(0,-2),p=!0);let y=n.get(g),S=/^numeric\((\d+),(\d+)\)$/.exec(g);if(Zs[g]?f=Zs[g]:y&&y.schema===t.schema?(f=`enumType(${j(y.name)}, [${y.values.map(j).join(", ")}])`,s.add("enumType")):S?f=`numeric({ precision: ${S[1]}, scale: ${S[2]} })`:(f=`sqlType(${j(e.type)})`,s.add("sqlType"),p=!1),s.add(f.slice(0,f.indexOf("("))),p&&(f+=".array()"),o&&(f+=".primaryKey()"),e.identity&&(f+=e.identity==="always"?".identity()":".identity({ always: false })"),e.nullable&&!o&&(f+=".nullable()"),a&&(f+=".unique()"),e.generated&&(f+=`.generated(${j(e.generated.expr)}${e.generated.stored?", { stored: true }":""})`),e.default!==void 0&&(f+=ea(e,p)),l){let h=i.name(l.refSchema,l.refTable),m=[];d?.as!==void 0&&m.push(`as: ${j(d.as)}`),d?.reverseAs!==void 0&&m.push(`reverseAs: ${j(d.reverseAs)}`),l.onDelete!=="no action"&&m.push(`onDelete: ${j(l.onDelete)}`),l.onUpdate!=="no action"&&m.push(`onUpdate: ${j(l.onUpdate)}`),sr(t,e.name)||m.push("index: false"),f+=`.references(${i.arrow(l.refSchema,l.refTable,"AnyColumn")} ${h}.${nr(l.refColumns[0])}${m.length>0?`, { ${m.join(", ")} }`:""})`}return/^(owner|user_id|created_by|author_id|.*_user_id)$/.test(e.name)&&!l&&r.push(`column ${u} may hold a user's id: mark it ownedByUser() or userRef() so user.deleted reaches it`),f}function sr(t,e){let n=t.indexes.find(r=>r.name===`${t.name}_${e}_idx`);if(n===void 0||n.unique||n.where!==void 0||n.method!==void 0||n.include?.length||n.keys.length!==1)return!1;let s=n.keys[0];return"column"in s&&s.column===e&&!s.desc&&s.nulls===void 0}function ea(t,e){let n=t.default;if(n==="gen_random_uuid()")return".defaultRandom()";if(n==="now()"&&t.type==="timestamp with time zone"||n==="CURRENT_DATE"&&t.type==="date")return".defaultNow()";if(n==="true"||n==="false")return`.default(${n})`;if(/^-?\d+(\.\d+)?$/.test(n)&&(t.type==="integer"||t.type==="bigint"))return`.default(${n})`;if(n==="'{}'::text[]"||/^'\{\}'::/.test(n))return e?".default([])":`.defaultSql(${j(n)})`;let s=Xo(n);if(s!==void 0&&(/^'(?:[^']|'')*'::text$/.test(n)||t.type.startsWith("public.")&&!/::(?:text|character varying|character)\b/.test(n)))return`.default(${j(s)})`;if(s!==void 0&&t.type==="jsonb"&&/^'(?:[^']|'')*'::jsonb$/.test(n))try{let r=JSON.stringify(JSON.parse(s));if(r===s&&!/"__proto__"\s*:/.test(s))return`.default(${r})`}catch{}return`.defaultSql(${j(n)})`}function na(t){if(t.method!==void 0||t.where!==void 0||!t.keys.every(n=>"column"in n))return!1;let e=t.keys[0];return t.keys.every(n=>n.desc===e.desc&&n.nulls===e.nulls)}function ta(t){let e=t.keys[0],n=[`name: ${j(t.name)}`,`columns: [${t.keys.map(s=>j(s.column)).join(", ")}]`];return t.unique&&n.push("unique: true"),e.desc&&n.push("sort: 'desc'"),e.nulls&&n.push(`nulls: ${j(e.nulls)}`),t.include?.length&&n.push(`include: [${t.include.map(j).join(", ")}]`),`{ ${n.join(", ")} }`}function sa(t,e){let n=r=>`"${r.replace(/"/g,'""')}"`,s=e.keys.map(r=>`${"column"in r?n(r.column):r.expr}${r.desc?" DESC":""}${r.nulls?` NULLS ${r.nulls.toUpperCase()}`:""}`).join(", ");return`CREATE ${e.unique?"UNIQUE ":""}INDEX ${n(e.name)} ON ${n(t.schema)}.${n(t.name)}${e.method?` USING ${e.method}`:""} (${s})${e.include?.length?` INCLUDE (${e.include.map(n).join(", ")})`:""}${e.where?` WHERE ${e.where}`:""}`}function ra(t,e,n){let s=`foreignKey(${j(e.name)}).on(${e.columns.map(r=>`c.col(${j(r)})`).join(", ")}).references(${n.arrow(e.refSchema,e.refTable,"TableHandle")} ${n.name(e.refSchema,e.refTable)}, ${e.refColumns.map(j).join(", ")})`;return e.onDelete!=="no action"&&(s+=`.onDelete(${j(e.onDelete)})`),e.onUpdate!=="no action"&&(s+=`.onUpdate(${j(e.onUpdate)})`),e.match==="full"&&(s+=".match('full')"),e.columns.length===1&&!sr(t,e.columns[0])&&(s+=".index(false)"),s}function oa(t,e){let n=`policy(${j(e.name)}).for(${j(e.for)}).to(${e.to.map(j).join(", ")})`;e.restrictive&&(n+=".as('restrictive')"),e.using!==void 0&&(n+=`.using(${j(e.using)})`),e.withCheck!==void 0&&(n+=`.withCheck(${j(e.withCheck)})`);let s=r=>t.primaryKey[0]===r||t.uniques.some(o=>o.columns[0]===r)||t.indexes.some(o=>o.where===void 0&&"column"in(o.keys[0]??{})&&o.keys[0].column===r);return ds(t,e).some(r=>!s(r)&&!t.indexes.some(o=>o.name===`${t.name}_${r}_idx`))&&(n+=".withoutIndexes()"),n}function Ye(t){if(t==null)return null;switch(typeof t){case"string":return t;case"number":case"bigint":return String(t);case"boolean":return t?"true":"false";case"symbol":case"function":throw new TypeError(`a ${typeof t} is not a database value`)}return t instanceof Date?aa(t):t instanceof Uint8Array?`\\x${Buffer.from(t).toString("hex")}`:Array.isArray(t)&&ar(t)?gn(t):JSON.stringify(t)}function ar(t){return t.every(e=>e==null||typeof e!="object"&&typeof e!="function"&&typeof e!="symbol"||e instanceof Date||e instanceof Uint8Array||Array.isArray(e)&&ar(e))}function gn(t){let e=n=>n==null?"NULL":Array.isArray(n)?gn(n):`"${String(Ye(n)).replace(/(["\\])/g,"\\$1")}"`;return`{${t.map(e).join(",")}}`}function aa(t){if(Number.isNaN(t.getTime()))throw new TypeError("an invalid Date is not a database value");return t.toISOString()}var ia={json:{to:114,from:[114,3802],serialize:t=>t,parse:t=>JSON.parse(t)},boolean:{to:16,from:[16],serialize:t=>t,parse:t=>t==="t"},date:{to:1184,from:[1082,1114,1184],serialize:t=>t,parse:t=>new Date(t)},bytea:{to:17,from:[17],serialize:t=>t,parse:t=>Buffer.from(t.slice(2),"hex")}};function la(t){let e=t;return e.length>0&&Array.isArray(e[e.length-1])?e[e.length-1]:e}var ir=/^[A-Za-z0-9:_-]{1,80}$/;function lr(t,e){let n=new Array(e.length).fill(!1),s="",r=0,o=0;for(;o<t.length;){let a=t.charCodeAt(o);if(a===39||a===34){let l=o+1;for(;;){if(l>=t.length||t.charCodeAt(l)===92)return;if(t.charCodeAt(l)===a){if(t.charCodeAt(l+1)!==a)break;l+=2}else l++}o=l+1;continue}if(a===36){let l=o+1,c=0;for(;l<t.length&&t.charCodeAt(l)>=48&&t.charCodeAt(l)<=57&&l-o<=6;)c=c*10+(t.charCodeAt(l++)-48);if(l===o+1||c<1||c>e.length||n[c-1])return;n[c-1]=!0;let i=e[c-1];if(i!==null&&typeof i!="string"||i!==null&&i.includes("\0"))return;s+=t.slice(r,o)+(i===null?"NULL":ue(i)),r=o=l;continue}if(a===59||a===92||a===45&&t.charCodeAt(o+1)===45||a===47&&t.charCodeAt(o+1)===42)return;o++}for(let a of n)if(!a)return;return s+t.slice(r)}function rr(t){return Nn(t)?Object.assign(new Error("current transaction is aborted, commands ignored until end of transaction block",{cause:t}),{errno:"25P02",code:"ERR_POSTGRES_SERVER_ERROR"}):t}var Yn=10,Jn=2;function cr(t,e){let n={unsafe:(s,r)=>n.begin(o=>Promise.resolve(o.unsafe(s,r))),async begin(s,r){if(r?.tag!==void 0&&!ir.test(r.tag))throw new Error(`palgres: an opening's tag is letters, digits, ":", "_" and "-"`);let o=`${r?.tag===void 0?"":`/* ${r.tag} */ `}BEGIN${r===void 0?"":`; ${r.sql}`}`,a=await t.reserve(),l=!0,c=!1,i=!1,d=null,u,f=!1,g=0,p=m=>(d=(async()=>{for(let T=1;;T++){c=!0;try{let E=await a.unsafe(m===void 0?o:`${o}; ${m}`).simple();if((E.length>0&&Array.isArray(E[0])?E[0]:E).command!=="BEGIN")throw new Error("palgres: the round trip that opens the transaction did not begin one");return r?.opened?.(E[E.length-(m===void 0?1:2)]),i=!0,m===void 0?void 0:E[E.length-1]}catch(E){let w=us(E);w&&(t.discard!==void 0?t.discard(a):Promise.resolve(a.close({timeout:0})).catch(()=>{}),l=!1,c=!1);let A=w?"unusable":ce(E);if(A===void 0||T>=(A==="never"?Jn:Yn))throw u={error:E},E;w||(await Promise.resolve(a.close()).catch(()=>{}),a.release(),l=!1,c=!1);try{a=await t.reserve()}catch(k){throw u={error:k},k}l=!0}}})(),d.catch(()=>{}),d),y=()=>(d===null&&p(),d.then(()=>{},()=>{throw rr(u.error)})),S={unsafe(m,T,E){if(u!==void 0)return Promise.reject(rr(u.error));if(i)return a.unsafe(m,T);if(d===null&&E?.inline===!0){let w=lr(m,T??[]);if(w!==void 0)return p(w)}return y().then(()=>a.unsafe(m,T))},async savepoint(m){await y();let T=`palgres_sp_${++g}`;await a.unsafe(`SAVEPOINT ${T}`).simple();let E;try{E=await m(S)}catch(w){throw await Promise.resolve(a.unsafe(`ROLLBACK TO SAVEPOINT ${T}; RELEASE SAVEPOINT ${T}`).simple()).catch(()=>{}),w}return await a.unsafe(`RELEASE SAVEPOINT ${T}`).simple(),E},abandon(){f||(f=!0,Promise.resolve(a.close({timeout:0})).catch(()=>{}))}},h=async m=>{let T=await a.unsafe(m).simple();if(c=!1,m==="COMMIT"&&T.command==="ROLLBACK")throw new Error("palgres: COMMIT was answered with ROLLBACK \u2014 a statement of the transaction had failed")};try{let m;try{if(m=await s(S),await d?.catch(()=>{}),u!==void 0)throw u.error}catch(T){throw await d?.catch(()=>{}),c&&!f&&await h("ROLLBACK").catch(()=>{}),T}if(!c)return m;try{await h("COMMIT")}catch(T){throw c&&await h("ROLLBACK").catch(()=>{}),T}return m}finally{l&&(c&&!f&&await Promise.resolve(a.close()).catch(()=>{}),a.release())}}};if(e!==void 0){let s=cr(e);n.cancelBackend=Dt(s),n.cancelOpening=dr(s)}return n}var _t=1e3;async function ur(t,e){let n;try{await Promise.race([Promise.resolve(t.close({timeout:e})),new Promise(s=>{n=setTimeout(s,e*1e3+_t)})])}finally{clearTimeout(n)}}function ca(t){let e=t(),n=0,s=!1,r=new WeakMap,o=new Map;return{get current(){return e},get renewals(){return n},unsafe:(a,l)=>e.unsafe(a,l),async reserve(){let a=e,l=await a.reserve();return r.set(l,a),l},discard(a){let l=r.get(a);if(l===e&&!s){e=t(),n++;let c=Promise.resolve(l.close()).catch(()=>{});o.set(l,c),c.then(()=>o.delete(l))}a.release()},async close(a){s=!0;let l=a?.timeout??0,c;try{await Promise.all([ur(e,l),Promise.race([Promise.all(o.values()),new Promise(i=>{c=setTimeout(i,l*1e3+_t)})])])}finally{clearTimeout(c)}}}}function dr(t){return async e=>{if(!ir.test(e))throw new Error(`palgres: an opening's tag is letters, digits, ":", "_" and "-"`);let n=`/* ${e} */`;return(await t.unsafe(`SELECT pg_cancel_backend(pid) AS cancelled FROM pg_stat_activity
310
+ });`}function ia(t,e,n,s,r,o,a,i,c,l,u){let g=`${D(t)}.${e.name}`,p,h=e.type,m=!1;h.endsWith("[]")&&(na.has(h.slice(0,-2))||n.has(h.slice(0,-2)))&&(h=h.slice(0,-2),m=!0);let y=n.get(h),S=/^numeric\((\d+),(\d+)\)$/.exec(h);if(ir[h]?p=ir[h]:y&&y.schema===t.schema?(p=`enumType(${j(y.name)}, [${y.values.map(j).join(", ")}])`,s.add("enumType")):S?p=`numeric({ precision: ${S[1]}, scale: ${S[2]} })`:(p=`sqlType(${j(e.type)})`,s.add("sqlType"),m=!1),s.add(p.slice(0,p.indexOf("("))),m&&(p+=".array()"),o&&(p+=".primaryKey()"),e.identity&&(p+=e.identity==="always"?".identity()":".identity({ always: false })"),e.nullable&&!o&&(p+=".nullable()"),a&&(p+=".unique()"),e.generated&&(p+=`.generated(${j(e.generated.expr)}${e.generated.stored?", { stored: true }":""})`),e.default!==void 0&&(p+=la(e,m)),i){let f=l.name(i.refSchema,i.refTable),d=[];u?.as!==void 0&&d.push(`as: ${j(u.as)}`),u?.reverseAs!==void 0&&d.push(`reverseAs: ${j(u.reverseAs)}`),i.onDelete!=="no action"&&d.push(`onDelete: ${j(i.onDelete)}`),i.onUpdate!=="no action"&&d.push(`onUpdate: ${j(i.onUpdate)}`),dr(t,e.name)||d.push("index: false"),je(i.onDelete,i.onUpdate)&&!i.deferrable&&d.push("deferrable: false"),p+=`.references(${l.arrow(i.refSchema,i.refTable,"AnyColumn")} ${f}.${cr(i.refColumns[0])}${d.length>0?`, { ${d.join(", ")} }`:""})`}return/^(owner|user_id|created_by|author_id|.*_user_id)$/.test(e.name)&&!i&&r.push(`column ${g} may hold a user's id: mark it ownedByUser() or userRef() so user.deleted reaches it`),p}function dr(t,e){let n=t.indexes.find(r=>r.name===`${t.name}_${e}_idx`);if(n===void 0||n.unique||n.where!==void 0||n.method!==void 0||n.include?.length||n.keys.length!==1)return!1;let s=n.keys[0];return"column"in s&&s.column===e&&!s.desc&&s.nulls===void 0}function la(t,e){let n=t.default;if(n==="gen_random_uuid()")return".defaultRandom()";if(n==="now()"&&t.type==="timestamp with time zone"||n==="CURRENT_DATE"&&t.type==="date")return".defaultNow()";if(n==="true"||n==="false")return`.default(${n})`;if(/^-?\d+(\.\d+)?$/.test(n)&&(t.type==="integer"||t.type==="bigint"))return`.default(${n})`;if(n==="'{}'::text[]"||/^'\{\}'::/.test(n))return e?".default([])":`.defaultSql(${j(n)})`;let s=ta(n);if(s!==void 0&&(/^'(?:[^']|'')*'::text$/.test(n)||t.type.startsWith("public.")&&!/::(?:text|character varying|character)\b/.test(n)))return`.default(${j(s)})`;if(s!==void 0&&t.type==="jsonb"&&/^'(?:[^']|'')*'::jsonb$/.test(n))try{let r=JSON.stringify(JSON.parse(s));if(r===s&&!/"__proto__"\s*:/.test(s))return`.default(${r})`}catch{}return`.defaultSql(${j(n)})`}function ca(t){if(t.method!==void 0||t.where!==void 0||!t.keys.every(n=>"column"in n))return!1;let e=t.keys[0];return t.keys.every(n=>n.desc===e.desc&&n.nulls===e.nulls)}function ua(t){let e=t.keys[0],n=[`name: ${j(t.name)}`,`columns: [${t.keys.map(s=>j(s.column)).join(", ")}]`];return t.unique&&n.push("unique: true"),e.desc&&n.push("sort: 'desc'"),e.nulls&&n.push(`nulls: ${j(e.nulls)}`),t.include?.length&&n.push(`include: [${t.include.map(j).join(", ")}]`),`{ ${n.join(", ")} }`}function da(t,e){let n=r=>`"${r.replace(/"/g,'""')}"`,s=e.keys.map(r=>`${"column"in r?n(r.column):r.expr}${r.desc?" DESC":""}${r.nulls?` NULLS ${r.nulls.toUpperCase()}`:""}`).join(", ");return`CREATE ${e.unique?"UNIQUE ":""}INDEX ${n(e.name)} ON ${n(t.schema)}.${n(t.name)}${e.method?` USING ${e.method}`:""} (${s})${e.include?.length?` INCLUDE (${e.include.map(n).join(", ")})`:""}${e.where?` WHERE ${e.where}`:""}`}function ma(t,e,n){let s=`foreignKey(${j(e.name)}).on(${e.columns.map(r=>`c.col(${j(r)})`).join(", ")}).references(${n.arrow(e.refSchema,e.refTable,"TableHandle")} ${n.name(e.refSchema,e.refTable)}, ${e.refColumns.map(j).join(", ")})`;return e.onDelete!=="no action"&&(s+=`.onDelete(${j(e.onDelete)})`),e.onUpdate!=="no action"&&(s+=`.onUpdate(${j(e.onUpdate)})`),e.match==="full"&&(s+=".match('full')"),e.columns.length===1&&!dr(t,e.columns[0])&&(s+=".index(false)"),je(e.onDelete,e.onUpdate)&&!e.deferrable&&(s+=".deferrable(false)"),s}function pa(t,e){let n=`policy(${j(e.name)}).for(${j(e.for)}).to(${e.to.map(j).join(", ")})`;e.restrictive&&(n+=".as('restrictive')"),e.using!==void 0&&(n+=`.using(${j(e.using)})`),e.withCheck!==void 0&&(n+=`.withCheck(${j(e.withCheck)})`);let s=r=>t.primaryKey[0]===r||t.uniques.some(o=>o.columns[0]===r)||t.indexes.some(o=>o.where===void 0&&"column"in(o.keys[0]??{})&&o.keys[0].column===r);return ys(t,e).some(r=>!s(r)&&!t.indexes.some(o=>o.name===`${t.name}_${r}_idx`))&&(n+=".withoutIndexes()"),n}function ze(t){if(t==null)return null;switch(typeof t){case"string":return t;case"number":case"bigint":return String(t);case"boolean":return t?"true":"false";case"symbol":case"function":throw new TypeError(`a ${typeof t} is not a database value`)}return t instanceof Date?fa(t):t instanceof Uint8Array?`\\x${Buffer.from(t).toString("hex")}`:Array.isArray(t)&&fr(t)?En(t):JSON.stringify(t)}function fr(t){return t.every(e=>e==null||typeof e!="object"&&typeof e!="function"&&typeof e!="symbol"||e instanceof Date||e instanceof Uint8Array||Array.isArray(e)&&fr(e))}function En(t){let e=n=>n==null?"NULL":Array.isArray(n)?En(n):`"${String(ze(n)).replace(/(["\\])/g,"\\$1")}"`;return`{${t.map(e).join(",")}}`}function fa(t){if(Number.isNaN(t.getTime()))throw new TypeError("an invalid Date is not a database value");return t.toISOString()}var ga={json:{to:114,from:[114,3802],serialize:t=>t,parse:t=>JSON.parse(t)},boolean:{to:16,from:[16],serialize:t=>t,parse:t=>t==="t"},date:{to:1184,from:[1082,1114,1184],serialize:t=>t,parse:t=>new Date(t)},bytea:{to:17,from:[17],serialize:t=>t,parse:t=>Buffer.from(t.slice(2),"hex")}};function ha(t){let e=t;return e.length>0&&Array.isArray(e[e.length-1])?e[e.length-1]:e}var gr=/^[A-Za-z0-9:_-]{1,80}$/;function hr(t,e){let n=new Array(e.length).fill(!1),s="",r=0,o=0;for(;o<t.length;){let a=t.charCodeAt(o);if(a===39||a===34){let i=o+1;for(;;){if(i>=t.length||t.charCodeAt(i)===92)return;if(t.charCodeAt(i)===a){if(t.charCodeAt(i+1)!==a)break;i+=2}else i++}o=i+1;continue}if(a===36){let i=o+1,c=0;for(;i<t.length&&t.charCodeAt(i)>=48&&t.charCodeAt(i)<=57&&i-o<=6;)c=c*10+(t.charCodeAt(i++)-48);if(i===o+1||c<1||c>e.length||n[c-1])return;n[c-1]=!0;let l=e[c-1];if(l!==null&&typeof l!="string"||l!==null&&l.includes("\0"))return;s+=t.slice(r,o)+(l===null?"NULL":de(l)),r=o=i;continue}if(a===59||a===92||a===45&&t.charCodeAt(o+1)===45||a===47&&t.charCodeAt(o+1)===42)return;o++}for(let a of n)if(!a)return;return s+t.slice(r)}function mr(t){return On(t)?Object.assign(new Error("current transaction is aborted, commands ignored until end of transaction block",{cause:t}),{errno:"25P02",code:"ERR_POSTGRES_SERVER_ERROR"}):t}var et=10,nt=2;function Er(t,e){let n={unsafe:(s,r)=>n.begin(o=>Promise.resolve(o.unsafe(s,r))),async begin(s,r){if(r?.tag!==void 0&&!gr.test(r.tag))throw new Error(`palgres: an opening's tag is letters, digits, ":", "_" and "-"`);let o=`${r?.tag===void 0?"":`/* ${r.tag} */ `}BEGIN${r===void 0?"":`; ${r.sql}`}`,a=await t.reserve(),i=!0,c=!1,l=!1,u=null,g,p=!1,h=0,m=d=>(u=(async()=>{for(let T=1;;T++){c=!0;try{let E=await a.unsafe(d===void 0?o:`${o}; ${d}`).simple();if((E.length>0&&Array.isArray(E[0])?E[0]:E).command!=="BEGIN")throw new Error("palgres: the round trip that opens the transaction did not begin one");return r?.opened?.(E[E.length-(d===void 0?1:2)]),l=!0,d===void 0?void 0:E[E.length-1]}catch(E){let w=Es(E);w&&(t.discard!==void 0?t.discard(a):Promise.resolve(a.close({timeout:0})).catch(()=>{}),i=!1,c=!1);let O=w?"unusable":ue(E);if(O===void 0||T>=(O==="never"?nt:et))throw g={error:E},E;w||(await Promise.resolve(a.close()).catch(()=>{}),a.release(),i=!1,c=!1);try{a=await t.reserve()}catch(k){throw g={error:k},k}i=!0}}})(),u.catch(()=>{}),u),y=()=>(u===null&&m(),u.then(()=>{},()=>{throw mr(g.error)})),S={unsafe(d,T,E){if(g!==void 0)return Promise.reject(mr(g.error));if(l)return a.unsafe(d,T);if(u===null&&E?.inline===!0){let w=hr(d,T??[]);if(w!==void 0)return m(w)}return y().then(()=>a.unsafe(d,T))},async savepoint(d){await y();let T=`palgres_sp_${++h}`;await a.unsafe(`SAVEPOINT ${T}`).simple();let E;try{E=await d(S)}catch(w){throw await Promise.resolve(a.unsafe(`ROLLBACK TO SAVEPOINT ${T}; RELEASE SAVEPOINT ${T}`).simple()).catch(()=>{}),w}return await a.unsafe(`RELEASE SAVEPOINT ${T}`).simple(),E},abandon(){p||(p=!0,Promise.resolve(a.close({timeout:0})).catch(()=>{}))}},f=async d=>{let T=await a.unsafe(d).simple();if(c=!1,d==="COMMIT"&&T.command==="ROLLBACK")throw new Error("palgres: COMMIT was answered with ROLLBACK \u2014 a statement of the transaction had failed")};try{let d;try{if(d=await s(S),await u?.catch(()=>{}),g!==void 0)throw g.error}catch(T){throw await u?.catch(()=>{}),c&&!p&&await f("ROLLBACK").catch(()=>{}),T}if(!c)return d;try{await f("COMMIT")}catch(T){throw c&&await f("ROLLBACK").catch(()=>{}),T}return d}finally{i&&(c&&!p&&await Promise.resolve(a.close()).catch(()=>{}),a.release())}}};if(e!==void 0){let s=Er(e);n.cancelBackend=Ut(s),n.cancelOpening=yr(s)}return n}var Ft=1e3;async function wr(t,e){let n;try{await Promise.race([Promise.resolve(t.close({timeout:e})),new Promise(s=>{n=setTimeout(s,e*1e3+Ft)})])}finally{clearTimeout(n)}}function Ea(t){let e=t(),n=0,s=!1,r=new WeakMap,o=new Map;return{get current(){return e},get renewals(){return n},unsafe:(a,i)=>e.unsafe(a,i),async reserve(){let a=e,i=await a.reserve();return r.set(i,a),i},discard(a){let i=r.get(a);if(i===e&&!s){e=t(),n++;let c=Promise.resolve(i.close()).catch(()=>{});o.set(i,c),c.then(()=>o.delete(i))}a.release()},async close(a){s=!0;let i=a?.timeout??0,c;try{await Promise.all([wr(e,i),Promise.race([Promise.all(o.values()),new Promise(l=>{c=setTimeout(l,i*1e3+Ft)})])])}finally{clearTimeout(c)}}}}function yr(t){return async e=>{if(!gr.test(e))throw new Error(`palgres: an opening's tag is letters, digits, ":", "_" and "-"`);let n=`/* ${e} */`;return(await t.unsafe(`SELECT pg_cancel_backend(pid) AS cancelled FROM pg_stat_activity
300
311
  WHERE state = 'active' AND left(query, $1::integer) = $2
301
- AND usename = session_user AND datname = current_database() AND backend_type = 'client backend'`,[String(n.length),n])).length>0}}function Dt(t){return async(e,n,s)=>{await t.unsafe(`SELECT pg_cancel_backend(pid) FROM pg_stat_activity
312
+ AND usename = session_user AND datname = current_database() AND backend_type = 'client backend'`,[String(n.length),n])).length>0}}function Ut(t){return async(e,n,s)=>{await t.unsafe(`SELECT pg_cancel_backend(pid) FROM pg_stat_activity
302
313
  WHERE pid = $1::integer AND backend_start = $2::timestamptz AND xact_start = $3::timestamptz
303
- AND usename = session_user AND datname = current_database() AND backend_type = 'client backend'`,[String(e),n,s])}}var Le=class extends Error{constructor(e){super(`palgres: the driver ${e}. The engine sends every parameter as the text Postgres reads, and the driver must send it as it is \u2014 build Bun.SQL with { prepare: false } (Bun 1.4.2 or later) through bunDriver(), postgres.js with { types: POSTGRES_JS_TYPES } through postgresJsDriver()`),this.name="DriverRefused"}};async function Pt(t){let n=(await t.unsafe("SELECT $1::jsonb AS j, $2::boolean AS b, $3::timestamptz = '2026-01-02 03:04:05.123456+00'::timestamptz AS t, array_length($4::int[], 1)::int AS a, $5::bytea = '\\x0102'::bytea AS y",['{"palgres":[1]}',"true","2026-01-02T03:04:05.123456Z","{1,2}","\\x0102"]))[0]??{},s=n.j;if(typeof s=="string"||!Array.isArray(s?.palgres))throw new Le("re-encodes text bound for a jsonb parameter (JSON text became a JSON string)");if(n.b!==!0)throw new Le("changes the text bound for a boolean ('true' was not read as true)");if(n.t!==!0)throw new Le("changes the text bound for a timestamp (its microseconds did not arrive)");if(Number(n.a)!==2)throw new Le("changes the text bound for an array (the literal {1,2} did not arrive)");if(n.y!==!0)throw new Le("changes the text bound for a bytea");if((await t.begin(async o=>await o.unsafe("SELECT pg_catalog.current_setting('palgres.driver_opened', true) AS v"),{sql:"SELECT pg_catalog.set_config('palgres.driver_opened', 'yes', true)"}))[0]?.v!=="yes")throw new Le("does not run the opening of a transaction (its role and its caller) before the work \u2014 every request would run as the login, no one's")}var or=()=>new Promise(()=>{});function ua(t,e){let n=a=>Promise.resolve(a).then(l=>l),s=a=>async(l,c)=>{for(let i=1;;i++){let d=!1,u,f=S=>u!==void 0?Promise.reject(u.error):n(S()).catch(h=>{throw ce(h)==="lost"&&(u??={error:h}),h}),g=(S,h)=>S.then(m=>u===void 0?m:(h(u.error),or()),m=>u===void 0?Promise.reject(m):(h(m),or())),p=S=>({unsafe:(h,m)=>f(()=>S.unsafe(h,m??[])),savepoint:h=>new Promise((m,T)=>{if(u!==void 0)return T(u.error);S.savepoint(E=>g(h(p(E)),T)).then(m,T)})}),y=a.begin(S=>g((async()=>{if(c!==void 0){let h=la(await f(()=>S.unsafe(c.sql).simple()));c.opened?.(h)}return d=!0,l(p(S))})(),()=>{}));y.catch(S=>{ce(S)==="lost"&&(u??={error:S})});try{return await y}catch(S){let h=ce(S);if(d||h===void 0||i>=(h==="lost"?Yn:Jn))throw S}}},r=s(t),o=s(e);return{unsafe:(a,l)=>r(c=>Promise.resolve(c.unsafe(a,l))),begin:r,cancelBackend:Dt({unsafe:(a,l)=>o(c=>Promise.resolve(c.unsafe(a,l)))})}}var mr=(t,e)=>({embed:t,serving:new Map,said:new Map,log:e}),qt=3e4,Ft=`SELECT set_config('hnsw.iterative_scan', 'relaxed_order', true), set_config('hnsw.ef_search', '200', true), set_config('hnsw.max_scan_tuples', '200000', true), g.gen, g.model, g.dimensions, g.metric, (SELECT n.nspname FROM pg_catalog.pg_extension e JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace WHERE e.extname = 'vector') AS types FROM (SELECT 1) one LEFT JOIN ${J}.generations g ON g.table_key = $1 AND g.status = 'active' ORDER BY g.gen DESC LIMIT 1`;function Ut(t){if(!(t===void 0||t.gen===null||t.gen===void 0||typeof t.types!="string"))return{gen:Number(t.gen),model:String(t.model),dimensions:Number(t.dimensions),metric:t.metric,types:t.types}}var da=["hybrid","text","vector"],Mt=(t,e)=>be(new Ne({fields:[{field:t,message:e}]},`search: ${t} ${e}`));function pr(t,e,n){if(n===null||typeof n!="object")throw new TypeError(`search(${t}): its options are { limit, where, mode, minScore }`);if(typeof e!="string")throw new TypeError(`search(${t}): the query is the text searched for, not ${e===null?"null":typeof e}`);let s=e.trim().slice(0,qt);if(s==="")throw Mt("query","is empty \u2014 say what to search for");let r=n.mode??"hybrid";if(typeof r!="string"||!da.includes(r))throw new TypeError(`search(${t}): mode is "hybrid", "text" or "vector", not ${typeof r=="string"?JSON.stringify(String(r).slice(0,40)):typeof r}`);let o=n.limit??20;if(typeof o!="number"||!Number.isFinite(o))throw Mt("limit","must be a number");if(n.minScore!==void 0&&(typeof n.minScore!="number"||!Number.isFinite(n.minScore)))throw Mt("minScore","must be a number");return{text:s,mode:r,limit:Math.min(Math.max(1,Math.trunc(o)),100),...n.minScore===void 0?{}:{minScore:n.minScore}}}function Bt(t,e){if(!Array.isArray(t)||t.length!==e)throw new Error(`the embedder answered ${Array.isArray(t)?`a vector of ${t.length} dimensions`:"no vector"} where ${e} were asked for`);for(let n of t)if(typeof n!="number"||!Number.isFinite(n))throw new Error("the embedder answered a vector that holds what is not a number");return`[${t.join(",")}]`}async function fr(t,e,n){let s=await t({texts:[e],model:n.model,dimensions:n.dimensions,purpose:"query"});if(s===null||typeof s!="object"||!Array.isArray(s.vectors)||s.vectors.length!==1)throw new Error("the embedder answered no vector for the one text it was given");return Bt(s.vectors[0],n.dimensions)}var gr=100,ma="2 minutes",pa=8,fa=256,ga=25e4,hr=1e3,ha=15e3,Ea=3e4,wa=5e3,le=J,zn=t=>(t instanceof Error?t.message:String(t)).slice(0,500);function ya(t){let{sql:e,embed:n,log:s}=t,r={...Xe,...t.roles},o=`SELECT set_config('role', ${ue(r.service)}, true), set_config('search_path', 'pg_catalog, pg_temp', true), set_config('datestyle', 'ISO, YMD', true), set_config('lock_timeout', '5s', true), set_config('statement_timeout', '60s', true)`,a=R=>e.begin(b=>R(async(O,L=[])=>await b.unsafe(O,L.map(Ye))),{sql:o}),l=(R,b)=>a(O=>O(R,b)),c=!0,i=R=>l(`WITH due AS (SELECT id FROM ${le}.queue WHERE next_attempt_at <= now() ORDER BY enqueued_at, id LIMIT ${R} FOR UPDATE SKIP LOCKED)
304
- UPDATE ${le}.queue q SET next_attempt_at = now() + interval '${ma}' FROM due WHERE q.id = due.id
305
- RETURNING q.id::text AS id, q.table_key, q.gen, q.row_id, q.enqueued_at::text AS enqueued_at`).then(b=>b.map(O=>({id:String(O.id),table_key:String(O.table_key),gen:Number(O.gen),row_id:String(O.row_id),enqueued_at:String(O.enqueued_at)})));async function d(R,b){let[O]=await l(`SELECT g.model, g.dimensions, g.metric, g.from_columns, t.rel::text AS parent, a.attname AS pk, format_type(a.atttypid, a.atttypmod) AS pk_type,
314
+ AND usename = session_user AND datname = current_database() AND backend_type = 'client backend'`,[String(e),n,s])}}var Ie=class extends Error{constructor(e){super(`palgres: the driver ${e}. The engine sends every parameter as the text Postgres reads, and the driver must send it as it is \u2014 build Bun.SQL with { prepare: false } (Bun 1.4.2 or later) through bunDriver(), postgres.js with { types: POSTGRES_JS_TYPES } through postgresJsDriver()`),this.name="DriverRefused"}};async function Bt(t){let n=(await t.unsafe("SELECT $1::jsonb AS j, $2::boolean AS b, $3::timestamptz = '2026-01-02 03:04:05.123456+00'::timestamptz AS t, array_length($4::int[], 1)::int AS a, $5::bytea = '\\x0102'::bytea AS y",['{"palgres":[1]}',"true","2026-01-02T03:04:05.123456Z","{1,2}","\\x0102"]))[0]??{},s=n.j;if(typeof s=="string"||!Array.isArray(s?.palgres))throw new Ie("re-encodes text bound for a jsonb parameter (JSON text became a JSON string)");if(n.b!==!0)throw new Ie("changes the text bound for a boolean ('true' was not read as true)");if(n.t!==!0)throw new Ie("changes the text bound for a timestamp (its microseconds did not arrive)");if(Number(n.a)!==2)throw new Ie("changes the text bound for an array (the literal {1,2} did not arrive)");if(n.y!==!0)throw new Ie("changes the text bound for a bytea");if((await t.begin(async o=>await o.unsafe("SELECT pg_catalog.current_setting('palgres.driver_opened', true) AS v"),{sql:"SELECT pg_catalog.set_config('palgres.driver_opened', 'yes', true)"}))[0]?.v!=="yes")throw new Ie("does not run the opening of a transaction (its role and its caller) before the work \u2014 every request would run as the login, no one's")}var pr=()=>new Promise(()=>{});function wa(t,e){let n=a=>Promise.resolve(a).then(i=>i),s=a=>async(i,c)=>{for(let l=1;;l++){let u=!1,g,p=S=>g!==void 0?Promise.reject(g.error):n(S()).catch(f=>{throw ue(f)==="lost"&&(g??={error:f}),f}),h=(S,f)=>S.then(d=>g===void 0?d:(f(g.error),pr()),d=>g===void 0?Promise.reject(d):(f(d),pr())),m=S=>({unsafe:(f,d)=>p(()=>S.unsafe(f,d??[])),savepoint:f=>new Promise((d,T)=>{if(g!==void 0)return T(g.error);S.savepoint(E=>h(f(m(E)),T)).then(d,T)})}),y=a.begin(S=>h((async()=>{if(c!==void 0){let f=ha(await p(()=>S.unsafe(c.sql).simple()));c.opened?.(f)}return u=!0,i(m(S))})(),()=>{}));y.catch(S=>{ue(S)==="lost"&&(g??={error:S})});try{return await y}catch(S){let f=ue(S);if(u||f===void 0||l>=(f==="lost"?et:nt))throw S}}},r=s(t),o=s(e);return{unsafe:(a,i)=>r(c=>Promise.resolve(c.unsafe(a,i))),begin:r,cancelBackend:Ut({unsafe:(a,i)=>o(c=>Promise.resolve(c.unsafe(a,i)))})}}var Tr=(t,e)=>({embed:t,serving:new Map,said:new Map,log:e}),jt=3e4,Kt=`SELECT set_config('hnsw.iterative_scan', 'relaxed_order', true), set_config('hnsw.ef_search', '200', true), set_config('hnsw.max_scan_tuples', '200000', true), g.gen, g.model, g.dimensions, g.metric, (SELECT n.nspname FROM pg_catalog.pg_extension e JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace WHERE e.extname = 'vector') AS types FROM (SELECT 1) one LEFT JOIN ${J}.generations g ON g.table_key = $1 AND g.status = 'active' ORDER BY g.gen DESC LIMIT 1`;function Ht(t){if(!(t===void 0||t.gen===null||t.gen===void 0||typeof t.types!="string"))return{gen:Number(t.gen),model:String(t.model),dimensions:Number(t.dimensions),metric:t.metric,types:t.types}}var ya=["hybrid","text","vector"],Wt=(t,e)=>Ne(new Ae({fields:[{field:t,message:e}]},`search: ${t} ${e}`));function $r(t,e,n){if(n===null||typeof n!="object")throw new TypeError(`search(${t}): its options are { limit, where, mode, minScore }`);if(typeof e!="string")throw new TypeError(`search(${t}): the query is the text searched for, not ${e===null?"null":typeof e}`);let s=e.trim().slice(0,jt);if(s==="")throw Wt("query","is empty \u2014 say what to search for");let r=n.mode??"hybrid";if(typeof r!="string"||!ya.includes(r))throw new TypeError(`search(${t}): mode is "hybrid", "text" or "vector", not ${typeof r=="string"?JSON.stringify(String(r).slice(0,40)):typeof r}`);let o=n.limit??20;if(typeof o!="number"||!Number.isFinite(o))throw Wt("limit","must be a number");if(n.minScore!==void 0&&(typeof n.minScore!="number"||!Number.isFinite(n.minScore)))throw Wt("minScore","must be a number");return{text:s,mode:r,limit:Math.min(Math.max(1,Math.trunc(o)),100),...n.minScore===void 0?{}:{minScore:n.minScore}}}function Vt(t,e){if(!Array.isArray(t)||t.length!==e)throw new Error(`the embedder answered ${Array.isArray(t)?`a vector of ${t.length} dimensions`:"no vector"} where ${e} were asked for`);for(let n of t)if(typeof n!="number"||!Number.isFinite(n))throw new Error("the embedder answered a vector that holds what is not a number");return`[${t.join(",")}]`}async function Sr(t,e,n){let s=await t({texts:[e],model:n.model,dimensions:n.dimensions,purpose:"query"});if(s===null||typeof s!="object"||!Array.isArray(s.vectors)||s.vectors.length!==1)throw new Error("the embedder answered no vector for the one text it was given");return Vt(s.vectors[0],n.dimensions)}var Rr=100,Ta="2 minutes",$a=8,Sa=256,Ra=25e4,br=1e3,ba=15e3,Na=3e4,Aa=5e3,le=J,tt=t=>(t instanceof Error?t.message:String(t)).slice(0,500);function Oa(t){let{sql:e,embed:n,log:s}=t,r={...Je,...t.roles},o=`SELECT set_config('role', ${de(r.service)}, true), set_config('search_path', 'pg_catalog, pg_temp', true), set_config('datestyle', 'ISO, YMD', true), set_config('lock_timeout', '5s', true), set_config('statement_timeout', '60s', true)`,a=N=>e.begin(A=>N(async(R,L=[])=>await A.unsafe(R,L.map(ze))),{sql:o}),i=(N,A)=>a(R=>R(N,A)),c=!0,l=N=>i(`WITH due AS (SELECT id FROM ${le}.queue WHERE next_attempt_at <= now() ORDER BY enqueued_at, id LIMIT ${N} FOR UPDATE SKIP LOCKED)
315
+ UPDATE ${le}.queue q SET next_attempt_at = now() + interval '${Ta}' FROM due WHERE q.id = due.id
316
+ RETURNING q.id::text AS id, q.table_key, q.gen, q.row_id, q.enqueued_at::text AS enqueued_at`).then(A=>A.map(R=>({id:String(R.id),table_key:String(R.table_key),gen:Number(R.gen),row_id:String(R.row_id),enqueued_at:String(R.enqueued_at)})));async function u(N,A){let[R]=await i(`SELECT g.model, g.dimensions, g.metric, g.from_columns, t.rel::text AS parent, a.attname AS pk, format_type(a.atttypid, a.atttypmod) AS pk_type,
306
317
  (SELECT n.nspname FROM pg_extension e JOIN pg_namespace n ON n.oid = e.extnamespace WHERE e.extname = 'vector') AS types
307
318
  FROM ${le}.generations g
308
319
  CROSS JOIN LATERAL (SELECT to_regclass(format('%I.%I', split_part(g.table_key, '.', 1), substr(g.table_key, strpos(g.table_key, '.') + 1))) AS rel) t
309
320
  JOIN pg_index i ON i.indrelid = t.rel AND i.indisprimary AND i.indnkeyatts = 1
310
321
  JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = i.indkey[0]
311
- WHERE g.table_key = $1 AND g.gen = $2::integer AND g.status <> 'retired'`,[R,b]);if(O===void 0||typeof O.types!="string")return;let L=Number(O.dimensions),$=O.metric;return{model:String(O.model),dimensions:L,metric:$,from:O.from_columns.map(String),parent:String(O.parent),pk:N(String(O.pk)),pkType:String(O.pk_type),vectors:`${le}.${N(Ie(R,b))}`,vectorType:`${N(O.types)}.${an(L,$).type}`}}let u="unnest($1::bigint[], $2::timestamptz[]) AS c(id, at) WHERE q.id = c.id AND q.enqueued_at = c.at",f=R=>[R.map(b=>b.id),R.map(b=>b.enqueued_at)],g=R=>l(`DELETE FROM ${le}.queue q USING ${u}`,f(R)),p=async(R,b)=>{R.length!==0&&(s?.(`palgres: search of ${R[0].table_key}: ${R.length} row(s) not embedded, tried again later (${zn(b)})`),await l(`UPDATE ${le}.queue q SET attempts = q.attempts + 1, last_error = $3,
312
- next_attempt_at = CASE WHEN q.attempts + 1 >= ${pa} THEN 'infinity'::timestamptz ELSE now() + interval '1 minute' * power(4, least(q.attempts, 4)) END
313
- FROM ${u}`,[...f(R),zn(b)]))};async function y(R,b,O,L){let $=[],_=[],M=0;for(let v of b){let B=Math.ceil(v.text.length/4);_.length>0&&(_.length>=fa||M+B>ga)&&($.push(_),_=[],M=0),_.push(v),M+=B}_.length>0&&$.push(_);let U=(v,B)=>(L.failed+=v.length,p(v.map(q=>q.row),B)),x=async v=>{if(v.length===0)return;let B=Math.ceil(v.length/2);await I(v.slice(0,B)),B<v.length&&await I(v.slice(B))},I=async v=>{if(L.down)return U(v,new Error("the embedder is not answering"));let B,q;try{let W=await n({texts:v.map(G=>G.text),model:R.model,dimensions:R.dimensions,purpose:"document"});if(W===null||typeof W!="object"||!Array.isArray(W.vectors)||W.vectors.length!==v.length)throw new Error(`the embedder answered ${Array.isArray(W?.vectors)?W.vectors.length:"no"} vector(s) for ${v.length} text(s)`);B=W.vectors.map(G=>Bt(G,R.dimensions)),q=typeof W.model=="string"&&W.model!==""?W.model:R.model}catch(W){return v.length===1?U(v,W):L.answered?x(v):(await I([v[0]]),await I([v[v.length-1]]),L.answered?x(v.slice(1,-1)):(L.down=!0,U(v.slice(1,-1),W)))}L.answered=!0,await O(v,q,B)};for(let v of $)await I(v)}async function S(R,b){let O=await d(R[0].table_key,R[0].gen);if(O===void 0)return await g(R),0;let L=new Map((await l(`SELECT p.${O.pk}::text AS id, ${Pn(O.from,"p")} AS text FROM ${O.parent} p WHERE p.${O.pk} = ANY($1::${O.pkType}[])`,[R.map(x=>x.row_id)])).map(x=>[String(x.id),String(x.text??"")])),$=R.filter(x=>!L.has(x.row_id));$.length>0&&await g($);let _=R.filter(x=>L.has(x.row_id)&&L.get(x.row_id).trim()==="");_.length>0&&await l(`WITH done AS (DELETE FROM ${le}.queue q USING ${u} RETURNING q.row_id) DELETE FROM ${O.vectors} v USING done d WHERE v.id = d.row_id::${O.pkType}`,f(_));let M=R.filter(x=>(L.get(x.row_id)??"").trim()!=="").map(x=>({row:x,text:L.get(x.row_id).slice(0,qt)})),U=0;return await y(O,M,async(x,I,v)=>{try{let[B]=await l(`WITH done AS (DELETE FROM ${le}.queue q USING ${u} RETURNING q.row_id),
314
- there AS (SELECT p.${O.pk}::text AS id FROM ${O.parent} p WHERE p.${O.pk} = ANY($3::${O.pkType}[]) FOR KEY SHARE),
322
+ WHERE g.table_key = $1 AND g.gen = $2::integer AND g.status <> 'retired'`,[N,A]);if(R===void 0||typeof R.types!="string")return;let L=Number(R.dimensions),$=R.metric;return{model:String(R.model),dimensions:L,metric:$,from:R.from_columns.map(String),parent:String(R.parent),pk:b(String(R.pk)),pkType:String(R.pk_type),vectors:`${le}.${b(ve(N,A))}`,vectorType:`${b(R.types)}.${cn(L,$).type}`}}let g="unnest($1::bigint[], $2::timestamptz[]) AS c(id, at) WHERE q.id = c.id AND q.enqueued_at = c.at",p=N=>[N.map(A=>A.id),N.map(A=>A.enqueued_at)],h=N=>i(`DELETE FROM ${le}.queue q USING ${g}`,p(N)),m=async(N,A)=>{N.length!==0&&(s?.(`palgres: search of ${N[0].table_key}: ${N.length} row(s) not embedded, tried again later (${tt(A)})`),await i(`UPDATE ${le}.queue q SET attempts = q.attempts + 1, last_error = $3,
323
+ next_attempt_at = CASE WHEN q.attempts + 1 >= ${$a} THEN 'infinity'::timestamptz ELSE now() + interval '1 minute' * power(4, least(q.attempts, 4)) END
324
+ FROM ${g}`,[...p(N),tt(A)]))};async function y(N,A,R,L){let $=[],C=[],q=0;for(let x of A){let B=Math.ceil(x.text.length/4);C.length>0&&(C.length>=Sa||q+B>Ra)&&($.push(C),C=[],q=0),C.push(x),q+=B}C.length>0&&$.push(C);let U=(x,B)=>(L.failed+=x.length,m(x.map(F=>F.row),B)),v=async x=>{if(x.length===0)return;let B=Math.ceil(x.length/2);await I(x.slice(0,B)),B<x.length&&await I(x.slice(B))},I=async x=>{if(L.down)return U(x,new Error("the embedder is not answering"));let B,F;try{let W=await n({texts:x.map(V=>V.text),model:N.model,dimensions:N.dimensions,purpose:"document"});if(W===null||typeof W!="object"||!Array.isArray(W.vectors)||W.vectors.length!==x.length)throw new Error(`the embedder answered ${Array.isArray(W?.vectors)?W.vectors.length:"no"} vector(s) for ${x.length} text(s)`);B=W.vectors.map(V=>Vt(V,N.dimensions)),F=typeof W.model=="string"&&W.model!==""?W.model:N.model}catch(W){return x.length===1?U(x,W):L.answered?v(x):(await I([x[0]]),await I([x[x.length-1]]),L.answered?v(x.slice(1,-1)):(L.down=!0,U(x.slice(1,-1),W)))}L.answered=!0,await R(x,F,B)};for(let x of $)await I(x)}async function S(N,A){let R=await u(N[0].table_key,N[0].gen);if(R===void 0)return await h(N),0;let L=new Map((await i(`SELECT p.${R.pk}::text AS id, ${qn(R.from,"p")} AS text FROM ${R.parent} p WHERE p.${R.pk} = ANY($1::${R.pkType}[])`,[N.map(v=>v.row_id)])).map(v=>[String(v.id),String(v.text??"")])),$=N.filter(v=>!L.has(v.row_id));$.length>0&&await h($);let C=N.filter(v=>L.has(v.row_id)&&L.get(v.row_id).trim()==="");C.length>0&&await i(`WITH done AS (DELETE FROM ${le}.queue q USING ${g} RETURNING q.row_id) DELETE FROM ${R.vectors} v USING done d WHERE v.id = d.row_id::${R.pkType}`,p(C));let q=N.filter(v=>(L.get(v.row_id)??"").trim()!=="").map(v=>({row:v,text:L.get(v.row_id).slice(0,jt)})),U=0;return await y(R,q,async(v,I,x)=>{try{let[B]=await i(`WITH done AS (DELETE FROM ${le}.queue q USING ${g} RETURNING q.row_id),
325
+ there AS (SELECT p.${R.pk}::text AS id FROM ${R.parent} p WHERE p.${R.pk} = ANY($3::${R.pkType}[]) FOR KEY SHARE),
315
326
  written AS (
316
- INSERT INTO ${O.vectors} (id, embedding, model)
317
- SELECT s.id::${O.pkType}, s.embedding::${O.vectorType}, $5::text
327
+ INSERT INTO ${R.vectors} (id, embedding, model)
328
+ SELECT s.id::${R.pkType}, s.embedding::${R.vectorType}, $5::text
318
329
  FROM unnest($6::text[], $4::text[]) AS s(id, embedding) JOIN done d ON d.row_id = s.id JOIN there ON there.id = s.id
319
330
  ON CONFLICT (id) DO UPDATE SET embedding = EXCLUDED.embedding, model = EXCLUDED.model, embedded_at = now()
320
331
  RETURNING 1)
321
- SELECT count(*)::int AS n FROM written`,[...f(x.map(q=>q.row)),x.map(q=>q.row.row_id),v,I,x.map(q=>q.row.row_id)]);U+=Number(B?.n??0)}catch(B){b.failed+=x.length,await p(x.map(q=>q.row),B)}},b),U}async function h(R){let b=0,O=await l(`SELECT g.table_key, g.gen FROM ${le}.generations g WHERE g.status = 'filling' AND NOT EXISTS (SELECT 1 FROM ${le}.queue q WHERE q.table_key = g.table_key AND q.gen = g.gen) ORDER BY 1, 2`);for(let L of O){let $=String(L.table_key),_=Number(L.gen),M=await d($,_);if(M===void 0)continue;let[U]=await l(`WITH missing AS (
332
+ SELECT count(*)::int AS n FROM written`,[...p(v.map(F=>F.row)),v.map(F=>F.row.row_id),x,I,v.map(F=>F.row.row_id)]);U+=Number(B?.n??0)}catch(B){A.failed+=v.length,await m(v.map(F=>F.row),B)}},A),U}async function f(N){let A=0,R=await i(`SELECT g.table_key, g.gen FROM ${le}.generations g WHERE g.status = 'filling' AND NOT EXISTS (SELECT 1 FROM ${le}.queue q WHERE q.table_key = g.table_key AND q.gen = g.gen) ORDER BY 1, 2`);for(let L of R){let $=String(L.table_key),C=Number(L.gen),q=await u($,C);if(q===void 0)continue;let[U]=await i(`WITH missing AS (
322
333
  INSERT INTO ${le}.queue (table_key, gen, row_id)
323
- SELECT $1::text, $2::integer, p.${M.pk}::text FROM ${M.parent} p
324
- WHERE btrim(${Pn(M.from,"p")}) <> '' AND NOT EXISTS (SELECT 1 FROM ${M.vectors} v WHERE v.id = p.${M.pk})
334
+ SELECT $1::text, $2::integer, p.${q.pk}::text FROM ${q.parent} p
335
+ WHERE btrim(${qn(q.from,"p")}) <> '' AND NOT EXISTS (SELECT 1 FROM ${q.vectors} v WHERE v.id = p.${q.pk})
325
336
  ON CONFLICT ON CONSTRAINT queue_one_pending DO NOTHING RETURNING 1)
326
- SELECT count(*)::int AS n FROM missing`,[$,_]);if(Number(U?.n??0)>0){b+=Number(U.n);continue}let[x]=await l(`SELECT ${le}.promote($1) AS gen`,[$]);x?.gen!==null&&x?.gen!==void 0&&(R.push($),s?.(`palgres: search of ${$}: every row is embedded again \u2014 generation ${String(x.gen)} serves`))}return b}async function m(R={}){let b=R.timeoutMs===void 0?1/0:Date.now()+R.timeoutMs,O=[],L=0,$=()=>Date.now()<b&&(R.max===void 0||L<R.max)&&!(R.whileRunning===!0&&c);for(;;){for(;$();){let _=await i(R.max===void 0?gr:Math.min(gr,R.max-L));if(_.length===0)break;let M=new Map;for(let I of _){let v=`${I.table_key}\0${I.gen}`,B=M.get(v);B?B.push(I):M.set(v,[I])}let U={answered:!1,down:!1,failed:0},x=0;for(let I of M.values())try{x+=await S(I,U)}catch(v){U.failed+=I.length,await p(I,v).catch(()=>{})}if(L+=x,x===0&&U.failed>0)return{embedded:L,promoted:O}}if(!$()||await h(O)===0)break}return{embedded:L,promoted:O}}let T,E,w=!1,A,k=()=>{w=!0,E?.()};return{drain:R=>m(R),kick:k,start(){T===void 0&&(c=!1,A=Bn.listen(k),T=(async()=>{await l(`UPDATE ${le}.queue SET attempts = 0, next_attempt_at = now(), last_error = NULL WHERE next_attempt_at = 'infinity'`).catch(b=>s?.(`palgres: the search worker could not read its queue (${zn(b)})`));let R=hr;for(;!c;){w=!1;let b=!1;try{let O=await m({timeoutMs:Ea,whileRunning:!0});b=O.embedded>0||O.promoted.length>0}catch(O){s?.(`palgres: the search worker could not embed (${zn(O)}); it looks again`)}R=b?hr:Math.min(ha,R*2),!(c||w)&&(await new Promise(O=>{let L=setTimeout(O,R);L.unref?.(),E=()=>{clearTimeout(L),O()}}),E=void 0)}})())},async stop(){if(T===void 0)return;c=!0,A?.(),A=void 0,E?.();let R=T;T=void 0;let b;await Promise.race([R,new Promise(O=>b=setTimeout(O,wa))]),clearTimeout(b)}}}var Wt=class{options;active=0;queued=0;tenants=new Map;pending=new Map;lastTenant;accepted=0;rejected={full:0,timeout:0,cancelled:0};limit;timeout;nestedLimit;nestedActive=0;nestedPending=new Set;constructor(e){if(this.options=e,this.limit=e.queueLimit??Math.min(512,Math.max(32,e.concurrency*8)),this.timeout=e.queueTimeoutMs??1e3,this.nestedLimit=e.nestedConcurrency??e.concurrency*2,!Number.isSafeInteger(e.concurrency)||e.concurrency<1||!Number.isSafeInteger(this.nestedLimit)||this.nestedLimit<1||!Number.isSafeInteger(this.limit)||this.limit<0||this.limit>1e4||!Number.isSafeInteger(this.timeout)||this.timeout<1||this.timeout>6e4)throw new Error("Invalid database pool or queue deployment settings")}get queueTimeoutMs(){return this.timeout}get snapshot(){return{active:this.active,queued:this.queued,nested:{active:this.nestedActive,queued:this.nestedPending.size},tenants:this.tenants.size,accepted:this.accepted,rejected:{...this.rejected}}}nestedLease(e){this.nestedActive++;let n=!1;return{queueMs:performance.now()-e,release:()=>{if(n)return;n=!0,this.nestedActive--;let s=this.nestedPending.values().next().value;s&&(this.nestedPending.delete(s),s.cleanup(),s.grant(this.nestedLease(s.entered)))}}}acquireNested(e){let n=performance.now();return this.nestedActive<this.nestedLimit?Promise.resolve(this.nestedLease(n)):new Promise((s,r)=>{let o={grant:s,reject:r,entered:n,cleanup(){}},a=(i,d)=>{this.nestedPending.delete(o)&&(o.cleanup(),this.rejection(i),r(d))},l=()=>a("cancelled",e?.reason??new Error("Database admission cancelled")),c=setTimeout(()=>a("timeout",new Pe({retryAfter:1},"Database queue deadline exceeded")),this.timeout);o.cleanup=()=>{clearTimeout(c),e?.removeEventListener("abort",l)},this.nestedPending.add(o),e?.addEventListener("abort",l,{once:!0}),e?.aborted&&l()})}rejection(e){this.rejected[e]++;try{this.options.onReject?.({reason:e,active:this.active,queued:this.queued})}catch{}}take(e,n){this.active++,this.accepted++,this.lastTenant=e,this.tenants.set(e,(this.tenants.get(e)??0)+1);let s=!1;return{queueMs:performance.now()-n,release:()=>{if(s)return;s=!0,this.active--;let r=(this.tenants.get(e)??1)-1;r?this.tenants.set(e,r):this.tenants.delete(e),this.drain()}}}drain(){for(;this.active<this.options.concurrency&&this.queued;){let e=this.pending.keys().next().value;if(this.pending.size>1&&e===this.lastTenant){let r=this.pending.get(e);this.pending.delete(e),this.pending.set(e,r),e=this.pending.keys().next().value}let n=this.pending.get(e),s=n.values().next().value;n.delete(s),this.pending.delete(e),n.size&&this.pending.set(e,n),this.queued--,s.cleanup(),s.grant(this.take(e,s.entered))}}async acquire(e,n,s={}){if(n?.aborted)throw this.rejection("cancelled"),n.reason??new Error("Database admission cancelled");if(s.nested)return this.acquireNested(n);let r=this.options.tenantClaim?e[this.options.tenantClaim]:e.sub,o=typeof r=="string"&&r.length?r:"__anonymous__",a=performance.now();if(this.active<this.options.concurrency&&!this.queued)return this.take(o,a);if(!s.returning&&this.queued>=this.limit)throw this.rejection("full"),new Pe({retryAfter:1},"Database queue is full");return new Promise((l,c)=>{let i=this.pending.get(o)??new Set;this.pending.has(o)||this.pending.set(o,i);let d={grant:l,reject:c,entered:a,cleanup(){}},u=(p,y)=>{i.delete(d)&&(i.size||this.pending.delete(o),this.queued--,d.cleanup(),this.rejection(p),c(y),this.drain())},f=()=>u("cancelled",n?.reason??new Error("Database admission cancelled")),g=setTimeout(()=>u("timeout",new Pe({retryAfter:1},"Database queue deadline exceeded")),this.timeout);if(d.cleanup=()=>{clearTimeout(g),n?.removeEventListener("abort",f)},s.returning){d.returning=!0;let p=[...i];i.clear();for(let y of p)y.returning&&i.add(y);i.add(d);for(let y of p)y.returning||i.add(y)}else i.add(d);this.queued++,n?.addEventListener("abort",f,{once:!0}),n?.aborted?f():this.drain()})}},Er=new WeakMap;function wr(t,e={concurrency:10}){let n=Er.get(t);if(!n)return n=new Wt(e),Er.set(t,n),n;for(let s of["concurrency","queueLimit","queueTimeoutMs","tenantClaim"])if(e[s]!==n.options[s])throw new Error("Database admission cannot be reconfigured while its pool is shared");return n}function Ta(t){return t.endsWith("_id")?t.slice(0,-3):t}function yr(t){return t.direction==="forward"?`the foreign key "${t.table}.${t.column}"`:`the reverse of "${t.table}.${t.column}"`}function Tr(t){return t.direction==="reverse"?'references(() => \u2026, { reverseAs: "\u2026" })':t.def.selfRefColumn!==void 0?'selfReferences("\u2026", { as: "\u2026" })':'references(() => \u2026, { as: "\u2026" })'}function hn(t){let e=new Map,n=new Map;for(let r of t)for(let o of Object.values(r.tables)){let a=he(r.name,o.name);e.set(a,[]),n.set(a,new Map)}let s=(r,o,a,l)=>{let c=n.get(r);if(c===void 0)return;let i=c.get(o);if(i!==void 0){let d=Tr(i),u=Tr(a),f=d===u?`rename one of them: ${d}`:`rename one of them \u2014 "${i.table}.${i.column}": ${d}; "${a.table}.${a.column}": ${u}`;throw new _e([`table "${r}": ${yr(i)} and ${yr(a)} both resolve to the relation name "${o}" \u2014 ${f}`])}c.set(o,a),e.get(r)?.push(l)};for(let r of t)for(let o of Object.values(r.tables)){let a=he(r.name,o.name);for(let[l,c]of Object.entries(o.columns)){let i=c._def;if(i.references===void 0)continue;let d=i.references.table,u=i.refAs??Ta(l);if(s(a,u,{table:o.name,column:l,direction:"forward",def:i},{name:u,to:d,kind:"one",via:l}),!e.has(d)||d===a)continue;let f=i.reverseAs??o.name;s(d,f,{table:o.name,column:l,direction:"reverse",def:i},{name:f,to:a,kind:"many",via:l})}}return e}function $a(t){let e=t.columns??{},n=[],s=[];for(let[a,l]of Object.entries(e)){let c=l&&typeof l=="object"&&"_def"in l?l._def:l;c?.primaryKey&&n.push(a),c?.unique&&s.push([a])}let r=t.primaryKey??n;r.length&&s.unshift([...r]);for(let a of t.unique??[])s.push([...a.columns]);let o=new Set;return s.filter(a=>{if(!a.length||new Set(a).size!==a.length||a.some(c=>!Object.hasOwn(e,c)))throw new Error("Unique lookup requires a nonempty declared key of distinct, known columns");let l=JSON.stringify([...a].sort());return o.has(l)?!1:(o.add(l),!0)})}function Qn(t){let e=Object.fromEntries(Object.entries(t.columns).filter(([,n])=>n?._def?.ignored!==!0));return $a({columns:e,primaryKey:t.primaryKey,unique:t.unique})}function $r(t,e,n){if(!n||typeof n!="object"||Array.isArray(n))throw new Error(`findUnique(${t}): where must contain one declared unique key`);let s=Object.keys(n);if(!e.some(r=>r.length===s.length&&r.every(o=>Object.hasOwn(n,o))))throw new Error(`findUnique(${t}): supply exactly one declared unique key (${e.map(r=>r.join(" + ")).join("; ")||"none declared"}); use findMany for other filters`);for(let[r,o]of Object.entries(n))if(o==null||typeof o=="object"&&!(o instanceof Date)||typeof o=="function"||typeof o=="symbol")throw new Error(`findUnique(${t}): ${r} requires a non-null scalar equality value`)}var C=t=>`"${t.replace(/"/g,'""')}"`;function Nr(t){return t.type==="sql"&&typeof t.sqlType=="string"?`sql:${t.sqlType}`:t.type}var Zn=class{version;tables=new Map;ignoredTables=new Set;constructor(e,n){this.version=n.version;let s=hn(e);for(let r of e)for(let o of Object.values(r.tables)){let a=he(r.name,o.name);if(o.ignored){this.ignoredTables.add(a);continue}this.tables.set(a,Sa(r.name,o,a,n,s.get(a)??[]))}}table(e,n){let s=this.tables.get(e);if(s)return s;throw this.ignoredTables.has(e)?new Error(`${n}(${e}): the table is .ignored() in this version \u2014 the version has promised not to name it, so the next one can drop it`):new Error(`${n}(${e}): no such table in this version's schema (a table outside public is "schema.table")`)}};function Sa(t,e,n,s,r){let o=new Map,a=new Set,l=[],c=new Map,i=new Set;for(let[u,f]of Object.entries(e.columns)){let g=f._def;if(g.ignored){a.add(u);continue}if(o.set(u,g),g.primaryKey&&l.push(u),typeof g.codec=="string"&&g.codec in He){let p=He[g.codec];c.set(u,{fromDb:p.fromDb,toDb:p.toDb})}else g.transform!==void 0&&c.set(u,g.transform);(g.identity==="always"||g.generated!==void 0)&&i.add(u)}let d=e.primaryKey??l;return{key:n,schema:t,name:e.name,sql:t==="public"?C(e.name):`${C(s.schema(t))}.${C(e.name)}`,columns:o,ignored:a,primaryKey:d,singleKey:d.length===1?d[0]:null,uniqueKeys:Qn(e),transforms:c,databaseOnly:i,relations:r,projections:new Map,...e.search!==void 0?{search:{key:`${t}.${e.name}`,from:[...e.search.from]}}:{}}}function Sr(t){return`CASE WHEN isfinite(${t}) AND extract(year FROM ${t} AT TIME ZONE 'UTC') BETWEEN 1 AND 9999 THEN to_char(${t} AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') ELSE ${t}::text END`}function Rr(t){return`CASE WHEN isfinite(${t}) AND extract(year FROM ${t}) BETWEEN 1 AND 9999 THEN to_char(${t}, 'YYYY-MM-DD') ELSE ${t}::text END`}function br(t,e){return`CASE WHEN ${t} IS NULL THEN NULL ELSE ARRAY(SELECT ${e("u.e")} FROM unnest(${t}) WITH ORDINALITY AS u(e, i) ORDER BY u.i) END`}function Ue(t,e){if(t!==null&&t.startsWith("sql:")){let n=t.slice(4);return An.has(n)||On.has(n)?null:`CASE WHEN ${e} IS NULL THEN NULL ELSE format('%s', ${e}) END`}switch(t){case"timestamp":return Sr(e);case"date":return Rr(e);case"bigint[]":case"numeric[]":case"uuid[]":case"enum[]":return`${e}::text[]`;case"date[]":return br(e,Rr);case"timestamp[]":return br(e,Sr)}return null}function Ar(t){return t==="jsonb"||t==="sql:json"}import{createCipheriv as Ra,createDecipheriv as ba,createHash as Or,randomBytes as Na}from"node:crypto";var Ee=(t,e="page")=>new Ne({fields:[{field:e,message:t}]},t);function kr(t){return Or("sha256").update("palbase:cursor:v1\0").update(t).digest()}function jt(t){let e=n=>typeof n=="bigint"?{$bigint:n.toString()}:n instanceof Date?{$date:n.toISOString()}:Array.isArray(n)?n.map(e):n&&typeof n=="object"?Object.fromEntries(Object.keys(n).sort().map(s=>[s,e(n[s])])):n;return Or("sha256").update(JSON.stringify(e(t))).digest("base64url")}function Lr(t,e){let{iat:n,exp:s,nbf:r,jti:o,...a}=e;return jt({role:t,authorization:a})}function xr(t,e,n){let s=Na(12),r=Ra("aes-256-gcm",t,s);r.setAAD(Buffer.from(e));let o=Buffer.concat([r.update(JSON.stringify(n),"utf8"),r.final()]),a=`p1.${Buffer.concat([s,r.getAuthTag(),o]).toString("base64url")}`;if(a.length>16384)throw Ee("page cursor values are too large; choose shorter ordering columns");return a}function Ir(t,e,n,s){try{if(typeof n!="string"||n.length>16384||!/^p1\.[\w-]+$/.test(n))throw new Error;let r=Buffer.from(n.slice(3),"base64url");if(r.length<30||r.toString("base64url")!==n.slice(3))throw new Error;let o=ba("aes-256-gcm",t,r.subarray(0,12));o.setAAD(Buffer.from(e)),o.setAuthTag(r.subarray(12,28));let a=JSON.parse(Buffer.concat([o.update(r.subarray(28)),o.final()]).toString("utf8"));if(!Array.isArray(a)||a.length!==s||a.some(l=>l!==null&&typeof l!="string"))throw new Error;return a}catch{throw Ee("Invalid cursor for this query or identity; restart from the first page","cursor")}}var Aa=new Set(["uuid","text","integer","bigint","boolean","timestamp","numeric","enum"]);function vr(t,e,n){let s=n;if("limit"in s||"offset"in s)throw Ee("page uses first/after or last/before, not limit/offset");let r=n.last!==void 0||n.before!==void 0;if(r&&(n.first!==void 0||n.after!==void 0))throw Ee("Use first/after or last/before in one page request");let o=(r?n.last:n.first)??50;if(!Number.isInteger(o)||o<1||o>1e3)throw Ee("page size must be an integer between 1 and 1000");if(e.length===0)throw new Error("page requires a declared primary key for a unique, stable ordering");Y(n.orderBy,"orderBy","the order");let a=n.orderBy===void 0?[]:Array.isArray(n.orderBy)?n.orderBy:[n.orderBy];if(a.length>16)throw Ee("page supports at most 16 ordering columns");let l=new Set,c=[],i=d=>{Y(d,"orderBy","the order");let u=t[d?.column];if(!Object.hasOwn(t,d?.column)||!u)throw Ee(`page order column ${d?.column} is not declared`);if(!Aa.has(u.type??""))throw Ee(`page cannot order ${d.column}: ${u.type??"unknown"} is not a supported scalar column`);if(l.has(d.column))throw Ee(`page order column ${d.column} is repeated`);if(d.direction!==void 0&&d.direction!=="asc"&&d.direction!=="desc")throw Ee("page direction must be asc or desc");if(d.nulls!==void 0&&d.nulls!=="first"&&d.nulls!=="last")throw Ee("page nulls must be first or last");let f=d.direction??"asc";l.add(d.column),c.push({column:d.column,direction:f,nulls:d.nulls??(f==="asc"?"last":"first"),nullable:u.nullable!==!1})};for(let d of a)i(d);for(let d of e)l.has(d)||i({column:d,direction:c.at(-1)?.direction??"asc"});return{order:c,size:o,backward:r,token:r?n.before:n.after}}function Kt(t,e,n,s,r,o=!1){let a=e.map(i=>s(i));if(t.every(i=>!i.nullable&&i.direction===t[0].direction)&&e.every(i=>i!==null)){let i=t[0].direction==="asc"!==n;return`(${t.map(d=>r(d.column)).join(", ")}) ${i?">":"<"}${o?"=":""} (${a.join(", ")})`}let l=[],c=[];for(let i=0;i<t.length;i++){let d=t[i],u=r(d.column),f=a[i],g=d.nulls==="last"!==n,p=d.direction==="asc"!==n?">":"<",y=e[i]===null?g?"false":`${u} IS NOT NULL`:`(${u} ${p} ${f}${g?` OR ${u} IS NULL`:""})`;l.push(`(${[...c,y].join(" AND ")})`),c.push(`${u} IS NOT DISTINCT FROM ${f}`)}return o&&l.push(`(${c.join(" AND ")})`),`(${l.join(" OR ")})`}import{AsyncResource as so}from"node:async_hooks";var Oa=5e3,et=()=>new oe(503,"database_deadline_exceeded","Database transaction exceeded its deadline"),Je=class{deadline;controller=new AbortController;active=new Set;cancelBackend;cancellation=Promise.resolve();cancelledAt;timer;unlink;rejection;get signal(){return this.controller.signal}constructor(e,n){this.deadline=e===void 0?void 0:performance.now()+e,this.rejection=new Promise((r,o)=>{this.signal.addEventListener("abort",()=>{o(this.signal.reason),this.active.size&&this.cancelBackend&&this.cancel()},{once:!0})}),this.rejection.catch(()=>{});let s=()=>this.controller.abort(n?.reason??new DOMException("Database transaction cancelled","AbortError"));n?.addEventListener("abort",s,{once:!0}),this.unlink=()=>n?.removeEventListener("abort",s),n?.aborted&&s(),e!==void 0&&(this.timer=setTimeout(()=>this.controller.abort(et()),e))}check(){this.deadline!==void 0&&performance.now()>=this.deadline&&!this.signal.aborted&&this.controller.abort(et()),this.signal.throwIfAborted()}checkDeadline(){if(!(this.deadline===void 0||performance.now()<this.deadline))throw this.signal.aborted||this.controller.abort(et()),et()}remainingMs(){return this.check(),this.deadline===void 0?void 0:Math.max(1,Math.ceil(this.deadline-performance.now()))}race(e){return Promise.race([e,this.rejection])}bindBackend(e){this.cancelBackend=e}async drainCancellation(){let e=(async()=>(await this.cancellation,await Promise.allSettled([...this.active]),!0))();if(this.cancelledAt===void 0)return e;let n,s=new Promise(r=>{n=setTimeout(()=>r(!1),Math.max(0,this.cancelledAt+Oa-performance.now()))});try{return await Promise.race([e,s])}finally{clearTimeout(n)}}async watch(e){this.active.add(e);try{return this.signal.aborted&&this.cancelBackend&&this.cancel(),await e}finally{this.active.delete(e)}}cancel(){this.cancelledAt??=performance.now(),this.cancellation=this.cancelBackend().catch(()=>{})}dispose(){this.timer!==void 0&&clearTimeout(this.timer),this.unlink()}};import{createHash as ka}from"node:crypto";var nt=class{start=performance.now();options;ended;failure;state;constructor(e){this.options=e;for(let n of["maxQueries","maxRows","maxBytes"]){let s=e[n];if(s!==void 0&&(!Number.isSafeInteger(s)||s<0))throw new Error(`database ${n} must be a nonnegative safe integer`)}this.state={endpoint:e.endpoint,requestId:e.requestId,outcome:"active",elapsedMs:0,queries:0,rows:0,bytes:0,queueMs:0,poolMs:0,setupMs:0,sqlMs:0,commitMs:0,rollbackMs:0,connectionMs:0,commits:0,commitFailures:0,rollbackRequests:0,retries:0,statements:[]}}check(){if(this.failure)throw this.failure}time(e,n){this.state[e]=(this.state[e]??0)+performance.now()-n}retry(){this.state.retries++}committed(){this.state.commits++}commitFailed(){this.state.commitFailures++}failed(){this.state.outcome="failed"}rolledBack(){this.state.rollbackRequests++}refuse(e){throw this.failure??=new oe(500,"database_budget_exceeded",`Database ${e} budget exceeded${this.options.endpoint?` in ${this.options.endpoint}`:""}`),this.failure}emit(e,n){try{let s=e?.(n);s&&Promise.resolve(s).catch(()=>{})}catch{}}async query(e,n){this.check(),this.options.maxQueries!==void 0&&this.state.queries>=this.options.maxQueries&&this.refuse("query count");let r={ordinal:++this.state.queries,fingerprint:ka("sha256").update(e).digest("hex").slice(0,24),...this.options.includeSql?{sql:e.slice(0,4096)}:{},durationMs:0,rows:0,bytes:0};this.options.captureSource&&(r.source=new Error().stack?.split(`
337
+ SELECT count(*)::int AS n FROM missing`,[$,C]);if(Number(U?.n??0)>0){A+=Number(U.n);continue}let[v]=await i(`SELECT ${le}.promote($1) AS gen`,[$]);v?.gen!==null&&v?.gen!==void 0&&(N.push($),s?.(`palgres: search of ${$}: every row is embedded again \u2014 generation ${String(v.gen)} serves`))}return A}async function d(N={}){let A=N.timeoutMs===void 0?1/0:Date.now()+N.timeoutMs,R=[],L=0,$=()=>Date.now()<A&&(N.max===void 0||L<N.max)&&!(N.whileRunning===!0&&c);for(;;){for(;$();){let C=await l(N.max===void 0?Rr:Math.min(Rr,N.max-L));if(C.length===0)break;let q=new Map;for(let I of C){let x=`${I.table_key}\0${I.gen}`,B=q.get(x);B?B.push(I):q.set(x,[I])}let U={answered:!1,down:!1,failed:0},v=0;for(let I of q.values())try{v+=await S(I,U)}catch(x){U.failed+=I.length,await m(I,x).catch(()=>{})}if(L+=v,v===0&&U.failed>0)return{embedded:L,promoted:R}}if(!$()||await f(R)===0)break}return{embedded:L,promoted:R}}let T,E,w=!1,O,k=()=>{w=!0,E?.()};return{drain:N=>d(N),kick:k,start(){T===void 0&&(c=!1,O=jn.listen(k),T=(async()=>{await i(`UPDATE ${le}.queue SET attempts = 0, next_attempt_at = now(), last_error = NULL WHERE next_attempt_at = 'infinity'`).catch(A=>s?.(`palgres: the search worker could not read its queue (${tt(A)})`));let N=br;for(;!c;){w=!1;let A=!1;try{let R=await d({timeoutMs:Na,whileRunning:!0});A=R.embedded>0||R.promoted.length>0}catch(R){s?.(`palgres: the search worker could not embed (${tt(R)}); it looks again`)}N=A?br:Math.min(ba,N*2),!(c||w)&&(await new Promise(R=>{let L=setTimeout(R,N);L.unref?.(),E=()=>{clearTimeout(L),R()}}),E=void 0)}})())},async stop(){if(T===void 0)return;c=!0,O?.(),O=void 0,E?.();let N=T;T=void 0;let A;await Promise.race([N,new Promise(R=>A=setTimeout(R,Aa))]),clearTimeout(A)}}}var Gt=class{options;active=0;queued=0;tenants=new Map;pending=new Map;lastTenant;accepted=0;rejected={full:0,timeout:0,cancelled:0};limit;timeout;nestedLimit;nestedActive=0;nestedPending=new Set;constructor(e){if(this.options=e,this.limit=e.queueLimit??Math.min(512,Math.max(32,e.concurrency*8)),this.timeout=e.queueTimeoutMs??1e3,this.nestedLimit=e.nestedConcurrency??e.concurrency*2,!Number.isSafeInteger(e.concurrency)||e.concurrency<1||!Number.isSafeInteger(this.nestedLimit)||this.nestedLimit<1||!Number.isSafeInteger(this.limit)||this.limit<0||this.limit>1e4||!Number.isSafeInteger(this.timeout)||this.timeout<1||this.timeout>6e4)throw new Error("Invalid database pool or queue deployment settings")}get queueTimeoutMs(){return this.timeout}get snapshot(){return{active:this.active,queued:this.queued,nested:{active:this.nestedActive,queued:this.nestedPending.size},tenants:this.tenants.size,accepted:this.accepted,rejected:{...this.rejected}}}nestedLease(e){this.nestedActive++;let n=!1;return{queueMs:performance.now()-e,release:()=>{if(n)return;n=!0,this.nestedActive--;let s=this.nestedPending.values().next().value;s&&(this.nestedPending.delete(s),s.cleanup(),s.grant(this.nestedLease(s.entered)))}}}acquireNested(e){let n=performance.now();return this.nestedActive<this.nestedLimit?Promise.resolve(this.nestedLease(n)):new Promise((s,r)=>{let o={grant:s,reject:r,entered:n,cleanup(){}},a=(l,u)=>{this.nestedPending.delete(o)&&(o.cleanup(),this.rejection(l),r(u))},i=()=>a("cancelled",e?.reason??new Error("Database admission cancelled")),c=setTimeout(()=>a("timeout",new Me({retryAfter:1},"Database queue deadline exceeded")),this.timeout);o.cleanup=()=>{clearTimeout(c),e?.removeEventListener("abort",i)},this.nestedPending.add(o),e?.addEventListener("abort",i,{once:!0}),e?.aborted&&i()})}rejection(e){this.rejected[e]++;try{this.options.onReject?.({reason:e,active:this.active,queued:this.queued})}catch{}}take(e,n){this.active++,this.accepted++,this.lastTenant=e,this.tenants.set(e,(this.tenants.get(e)??0)+1);let s=!1;return{queueMs:performance.now()-n,release:()=>{if(s)return;s=!0,this.active--;let r=(this.tenants.get(e)??1)-1;r?this.tenants.set(e,r):this.tenants.delete(e),this.drain()}}}drain(){for(;this.active<this.options.concurrency&&this.queued;){let e=this.pending.keys().next().value;if(this.pending.size>1&&e===this.lastTenant){let r=this.pending.get(e);this.pending.delete(e),this.pending.set(e,r),e=this.pending.keys().next().value}let n=this.pending.get(e),s=n.values().next().value;n.delete(s),this.pending.delete(e),n.size&&this.pending.set(e,n),this.queued--,s.cleanup(),s.grant(this.take(e,s.entered))}}async acquire(e,n,s={}){if(n?.aborted)throw this.rejection("cancelled"),n.reason??new Error("Database admission cancelled");if(s.nested)return this.acquireNested(n);let r=this.options.tenantClaim?e[this.options.tenantClaim]:e.sub,o=typeof r=="string"&&r.length?r:"__anonymous__",a=performance.now();if(this.active<this.options.concurrency&&!this.queued)return this.take(o,a);if(!s.returning&&this.queued>=this.limit)throw this.rejection("full"),new Me({retryAfter:1},"Database queue is full");return new Promise((i,c)=>{let l=this.pending.get(o)??new Set;this.pending.has(o)||this.pending.set(o,l);let u={grant:i,reject:c,entered:a,cleanup(){}},g=(m,y)=>{l.delete(u)&&(l.size||this.pending.delete(o),this.queued--,u.cleanup(),this.rejection(m),c(y),this.drain())},p=()=>g("cancelled",n?.reason??new Error("Database admission cancelled")),h=setTimeout(()=>g("timeout",new Me({retryAfter:1},"Database queue deadline exceeded")),this.timeout);if(u.cleanup=()=>{clearTimeout(h),n?.removeEventListener("abort",p)},s.returning){u.returning=!0;let m=[...l];l.clear();for(let y of m)y.returning&&l.add(y);l.add(u);for(let y of m)y.returning||l.add(y)}else l.add(u);this.queued++,n?.addEventListener("abort",p,{once:!0}),n?.aborted?p():this.drain()})}},Nr=new WeakMap;function Ar(t,e={concurrency:10}){let n=Nr.get(t);if(!n)return n=new Gt(e),Nr.set(t,n),n;for(let s of["concurrency","queueLimit","queueTimeoutMs","tenantClaim"])if(e[s]!==n.options[s])throw new Error("Database admission cannot be reconfigured while its pool is shared");return n}function ka(t){return t.endsWith("_id")?t.slice(0,-3):t}function Or(t){return t.direction==="forward"?`the foreign key "${t.table}.${t.column}"`:`the reverse of "${t.table}.${t.column}"`}function kr(t){return t.direction==="reverse"?'references(() => \u2026, { reverseAs: "\u2026" })':t.def.selfRefColumn!==void 0?'selfReferences("\u2026", { as: "\u2026" })':'references(() => \u2026, { as: "\u2026" })'}function wn(t){let e=new Map,n=new Map;for(let r of t)for(let o of Object.values(r.tables)){let a=he(r.name,o.name);e.set(a,[]),n.set(a,new Map)}let s=(r,o,a,i)=>{let c=n.get(r);if(c===void 0)return;let l=c.get(o);if(l!==void 0){let u=kr(l),g=kr(a),p=u===g?`rename one of them: ${u}`:`rename one of them \u2014 "${l.table}.${l.column}": ${u}; "${a.table}.${a.column}": ${g}`;throw new De([`table "${r}": ${Or(l)} and ${Or(a)} both resolve to the relation name "${o}" \u2014 ${p}`])}c.set(o,a),e.get(r)?.push(i)};for(let r of t)for(let o of Object.values(r.tables)){let a=he(r.name,o.name);for(let[i,c]of Object.entries(o.columns)){let l=c._def;if(l.references===void 0)continue;let u=l.references.table,g=l.refAs??ka(i);if(s(a,g,{table:o.name,column:i,direction:"forward",def:l},{name:g,to:u,kind:"one",via:i}),!e.has(u)||u===a)continue;let p=l.reverseAs??o.name;s(u,p,{table:o.name,column:i,direction:"reverse",def:l},{name:p,to:a,kind:"many",via:i})}}return e}function La(t){let e=t.columns??{},n=[],s=[];for(let[a,i]of Object.entries(e)){let c=i&&typeof i=="object"&&"_def"in i?i._def:i;c?.primaryKey&&n.push(a),c?.unique&&s.push([a])}let r=t.primaryKey??n;r.length&&s.unshift([...r]);for(let a of t.unique??[])s.push([...a.columns]);let o=new Set;return s.filter(a=>{if(!a.length||new Set(a).size!==a.length||a.some(c=>!Object.hasOwn(e,c)))throw new Error("Unique lookup requires a nonempty declared key of distinct, known columns");let i=JSON.stringify([...a].sort());return o.has(i)?!1:(o.add(i),!0)})}function st(t){let e=Object.fromEntries(Object.entries(t.columns).filter(([,n])=>n?._def?.ignored!==!0));return La({columns:e,primaryKey:t.primaryKey,unique:t.unique})}function Lr(t,e,n){if(!n||typeof n!="object"||Array.isArray(n))throw new Error(`findUnique(${t}): where must contain one declared unique key`);let s=Object.keys(n);if(!e.some(r=>r.length===s.length&&r.every(o=>Object.hasOwn(n,o))))throw new Error(`findUnique(${t}): supply exactly one declared unique key (${e.map(r=>r.join(" + ")).join("; ")||"none declared"}); use findMany for other filters`);for(let[r,o]of Object.entries(n))if(o==null||typeof o=="object"&&!(o instanceof Date)||typeof o=="function"||typeof o=="symbol")throw new Error(`findUnique(${t}): ${r} requires a non-null scalar equality value`)}var _=t=>`"${t.replace(/"/g,'""')}"`;function Cr(t){return t.type==="sql"&&typeof t.sqlType=="string"?`sql:${t.sqlType}`:t.type}var rt=class{version;tables=new Map;ignoredTables=new Set;schemas;deferrals;constructor(e,n){this.version=n.version,this.schemas=e;let s=wn(e);for(let r of e)for(let o of Object.values(r.tables)){let a=he(r.name,o.name);if(o.ignored){this.ignoredTables.add(a);continue}this.tables.set(a,xa(r.name,o,a,n,s.get(a)??[]))}}table(e,n){let s=this.tables.get(e);if(s)return s;throw this.ignoredTables.has(e)?new Error(`${n}(${e}): the table is .ignored() in this version \u2014 the version has promised not to name it, so the next one can drop it`):new Error(`${n}(${e}): no such table in this version's schema (a table outside public is "schema.table")`)}deferral(e){return this.deferrals??=Ia(ws([...this.schemas])),this.deferrals.get(`${e.schema}.${e.name}`)??null}};function Ia(t){let e=(o,a)=>`${o}.${a}`,n=new Map,s=new Map;for(let o of t.tables)for(let a of o.foreignKeys){let i=e(a.refSchema,a.refTable);a.onDelete==="cascade"&&n.set(i,[...n.get(i)??[],e(o.schema,o.name)]),a.deferrable&&s.set(i,[...s.get(i)??[],e(o.schema,a.name)])}let r=new Map;for(let o of t.tables){let a=new Set([e(o.schema,o.name)]);for(let l of a)for(let u of n.get(l)??[])a.add(u);let i=[...new Set([...a].flatMap(l=>s.get(l)??[]))].sort();if(i.length===0)continue;let c=`ARRAY[${i.map(M).join(", ")}]::text[]`;r.set(e(o.schema,o.name),{defer:`SELECT palgres.defer_keys(${c}, true)`,check:`SELECT palgres.defer_keys(${c}, false)`})}return r}function xa(t,e,n,s,r){let o=new Map,a=new Set,i=[],c=new Map,l=new Set;for(let[g,p]of Object.entries(e.columns)){let h=p._def;if(h.ignored){a.add(g);continue}if(o.set(g,h),h.primaryKey&&i.push(g),typeof h.codec=="string"&&h.codec in Ge){let m=Ge[h.codec];c.set(g,{fromDb:m.fromDb,toDb:m.toDb})}else h.transform!==void 0&&c.set(g,h.transform);(h.identity==="always"||h.generated!==void 0)&&l.add(g)}let u=e.primaryKey??i;return{key:n,schema:t,name:e.name,sql:t==="public"?_(e.name):`${_(s.schema(t))}.${_(e.name)}`,columns:o,ignored:a,primaryKey:u,singleKey:u.length===1?u[0]:null,uniqueKeys:st(e),transforms:c,databaseOnly:l,relations:r,projections:new Map,...e.search!==void 0?{search:{key:`${t}.${e.name}`,from:[...e.search.from]}}:{}}}function Ir(t){return`CASE WHEN isfinite(${t}) AND extract(year FROM ${t} AT TIME ZONE 'UTC') BETWEEN 1 AND 9999 THEN to_char(${t} AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') ELSE ${t}::text END`}function xr(t){return`CASE WHEN isfinite(${t}) AND extract(year FROM ${t}) BETWEEN 1 AND 9999 THEN to_char(${t}, 'YYYY-MM-DD') ELSE ${t}::text END`}function vr(t,e){return`CASE WHEN ${t} IS NULL THEN NULL ELSE ARRAY(SELECT ${e("u.e")} FROM unnest(${t}) WITH ORDINALITY AS u(e, i) ORDER BY u.i) END`}function Ue(t,e){if(t!==null&&t.startsWith("sql:")){let n=t.slice(4);return kn.has(n)||Ln.has(n)?null:`CASE WHEN ${e} IS NULL THEN NULL ELSE format('%s', ${e}) END`}switch(t){case"timestamp":return Ir(e);case"date":return xr(e);case"bigint[]":case"numeric[]":case"uuid[]":case"enum[]":return`${e}::text[]`;case"date[]":return vr(e,xr);case"timestamp[]":return vr(e,Ir)}return null}function _r(t){return t==="jsonb"||t==="sql:json"}import{createCipheriv as va,createDecipheriv as Ca,createHash as Dr,randomBytes as _a}from"node:crypto";var Ee=(t,e="page")=>new Ae({fields:[{field:e,message:t}]},t);function Pr(t){return Dr("sha256").update("palbase:cursor:v1\0").update(t).digest()}function Xt(t){let e=n=>typeof n=="bigint"?{$bigint:n.toString()}:n instanceof Date?{$date:n.toISOString()}:Array.isArray(n)?n.map(e):n&&typeof n=="object"?Object.fromEntries(Object.keys(n).sort().map(s=>[s,e(n[s])])):n;return Dr("sha256").update(JSON.stringify(e(t))).digest("base64url")}function Mr(t,e){let{iat:n,exp:s,nbf:r,jti:o,...a}=e;return Xt({role:t,authorization:a})}function qr(t,e,n){let s=_a(12),r=va("aes-256-gcm",t,s);r.setAAD(Buffer.from(e));let o=Buffer.concat([r.update(JSON.stringify(n),"utf8"),r.final()]),a=`p1.${Buffer.concat([s,r.getAuthTag(),o]).toString("base64url")}`;if(a.length>16384)throw Ee("page cursor values are too large; choose shorter ordering columns");return a}function Fr(t,e,n,s){try{if(typeof n!="string"||n.length>16384||!/^p1\.[\w-]+$/.test(n))throw new Error;let r=Buffer.from(n.slice(3),"base64url");if(r.length<30||r.toString("base64url")!==n.slice(3))throw new Error;let o=Ca("aes-256-gcm",t,r.subarray(0,12));o.setAAD(Buffer.from(e)),o.setAuthTag(r.subarray(12,28));let a=JSON.parse(Buffer.concat([o.update(r.subarray(28)),o.final()]).toString("utf8"));if(!Array.isArray(a)||a.length!==s||a.some(i=>i!==null&&typeof i!="string"))throw new Error;return a}catch{throw Ee("Invalid cursor for this query or identity; restart from the first page","cursor")}}var Da=new Set(["uuid","text","integer","bigint","boolean","timestamp","numeric","enum"]);function Ur(t,e,n){let s=n;if("limit"in s||"offset"in s)throw Ee("page uses first/after or last/before, not limit/offset");let r=n.last!==void 0||n.before!==void 0;if(r&&(n.first!==void 0||n.after!==void 0))throw Ee("Use first/after or last/before in one page request");let o=(r?n.last:n.first)??50;if(!Number.isInteger(o)||o<1||o>1e3)throw Ee("page size must be an integer between 1 and 1000");if(e.length===0)throw new Error("page requires a declared primary key for a unique, stable ordering");Y(n.orderBy,"orderBy","the order");let a=n.orderBy===void 0?[]:Array.isArray(n.orderBy)?n.orderBy:[n.orderBy];if(a.length>16)throw Ee("page supports at most 16 ordering columns");let i=new Set,c=[],l=u=>{Y(u,"orderBy","the order");let g=t[u?.column];if(!Object.hasOwn(t,u?.column)||!g)throw Ee(`page order column ${u?.column} is not declared`);if(!Da.has(g.type??""))throw Ee(`page cannot order ${u.column}: ${g.type??"unknown"} is not a supported scalar column`);if(i.has(u.column))throw Ee(`page order column ${u.column} is repeated`);if(u.direction!==void 0&&u.direction!=="asc"&&u.direction!=="desc")throw Ee("page direction must be asc or desc");if(u.nulls!==void 0&&u.nulls!=="first"&&u.nulls!=="last")throw Ee("page nulls must be first or last");let p=u.direction??"asc";i.add(u.column),c.push({column:u.column,direction:p,nulls:u.nulls??(p==="asc"?"last":"first"),nullable:g.nullable!==!1})};for(let u of a)l(u);for(let u of e)i.has(u)||l({column:u,direction:c.at(-1)?.direction??"asc"});return{order:c,size:o,backward:r,token:r?n.before:n.after}}function Yt(t,e,n,s,r,o=!1){let a=e.map(l=>s(l));if(t.every(l=>!l.nullable&&l.direction===t[0].direction)&&e.every(l=>l!==null)){let l=t[0].direction==="asc"!==n;return`(${t.map(u=>r(u.column)).join(", ")}) ${l?">":"<"}${o?"=":""} (${a.join(", ")})`}let i=[],c=[];for(let l=0;l<t.length;l++){let u=t[l],g=r(u.column),p=a[l],h=u.nulls==="last"!==n,m=u.direction==="asc"!==n?">":"<",y=e[l]===null?h?"false":`${g} IS NOT NULL`:`(${g} ${m} ${p}${h?` OR ${g} IS NULL`:""})`;i.push(`(${[...c,y].join(" AND ")})`),c.push(`${g} IS NOT DISTINCT FROM ${p}`)}return o&&i.push(`(${c.join(" AND ")})`),`(${i.join(" OR ")})`}import{AsyncResource as mo}from"node:async_hooks";var Pa=5e3,ot=()=>new oe(503,"database_deadline_exceeded","Database transaction exceeded its deadline"),Qe=class{deadline;controller=new AbortController;active=new Set;cancelBackend;cancellation=Promise.resolve();cancelledAt;timer;unlink;rejection;get signal(){return this.controller.signal}constructor(e,n){this.deadline=e===void 0?void 0:performance.now()+e,this.rejection=new Promise((r,o)=>{this.signal.addEventListener("abort",()=>{o(this.signal.reason),this.active.size&&this.cancelBackend&&this.cancel()},{once:!0})}),this.rejection.catch(()=>{});let s=()=>this.controller.abort(n?.reason??new DOMException("Database transaction cancelled","AbortError"));n?.addEventListener("abort",s,{once:!0}),this.unlink=()=>n?.removeEventListener("abort",s),n?.aborted&&s(),e!==void 0&&(this.timer=setTimeout(()=>this.controller.abort(ot()),e))}check(){this.deadline!==void 0&&performance.now()>=this.deadline&&!this.signal.aborted&&this.controller.abort(ot()),this.signal.throwIfAborted()}checkDeadline(){if(!(this.deadline===void 0||performance.now()<this.deadline))throw this.signal.aborted||this.controller.abort(ot()),ot()}remainingMs(){return this.check(),this.deadline===void 0?void 0:Math.max(1,Math.ceil(this.deadline-performance.now()))}race(e){return Promise.race([e,this.rejection])}bindBackend(e){this.cancelBackend=e}async drainCancellation(){let e=(async()=>(await this.cancellation,await Promise.allSettled([...this.active]),!0))();if(this.cancelledAt===void 0)return e;let n,s=new Promise(r=>{n=setTimeout(()=>r(!1),Math.max(0,this.cancelledAt+Pa-performance.now()))});try{return await Promise.race([e,s])}finally{clearTimeout(n)}}async watch(e){this.active.add(e);try{return this.signal.aborted&&this.cancelBackend&&this.cancel(),await e}finally{this.active.delete(e)}}cancel(){this.cancelledAt??=performance.now(),this.cancellation=this.cancelBackend().catch(()=>{})}dispose(){this.timer!==void 0&&clearTimeout(this.timer),this.unlink()}};import{createHash as Ma}from"node:crypto";var at=class{start=performance.now();options;ended;failure;state;constructor(e){this.options=e;for(let n of["maxQueries","maxRows","maxBytes"]){let s=e[n];if(s!==void 0&&(!Number.isSafeInteger(s)||s<0))throw new Error(`database ${n} must be a nonnegative safe integer`)}this.state={endpoint:e.endpoint,requestId:e.requestId,outcome:"active",elapsedMs:0,queries:0,rows:0,bytes:0,queueMs:0,poolMs:0,setupMs:0,sqlMs:0,commitMs:0,rollbackMs:0,connectionMs:0,commits:0,commitFailures:0,rollbackRequests:0,retries:0,statements:[]}}check(){if(this.failure)throw this.failure}time(e,n){this.state[e]=(this.state[e]??0)+performance.now()-n}retry(){this.state.retries++}committed(){this.state.commits++}commitFailed(){this.state.commitFailures++}failed(){this.state.outcome="failed"}rolledBack(){this.state.rollbackRequests++}refuse(e){throw this.failure??=new oe(500,"database_budget_exceeded",`Database ${e} budget exceeded${this.options.endpoint?` in ${this.options.endpoint}`:""}`),this.failure}emit(e,n){try{let s=e?.(n);s&&Promise.resolve(s).catch(()=>{})}catch{}}async query(e,n){this.check(),this.options.maxQueries!==void 0&&this.state.queries>=this.options.maxQueries&&this.refuse("query count");let r={ordinal:++this.state.queries,fingerprint:Ma("sha256").update(e).digest("hex").slice(0,24),...this.options.includeSql?{sql:e.slice(0,4096)}:{},durationMs:0,rows:0,bytes:0};this.options.captureSource&&(r.source=new Error().stack?.split(`
327
338
  `).slice(1,9).join(`
328
- `).slice(0,2500));let o=performance.now(),a=!1;try{let l=await n();return r.durationMs=performance.now()-o,a=!0,Array.isArray(l)&&(r.rows=l.length,r.bytes=Buffer.byteLength(JSON.stringify(l,(c,i)=>typeof i=="bigint"?i.toString():i))),this.state.rows+=r.rows,this.state.bytes+=r.bytes,this.options.maxRows!==void 0&&this.state.rows>this.options.maxRows&&this.refuse("returned rows"),this.options.maxBytes!==void 0&&this.state.bytes>this.options.maxBytes&&this.refuse("result bytes"),l}catch(l){let c=l;throw r.errorCode=String(c?.errno??c?.code??"query_failed").slice(0,80),l}finally{a||(r.durationMs=performance.now()-o),this.state.sqlMs+=r.durationMs;let l=Object.freeze(r);this.state.statements.length<200&&this.state.statements.push(l),this.emit(this.options.onQuery,l)}}get snapshot(){return Object.freeze({...this.state,elapsedMs:(this.ended??performance.now())-this.start,statements:Object.freeze([...this.state.statements].sort((e,n)=>e.ordinal-n.ordinal))})}complete(e){this.ended===void 0&&(this.ended=performance.now(),this.state.outcome=e,this.emit(this.options.onComplete,this.snapshot))}};var Pr=new WeakMap,Mr=new WeakSet;function ee(t){let e=n=>`$${t.push(n)}`;return Pr.set(e,t),e}var Gt=t=>Mr.has(t);var qr=t=>`'${t.replace(/'/g,"''")}'`;function $e(t,e){let n=t.columns.get(e);return n?Nr(n):null}function se(t,e,n,s){if(!t.columns.has(e))throw t.ignored.has(e)?new Error(`${n}(${t.key}): ${s} "${e}" is .ignored() in this version \u2014 the version has promised not to name it, so the next one can drop it`):new Error(`${n}(${t.key}): ${s} "${e}" is not a column of the table`)}function we(t,e,n){if(n==null)return n;let s=t.transforms.get(e)?.toDb,r=s===void 0?n:s(n),o=$e(t,e);return o===null||r===null||r===void 0?r:o.endsWith("[]")&&Array.isArray(r)?gn(r):Ar(o)?JSON.stringify(r):r}function Fr(t,e,n){let s=n.find(r=>t.databaseOnly.has(r));if(s!==void 0)throw new Error(`${e}(${t.key}): "${s}" is written by the database (an ALWAYS .identity()) \u2014 leave it out of the write; Postgres refuses any value for it`)}function Ze(t,e,n){for(let s of n)se(t,s,e,"column");Fr(t,e,n)}function Ur(t,e,n,s){return Ln(e,t.key,[n],s),hs(e,t.key,[n],s),we(t,n,s[n])}function Xt(t,e,n){let s=`${n?`${C(n)}.`:""}${C(e)}`;return Ue($e(t,e),s)??s}function Br(t,e,n){let s=Xt(t,e,n);return Ue($e(t,e),"x")!==null?`${s} AS ${C(e)}`:s}function Z(t,e){let n=e??"",s=t.projections.get(n);return s===void 0&&(s=[...t.columns.keys()].map(r=>Br(t,r,e)).join(", "),t.projections.set(n,s)),s}function Yt(t,e,n){if(Y(e,"select","which columns a call reads"),e!==void 0&&e.length===0)throw new Error(`${n}(${t.key}): select got an empty list \u2014 that is not "all of them" but "none counted". Leave the key out for every column; name at least one otherwise.`);return e===void 0?Z(t,"t"):e.map(s=>(se(t,s,n,"select column"),Br(t,s,"t"))).join(", ")}function Wr(t,e,n,s="t"){if(e==null)return"";Y(e,"orderBy","the order");let r=Array.isArray(e)?e:[e];return r.length===0?"":` ORDER BY ${r.map(a=>{Y(a,"orderBy","the order"),se(t,a.column,n,"orderBy column");let l=a.direction==="desc"?"DESC":"ASC",c=a.nulls==="first"?" NULLS FIRST":a.nulls==="last"?" NULLS LAST":"";return`${C(s)}.${C(a.column)} ${l}${c}`}).join(", ")}`}function Vt(t,e="findMany"){if(t===void 0)return"";if(!Number.isInteger(t)||t<0)throw new Error(`${e}: limit must be a non-negative integer (got ${String(t)})`);return` LIMIT ${t}`}function La(t,e,n="findMany"){if(t===void 0)return"";if(!Number.isInteger(t)||t<0)throw new Error(`${n}: offset must be a non-negative integer (got ${String(t)})`);if(e===void 0)throw new Error(`${n}: offset is given only with limit \u2014 an offset without one is the shifted tail of an unbounded result, not a page`);return` OFFSET ${t}`}var ze={gt:">",gte:">=",lt:"<",lte:"<=",neq:"<>"},xa=[["days","days","int"],["hours","hours","int"],["minutes","mins","int"],["seconds","secs","double precision"]];function Qe(t,e){let n=t?.minus;if(n===void 0)return"now()";let s=ws(n);if("refused"in s)throw new TypeError(`palgres: ago() ${s.refused}`);return`(now() - make_interval(${xa.filter(([o])=>s.span[o]!==void 0).map(([o,a,l])=>`${a} => ${e(s.span[o])}::${l}`).join(", ")}))`}var Cr=Object.freeze({update:"FOR UPDATE",share:"FOR SHARE",noKeyUpdate:"FOR NO KEY UPDATE"});function Jt(t,e,n,s){if(typeof s=="string"&&Object.hasOwn(Cr,s))return Cr[s];let r=typeof s=="string"?JSON.stringify(s.slice(0,40)):typeof s;throw new TypeError(`${t}(${e}): unknown ${n} ${r} \u2014 "update" (FOR UPDATE), "share" (FOR SHARE) or "noKeyUpdate" (FOR NO KEY UPDATE).`)}var _r=t=>xn(t);function Dr(t,e,n,s){if(!t.columns.has(e.$col))throw new Error(`${n}(${t.key}): col("${e.$col}") is not a column of the table`);return`${C(s)}.${C(e.$col)}`}function jr(t,e,n,s){let o=(n.kind==="many"?t.table(n.to,s):e).columns.get(n.via)?.references?.column??(n.kind==="many"?e.singleKey:t.table(n.to,s).singleKey);return n.kind==="many"?[o,n.via]:[n.via,o]}function Be(t,e,n){return be(new Ne({fields:[{field:e,message:n}]},`${t}: ${e} ${n}`))}function Ia(t,e){if(Y(t,e,"a filter"),!(typeof t!="object"||t===null))for(let[n,s]of Object.entries(t))n==="OR"||n==="AND"||n==="NOT"||n==="has"?Y(s,`${e}.${n}`,`a filter's ${n}`):s!==null&&typeof s=="object"&&!Array.isArray(s)&&Y(s,`${e}.${n}`,"an operator")}function tt(t,e,n,s,r,o,a,l){let c=[];if(Ia(n,l),Es(r,e.key,n),on(n)){let{text:i,values:d}=n.$sql,u=Pr.get(s);if(u===void 0)throw new Error("palgres: an sqlFragment was compiled outside binder()");if(Mr.add(u),i.length!==d.length+1)throw new Error(`${r}(${e.key}): the sqlFragment's parts are broken (${i.length} texts, ${d.length} values) \u2014 only the template tag makes one`);let f=i[0]??"";for(let g=0;g<d.length;g++)f+=`${s(d[g])}${i[g+1]??""}`;if(f.trim()==="")throw Be(r,l,"is an empty sqlFragment \u2014 no condition. Write the condition in it, or leave it out.");return[`(${f})`]}for(let[i,d]of Object.entries(n)){if(i==="OR"||i==="AND"){let p=d;if(p.length===0){if(i==="AND")throw Be(r,`${l}.AND`,"is an empty list \u2014 no condition at all. Leave it out, or name a condition in it.");c.push("false");continue}let y=p.map((S,h)=>{let m=tt(t,e,S,s,r,o,a,`${l}.${i}[${h}]`);if(m.length===0)throw Be(r,`${l}.${i}[${h}]`,"holds no condition \u2014 a branch that matches every row. Name a condition in it, or leave it out.");return`(${m.join(" AND ")})`});c.push(`(${y.join(i==="OR"?" OR ":" AND ")})`);continue}if(i==="NOT"){if(Array.isArray(d))throw Be(r,`${l}.NOT`,"is a list \u2014 NOT takes one filter object. For several conditions, NOT: { AND: [...] } or NOT: { OR: [...] }.");let p=tt(t,e,d,s,r,o,a,`${l}.NOT`);if(p.length===0)throw Be(r,`${l}.NOT`,"holds no condition \u2014 NOT of nothing is not a filter. Name the condition to exclude.");c.push(`NOT (${p.join(" AND ")})`);continue}if(i==="has"){let p=Object.entries(d);if(p.length===0)throw Be(r,`${l}.has`,"names no relation \u2014 no condition. Name one: has: { <relation>: { \u2026 } }.");for(let[y,S]of p){let h=e.relations.find(R=>R.name===y);if(h===void 0){let R=e.relations.map(b=>b.name).sort();throw new Error(`${r}(${e.key}): there is no relation has.${y}`+(R.length>0?` (known: ${R.join(", ")}). Relations come from foreign keys; to rename one, references(..., { as, reverseAs }).`:". The table declares no foreign key, and relations come from them: references(() => <table>.<column>)."))}let[m,T]=jr(t,e,h,r);if(m===null||T===null)throw new Error(`${r}(${e.key}): has.${y} cannot be built \u2014 "${m===null?e.key:h.to}" has no single-column primary key to correlate on; write the condition as an sqlFragment`);let E=t.table(h.to,r),w=`r${a+1}`,A=st(t,E,S??{},s,r,w,a+1,`${l}.has.${y}`),k=`${w}.${C(T)} = ${C(o)}.${C(m)}`;c.push(`EXISTS (SELECT 1 FROM ${E.sql} ${w} WHERE ${k}${A===""?"":` AND ${A}`})`)}continue}se(e,i,r,"where column");let u=`${C(o)}.${C(i)}`,f=$e(e,i)?.endsWith("[]")===!0,g=p=>s(we(e,i,p));if(xe(d)){c.push(`${u} = ${Dr(e,d,r,o)}`);continue}if(gt(d)){c.push(`${u} = ${Qe(_r(d),s)}`);continue}if(d!==null&&typeof d=="object"&&!Array.isArray(d))for(let[p,y]of Object.entries(d)){if(f&&(p==="contains"||p==="overlaps")){if(!Array.isArray(y))throw new Error(`${r}(${e.key}): where.${i}.${p} must be an array \u2014 ${i} is an array column`);c.push(`${u} ${p==="contains"?"@>":"&&"} ${g(y)}`);continue}if(!f&&(p==="contains"||p==="icontains"||p==="startsWith"||p==="endsWith")){if(typeof y!="string")throw new Error(`${r}(${e.key}): where.${i}.${p} must be a string`);let h=y.replace(/([\\%_])/g,"\\$1"),m=p==="startsWith"?`${h}%`:p==="endsWith"?`%${h}`:`%${h}%`;c.push(`${u} ${p==="icontains"?"ILIKE":"LIKE"} ${s(m)}`);continue}if(p==="isNull"){if(typeof y!="boolean")throw new Error(`${r}(${e.key}): where.${i}.isNull must be a boolean`);c.push(`${u} IS ${y?"NULL":"NOT NULL"}`);continue}if(p==="in"){if(!Array.isArray(y))throw new Error(`${r}(${e.key}): where.${i}.in must be an array`);if(y.some(xe))throw new Error(`${r}(${e.key}): where.${i}.in takes values, not col() \u2014 compare with a column bare ({ ${i}: col("\u2026") }) or with gt/gte/lt/lte/neq`);if(y.length===0){c.push("false");continue}c.push(`${u} IN (${y.map(h=>g(h)).join(", ")})`);continue}if(gt(y)){if(!(p in ze))throw new Error(`${r}(${e.key}): where.${i}.${p} cannot take now() \u2014 for equality write it bare: { ${i}: now() }`);c.push(`${u} ${ze[p]} ${Qe(_r(y),s)}`);continue}if(kn(y))throw new Error(`${r}(${e.key}): where.${i}.${p} got a WRITE expression (increment/decrement), not a comparison value. For the server's time use now(); for a numeric threshold, the value.`);if(xe(y)){if(!(p in ze))throw new Error(`${r}(${e.key}): where.${i}.${p} cannot take col() \u2014 for equality write it bare: { ${i}: col("\u2026") }`);c.push(`${u} ${ze[p]} ${Dr(e,y,r,o)}`);continue}let S=gs(y);if(S!==null){let h=S==="col"?"col()":S==="sql"?"sqlFragment":"now()";throw new Error(`${r}(${e.key}): where.${i}.${p} got an imitation of ${h} \u2014 this object was not made by ${h} in this process. A filter from a request body cannot carry a handle; build the condition in your code.`)}if(p in ze){if(y===null&&p==="neq"){c.push(`${u} IS NOT NULL`);continue}c.push(`${u} ${ze[p]} ${g(y)}`);continue}throw new Error(`${r}(${e.key}): where.${i} has an unknown operator "${p}" (gt/gte/lt/lte/neq/in)`)}else d===null?c.push(`${u} IS NULL`):c.push(`${u} = ${g(d)}`)}return c}function st(t,e,n,s,r,o="t",a=0,l="where"){return tt(t,e,n,s,r,o,a,l).join(" AND ")}function Se(t,e,n,s,r){let o=tt(t,e,n,s,r,"t",0,"where");return{sql:o.length===0?"":` AND ${o.join(" AND ")}`,conditions:o.length}}function en(t,e){if(!(t.conditions>0))throw Be(e,"where",`holds no condition, so ${e} would reach EVERY row of the table. Name a condition.`)}function wn(t,e,n){if(n!=="ignore"&&n!=="update")throw new TypeError(`palgres: a conflict's action is "ignore" or "update", not ${typeof n=="string"?JSON.stringify(String(n).slice(0,40)):typeof n}`);if(e.length===0){if(n==="ignore")return"ON CONFLICT DO NOTHING";throw new Error("palgres: ON CONFLICT DO UPDATE needs the columns a collision is matched on")}let s=`ON CONFLICT (${e.map(C).join(", ")})`;if(n==="ignore")return`${s} DO NOTHING`;let r=new Set(e),o=t.filter(a=>!r.has(a)).map(a=>`${C(a)} = EXCLUDED.${C(a)}`);return o.length?`${s} DO UPDATE SET ${o.join(", ")}`:`${s} DO UPDATE SET ${C(e[0])} = EXCLUDED.${C(e[0])}`}function zt(t,e,n,s,r){return Fr(t,r,e),e.map(o=>{let a=xn(n[o]);if(a!==null&&a.fn==="now")return`${C(o)} = ${Qe(a,s)}`;if(a!==null){let l=t.transforms.get(o)?.toDb,c=l===void 0?a.by:l(a.by);return`${C(o)} = ${C(o)} ${a.fn==="inc"?"+":"-"} ${s(c)}`}return`${C(o)} = ${s(Ur(t,r,o,n))}`}).join(", ")}function yn(t,e,n,s,r){return e.map(o=>{let a=xn(n[o]);if(a!==null&&a.fn==="now")return Qe(a,s);if(a!==null){let l=a.fn==="inc"?"increment":"decrement";throw new Error(`${r}(${t.key}): "${o}" got ${l}() \u2014 the row does not exist yet, so there is no current value. Write the starting value; to change it once the row exists, updateMany({ where, set: { ${o}: ${l}(n) } }).`)}return s(Ur(t,r,o,n))}).join(", ")}var va=new Set(["integer","bigint","numeric"]);function Kr(t){return t!==null&&va.has(t)}function Hr(t,e,n,s){let r=`t.${C(n)}`,o=$e(t,n);if(e==="sum"||e==="avg")return`${e}(${r})::text AS ${C(s)}`;let a=o!==null&&!o.endsWith("[]")?Ue(o,`${e}(${r})`):null;return a!==null?`${a} AS ${C(s)}`:o==="bigint"||o==="numeric"?`${e}(${r})::text AS ${C(s)}`:`${e}(${r}) AS ${C(s)}`}function Ca(t,e,n,s=[]){return`json_build_object(${[...e.map(o=>{let a=$e(t,o),l=`${n}.${C(o)}`,c=a==="numeric"||a==="bigint"?`${l}::text`:Ue(a,l)??l;return`${qr(o)}, ${c}`}),...s].join(", ")})`}function rt(t,e,n,s,r,o="t",a=0,l={value:0}){if(Y(n,"with","which relations a call reads"),a>=3&&Object.values(n).some(Boolean))throw new Error(`${r}(${e.key}): with supports at most three relation levels`);let c=[],i=[],d=[];for(let[u,f]of Object.entries(n)){if(f===!1||f===void 0)continue;Y(f,`with.${u}`,"which relations a call reads");let g=e.relations.find(x=>x.name===u);if(g===void 0){let x=e.relations.map(I=>I.name).sort();throw new Error(`${r}(${e.key}): there is no relation with.${u}`+(x.length>0?` (known: ${x.join(", ")})`:". The table declares no foreign key, and relations come from them")+". A relation is declared with references(() => <table>.<column>).")}let[p,y]=jr(t,e,g,r);if(p===null||y===null)throw new Error(`${r}(${e.key}): with.${u} needs a single-column primary key at one end of the relation`);let S=t.table(g.to,r),h=typeof f=="object"&&f!==null?f:{},m=++l.value,T=`w${m}`;if(Y(h.select,`with.${u}.select`,"which columns a call reads"),Y(h.orderBy,`with.${u}.orderBy`,"the order"),h.select!==void 0&&(!Array.isArray(h.select)||h.select.length===0))throw new Error(`${r}(${e.key}): with.${u}.select must be a non-empty array of columns`);let E=Array.isArray(h.select)?h.select:[...S.columns.keys()];for(let x of E)se(S,x,r,`with.${u}.select column`);let w=h.with;if(w!==void 0&&(w===null||typeof w!="object"||Array.isArray(w)))throw new Error(`${r}(${e.key}): with.${u}.with must be a relation object`);let A=w===void 0?{joins:[],jsonPairs:[]}:rt(t,S,w,s,r,T,a+1,l),k=A.joins.length?` ${A.joins.join(" ")}`:"",R=Ca(S,E,T,A.jsonPairs),b=h.where!==void 0&&h.where!==null?st(t,S,h.where,s,r,T,m,`with.${u}.where`):"",O=`${T}.${C(y)} = ${C(o)}.${C(p)}`,L=b===""?O:`${O} AND ${b}`,$=h.limit;Vt($,r);let _=g.kind==="many"?$:Math.min(1,$??1),M=Wr(S,h.orderBy,r,T)+Vt(_,r),U=`wj${m}`;g.kind==="many"?i.push(`LEFT JOIN LATERAL (SELECT coalesce(json_agg(x.o), '[]'::json) AS d FROM (SELECT ${R} AS o FROM ${S.sql} ${T}${k} WHERE ${L}${M}) x) ${C(U)} ON true`):i.push(`LEFT JOIN LATERAL (SELECT ${R} AS d FROM ${S.sql} ${T}${k} WHERE ${L}${M}) ${C(U)} ON true`),c.push(`${C(U)}."d" AS ${C(u)}`),d.push(`${qr(u)}, ${C(U)}."d"`)}return{select:c,joins:i,jsonPairs:d}}function Qt(t,e,n,s){let r=[],o=ee(r),a=Se(t,e,n,o,"findMany").sql,l=Wr(e,s.orderBy,"findMany"),c=Vt(s.limit),i=La(s.offset,s.limit),d=Yt(e,s.select,"findMany"),u=s.with!==void 0&&Object.keys(s.with).length>0?rt(t,e,s.with,o,"findMany"):{select:[],joins:[]},f=u.select.length>0?`${d}, ${u.select.join(", ")}`:d,g=u.joins.length>0?` ${u.joins.join(" ")}`:"",p=s.lock===void 0?"":` ${Jt("findUnique",e.key,"lock",s.lock)} OF t`;return{sql:`SELECT ${f} FROM ${e.sql} t${g} WHERE true${a}${l}${c}${i}${p}`,params:r}}var Ht=50;function Zt(t,e,n,s){if(e.search===void 0||e.singleKey===null)throw new Error(`search(${e.key}): the table declares no search`);if(n.text===void 0&&n.vector===void 0)throw new Error(`search(${e.key}): neither the query's words nor its vector to search by`);let r=[],o=ee(r),a=C(e.singleKey),l=Se(t,e,s.where??{},o,"search").sql,c=Math.max(s.limit*3,30),i=[];if(n.vector!==void 0){let u=`v."embedding" ${n.vector.operator} ${o(n.vector.literal)}::${n.vector.type}`;i.push(`sem AS (SELECT s."id", row_number() OVER (ORDER BY s."d", s."id") AS r FROM (SELECT t.${a} AS "id", ${u} AS "d" FROM ${n.vector.table} v JOIN ${e.sql} t ON t.${a} = v."id" WHERE true${l} ORDER BY ${u} LIMIT ${c}) s)`)}if(n.text!==void 0){let u=Mn(e.search.from,"t"),f=`websearch_to_tsquery('simple', ${o(n.text)})`;i.push(`kw AS (SELECT s."id", row_number() OVER (ORDER BY s."k" DESC, s."id") AS r FROM (SELECT t.${a} AS "id", ts_rank_cd(${u}, ${f}) AS "k" FROM ${e.sql} t WHERE ${u} @@ ${f}${l} ORDER BY "k" DESC LIMIT ${c}) s)`)}i.push(i.length===2?`fused AS (SELECT coalesce(sem."id", kw."id") AS "id", (coalesce(1.0 / (${Ht} + sem.r), 0) + coalesce(1.0 / (${Ht} + kw.r), 0))::float8 AS "score" FROM sem FULL OUTER JOIN kw ON sem."id" = kw."id")`:`fused AS (SELECT x."id", (1.0 / (${Ht} + x.r))::float8 AS "score" FROM ${n.vector!==void 0?"sem":"kw"} x)`);let d=s.minScore===void 0?"":` AND f."score" >= ${o(s.minScore)}`;return{sql:`WITH ${i.join(", ")} SELECT ${Z(e,"t")}, f."score" AS "_score" FROM fused f JOIN ${e.sql} t ON t.${a} = f."id" WHERE true${d} ORDER BY f."score" DESC, t.${a} LIMIT ${_a(s.limit)}`,params:r}}function _a(t){if(!Number.isInteger(t)||t<1)throw new Error(`search: limit must be a positive integer (got ${String(t)})`);return t}function Vr(t){return t instanceof Date?t.toISOString():Array.isArray(t)?t.map(Vr):t}function es(t){if(t===null||typeof t!="object")return t;let e={};for(let[n,s]of Object.entries(t))e[n]=Vr(s);return e}function Da(t,e){for(let[n,s]of e.transforms){if(s.fromDb===void 0)continue;let r=t[n];r!=null&&(t[n]=s.fromDb(r))}return t}function En(t,e,n,s){let r=n.map(o=>Da(es(o),e));if(s)for(let o of e.relations){let a=s[o.name];if(!a)continue;let l=typeof a=="object"?a.with:void 0,c=t.table(o.to,"with");for(let i of r){let d=i[o.name];Array.isArray(d)?i[o.name]=En(t,c,d,l):d&&typeof d=="object"&&(i[o.name]=En(t,c,[d],l)[0])}}return Ve(r)}var Pa={inline:!0},ns={inline:!0,read:!0},Ma=t=>ae(t)===te.uniqueViolation||We.is(t);function Gr(t,e){if(e===null)return!1;let n=Object.keys(t);if(n.length!==1||n[0]!==e)return!1;let s=t[e];return s!==null&&(typeof s!="object"||Array.isArray(s))}var qa=6e4;function Tn(t,e){let{cat:n}=e,s=(u,f)=>n.table(u,f),r=(u,f)=>{let g=n.table(u,f);return g.search!==void 0&&e.searchable?.(),g},o=async(u,f=[],g=!1)=>await(await t()).unsafe(u,f,Gt(f)?void 0:g?ns:Pa),a=(u,f=[])=>o(u,f,!0),l=async(u,f=[])=>await(await t()).unsafe(u,f),c=(u,f,g)=>En(n,u,f,g),i=(u,f)=>En(n,u,[f])[0],d={diagnostics(){return e.trace?.snapshot??null},async query(u,f=[]){return Ve((await l(u,f)).map(g=>es(g)))},async insert(u,f){let g=r(u,"insert"),p=Object.keys(f);if(p.length===0)throw new Error(`insert into ${u}: no columns given`);Ze(g,"insert",p);let y=[],S=yn(g,p,f,ee(y),"insert"),h=await o(`INSERT INTO ${g.sql} (${p.map(C).join(", ")}) VALUES (${S}) RETURNING ${Z(g)}`,y);if(!h[0])throw new Error(`insert into ${u} returned no row \u2014 the write was rejected (an RLS policy, most likely).`);return i(g,h[0])},insertMany:(async function(u,f,g){if(Ss(g),f.length===0)return g?.returning===!1?0:[];let p=r(u,"insertMany"),y=Object.keys(f[0]);if(y.length===0)throw new Error(`insertMany into ${u}: no columns given`);for(let A=1;A<f.length;A++){let k=y.filter(b=>!(b in f[A])),R=Object.keys(f[A]).filter(b=>!y.includes(b));if(k.length>0||R.length>0)throw new Error(`insertMany(${u}): row ${A} does not have the first row's columns`+(k.length>0?` (missing: ${k.join(", ")})`:"")+(R.length>0?` (extra: ${R.join(", ")})`:"")+". One statement shares the column list: write null for a missing value, or insert differently shaped rows in calls of their own.")}Ze(p,"insertMany",y),Y(g?.onConflict,"onConflict","which columns a conflict is on");for(let A of g?.onConflict??[])se(p,A,"insertMany","onConflict column");let S=[],h=ee(S),m=f.map(A=>`(${yn(p,y,A,h,"insertMany")})`),T=g?.onConflict??[],E=T.length>0||g?.action==="ignore"?` ${wn(y,T,g?.action??"ignore")}`:"",w=await o(`INSERT INTO ${p.sql} (${y.map(C).join(", ")}) VALUES ${m.join(", ")}${E}${g?.returning===!1?"":` RETURNING ${Z(p)}`}`,S);if(g?.returning===!1){if(!Number.isSafeInteger(w.count)||w.count<0)throw new Error("insertMany returning:false requires a driver with an affected row count");return w.count}return c(p,w)}),async advisoryXactLock(u){await o("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))",[u])},async lockRows(u,f){let g=[...new Set(f)].sort();if(g.length===0)return;let p=s(u,"lockRows");if(p.singleKey===null)throw new Error(`lockRows(${u}): no single-column primary key \u2014 the lock order needs one`);let y=C(p.singleKey),S=[],h=ee(S),m=g.map(T=>h(we(p,p.singleKey,T))).join(", ");await o(`SELECT t.${y} FROM ${p.sql} t WHERE t.${y} IN (${m}) ORDER BY t.${y} FOR NO KEY UPDATE`,S)},async lockRowsWhere(u,f,g){let p=Jt("lockRowsWhere",u,"mode",g?.mode===void 0?"update":g.mode),y=s(u,"lockRowsWhere");if(y.singleKey===null)throw new Error(`lockRowsWhere(${u}): no single-column primary key \u2014 the lock order needs one`);let S=[],h=Se(n,y,f,ee(S),"lockRowsWhere");en(h,"lockRowsWhere");let m=C(y.singleKey);await o(`SELECT t.${m} FROM ${y.sql} t WHERE true${h.sql} ORDER BY t.${m} ${p}`,S)},async claim(u,f,g={}){let p=r(u,"claim");Y(f,"unique","which key a claim is on");let y=Object.keys(f);if(y.length===0)throw new Error(`claim(${u}): no unique field given. claim takes ownership of a key; with none there is nothing to own \u2014 use insert(${u}, \u2026).`);Ln("claim",u,y,f);for(let E of y){let w=f[E];if(w===null||typeof w=="object"||typeof w=="function")throw new Error(`claim(${u}): the unique field "${E}" is ${w===null?"null":"an object"} \u2014 a key is one value (a string, number, bigint or boolean); NULL never equals a claimed row.`)}let S=Object.keys(g).filter(E=>Object.hasOwn(f,E));if(S.length>0)throw new Error(`claim(${u}): ${S.map(E=>`"${E}"`).join(", ")} is in both the unique fields and extra \u2014 say a key once, in the unique fields.`);let h={...f,...g},m=Object.keys(h);Ze(p,"claim",m);let T=await t();try{return await T.savepoint(async E=>{let w=[],A=yn(p,m,h,ee(w),"claim"),k=await E.unsafe(`INSERT INTO ${p.sql} (${m.map(C).join(", ")}) VALUES (${A}) RETURNING ${Z(p)}`,w);if(!k[0])throw new Error(`claim(${u}): the INSERT returned no row \u2014 the write was rejected (an RLS policy, most likely).`);return{inserted:!0,row:i(p,k[0])}})}catch(E){if(!Ma(E))throw E;let w=[],A=ee(w),k=y.map(b=>`t.${C(b)} = ${A(we(p,b,f[b]))}`).join(" AND "),R=await o(`SELECT ${Z(p,"t")} FROM ${p.sql} t WHERE ${k} LIMIT 1`,w);if(!R[0]){let b=sn(E);throw new Error(`claim(${u}): a unique violation${b?` (${b})`:""} was caught but no row has these ${y.join(", ")}. Either the violation is of ANOTHER unique constraint \u2014 claim() is the wrong tool for this call \u2014 or the row exists and an RLS policy hides it from this caller.`)}return{inserted:!1,row:i(p,R[0])}}},async put(u,f,g){let p=r(u,"put"),y=g.onConflict;if(y.length===0)throw new Error(`put into ${u}: onConflict needs at least one column`);let S=Object.keys(f);if(S.length===0)throw new Error(`put into ${u}: no columns given`);Ze(p,"put",S),Y(y,"onConflict","which columns a conflict is on");for(let E of y)se(p,E,"put","onConflict column");let h=[],m=yn(p,S,f,ee(h),"put"),T=await o(`INSERT INTO ${p.sql} (${S.map(C).join(", ")}) VALUES (${m}) ${wn(S,y,"update")} RETURNING ${Z(p)}`,h);if(!T[0])throw new Error(`put into ${u} returned no row \u2014 the write was rejected (an RLS policy, most likely).`);return i(p,T[0])},async update(u,f,g){let p=r(u,"update");if(!p.columns.has("id"))throw new Error(`update(${u}): the table has no "id" column \u2014 update a row by its key with updateMany({ where, set })`);let y=Object.keys(g);if(y.length===0)return d.findById(u,f);for(let E of y)se(p,E,"update","set column");let S=[],h=ee(S),m=zt(p,y,g,h,"update"),T=await o(`UPDATE ${p.sql} SET ${m} WHERE "id" = ${h(we(p,"id",f))} RETURNING ${Z(p)}`,S);return T[0]?i(p,T[0]):null},async delete(u,f){let g=s(u,"delete");if(!g.columns.has("id"))throw new Error(`delete(${u}): the table has no "id" column \u2014 delete by its key with deleteMany({ where })`);await o(`DELETE FROM ${g.sql} WHERE "id" = $1`,[we(g,"id",f)])},async updateMany(u,f,g,p){let y=r(u,"updateMany"),S=Object.keys(g);if(S.length===0)throw new Error(`updateMany(${u}): nothing to set. An update with no columns is not a no-op worth pretending happened \u2014 pass the columns to write.`);for(let M of S)se(y,M,"updateMany","set column");let h=[],m=ee(h),T=zt(y,S,g,m,"updateMany"),E=Se(n,y,f,m,"updateMany");en(E,"updateMany");let w=E.sql,A=y.singleKey,k=p?.returning!==!1,R=A===null||Gr(f,A),b=k?`RETURNING ${Z(y,"t")}`:"RETURNING 1",O=R?`UPDATE ${y.sql} AS t SET ${T} WHERE true${w} ${b}`:`UPDATE ${y.sql} AS t SET ${T} FROM locked WHERE t.${C(A)} = locked."__pb_lock" ${b}`,L=R?"":`WITH locked AS (SELECT t.${C(A)} AS "__pb_lock" FROM ${y.sql} t WHERE true${w} ORDER BY t.${C(A)} FOR NO KEY UPDATE)`,$=k?`${L} ${O}`.trim():R?`WITH upd AS (${O}) SELECT count(*)::int AS n FROM upd`:`${L}, upd AS (${O}) SELECT count(*)::int AS n FROM upd`,_=await o($,h);return k?c(y,_):Number(_[0]?.n??0)},async deleteMany(u,f){let g=s(u,"deleteMany"),p=[],y=Se(n,g,f,ee(p),"deleteMany");return en(y,"deleteMany"),(await o(`DELETE FROM ${g.sql} AS t WHERE true${y.sql} RETURNING 1`,p)).length},async aggregate(u,f={}){let g=s(u,"aggregate");for(let b of["sum","avg","min","max","groupBy"])Y(f[b],b,"which columns an aggregate reads");Y(f.count,"count","which counts an aggregate takes");let p=[],y=ee(p),S=[],h=[];for(let b of["sum","avg","min","max"])for(let O of f[b]??[]){if(se(g,O,"aggregate",`${b} column`),(b==="sum"||b==="avg")&&!Kr($e(g,O)))throw new Error(`aggregate(${u}): ${b}("${O}") on a column that is not numeric \u2014 "${O}" is ${$e(g,O)}. Only integer, bigint and numeric sum; to count rows, count: true.`);let L=`${b}__${O}`;h.push({fn:b,col:O,alias:L}),S.push(Hr(g,b,O,L))}f.count===!0&&S.push('count(*) AS "_count"');let m=f.count!==null&&typeof f.count=="object"?Object.entries(f.count):[];if(f.count!==void 0&&typeof f.count!="boolean"&&(f.count===null||typeof f.count!="object"||Array.isArray(f.count)||m.length===0))throw new Error(`aggregate(${u}): count is true, or named filters \u2014 { <name>: <filter> }, at least one`);if(m.forEach(([b,O],L)=>{let $=st(n,g,O,y,"aggregate","t",0,`count.${b}`);S.push(`count(*) FILTER (WHERE ${$===""?"true":$}) AS ${C(`count__${L}`)}`)}),S.length===0)throw new Error(`aggregate(${u}): no aggregate asked for \u2014 the result would be an empty object. Name sum/avg/min/max columns or count: true.`);let T=f.groupBy??[];for(let b of T)se(g,b,"aggregate","groupBy column");let E=T.map(b=>`${Xt(g,b,"t")} AS ${C(`g__${b}`)}`),w=Se(n,g,f.where??{},y,"aggregate").sql,A=T.length>0?` GROUP BY ${T.map(b=>`t.${C(b)}`).join(", ")}`:"",k=await a(`SELECT ${[...E,...S].join(", ")} FROM ${g.sql} t WHERE true${w}${A}`,p),R=b=>{let O={};for(let L of T)O[L]=b[`g__${L}`];for(let L of h){let $=O[L.fn]??={};$[L.col]=b[L.alias]??null}return f.count===!0&&(O.count=Number(b._count??0)),m.length>0&&(O.count=Object.fromEntries(m.map(([L],$)=>[L,Number(b[`count__${$}`]??0)]))),O};return T.length>0?k.map(R):R(k[0]??{})},async count(u,f={}){let g=s(u,"count"),p=[],y=Se(n,g,f,ee(p),"count").sql,S=await a(`SELECT count(*) AS n FROM ${g.sql} t WHERE true${y}`,p);return Number(S[0]?.n??0)},async findUnique(u,f,g={}){let p=s(u,"findUnique");$r(u,p.uniqueKeys,f);let y={select:g.select===void 0?void 0:rn(g.select,[...g.select]),with:g.with,limit:2},S;if(g.lock===void 0)S=await d.findMany(u,f,y);else{let h=Qt(n,p,f,{...y,lock:g.lock});S=c(p,await o(h.sql,h.params),g.with)}if(S.length>1)throw new Error(`findUnique(${u}): the database's constraints disagree with the schema; more than one row matched`);return S[0]??null},async findById(u,f){let g=s(u,"findById");if(!g.columns.has("id"))throw new Error(`findById(${u}): the table has no "id" column \u2014 use findUnique({ where: <its key> })`);let p=await a(`SELECT ${Z(g)} FROM ${g.sql} WHERE "id" = $1`,[we(g,"id",f)]);return p[0]?i(g,p[0]):null},async findMany(u,f={},g={}){let p=s(u,"findMany"),y=Qt(n,p,f,g),S=g.lock!==void 0;return c(p,await(S?o:a)(y.sql,y.params),g.with)},async search(u,f,g={}){let p=s(u,"search"),y=p.search;if(y===void 0)throw new Error(`search(${u}): the table declares no search \u2014 defineTable(${JSON.stringify(p.name)}, { \u2026, search: { from: [<its text columns>], model } })`);let S=pr(u,f,g),h=e.search,m=R=>c(p,R),T=async()=>{let R=Zt(n,p,{text:S.text},{where:g.where,limit:S.limit,minScore:S.minScore});return m(await a(R.sql,R.params))};if(S.mode==="text")return T();let E=h.embed;if(E===void 0)throw new Error(`search(${u}): no embedder was given, and a search by meaning embeds its query \u2014 createDatabase({ embed }) (the backend's db.embed); mode: "text" searches by word alone`);let w=R=>(R===void 0?h.serving.delete(y.key):h.serving.set(y.key,R),R),A=h.serving.get(y.key)??w(Ut((await a(Ft,[y.key]))[0])),k;for(let R=1;;R++){if(A===void 0){if(S.mode==="vector")throw new Error(`search(${u}): the database holds no vectors for this table \u2014 its schema is migrated before the app serves`);return T()}if(k===void 0||k.of.model!==A.model||k.of.dimensions!==A.dimensions)try{k={of:A,literal:await fr(E,S.text,A)}}catch(x){if(S.mode==="vector")throw x;let I=Date.now();return I-(h.said.get(y.key)??-1/0)>=qa&&(h.said.set(y.key,I),h.log?.(`palgres: search(${u}) answered by word alone: its query could not be embedded (${x instanceof Error?x.message:String(x)})`)),T()}let b=an(A.dimensions,A.metric),O=C(A.types),L=Zt(n,p,{...S.mode==="hybrid"?{text:S.text}:{},vector:{table:`${C(J)}.${C(Ie(y.key,A.gen))}`,literal:k.literal,type:`${O}.${b.type}`,operator:`OPERATOR(${O}.${b.operator})`}},{where:g.where,limit:S.limit,minScore:S.minScore}),$=await t(),[_,M]=await Promise.all([$.unsafe(Ft,[y.key],ns),$.unsafe(L.sql,L.params,Gt(L.params)?void 0:ns)]),U=w(Ut(_[0]));if(U!==void 0&&U.gen===A.gen)return m(M);if(R>=3)throw new Error(`search(${u}): the vectors that serve the table changed while it was searched, three times over`);A=U}},async page(u,f={},g={}){let p=s(u,"page");if(e.cursorKey===null)throw new Error("Database.page needs a cursor secret shared by every replica: createDatabase({ cursorSecret })");let y=e.cursorKey,S=Object.create(null);for(let[H,ne]of p.columns)S[H]={type:ne.type,nullable:ne.nullable&&!p.primaryKey.includes(H)};let{order:h,size:m,backward:T,token:E}=vr(S,p.primaryKey,g),w=[],A=ee(w),k=Se(n,p,f,A,"page").sql,R=Yt(p,g.select===void 0?void 0:rn(g.select,[...g.select]),"page"),b=jt({table:u,version:n.version,columns:S,order:h,where:k,params:w,select:g.select,with:g.with,identity:e.cursorScope()}),O=E===void 0?void 0:Ir(y,b,E,h.length),L=H=>`t.${C(H)}`,$=O?` AND ${Kt(h,O,T,A,L)}`:"",_=O?`EXISTS (SELECT 1 FROM ${p.sql} t WHERE true${k} AND ${Kt(h,O,!T,A,L,!0)})`:"false",M="__pb_page_",U=[...p.columns.keys(),...Object.keys(g.with??{})];for(;U.some(H=>H.startsWith(M));)M+="_";let x=`${M}ordinal`,I=`${M}opposite`,v=h.map((H,ne)=>`${M}${ne}`),B=h.map((H,ne)=>`${$e(p,H.column)==="timestamp"?Ue("timestamp",L(H.column)):`${L(H.column)}::text`} AS ${C(v[ne])}`),q=g.with&&Object.keys(g.with).length?rt(n,p,g.with,A,"page"):{select:[],joins:[]},W=h.map(H=>`${L(H.column)} ${H.direction==="asc"!==T?"ASC":"DESC"} NULLS ${H.nulls==="first"!==T?"FIRST":"LAST"}`).join(", "),G=`SELECT ${[R,...q.select,...B,`row_number() OVER (ORDER BY ${W}) AS ${C(x)}`].join(", ")} FROM ${p.sql} t ${q.joins.join(" ")} WHERE true${k}${$} ORDER BY ${W} LIMIT ${m+1}`,X=`SELECT p.*, b.${C(I)} FROM (SELECT ${_} AS ${C(I)}) b LEFT JOIN LATERAL (${G}) p ON true ORDER BY p.${C(x)}`,K=await a(X,w),V=K[0]?.[I]===!0,Q=K.filter(H=>H[x]!==null&&H[x]!==void 0),re=Q.length>m;Q=Q.slice(0,m),T&&Q.reverse();let bn=H=>H?xr(y,b,v.map(ne=>H[ne]===null?null:String(H[ne]))):null,fe={startCursor:bn(Q[0]),endCursor:bn(Q.at(-1)),hasNextPage:T?V:re,hasPreviousPage:T?re:V};for(let H of Q)for(let ne of[...v,x,I])delete H[ne];return{rows:c(p,Q,g.with),pageInfo:fe}},async attempt(u){return(await t()).savepoint(g=>u(Tn(()=>Promise.resolve(g),e)))},async txPlan(u){return(await t()).savepoint(async g=>{let p=[];for(let y of u.ops){let S=y.op==="select"||y.op==="delete"?s(y.table,"transaction plan"):r(y.table,"transaction plan"),h=Ua(n,S,y,p),m=h===null?[]:c(S,await g.unsafe(h.sql,h.params)),T={rows:m,rows_affected:m.length};p.push(T),Ba(y,T)}return{results:p}})}};return d}function Fa(t,e){let n;return t.op<0||t.op>=e.length?n=`transaction plan: a reference points at operation ${t.op}, but the plan has run ${e.length} (0\u2026${e.length-1}). A reference can only read an EARLIER operation's row.`:e[t.op].rows[0]?n=`transaction plan: operation ${t.op}'s row has no column "${t.field}" (its columns: ${Object.keys(e[t.op].rows[0]).join(", ")}).`:n=`transaction plan: operation ${t.op} returned NO ROW, so "${t.field}" cannot be read. A reference does not guarantee its row exists \u2014 declare the expectation first (\`.expectOne(err)\`).`,Object.assign(new Error(n),{error_code:"tx_ref_unresolved"})}function Xr(t,e){if(fs(t))throw new Error(`${e}: got an imitation of a plan reference ({ $ref: \u2026 }) \u2014 this object was not made by the plan's builder. A reference is built in your code from an earlier operation's row (insert(...).expectOne(err).id); a filter from a request body cannot carry one.`)}function Yr(t,e){let n=e[t.$ref.op]?.rows[0];if(!n||!(t.$ref.field in n))throw Fa(t.$ref,e);return n[t.$ref.field]}function ot(t,e,n,s,r){if(Xr(e,`transaction plan (${n})`),ft(e))return s(we(t,n,Yr(e,r)));if(ps(e)){let o=e.$expr;return o.fn==="now"?Qe(o,s):`${C(n)} ${o.fn==="inc"?"+":"-"} ${s(we(t,n,o.by))}`}return s(we(t,n,e))}function ss(t,e){if(Xr(t,"transaction plan filter"),ft(t)){let n=Yr(t,e);if(n==null)throw Object.assign(new Error(`transaction plan: a filter's reference to operation ${t.$ref.op}'s "${t.$ref.field}" is NULL. As a filter value NULL means IS NULL \u2014 every row with no value there, not the one the reference named. Write null in the filter when that is what you mean.`),{error_code:"tx_ref_null"});if(typeof n=="object"&&!Array.isArray(n)&&!ms(n))throw Object.assign(new Error(`transaction plan: a filter's reference to operation ${t.$ref.op}'s "${t.$ref.field}" is an object. A filter compares a column with a value; an object there would be read as operators. Reference a scalar column.`),{error_code:"tx_ref_object"});return n}if(Array.isArray(t))return rn(t,t.map(n=>ss(n,e)));if(t!==null&&typeof t=="object"&&!xe(t)&&!on(t)&&!kn(t)&&!(t instanceof Date)){let n=rn(t,{});for(let[s,r]of Object.entries(t))n[s]=ss(r,e);return n}return t}function ts(t,e,n,s,r,o){if(n===void 0||Object.keys(n).length===0&&!on(n))return{sql:"",conditions:0,resolved:{}};let a=ss(n,r);return{...Se(t,e,a,s,o),resolved:a}}function Ua(t,e,n,s){let r=[],o=ee(r),a=`transaction plan ${n.op}`,l=n.op==="insertMany"?[...new Set((n.rows??[]).flatMap(i=>Object.keys(i)))]:Object.keys(n.values??n.set??{});Ze(e,a,l);let c;switch(n.op){case"insert":{let i=n.values??{},d=Object.keys(i),u=d.map(f=>ot(e,i[f],f,o,s));c=d.length?`INSERT INTO ${e.sql} (${d.map(C).join(", ")}) VALUES (${u.join(", ")}) RETURNING ${Z(e)}`:`INSERT INTO ${e.sql} DEFAULT VALUES RETURNING ${Z(e)}`;break}case"upsert":{let i=n.values??{},d=Object.keys(i),u=n.onConflict??[];if(d.length===0||u.length===0)throw Object.assign(new Error(`put on ${n.table} needs columns and onConflict`),{error_code:"tx_bad_upsert"});Y(n.onConflict,"onConflict","which columns a conflict is on");for(let g of u)se(e,g,a,"onConflict column");let f=d.map(g=>ot(e,i[g],g,o,s));c=`INSERT INTO ${e.sql} (${d.map(C).join(", ")}) VALUES (${f.join(", ")}) ${wn(d,u,"update")} RETURNING ${Z(e)}`;break}case"insertMany":{let i=n.rows??[];if(i.length===0)return null;let d=Object.keys(i[0]),u=i.map(p=>`(${d.map(y=>ot(e,p[y],y,o,s)).join(", ")})`),f=n.onConflict??[];Y(n.onConflict,"onConflict","which columns a conflict is on");for(let p of f)se(e,p,a,"onConflict column");if(n.action==="update"&&f.length===0)throw Object.assign(new Error(`insertMany on ${n.table}: an overwrite needs onConflict columns`),{error_code:"tx_bad_upsert"});let g=n.action!==void 0?` ${wn(d,f,n.action)}`:"";c=`INSERT INTO ${e.sql} (${d.map(C).join(", ")}) VALUES ${u.join(", ")}${g} RETURNING ${Z(e)}`;break}case"update":{let i=n.set??{},d=Object.keys(i);if(d.length===0)throw new Error(`update ${n.table}: nothing to set`);let u=d.map(p=>`${C(p)} = ${ot(e,i[p],p,o,s)}`),f=ts(t,e,n.where,o,s,a);en(f,a);let g=e.singleKey;c=g===null||Gr(f.resolved,g)?`UPDATE ${e.sql} AS t SET ${u.join(", ")} WHERE true${f.sql} RETURNING ${Z(e,"t")}`:`WITH locked AS (SELECT t.${C(g)} AS "__pb_lock" FROM ${e.sql} t WHERE true${f.sql} ORDER BY t.${C(g)} FOR NO KEY UPDATE) UPDATE ${e.sql} AS t SET ${u.join(", ")} FROM locked WHERE t.${C(g)} = locked."__pb_lock" RETURNING ${Z(e,"t")}`;break}case"delete":{let i=ts(t,e,n.where,o,s,a);en(i,a),c=`DELETE FROM ${e.sql} AS t WHERE true${i.sql} RETURNING ${Z(e,"t")}`;break}case"select":{let i="";if(n.limit!==void 0){let u=Number(n.limit);if(!Number.isInteger(u)||u<0)throw new Error(`transaction plan (${n.table}): limit must be an integer, got "${String(n.limit)}"`);i=` LIMIT ${u}`}let d=n.lock==="update"?" FOR UPDATE":"";c=`SELECT ${Z(e,"t")} FROM ${e.sql} t WHERE true${ts(t,e,n.where,o,s,a).sql}${i}${d}`;break}default:throw new Error(`unknown operation "${String(n.op)}" in a transaction plan`)}return{sql:c,params:r}}function Ba(t,e){let n=t.guard;if(!n)return;let s=e.rows.length;if(!(n.kind==="one"?s===1:n.kind==="none"?s===0:n.kind==="atLeast"?s>=n.n:s<=n.n))throw Object.assign(new Error(`transaction expectation failed: ${n.kind} (${s} row(s))`),{error_code:"tx_guard_failed",slot:n.slot})}import{AsyncLocalStorage as Qr}from"node:async_hooks";import{randomUUID as Wa}from"node:crypto";var at=new Qr,Jr=()=>({running:new Set}),ja=t=>String(t?.message??t);function Ka(t){return new Ke(`The transaction was rolled back, not committed: a statement in it failed (${ja(t)}) and the code went on. One failed statement aborts the whole transaction \u2014 Postgres refuses every later one and answers COMMIT with ROLLBACK. Wrap a write you expect to fail in \`Database.$attempt(async (tx) => \u2026)\` \u2014 it takes a SAVEPOINT, so only that write rolls back \u2014 or use \`Database.public.<t>.put({ data, onConflict: [...] })\` when what you brace for is a duplicate row.`,t)}async function zr(t){for(;t.running.size;)await Promise.allSettled([...t.running])}function Zr(t,e){let n=e.bind;if(e.lockTimeout!==void 0&&(n+=`, set_config('lock_timeout', ${ue(e.lockTimeout)}, true)`),e.statementTimeout!==void 0&&(n+=`, set_config('statement_timeout', ${ue(e.statementTimeout)}, true)`),!/^select /i.test(n))throw new Error("palgres: the binding statement must be a SELECT list");n+=", pg_catalog.current_setting('transaction_isolation') AS palgres_isolation";let s=null,r=null,o=null,a=null,l=!1,c=null,i=Jr(),d=!0,u,f=!1,g,p=async()=>!e.control||await e.control.drainCancellation()?!0:(g?.abandon?.(),!1),y=()=>{f=!0},S=()=>{if(l)throw new Error("Database transaction has settled; its handle cannot be used again");if(e.control?.check(),e.trace?.check(),at.getStore()!==e.scope)throw new Error("Database.$atomic cannot use a handle from another transaction or identity; use its tx argument")},h,m,T=new Set,E=0,w=!1,A=()=>s!==null&&!l&&f&&i.running.size===0&&T.size===0,k=(O,L,$=S,_,M=new Set)=>{let U=()=>{if($(),m!==_||h?.getStore()!==_)throw new Error("Database connection belongs to another active savepoint; use its tx argument and await it before using the parent handle")};return{unsafe(x,I,v){try{U()}catch(W){return Promise.reject(W)}v?.read!==!0&&(d=!1);let B=(I??[]).map(Ye),q=()=>{let W=O.unsafe(x,B,v),G=Promise.resolve(e.control?e.control.watch(W):W).catch(X=>{throw Nn(X)&&(L.refused??={error:X}),X});return L.running.add(G),E++,G.catch(()=>{}).finally(()=>{L.running.delete(G),y(),--E===0&&!l&&e.onIdle?.()}),G};return e.trace?e.trace.query(x,q):q()},async savepoint(x){U(),d=!1;let I=Symbol("savepoint"),v=new Set,B=Jr();h??=new Qr,m=I;let q=(async()=>{try{return await O.savepoint(async W=>{let G=!1,X=()=>{if($(),G)throw new Error("Database savepoint has settled; its handle cannot be used again")};try{let K=h.run(I,()=>x(k(W,B,X,I,v))),V=await(e.control?e.control.race(K):K);if(await zr(B),B.refused)throw B.refused.error;return V}finally{v.size&&await Promise.allSettled([...v]),G=!0}})}finally{m=_}})();T.add(q),M.add(q);try{return await q}finally{T.delete(q),M.delete(q)}}}},R=async()=>(S(),s||(s=new Promise((O,L)=>{let $=new Promise((_,M)=>{r=_,o=M});$.catch(()=>{}),a=(async()=>{let _=performance.now(),M;try{M=await(e.acquire?e.acquire(e.control?.signal):e.admission.acquire(e.claims,e.control?.signal))}finally{e.trace?.time("queueMs",_)}let U=performance.now(),x,I=e.control!==void 0&&t.cancelBackend!==void 0,v=[];if(e.isolation!==void 0||e.readOnly!==void 0){let K=e.isolation??"read committed";vn({isolation:K,readOnly:e.readOnly}),v.push(`SET TRANSACTION ISOLATION LEVEL ${K.toUpperCase()}${e.readOnly===void 0?"":e.readOnly?" READ ONLY":" READ WRITE"}`)}v.push(I?`WITH backend AS MATERIALIZED (SELECT pid, backend_start::text AS started, xact_start::text AS xact_started FROM pg_catalog.pg_stat_get_activity(pg_catalog.pg_backend_pid())) ${n}, backend.pid, backend.started, backend.xact_started FROM backend`:n);let B=I&&t.cancelOpening!==void 0?`palgres:${Wa()}`:void 0,q=!1,W=K=>{f=!0;let V=K[0];if(typeof V?.palgres_isolation=="string"&&(u=V.palgres_isolation),!I)return;if(!Number.isInteger(V?.pid)||typeof V?.started!="string"||typeof V?.xact_started!="string"){let re=new Error("Database cancellation cannot identify its own backend");throw i.refused??={error:re},re}let Q={pid:V.pid,started:V.started,xactStarted:V.xact_started};q=!0,e.control.bindBackend(()=>t.cancelBackend(Q.pid,Q.started,Q.xactStarted))},G=async()=>{for(let K=0;K<400&&!f&&!q;K++){if(await t.cancelOpening(B))return;await new Promise(V=>setTimeout(V,5))}},X=K=>t.begin(async V=>{x=performance.now(),e.trace?.time("poolMs",U);let Q=e.trace?performance.now():0;if(e.control?.check(),l)throw new Error("Database transaction closed while waiting for its connection");B!==void 0&&!q&&e.control.bindBackend(G),e.trace?.time("setupMs",Q),K(),g=V,O(k(V,i)),await $},{sql:v.join("; "),opened:W,tag:B});try{if(e.control?.check(),l)throw new Error("Database transaction closed while waiting for admission");let K=!1;for(let V=1;;V++)try{return await X(()=>{K=!0})}catch(Q){let re=ce(Q);if(K||l||re===void 0||V>=(re==="lost"?Yn:Jn))throw Q;e.control?.check()}}finally{x!==void 0&&e.trace?.time("connectionMs",x),M.release()}})().catch(_=>{throw L(_),_}),a.catch(()=>{})}),s.then(void 0,()=>{w=!0,l||e.onIdle?.()}),s)),b=O=>c??=(async()=>{if(l=!0,!s)return;await p(),T.size&&await Promise.allSettled([...T]);let L=e.trace?performance.now():0;o(O),await a?.catch(()=>{}),e.trace?.time("rollbackMs",L),e.trace?.rolledBack()})();return{ensure:R,get opened(){return s!==null},get busy(){return s!==null&&!l&&!w&&(!f||E>0)},get failed(){return w},get releasable(){return A()&&d&&i.refused===void 0&&(u==="read committed"||u==="read uncommitted")},async commit(){if(T.size&&await Promise.allSettled([...T]),!s){e.trace?.check(),l=!0;return}await zr(i);try{if(e.trace?.check(),e.control?.checkDeadline(),i.refused)throw Ka(i.refused.error)}catch(L){throw await b(L),L}if(l=!0,!await p()){let L=be(new oe(503,"database_unavailable","The database is temporarily unavailable",{retrySafe:!1},1));throw await b(L),L}let O=e.trace?performance.now():0;if(r(),d&&u!==void 0&&u!=="serializable"){a.then(()=>e.trace?.committed(),()=>e.trace?.commitFailed());return}try{await a,e.trace?.committed()}catch(L){throw e.trace?.commitFailed(),L}finally{e.trace?.time("commitMs",O)}},rollback:b}}var Ha="5s",Va="10s",Ga=new Set(["22P02","22007","22008","22003","22001"]),eo="55s",ro=1,Xa=1e3,Sn=new Set,$n,Ya=new so("palgres.release");function Ja(t){Sn.add(t),$n===void 0&&($n=Ya.runInAsyncScope(()=>setInterval(()=>{let e=performance.now();for(let n of Sn){let s=!0;try{s=n(e)}catch{s=!0}s&&Sn.delete(n)}Sn.size===0&&(clearInterval($n),$n=void 0)},ro)),$n.unref?.())}function za(t,e){let n=[],s=d=>d!==null&&d.opened&&!d.failed,r=new Set,o,a=!1,l=()=>{for(let d of n)if(s(d.tx)&&d.tx.busy)return!0;for(let d of r)if(d.busy)return!0;return!1},c=so.bind(d=>{if(o===void 0||l())return!0;let u=r.size,f=r.size===0;for(let p of n)s(p.tx)&&(u++,p.tx.releasable||(f=!1));if(u===0)return!0;let g=d-o;if(f){if(g<ro)return!1;for(let p of n){let y=p.tx;s(y)&&(p.tx=null,p.lease?.leave(),p.lease=void 0,y.commit().catch(()=>{}))}return o=void 0,!0}return e===void 0||a?!0:g<Xa?!1:(a=!0,e(Math.round(g)),!0)}),i=()=>{l()||(o=performance.now(),Ja(c))};return{onIdle:i,surface(d,u){let f={tx:null};n.push(f);let g=()=>{Sn.delete(c);let p=f.tx;return f.tx=null,f.lease=void 0,p};return{ensure(){if(o!==void 0&&(o=performance.now()),f.tx===null){if(t())return Promise.reject(new Error("Database transaction has settled; its handle cannot be used again"));f.tx=d(i,p=>u(p).then(y=>(f.lease=y,y)))}return f.tx.ensure()},commit:()=>g()?.commit()??Promise.resolve(),rollback:p=>g()?.rollback(p)??Promise.resolve()}},atomic(d){return r.add(d),()=>{r.delete(d)&&i()}}}}function Qa(t){let e=ae(t),n=t?.code,s=o=>e!==void 0&&o.has(e)||typeof n=="string"&&o.has(n),r;if(s(ls))r=!0;else if(s(cs))r=!1;else return;return Ce(new oe(503,"database_unavailable","The database is temporarily unavailable",{retrySafe:r},1),t)}var nn=t=>String(t?.message??t);function Ce(t,e){return t.cause=e,be(t)}function rs(t,e,n){if(typeof t=="object"&&t!==null&&no.has(t))return t;let s=Za(t,e,n);return typeof s=="object"&&s!==null&&no.add(s),s}var no=new WeakSet,it=(t,e,n)=>Object.assign(new Error(t,{cause:e}),{sqlstate:n});function Za(t,e,n){if(Ke.is(t))return t;let s=ae(t);if(s===te.inFailedTransaction||/current transaction is aborted/i.test(nn(t)))return it(`This request cannot write any more: an earlier write in it failed, and a request runs in ONE Postgres transaction, so every statement after the failure is refused. Wrap a write you expect to fail in \`Database.$attempt(async (tx) => \u2026)\` \u2014 it takes a SAVEPOINT, so only that write rolls back \u2014 or use \`Database.public.<t>.put({ data, onConflict: [...] })\` when what you brace for is a duplicate row. (${nn(t)})`,t,te.inFailedTransaction);if(We.is(t))return t;if(s===te.uniqueViolation)return Ce(new We(sn(t)),t);if(s===te.serializationFailure)return Ce(new dt(s),t);if(s===te.deadlockDetected)return Ce(new mt(s),t);if(s===te.queryCancelled)return Ce(Object.assign(new oe(503,"database_query_cancelled","Database statement was cancelled"),{sqlstate:s}),t);let r=Qa(t);if(r)return r;if(e==="service"&&s===te.foreignKeyViolation)return it(`Database.$asService() hit a foreign key with nothing to point at. It runs in its OWN transaction on its OWN connection, so a row this request wrote through \`Database.*\` is not visible to it until the request commits \u2014 and the request cannot commit until the handler returns. If the row it needs is one this request just created, do both writes on ONE surface. (${nn(t)})`,t,s);if(/lock timeout/i.test(nn(t))){let o;return e==="service"?o=it(`Database.$asService() waited too long for a row lock. It runs in its OWN transaction, so a row this request already wrote through Database.* is locked against it until the request commits \u2014 a wait that cannot end. Do that row's work on one surface or the other. (${nn(t)})`,t,s??te.lockNotAvailable):o=it(`Database waited too long for a lock. Another transaction holds it: a concurrent one, or another of this request's own \u2014 Database.*, $asService() and $atomic each run in their OWN transaction, and a row one of them wrote stays locked against the others until it commits, a wait that cannot end while the handler runs. Do that row's work on one surface. (${nn(t)})`,t,s??te.lockNotAvailable),Ce(Object.assign(new oe(503,"database_busy","The database waited too long for a lock",{retrySafe:!1},1),{sqlstate:s??te.lockNotAvailable}),o)}return s!==void 0&&Ga.has(s)?Ce(Object.assign(new oe(400,"invalid_value","A value is not one its column can take"),{sqlstate:s}),t):s===te.insufficientPrivilege&&n!==null?Ce(Object.assign(new oe(n==="anon"?401:403,"permission_denied","The database does not allow this caller to do this"),{sqlstate:s}),t):(s!==void 0&&typeof t=="object"&&t!==null&&!Object.hasOwn(t,"sqlstate")&&Object.defineProperty(t,"sqlstate",{value:s,enumerable:!0,configurable:!0}),t)}function ei(t,e,n){let s=r=>({unsafe(o,a,l){let c;try{c=r.unsafe(o,a,l)}catch(i){return Promise.reject(rs(i,e,n))}return Promise.resolve(c).then(Ve,i=>{throw rs(i,e,n)})},savepoint:o=>r.savepoint(a=>o(s(a))),...r.abandon?{abandon:r.abandon.bind(r)}:{}});return{unsafe:(r,o)=>t.unsafe(r,o),begin:(r,o)=>t.begin(a=>r(s(a)),o).catch(a=>{throw rs(a,e,n)}),...t.cancelBackend?{cancelBackend:t.cancelBackend.bind(t)}:{},...t.cancelOpening?{cancelOpening:t.cancelOpening.bind(t)}:{}}}var ni=2,to={anon:"anon",authenticated:"authenticated",service:"service"};function ti(t,e){let n=Object.hasOwn(to,e.role)?to[e.role]:void 0;if(n===void 0)throw new Error(`palgres: a request is anon, authenticated or service, not "${String(e.role)}"`);return t[n]}function oo(t,e,n,s={}){let{signal:r}=s,o=s.diagnostics===void 0?void 0:new nt(s.diagnostics),a=r===void 0?void 0:new Je(void 0,r),l=ti(e.roles,n),c=e.roles.service,i=n.role==="anon"||n.role==="authenticated"?n.role:null,d=n.claims,u=!1,f=!1,g=()=>{f&&Bn.committed()},p=I=>{let v;return{cat:e.cat,trace:o,cursorKey:e.cursorKey,cursorScope:()=>v??=Lr(I,d),search:e.search,searchable:()=>{f=!0}}},y=p(l),S=p(c),h=new Set,m=null,T=!1,E=0,w=new Set,A=I=>new Promise((v,B)=>{let q=K=>{w.delete(W),clearTimeout(X),I?.removeEventListener("abort",G),K===void 0?v():B(K)},W=()=>q(),G=()=>q(I?.reason??new Error("Database admission cancelled")),X=setTimeout(()=>q(new Pe({retryAfter:1},"Database queue deadline exceeded")),e.admission.queueTimeoutMs);w.add(W),I?.addEventListener("abort",G,{once:!0}),I?.aborted&&G()}),k=async I=>{let v;for(;;){for(;h.size===0&&m!==null;)await m;let W=h.size>0;if(W){for(;E>=ni;)await A(I);E++}let G;try{let X=e.admission.acquire(d,I,{nested:W,returning:!W&&T}).then(K=>{if(W&&h.size===0)return K.release(),null;let V={nested:W,turn:K};return h.add(V),W||(T=!0),V});if(!W){let K=X.then(()=>{},()=>{});m=K,K.then(()=>{m===K&&(m=null)})}G=await X}catch(X){throw W&&R(),X}if(G!==null){v=G;break}R()}let B=!1,q=()=>{if(!B){if(B=!0,h.delete(v),v.nested){R();return}for(let W of h){[v.turn,W.turn]=[W.turn,v.turn],W.nested=!1,v.nested=!0,R();return}}};return{queueMs:v.turn.queueMs,leave:q,release:()=>{q(),v.turn.release()}}},R=()=>{E--;let I=w.values().next().value;I?.()},b=za(()=>u,s.heldIdle),O=(I,v,B={})=>Zr(ei(t,v,v==="user"?i:null),{bind:fn(e.binding,I,d),claims:d,admission:e.admission,acquire:k,trace:o,...B}),L=(I,v)=>async(B,q={})=>{if(vn(q),u)throw new Error("Database request has settled; it cannot open another transaction");if(at.getStore()!==void 0)throw new Error("Nested Database.$atomic is not supported; use tx.$attempt for a savepoint");let W=In(q.retry),G=q.signal&&r?AbortSignal.any([q.signal,r]):q.signal??r,X=q.timeoutMs!==void 0||G!==void 0?new Je(q.timeoutMs,G):void 0;try{X?.check();for(let K=0;;K++){X?.check();let V=[q.statementTimeoutMs,X?.remainingMs()].filter(fe=>fe!==void 0),Q=Symbol("atomic transaction"),re=O(I,v,{scope:Q,isolation:q.isolation,readOnly:q.readOnly,control:X,lockTimeout:`${q.lockTimeoutMs??5e3}ms`,statementTimeout:V.length===0?void 0:`${Math.min(...V)}ms`,onIdle:b.onIdle}),bn=b.atomic(re);try{let fe=Tn(()=>re.ensure(),v==="user"?y:S),H=at.run(Q,()=>B(fe)),ne=await(X?X.race(H):H);return await re.commit(),g(),ne}catch(fe){await re.rollback(fe);let H=je(fe)||Ke.is(fe)&&je(fe.cause);if(K>=W||!H)throw fe;o?.retry();let ne=new Promise(uo=>setTimeout(uo,Math.min(100,2**K)));await(X?X.race(ne):ne)}finally{bn()}}}finally{X?.dispose()}},$=b.surface((I,v)=>O(l,"user",{lockTimeout:Va,statementTimeout:eo,control:a,onIdle:I,acquire:v}),k),_=null,M,U=null,x=()=>{if(U===null){if(u)throw new Error("Database request has settled; it cannot open another transaction");M=r===void 0?void 0:new Je(void 0,r);let I=M,v=b.surface((B,q)=>O(c,"service",{lockTimeout:Ha,statementTimeout:eo,control:I,onIdle:B,acquire:q}),k);_=v,U=Object.assign(Tn(()=>v.ensure(),S),{atomic:L(c,"service")})}return U};return{client:Object.assign(Tn(()=>$.ensure(),y),{asService:x,atomic:L(l,"user")}),async commit(){u=!0;try{try{o?.check()}catch(I){throw await $.rollback(I),await _?.rollback(I),I}try{await $.commit()}catch(I){throw await _?.rollback(I),I}await _?.commit(),g(),o?.complete("committed"),a?.dispose(),M?.dispose()}catch(I){throw o?.failed(),I}},async rollback(I){u=!0,await $.rollback(I),await _?.rollback(I),a?.dispose(),M?.dispose(),o?.complete("failed")}}}function si(t){if(t.codec!==void 0&&t.codec in He)return He[t.codec].tsType;switch(t.type){case"uuid":case"text":case"timestamp":case"date":return"string";case"sql":return t.sqlType!==void 0&&An.has(t.sqlType)?"number":t.sqlType!==void 0&&On.has(t.sqlType)?"Json":"string";case"integer[]":return"number[]";case"boolean[]":return"boolean[]";case"uuid[]":case"text[]":case"bigint[]":case"numeric[]":case"timestamp[]":case"date[]":return"string[]";case"integer":return"number";case"bigint":case"numeric":return"string";case"boolean":return"boolean";case"jsonb":return"Json";case"enum":{let e=t.enumValues??[];return e.length===0?"string":e.map(n=>JSON.stringify(n)).join(" | ")}case"enum[]":{let e=t.enumValues??[];return e.length===0?"string[]":`(${e.map(n=>JSON.stringify(n)).join(" | ")})[]`}default:{let e=t.type;throw new Error(`env-gen: the column type "${String(e)}" has no TypeScript type \u2014 add a case to baseTsType`)}}}function ri(t){if(t.type==="enum"){let e=[...t.enumValues??[]].sort();return e.length===0?"enum":`enum:${e.join("|")}`}return t.type}function ao(t){let e=`Pg<${si(t)}, ${JSON.stringify(ri(t))}>`;return t.nullable?`${e} | null`:e}function oi(t){return t.nullable===!0||t.defaultRandom===!0||t.defaultNow===!0||t.defaultValue!==void 0||t.defaultSql!==void 0||t.dbAssigned===!0||t.identity!==void 0}function ai(t){return t.identity==="always"||t.generated!==void 0}var Rn=t=>/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)?t:JSON.stringify(t);function io(t,e,n){let s=Object.entries(t.columns).filter(([,l])=>l._def.ignored!==!0),r=s.map(([l,c])=>`${n} ${Rn(l)}: ${ao(c._def)};`),o=s.filter(([,l])=>!ai(l._def)).map(([l,c])=>`${n} ${Rn(l)}${oi(c._def)?"?":""}: ${ao(c._def)};`),a=e.map(l=>`${Rn(l.name)}: { to: ${JSON.stringify(l.to)}; kind: ${JSON.stringify(l.kind)}; via: ${JSON.stringify(l.via)} }`).join("; ");return[`${n}${Rn(t.name)}: {`,`${n} row: {`,...r,`${n} };`,`${n} insert: {`,...o,`${n} };`,`${n} relations: {${a===""?"":` ${a} `}};`,`${n} uniqueKeys: ${JSON.stringify(Qn(t))};`,...t.search!==void 0?[`${n} searchable: true;`]:[],`${n}};`].join(`
329
- `)}function ii(t){for(let e of t)for(let n of Object.values(e.tables))for(let[s,r]of Object.entries(n.columns)){let o=r._def;if(!(o.transform===void 0||o.codec!==void 0))throw new _e([`table "${he(e.name,n.name)}" column "${s}" declares .transform<T>(): its target type does not exist at runtime, so the generated types would say something the engine contradicts. Declare a named codec instead \u2014 .asNumber() for a JS number, .asDecimal() for the exact string.`])}}var lo="// palbase:env:begin",co="// palbase:env:end";function li(t,e={}){let n=e.module??"palbase/backend/env";ii(t);let s=hn(t),r=u=>Object.values(u.tables).filter(f=>f.ignored!==!0),o=t.find(u=>u.name==="public"),a=t.filter(u=>u.name!=="public"),l=(o?r(o):[]).map(u=>io(u,s.get(u.name)??[]," ")),c=l.length>0?`
330
- ${l.join(`
331
- `)}
332
- `:"",i=a.map(u=>{let f=r(u).map(g=>io(g,s.get(he(u.name,g.name))??[]," "));return` ${Rn(u.name)}: {
333
- ${f.join(`
334
- `)}
335
- };`}),d=i.length>0?`
336
- ${i.join(`
337
- `)}
338
- `:"";return`// AUTO-GENERATED by Palgres' makeEnvDts from the app's schema files \u2014 DO NOT EDIT.
339
- // Nothing writes it again on its own: run makeEnvDts again when a schema changes.
340
- // Augments the ${n} \`Tables\` interface so \`Database.public.*\`
341
- // is typed with no import and no generic. Schemas other than \`public\` land
342
- // under \`Schemas\` and are reached by name the same way:
343
- // \`Database.<schema>.<table>\`.
344
-
345
- /**
339
+ `).slice(0,2500));let o=performance.now(),a=!1;try{let i=await n();return r.durationMs=performance.now()-o,a=!0,Array.isArray(i)&&(r.rows=i.length,r.bytes=Buffer.byteLength(JSON.stringify(i,(c,l)=>typeof l=="bigint"?l.toString():l))),this.state.rows+=r.rows,this.state.bytes+=r.bytes,this.options.maxRows!==void 0&&this.state.rows>this.options.maxRows&&this.refuse("returned rows"),this.options.maxBytes!==void 0&&this.state.bytes>this.options.maxBytes&&this.refuse("result bytes"),i}catch(i){let c=i;throw r.errorCode=String(c?.errno??c?.code??"query_failed").slice(0,80),i}finally{a||(r.durationMs=performance.now()-o),this.state.sqlMs+=r.durationMs;let i=Object.freeze(r);this.state.statements.length<200&&this.state.statements.push(i),this.emit(this.options.onQuery,i)}}get snapshot(){return Object.freeze({...this.state,elapsedMs:(this.ended??performance.now())-this.start,statements:Object.freeze([...this.state.statements].sort((e,n)=>e.ordinal-n.ordinal))})}complete(e){this.ended===void 0&&(this.ended=performance.now(),this.state.outcome=e,this.emit(this.options.onComplete,this.snapshot))}};var Kr=new WeakMap,Hr=new WeakSet;function ee(t){let e=n=>`$${t.push(n)}`;return Kr.set(e,t),e}var Qt=t=>Hr.has(t);var Vr=t=>`'${t.replace(/'/g,"''")}'`;function Se(t,e){let n=t.columns.get(e);return n?Cr(n):null}function se(t,e,n,s){if(!t.columns.has(e))throw t.ignored.has(e)?new Error(`${n}(${t.key}): ${s} "${e}" is .ignored() in this version \u2014 the version has promised not to name it, so the next one can drop it`):new Error(`${n}(${t.key}): ${s} "${e}" is not a column of the table`)}function we(t,e,n){if(n==null)return n;let s=t.transforms.get(e)?.toDb,r=s===void 0?n:s(n),o=Se(t,e);return o===null||r===null||r===void 0?r:o.endsWith("[]")&&Array.isArray(r)?En(r):_r(o)?JSON.stringify(r):r}function Gr(t,e,n){let s=n.find(r=>t.databaseOnly.has(r));if(s!==void 0)throw new Error(`${e}(${t.key}): "${s}" is written by the database (an ALWAYS .identity()) \u2014 leave it out of the write; Postgres refuses any value for it`)}function nn(t,e,n){for(let s of n)se(t,s,e,"column");Gr(t,e,n)}function Xr(t,e,n,s){return xn(e,t.key,[n],s),bs(e,t.key,[n],s),we(t,n,s[n])}function Zt(t,e,n){let s=`${n?`${_(n)}.`:""}${_(e)}`;return Ue(Se(t,e),s)??s}function Yr(t,e,n){let s=Zt(t,e,n);return Ue(Se(t,e),"x")!==null?`${s} AS ${_(e)}`:s}function Z(t,e){let n=e??"",s=t.projections.get(n);return s===void 0&&(s=[...t.columns.keys()].map(r=>Yr(t,r,e)).join(", "),t.projections.set(n,s)),s}function es(t,e,n){if(Y(e,"select","which columns a call reads"),e!==void 0&&e.length===0)throw new Error(`${n}(${t.key}): select got an empty list \u2014 that is not "all of them" but "none counted". Leave the key out for every column; name at least one otherwise.`);return e===void 0?Z(t,"t"):e.map(s=>(se(t,s,n,"select column"),Yr(t,s,"t"))).join(", ")}function Jr(t,e,n,s="t"){if(e==null)return"";Y(e,"orderBy","the order");let r=Array.isArray(e)?e:[e];return r.length===0?"":` ORDER BY ${r.map(a=>{Y(a,"orderBy","the order"),se(t,a.column,n,"orderBy column");let i=a.direction==="desc"?"DESC":"ASC",c=a.nulls==="first"?" NULLS FIRST":a.nulls==="last"?" NULLS LAST":"";return`${_(s)}.${_(a.column)} ${i}${c}`}).join(", ")}`}function zt(t,e="findMany"){if(t===void 0)return"";if(!Number.isInteger(t)||t<0)throw new Error(`${e}: limit must be a non-negative integer (got ${String(t)})`);return` LIMIT ${t}`}function qa(t,e,n="findMany"){if(t===void 0)return"";if(!Number.isInteger(t)||t<0)throw new Error(`${n}: offset must be a non-negative integer (got ${String(t)})`);if(e===void 0)throw new Error(`${n}: offset is given only with limit \u2014 an offset without one is the shifted tail of an unbounded result, not a page`);return` OFFSET ${t}`}var Ze={gt:">",gte:">=",lt:"<",lte:"<=",neq:"<>"},Fa=[["days","days","int"],["hours","hours","int"],["minutes","mins","int"],["seconds","secs","double precision"]];function en(t,e){let n=t?.minus;if(n===void 0)return"now()";let s=As(n);if("refused"in s)throw new TypeError(`palgres: ago() ${s.refused}`);return`(now() - make_interval(${Fa.filter(([o])=>s.span[o]!==void 0).map(([o,a,i])=>`${a} => ${e(s.span[o])}::${i}`).join(", ")}))`}var Br=Object.freeze({update:"FOR UPDATE",share:"FOR SHARE",noKeyUpdate:"FOR NO KEY UPDATE"});function ns(t,e,n,s){if(typeof s=="string"&&Object.hasOwn(Br,s))return Br[s];let r=typeof s=="string"?JSON.stringify(s.slice(0,40)):typeof s;throw new TypeError(`${t}(${e}): unknown ${n} ${r} \u2014 "update" (FOR UPDATE), "share" (FOR SHARE) or "noKeyUpdate" (FOR NO KEY UPDATE).`)}var Wr=t=>vn(t);function jr(t,e,n,s){if(!t.columns.has(e.$col))throw new Error(`${n}(${t.key}): col("${e.$col}") is not a column of the table`);return`${_(s)}.${_(e.$col)}`}function zr(t,e,n,s){let o=(n.kind==="many"?t.table(n.to,s):e).columns.get(n.via)?.references?.column??(n.kind==="many"?e.singleKey:t.table(n.to,s).singleKey);return n.kind==="many"?[o,n.via]:[n.via,o]}function Be(t,e,n){return Ne(new Ae({fields:[{field:e,message:n}]},`${t}: ${e} ${n}`))}function Ua(t,e){if(Y(t,e,"a filter"),!(typeof t!="object"||t===null))for(let[n,s]of Object.entries(t))n==="OR"||n==="AND"||n==="NOT"||n==="has"?Y(s,`${e}.${n}`,`a filter's ${n}`):s!==null&&typeof s=="object"&&!Array.isArray(s)&&Y(s,`${e}.${n}`,"an operator")}function it(t,e,n,s,r,o,a,i){let c=[];if(Ua(n,i),Ns(r,e.key,n),ln(n)){let{text:l,values:u}=n.$sql,g=Kr.get(s);if(g===void 0)throw new Error("palgres: an sqlFragment was compiled outside binder()");if(Hr.add(g),l.length!==u.length+1)throw new Error(`${r}(${e.key}): the sqlFragment's parts are broken (${l.length} texts, ${u.length} values) \u2014 only the template tag makes one`);let p=l[0]??"";for(let h=0;h<u.length;h++)p+=`${s(u[h])}${l[h+1]??""}`;if(p.trim()==="")throw Be(r,i,"is an empty sqlFragment \u2014 no condition. Write the condition in it, or leave it out.");return[`(${p})`]}for(let[l,u]of Object.entries(n)){if(l==="OR"||l==="AND"){let m=u;if(m.length===0){if(l==="AND")throw Be(r,`${i}.AND`,"is an empty list \u2014 no condition at all. Leave it out, or name a condition in it.");c.push("false");continue}let y=m.map((S,f)=>{let d=it(t,e,S,s,r,o,a,`${i}.${l}[${f}]`);if(d.length===0)throw Be(r,`${i}.${l}[${f}]`,"holds no condition \u2014 a branch that matches every row. Name a condition in it, or leave it out.");return`(${d.join(" AND ")})`});c.push(`(${y.join(l==="OR"?" OR ":" AND ")})`);continue}if(l==="NOT"){if(Array.isArray(u))throw Be(r,`${i}.NOT`,"is a list \u2014 NOT takes one filter object. For several conditions, NOT: { AND: [...] } or NOT: { OR: [...] }.");let m=it(t,e,u,s,r,o,a,`${i}.NOT`);if(m.length===0)throw Be(r,`${i}.NOT`,"holds no condition \u2014 NOT of nothing is not a filter. Name the condition to exclude.");c.push(`NOT (${m.join(" AND ")})`);continue}if(l==="has"){let m=Object.entries(u);if(m.length===0)throw Be(r,`${i}.has`,"names no relation \u2014 no condition. Name one: has: { <relation>: { \u2026 } }.");for(let[y,S]of m){let f=e.relations.find(N=>N.name===y);if(f===void 0){let N=e.relations.map(A=>A.name).sort();throw new Error(`${r}(${e.key}): there is no relation has.${y}`+(N.length>0?` (known: ${N.join(", ")}). Relations come from foreign keys; to rename one, references(..., { as, reverseAs }).`:". The table declares no foreign key, and relations come from them: references(() => <table>.<column>)."))}let[d,T]=zr(t,e,f,r);if(d===null||T===null)throw new Error(`${r}(${e.key}): has.${y} cannot be built \u2014 "${d===null?e.key:f.to}" has no single-column primary key to correlate on; write the condition as an sqlFragment`);let E=t.table(f.to,r),w=`r${a+1}`,O=lt(t,E,S??{},s,r,w,a+1,`${i}.has.${y}`),k=`${w}.${_(T)} = ${_(o)}.${_(d)}`;c.push(`EXISTS (SELECT 1 FROM ${E.sql} ${w} WHERE ${k}${O===""?"":` AND ${O}`})`)}continue}se(e,l,r,"where column");let g=`${_(o)}.${_(l)}`,p=Se(e,l)?.endsWith("[]")===!0,h=m=>s(we(e,l,m));if(xe(u)){c.push(`${g} = ${jr(e,u,r,o)}`);continue}if(Tt(u)){c.push(`${g} = ${en(Wr(u),s)}`);continue}if(u!==null&&typeof u=="object"&&!Array.isArray(u))for(let[m,y]of Object.entries(u)){if(p&&(m==="contains"||m==="overlaps")){if(!Array.isArray(y))throw new Error(`${r}(${e.key}): where.${l}.${m} must be an array \u2014 ${l} is an array column`);c.push(`${g} ${m==="contains"?"@>":"&&"} ${h(y)}`);continue}if(!p&&(m==="contains"||m==="icontains"||m==="startsWith"||m==="endsWith")){if(typeof y!="string")throw new Error(`${r}(${e.key}): where.${l}.${m} must be a string`);let f=y.replace(/([\\%_])/g,"\\$1"),d=m==="startsWith"?`${f}%`:m==="endsWith"?`%${f}`:`%${f}%`;c.push(`${g} ${m==="icontains"?"ILIKE":"LIKE"} ${s(d)}`);continue}if(m==="isNull"){if(typeof y!="boolean")throw new Error(`${r}(${e.key}): where.${l}.isNull must be a boolean`);c.push(`${g} IS ${y?"NULL":"NOT NULL"}`);continue}if(m==="in"){if(!Array.isArray(y))throw new Error(`${r}(${e.key}): where.${l}.in must be an array`);if(y.some(xe))throw new Error(`${r}(${e.key}): where.${l}.in takes values, not col() \u2014 compare with a column bare ({ ${l}: col("\u2026") }) or with gt/gte/lt/lte/neq`);if(y.length===0){c.push("false");continue}c.push(`${g} IN (${y.map(f=>h(f)).join(", ")})`);continue}if(Tt(y)){if(!(m in Ze))throw new Error(`${r}(${e.key}): where.${l}.${m} cannot take now() \u2014 for equality write it bare: { ${l}: now() }`);c.push(`${g} ${Ze[m]} ${en(Wr(y),s)}`);continue}if(In(y))throw new Error(`${r}(${e.key}): where.${l}.${m} got a WRITE expression (increment/decrement), not a comparison value. For the server's time use now(); for a numeric threshold, the value.`);if(xe(y)){if(!(m in Ze))throw new Error(`${r}(${e.key}): where.${l}.${m} cannot take col() \u2014 for equality write it bare: { ${l}: col("\u2026") }`);c.push(`${g} ${Ze[m]} ${jr(e,y,r,o)}`);continue}let S=Rs(y);if(S!==null){let f=S==="col"?"col()":S==="sql"?"sqlFragment":"now()";throw new Error(`${r}(${e.key}): where.${l}.${m} got an imitation of ${f} \u2014 this object was not made by ${f} in this process. A filter from a request body cannot carry a handle; build the condition in your code.`)}if(m in Ze){if(y===null&&m==="neq"){c.push(`${g} IS NOT NULL`);continue}c.push(`${g} ${Ze[m]} ${h(y)}`);continue}throw new Error(`${r}(${e.key}): where.${l} has an unknown operator "${m}" (gt/gte/lt/lte/neq/in)`)}else u===null?c.push(`${g} IS NULL`):c.push(`${g} = ${h(u)}`)}return c}function lt(t,e,n,s,r,o="t",a=0,i="where"){return it(t,e,n,s,r,o,a,i).join(" AND ")}function Re(t,e,n,s,r){let o=it(t,e,n,s,r,"t",0,"where");return{sql:o.length===0?"":` AND ${o.join(" AND ")}`,conditions:o.length}}function tn(t,e){if(!(t.conditions>0))throw Be(e,"where",`holds no condition, so ${e} would reach EVERY row of the table. Name a condition.`)}function Tn(t,e,n){if(n!=="ignore"&&n!=="update")throw new TypeError(`palgres: a conflict's action is "ignore" or "update", not ${typeof n=="string"?JSON.stringify(String(n).slice(0,40)):typeof n}`);if(e.length===0){if(n==="ignore")return"ON CONFLICT DO NOTHING";throw new Error("palgres: ON CONFLICT DO UPDATE needs the columns a collision is matched on")}let s=`ON CONFLICT (${e.map(_).join(", ")})`;if(n==="ignore")return`${s} DO NOTHING`;let r=new Set(e),o=t.filter(a=>!r.has(a)).map(a=>`${_(a)} = EXCLUDED.${_(a)}`);return o.length?`${s} DO UPDATE SET ${o.join(", ")}`:`${s} DO UPDATE SET ${_(e[0])} = EXCLUDED.${_(e[0])}`}function ts(t,e,n,s,r){return Gr(t,r,e),e.map(o=>{let a=vn(n[o]);if(a!==null&&a.fn==="now")return`${_(o)} = ${en(a,s)}`;if(a!==null){let i=t.transforms.get(o)?.toDb,c=i===void 0?a.by:i(a.by);return`${_(o)} = ${_(o)} ${a.fn==="inc"?"+":"-"} ${s(c)}`}return`${_(o)} = ${s(Xr(t,r,o,n))}`}).join(", ")}function $n(t,e,n,s,r){return e.map(o=>{let a=vn(n[o]);if(a!==null&&a.fn==="now")return en(a,s);if(a!==null){let i=a.fn==="inc"?"increment":"decrement";throw new Error(`${r}(${t.key}): "${o}" got ${i}() \u2014 the row does not exist yet, so there is no current value. Write the starting value; to change it once the row exists, updateMany({ where, set: { ${o}: ${i}(n) } }).`)}return s(Xr(t,r,o,n))}).join(", ")}var Ba=new Set(["integer","bigint","numeric"]);function Qr(t){return t!==null&&Ba.has(t)}function Zr(t,e,n,s){let r=`t.${_(n)}`,o=Se(t,n);if(e==="sum"||e==="avg")return`${e}(${r})::text AS ${_(s)}`;let a=o!==null&&!o.endsWith("[]")?Ue(o,`${e}(${r})`):null;return a!==null?`${a} AS ${_(s)}`:o==="bigint"||o==="numeric"?`${e}(${r})::text AS ${_(s)}`:`${e}(${r}) AS ${_(s)}`}function Wa(t,e,n,s=[]){return`json_build_object(${[...e.map(o=>{let a=Se(t,o),i=`${n}.${_(o)}`,c=a==="numeric"||a==="bigint"?`${i}::text`:Ue(a,i)??i;return`${Vr(o)}, ${c}`}),...s].join(", ")})`}function ct(t,e,n,s,r,o="t",a=0,i={value:0}){if(Y(n,"with","which relations a call reads"),a>=3&&Object.values(n).some(Boolean))throw new Error(`${r}(${e.key}): with supports at most three relation levels`);let c=[],l=[],u=[];for(let[g,p]of Object.entries(n)){if(p===!1||p===void 0)continue;Y(p,`with.${g}`,"which relations a call reads");let h=e.relations.find(v=>v.name===g);if(h===void 0){let v=e.relations.map(I=>I.name).sort();throw new Error(`${r}(${e.key}): there is no relation with.${g}`+(v.length>0?` (known: ${v.join(", ")})`:". The table declares no foreign key, and relations come from them")+". A relation is declared with references(() => <table>.<column>).")}let[m,y]=zr(t,e,h,r);if(m===null||y===null)throw new Error(`${r}(${e.key}): with.${g} needs a single-column primary key at one end of the relation`);let S=t.table(h.to,r),f=typeof p=="object"&&p!==null?p:{},d=++i.value,T=`w${d}`;if(Y(f.select,`with.${g}.select`,"which columns a call reads"),Y(f.orderBy,`with.${g}.orderBy`,"the order"),f.select!==void 0&&(!Array.isArray(f.select)||f.select.length===0))throw new Error(`${r}(${e.key}): with.${g}.select must be a non-empty array of columns`);let E=Array.isArray(f.select)?f.select:[...S.columns.keys()];for(let v of E)se(S,v,r,`with.${g}.select column`);let w=f.with;if(w!==void 0&&(w===null||typeof w!="object"||Array.isArray(w)))throw new Error(`${r}(${e.key}): with.${g}.with must be a relation object`);let O=w===void 0?{joins:[],jsonPairs:[]}:ct(t,S,w,s,r,T,a+1,i),k=O.joins.length?` ${O.joins.join(" ")}`:"",N=Wa(S,E,T,O.jsonPairs),A=f.where!==void 0&&f.where!==null?lt(t,S,f.where,s,r,T,d,`with.${g}.where`):"",R=`${T}.${_(y)} = ${_(o)}.${_(m)}`,L=A===""?R:`${R} AND ${A}`,$=f.limit;zt($,r);let C=h.kind==="many"?$:Math.min(1,$??1),q=Jr(S,f.orderBy,r,T)+zt(C,r),U=`wj${d}`;h.kind==="many"?l.push(`LEFT JOIN LATERAL (SELECT coalesce(json_agg(x.o), '[]'::json) AS d FROM (SELECT ${N} AS o FROM ${S.sql} ${T}${k} WHERE ${L}${q}) x) ${_(U)} ON true`):l.push(`LEFT JOIN LATERAL (SELECT ${N} AS d FROM ${S.sql} ${T}${k} WHERE ${L}${q}) ${_(U)} ON true`),c.push(`${_(U)}."d" AS ${_(g)}`),u.push(`${Vr(g)}, ${_(U)}."d"`)}return{select:c,joins:l,jsonPairs:u}}function ss(t,e,n,s){let r=[],o=ee(r),a=Re(t,e,n,o,"findMany").sql,i=Jr(e,s.orderBy,"findMany"),c=zt(s.limit),l=qa(s.offset,s.limit),u=es(e,s.select,"findMany"),g=s.with!==void 0&&Object.keys(s.with).length>0?ct(t,e,s.with,o,"findMany"):{select:[],joins:[]},p=g.select.length>0?`${u}, ${g.select.join(", ")}`:u,h=g.joins.length>0?` ${g.joins.join(" ")}`:"",m=s.lock===void 0?"":` ${ns("findUnique",e.key,"lock",s.lock)} OF t`;return{sql:`SELECT ${p} FROM ${e.sql} t${h} WHERE true${a}${i}${c}${l}${m}`,params:r}}var Jt=50;function rs(t,e,n,s){if(e.search===void 0||e.singleKey===null)throw new Error(`search(${e.key}): the table declares no search`);if(n.text===void 0&&n.vector===void 0)throw new Error(`search(${e.key}): neither the query's words nor its vector to search by`);let r=[],o=ee(r),a=_(e.singleKey),i=Re(t,e,s.where??{},o,"search").sql,c=Math.max(s.limit*3,30),l=[];if(n.vector!==void 0){let g=`v."embedding" ${n.vector.operator} ${o(n.vector.literal)}::${n.vector.type}`;l.push(`sem AS (SELECT s."id", row_number() OVER (ORDER BY s."d", s."id") AS r FROM (SELECT t.${a} AS "id", ${g} AS "d" FROM ${n.vector.table} v JOIN ${e.sql} t ON t.${a} = v."id" WHERE true${i} ORDER BY ${g} LIMIT ${c}) s)`)}if(n.text!==void 0){let g=Fn(e.search.from,"t"),p=`websearch_to_tsquery('simple', ${o(n.text)})`;l.push(`kw AS (SELECT s."id", row_number() OVER (ORDER BY s."k" DESC, s."id") AS r FROM (SELECT t.${a} AS "id", ts_rank_cd(${g}, ${p}) AS "k" FROM ${e.sql} t WHERE ${g} @@ ${p}${i} ORDER BY "k" DESC LIMIT ${c}) s)`)}l.push(l.length===2?`fused AS (SELECT coalesce(sem."id", kw."id") AS "id", (coalesce(1.0 / (${Jt} + sem.r), 0) + coalesce(1.0 / (${Jt} + kw.r), 0))::float8 AS "score" FROM sem FULL OUTER JOIN kw ON sem."id" = kw."id")`:`fused AS (SELECT x."id", (1.0 / (${Jt} + x.r))::float8 AS "score" FROM ${n.vector!==void 0?"sem":"kw"} x)`);let u=s.minScore===void 0?"":` AND f."score" >= ${o(s.minScore)}`;return{sql:`WITH ${l.join(", ")} SELECT ${Z(e,"t")}, f."score" AS "_score" FROM fused f JOIN ${e.sql} t ON t.${a} = f."id" WHERE true${u} ORDER BY f."score" DESC, t.${a} LIMIT ${ja(s.limit)}`,params:r}}function ja(t){if(!Number.isInteger(t)||t<1)throw new Error(`search: limit must be a positive integer (got ${String(t)})`);return t}function eo(t){return t instanceof Date?t.toISOString():Array.isArray(t)?t.map(eo):t}function os(t){if(t===null||typeof t!="object")return t;let e={};for(let[n,s]of Object.entries(t))e[n]=eo(s);return e}function Ka(t,e){for(let[n,s]of e.transforms){if(s.fromDb===void 0)continue;let r=t[n];r!=null&&(t[n]=s.fromDb(r))}return t}function yn(t,e,n,s){let r=n.map(o=>Ka(os(o),e));if(s)for(let o of e.relations){let a=s[o.name];if(!a)continue;let i=typeof a=="object"?a.with:void 0,c=t.table(o.to,"with");for(let l of r){let u=l[o.name];Array.isArray(u)?l[o.name]=yn(t,c,u,i):u&&typeof u=="object"&&(l[o.name]=yn(t,c,[u],i)[0])}}return Xe(r)}var Ha={inline:!0},as={inline:!0,read:!0},Va=t=>ae(t)===ne.uniqueViolation||Ke.is(t);function no(t,e){if(e===null)return!1;let n=Object.keys(t);if(n.length!==1||n[0]!==e)return!1;let s=t[e];return s!==null&&(typeof s!="object"||Array.isArray(s))}var Ga=6e4;function Sn(t,e){let{cat:n}=e,s=(p,h)=>n.table(p,h),r=(p,h)=>{let m=n.table(p,h);return m.search!==void 0&&e.searchable?.(),m},o=async(p,h=[],m=!1)=>await(await t()).unsafe(p,h,Qt(h)?void 0:m?as:Ha),a=(p,h=[])=>o(p,h,!0),i=async(p,h=[])=>await(await t()).unsafe(p,h),c=async(p,h)=>{let m=n.deferral(p);if(m===null)return h();await o(m.defer);let y=await h();return await o(m.check),y},l=(p,h,m)=>yn(n,p,h,m),u=(p,h)=>yn(n,p,[h])[0],g={diagnostics(){return e.trace?.snapshot??null},async query(p,h=[]){return Xe((await i(p,h)).map(m=>os(m)))},async insert(p,h){let m=r(p,"insert"),y=Object.keys(h);if(y.length===0)throw new Error(`insert into ${p}: no columns given`);nn(m,"insert",y);let S=[],f=$n(m,y,h,ee(S),"insert"),d=await o(`INSERT INTO ${m.sql} (${y.map(_).join(", ")}) VALUES (${f}) RETURNING ${Z(m)}`,S);if(!d[0])throw new Error(`insert into ${p} returned no row \u2014 the write was rejected (an RLS policy, most likely).`);return u(m,d[0])},insertMany:(async function(p,h,m){if(Is(m),h.length===0)return m?.returning===!1?0:[];let y=r(p,"insertMany"),S=Object.keys(h[0]);if(S.length===0)throw new Error(`insertMany into ${p}: no columns given`);for(let k=1;k<h.length;k++){let N=S.filter(R=>!(R in h[k])),A=Object.keys(h[k]).filter(R=>!S.includes(R));if(N.length>0||A.length>0)throw new Error(`insertMany(${p}): row ${k} does not have the first row's columns`+(N.length>0?` (missing: ${N.join(", ")})`:"")+(A.length>0?` (extra: ${A.join(", ")})`:"")+". One statement shares the column list: write null for a missing value, or insert differently shaped rows in calls of their own.")}nn(y,"insertMany",S),Y(m?.onConflict,"onConflict","which columns a conflict is on");for(let k of m?.onConflict??[])se(y,k,"insertMany","onConflict column");let f=[],d=ee(f),T=h.map(k=>`(${$n(y,S,k,d,"insertMany")})`),E=m?.onConflict??[],w=E.length>0||m?.action==="ignore"?` ${Tn(S,E,m?.action??"ignore")}`:"",O=await o(`INSERT INTO ${y.sql} (${S.map(_).join(", ")}) VALUES ${T.join(", ")}${w}${m?.returning===!1?"":` RETURNING ${Z(y)}`}`,f);if(m?.returning===!1){if(!Number.isSafeInteger(O.count)||O.count<0)throw new Error("insertMany returning:false requires a driver with an affected row count");return O.count}return l(y,O)}),async advisoryXactLock(p){await o("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))",[p])},async lockRows(p,h){let m=[...new Set(h)].sort();if(m.length===0)return;let y=s(p,"lockRows");if(y.singleKey===null)throw new Error(`lockRows(${p}): no single-column primary key \u2014 the lock order needs one`);let S=_(y.singleKey),f=[],d=ee(f),T=m.map(E=>d(we(y,y.singleKey,E))).join(", ");await o(`SELECT t.${S} FROM ${y.sql} t WHERE t.${S} IN (${T}) ORDER BY t.${S} FOR NO KEY UPDATE`,f)},async lockRowsWhere(p,h,m){let y=ns("lockRowsWhere",p,"mode",m?.mode===void 0?"update":m.mode),S=s(p,"lockRowsWhere");if(S.singleKey===null)throw new Error(`lockRowsWhere(${p}): no single-column primary key \u2014 the lock order needs one`);let f=[],d=Re(n,S,h,ee(f),"lockRowsWhere");tn(d,"lockRowsWhere");let T=_(S.singleKey);await o(`SELECT t.${T} FROM ${S.sql} t WHERE true${d.sql} ORDER BY t.${T} ${y}`,f)},async claim(p,h,m={}){let y=r(p,"claim");Y(h,"unique","which key a claim is on");let S=Object.keys(h);if(S.length===0)throw new Error(`claim(${p}): no unique field given. claim takes ownership of a key; with none there is nothing to own \u2014 use insert(${p}, \u2026).`);xn("claim",p,S,h);for(let w of S){let O=h[w];if(O===null||typeof O=="object"||typeof O=="function")throw new Error(`claim(${p}): the unique field "${w}" is ${O===null?"null":"an object"} \u2014 a key is one value (a string, number, bigint or boolean); NULL never equals a claimed row.`)}let f=Object.keys(m).filter(w=>Object.hasOwn(h,w));if(f.length>0)throw new Error(`claim(${p}): ${f.map(w=>`"${w}"`).join(", ")} is in both the unique fields and extra \u2014 say a key once, in the unique fields.`);let d={...h,...m},T=Object.keys(d);nn(y,"claim",T);let E=await t();try{return await E.savepoint(async w=>{let O=[],k=$n(y,T,d,ee(O),"claim"),N=await w.unsafe(`INSERT INTO ${y.sql} (${T.map(_).join(", ")}) VALUES (${k}) RETURNING ${Z(y)}`,O);if(!N[0])throw new Error(`claim(${p}): the INSERT returned no row \u2014 the write was rejected (an RLS policy, most likely).`);return{inserted:!0,row:u(y,N[0])}})}catch(w){if(!Va(w))throw w;let O=[],k=ee(O),N=S.map(R=>`t.${_(R)} = ${k(we(y,R,h[R]))}`).join(" AND "),A=await o(`SELECT ${Z(y,"t")} FROM ${y.sql} t WHERE ${N} LIMIT 1`,O);if(!A[0]){let R=on(w);throw new Error(`claim(${p}): a unique violation${R?` (${R})`:""} was caught but no row has these ${S.join(", ")}. Either the violation is of ANOTHER unique constraint \u2014 claim() is the wrong tool for this call \u2014 or the row exists and an RLS policy hides it from this caller.`)}return{inserted:!1,row:u(y,A[0])}}},async put(p,h,m){let y=r(p,"put"),S=m.onConflict;if(S.length===0)throw new Error(`put into ${p}: onConflict needs at least one column`);let f=Object.keys(h);if(f.length===0)throw new Error(`put into ${p}: no columns given`);nn(y,"put",f),Y(S,"onConflict","which columns a conflict is on");for(let w of S)se(y,w,"put","onConflict column");let d=[],T=$n(y,f,h,ee(d),"put"),E=await o(`INSERT INTO ${y.sql} (${f.map(_).join(", ")}) VALUES (${T}) ${Tn(f,S,"update")} RETURNING ${Z(y)}`,d);if(!E[0])throw new Error(`put into ${p} returned no row \u2014 the write was rejected (an RLS policy, most likely).`);return u(y,E[0])},async update(p,h,m){let y=r(p,"update");if(!y.columns.has("id"))throw new Error(`update(${p}): the table has no "id" column \u2014 update a row by its key with updateMany({ where, set })`);let S=Object.keys(m);if(S.length===0)return g.findById(p,h);for(let w of S)se(y,w,"update","set column");let f=[],d=ee(f),T=ts(y,S,m,d,"update"),E=await o(`UPDATE ${y.sql} SET ${T} WHERE "id" = ${d(we(y,"id",h))} RETURNING ${Z(y)}`,f);return E[0]?u(y,E[0]):null},async delete(p,h){let m=s(p,"delete");if(!m.columns.has("id"))throw new Error(`delete(${p}): the table has no "id" column \u2014 delete by its key with deleteMany({ where })`);let y=[we(m,"id",h)];await c(m,()=>o(`DELETE FROM ${m.sql} WHERE "id" = $1`,y))},async updateMany(p,h,m,y){let S=r(p,"updateMany"),f=Object.keys(m);if(f.length===0)throw new Error(`updateMany(${p}): nothing to set. An update with no columns is not a no-op worth pretending happened \u2014 pass the columns to write.`);for(let U of f)se(S,U,"updateMany","set column");let d=[],T=ee(d),E=ts(S,f,m,T,"updateMany"),w=Re(n,S,h,T,"updateMany");tn(w,"updateMany");let O=w.sql,k=S.singleKey,N=y?.returning!==!1,A=k===null||no(h,k),R=N?`RETURNING ${Z(S,"t")}`:"RETURNING 1",L=A?`UPDATE ${S.sql} AS t SET ${E} WHERE true${O} ${R}`:`UPDATE ${S.sql} AS t SET ${E} FROM locked WHERE t.${_(k)} = locked."__pb_lock" ${R}`,$=A?"":`WITH locked AS (SELECT t.${_(k)} AS "__pb_lock" FROM ${S.sql} t WHERE true${O} ORDER BY t.${_(k)} FOR NO KEY UPDATE)`,C=N?`${$} ${L}`.trim():A?`WITH upd AS (${L}) SELECT count(*)::int AS n FROM upd`:`${$}, upd AS (${L}) SELECT count(*)::int AS n FROM upd`,q=await o(C,d);return N?l(S,q):Number(q[0]?.n??0)},async deleteMany(p,h){let m=s(p,"deleteMany"),y=[],S=Re(n,m,h,ee(y),"deleteMany");return tn(S,"deleteMany"),(await c(m,()=>o(`DELETE FROM ${m.sql} AS t WHERE true${S.sql} RETURNING 1`,y))).length},async aggregate(p,h={}){let m=s(p,"aggregate");for(let R of["sum","avg","min","max","groupBy"])Y(h[R],R,"which columns an aggregate reads");Y(h.count,"count","which counts an aggregate takes");let y=[],S=ee(y),f=[],d=[];for(let R of["sum","avg","min","max"])for(let L of h[R]??[]){if(se(m,L,"aggregate",`${R} column`),(R==="sum"||R==="avg")&&!Qr(Se(m,L)))throw new Error(`aggregate(${p}): ${R}("${L}") on a column that is not numeric \u2014 "${L}" is ${Se(m,L)}. Only integer, bigint and numeric sum; to count rows, count: true.`);let $=`${R}__${L}`;d.push({fn:R,col:L,alias:$}),f.push(Zr(m,R,L,$))}h.count===!0&&f.push('count(*) AS "_count"');let T=h.count!==null&&typeof h.count=="object"?Object.entries(h.count):[];if(h.count!==void 0&&typeof h.count!="boolean"&&(h.count===null||typeof h.count!="object"||Array.isArray(h.count)||T.length===0))throw new Error(`aggregate(${p}): count is true, or named filters \u2014 { <name>: <filter> }, at least one`);if(T.forEach(([R,L],$)=>{let C=lt(n,m,L,S,"aggregate","t",0,`count.${R}`);f.push(`count(*) FILTER (WHERE ${C===""?"true":C}) AS ${_(`count__${$}`)}`)}),f.length===0)throw new Error(`aggregate(${p}): no aggregate asked for \u2014 the result would be an empty object. Name sum/avg/min/max columns or count: true.`);let E=h.groupBy??[];for(let R of E)se(m,R,"aggregate","groupBy column");let w=E.map(R=>`${Zt(m,R,"t")} AS ${_(`g__${R}`)}`),O=Re(n,m,h.where??{},S,"aggregate").sql,k=E.length>0?` GROUP BY ${E.map(R=>`t.${_(R)}`).join(", ")}`:"",N=await a(`SELECT ${[...w,...f].join(", ")} FROM ${m.sql} t WHERE true${O}${k}`,y),A=R=>{let L={};for(let $ of E)L[$]=R[`g__${$}`];for(let $ of d){let C=L[$.fn]??={};C[$.col]=R[$.alias]??null}return h.count===!0&&(L.count=Number(R._count??0)),T.length>0&&(L.count=Object.fromEntries(T.map(([$],C)=>[$,Number(R[`count__${C}`]??0)]))),L};return E.length>0?N.map(A):A(N[0]??{})},async count(p,h={}){let m=s(p,"count"),y=[],S=Re(n,m,h,ee(y),"count").sql,f=await a(`SELECT count(*) AS n FROM ${m.sql} t WHERE true${S}`,y);return Number(f[0]?.n??0)},async findUnique(p,h,m={}){let y=s(p,"findUnique");Lr(p,y.uniqueKeys,h);let S={select:m.select===void 0?void 0:an(m.select,[...m.select]),with:m.with,limit:2},f;if(m.lock===void 0)f=await g.findMany(p,h,S);else{let d=ss(n,y,h,{...S,lock:m.lock});f=l(y,await o(d.sql,d.params),m.with)}if(f.length>1)throw new Error(`findUnique(${p}): the database's constraints disagree with the schema; more than one row matched`);return f[0]??null},async findById(p,h){let m=s(p,"findById");if(!m.columns.has("id"))throw new Error(`findById(${p}): the table has no "id" column \u2014 use findUnique({ where: <its key> })`);let y=await a(`SELECT ${Z(m)} FROM ${m.sql} WHERE "id" = $1`,[we(m,"id",h)]);return y[0]?u(m,y[0]):null},async findMany(p,h={},m={}){let y=s(p,"findMany"),S=ss(n,y,h,m),f=m.lock!==void 0;return l(y,await(f?o:a)(S.sql,S.params),m.with)},async search(p,h,m={}){let y=s(p,"search"),S=y.search;if(S===void 0)throw new Error(`search(${p}): the table declares no search \u2014 defineTable(${JSON.stringify(y.name)}, { \u2026, search: { from: [<its text columns>], model } })`);let f=$r(p,h,m),d=e.search,T=A=>l(y,A),E=async()=>{let A=rs(n,y,{text:f.text},{where:m.where,limit:f.limit,minScore:f.minScore});return T(await a(A.sql,A.params))};if(f.mode==="text")return E();let w=d.embed;if(w===void 0)throw new Error(`search(${p}): no embedder was given, and a search by meaning embeds its query \u2014 createDatabase({ embed }) (the backend's db.embed); mode: "text" searches by word alone`);let O=A=>(A===void 0?d.serving.delete(S.key):d.serving.set(S.key,A),A),k=d.serving.get(S.key)??O(Ht((await a(Kt,[S.key]))[0])),N;for(let A=1;;A++){if(k===void 0){if(f.mode==="vector")throw new Error(`search(${p}): the database holds no vectors for this table \u2014 its schema is migrated before the app serves`);return E()}if(N===void 0||N.of.model!==k.model||N.of.dimensions!==k.dimensions)try{N={of:k,literal:await Sr(w,f.text,k)}}catch(I){if(f.mode==="vector")throw I;let x=Date.now();return x-(d.said.get(S.key)??-1/0)>=Ga&&(d.said.set(S.key,x),d.log?.(`palgres: search(${p}) answered by word alone: its query could not be embedded (${I instanceof Error?I.message:String(I)})`)),E()}let R=cn(k.dimensions,k.metric),L=_(k.types),$=rs(n,y,{...f.mode==="hybrid"?{text:f.text}:{},vector:{table:`${_(J)}.${_(ve(S.key,k.gen))}`,literal:N.literal,type:`${L}.${R.type}`,operator:`OPERATOR(${L}.${R.operator})`}},{where:m.where,limit:f.limit,minScore:f.minScore}),C=await t(),[q,U]=await Promise.all([C.unsafe(Kt,[S.key],as),C.unsafe($.sql,$.params,Qt($.params)?void 0:as)]),v=O(Ht(q[0]));if(v!==void 0&&v.gen===k.gen)return T(U);if(A>=3)throw new Error(`search(${p}): the vectors that serve the table changed while it was searched, three times over`);k=v}},async page(p,h={},m={}){let y=s(p,"page");if(e.cursorKey===null)throw new Error("Database.page needs a cursor secret shared by every replica: createDatabase({ cursorSecret })");let S=e.cursorKey,f=Object.create(null);for(let[H,re]of y.columns)f[H]={type:re.type,nullable:re.nullable&&!y.primaryKey.includes(H)};let{order:d,size:T,backward:E,token:w}=Ur(f,y.primaryKey,m),O=[],k=ee(O),N=Re(n,y,h,k,"page").sql,A=es(y,m.select===void 0?void 0:an(m.select,[...m.select]),"page"),R=Xt({table:p,version:n.version,columns:f,order:d,where:N,params:O,select:m.select,with:m.with,identity:e.cursorScope()}),L=w===void 0?void 0:Fr(S,R,w,d.length),$=H=>`t.${_(H)}`,C=L?` AND ${Yt(d,L,E,k,$)}`:"",q=L?`EXISTS (SELECT 1 FROM ${y.sql} t WHERE true${N} AND ${Yt(d,L,!E,k,$,!0)})`:"false",U="__pb_page_",v=[...y.columns.keys(),...Object.keys(m.with??{})];for(;v.some(H=>H.startsWith(U));)U+="_";let I=`${U}ordinal`,x=`${U}opposite`,B=d.map((H,re)=>`${U}${re}`),F=d.map((H,re)=>`${Se(y,H.column)==="timestamp"?Ue("timestamp",$(H.column)):`${$(H.column)}::text`} AS ${_(B[re])}`),W=m.with&&Object.keys(m.with).length?ct(n,y,m.with,k,"page"):{select:[],joins:[]},V=d.map(H=>`${$(H.column)} ${H.direction==="asc"!==E?"ASC":"DESC"} NULLS ${H.nulls==="first"!==E?"FIRST":"LAST"}`).join(", "),X=`SELECT ${[A,...W.select,...F,`row_number() OVER (ORDER BY ${V}) AS ${_(I)}`].join(", ")} FROM ${y.sql} t ${W.joins.join(" ")} WHERE true${N}${C} ORDER BY ${V} LIMIT ${T+1}`,K=`SELECT p.*, b.${_(x)} FROM (SELECT ${q} AS ${_(x)}) b LEFT JOIN LATERAL (${X}) p ON true ORDER BY p.${_(I)}`,G=await a(K,O),te=G[0]?.[x]===!0,Q=G.filter(H=>H[I]!==null&&H[I]!==void 0),An=Q.length>T;Q=Q.slice(0,T),E&&Q.reverse();let ce=H=>H?qr(S,R,B.map(re=>H[re]===null?null:String(H[re]))):null,We={startCursor:ce(Q[0]),endCursor:ce(Q.at(-1)),hasNextPage:E?te:An,hasPreviousPage:E?An:te};for(let H of Q)for(let re of[...B,I,x])delete H[re];return{rows:l(y,Q,m.with),pageInfo:We}},async attempt(p){return(await t()).savepoint(m=>p(Sn(()=>Promise.resolve(m),e)))},async txPlan(p){return(await t()).savepoint(async m=>{let y=[];for(let S of p.ops){let f=S.op==="select"||S.op==="delete"?s(S.table,"transaction plan"):r(S.table,"transaction plan"),d=Ya(n,f,S,y),T=S.op==="delete"?n.deferral(f):null;d!==null&&T!==null&&await m.unsafe(T.defer,[]);let E=d===null?[]:l(f,await m.unsafe(d.sql,d.params));d!==null&&T!==null&&await m.unsafe(T.check,[]);let w={rows:E,rows_affected:E.length};y.push(w),Ja(S,w)}return{results:y}})}};return g}function Xa(t,e){let n;return t.op<0||t.op>=e.length?n=`transaction plan: a reference points at operation ${t.op}, but the plan has run ${e.length} (0\u2026${e.length-1}). A reference can only read an EARLIER operation's row.`:e[t.op].rows[0]?n=`transaction plan: operation ${t.op}'s row has no column "${t.field}" (its columns: ${Object.keys(e[t.op].rows[0]).join(", ")}).`:n=`transaction plan: operation ${t.op} returned NO ROW, so "${t.field}" cannot be read. A reference does not guarantee its row exists \u2014 declare the expectation first (\`.expectOne(err)\`).`,Object.assign(new Error(n),{error_code:"tx_ref_unresolved"})}function to(t,e){if(Ss(t))throw new Error(`${e}: got an imitation of a plan reference ({ $ref: \u2026 }) \u2014 this object was not made by the plan's builder. A reference is built in your code from an earlier operation's row (insert(...).expectOne(err).id); a filter from a request body cannot carry one.`)}function so(t,e){let n=e[t.$ref.op]?.rows[0];if(!n||!(t.$ref.field in n))throw Xa(t.$ref,e);return n[t.$ref.field]}function ut(t,e,n,s,r){if(to(e,`transaction plan (${n})`),yt(e))return s(we(t,n,so(e,r)));if($s(e)){let o=e.$expr;return o.fn==="now"?en(o,s):`${_(n)} ${o.fn==="inc"?"+":"-"} ${s(we(t,n,o.by))}`}return s(we(t,n,e))}function ls(t,e){if(to(t,"transaction plan filter"),yt(t)){let n=so(t,e);if(n==null)throw Object.assign(new Error(`transaction plan: a filter's reference to operation ${t.$ref.op}'s "${t.$ref.field}" is NULL. As a filter value NULL means IS NULL \u2014 every row with no value there, not the one the reference named. Write null in the filter when that is what you mean.`),{error_code:"tx_ref_null"});if(typeof n=="object"&&!Array.isArray(n)&&!Ts(n))throw Object.assign(new Error(`transaction plan: a filter's reference to operation ${t.$ref.op}'s "${t.$ref.field}" is an object. A filter compares a column with a value; an object there would be read as operators. Reference a scalar column.`),{error_code:"tx_ref_object"});return n}if(Array.isArray(t))return an(t,t.map(n=>ls(n,e)));if(t!==null&&typeof t=="object"&&!xe(t)&&!ln(t)&&!In(t)&&!(t instanceof Date)){let n=an(t,{});for(let[s,r]of Object.entries(t))n[s]=ls(r,e);return n}return t}function is(t,e,n,s,r,o){if(n===void 0||Object.keys(n).length===0&&!ln(n))return{sql:"",conditions:0,resolved:{}};let a=ls(n,r);return{...Re(t,e,a,s,o),resolved:a}}function Ya(t,e,n,s){let r=[],o=ee(r),a=`transaction plan ${n.op}`,i=n.op==="insertMany"?[...new Set((n.rows??[]).flatMap(l=>Object.keys(l)))]:Object.keys(n.values??n.set??{});nn(e,a,i);let c;switch(n.op){case"insert":{let l=n.values??{},u=Object.keys(l),g=u.map(p=>ut(e,l[p],p,o,s));c=u.length?`INSERT INTO ${e.sql} (${u.map(_).join(", ")}) VALUES (${g.join(", ")}) RETURNING ${Z(e)}`:`INSERT INTO ${e.sql} DEFAULT VALUES RETURNING ${Z(e)}`;break}case"upsert":{let l=n.values??{},u=Object.keys(l),g=n.onConflict??[];if(u.length===0||g.length===0)throw Object.assign(new Error(`put on ${n.table} needs columns and onConflict`),{error_code:"tx_bad_upsert"});Y(n.onConflict,"onConflict","which columns a conflict is on");for(let h of g)se(e,h,a,"onConflict column");let p=u.map(h=>ut(e,l[h],h,o,s));c=`INSERT INTO ${e.sql} (${u.map(_).join(", ")}) VALUES (${p.join(", ")}) ${Tn(u,g,"update")} RETURNING ${Z(e)}`;break}case"insertMany":{let l=n.rows??[];if(l.length===0)return null;let u=Object.keys(l[0]),g=l.map(m=>`(${u.map(y=>ut(e,m[y],y,o,s)).join(", ")})`),p=n.onConflict??[];Y(n.onConflict,"onConflict","which columns a conflict is on");for(let m of p)se(e,m,a,"onConflict column");if(n.action==="update"&&p.length===0)throw Object.assign(new Error(`insertMany on ${n.table}: an overwrite needs onConflict columns`),{error_code:"tx_bad_upsert"});let h=n.action!==void 0?` ${Tn(u,p,n.action)}`:"";c=`INSERT INTO ${e.sql} (${u.map(_).join(", ")}) VALUES ${g.join(", ")}${h} RETURNING ${Z(e)}`;break}case"update":{let l=n.set??{},u=Object.keys(l);if(u.length===0)throw new Error(`update ${n.table}: nothing to set`);let g=u.map(m=>`${_(m)} = ${ut(e,l[m],m,o,s)}`),p=is(t,e,n.where,o,s,a);tn(p,a);let h=e.singleKey;c=h===null||no(p.resolved,h)?`UPDATE ${e.sql} AS t SET ${g.join(", ")} WHERE true${p.sql} RETURNING ${Z(e,"t")}`:`WITH locked AS (SELECT t.${_(h)} AS "__pb_lock" FROM ${e.sql} t WHERE true${p.sql} ORDER BY t.${_(h)} FOR NO KEY UPDATE) UPDATE ${e.sql} AS t SET ${g.join(", ")} FROM locked WHERE t.${_(h)} = locked."__pb_lock" RETURNING ${Z(e,"t")}`;break}case"delete":{let l=is(t,e,n.where,o,s,a);tn(l,a),c=`DELETE FROM ${e.sql} AS t WHERE true${l.sql} RETURNING ${Z(e,"t")}`;break}case"select":{let l="";if(n.limit!==void 0){let g=Number(n.limit);if(!Number.isInteger(g)||g<0)throw new Error(`transaction plan (${n.table}): limit must be an integer, got "${String(n.limit)}"`);l=` LIMIT ${g}`}let u=n.lock==="update"?" FOR UPDATE":"";c=`SELECT ${Z(e,"t")} FROM ${e.sql} t WHERE true${is(t,e,n.where,o,s,a).sql}${l}${u}`;break}default:throw new Error(`unknown operation "${String(n.op)}" in a transaction plan`)}return{sql:c,params:r}}function Ja(t,e){let n=t.guard;if(!n)return;let s=e.rows.length;if(!(n.kind==="one"?s===1:n.kind==="none"?s===0:n.kind==="atLeast"?s>=n.n:s<=n.n))throw Object.assign(new Error(`transaction expectation failed: ${n.kind} (${s} row(s))`),{error_code:"tx_guard_failed",slot:n.slot})}import{AsyncLocalStorage as ao}from"node:async_hooks";import{randomUUID as za}from"node:crypto";var dt=new ao,ro=()=>({running:new Set}),Qa=t=>String(t?.message??t);function Za(t){return new Ve(`The transaction was rolled back, not committed: a statement in it failed (${Qa(t)}) and the code went on. One failed statement aborts the whole transaction \u2014 Postgres refuses every later one and answers COMMIT with ROLLBACK. Wrap a write you expect to fail in \`Database.$attempt(async (tx) => \u2026)\` \u2014 it takes a SAVEPOINT, so only that write rolls back \u2014 or use \`Database.public.<t>.put({ data, onConflict: [...] })\` when what you brace for is a duplicate row.`,t)}async function oo(t){for(;t.running.size;)await Promise.allSettled([...t.running])}function io(t,e){let n=e.bind;if(e.lockTimeout!==void 0&&(n+=`, set_config('lock_timeout', ${de(e.lockTimeout)}, true)`),e.statementTimeout!==void 0&&(n+=`, set_config('statement_timeout', ${de(e.statementTimeout)}, true)`),!/^select /i.test(n))throw new Error("palgres: the binding statement must be a SELECT list");n+=", pg_catalog.current_setting('transaction_isolation') AS palgres_isolation";let s=null,r=null,o=null,a=null,i=!1,c=null,l=ro(),u=!0,g,p=!1,h,m=async()=>!e.control||await e.control.drainCancellation()?!0:(h?.abandon?.(),!1),y=()=>{p=!0},S=()=>{if(i)throw new Error("Database transaction has settled; its handle cannot be used again");if(e.control?.check(),e.trace?.check(),dt.getStore()!==e.scope)throw new Error("Database.$atomic cannot use a handle from another transaction or identity; use its tx argument")},f,d,T=new Set,E=0,w=!1,O=()=>s!==null&&!i&&p&&l.running.size===0&&T.size===0,k=(R,L,$=S,C,q=new Set)=>{let U=()=>{if($(),d!==C||f?.getStore()!==C)throw new Error("Database connection belongs to another active savepoint; use its tx argument and await it before using the parent handle")};return{unsafe(v,I,x){try{U()}catch(W){return Promise.reject(W)}x?.read!==!0&&(u=!1);let B=(I??[]).map(ze),F=()=>{let W=R.unsafe(v,B,x),V=Promise.resolve(e.control?e.control.watch(W):W).catch(X=>{throw On(X)&&(L.refused??={error:X}),X});return L.running.add(V),E++,V.catch(()=>{}).finally(()=>{L.running.delete(V),y(),--E===0&&!i&&e.onIdle?.()}),V};return e.trace?e.trace.query(v,F):F()},async savepoint(v){U(),u=!1;let I=Symbol("savepoint"),x=new Set,B=ro();f??=new ao,d=I;let F=(async()=>{try{return await R.savepoint(async W=>{let V=!1,X=()=>{if($(),V)throw new Error("Database savepoint has settled; its handle cannot be used again")};try{let K=f.run(I,()=>v(k(W,B,X,I,x))),G=await(e.control?e.control.race(K):K);if(await oo(B),B.refused)throw B.refused.error;return G}finally{x.size&&await Promise.allSettled([...x]),V=!0}})}finally{d=C}})();T.add(F),q.add(F);try{return await F}finally{T.delete(F),q.delete(F)}}}},N=async()=>(S(),s||(s=new Promise((R,L)=>{let $=new Promise((C,q)=>{r=C,o=q});$.catch(()=>{}),a=(async()=>{let C=performance.now(),q;try{q=await(e.acquire?e.acquire(e.control?.signal):e.admission.acquire(e.claims,e.control?.signal))}finally{e.trace?.time("queueMs",C)}let U=performance.now(),v,I=e.control!==void 0&&t.cancelBackend!==void 0,x=[];if(e.isolation!==void 0||e.readOnly!==void 0){let K=e.isolation??"read committed";_n({isolation:K,readOnly:e.readOnly}),x.push(`SET TRANSACTION ISOLATION LEVEL ${K.toUpperCase()}${e.readOnly===void 0?"":e.readOnly?" READ ONLY":" READ WRITE"}`)}x.push(I?`WITH backend AS MATERIALIZED (SELECT pid, backend_start::text AS started, xact_start::text AS xact_started FROM pg_catalog.pg_stat_get_activity(pg_catalog.pg_backend_pid())) ${n}, backend.pid, backend.started, backend.xact_started FROM backend`:n);let B=I&&t.cancelOpening!==void 0?`palgres:${za()}`:void 0,F=!1,W=K=>{p=!0;let G=K[0];if(typeof G?.palgres_isolation=="string"&&(g=G.palgres_isolation),!I)return;if(!Number.isInteger(G?.pid)||typeof G?.started!="string"||typeof G?.xact_started!="string"){let Q=new Error("Database cancellation cannot identify its own backend");throw l.refused??={error:Q},Q}let te={pid:G.pid,started:G.started,xactStarted:G.xact_started};F=!0,e.control.bindBackend(()=>t.cancelBackend(te.pid,te.started,te.xactStarted))},V=async()=>{for(let K=0;K<400&&!p&&!F;K++){if(await t.cancelOpening(B))return;await new Promise(G=>setTimeout(G,5))}},X=K=>t.begin(async G=>{v=performance.now(),e.trace?.time("poolMs",U);let te=e.trace?performance.now():0;if(e.control?.check(),i)throw new Error("Database transaction closed while waiting for its connection");B!==void 0&&!F&&e.control.bindBackend(V),e.trace?.time("setupMs",te),K(),h=G,R(k(G,l)),await $},{sql:x.join("; "),opened:W,tag:B});try{if(e.control?.check(),i)throw new Error("Database transaction closed while waiting for admission");let K=!1;for(let G=1;;G++)try{return await X(()=>{K=!0})}catch(te){let Q=ue(te);if(K||i||Q===void 0||G>=(Q==="lost"?et:nt))throw te;e.control?.check()}}finally{v!==void 0&&e.trace?.time("connectionMs",v),q.release()}})().catch(C=>{throw L(C),C}),a.catch(()=>{})}),s.then(void 0,()=>{w=!0,i||e.onIdle?.()}),s)),A=R=>c??=(async()=>{if(i=!0,!s)return;await m(),T.size&&await Promise.allSettled([...T]);let L=e.trace?performance.now():0;o(R),await a?.catch(()=>{}),e.trace?.time("rollbackMs",L),e.trace?.rolledBack()})();return{ensure:N,get opened(){return s!==null},get busy(){return s!==null&&!i&&!w&&(!p||E>0)},get failed(){return w},get releasable(){return O()&&u&&l.refused===void 0&&(g==="read committed"||g==="read uncommitted")},async commit(){if(T.size&&await Promise.allSettled([...T]),!s){e.trace?.check(),i=!0;return}await oo(l);try{if(e.trace?.check(),e.control?.checkDeadline(),l.refused)throw Za(l.refused.error)}catch(L){throw await A(L),L}if(i=!0,!await m()){let L=Ne(new oe(503,"database_unavailable","The database is temporarily unavailable",{retrySafe:!1},1));throw await A(L),L}let R=e.trace?performance.now():0;if(r(),u&&g!==void 0&&g!=="serializable"){a.then(()=>e.trace?.committed(),()=>e.trace?.commitFailed());return}try{await a,e.trace?.committed()}catch(L){throw e.trace?.commitFailed(),L}finally{e.trace?.time("commitMs",R)}},rollback:A}}var ei="5s",ni="10s",ti=new Set(["22P02","22007","22008","22003","22001"]),lo="55s",po=1,si=1e3,bn=new Set,Rn,ri=new mo("palgres.release");function oi(t){bn.add(t),Rn===void 0&&(Rn=ri.runInAsyncScope(()=>setInterval(()=>{let e=performance.now();for(let n of bn){let s=!0;try{s=n(e)}catch{s=!0}s&&bn.delete(n)}bn.size===0&&(clearInterval(Rn),Rn=void 0)},po)),Rn.unref?.())}function ai(t,e){let n=[],s=u=>u!==null&&u.opened&&!u.failed,r=new Set,o,a=!1,i=()=>{for(let u of n)if(s(u.tx)&&u.tx.busy)return!0;for(let u of r)if(u.busy)return!0;return!1},c=mo.bind(u=>{if(o===void 0||i())return!0;let g=r.size,p=r.size===0;for(let m of n)s(m.tx)&&(g++,m.tx.releasable||(p=!1));if(g===0)return!0;let h=u-o;if(p){if(h<po)return!1;for(let m of n){let y=m.tx;s(y)&&(m.tx=null,m.lease?.leave(),m.lease=void 0,y.commit().catch(()=>{}))}return o=void 0,!0}return e===void 0||a?!0:h<si?!1:(a=!0,e(Math.round(h)),!0)}),l=()=>{i()||(o=performance.now(),oi(c))};return{onIdle:l,surface(u,g){let p={tx:null};n.push(p);let h=()=>{bn.delete(c);let m=p.tx;return p.tx=null,p.lease=void 0,m};return{ensure(){if(o!==void 0&&(o=performance.now()),p.tx===null){if(t())return Promise.reject(new Error("Database transaction has settled; its handle cannot be used again"));p.tx=u(l,m=>g(m).then(y=>(p.lease=y,y)))}return p.tx.ensure()},commit:()=>h()?.commit()??Promise.resolve(),rollback:m=>h()?.rollback(m)??Promise.resolve()}},atomic(u){return r.add(u),()=>{r.delete(u)&&l()}}}}function ii(t){let e=ae(t),n=t?.code,s=o=>e!==void 0&&o.has(e)||typeof n=="string"&&o.has(n),r;if(s(gs))r=!0;else if(s(hs))r=!1;else return;return _e(new oe(503,"database_unavailable","The database is temporarily unavailable",{retrySafe:r},1),t)}var sn=t=>String(t?.message??t);function _e(t,e){return t.cause=e,Ne(t)}function cs(t,e,n){if(typeof t=="object"&&t!==null&&co.has(t))return t;let s=li(t,e,n);return typeof s=="object"&&s!==null&&co.add(s),s}var co=new WeakSet,mt=(t,e,n)=>Object.assign(new Error(t,{cause:e}),{sqlstate:n});function li(t,e,n){if(Ve.is(t))return t;let s=ae(t);if(s===ne.inFailedTransaction||/current transaction is aborted/i.test(sn(t)))return mt(`This request cannot write any more: an earlier write in it failed, and a request runs in ONE Postgres transaction, so every statement after the failure is refused. Wrap a write you expect to fail in \`Database.$attempt(async (tx) => \u2026)\` \u2014 it takes a SAVEPOINT, so only that write rolls back \u2014 or use \`Database.public.<t>.put({ data, onConflict: [...] })\` when what you brace for is a duplicate row. (${sn(t)})`,t,ne.inFailedTransaction);if(Ke.is(t))return t;if(s===ne.uniqueViolation)return _e(new Ke(on(t)),t);if(s===ne.serializationFailure)return _e(new ht(s),t);if(s===ne.deadlockDetected)return _e(new Et(s),t);if(s===ne.queryCancelled)return _e(Object.assign(new oe(503,"database_query_cancelled","Database statement was cancelled"),{sqlstate:s}),t);let r=ii(t);if(r)return r;if(e==="service"&&s===ne.foreignKeyViolation)return mt(`Database.$asService() hit a foreign key with nothing to point at. It runs in its OWN transaction on its OWN connection, so a row this request wrote through \`Database.*\` is not visible to it until the request commits \u2014 and the request cannot commit until the handler returns. If the row it needs is one this request just created, do both writes on ONE surface. (${sn(t)})`,t,s);if(/lock timeout/i.test(sn(t))){let o;return e==="service"?o=mt(`Database.$asService() waited too long for a row lock. It runs in its OWN transaction, so a row this request already wrote through Database.* is locked against it until the request commits \u2014 a wait that cannot end. Do that row's work on one surface or the other. (${sn(t)})`,t,s??ne.lockNotAvailable):o=mt(`Database waited too long for a lock. Another transaction holds it: a concurrent one, or another of this request's own \u2014 Database.*, $asService() and $atomic each run in their OWN transaction, and a row one of them wrote stays locked against the others until it commits, a wait that cannot end while the handler runs. Do that row's work on one surface. (${sn(t)})`,t,s??ne.lockNotAvailable),_e(Object.assign(new oe(503,"database_busy","The database waited too long for a lock",{retrySafe:!1},1),{sqlstate:s??ne.lockNotAvailable}),o)}return s!==void 0&&ti.has(s)?_e(Object.assign(new oe(400,"invalid_value","A value is not one its column can take"),{sqlstate:s}),t):s===ne.insufficientPrivilege&&n!==null?_e(Object.assign(new oe(n==="anon"?401:403,"permission_denied","The database does not allow this caller to do this"),{sqlstate:s}),t):(s!==void 0&&typeof t=="object"&&t!==null&&!Object.hasOwn(t,"sqlstate")&&Object.defineProperty(t,"sqlstate",{value:s,enumerable:!0,configurable:!0}),t)}function ci(t,e,n){let s=r=>({unsafe(o,a,i){let c;try{c=r.unsafe(o,a,i)}catch(l){return Promise.reject(cs(l,e,n))}return Promise.resolve(c).then(Xe,l=>{throw cs(l,e,n)})},savepoint:o=>r.savepoint(a=>o(s(a))),...r.abandon?{abandon:r.abandon.bind(r)}:{}});return{unsafe:(r,o)=>t.unsafe(r,o),begin:(r,o)=>t.begin(a=>r(s(a)),o).catch(a=>{throw cs(a,e,n)}),...t.cancelBackend?{cancelBackend:t.cancelBackend.bind(t)}:{},...t.cancelOpening?{cancelOpening:t.cancelOpening.bind(t)}:{}}}var ui=2,uo={anon:"anon",authenticated:"authenticated",service:"service"};function di(t,e){let n=Object.hasOwn(uo,e.role)?uo[e.role]:void 0;if(n===void 0)throw new Error(`palgres: a request is anon, authenticated or service, not "${String(e.role)}"`);return t[n]}function fo(t,e,n,s={}){let{signal:r}=s,o=s.diagnostics===void 0?void 0:new at(s.diagnostics),a=r===void 0?void 0:new Qe(void 0,r),i=di(e.roles,n),c=e.roles.service,l=n.role==="anon"||n.role==="authenticated"?n.role:null,u=n.claims,g=!1,p=!1,h=()=>{p&&jn.committed()},m=I=>{let x;return{cat:e.cat,trace:o,cursorKey:e.cursorKey,cursorScope:()=>x??=Mr(I,u),search:e.search,searchable:()=>{p=!0}}},y=m(i),S=m(c),f=new Set,d=null,T=!1,E=0,w=new Set,O=I=>new Promise((x,B)=>{let F=K=>{w.delete(W),clearTimeout(X),I?.removeEventListener("abort",V),K===void 0?x():B(K)},W=()=>F(),V=()=>F(I?.reason??new Error("Database admission cancelled")),X=setTimeout(()=>F(new Me({retryAfter:1},"Database queue deadline exceeded")),e.admission.queueTimeoutMs);w.add(W),I?.addEventListener("abort",V,{once:!0}),I?.aborted&&V()}),k=async I=>{let x;for(;;){for(;f.size===0&&d!==null;)await d;let W=f.size>0;if(W){for(;E>=ui;)await O(I);E++}let V;try{let X=e.admission.acquire(u,I,{nested:W,returning:!W&&T}).then(K=>{if(W&&f.size===0)return K.release(),null;let G={nested:W,turn:K};return f.add(G),W||(T=!0),G});if(!W){let K=X.then(()=>{},()=>{});d=K,K.then(()=>{d===K&&(d=null)})}V=await X}catch(X){throw W&&N(),X}if(V!==null){x=V;break}N()}let B=!1,F=()=>{if(!B){if(B=!0,f.delete(x),x.nested){N();return}for(let W of f){[x.turn,W.turn]=[W.turn,x.turn],W.nested=!1,x.nested=!0,N();return}}};return{queueMs:x.turn.queueMs,leave:F,release:()=>{F(),x.turn.release()}}},N=()=>{E--;let I=w.values().next().value;I?.()},A=ai(()=>g,s.heldIdle),R=(I,x,B={})=>io(ci(t,x,x==="user"?l:null),{bind:hn(e.binding,I,u),claims:u,admission:e.admission,acquire:k,trace:o,...B}),L=(I,x)=>async(B,F={})=>{if(_n(F),g)throw new Error("Database request has settled; it cannot open another transaction");if(dt.getStore()!==void 0)throw new Error("Nested Database.$atomic is not supported; use tx.$attempt for a savepoint");let W=Cn(F.retry),V=F.signal&&r?AbortSignal.any([F.signal,r]):F.signal??r,X=F.timeoutMs!==void 0||V!==void 0?new Qe(F.timeoutMs,V):void 0;try{X?.check();for(let K=0;;K++){X?.check();let G=[F.statementTimeoutMs,X?.remainingMs()].filter(ce=>ce!==void 0),te=Symbol("atomic transaction"),Q=R(I,x,{scope:te,isolation:F.isolation,readOnly:F.readOnly,control:X,lockTimeout:`${F.lockTimeoutMs??5e3}ms`,statementTimeout:G.length===0?void 0:`${Math.min(...G)}ms`,onIdle:A.onIdle}),An=A.atomic(Q);try{let ce=Sn(()=>Q.ensure(),x==="user"?y:S),We=dt.run(te,()=>B(ce)),H=await(X?X.race(We):We);return await Q.commit(),h(),H}catch(ce){await Q.rollback(ce);let We=He(ce)||Ve.is(ce)&&He(ce.cause);if(K>=W||!We)throw ce;o?.retry();let H=new Promise(re=>setTimeout(re,Math.min(100,2**K)));await(X?X.race(H):H)}finally{An()}}}finally{X?.dispose()}},$=A.surface((I,x)=>R(i,"user",{lockTimeout:ni,statementTimeout:lo,control:a,onIdle:I,acquire:x}),k),C=null,q,U=null,v=()=>{if(U===null){if(g)throw new Error("Database request has settled; it cannot open another transaction");q=r===void 0?void 0:new Qe(void 0,r);let I=q,x=A.surface((B,F)=>R(c,"service",{lockTimeout:ei,statementTimeout:lo,control:I,onIdle:B,acquire:F}),k);C=x,U=Object.assign(Sn(()=>x.ensure(),S),{atomic:L(c,"service")})}return U};return{client:Object.assign(Sn(()=>$.ensure(),y),{asService:v,atomic:L(i,"user")}),async commit(){g=!0;try{try{o?.check()}catch(I){throw await $.rollback(I),await C?.rollback(I),I}try{await $.commit()}catch(I){throw await C?.rollback(I),I}await C?.commit(),h(),o?.complete("committed"),a?.dispose(),q?.dispose()}catch(I){throw o?.failed(),I}},async rollback(I){g=!0,await $.rollback(I),await C?.rollback(I),a?.dispose(),q?.dispose(),o?.complete("failed")}}}function mi(t){if(t.codec!==void 0&&t.codec in Ge)return Ge[t.codec].tsType;switch(t.type){case"uuid":case"text":case"timestamp":case"date":return"string";case"sql":return t.sqlType!==void 0&&kn.has(t.sqlType)?"number":t.sqlType!==void 0&&Ln.has(t.sqlType)?"Json":"string";case"integer[]":return"number[]";case"boolean[]":return"boolean[]";case"uuid[]":case"text[]":case"bigint[]":case"numeric[]":case"timestamp[]":case"date[]":return"string[]";case"integer":return"number";case"bigint":case"numeric":return"string";case"boolean":return"boolean";case"jsonb":return"Json";case"enum":{let e=t.enumValues??[];return e.length===0?"string":e.map(n=>JSON.stringify(n)).join(" | ")}case"enum[]":{let e=t.enumValues??[];return e.length===0?"string[]":`(${e.map(n=>JSON.stringify(n)).join(" | ")})[]`}default:{let e=t.type;throw new Error(`env-gen: the column type "${String(e)}" has no TypeScript type \u2014 add a case to baseTsType`)}}}function pi(t){if(t.type==="enum"){let e=[...t.enumValues??[]].sort();return e.length===0?"enum":`enum:${e.join("|")}`}return t.type}function go(t){let e=`Pg<${mi(t)}, ${JSON.stringify(pi(t))}>`;return t.nullable?`${e} | null`:e}function fi(t){return t.nullable===!0||t.defaultRandom===!0||t.defaultNow===!0||t.defaultValue!==void 0||t.defaultSql!==void 0||t.dbAssigned===!0||t.identity!==void 0}function gi(t){return t.identity==="always"||t.generated!==void 0}var Nn=t=>/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)?t:JSON.stringify(t);function ho(t,e,n){let s=Object.entries(t.columns).filter(([,i])=>i._def.ignored!==!0),r=s.map(([i,c])=>`${n} ${Nn(i)}: ${go(c._def)};`),o=s.filter(([,i])=>!gi(i._def)).map(([i,c])=>`${n} ${Nn(i)}${fi(c._def)?"?":""}: ${go(c._def)};`),a=e.map(i=>`${Nn(i.name)}: { to: ${JSON.stringify(i.to)}; kind: ${JSON.stringify(i.kind)}; via: ${JSON.stringify(i.via)} }`).join("; ");return[`${n}${Nn(t.name)}: {`,`${n} row: {`,...r,`${n} };`,`${n} insert: {`,...o,`${n} };`,`${n} relations: {${a===""?"":` ${a} `}};`,`${n} uniqueKeys: ${JSON.stringify(st(t))};`,...t.search!==void 0?[`${n} searchable: true;`]:[],`${n}};`].join(`
340
+ `)}function hi(t){for(let e of t)for(let n of Object.values(e.tables))for(let[s,r]of Object.entries(n.columns)){let o=r._def;if(!(o.transform===void 0||o.codec!==void 0))throw new De([`table "${he(e.name,n.name)}" column "${s}" declares .transform<T>(): its target type does not exist at runtime, so the generated types would say something the engine contradicts. Declare a named codec instead \u2014 .asNumber() for a JS number, .asDecimal() for the exact string.`])}}var us="// palbase:env:begin",ds="// palbase:env:end",Eo=`/**
346
341
  * A column's Postgres type, carried in its TypeScript type.
347
342
  *
348
343
  * __pg is optional, so a row still reads as a plain string or number and is
@@ -359,14 +354,36 @@ type Pg<T, N extends string> = T & { readonly __pg?: N };
359
354
  * undefined included: JSON.stringify drops undefined fields, and code that
360
355
  * writes { a: undefined } is everywhere \u2014 refusing it would be wrong.
361
356
  */
362
- type Json = string | number | boolean | null | undefined | Json[] | { [k: string]: Json };
357
+ type Json = string | number | boolean | null | undefined | Json[] | { [k: string]: Json };`;function wo(t,e){hi(t);let n=wn(t),s=u=>Object.values(u.tables).filter(g=>g.ignored!==!0),r=t.find(u=>u.name==="public"),o=t.filter(u=>u.name!=="public"),a=(r?s(r):[]).map(u=>ho(u,n.get(u.name)??[]," ")),i=a.length>0?`
358
+ ${a.join(`
359
+ `)}
360
+ `:"",c=o.map(u=>{let g=s(u).map(p=>ho(p,n.get(he(u.name,p.name))??[]," "));return` ${Nn(u.name)}: {
361
+ ${g.join(`
362
+ `)}
363
+ };`}),l=c.length>0?`
364
+ ${c.join(`
365
+ `)}
366
+ `:"";return`declare module ${JSON.stringify(e)} {
367
+ interface Tables {${i}}
368
+ interface Schemas {${l}}
369
+ }`}function Ei(t,e={}){let n=e.module??"palbase/backend/env",s=wo(t,n);return`// AUTO-GENERATED by Palgres' makeEnvDts from the app's schema files \u2014 DO NOT EDIT.
370
+ // Nothing writes it again on its own: run makeEnvDts again when a schema changes.
371
+ // Augments the ${n} \`Tables\` interface so \`Database.public.*\`
372
+ // is typed with no import and no generic. Schemas other than \`public\` land
373
+ // under \`Schemas\` and are reached by name the same way:
374
+ // \`Database.<schema>.<table>\`.
375
+
376
+ ${Eo}
363
377
 
364
- ${lo}
365
- declare module ${JSON.stringify(n)} {
366
- interface Tables {${c}}
367
- interface Schemas {${d}}
368
- }
369
- ${co}
378
+ ${us}
379
+ ${s}
380
+ ${ds}
370
381
 
371
382
  export {};
372
- `}function ci(t,e){let n=e.tenant,s=e.key??"id",r=(d,u)=>{if(u==null||typeof u!="string"&&typeof u!="number"&&typeof u!="bigint"&&typeof u!="boolean")throw new TypeError(`defineRepository: the ${d} is a string, number, bigint or boolean, not ${u===null?"null":typeof u}`);return u},o=d=>({[n]:r("tenant",d)}),a=(d,u)=>({[n]:r("tenant",d),[s]:r(s,u)}),l=(d,u)=>({...d,[n]:r("tenant",u)}),c=d=>{let u=[n,s].filter(f=>Object.hasOwn(d,f));if(u.length>0)throw new Ne({fields:u.map(f=>({field:f,message:"cannot be changed through this update"}))},`defineRepository: the patch carries ${u.join(" and ")}, which an update does not change`);return d};class i{async list(u){return t.findMany({where:o(u)})}async find(u,f){return(await t.findMany({where:a(u,f),limit:1}))[0]??null}async insert(u,f){return t.insert(l(f,u))}async update(u,f,g){let p=await this.updateScoped(u,f,g);if(p===null)throw new ut(`no row ${s}=${String(f)} where ${String(n)}=${String(u)}`);return p}async updateScoped(u,f,g){return(await t.updateMany({where:a(u,f),set:c(g)}))[0]??null}async delete(u,f){await t.deleteMany({where:a(u,f)})}}return i}function ui(t){return pt({$col:t},"col")}function di(t,...e){return pt({$sql:{text:[...t],values:e}},"sql")}async function mi(t,e={}){let n=In(e.retry),s=0;for(;;)try{return await t()}catch(r){if(s>=n||!je(r))throw r;s+=1}}function pi(t,e){return{search:(n,s)=>e.search(t,n,s),insert:n=>e.insert(t,n),aggregate:(n=>e.aggregate(t,n)),insertMany:((n,s)=>e.insertMany(t,n,s)),put:n=>e.put(t,n.data,{onConflict:n.onConflict}),update:n=>e.update(t,ys(t,n.where),n.set),delete:n=>e.delete(t,n),findUnique:(n=>e.findUnique(t,n.where,{select:n.select,lock:n.lock})),findById:n=>e.findById(t,n),page:(n=>{let{where:s,...r}=n??{};return e.page(t,s,r)}),findMany:(n=>{let{where:s,...r}=n??{};return e.findMany(t,s,r)}),updateMany:(n=>e.updateMany(t,n.where,n.set,n.returning===void 0?void 0:{returning:n.returning})),deleteMany:n=>e.deleteMany(t,n.where),count:n=>e.count(t,n?.where)}}function fi(t,e){let n={},s=r=>Object.fromEntries(Object.values(t.tables).map(o=>[o.name,r.table(he(t.name,o.name))]));for(let[r,o]of Object.entries(t.tables))n[r]=pi(he(t.name,o.name),e);return{tables:n,diagnostics:()=>e.diagnostics(),transaction(r){let o=new Ts;return $s(e,{tables:s(o)},o,r)}}}async function jc(t){await Pt(t.sql);let e={cat:new Zn(t.schemas,t.binding),binding:t.binding,roles:{...Xe,...t.roles},admission:wr(t.sql,t.admission),cursorKey:t.cursorSecret?kr(t.cursorSecret):null,search:mr(t.embed,t.log)};return{binding:t.binding,request(n,s){let r=oo(t.sql,e,n,s);return{database:Rs(r.client),client:r.client,commit:()=>r.commit(),rollback:o=>r.rollback(o)}}}}export{_t as BUN_CLOSE_GRACE_MS,Ne as BadRequest,go as Conflict,Xe as DEFAULT_ROLES,mt as DeadlockDetected,Le as DriverRefused,po as ENGINE_RAISED,co as ENV_BLOCK_END,lo as ENV_BLOCK_START,mo as HTTP_ERROR_BRAND,oe as HttpError,dn as MigrationOpen,Fe as MigrationRefused,xt as Migrator,ut as NotFound,ia as POSTGRES_JS_TYPES,pn as RowsHeld,J as SEARCH_SCHEMA,te as SQLSTATE,_e as SchemaError,dt as SerializationFailure,Pe as TooManyRequests,Ke as TransactionAborted,yo as TxPlanError,wo as TxRefError,We as UniqueViolation,Xn as VersionNotMigrated,mn as VersionStillConnected,At as adoptionBaseline,$o as ago,Gn as applicationName,Wo as bind,fn as bindStatement,Qs as binding,hn as buildRelations,cr as bunDriver,Dt as cancelBackendThrough,dr as cancelOpeningThrough,Re as canonicalType,ur as closeBunPool,ui as col,ce as connectionFailure,sn as constraintOf,jc as createDatabase,ya as createSearchWorker,Fo as cutOff,Ro as decrement,ci as defineRepository,De as digest,Lo as drift,lt as emptySchema,Ye as encodeParam,gn as encodePgArray,jo as forgetUser,So as increment,lr as inlineParams,Kn as introspect,xe as isColRef,Eo as isData,Js as isDirect,fo as isEngineRaised,je as isRetryable,li as makeEnvDts,fi as makeTypedDB,ho as markData,be as markEngineRaised,ge as normalize,To as now,bt as plan,ua as postgresJsDriver,Yo as pullSchema,ca as renewingBunPool,It as searchPath,di as sqlFragment,ae as sqlstateOf,ct as validate,Ie as vectorTable,Pt as verifyDriver,Te as versionSchema,mi as withRetry};
383
+ `}function wi(t,e={}){let n=e.module??"palbase/backend/env";return`${us}
384
+ // The app's tables, from its schema files: rows, inserts, unique keys and relations (with, has). Written by
385
+ // palbase db:types \u2014 run it again when a schema changes.
386
+ ${Eo}
387
+
388
+ ${wo(t,n)}
389
+ ${ds}`}function yi(t,e){let n=e.tenant,s=e.key??"id",r=(u,g)=>{if(g==null||typeof g!="string"&&typeof g!="number"&&typeof g!="bigint"&&typeof g!="boolean")throw new TypeError(`defineRepository: the ${u} is a string, number, bigint or boolean, not ${g===null?"null":typeof g}`);return g},o=u=>({[n]:r("tenant",u)}),a=(u,g)=>({[n]:r("tenant",u),[s]:r(s,g)}),i=(u,g)=>({...u,[n]:r("tenant",g)}),c=u=>{let g=[n,s].filter(p=>Object.hasOwn(u,p));if(g.length>0)throw new Ae({fields:g.map(p=>({field:p,message:"cannot be changed through this update"}))},`defineRepository: the patch carries ${g.join(" and ")}, which an update does not change`);return u};class l{async list(g){return t.findMany({where:o(g)})}async find(g,p){return(await t.findMany({where:a(g,p),limit:1}))[0]??null}async insert(g,p){return t.insert(i(p,g))}async update(g,p,h){let m=await this.updateScoped(g,p,h);if(m===null)throw new gt(`no row ${s}=${String(p)} where ${String(n)}=${String(g)}`);return m}async updateScoped(g,p,h){return(await t.updateMany({where:a(g,p),set:c(h)}))[0]??null}async delete(g,p){await t.deleteMany({where:a(g,p)})}}return l}function Ti(t){return wt({$col:t},"col")}function $i(t,...e){return wt({$sql:{text:[...t],values:e}},"sql")}async function Si(t,e={}){let n=Cn(e.retry),s=0;for(;;)try{return await t()}catch(r){if(s>=n||!He(r))throw r;s+=1}}function Ri(t,e){return{search:(n,s)=>e.search(t,n,s),insert:n=>e.insert(t,n),aggregate:(n=>e.aggregate(t,n)),insertMany:((n,s)=>e.insertMany(t,n,s)),put:n=>e.put(t,n.data,{onConflict:n.onConflict}),update:n=>e.update(t,Os(t,n.where),n.set),delete:n=>e.delete(t,n),findUnique:(n=>e.findUnique(t,n.where,{select:n.select,lock:n.lock})),findById:n=>e.findById(t,n),page:(n=>{let{where:s,...r}=n??{};return e.page(t,s,r)}),findMany:(n=>{let{where:s,...r}=n??{};return e.findMany(t,s,r)}),updateMany:(n=>e.updateMany(t,n.where,n.set,n.returning===void 0?void 0:{returning:n.returning})),deleteMany:n=>e.deleteMany(t,n.where),count:n=>e.count(t,n?.where)}}function bi(t,e){let n={},s=r=>Object.fromEntries(Object.values(t.tables).map(o=>[o.name,r.table(he(t.name,o.name))]));for(let[r,o]of Object.entries(t.tables))n[r]=Ri(he(t.name,o.name),e);return{tables:n,diagnostics:()=>e.diagnostics(),transaction(r){let o=new ks;return Ls(e,{tables:s(o)},o,r)}}}async function nu(t){await Bt(t.sql);let e={cat:new rt(t.schemas,t.binding),binding:t.binding,roles:{...Je,...t.roles},admission:Ar(t.sql,t.admission),cursorKey:t.cursorSecret?Pr(t.cursorSecret):null,search:Tr(t.embed,t.log)};return{binding:t.binding,request(n,s){let r=fo(t.sql,e,n,s);return{database:xs(r.client),client:r.client,commit:()=>r.commit(),rollback:o=>r.rollback(o)}}}}export{Ft as BUN_CLOSE_GRACE_MS,Ae as BadRequest,So as Conflict,Je as DEFAULT_ROLES,Et as DeadlockDetected,Ie as DriverRefused,To as ENGINE_RAISED,ds as ENV_BLOCK_END,us as ENV_BLOCK_START,yo as HTTP_ERROR_BRAND,oe as HttpError,pn as MigrationOpen,$e as MigrationRefused,Dt as Migrator,gt as NotFound,ga as POSTGRES_JS_TYPES,gn as RowsHeld,J as SEARCH_SCHEMA,ne as SQLSTATE,De as SchemaError,ht as SerializationFailure,Me as TooManyRequests,Ve as TransactionAborted,Ao as TxPlanError,No as TxRefError,Ke as UniqueViolation,Zn as VersionNotMigrated,fn as VersionStillConnected,xt as adoptionBaseline,ko as ago,Qn as applicationName,Jo as bind,hn as bindStatement,ar as binding,wn as buildRelations,Er as bunDriver,Ut as cancelBackendThrough,yr as cancelOpeningThrough,be as canonicalType,wr as closeBunPool,Ti as col,ue as connectionFailure,on as constraintOf,nu as createDatabase,Oa as createSearchWorker,Vo as cutOff,Io as decrement,yi as defineRepository,Pe as digest,Po as drift,pt as emptySchema,ze as encodeParam,En as encodePgArray,wi as envBlock,zo as forgetUser,Lo as increment,hr as inlineParams,Yn as introspect,xe as isColRef,bo as isData,rr as isDirect,$o as isEngineRaised,He as isRetryable,Ei as makeEnvDts,bi as makeTypedDB,Ro as markData,Ne as markEngineRaised,ge as normalize,Oo as now,Lt as plan,wa as postgresJsDriver,sa as pullSchema,Ea as renewingBunPool,Pt as searchPath,$i as sqlFragment,ae as sqlstateOf,ft as validate,ve as vectorTable,Bt as verifyDriver,Te as versionSchema,Si as withRetry};