masterrecord 1.0.6 → 1.0.8
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/.claude/settings.local.json +4 -1
- package/Migrations/migrationMySQLQuery.js +8 -1
- package/Migrations/migrationPostgresQuery.js +5 -2
- package/Migrations/migrationSQLiteQuery.js +6 -5
- package/Migrations/schema.js +14 -4
- package/QueryLanguage/queryMethods.js +8 -0
- package/QueryLanguage/queryScript.js +18 -8
- package/SQLLiteEngine.js +17 -7
- package/context.js +34 -7
- package/mySQLEngine.js +23 -7
- package/package.json +1 -1
- package/postgresEngine.js +155 -35
- package/test/count-no-from-audit.test.js +1 -1
- package/test/ctx-dollar-placeholder.test.js +142 -0
- package/test/drop-column-idempotent.test.js +181 -0
- package/test/postgres-camelcase-quoting.test.js +249 -0
- package/test/query-builder-parity.test.js +2 -2
- package/test/fixtures/broad-audit/config/environments/env.audit.json +0 -1
- package/test/fixtures/broad-audit/db/auditctx.sqlite +0 -0
- package/test/fixtures/count-chained-where/config/environments/env.countchained.json +0 -1
- package/test/fixtures/count-chained-where/db/countctx.sqlite +0 -0
- package/test/fixtures/count-no-from/config/environments/env.countfrom.json +0 -1
- package/test/fixtures/count-no-from/db/countfromctx.sqlite +0 -0
- package/test/fixtures/edge-cases-audit/config/environments/env.edgeaudit.json +0 -1
- package/test/fixtures/edge-cases-audit/db/edgectx.sqlite +0 -0
- package/test/fixtures/multi-migration-N4bUYy/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +0 -14
- package/test/fixtures/multi-migration-N4bUYy/app/models/db/migrations/1700000002000_CreateBook_migration.js +0 -14
- package/test/fixtures/multi-migration-N4bUYy/app/models/db/migrations/1700000003000_CreateReview_migration.js +0 -14
- package/test/fixtures/multi-migration-N4bUYy/app/models/db/migrations/multimigctx_contextSnapShot.json +0 -96
- package/test/fixtures/multi-migration-N4bUYy/app/models/multimigctx.js +0 -26
- package/test/fixtures/multi-migration-N4bUYy/config/environments/env.multimig.json +0 -1
- package/test/fixtures/multi-migration-N4bUYy/db/multimigctx.sqlite +0 -0
- package/test/fixtures/multi-migration-N4bUYy/package.json +0 -1
- package/test/fixtures/multi-migration-Qu42ZE/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +0 -14
- package/test/fixtures/multi-migration-Qu42ZE/app/models/db/migrations/1700000002000_CreateBook_migration.js +0 -14
- package/test/fixtures/multi-migration-Qu42ZE/app/models/db/migrations/1700000003000_CreateReview_migration.js +0 -14
- package/test/fixtures/multi-migration-Qu42ZE/app/models/db/migrations/multimigctx_contextSnapShot.json +0 -96
- package/test/fixtures/multi-migration-Qu42ZE/app/models/multimigctx.js +0 -26
- package/test/fixtures/multi-migration-Qu42ZE/config/environments/env.multimig.json +0 -1
- package/test/fixtures/multi-migration-Qu42ZE/db/multimigctx.sqlite +0 -0
- package/test/fixtures/multi-migration-Qu42ZE/package.json +0 -1
- package/test/fixtures/multi-migration-xDfXbJ/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +0 -14
- package/test/fixtures/multi-migration-xDfXbJ/app/models/db/migrations/1700000002000_CreateBook_migration.js +0 -14
- package/test/fixtures/multi-migration-xDfXbJ/app/models/db/migrations/1700000003000_CreateReview_migration.js +0 -14
- package/test/fixtures/multi-migration-xDfXbJ/app/models/db/migrations/multimigctx_contextSnapShot.json +0 -104
- package/test/fixtures/multi-migration-xDfXbJ/app/models/multimigctx.js +0 -26
- package/test/fixtures/multi-migration-xDfXbJ/config/environments/env.multimig.json +0 -1
- package/test/fixtures/multi-migration-xDfXbJ/db/multimigctx.sqlite +0 -0
- package/test/fixtures/multi-migration-xDfXbJ/package.json +0 -1
- package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +0 -1
- package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
- package/test/fixtures/query-builder-parity/config/environments/env.parity.json +0 -1
- package/test/fixtures/query-builder-parity/db/parityctx.sqlite +0 -0
- package/test/fixtures/setter-and-update-drop/config/environments/env.setterdrop.json +0 -1
- package/test/fixtures/setter-and-update-drop/db/setdropctx.sqlite +0 -0
- package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +0 -1
- package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
- package/test/fixtures/text-types/config/environments/env.texttypes.json +0 -1
- package/test/fixtures/text-types/db/textctx.sqlite +0 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression for two related 1.0.8 fixes:
|
|
3
|
+
*
|
|
4
|
+
* 1. postgresEngine was missing getTableInfo() — schema.syncTable() couldn't
|
|
5
|
+
* diff against the existing schema, so re-running migrations failed with
|
|
6
|
+
* `column "X" already exists`.
|
|
7
|
+
*
|
|
8
|
+
* 2. DROP COLUMN DDL was not idempotent — Postgres/MySQL didn't emit
|
|
9
|
+
* `IF EXISTS` and SQLite had no runtime guard, so re-running a
|
|
10
|
+
* drop-column migration after a failed/partial run errored with
|
|
11
|
+
* `column "X" does not exist`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { test } from 'node:test';
|
|
15
|
+
import assert from 'node:assert/strict';
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
import { fileURLToPath } from 'node:url';
|
|
19
|
+
|
|
20
|
+
process.env.master = 'dropcol';
|
|
21
|
+
|
|
22
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
23
|
+
const fixturesDir = path.join(__dirname, 'fixtures', 'drop-col');
|
|
24
|
+
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
25
|
+
const dbDir = path.join(fixturesDir, 'db');
|
|
26
|
+
const dbFile = path.join(dbDir, 'dropcolctx.sqlite');
|
|
27
|
+
|
|
28
|
+
fs.mkdirSync(envDir, { recursive: true });
|
|
29
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
30
|
+
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
31
|
+
|
|
32
|
+
fs.writeFileSync(
|
|
33
|
+
path.join(envDir, 'env.dropcol.json'),
|
|
34
|
+
JSON.stringify({
|
|
35
|
+
DropColCtx: {
|
|
36
|
+
type: 'better-sqlite3',
|
|
37
|
+
connection: dbDir + path.sep,
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const { default: context } = await import('../context.js');
|
|
43
|
+
const { default: schema } = await import('../Migrations/schema.js');
|
|
44
|
+
const { default: PostgresEngine } = await import('../postgresEngine.js');
|
|
45
|
+
const { default: MySQLEngine } = await import('../mySQLEngine.js');
|
|
46
|
+
const { default: SQLiteQuery } = await import('../Migrations/migrationSQLiteQuery.js');
|
|
47
|
+
const { default: MySQLQuery } = await import('../Migrations/migrationMySQLQuery.js');
|
|
48
|
+
const { default: PostgresQuery } = await import('../Migrations/migrationPostgresQuery.js');
|
|
49
|
+
|
|
50
|
+
// ---- DDL string-level checks (no DB connection required) ----
|
|
51
|
+
|
|
52
|
+
test('Postgres dropColumn emits IF EXISTS with quoted identifiers', () => {
|
|
53
|
+
const q = new PostgresQuery();
|
|
54
|
+
const sql = q.dropColumn({ tableName: 'SchedulerLeader', name: 'staleField' });
|
|
55
|
+
assert.match(sql, /ALTER TABLE "SchedulerLeader" DROP COLUMN IF EXISTS "staleField"/);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('MySQL dropColumn emits IF EXISTS with backtick quotes', () => {
|
|
59
|
+
const q = new MySQLQuery();
|
|
60
|
+
const sql = q.dropColumn({ tableName: 'User', name: 'legacyField' });
|
|
61
|
+
assert.match(sql, /ALTER TABLE `User` DROP COLUMN IF EXISTS `legacyField`/);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('SQLite dropColumn quotes identifiers with square brackets', () => {
|
|
65
|
+
const q = new SQLiteQuery();
|
|
66
|
+
const sql = q.dropColumn({ tableName: 'User', name: 'legacyField' });
|
|
67
|
+
assert.match(sql, /ALTER TABLE \[User\] DROP COLUMN \[legacyField\]/);
|
|
68
|
+
// SQLite has no `IF EXISTS` on DROP COLUMN; the guard lives in schema.dropColumn().
|
|
69
|
+
assert.doesNotMatch(sql, /IF EXISTS/);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// ---- Postgres getTableInfo presence ----
|
|
73
|
+
|
|
74
|
+
test('Postgres engine exposes getTableInfo()', () => {
|
|
75
|
+
const engine = new PostgresEngine();
|
|
76
|
+
assert.equal(typeof engine.getTableInfo, 'function');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('Postgres getTableInfo returns rows shaped like SQLite/MySQL counterparts', async () => {
|
|
80
|
+
const engine = new PostgresEngine();
|
|
81
|
+
// Stub _runWithParams so we can verify the SQL without a live Postgres.
|
|
82
|
+
let capturedSql;
|
|
83
|
+
let capturedParams;
|
|
84
|
+
engine._runWithParams = async (sql, params) => {
|
|
85
|
+
capturedSql = sql;
|
|
86
|
+
capturedParams = params;
|
|
87
|
+
return {
|
|
88
|
+
rows: [
|
|
89
|
+
{ name: 'id', dflt_value: null, is_nullable: 'NO', data_type: 'integer' },
|
|
90
|
+
{ name: 'createdAt', dflt_value: null, is_nullable: 'YES', data_type: 'timestamp without time zone' },
|
|
91
|
+
],
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
const rows = await engine.getTableInfo('SchedulerLeader');
|
|
95
|
+
assert.match(capturedSql, /information_schema\.columns/);
|
|
96
|
+
assert.match(capturedSql, /table_name\s*=\s*\$1/);
|
|
97
|
+
assert.deepEqual(capturedParams, ['SchedulerLeader']);
|
|
98
|
+
assert.equal(rows.length, 2);
|
|
99
|
+
assert.equal(rows[0].name, 'id');
|
|
100
|
+
assert.equal(rows[1].name, 'createdAt');
|
|
101
|
+
// Field names mirror SQLite's PRAGMA table_info / MySQL's INFORMATION_SCHEMA query.
|
|
102
|
+
for (const key of ['name', 'dflt_value', 'is_nullable', 'data_type']) {
|
|
103
|
+
assert.ok(key in rows[0], `expected '${key}' in row`);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('Postgres getTableInfo returns [] on error', async () => {
|
|
108
|
+
const engine = new PostgresEngine();
|
|
109
|
+
engine._runWithParams = async () => { throw new Error('boom'); };
|
|
110
|
+
assert.deepEqual(await engine.getTableInfo('Whatever'), []);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// ---- SQLite end-to-end: dropColumn twice in a row must not throw ----
|
|
114
|
+
|
|
115
|
+
class Widget {
|
|
116
|
+
id(db) { db.integer().primary().auto(); }
|
|
117
|
+
name(db) { db.string(); }
|
|
118
|
+
legacyField(db) { db.string(); }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
class DropColCtx extends context {
|
|
122
|
+
constructor() {
|
|
123
|
+
super();
|
|
124
|
+
this.env(envDir);
|
|
125
|
+
this.dbset(Widget);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
{
|
|
130
|
+
// Seed the schema using raw SQL so we don't depend on the rest of the
|
|
131
|
+
// migration pipeline for setup.
|
|
132
|
+
const ctx = new DropColCtx();
|
|
133
|
+
ctx._SQLEngine.db.exec(`
|
|
134
|
+
CREATE TABLE IF NOT EXISTS Widget (
|
|
135
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
136
|
+
name TEXT,
|
|
137
|
+
legacyField TEXT
|
|
138
|
+
);
|
|
139
|
+
`);
|
|
140
|
+
await ctx.close();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
test('SQLite schema.dropColumn() is idempotent (re-running does not throw)', async () => {
|
|
144
|
+
const ctx = new DropColCtx();
|
|
145
|
+
const sch = new schema(DropColCtx);
|
|
146
|
+
await sch._ensureReady();
|
|
147
|
+
// schema.dropColumn() gates on `this.fullTable` being truthy as a "did
|
|
148
|
+
// init run" sentinel. Real migrations get a real tableObj here; tests
|
|
149
|
+
// just need a truthy value to pass the gate.
|
|
150
|
+
sch.fullTable = { __name: 'Widget' };
|
|
151
|
+
|
|
152
|
+
// Confirm the column exists initially.
|
|
153
|
+
const before = await ctx._SQLEngine.getTableInfo('Widget');
|
|
154
|
+
assert.ok(before.some(c => c.name === 'legacyField'), 'precondition: legacyField column exists');
|
|
155
|
+
|
|
156
|
+
// First drop: removes the column.
|
|
157
|
+
await sch.dropColumn({ tableName: 'Widget', name: 'legacyField' });
|
|
158
|
+
const afterFirst = await ctx._SQLEngine.getTableInfo('Widget');
|
|
159
|
+
assert.ok(!afterFirst.some(c => c.name === 'legacyField'), 'column was dropped');
|
|
160
|
+
|
|
161
|
+
// Second drop: must be a no-op, not an error. This is the regression
|
|
162
|
+
// that 1.0.8 fixes — without the runtime guard in schema.dropColumn(),
|
|
163
|
+
// SQLite would have thrown "no such column: legacyField".
|
|
164
|
+
await assert.doesNotReject(
|
|
165
|
+
sch.dropColumn({ tableName: 'Widget', name: 'legacyField' }),
|
|
166
|
+
'second dropColumn must be idempotent'
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
await ctx.close();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('SQLite schema.dropColumn() on a never-existed column is also a no-op', async () => {
|
|
173
|
+
const ctx = new DropColCtx();
|
|
174
|
+
const sch = new schema(DropColCtx);
|
|
175
|
+
await sch._ensureReady();
|
|
176
|
+
sch.fullTable = { __name: 'Widget' };
|
|
177
|
+
await assert.doesNotReject(
|
|
178
|
+
sch.dropColumn({ tableName: 'Widget', name: 'neverExisted' })
|
|
179
|
+
);
|
|
180
|
+
await ctx.close();
|
|
181
|
+
});
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression for the Postgres CamelCase-identifier bug.
|
|
3
|
+
*
|
|
4
|
+
* Symptom: DDL was correctly emitted as CREATE TABLE "SchedulerLeader" (...)
|
|
5
|
+
* but runtime SELECT/UPDATE/DELETE used unquoted identifiers, so Postgres
|
|
6
|
+
* folded `SchedulerLeader` to `schedulerleader` and threw:
|
|
7
|
+
* ERROR: relation "schedulerleader" does not exist
|
|
8
|
+
*
|
|
9
|
+
* The fix double-quotes every table and column name in generated SQL.
|
|
10
|
+
*
|
|
11
|
+
* These tests build SQL via the engine's pure-function builders (no live
|
|
12
|
+
* Postgres connection required) and assert the resulting SQL contains
|
|
13
|
+
* properly-quoted identifiers.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { test } from 'node:test';
|
|
17
|
+
import assert from 'node:assert/strict';
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { fileURLToPath } from 'node:url';
|
|
21
|
+
|
|
22
|
+
process.env.master = 'pgcamelcase';
|
|
23
|
+
|
|
24
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
25
|
+
const fixturesDir = path.join(__dirname, 'fixtures', 'pg-camelcase');
|
|
26
|
+
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
27
|
+
const dbDir = path.join(fixturesDir, 'db');
|
|
28
|
+
const dbFile = path.join(dbDir, 'pgcamelctx.sqlite');
|
|
29
|
+
|
|
30
|
+
fs.mkdirSync(envDir, { recursive: true });
|
|
31
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
32
|
+
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
33
|
+
|
|
34
|
+
fs.writeFileSync(
|
|
35
|
+
path.join(envDir, 'env.pgcamelcase.json'),
|
|
36
|
+
JSON.stringify({
|
|
37
|
+
PgCamelCtx: {
|
|
38
|
+
type: 'better-sqlite3',
|
|
39
|
+
connection: dbDir + path.sep,
|
|
40
|
+
},
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const { default: context } = await import('../context.js');
|
|
45
|
+
const { default: PostgresEngine } = await import('../postgresEngine.js');
|
|
46
|
+
|
|
47
|
+
// CamelCase entities — the exact pattern that triggered the production bug.
|
|
48
|
+
class SchedulerLeader {
|
|
49
|
+
id(db) { db.integer().primary().auto(); }
|
|
50
|
+
nodeName(db) { db.string(); }
|
|
51
|
+
acquiredAt(db) { db.integer(); }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
class MemoryDoc {
|
|
55
|
+
id(db) { db.integer().primary().auto(); }
|
|
56
|
+
title(db) { db.string(); }
|
|
57
|
+
body(db) { db.text(); }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class PgCamelCtx extends context {
|
|
61
|
+
constructor() {
|
|
62
|
+
super();
|
|
63
|
+
this.env(envDir);
|
|
64
|
+
this.dbset(SchedulerLeader);
|
|
65
|
+
this.dbset(MemoryDoc);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Helper: drive the fluent API through SQLite to get a populated query
|
|
70
|
+
// script, then feed it to the Postgres engine builders.
|
|
71
|
+
function buildScript(chainFn) {
|
|
72
|
+
const ctx = new PgCamelCtx();
|
|
73
|
+
const builder = chainFn(ctx);
|
|
74
|
+
const script = builder.__queryObject.script;
|
|
75
|
+
const entity = builder.__entity;
|
|
76
|
+
ctx.close();
|
|
77
|
+
return { script, entity };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
test('Postgres _q helper quotes CamelCase identifiers', () => {
|
|
81
|
+
const engine = new PostgresEngine();
|
|
82
|
+
assert.equal(engine._q('SchedulerLeader'), '"SchedulerLeader"');
|
|
83
|
+
assert.equal(engine._q('nodeName'), '"nodeName"');
|
|
84
|
+
assert.equal(engine._q('lower'), '"lower"');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('Postgres _q helper passes through *', () => {
|
|
88
|
+
const engine = new PostgresEngine();
|
|
89
|
+
assert.equal(engine._q('*'), '*');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('Postgres _q helper escapes embedded double-quotes', () => {
|
|
93
|
+
const engine = new PostgresEngine();
|
|
94
|
+
assert.equal(engine._q('weird"name'), '"weird""name"');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('Postgres _q helper is idempotent (already-quoted passes through)', () => {
|
|
98
|
+
const engine = new PostgresEngine();
|
|
99
|
+
assert.equal(engine._q('"Quoted"'), '"Quoted"');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('Postgres buildFrom emits quoted CamelCase table name with alias', () => {
|
|
103
|
+
const { script, entity } = buildScript((ctx) =>
|
|
104
|
+
ctx.SchedulerLeader.where('l => l.nodeName == $$', 'node-1')
|
|
105
|
+
);
|
|
106
|
+
const engine = new PostgresEngine();
|
|
107
|
+
const from = engine.buildFrom(script, entity);
|
|
108
|
+
assert.match(from, /FROM\s+"SchedulerLeader"/, `FROM clause must quote the CamelCase table name. Got: ${from}`);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('Postgres buildWhere emits quoted CamelCase column name and does NOT capitalize it', () => {
|
|
112
|
+
const { script, entity } = buildScript((ctx) =>
|
|
113
|
+
ctx.SchedulerLeader.where('l => l.nodeName == $$', 'node-1')
|
|
114
|
+
);
|
|
115
|
+
const engine = new PostgresEngine();
|
|
116
|
+
const where = engine.buildWhere(script, entity);
|
|
117
|
+
assert.match(where, /"nodeName"/, `WHERE clause must use quoted "nodeName" (original case). Got: ${where}`);
|
|
118
|
+
assert.doesNotMatch(where, /"NodeName"/, 'WHERE must NOT capitalize the field — Postgres preserves case when quoted, so "NodeName" would not match the actual "nodeName" column');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('Postgres full buildQuery emits quoted table, alias, and columns', () => {
|
|
122
|
+
const { script, entity } = buildScript((ctx) =>
|
|
123
|
+
ctx.SchedulerLeader
|
|
124
|
+
.where('l => l.nodeName == $$', 'node-1')
|
|
125
|
+
.orderBy('l => l.acquiredAt')
|
|
126
|
+
);
|
|
127
|
+
const engine = new PostgresEngine();
|
|
128
|
+
const out = engine.buildQuery(script, entity, {});
|
|
129
|
+
|
|
130
|
+
// Table quoted with alias
|
|
131
|
+
assert.match(out.query, /FROM\s+"SchedulerLeader"\s+AS\s+\w+/);
|
|
132
|
+
// Column names in SELECT are quoted
|
|
133
|
+
assert.match(out.query, /\w+\."id"/);
|
|
134
|
+
assert.match(out.query, /\w+\."nodeName"/);
|
|
135
|
+
// ORDER BY uses original case
|
|
136
|
+
assert.match(out.query, /ORDER BY\s+\w+\."acquiredAt"\s+ASC/);
|
|
137
|
+
// No unquoted CamelCase table name leaking through
|
|
138
|
+
assert.doesNotMatch(out.query, /FROM\s+SchedulerLeader\b/, 'unquoted CamelCase table must not appear');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test('Postgres INSERT emits quoted CamelCase table and columns', () => {
|
|
142
|
+
const engine = new PostgresEngine();
|
|
143
|
+
const fields = {
|
|
144
|
+
__entity: { __name: 'MemoryDoc' },
|
|
145
|
+
title: 'hello',
|
|
146
|
+
body: 'world',
|
|
147
|
+
};
|
|
148
|
+
const modelEntity = {
|
|
149
|
+
__name: 'MemoryDoc',
|
|
150
|
+
id: { name: 'id', type: 'integer', primary: true, auto: true },
|
|
151
|
+
title: { name: 'title', type: 'string' },
|
|
152
|
+
body: { name: 'body', type: 'text' },
|
|
153
|
+
};
|
|
154
|
+
const sqlObj = engine._buildSQLInsertObjectParameterized(fields, modelEntity);
|
|
155
|
+
assert.notEqual(sqlObj, -1);
|
|
156
|
+
assert.equal(sqlObj.tableName, 'MemoryDoc');
|
|
157
|
+
assert.match(sqlObj.columns, /"title"/);
|
|
158
|
+
assert.match(sqlObj.columns, /"body"/);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('Postgres UPDATE SET clause quotes each column name', () => {
|
|
162
|
+
const engine = new PostgresEngine();
|
|
163
|
+
const model = {
|
|
164
|
+
__entity: {
|
|
165
|
+
__name: 'SchedulerLeader',
|
|
166
|
+
nodeName: { name: 'nodeName', type: 'string', nullable: true },
|
|
167
|
+
acquiredAt: { name: 'acquiredAt', type: 'integer', nullable: true },
|
|
168
|
+
},
|
|
169
|
+
__dirtyFields: ['nodeName', 'acquiredAt'],
|
|
170
|
+
_nodeName: 'node-2',
|
|
171
|
+
_acquiredAt: 12345,
|
|
172
|
+
};
|
|
173
|
+
const argu = engine._buildSQLEqualToParameterized(model);
|
|
174
|
+
assert.notEqual(argu, -1);
|
|
175
|
+
assert.match(argu.query, /"nodeName"\s*=\s*\$1/);
|
|
176
|
+
assert.match(argu.query, /"acquiredAt"\s*=\s*\$2/);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('Postgres COUNT(*) for untyped count (not COUNT(alias.*) which is invalid)', () => {
|
|
180
|
+
const engine = new PostgresEngine();
|
|
181
|
+
const script = { count: 'none', entityMap: [], parentName: 'SchedulerLeader' };
|
|
182
|
+
const entity = { __name: 'SchedulerLeader' };
|
|
183
|
+
assert.equal(engine.buildCount(script, entity), 'COUNT(*)');
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test('Postgres DELETE emits quoted CamelCase table and primary key', async () => {
|
|
187
|
+
const engine = new PostgresEngine();
|
|
188
|
+
// Stub out _runWithParams so we can capture the generated SQL without
|
|
189
|
+
// needing a live Postgres connection.
|
|
190
|
+
let capturedSql;
|
|
191
|
+
let capturedParams;
|
|
192
|
+
engine._runWithParams = async (sql, params) => {
|
|
193
|
+
capturedSql = sql;
|
|
194
|
+
capturedParams = params;
|
|
195
|
+
return { rows: [], rowCount: 0 };
|
|
196
|
+
};
|
|
197
|
+
const queryObject = {
|
|
198
|
+
__entity: {
|
|
199
|
+
__name: 'SchedulerLeader',
|
|
200
|
+
id: { name: 'id', primary: true, auto: true },
|
|
201
|
+
nodeName: { name: 'nodeName' },
|
|
202
|
+
},
|
|
203
|
+
id: 42,
|
|
204
|
+
};
|
|
205
|
+
await engine.delete(queryObject);
|
|
206
|
+
assert.match(capturedSql, /DELETE FROM "SchedulerLeader" WHERE "SchedulerLeader"\."id"\s*=\s*\$1/);
|
|
207
|
+
assert.deepEqual(capturedParams, [42]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('Postgres UPDATE wraps table and primary key', async () => {
|
|
211
|
+
const engine = new PostgresEngine();
|
|
212
|
+
let capturedSql;
|
|
213
|
+
engine._runWithParams = async (sql, params) => {
|
|
214
|
+
capturedSql = sql;
|
|
215
|
+
return { rows: [], rowCount: 0 };
|
|
216
|
+
};
|
|
217
|
+
await engine.update({
|
|
218
|
+
tableName: 'SchedulerLeader',
|
|
219
|
+
primaryKey: 'id',
|
|
220
|
+
primaryKeyValue: 7,
|
|
221
|
+
arg: {
|
|
222
|
+
query: '"nodeName" = $1',
|
|
223
|
+
params: ['node-9'],
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
assert.match(capturedSql, /UPDATE "SchedulerLeader" SET "nodeName" = \$1 WHERE "SchedulerLeader"\."id" = \$2/);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test('Postgres bulkDelete uses entity primary key (not hardcoded id) and quotes it', async () => {
|
|
230
|
+
const engine = new PostgresEngine();
|
|
231
|
+
let capturedSql;
|
|
232
|
+
engine._runWithParams = async (sql, params) => {
|
|
233
|
+
capturedSql = sql;
|
|
234
|
+
return { rows: [], rowCount: 0 };
|
|
235
|
+
};
|
|
236
|
+
await engine.bulkDelete('MemoryDoc', [1, 2, 3], 'docId');
|
|
237
|
+
assert.match(capturedSql, /DELETE FROM "MemoryDoc" WHERE "docId" IN \(\$1, \$2, \$3\)/);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test('Postgres bulkDelete defaults to id for back-compat', async () => {
|
|
241
|
+
const engine = new PostgresEngine();
|
|
242
|
+
let capturedSql;
|
|
243
|
+
engine._runWithParams = async (sql, params) => {
|
|
244
|
+
capturedSql = sql;
|
|
245
|
+
return { rows: [], rowCount: 0 };
|
|
246
|
+
};
|
|
247
|
+
await engine.bulkDelete('SchedulerLeader', [10, 20]);
|
|
248
|
+
assert.match(capturedSql, /DELETE FROM "SchedulerLeader" WHERE "id" IN \(\$1, \$2\)/);
|
|
249
|
+
});
|
|
@@ -221,7 +221,7 @@ test('Postgres buildQuery emits ORDER BY ... ASC for .orderBy()', () => {
|
|
|
221
221
|
);
|
|
222
222
|
const engine = new PostgresEngine();
|
|
223
223
|
const out = engine.buildQuery(script, entity, {});
|
|
224
|
-
assert.match(out.query, /ORDER BY\s+\w+\.updatedAt
|
|
224
|
+
assert.match(out.query, /ORDER BY\s+\w+\."?updatedAt"?\s+ASC/);
|
|
225
225
|
});
|
|
226
226
|
|
|
227
227
|
test('Postgres buildQuery emits ORDER BY ... DESC for .orderByDescending()', () => {
|
|
@@ -230,7 +230,7 @@ test('Postgres buildQuery emits ORDER BY ... DESC for .orderByDescending()', ()
|
|
|
230
230
|
);
|
|
231
231
|
const engine = new PostgresEngine();
|
|
232
232
|
const out = engine.buildQuery(script, entity, {});
|
|
233
|
-
assert.match(out.query, /ORDER BY\s+\w+\.updatedAt
|
|
233
|
+
assert.match(out.query, /ORDER BY\s+\w+\."?updatedAt"?\s+DESC/);
|
|
234
234
|
});
|
|
235
235
|
|
|
236
236
|
test('Postgres buildQuery emits LIMIT and OFFSET', () => {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"AuditCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/broad-audit/db/"}}
|
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"CountCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/count-chained-where/db/"}}
|
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"CountFromCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/count-no-from/db/"}}
|
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"EdgeCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/edge-cases-audit/db/"}}
|
|
Binary file
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateAuthor extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Author);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Author);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateAuthor;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateBook extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Book);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Book);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateBook;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateReview extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Review);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Review);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateReview;
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"contextLocation": "../../multimigctx.js",
|
|
3
|
-
"migrationFolder": ".",
|
|
4
|
-
"snapShotLocation": "multimigctx_contextSnapShot.json",
|
|
5
|
-
"schema": [
|
|
6
|
-
{
|
|
7
|
-
"id": {
|
|
8
|
-
"name": "id",
|
|
9
|
-
"type": "integer",
|
|
10
|
-
"primary": true,
|
|
11
|
-
"nullable": false,
|
|
12
|
-
"unique": true,
|
|
13
|
-
"auto": true,
|
|
14
|
-
"cascadeOnDelete": true,
|
|
15
|
-
"lazyLoading": true,
|
|
16
|
-
"isNavigational": false,
|
|
17
|
-
"skipGetFunction": false,
|
|
18
|
-
"valueConversion": true
|
|
19
|
-
},
|
|
20
|
-
"name": {
|
|
21
|
-
"name": "name",
|
|
22
|
-
"type": "string",
|
|
23
|
-
"nullable": true,
|
|
24
|
-
"unique": false,
|
|
25
|
-
"auto": false,
|
|
26
|
-
"cascadeOnDelete": true,
|
|
27
|
-
"lazyLoading": true,
|
|
28
|
-
"isNavigational": false,
|
|
29
|
-
"skipGetFunction": false,
|
|
30
|
-
"valueConversion": true
|
|
31
|
-
},
|
|
32
|
-
"__compositeIndexes": [],
|
|
33
|
-
"__name": "Author"
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
"id": {
|
|
37
|
-
"name": "id",
|
|
38
|
-
"type": "integer",
|
|
39
|
-
"primary": true,
|
|
40
|
-
"nullable": false,
|
|
41
|
-
"unique": true,
|
|
42
|
-
"auto": true,
|
|
43
|
-
"cascadeOnDelete": true,
|
|
44
|
-
"lazyLoading": true,
|
|
45
|
-
"isNavigational": false,
|
|
46
|
-
"skipGetFunction": false,
|
|
47
|
-
"valueConversion": true
|
|
48
|
-
},
|
|
49
|
-
"title": {
|
|
50
|
-
"name": "title",
|
|
51
|
-
"type": "string",
|
|
52
|
-
"nullable": true,
|
|
53
|
-
"unique": false,
|
|
54
|
-
"auto": false,
|
|
55
|
-
"cascadeOnDelete": true,
|
|
56
|
-
"lazyLoading": true,
|
|
57
|
-
"isNavigational": false,
|
|
58
|
-
"skipGetFunction": false,
|
|
59
|
-
"valueConversion": true
|
|
60
|
-
},
|
|
61
|
-
"__compositeIndexes": [],
|
|
62
|
-
"__name": "Book"
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
"id": {
|
|
66
|
-
"name": "id",
|
|
67
|
-
"type": "integer",
|
|
68
|
-
"primary": true,
|
|
69
|
-
"nullable": false,
|
|
70
|
-
"unique": true,
|
|
71
|
-
"auto": true,
|
|
72
|
-
"cascadeOnDelete": true,
|
|
73
|
-
"lazyLoading": true,
|
|
74
|
-
"isNavigational": false,
|
|
75
|
-
"skipGetFunction": false,
|
|
76
|
-
"valueConversion": true
|
|
77
|
-
},
|
|
78
|
-
"body": {
|
|
79
|
-
"name": "body",
|
|
80
|
-
"type": "string",
|
|
81
|
-
"nullable": true,
|
|
82
|
-
"unique": false,
|
|
83
|
-
"auto": false,
|
|
84
|
-
"cascadeOnDelete": true,
|
|
85
|
-
"lazyLoading": true,
|
|
86
|
-
"isNavigational": false,
|
|
87
|
-
"skipGetFunction": false,
|
|
88
|
-
"valueConversion": true
|
|
89
|
-
},
|
|
90
|
-
"__compositeIndexes": [],
|
|
91
|
-
"__name": "Review"
|
|
92
|
-
}
|
|
93
|
-
],
|
|
94
|
-
"seedData": {},
|
|
95
|
-
"seedConfig": {}
|
|
96
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import context from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/context.js";
|
|
3
|
-
|
|
4
|
-
class Author {
|
|
5
|
-
id(db) { db.integer().primary().auto(); }
|
|
6
|
-
name(db) { db.string(); }
|
|
7
|
-
}
|
|
8
|
-
class Book {
|
|
9
|
-
id(db) { db.integer().primary().auto(); }
|
|
10
|
-
title(db) { db.string(); }
|
|
11
|
-
}
|
|
12
|
-
class Review {
|
|
13
|
-
id(db) { db.integer().primary().auto(); }
|
|
14
|
-
body(db) { db.string(); }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class multimigctx extends context {
|
|
18
|
-
constructor() {
|
|
19
|
-
super();
|
|
20
|
-
this.env("/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/multi-migration-N4bUYy/config/environments");
|
|
21
|
-
this.dbset(Author);
|
|
22
|
-
this.dbset(Book);
|
|
23
|
-
this.dbset(Review);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
export default multimigctx;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"multimigctx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/multi-migration-N4bUYy/db/"}}
|
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"name":"multi-migration-fixture","type":"module"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateAuthor extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Author);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Author);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateAuthor;
|