Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
70 changes: 69 additions & 1 deletion src/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -316,6 +316,74 @@ 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 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 = {
Expand Down
Loading