UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.71 kB
import{h as resolveDefaultSecretProviderAlias}from"./ref-contract-CdpCUlb6.mjs";import{n as runtimeProcessEntrypoints}from"./runtime-process-entrypoints-Dnd1WskT.mjs";import{a as resolveRuntimeWorkerUrl}from"./runtime-worker-url-Dqf-I1cs.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{n as openNodeSqliteDatabase}from"./node-sqlite-DXyF1fBG.mjs";import{l as hasActiveGatewayStateOwner}from"./gateway-state-owner-D_klysuh.mjs";import{n as captureRuntimeWorkerSource}from"./runtime-worker-generation-0fwjR0Cv.mjs";import{a as withDatabaseEngineSelection,r as getScopedDatabaseEngineSelection}from"./context-DZjni16e.mjs";import{a as isConfigReadFailure,i as formatInvalidConfigDetails,n as createConfigReadError,r as createInvalidConfigError}from"./io.invalid-config-B2POcxOa.mjs";import{t as readBackupEngine}from"./physical-store-Dt80uUi6.mjs";import{n as resolveDatabaseEngineSelection}from"./config-eTB-xhop.mjs";import"./physical-store-inspection-BHEZGfsf.mjs";import"./database-backup.operations-C3IunZW3.mjs";import path from"node:path";import fs from"node:fs/promises";import{AsyncLocalStorage}from"node:async_hooks"; //!src/snapshot/database-backup.ts const offlineBackup=new AsyncLocalStorage;async function runOfflineDatabaseBackup(e,t,n,r){const i=offlineBackup.getStore();if(i){i.assertCurrent();return t(i)}const{runWithLocalStateOwner:a}=await import(`./local-state-owner-D6lzsNxM.mjs`);const{resolveStateDir:o}=await import(`./paths-CaVF-E93.mjs`);if(hasActiveGatewayStateOwner(path.join(o(),`state`,`openclaw.sqlite`)))throw new Error(`PostgreSQL backup requires the Gateway stopped; stop through the service owner, then rerun.`);const{createConfigIO:s}=await import(`./config/config.js`);const c=n&&`config`in n?n:void 0;const runScoped=(e,n,r)=>{const i={config:e,assertCurrent:r};return withDatabaseEngineSelection(n,()=>offlineBackup.run(i,()=>t(i)))};return a({method:`backup.create`,params:{},target:e,onForeignOwner:c&&r?.online?async({assertCurrent:e})=>runScoped(c.config,c.selection,e):`refuse`,runLocal:async({assertCurrent:e})=>{e();let t=c?.config;if(!t){const r=n&&`configPath`in n?n:void 0;const i=s({...n&&`configPath`in n?n:{},observe:false,pluginValidation:`core-only`});const a=r?.configSourcePath?await fs.readFile(r.configSourcePath,`utf8`):void 0;e();const o=await(a===void 0?i.readConfigFileSnapshot():i.readConfigFileSnapshotFromRaw(a));e();if(!o.valid){const e=formatInvalidConfigDetails(o.issues);throw isConfigReadFailure(o)?createConfigReadError(o,e):createInvalidConfigError(o.path,e)}t=o.config;if(r?.postgresConnectionEnv)t={...t,database:{...t.database,postgres:{...t.database?.postgres,connection:{source:`env`,provider:resolveDefaultSecretProviderAlias(t,`env`),id:r.postgresConnectionEnv}}}}}const r=c?.selection??await resolveDatabaseEngineSelection(t);e();return runScoped(t,r,e)}})}async function runBackupTask(e,t){t();const{WorkerTaskPool:n}=await import(`./worker-task-pool-rzaYfBJL.mjs`);const r=captureRuntimeWorkerSource(resolveRuntimeWorkerUrl(runtimeProcessEntrypoints.databaseBackup));const i=getScopedDatabaseEngineSelection()??await resolveDatabaseEngineSelection(e.params.config);t();return withDatabaseEngineSelection(i,async()=>{const i=new n({workerUrl:r.moduleUrl,maxWorkers:1});try{const n=await i.run(e,{});t();return n}finally{await i.close()}})}function capturePostgresBackup(e){if(!(`config`in e))return runOfflineDatabaseBackup(e.sourcePath,({config:t,assertCurrent:n})=>capturePostgresBackup({...e,config:t,assertCurrent:n,profile:`user-backup`}));const{assertCurrent:t,...n}=e;return runBackupTask({operation:`capture`,params:n},t)}async function createVerifiedDatabaseMigrationBackup(e){try{var t=_usingCtx();e.assertCurrent();if(readBackupEngine(e.sourcePath).engine===`sqlite`){const{createVerifiedSqliteSnapshot:t}=await import(`./sqlite-snapshot-C6LLrZkv.mjs`);return t({sourcePath:e.sourcePath,targetPath:e.targetPath,preserveRowIds:true,beforePublish:e.assertCurrent,validate:e.validate})}await capturePostgresBackup({sourcePath:e.sourcePath,directory:e.targetPath,profile:`exact`,config:e.config,assertCurrent:e.assertCurrent});const n=t.u(openNodeSqliteDatabase(path.join(e.targetPath,`database.sqlite`),{readOnly:true}));e.validate(n,e.targetPath);e.assertCurrent();return{path:e.targetPath}}catch(e){t.e=e}finally{t.d()}}async function captureGitPostgresBackup(e){if((e.physicalEngine??readBackupEngine(e.snapshotPath).engine)!==`postgres`)return;const{captureRuntimeConfigAsyncReader:t}=await import(`./config/config.js`);const n=await t({observe:false,skipPluginValidation:true})();const r=await resolveDatabaseEngineSelection(n);await fs.mkdir(e.outputPath,{mode:448});const i=await runOfflineDatabaseBackup(e.snapshotPath,({config:t,assertCurrent:n})=>capturePostgresBackup({sourcePath:e.snapshotPath,directory:path.join(e.outputPath,`postgres`),profile:`user-backup`,config:t,assertCurrent:n,...e.excludeSecrets?{gitIdentity:e.identity}:{}}),{config:n,selection:r},{online:true});const{gitBackupRedaction:a}=await import(`./git-backup-sanitizer-DxazLdiB.mjs`);const o=a(e.identity,new Set(Object.keys(i.verification.tables)),e.excludeSecrets===true,i.source.owner);const s={schemaVersion:2,identity:e.identity,postgresSource:{storeId:i.source.storeId,namespace:i.source.namespace},userVersion:i.exportSchemaVersion,...o,tables:{}};await fs.writeFile(path.join(e.outputPath,`manifest.json`),`${JSON.stringify(s,null,2)}\n`,{mode:384});return s}function discardPostgresRestores(e,t){return runBackupTask({operation:`discard`,params:{config:e,namespaces:t}},()=>{})}async function discardPostgresSchemaCaptures(e,t){const{readSnapshotExportEnvelope:n}=await import(`./export-envelope-B-yD-b-I.mjs`);const r=[];for(const e of t){const t=await n(e);if(t.engine===`postgres`&&t.manifest.schema)r.push(t.manifest.schema)}if(r.length>0)await discardPostgresRestores(e,r)}function preparePostgresRestore({assertCurrent:e,...t}){return runBackupTask({operation:`restore`,params:t},e)}function prunePostgresSchemaCaptures(e,t,n){return runBackupTask({operation:`prune`,params:{sourcePath:e,config:t,directoryPrefix:n}},()=>{})}function readPostgresBackupFamily(e,t){return runBackupTask({operation:`family`,params:{sourcePath:e,config:t}},()=>{})}function fingerprintPostgresBackup(e,t,n){return runBackupTask({operation:`fingerprint`,params:{sourcePath:e,config:t,pluginOwners:n}},()=>{})}export{discardPostgresSchemaCaptures as a,prunePostgresSchemaCaptures as c,discardPostgresRestores as i,readPostgresBackupFamily as l,capturePostgresBackup as n,fingerprintPostgresBackup as o,createVerifiedDatabaseMigrationBackup as r,preparePostgresRestore as s,captureGitPostgresBackup as t,runOfflineDatabaseBackup as u};