Skip to content

Commit ed42218

Browse files
committed
fix(driver-sql): only cache sequences-ready flag when DDL ran durably
Harden the lazy ensureSequencesTable fallback: when the table is created on the caller's transaction (SQLite in-tx path), the create is commit-conditional — a rollback would drop it. Cache sequencesTableReady only when the DDL ran on a durable pooled connection (this.knex); on the in-tx path leave the flag unset so the next write cheaply re-verifies via hasTable instead of trusting a stale process-level flag. initObjects still sets it durably up front, so the hot path is unchanged.
1 parent 1a5bc63 commit ed42218

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

packages/plugins/driver-sql/src/sql-driver-autonumber-tokens.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ describe('SqlDriver auto_number format tokens', () => {
209209
await driver.initObjects([
210210
{ name: 'invoice', fields: { invoice_number: { type: 'autonumber', format: 'INV-{0000}' } } },
211211
]);
212-
// First create triggers the lazy table-ensure → in-place migration.
212+
// initObjects pre-ensures the table → the in-place migration runs at init,
213+
// before any write; the first create then reads the already-migrated counter.
213214
const r = await driver.create('invoice', {});
214215

215216
const cols = await k('_objectstack_sequences').columnInfo();

packages/plugins/driver-sql/src/sql-driver.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,13 @@ export class SqlDriver implements IDataDriver {
776776
// Pre-existing table may predate the `key_hash`/`scope` shape. Migrate.
777777
await this.ensureSequencesKeyHashShape(runner);
778778
}
779-
this.sequencesTableReady = true;
779+
// Cache "ready" only when the DDL ran on a durable connection. If it rode
780+
// the caller's transaction (the SQLite in-tx fallback above), the table is
781+
// commit-conditional — a rollback would drop it — so leave the flag unset
782+
// and re-verify (a cheap `hasTable`) on the next write rather than trusting
783+
// a stale process-level flag. `initObjects` sets it durably up front, so
784+
// the hot path is unaffected.
785+
if (runner === this.knex) this.sequencesTableReady = true;
780786
})();
781787
try {
782788
await this.sequencesTableEnsurePromise;

0 commit comments

Comments
 (0)