masterrecord 1.1.1 → 1.1.2
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 +20 -0
- package/Migrations/migrationMySQLQuery.js +14 -20
- package/Migrations/migrationSQLiteQuery.js +17 -11
- package/context.js +48 -0
- package/docs/belongsTo-relationships.md +29 -0
- package/package.json +1 -1
- package/test/belongs-to-fk-type.test.js +253 -0
package/CHANGES.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# MasterRecord Changelog
|
|
2
2
|
|
|
3
|
+
## v1.1.2 — belongsTo FK type resolution + MySQL/SQLite type-map gaps
|
|
4
|
+
|
|
5
|
+
**Bug:** `belongsTo()` hardcoded the FK column type to `integer` at entity-definition time, before the parent entity was registered. If the parent's PK was `string` / `bigint` / `uuid`, the FK was still emitted as `INTEGER`. SQLite accepted the mismatch silently (dynamic typing); Postgres and MySQL rejected inserts of string IDs into integer columns.
|
|
6
|
+
|
|
7
|
+
**Fix:** after every `dbset()` call, walk every registered entity and re-resolve each `belongsTo` column's `type` from its parent's primary-key type. Order-independent (works whether parent or child is registered first), idempotent (running multiple times produces the same result), case-insensitive on the `foreignTable` lookup.
|
|
8
|
+
|
|
9
|
+
| Parent PK type | SQLite FK | MySQL FK | Postgres FK |
|
|
10
|
+
| -------------- | --------- | ------------ | ----------- |
|
|
11
|
+
| `string` | TEXT | VARCHAR(255) | VARCHAR(255) |
|
|
12
|
+
| `integer` | INTEGER | INTEGER | INTEGER |
|
|
13
|
+
| `bigint` | INTEGER | BIGINT | BIGINT |
|
|
14
|
+
| `uuid` | TEXT | VARCHAR(36) | UUID |
|
|
15
|
+
|
|
16
|
+
**Related cleanup in the migration type maps:**
|
|
17
|
+
|
|
18
|
+
- **MySQL** `typeManager` now handles `bigint`, `uuid`, `int`, `jsonb`, and falls back to `TEXT` for unknown types. Previously these emitted literal `undefined` into the DDL string (e.g. `` `run_id` undefined NOT NULL ``).
|
|
19
|
+
- **SQLite** `typeManager` now handles `bigint`, `uuid`, `int`, `float`, `decimal`, `binary`/`blob`, `json`/`jsonb`, `date`/`datetime`/`timestamp` explicitly (collapsing to the appropriate SQLite affinity). Same `TEXT` fallback as before.
|
|
20
|
+
|
|
21
|
+
17 new tests cover the resolver across all four PK types, both `dbset()` orders, and every engine's emitted DDL.
|
|
22
|
+
|
|
3
23
|
## v1.1.1 — Documentation
|
|
4
24
|
|
|
5
25
|
- Documented the v1.1.0 full-text search feature: new [`docs/FULL_TEXT_SEARCH.md`](docs/FULL_TEXT_SEARCH.md), README section, and `.search()` entry in [`docs/METHODS_REFERENCE.md`](docs/METHODS_REFERENCE.md).
|
|
@@ -89,52 +89,46 @@ class migrationMySQLQuery {
|
|
|
89
89
|
switch(type) {
|
|
90
90
|
case "string":
|
|
91
91
|
return "VARCHAR(255)"
|
|
92
|
-
break;
|
|
93
92
|
case "text":
|
|
94
93
|
return "TEXT"
|
|
95
|
-
break;
|
|
96
94
|
case "mediumtext":
|
|
97
95
|
return "MEDIUMTEXT"
|
|
98
|
-
break;
|
|
99
96
|
case "longtext":
|
|
100
97
|
return "LONGTEXT"
|
|
101
|
-
break;
|
|
102
98
|
case "float":
|
|
103
|
-
return "
|
|
104
|
-
break;
|
|
99
|
+
return "FLOAT(24)"
|
|
105
100
|
case "decimal":
|
|
106
101
|
return "DECIMAL"
|
|
107
|
-
break;
|
|
108
102
|
case "datetime":
|
|
109
103
|
return "DATETIME"
|
|
110
|
-
break;
|
|
111
104
|
case "timestamp":
|
|
112
105
|
return "TIMESTAMP"
|
|
113
|
-
break;
|
|
114
106
|
case "date":
|
|
115
107
|
return "DATE"
|
|
116
|
-
break;
|
|
117
108
|
case "time":
|
|
118
109
|
return "TIME"
|
|
119
|
-
break;
|
|
120
110
|
case "boolean":
|
|
121
111
|
return "TINYINT"
|
|
122
|
-
break;
|
|
123
112
|
case "integer":
|
|
113
|
+
case "int":
|
|
124
114
|
return "INTEGER"
|
|
125
|
-
|
|
115
|
+
case "bigint":
|
|
116
|
+
return "BIGINT"
|
|
117
|
+
case "uuid":
|
|
118
|
+
// MySQL has no native UUID type; store as fixed-length string.
|
|
119
|
+
return "VARCHAR(36)"
|
|
126
120
|
case "binary":
|
|
127
|
-
return "BLOB"
|
|
128
|
-
break;
|
|
129
121
|
case "blob":
|
|
130
122
|
return "BLOB"
|
|
131
|
-
break;
|
|
132
123
|
case "json":
|
|
124
|
+
case "jsonb":
|
|
133
125
|
return "JSON"
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
126
|
+
default:
|
|
127
|
+
// Fallback so we never emit `undefined` for an unknown type
|
|
128
|
+
// (matches SQLite's and Postgres's lenient behavior).
|
|
129
|
+
return "TEXT"
|
|
130
|
+
}
|
|
131
|
+
}
|
|
138
132
|
|
|
139
133
|
// table is the altered field
|
|
140
134
|
alterColumn( table){
|
|
@@ -70,26 +70,32 @@ class migrationSQLiteQuery {
|
|
|
70
70
|
#typeManager(type){
|
|
71
71
|
switch(type) {
|
|
72
72
|
case "string":
|
|
73
|
-
return "TEXT"
|
|
74
|
-
break;
|
|
75
73
|
case "text":
|
|
76
74
|
case "mediumtext":
|
|
77
75
|
case "longtext":
|
|
78
|
-
|
|
79
|
-
//
|
|
80
|
-
//
|
|
81
|
-
// SQL — accepted by SQLite's lax type affinity but still wrong.)
|
|
76
|
+
case "uuid":
|
|
77
|
+
// SQLite TEXT is unlimited; MEDIUMTEXT/LONGTEXT/UUID collapse to
|
|
78
|
+
// TEXT for cross-engine portability.
|
|
82
79
|
return "TEXT"
|
|
83
|
-
break;
|
|
84
80
|
case "time":
|
|
81
|
+
case "date":
|
|
82
|
+
case "datetime":
|
|
83
|
+
case "timestamp":
|
|
85
84
|
return "TEXT"
|
|
86
|
-
break;
|
|
87
85
|
case "boolean":
|
|
88
|
-
return "INTEGER"
|
|
89
|
-
break;
|
|
90
86
|
case "integer":
|
|
87
|
+
case "int":
|
|
88
|
+
case "bigint":
|
|
91
89
|
return "INTEGER"
|
|
92
|
-
|
|
90
|
+
case "float":
|
|
91
|
+
case "decimal":
|
|
92
|
+
return "REAL"
|
|
93
|
+
case "binary":
|
|
94
|
+
case "blob":
|
|
95
|
+
return "BLOB"
|
|
96
|
+
case "json":
|
|
97
|
+
case "jsonb":
|
|
98
|
+
return "TEXT"
|
|
93
99
|
default:
|
|
94
100
|
// Fallback for unknown types so we never emit `undefined`.
|
|
95
101
|
return "TEXT"
|
package/context.js
CHANGED
|
@@ -1299,12 +1299,60 @@ class context {
|
|
|
1299
1299
|
enumerable: true
|
|
1300
1300
|
});
|
|
1301
1301
|
|
|
1302
|
+
// belongsTo() hardcodes type='integer' before the parent entity is known.
|
|
1303
|
+
// Now that another entity is registered, re-resolve every belongsTo FK
|
|
1304
|
+
// column's type from its parent's primary-key type. Idempotent.
|
|
1305
|
+
this.#resolveBelongsToTypes();
|
|
1306
|
+
|
|
1302
1307
|
// Return chainable object with seed() method
|
|
1303
1308
|
return {
|
|
1304
1309
|
seed: (data) => this.#addSeedData(tableName, data)
|
|
1305
1310
|
};
|
|
1306
1311
|
}
|
|
1307
1312
|
|
|
1313
|
+
// Walk every registered entity. For each column with relationshipType ===
|
|
1314
|
+
// 'belongsTo', look up the parent entity by foreignTable name and copy the
|
|
1315
|
+
// parent's primary-key type onto the FK column. Without this, FKs to a
|
|
1316
|
+
// string/uuid/bigint PK end up declared INTEGER, which SQLite tolerates
|
|
1317
|
+
// (dynamic typing) but Postgres and MySQL reject.
|
|
1318
|
+
#resolveBelongsToTypes() {
|
|
1319
|
+
const entities = this.__entities;
|
|
1320
|
+
if (!entities || entities.length === 0) return;
|
|
1321
|
+
|
|
1322
|
+
// Case-insensitive lookup table — users may write
|
|
1323
|
+
// db.belongsTo('run') while the class is `Run`.
|
|
1324
|
+
const byName = {};
|
|
1325
|
+
for (const e of entities) {
|
|
1326
|
+
if (e && e.__name) {
|
|
1327
|
+
byName[e.__name.toLowerCase()] = e;
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
for (const entity of entities) {
|
|
1332
|
+
for (const key of Object.keys(entity)) {
|
|
1333
|
+
const col = entity[key];
|
|
1334
|
+
if (!col || typeof col !== 'object') continue;
|
|
1335
|
+
if (col.relationshipType !== 'belongsTo') continue;
|
|
1336
|
+
if (!col.foreignTable) continue;
|
|
1337
|
+
|
|
1338
|
+
const parent = byName[String(col.foreignTable).toLowerCase()];
|
|
1339
|
+
if (!parent) continue;
|
|
1340
|
+
|
|
1341
|
+
// Find the parent's primary-key column.
|
|
1342
|
+
for (const pKey of Object.keys(parent)) {
|
|
1343
|
+
const pCol = parent[pKey];
|
|
1344
|
+
if (!pCol || typeof pCol !== 'object') continue;
|
|
1345
|
+
if (pCol.primary === true && pCol.type) {
|
|
1346
|
+
if (col.type !== pCol.type) {
|
|
1347
|
+
col.type = pCol.type;
|
|
1348
|
+
}
|
|
1349
|
+
break;
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1308
1356
|
/**
|
|
1309
1357
|
* Define a composite index on an entity (Option C - Context-level)
|
|
1310
1358
|
* @param {Function|string} model - Entity class or table name
|
|
@@ -172,6 +172,35 @@ Even though `chat_id` is never explicitly defined as a field, it's accessible be
|
|
|
172
172
|
2. `entityTrackerModel.build()` creates properties for all columns from the result
|
|
173
173
|
3. The `belongsTo` relationship doesn't prevent the foreign key from being accessible
|
|
174
174
|
|
|
175
|
+
## Foreign Key Column Type
|
|
176
|
+
|
|
177
|
+
> Behavior since **v1.1.2**.
|
|
178
|
+
|
|
179
|
+
The foreign-key column's SQL type is **automatically resolved from the parent entity's primary-key type**. You do not declare a type on the FK; it inherits from the parent.
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
class Run {
|
|
183
|
+
id(db) { db.string().primary(); } // PK is VARCHAR(255)
|
|
184
|
+
name(db) { db.string(); }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
class Step {
|
|
188
|
+
id(db) { db.integer().primary().auto(); }
|
|
189
|
+
Run(db) { db.belongsTo('Run').notNullable(); } // run_id is VARCHAR(255), not INTEGER
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
This matters across engines:
|
|
194
|
+
|
|
195
|
+
| Parent PK type | SQLite `run_id` | MySQL `run_id` | Postgres `run_id` |
|
|
196
|
+
| -------------- | --------------- | ----------------- | ----------------- |
|
|
197
|
+
| `string` | TEXT | VARCHAR(255) | VARCHAR(255) |
|
|
198
|
+
| `integer` | INTEGER | INTEGER | INTEGER |
|
|
199
|
+
| `bigint` | INTEGER | BIGINT | BIGINT |
|
|
200
|
+
| `uuid` | TEXT | VARCHAR(36) | UUID |
|
|
201
|
+
|
|
202
|
+
Before v1.1.2, every belongsTo FK was declared INTEGER regardless of the parent's PK type. SQLite tolerated the mismatch (dynamic typing), but Postgres and MySQL rejected inserts of string IDs into INTEGER columns. The resolver is order-independent: register child and parent entities in either order via `dbset()`.
|
|
203
|
+
|
|
175
204
|
## Summary
|
|
176
205
|
|
|
177
206
|
| Operation | Property to Use | Example |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
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",
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* belongsTo FK type resolution (1.1.2).
|
|
3
|
+
*
|
|
4
|
+
* Bug: belongsTo() hardcoded type='integer' on the FK column at entity-
|
|
5
|
+
* definition time, before the parent entity was known. If the parent's PK
|
|
6
|
+
* was string/bigint/uuid, the FK column was still declared INTEGER. SQLite
|
|
7
|
+
* accepted the mismatch (dynamic typing); Postgres and MySQL rejected it.
|
|
8
|
+
*
|
|
9
|
+
* Fix: after each dbset() call, walk every entity and re-resolve every
|
|
10
|
+
* belongsTo column's `type` from its parent's primary-key type. Order-
|
|
11
|
+
* independent, idempotent.
|
|
12
|
+
*
|
|
13
|
+
* Notes for future maintenance:
|
|
14
|
+
* - EntityModelBuilder only sees own-prototype methods. Test entity classes
|
|
15
|
+
* must declare their fields directly (no `extends`) or the build silently
|
|
16
|
+
* produces an empty entity with no PK and the resolver has nothing to copy.
|
|
17
|
+
* - bigint/uuid go through EntityModel.type('bigint') because EntityModel
|
|
18
|
+
* doesn't have dedicated bigint()/uuid() field-setter methods.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { test } from 'node:test';
|
|
22
|
+
import assert from 'node:assert/strict';
|
|
23
|
+
import fs from 'node:fs';
|
|
24
|
+
import path from 'node:path';
|
|
25
|
+
import { fileURLToPath } from 'node:url';
|
|
26
|
+
|
|
27
|
+
process.env.master = 'fktypes';
|
|
28
|
+
|
|
29
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
30
|
+
const fixturesDir = path.join(__dirname, 'fixtures', 'belongs-to-fk-type');
|
|
31
|
+
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
32
|
+
const dbDir = path.join(fixturesDir, 'db');
|
|
33
|
+
fs.mkdirSync(envDir, { recursive: true });
|
|
34
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
35
|
+
|
|
36
|
+
fs.writeFileSync(
|
|
37
|
+
path.join(envDir, 'env.fktypes.json'),
|
|
38
|
+
JSON.stringify({
|
|
39
|
+
StringPkCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
40
|
+
IntPkCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
41
|
+
OutOfOrderCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
42
|
+
BigintPkCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
43
|
+
UuidPkCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
44
|
+
})
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const { default: context } = await import('../context.js');
|
|
48
|
+
const { default: SQLiteQuery } = await import('../Migrations/migrationSQLiteQuery.js');
|
|
49
|
+
const { default: MySQLQuery } = await import('../Migrations/migrationMySQLQuery.js');
|
|
50
|
+
const { default: PostgresQuery } = await import('../Migrations/migrationPostgresQuery.js');
|
|
51
|
+
|
|
52
|
+
// =====================================================================
|
|
53
|
+
// Resolver behavior (per parent PK type)
|
|
54
|
+
// =====================================================================
|
|
55
|
+
|
|
56
|
+
test('belongsTo FK type resolves from string PK on parent', () => {
|
|
57
|
+
class Run {
|
|
58
|
+
id(db) { db.string().primary(); }
|
|
59
|
+
name(db) { db.string(); }
|
|
60
|
+
}
|
|
61
|
+
class Step {
|
|
62
|
+
id(db) { db.integer().primary().auto(); }
|
|
63
|
+
name(db) { db.string(); }
|
|
64
|
+
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
65
|
+
}
|
|
66
|
+
class StringPkCtx extends context {
|
|
67
|
+
constructor() {
|
|
68
|
+
super();
|
|
69
|
+
this.env(envDir);
|
|
70
|
+
this.dbset(Run);
|
|
71
|
+
this.dbset(Step);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const ctx = new StringPkCtx();
|
|
75
|
+
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
76
|
+
const fk = stepEntity.Run;
|
|
77
|
+
assert.equal(fk.relationshipType, 'belongsTo');
|
|
78
|
+
assert.equal(fk.foreignKey, 'run_id');
|
|
79
|
+
assert.equal(fk.type, 'string', 'FK type should match parent string PK');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('belongsTo FK type stays integer when parent PK is integer', () => {
|
|
83
|
+
class Run {
|
|
84
|
+
id(db) { db.integer().primary().auto(); }
|
|
85
|
+
name(db) { db.string(); }
|
|
86
|
+
}
|
|
87
|
+
class Step {
|
|
88
|
+
id(db) { db.integer().primary().auto(); }
|
|
89
|
+
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
90
|
+
}
|
|
91
|
+
class IntPkCtx extends context {
|
|
92
|
+
constructor() {
|
|
93
|
+
super();
|
|
94
|
+
this.env(envDir);
|
|
95
|
+
this.dbset(Run);
|
|
96
|
+
this.dbset(Step);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const ctx = new IntPkCtx();
|
|
100
|
+
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
101
|
+
assert.equal(stepEntity.Run.type, 'integer');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('belongsTo FK type resolves regardless of dbset() order', () => {
|
|
105
|
+
class Run {
|
|
106
|
+
id(db) { db.string().primary(); }
|
|
107
|
+
name(db) { db.string(); }
|
|
108
|
+
}
|
|
109
|
+
class Step {
|
|
110
|
+
id(db) { db.integer().primary().auto(); }
|
|
111
|
+
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
112
|
+
}
|
|
113
|
+
class OutOfOrderCtx extends context {
|
|
114
|
+
constructor() {
|
|
115
|
+
super();
|
|
116
|
+
this.env(envDir);
|
|
117
|
+
this.dbset(Step); // child first
|
|
118
|
+
this.dbset(Run); // parent second
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const ctx = new OutOfOrderCtx();
|
|
122
|
+
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
123
|
+
assert.equal(stepEntity.Run.type, 'string', 'resolution must be order-independent');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('belongsTo FK type resolves for bigint parent PK', () => {
|
|
127
|
+
class Run {
|
|
128
|
+
id(db) { db.type('bigint').primary().auto(); }
|
|
129
|
+
name(db) { db.string(); }
|
|
130
|
+
}
|
|
131
|
+
class Step {
|
|
132
|
+
id(db) { db.integer().primary().auto(); }
|
|
133
|
+
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
134
|
+
}
|
|
135
|
+
class BigintPkCtx extends context {
|
|
136
|
+
constructor() {
|
|
137
|
+
super();
|
|
138
|
+
this.env(envDir);
|
|
139
|
+
this.dbset(Run);
|
|
140
|
+
this.dbset(Step);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const ctx = new BigintPkCtx();
|
|
144
|
+
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
145
|
+
assert.equal(stepEntity.Run.type, 'bigint');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('belongsTo FK type resolves for uuid parent PK', () => {
|
|
149
|
+
class Run {
|
|
150
|
+
id(db) { db.type('uuid').primary(); }
|
|
151
|
+
name(db) { db.string(); }
|
|
152
|
+
}
|
|
153
|
+
class Step {
|
|
154
|
+
id(db) { db.integer().primary().auto(); }
|
|
155
|
+
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
156
|
+
}
|
|
157
|
+
class UuidPkCtx extends context {
|
|
158
|
+
constructor() {
|
|
159
|
+
super();
|
|
160
|
+
this.env(envDir);
|
|
161
|
+
this.dbset(Run);
|
|
162
|
+
this.dbset(Step);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const ctx = new UuidPkCtx();
|
|
166
|
+
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
167
|
+
assert.equal(stepEntity.Run.type, 'uuid');
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// =====================================================================
|
|
171
|
+
// Migration DDL output — verify each engine emits a matching SQL type.
|
|
172
|
+
// Bypasses context; feeds a minimal table-like object straight to the
|
|
173
|
+
// migration query builders. Shape mirrors what the resolver produces.
|
|
174
|
+
// =====================================================================
|
|
175
|
+
|
|
176
|
+
function stepTableWith(fkType) {
|
|
177
|
+
return {
|
|
178
|
+
__name: 'Step',
|
|
179
|
+
id: { name: 'id', type: 'integer', primary: true, auto: true },
|
|
180
|
+
name: { name: 'name', type: 'string' },
|
|
181
|
+
Run: {
|
|
182
|
+
name: 'Run',
|
|
183
|
+
type: fkType,
|
|
184
|
+
relationshipType: 'belongsTo',
|
|
185
|
+
foreignTable: 'Run',
|
|
186
|
+
foreignKey: 'run_id',
|
|
187
|
+
nullable: false,
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
test('Postgres createTable emits VARCHAR(255) for FK to string PK', () => {
|
|
193
|
+
const sql = new PostgresQuery().createTable(stepTableWith('string'));
|
|
194
|
+
assert.match(sql, /"run_id"\s+VARCHAR\(255\)/, sql);
|
|
195
|
+
assert.doesNotMatch(sql, /"run_id"\s+INTEGER/);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test('Postgres createTable emits INTEGER for FK to integer PK', () => {
|
|
199
|
+
const sql = new PostgresQuery().createTable(stepTableWith('integer'));
|
|
200
|
+
assert.match(sql, /"run_id"\s+INTEGER/, sql);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test('Postgres createTable emits BIGINT for FK to bigint PK', () => {
|
|
204
|
+
const sql = new PostgresQuery().createTable(stepTableWith('bigint'));
|
|
205
|
+
assert.match(sql, /"run_id"\s+BIGINT/, sql);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test('Postgres createTable emits UUID for FK to uuid PK', () => {
|
|
209
|
+
const sql = new PostgresQuery().createTable(stepTableWith('uuid'));
|
|
210
|
+
assert.match(sql, /"run_id"\s+UUID/, sql);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test('MySQL createTable emits VARCHAR(255) for FK to string PK', () => {
|
|
214
|
+
const sql = new MySQLQuery().createTable(stepTableWith('string'));
|
|
215
|
+
assert.match(sql, /`run_id`\s+VARCHAR\(255\)/, sql);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('MySQL createTable emits BIGINT for FK to bigint PK', () => {
|
|
219
|
+
const sql = new MySQLQuery().createTable(stepTableWith('bigint'));
|
|
220
|
+
assert.match(sql, /`run_id`\s+BIGINT/, sql);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('MySQL createTable emits VARCHAR(36) for FK to uuid PK', () => {
|
|
224
|
+
const sql = new MySQLQuery().createTable(stepTableWith('uuid'));
|
|
225
|
+
assert.match(sql, /`run_id`\s+VARCHAR\(36\)/, sql);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test('MySQL createTable never emits "undefined" for any FK type', () => {
|
|
229
|
+
for (const t of ['string', 'integer', 'bigint', 'uuid', 'text']) {
|
|
230
|
+
const sql = new MySQLQuery().createTable(stepTableWith(t));
|
|
231
|
+
assert.doesNotMatch(sql, /undefined/, `MySQL ${t}: ${sql}`);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test('SQLite createTable emits TEXT for FK to string PK', () => {
|
|
236
|
+
const sql = new SQLiteQuery().createTable(stepTableWith('string'));
|
|
237
|
+
assert.match(sql, /\[?run_id\]?\s+TEXT/i, sql);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test('SQLite createTable emits INTEGER for FK to integer PK', () => {
|
|
241
|
+
const sql = new SQLiteQuery().createTable(stepTableWith('integer'));
|
|
242
|
+
assert.match(sql, /\[?run_id\]?\s+INTEGER/i, sql);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('SQLite createTable emits INTEGER for FK to bigint PK', () => {
|
|
246
|
+
const sql = new SQLiteQuery().createTable(stepTableWith('bigint'));
|
|
247
|
+
assert.match(sql, /\[?run_id\]?\s+INTEGER/i, sql);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test('SQLite createTable emits TEXT for FK to uuid PK', () => {
|
|
251
|
+
const sql = new SQLiteQuery().createTable(stepTableWith('uuid'));
|
|
252
|
+
assert.match(sql, /\[?run_id\]?\s+TEXT/i, sql);
|
|
253
|
+
});
|