@coreframe/db 0.2.4 → 0.2.6
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/migrate-held.d.ts +2 -0
- package/dist/migrate-held.js +1 -1
- package/package.json +3 -3
package/dist/migrate-held.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ type HeldMigrationOptions = {
|
|
|
11
11
|
cwd?: string;
|
|
12
12
|
env?: NodeJS.ProcessEnv;
|
|
13
13
|
target?: HeldMigrationTarget;
|
|
14
|
+
report?: (line: string) => void;
|
|
15
|
+
requireLocalLease?: boolean;
|
|
14
16
|
};
|
|
15
17
|
/** Validates a bare held migration ID, never a path, suffix, or pattern. */
|
|
16
18
|
declare function validateHeldMigrationId(id: string): string;
|
package/dist/migrate-held.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{LOCAL_POSTGRES_ENV_FLAG as e,startProjectPostgres as t}from"./local-postgres.js";import{LOCAL_TURSO_ENV_FLAG as n,startProjectTurso as r}from"./local-turso.js";import{collectHeldMigrationFiles as i}from"./migrations.js";import a from"./_virtual/_@oxc-project_runtime@0.138.0/helpers/esm/usingCtx.js";import{createHeldDatabase as o}from"./held-database.js";import{validateHeldTransactionSql as s}from"./held-sql.js";import{lstat as c,readFile as l}from"node:fs/promises";import{resolve as u}from"node:path";import{readProjectConfig as d}from"@coreframe/config";import{createHash as f}from"node:crypto";const p=/^[A-Za-z0-9][A-Za-z0-9_-]*$/u;function m(e){if(typeof e!=`string`||!p.test(e))throw Error(`Expected a held migration ID without .sql, paths, or wildcards. Use coreframe db held list.`);return e}async function h(e={}){let t=await v(e.cwd),
|
|
1
|
+
import{LOCAL_POSTGRES_ENV_FLAG as e,startProjectPostgres as t}from"./local-postgres.js";import{LOCAL_TURSO_ENV_FLAG as n,startProjectTurso as r}from"./local-turso.js";import{collectHeldMigrationFiles as i}from"./migrations.js";import a from"./_virtual/_@oxc-project_runtime@0.138.0/helpers/esm/usingCtx.js";import{createHeldDatabase as o}from"./held-database.js";import{validateHeldTransactionSql as s}from"./held-sql.js";import{lstat as c,readFile as l}from"node:fs/promises";import{resolve as u}from"node:path";import{readProjectConfig as d}from"@coreframe/config";import{createHash as f}from"node:crypto";const p=/^[A-Za-z0-9][A-Za-z0-9_-]*$/u;function m(e){if(typeof e!=`string`||!p.test(e))throw Error(`Expected a held migration ID without .sql, paths, or wildcards. Use coreframe db held list.`);return e}async function h(e={}){let t=e.report??console.log,n=await v(e.cwd),r=async e=>{for(let r of n)t(`${r.id} | prerequisites: ${r.dependencies.join(`, `)||`none`} | ${e?await x(e,r):`unknown (database not inspected)`}`);n.length||t(`No held migrations found.`)};e.target?await S(e,r,!0):await r()}async function g(e,t={}){let n=t.report??console.log,r=y(await v(t.cwd),e),i=async e=>{n(`${r.id} | prerequisites: ${r.dependencies.join(`, `)||`none`} | ${e?await x(e,r):`unknown (database not inspected)`}`),n(r.sql)};t.target?await S(t,i,!0):await i()}async function _(e,t){if(!t?.target)throw Error(`Use coreframe db held apply <id> --local or --environment <environment>.`);let n=t.report??console.log,r=await v(t.cwd),i=y(r,e);await S(t,async t=>{await t.transaction(async t=>{let a=await x(t,i);if(a===`checksum mismatch`)throw Error(`Held migration ${e} changed after it was applied.`);if(a===`applied`){n(`Already applied: ${e}`);return}for(let e of i.dependencies)if(await x(t,y(r,e))!==`applied`)throw Error(`Unmet prerequisite ${e}. Apply it separately with coreframe db held apply ${e} to the same target; changed applied files must be restored.`);await w(t),await t.executeScript(i.sql),await t.execute(D(t.dialect,e,C(i.sql)))}),n(`Held migration complete: ${e}. Verify this change separately; this does not establish release completion.`)})}async function v(e=process.cwd()){try{if((await c(u(e,`drizzle-hold`))).isSymbolicLink())throw Error(`drizzle-hold must be a directory, not a symbolic link.`)}catch(e){if(!(e instanceof Error&&`code`in e&&e.code===`ENOENT`))throw e}let t=await Promise.all((await i(e)).map(async e=>{m(e.id);let t=await l(e.path,`utf8`),n=b(t,e.relativePath);return s(t,e.relativePath),{...e,sql:t,dependencies:n}})),n=new Map(t.map(e=>[e.id,e])),r=new Set,a=new Set;function o(e){if(a.has(e))throw Error(`Held migration dependency cycle at ${e}.`);if(r.has(e))return;let t=n.get(e);if(!t)throw Error(`Missing held migration dependency ${e}.`);a.add(e);for(let e of t.dependencies)o(e);a.delete(e),r.add(e)}for(let e of t)o(e.id);return t}function y(e,t){m(t);let n=e.filter(e=>e.id===t);if(n.length!==1)throw Error(`Unknown or ambiguous held migration ${t}. Use coreframe db held list.`);return n[0]}function b(e,t){let n=e.match(/^(?:\s*--[^\n]*(?:\n|$)|\s*\n)*/u)?.[0]??``;for(let e of[`Held migration`,`Safe after`,`Verification`]){let r=[...n.matchAll(RegExp(`^-- ${e}:([^\\r\\n]*)`,`gm`))];if(r.length!==1||!r[0]?.[1]?.trim())throw Error(`${t} requires one nonempty -- ${e}: header.`)}let r=[...n.matchAll(/^-- Depends on:([^\r\n]*)/gm)];if(r.length>1)throw Error(`${t} has duplicate Depends on headers.`);if(!r.length)return[];let i=r[0][1].trim();if(i===`none`)return[];let a=i.split(`,`).map(e=>m(e.trim()));if(new Set(a).size!==a.length)throw Error(`${t} has duplicate dependencies.`);return a}async function x(e,t){if(!(await e.execute(e.dialect===`postgres`?`select to_regclass('drizzle.__held_migrations') as name`:`select name from sqlite_master where type = 'table' and name = '__held_migrations'`)).rows[0]?.name)return`pending`;let n=await e.execute(E(e.dialect,t.id));return n.rows.length?n.rows[0]?.sha256===C(t.sql)?`applied`:`checksum mismatch`:`pending`}async function S(i,s,l=!1){let f=i.cwd??process.cwd(),p=await d({cwd:f}),m=p.database?.dialect??`postgres`;if(i.target===`local`)try{var h=a();let o=i.env??process.env,s=m===`sqlite`?n:e;if(i.requireLocalLease&&o[s]!==`1`)throw Error(`Held --local requires an active Coreframe database lease for the configured dialect.`);if(l&&o[s]!==`1`){let e=u(f,m===`sqlite`?`.coreframe/turso.db`:`.coreframe/postgres/PG_VERSION`);try{if(!(await c(e)).isFile())throw Error(`Not an existing database`)}catch{throw Error(`The managed local database is not initialized. Set it up separately, or omit --local for static held inspection.`)}}let d=h.a(m===`sqlite`?await r({cwd:f,config:p,env:o,useSupervisorLease:!0}):await t({cwd:f,config:p,env:o,useSupervisorLease:!0}));if(d.env[s]!==`1`)throw Error(`Held --local requires a Coreframe-managed local database.`);await g(d.env)}catch(e){h.e=e}finally{await h.d()}else if(i.target&&[`staging`,`production`].includes(i.target.environment)){if(!i.target.env||typeof i.target.env!=`object`)throw Error(`Explicit environment database credentials are required; ambient credentials are not used.`);await g(i.target.env)}else throw Error(`An explicit held database target is required.`);async function g(e){let t=await o(m,{cwd:f,env:e});try{await s(t)}finally{await t.close?.()}}}function C(e){return f(`sha256`).update(e).digest(`hex`)}async function w(e){for(let t of T(e.dialect))await e.execute(t)}function T(e){switch(e){case`postgres`:return[`create schema if not exists drizzle`,`create table if not exists drizzle.__held_migrations (
|
|
2
2
|
id text primary key,
|
|
3
3
|
sha256 text not null,
|
|
4
4
|
applied_at timestamptz not null default now()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coreframe/db",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Database setup, migration, seeding, and local PostgreSQL and SQLite/libSQL utilities for Coreframe apps",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"import-meta-resolve": "^4.2.0",
|
|
27
27
|
"local-postgres": "0.3.1",
|
|
28
28
|
"nano-spawn": "^2.1.0",
|
|
29
|
-
"@coreframe/
|
|
30
|
-
"@coreframe/
|
|
29
|
+
"@coreframe/config": "^0.3.3",
|
|
30
|
+
"@coreframe/dev-supervisor": "^0.2.7"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@libsql/client": "^0.17.4",
|