@@ -47,6 +47,7 @@ import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'nod
4747import { tmpdir } from 'node:os' ;
4848import { dirname , join , resolve } from 'node:path' ;
4949import { fileURLToPath } from 'node:url' ;
50+ import Database from 'better-sqlite3' ;
5051
5152const HERE = dirname ( fileURLToPath ( import . meta. url ) ) ;
5253const CLI_ROOT = resolve ( HERE , '..' , '..' , '..' ) ;
@@ -125,26 +126,14 @@ process.exit(0);
125126` ;
126127
127128/** The repro's off-shape value, written past the write path: a location keyed the retired way. */
128- const OFF_SHAPE_CHILD = `
129- const { SqlDriver } = await import('@objectstack/driver-sql');
130- const raw = new SqlDriver({ client: 'better-sqlite3', connection: { filename: process.env.FIXTURE_DB }, useNullAsDefault: true });
131- await raw.knex('os21644_site').where({ id: 's1' }).update({ geo: JSON.stringify({ latitude: 1, longitude: 2 }) });
132- await raw.disconnect();
133- process.exit(0);
134- ` ;
135-
136- /** The flag rows and the app rows, read on a connection of our own. */
137- const READ_STATE_CHILD = `
138- const { SqlDriver } = await import('@objectstack/driver-sql');
139- const raw = new SqlDriver({ client: 'better-sqlite3', connection: { filename: process.env.FIXTURE_DB }, useNullAsDefault: true });
140- const k = raw.knex;
141- const flags = await k('sys_migration').select('id', 'last_run_at', 'verified_at', 'blocking').orderBy('id');
142- const site = await k('os21644_site').select('id', 'geo').orderBy('id');
143- const product = await k('os21644_product').select('id', 'image').orderBy('id');
144- await raw.disconnect();
145- process.stdout.write(JSON.stringify({ flags, site, product }));
146- process.exit(0);
147- ` ;
129+ function writeOffShape ( db : string ) : void {
130+ const conn = new Database ( db ) ;
131+ try {
132+ conn . prepare ( 'UPDATE os21644_site SET geo = ? WHERE id = ?' ) . run ( JSON . stringify ( { latitude : 1 , longitude : 2 } ) , 's1' ) ;
133+ } finally {
134+ conn . close ( ) ;
135+ }
136+ }
148137
149138interface Run {
150139 code : number | null ;
@@ -186,10 +175,22 @@ function freshDb(): string {
186175 return db ;
187176}
188177
178+ /**
179+ * The flag rows and the app rows, read on a connection of our own, in this
180+ * process: a child `node` per read cost ~0.5 s each, sixteen times a file.
181+ */
189182function readState ( db : string ) : State {
190- const out = childNode ( READ_STATE_CHILD , { FIXTURE_DB : db } ) ;
191- if ( out . status !== 0 ) throw new Error ( `could not read the fixture database (status ${ out . status } )\n${ out . stderr } ` ) ;
192- return JSON . parse ( out . stdout ) as State ;
183+ // Opened as the child it replaces opened it (read-write, knex's default).
184+ const conn = new Database ( db ) ;
185+ try {
186+ return {
187+ flags : conn . prepare ( 'SELECT id, last_run_at, verified_at, blocking FROM sys_migration ORDER BY id' ) . all ( ) as FlagRow [ ] ,
188+ site : conn . prepare ( 'SELECT id, geo FROM os21644_site ORDER BY id' ) . all ( ) as State [ 'site' ] ,
189+ product : conn . prepare ( 'SELECT id, image FROM os21644_product ORDER BY id' ) . all ( ) as State [ 'product' ] ,
190+ } ;
191+ } finally {
192+ conn . close ( ) ;
193+ }
193194}
194195
195196function flagRow ( state : State , id : string ) : FlagRow | undefined {
@@ -296,8 +297,7 @@ beforeAll(async () => {
296297
297298 // The measured repro: one off-shape value stored, then a misspelled --object --apply.
298299 const reproDb = freshDb ( ) ;
299- const offShape = childNode ( OFF_SHAPE_CHILD , { FIXTURE_DB : reproDb } ) ;
300- if ( offShape . status !== 0 ) throw new Error ( `the off-shape value was not written\n${ offShape . stderr } ` ) ;
300+ writeOffShape ( reproDb ) ;
301301 const reproRightDb = join ( dir , 'data' , 'repro-right.db' ) ;
302302 copyFileSync ( reproDb , reproRightDb ) ;
303303 reproFullDry = await runCommand ( 'value-shapes' , [ '--json' ] , reproDb ) ;
0 commit comments