openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
8 lines • 1.34 kB
JavaScript
const e=/CREATE (?:UNIQUE )?INDEX ("(?:[^"]|"")+") ON ("(?:[^"]|"")+") (?:'(?:[^']|'')*'|"(?:[^"]|"")*"|[^;'"])*;/g,t=/'(?:[^']|'')*'|"(?:[^"]|"")*"|\) (ASC|DESC)(?=,|\))/g;function withLegacyPostgresIndexOrdering(n,r=`sqlite`,i=n){let a=/* @__PURE__ */ new Map;if(r===`schema-nullability`)for(let[,e,t]of i.matchAll(/CREATE TABLE ("(?:[^"]|"")+") \(\n([\s\S]*?)\n\);/g))a.set(e,new Set([...t.matchAll(/^ {2}("(?:[^"]|"")+") (?:text|bigint|bytea|double precision)([^\n]*)/gm)].filter(e=>!/\bNOT NULL\b/.test(e[2].replace(`GENERATED BY DEFAULT AS IDENTITY`,``).split(/\bDEFAULT\b/)[0])).map(([,e])=>e)));return n.replace(e,(e,n,i)=>r===`schema-nullability`?e.replace(/'(?:[^']|'')*'|"(?:[^"]|"")*"|\(("(?:[^"]|"")+")\) (ASC|DESC)(?=,|\))/g,(e,t,n)=>t&&a.get(i)?.has(t)?`${e} NULLS ${n===`DESC`?`LAST`:`FIRST`}`:e):e.replace(t,(e,t)=>t?`${e} NULLS ${t===`DESC`?`LAST`:`FIRST`}`:e))}function postgresIndexOrderingMigration(t,n=`sqlite`){let r=withLegacyPostgresIndexOrdering(t,n),i=new Map([...r.matchAll(e)].map(([e,t])=>[t,e]));return[...t.matchAll(e)].filter(([e,t])=>i.get(t)!==e).map(([e,t,n])=>`DO $index_order$
BEGIN
IF to_regclass('${n.replaceAll(`'`,`''`)}') IS NOT NULL THEN
DROP INDEX ${t};
${e}
END IF;
END $index_order$;`).join(`
`)}export{withLegacyPostgresIndexOrdering as n,postgresIndexOrderingMigration as t};