@fabrika/platform-node 0.9.0-dev.22 → 0.9.0-dev.23
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/package.json +2 -2
- package/src/postgres-migrations.ts +27 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/platform-node",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.23",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -38,6 +38,6 @@
|
|
|
38
38
|
"test": "bun test"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
41
|
+
"@fabrika/platform": "0.9.0-dev.23"
|
|
42
42
|
}
|
|
43
43
|
}
|
|
@@ -202,6 +202,7 @@ export async function applyPostgresMigrations(
|
|
|
202
202
|
fresh.push(identity)
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
|
+
if (plan.application !== undefined) await checkDatabaseCode(db, plan, manifests)
|
|
205
206
|
if (plan.application?.runtimeRole !== undefined) {
|
|
206
207
|
await grantRuntimeRole(db, plan.application.schema, plan.ledgerTable, plan.application.runtimeRole)
|
|
207
208
|
}
|
|
@@ -223,6 +224,32 @@ export async function applyPostgresMigrations(
|
|
|
223
224
|
return fresh
|
|
224
225
|
}
|
|
225
226
|
|
|
227
|
+
/**
|
|
228
|
+
* The schema against every migration's declarations, on every run and not only after a file: an object created outside a
|
|
229
|
+
* migration (a hand-run statement, code that created one at runtime) fails the run, so the init command stops activation.
|
|
230
|
+
*/
|
|
231
|
+
async function checkDatabaseCode(
|
|
232
|
+
db: SqlDatabase,
|
|
233
|
+
plan: PostgresMigrationPlan,
|
|
234
|
+
manifests: ReadonlyMap<string, readonly DatabaseObject[]>,
|
|
235
|
+
): Promise<void> {
|
|
236
|
+
const application = plan.application
|
|
237
|
+
if (application === undefined) return
|
|
238
|
+
const last = plan.bundles.flatMap((bundle) => bundle.migrations.map((migration) => postgresMigrationIdentity(bundle.name, migration.name))).at(-1)
|
|
239
|
+
try {
|
|
240
|
+
await db.batch(databaseCodeStatements(db, application, last === undefined ? [] : manifests.get(last) ?? []))
|
|
241
|
+
} catch (error) {
|
|
242
|
+
if (error instanceof Error && error.message.startsWith(DATABASE_CODE_FAILURE)) {
|
|
243
|
+
throw new PostgresMigrationError(
|
|
244
|
+
`schema ${application.schema} holds database code that escapes it or that no migration declares: ${
|
|
245
|
+
error.message.slice(DATABASE_CODE_FAILURE.length)
|
|
246
|
+
}; declare it in a migration or drop it in a new one`,
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
throw new PostgresMigrationError('the database code check failed', { cause: error })
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
226
253
|
function validateBundle(bundle: PostgresMigrationBundle): void {
|
|
227
254
|
validateBundleName(bundle.name)
|
|
228
255
|
if (bundle.migrations.length === 0) throw new PostgresMigrationError(`Postgres migration bundle ${bundle.name} is empty`)
|