UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 1.86 kB
import{c as quoteSqliteIdentifier}from"./sqlite-schema-sql-D2HdV_QG.mjs";import{t as listDatabaseEngineStores}from"./registry-C9Ag6oZl.mjs"; //!src/infra/database-engine/postgres-store-metadata.ts var DatabasePortDataError=class extends Error{};async function withPostgresConnection(e,t,n){const{Client:r}=await import(`pg`);const i=new r({connectionString:e.connection,...n});i.on(`error`,()=>void 0);await i.connect();try{return await t(i)}finally{await i.end()}}async function assertDatabasePortNamespaceOwner(e,t){if(!(await e.query(`SELECT nspowner = (SELECT oid FROM pg_roles WHERE rolname = current_user) AS owned FROM pg_namespace WHERE nspname = $1`,[t])).rows[0]?.owned)throw new DatabasePortDataError(`observed missing namespace or different owner; required namespace owned by the configured role`)}async function readPostgresStoreMetadata(e,t){const n=await e.query(`SELECT store, store_id, version FROM ${quoteSqliteIdentifier(t.namespace)}.openclaw_schema_meta`).then(e=>e.rows,async()=>{await assertDatabasePortNamespaceOwner(e,t.namespace);throw new DatabasePortDataError(`Cannot resolve PostgreSQL anchor family; use a compatible binary or restore its matched backup.`)});const r=n[0];if(n.length!==1||!r||r.store_id!==t.storeId)throw new DatabasePortDataError(`PostgreSQL anchor does not match its store metadata`);const i=Number(r.version);if(!/^\d+$/u.test(r.version)||!Number.isSafeInteger(i))throw new DatabasePortDataError(`PostgreSQL store metadata has an invalid schema version`);if(!listDatabaseEngineStores().some(e=>e.postgres&&e.id===r.store))throw new DatabasePortDataError(`Unknown PostgreSQL anchor family; use a compatible binary.`);return{store:r.store,version:i}}function withPostgresStoreMetadata(e,t,n){return withPostgresConnection(e,async e=>n(e,await readPostgresStoreMetadata(e,t)))}export{withPostgresStoreMetadata};