masterrecord 1.2.8 → 1.2.9

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,17 @@
1
1
  # MasterRecord Changelog
2
2
 
3
+ ## v1.2.9 — alterColumn type changes (SQLite rebuild + affinity-aware no-op)
4
+
5
+ **Bug:** `alterColumn` on SQLite emitted invalid SQL (`near ")"`). SQLite has no `ALTER`/`MODIFY COLUMN`, and `schema.alterColumn`'s builder path was handed an empty/missing table schema, producing `CREATE TABLE x ()`. Additionally, `string→text` is a no-op on SQLite (both TEXT affinity) yet it still attempted a rebuild.
6
+
7
+ **Fix:**
8
+ - **SQLite** `alterColumn` now reconciles the table to its entity definition via `syncTable`'s proven rebuild (rename → recreate with the new schema → copy common columns → drop old). It is a **no-op when the type is unchanged at SQLite's affinity level** (`string→text`, `int→bigint`), and performs a **data-preserving rebuild on a real change** (e.g. `integer→text`).
9
+ - **`needRebuildSQLite`** now compares the **resolved SQLite affinity** of the desired vs existing column type, catching *any* real type change. The previous check hardcoded only boolean/string/integer and missed most.
10
+ - **MySQL** (`MODIFY COLUMN`) and **Postgres** (`ALTER COLUMN … TYPE`) keep their native type-change DDL — verified to emit correct SQL.
11
+ - `alterColumn` now `await this._ensureReady()` (parity with the other schema methods) and throws a clear error if the entity schema can't be resolved on SQLite (instead of emitting broken DDL).
12
+
13
+ New test: `test/alter-column-type.test.js` — SQLite `string→text` no-op (data intact), SQLite `integer→text` rebuild (column becomes TEXT, rows preserved), and MySQL/Postgres SQL output. Verified executing on SQLite; MySQL/Postgres via SQL-string assertions (no live servers here). Full suite 613/613, 0 lint errors.
14
+
3
15
  ## v1.2.8 — batched-insert auto-increment PK handling
4
16
 
5
17
  **Bug:** the batched-insert path could fail with `Type mismatch for <Entity>.id: Expected integer, got function` for an unset auto-increment primary key, then fall back to individual inserts (which handled it). The INSERT builder (`_buildSQLInsertObjectParameterized`, shared by single + batch inserts) only skipped the auto-PK when its value was `undefined`/`null`. When the unset PK surfaced as its **schema-definition function** (`id(db){…}`, e.g. when the row is a class instance rather than a `.new()` data instance), the builder fell through to the type validator and threw.
@@ -67,6 +67,14 @@ class migrationSQLiteQuery {
67
67
  return `${colName} ${type}${nullName}${defaultClause}${unique}${primaryKey}${auto}`;
68
68
  }
69
69
 
70
+ // Public accessor for the resolved SQLite column type (affinity) of a
71
+ // masterrecord type. Used by schema.syncTable to decide whether a column's
72
+ // type actually changes at SQLite's affinity level — e.g. string and text
73
+ // both resolve to TEXT, so string -> text is a no-op (no rebuild needed).
74
+ resolveColumnType(type){
75
+ return this.#typeManager(type);
76
+ }
77
+
70
78
  #typeManager(type){
71
79
  switch(type) {
72
80
  case "string":
@@ -454,6 +454,7 @@ class schema{
454
454
 
455
455
  const needRebuildSQLite = () => {
456
456
  if(!this.context.isSQLite) return false;
457
+ const sqliteBuilder = new sqliteQuery();
457
458
  const byName = {};
458
459
  for(const row of existing){ byName[row.name] = row; }
459
460
  for(const d of desiredCols){
@@ -461,8 +462,6 @@ class schema{
461
462
  if(!row) continue;
462
463
  const notnull = row.notnull === 1;
463
464
  const desiredNotNull = d.col.nullable === false;
464
- const desiredType = d.col.type;
465
- const existingType = (row.type || '').toLowerCase();
466
465
  // compare default (normalize quotes)
467
466
  const exDefRaw = row.dflt_value == null ? null : String(row.dflt_value);
468
467
  let exDef = exDefRaw;
@@ -472,10 +471,16 @@ class schema{
472
471
  const dsDef = d.col.default == null ? null : String(d.col.default);
473
472
  if(desiredNotNull !== notnull) return true;
474
473
  if(exDef !== dsDef) return true;
475
- // rough type differences that require rebuild
476
- if((desiredType === 'boolean' && existingType !== 'integer') ||
477
- (desiredType === 'string' && existingType !== 'text') ||
478
- (desiredType === 'integer' && existingType !== 'integer')){
474
+ // Type change at SQLite's AFFINITY level. Compare the resolved
475
+ // SQLite type of the desired column to the existing column's
476
+ // declared type. This catches ANY real change (integer->text,
477
+ // float->text, …) — the previous hardcoded boolean/string/
478
+ // integer checks missed most — while treating same-affinity
479
+ // changes (string->text, both TEXT; int->bigint, both INTEGER)
480
+ // as a no-op so we don't rebuild unnecessarily.
481
+ const desiredResolved = sqliteBuilder.resolveColumnType(d.col.type).toUpperCase();
482
+ const existingResolved = String(row.type || '').toUpperCase();
483
+ if(existingResolved && desiredResolved !== existingResolved){
479
484
  return true;
480
485
  }
481
486
  }
@@ -582,16 +587,28 @@ class schema{
582
587
 
583
588
  //"dbo.People", "Location"
584
589
  async alterColumn(table){
590
+ await this._ensureReady();
585
591
  if(table){
586
592
  if(this.fullTable){
587
593
  if(this.context.isSQLite){
588
- var queryBuilder = new sqliteQuery();
589
- const tableSchema = (this._tableObj && this._tableObj[table.tableName]) || this.fullTable.new;
590
- const queryObj = queryBuilder.alterColumn(tableSchema, table);
591
- for (const key in queryObj) {
592
- var query = queryObj[key];
593
- await this.context._execute(query);
594
+ // SQLite has no ALTER/MODIFY COLUMN — a type change needs a
595
+ // full table rebuild (rename → recreate with new schema →
596
+ // copy common columns → drop old). Reconcile the table to
597
+ // its entity definition via syncTable, which performs that
598
+ // rebuild and is a no-op when the type is unchanged at
599
+ // SQLite's affinity level (e.g. string -> text, both TEXT).
600
+ // (The previous builder.alterColumn path emitted invalid
601
+ // SQL — "near )" — when handed an empty/missing schema.)
602
+ const entity = (this.context.__entities || []).find(e => e && e.__name === table.tableName)
603
+ || (this._tableObj && this._tableObj[table.tableName])
604
+ || (this.fullTable && this.fullTable[table.tableName]);
605
+ if (!entity || !entity.__name) {
606
+ throw new Error(
607
+ `masterrecord: alterColumn could not resolve the full schema for table '${table.tableName}' ` +
608
+ `to rebuild it on SQLite. Ensure the context registers this entity (dbset).`
609
+ );
594
610
  }
611
+ await this.syncTable(entity);
595
612
  }
596
613
 
597
614
  if(this.context.isMySQL){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masterrecord",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
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",