From c64448a42bd805b05baa1c06583673b9195ad883 Mon Sep 17 00:00:00 2001 From: Jeff Repanich Date: Wed, 25 Nov 2026 16:05:07 -0500 Subject: [PATCH 1/3] Test transactional PostgreSQL migration rollback Refs #73 --- src/integration.test.ts | 43 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/integration.test.ts b/src/integration.test.ts index fe168e6..064eaf2 100644 --- a/src/integration.test.ts +++ b/src/integration.test.ts @@ -286,7 +286,7 @@ integration("PostgreSQL adapter conformance", () => { expect(fresh.rows[0]!.pid).not.toBe(killed); }); - it("should apply every statement of a PostgreSQL migration script atomically", async () => { + it("should apply every statement of a PostgreSQL migration script", async () => { await pool.query('DROP TABLE IF EXISTS "_askr_migrations", "orm_migration_script"'); const manifest: MigrationManifest = { migrations: [ @@ -316,6 +316,47 @@ integration("PostgreSQL adapter conformance", () => { } }); + it("should roll back a transactional PostgreSQL migration when a later statement fails", async () => { + await pool.query('DROP TABLE IF EXISTS "_askr_migrations", "orm_migration_rollback"'); + const createAndSeed = + 'CREATE TABLE "orm_migration_rollback" ("id" integer PRIMARY KEY); INSERT INTO "orm_migration_rollback" VALUES (1);'; + const migration = { + id: "01PGROLLBACK", + parent: null, + transactional: true, + } as const; + const broken = createMigrationsApi(adapter, { + migrations: [ + { + ...migration, + checksum: "broken", + sql: createAndSeed + ' INSERT INTO "orm_migration_rollback" VALUES (1);', + }, + ], + }); + try { + // A duplicate key proves PostgreSQL reached the last statement after inserting the first row. + await expect(broken.apply()).rejects.toMatchObject({ code: "23505" }); + expect( + (await pool.query("SELECT to_regclass('public.orm_migration_rollback') AS name")).rows, + ).toEqual([{ name: null }]); + expect((await pool.query('SELECT id FROM "_askr_migrations"')).rows).toEqual([]); + + const corrected = createMigrationsApi(adapter, { + migrations: [{ ...migration, checksum: "corrected", sql: createAndSeed }], + }); + await expect(corrected.apply()).resolves.toEqual({ applied: [migration.id] }); + expect((await pool.query('SELECT id FROM "orm_migration_rollback"')).rows).toEqual([ + { id: 1 }, + ]); + expect((await pool.query('SELECT id, state FROM "_askr_migrations"')).rows).toEqual([ + { id: migration.id, state: "applied" }, + ]); + } finally { + await pool.query('DROP TABLE IF EXISTS "_askr_migrations", "orm_migration_rollback"'); + } + }); + it("should release the migration lock and leave no application DDL or ledger entry after self-disconnect", async () => { await pool.query('DROP TABLE IF EXISTS "_askr_migrations", "orm_migration_disconnect"'); const manifest: MigrationManifest = { From 3069c662cfb59889f52c097d78a247340a2afb6f Mon Sep 17 00:00:00 2001 From: Jeff Repanich Date: Wed, 25 Nov 2026 16:07:42 -0500 Subject: [PATCH 2/3] Prove migration SQL and ledger update share a transaction Refs #73 --- src/integration.test.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/integration.test.ts b/src/integration.test.ts index 064eaf2..51faebd 100644 --- a/src/integration.test.ts +++ b/src/integration.test.ts @@ -357,6 +357,33 @@ integration("PostgreSQL adapter conformance", () => { } }); + it("should roll back a PostgreSQL migration when the applied ledger row is rejected", async () => { + await pool.query('DROP TABLE IF EXISTS "_askr_migrations", "orm_migration_ledger_failure"'); + const migrations = createMigrationsApi(adapter, { + migrations: [ + { + id: "01LEDGERFAILURE", + parent: null, + checksum: "ledger-failure", + transactional: true, + sql: 'CREATE TABLE "orm_migration_ledger_failure" ("id" integer PRIMARY KEY)', + }, + ], + }); + try { + await migrations.plan(); + await pool.query(`ALTER TABLE "_askr_migrations" ADD CHECK ("id" <> '01LEDGERFAILURE')`); + await expect(migrations.apply()).rejects.toMatchObject({ code: "23514" }); + expect( + (await pool.query("SELECT to_regclass('public.orm_migration_ledger_failure') AS name")) + .rows, + ).toEqual([{ name: null }]); + expect((await pool.query('SELECT id FROM "_askr_migrations"')).rows).toEqual([]); + } finally { + await pool.query('DROP TABLE IF EXISTS "_askr_migrations", "orm_migration_ledger_failure"'); + } + }); + it("should release the migration lock and leave no application DDL or ledger entry after self-disconnect", async () => { await pool.query('DROP TABLE IF EXISTS "_askr_migrations", "orm_migration_disconnect"'); const manifest: MigrationManifest = { From d458dcec19ab87d2a371d9591fcb116e4e51eca8 Mon Sep 17 00:00:00 2001 From: Jeff Repanich Date: Wed, 25 Nov 2026 16:10:54 -0500 Subject: [PATCH 3/3] test(orm): verify derived query immutability --- src/client.test.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/client.test.ts b/src/client.test.ts index 32b1160..2cc8119 100644 --- a/src/client.test.ts +++ b/src/client.test.ts @@ -196,12 +196,25 @@ describe("database client", () => { .distinct() .groupBy(({ users: columns }) => columns.email) .having(eq(columnRef("users", "email"), "a@example.com")) + .where(({ users: columns }) => eq(columns.email, "a@example.com")) .orderBy(({ users: columns }) => columns.email) .limit(5) .offset(2); - expect(query.toSQL().text).toContain("GROUP BY"); + const originalSql = query.toSQL(); + const derived = query.where(({ users: columns }) => eq(columns.groupId, "g")); + expect(query.toSQL()).toEqual(originalSql); + expect(derived.toSQL().text).toContain("GROUP BY"); + expect(derived.toSQL().text).toContain("AND"); expect(db.users.prepare("all-users").name).toBe("all-users"); - await expect(db.users.limit(1).execute()).resolves.toHaveLength(1); + adapter.rows = [{ id: "original", email: "a@example.com", groupId: "g" }]; + await expect(query.execute()).resolves.toEqual([ + { id: "original", email: "a@example.com", groupId: "g" }, + ]); + adapter.rows = [{ id: "derived", email: "a@example.com", groupId: "g" }]; + await expect(derived.execute()).resolves.toEqual([ + { id: "derived", email: "a@example.com", groupId: "g" }, + ]); + expect(adapter.queries.slice(-2)).toEqual([originalSql, derived.toSQL()]); }); it("should cover update, delete, upsert, and composite keys", async () => {