masterrecord 1.2.5 → 1.2.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/CHANGES.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # MasterRecord Changelog
2
2
 
3
+ ## v1.2.6 — idempotent addColumn (skip-if-exists)
4
+
5
+ `schema.addColumn` now **skips if the column already exists**, symmetric with `dropColumn`'s skip-if-gone and `createTable`'s `IF NOT EXISTS`. None of SQLite/MySQL support `ADD COLUMN IF NOT EXISTS`, so it probes the live schema via `getTableInfo` (which throws on a real introspection error — so a genuine failure still aborts loudly; only a clean "already present" result short-circuits, with a `[masterrecord:migration]` skip log).
6
+
7
+ Effect: re-running a migration, or applying one to a database that already has part of the schema, is now safe — the per-migration guards added in 1.2.3 become belt-and-suspenders rather than load-bearing. (belongsTo FK columns are matched by their `foreignKey` name.)
8
+
9
+ New test: idempotent-addColumn case in `test/migration-self-contained.test.js`. Suite green; 0 lint errors.
10
+
3
11
  ## v1.2.5 — documentation catch-up
4
12
 
5
13
  Docs-only. Brought `docs/MIGRATIONS_GUIDE.md` up to date with the 1.2.x work that was previously only in this changelog:
@@ -239,6 +239,27 @@ class schema{
239
239
  throw new Error('masterrecord: addColumn — no database dialect is active (context not initialized).');
240
240
  }
241
241
 
242
+ // Idempotency: skip if the column already exists — symmetric with
243
+ // dropColumn's skip-if-gone and createTable's IF NOT EXISTS. None of
244
+ // SQLite/MySQL support `ADD COLUMN IF NOT EXISTS`, so probe the live
245
+ // schema instead. getTableInfo throws on a real introspection error
246
+ // (1.2.0), so a genuine failure still aborts loudly; only a clean
247
+ // "column already present" result short-circuits. This makes
248
+ // re-running a migration safe and reduces the per-migration guards to
249
+ // belt-and-suspenders.
250
+ const _engine = this.context._SQLEngine;
251
+ if (_engine && typeof _engine.getTableInfo === 'function') {
252
+ const dbColumn = (table.relationshipType === 'belongsTo' && table.foreignKey) ? table.foreignKey : table.name;
253
+ const existingCols = await _engine.getTableInfo(table.tableName);
254
+ const alreadyExists = Array.isArray(existingCols) && existingCols.some(c => c && c.name === dbColumn);
255
+ if (alreadyExists) {
256
+ if (process.env.MR_SILENT_MIGRATIONS !== 'true') {
257
+ console.log(`[masterrecord:migration] addColumn skipped — ${table.tableName}.${dbColumn} already exists`);
258
+ }
259
+ return;
260
+ }
261
+ }
262
+
242
263
  if(this.context.isSQLite){
243
264
  var queryBuilder = new sqliteQuery();
244
265
  var query = queryBuilder.addColum(table);
@@ -304,6 +304,7 @@ You can also declare these on the entity/context (`this.compositeIndex(Model, ['
304
304
  Migrations are designed to fail loudly and be observable — there are no silent no-ops:
305
305
 
306
306
  - **Generated migrations are self-contained.** `add-migration` bakes the full column spec into each `addColumn`/`dropColumn` (`await this.addColumn({ tableName, name, type, … })`), so a migration replays deterministically on every database regardless of how far the committed snapshot has advanced.
307
+ - **Idempotent column ops.** `addColumn` skips if the column already exists (symmetric with `dropColumn`'s skip-if-gone and `createTable`'s `IF NOT EXISTS`), so re-running a migration — or applying one to a database that already has part of the schema — is safe. The existence check uses live introspection, which throws on a real error, so a genuine failure still aborts loudly.
307
308
  - **Loud failures.** `addColumn`/`dropColumn` throw on an incomplete operand; introspection (`tableExists`/`getTableInfo`) throws on a real error instead of masquerading as “table absent”; MySQL throws on partial-index `where`. A failed step aborts the migration rather than marking it applied.
308
309
  - **Observable DDL.** Every statement run by a migration is logged as `[masterrecord:migration] …`, **including in production**. Set `MR_SILENT_MIGRATIONS=true` to suppress it.
309
310
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masterrecord",
3
- "version": "1.2.5",
3
+ "version": "1.2.6",
4
4
  "description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
5
5
  "type": "module",
6
6
  "main": "./MasterRecord.js",