@avelonjs/postgres 0.8.5 → 0.8.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/README.md CHANGED
@@ -178,7 +178,7 @@ bun test
178
178
  | `PostgresDatabase.transaction` | `(callback: (tx: DatabaseTransaction) => Promise<T>) => Promise<T>` | Runs the callback atomically and returns its result. |
179
179
  | `PostgresDatabase.plan` | `() => Promise<MigrationPlan>` | Returns pending migration identifiers and SQL steps. |
180
180
  | `PostgresDatabase.apply` | `() => Promise<readonly MigrationStatus[]>` | Applies pending migrations inside transactions. |
181
- | `PostgresDatabase.rollback` | `(steps?: number) => Promise<readonly MigrationStatus[]>` | Rolls back the newest applied migration batches. |
181
+ | `PostgresDatabase.rollback` | `(steps?: number) => Promise<readonly MigrationStatus[]>` | Rolls back the newest applied migrations. Unknown history ids and non-positive steps fail. |
182
182
  | `PostgresDatabase.status` | `() => Promise<readonly MigrationStatus[]>` | Lists applied and pending migration states. |
183
183
  | `PostgresDatabase.raw` | `() => SQL` | Returns the Bun SQL client at the vendor boundary. |
184
184
  | `PostgresDatabase.resetFixtures` | `() => Promise<void>` | Recreates assay tables and `assay_echo` for live conformance. |
@@ -199,11 +199,12 @@ bun test
199
199
  | `RoundTripCounter` | `() => void` | Called once per statement so a driver can keep its round-trip counter. |
200
200
  | `SqlRunner` | `interface` | `unsafe(text, parameters?)` returning normalized rows; the seam every client implements. |
201
201
  | `SqlBatchRunner` | `interface` | A `SqlRunner` that also applies a known statement list atomically through `batch()`. |
202
+ | `bunSqlRunner` | `(sql: SQL) => SqlBatchRunner` | Adapts Bun's SQL client to the vendor-neutral runner. |
202
203
  | `SqlRows` | `interface` | Normalized `rows` plus the affected-row `count`. |
203
204
  | `SchemaCache` | `type` | `Map` of table name to column set. |
204
205
  | `planMigrations` | `(sql: SQL, migrations: readonly PostgresMigration[]) => Promise<MigrationPlan>` | Builds a pending plan from registered migrations and history. |
205
206
  | `applyMigrations` | `(sql: SQL, migrations: readonly PostgresMigration[]) => Promise<readonly MigrationStatus[]>` | Applies pending migrations inside transactions. |
206
- | `rollbackMigrations` | `(sql: SQL, migrations: readonly PostgresMigration[], steps?: number) => Promise<readonly MigrationStatus[]>` | Rolls back the newest applied migration batches. |
207
+ | `rollbackMigrations` | `(sql: SQL, migrations: readonly PostgresMigration[], steps?: number) => Promise<readonly MigrationStatus[]>` | Rolls back the newest applied migrations. Unknown history ids and non-positive steps fail. |
207
208
  | `statusMigrations` | `(sql: SQL, migrations: readonly PostgresMigration[]) => Promise<readonly MigrationStatus[]>` | Returns applied/pending status for every registered migration. |
208
209
  | `PostgresMigration` | `interface` | Driver-owned `id`, `up`, and `down` SQL pair. |
209
210
  | `resetAssayFixtures` | `(sql: SQL) => Promise<void>` | Provisions empty assay fixtures on a SQL client. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avelonjs/postgres",
3
- "version": "0.8.5",
3
+ "version": "0.8.6",
4
4
  "private": false,
5
5
  "description": "Postgres database driver that compiles Avelon QueryIR to SQL.",
6
6
  "license": "MIT",
@@ -1,4 +1,4 @@
1
- import type { MigrationPlan, MigrationStatus } from '@avelonjs/core'
1
+ import { Invalid, type MigrationPlan, type MigrationStatus } from '@avelonjs/core'
2
2
  import type { CompiledSql } from './compile'
3
3
  import { mapPostgresError } from './errors'
4
4
  import type { SqlBatchRunner, SqlRunner } from './sql-client'
@@ -97,24 +97,33 @@ export async function rollbackMigrations(
97
97
  migrations: readonly PostgresMigration[],
98
98
  steps = 1,
99
99
  ): Promise<readonly MigrationStatus[]> {
100
+ if (!Number.isInteger(steps) || steps < 1) {
101
+ throw new Invalid('Rollback steps must be a positive integer.', {
102
+ metadata: { fields: { steps: ['Expected a positive integer.'] } },
103
+ })
104
+ }
100
105
  await ensureHistory(runner)
101
- const count = Number.isInteger(steps) && steps > 0 ? steps : 1
102
106
  try {
103
107
  const applied = await runner.unsafe(
104
- `SELECT id, applied_at FROM ${HISTORY_TABLE} ORDER BY applied_at DESC LIMIT $1`,
105
- [count],
108
+ `SELECT id, applied_at FROM ${HISTORY_TABLE} ORDER BY applied_at DESC, id DESC LIMIT $1`,
109
+ [steps],
106
110
  )
107
111
 
108
112
  for (const row of applied.rows) {
109
113
  const id = String(row.id)
110
114
  const migration = migrations.find((entry) => entry.id === id)
111
- if (migration === undefined) continue
115
+ if (migration === undefined) {
116
+ throw new Invalid(`Cannot roll back unknown migration ${id}.`, {
117
+ metadata: { fields: { migration: [`${id} is not registered.`] } },
118
+ })
119
+ }
112
120
  await runner.batch([
113
121
  ...migration.down.map((text) => statement(text)),
114
122
  statement(`DELETE FROM ${HISTORY_TABLE} WHERE id = $1`, [id]),
115
123
  ])
116
124
  }
117
125
  } catch (error) {
126
+ if (error instanceof Invalid) throw error
118
127
  mapPostgresError(error, 'migrations.rollback')
119
128
  }
120
129
  return statusMigrations(runner, migrations)