@mikro-orm/sql 7.1.0-dev.5 → 7.1.0-dev.50
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/AbstractSqlConnection.d.ts +1 -1
- package/AbstractSqlConnection.js +27 -6
- package/AbstractSqlDriver.d.ts +26 -1
- package/AbstractSqlDriver.js +294 -37
- package/AbstractSqlPlatform.d.ts +15 -3
- package/AbstractSqlPlatform.js +25 -7
- package/PivotCollectionPersister.d.ts +2 -2
- package/PivotCollectionPersister.js +19 -3
- package/README.md +2 -1
- package/SqlEntityManager.d.ts +48 -5
- package/SqlEntityManager.js +77 -7
- package/SqlMikroORM.d.ts +23 -0
- package/SqlMikroORM.js +23 -0
- package/dialects/mysql/BaseMySqlPlatform.d.ts +4 -5
- package/dialects/mysql/BaseMySqlPlatform.js +9 -10
- package/dialects/mysql/MySqlSchemaHelper.d.ts +19 -3
- package/dialects/mysql/MySqlSchemaHelper.js +280 -49
- package/dialects/oracledb/OracleDialect.d.ts +1 -1
- package/dialects/oracledb/OracleDialect.js +2 -1
- package/dialects/postgresql/BasePostgreSqlEntityManager.d.ts +19 -0
- package/dialects/postgresql/BasePostgreSqlEntityManager.js +24 -0
- package/dialects/postgresql/BasePostgreSqlPlatform.d.ts +11 -5
- package/dialects/postgresql/BasePostgreSqlPlatform.js +75 -17
- package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +38 -1
- package/dialects/postgresql/PostgreSqlSchemaHelper.js +362 -28
- package/dialects/postgresql/index.d.ts +2 -0
- package/dialects/postgresql/index.js +2 -0
- package/dialects/postgresql/typeOverrides.d.ts +14 -0
- package/dialects/postgresql/typeOverrides.js +12 -0
- package/dialects/sqlite/SqlitePlatform.d.ts +2 -1
- package/dialects/sqlite/SqlitePlatform.js +4 -0
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +9 -2
- package/dialects/sqlite/SqliteSchemaHelper.js +148 -19
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +4 -4
- package/plugin/transformer.d.ts +11 -3
- package/plugin/transformer.js +138 -29
- package/query/CriteriaNode.d.ts +1 -1
- package/query/CriteriaNode.js +2 -2
- package/query/ObjectCriteriaNode.js +1 -1
- package/query/QueryBuilder.d.ts +42 -1
- package/query/QueryBuilder.js +78 -7
- package/schema/DatabaseSchema.d.ts +29 -2
- package/schema/DatabaseSchema.js +145 -4
- package/schema/DatabaseTable.d.ts +20 -1
- package/schema/DatabaseTable.js +182 -31
- package/schema/SchemaComparator.d.ts +19 -0
- package/schema/SchemaComparator.js +250 -1
- package/schema/SchemaHelper.d.ts +77 -1
- package/schema/SchemaHelper.js +297 -25
- package/schema/SqlSchemaGenerator.d.ts +2 -2
- package/schema/SqlSchemaGenerator.js +47 -10
- package/schema/partitioning.d.ts +13 -0
- package/schema/partitioning.js +326 -0
- package/typings.d.ts +72 -5
|
@@ -7,12 +7,34 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
7
7
|
'current_timestamp(?)': ['current_timestamp(?)'],
|
|
8
8
|
'0': ['0', 'false'],
|
|
9
9
|
};
|
|
10
|
+
// Greedy `(.+)` so nested CASE expressions inside the predicate don't trip the match on
|
|
11
|
+
// an inner `then <col> end` — the trailing `$` anchor forces the regex engine to extend
|
|
12
|
+
// the capture to the outermost case-end boundary.
|
|
13
|
+
static PARTIAL_INDEX_RE = /^\s*\(\s*case\s+when\s+(.+)\s+then\s+`([^`]+)`\s+end\s*\)\s*$/is;
|
|
10
14
|
getSchemaBeginning(charset, disableForeignKeys) {
|
|
11
15
|
if (disableForeignKeys) {
|
|
12
16
|
return `set names ${charset};\n${this.disableForeignKeysSQL()}\n\n`;
|
|
13
17
|
}
|
|
14
18
|
return `set names ${charset};\n\n`;
|
|
15
19
|
}
|
|
20
|
+
getSetSchemaSQL(schema) {
|
|
21
|
+
return `use ${this.quote(schema)}`;
|
|
22
|
+
}
|
|
23
|
+
getResetSchemaSQL(defaultSchema) {
|
|
24
|
+
return `use ${this.quote(defaultSchema)}`;
|
|
25
|
+
}
|
|
26
|
+
supportsMigrationSchema() {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
async tableExists(connection, tableName, schemaName, ctx) {
|
|
30
|
+
// MySQL "schema" = database — when none is requested, probe the connection's current DB
|
|
31
|
+
// via `schema()` rather than the base impl's `getDefaultSchemaName()` (which is undefined here).
|
|
32
|
+
const schemaClause = schemaName
|
|
33
|
+
? `table_schema = ${this.platform.quoteValue(schemaName)}`
|
|
34
|
+
: `table_schema = schema()`;
|
|
35
|
+
const rows = await connection.execute(`select 1 from information_schema.tables where ${schemaClause} and table_name = ${this.platform.quoteValue(tableName)}`, [], 'all', ctx);
|
|
36
|
+
return rows.length > 0;
|
|
37
|
+
}
|
|
16
38
|
disableForeignKeysSQL() {
|
|
17
39
|
return 'set foreign_key_checks = 0;';
|
|
18
40
|
}
|
|
@@ -31,7 +53,7 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
31
53
|
return sql;
|
|
32
54
|
}
|
|
33
55
|
getListTablesSQL() {
|
|
34
|
-
return `select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema()`;
|
|
56
|
+
return `select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment, table_collation as table_collation from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema()`;
|
|
35
57
|
}
|
|
36
58
|
getListViewsSQL() {
|
|
37
59
|
return `select table_name as view_name, nullif(table_schema, schema()) as schema_name, view_definition from information_schema.views where table_schema = schema()`;
|
|
@@ -64,11 +86,16 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
64
86
|
const checks = await this.getAllChecks(connection, tables, ctx);
|
|
65
87
|
const fks = await this.getAllForeignKeys(connection, tables, ctx);
|
|
66
88
|
const enums = await this.getAllEnumDefinitions(connection, tables, ctx);
|
|
89
|
+
const triggers = await this.getAllTriggers(connection, tables);
|
|
67
90
|
for (const t of tables) {
|
|
68
91
|
const key = this.getTableKey(t);
|
|
69
92
|
const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
|
|
93
|
+
table.collation = t.table_collation ?? undefined;
|
|
70
94
|
const pks = await this.getPrimaryKeys(connection, indexes[key], table.name, table.schema);
|
|
71
95
|
table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums[key]);
|
|
96
|
+
if (triggers[key]) {
|
|
97
|
+
table.setTriggers(triggers[key]);
|
|
98
|
+
}
|
|
72
99
|
}
|
|
73
100
|
}
|
|
74
101
|
async getAllIndexes(connection, tables, ctx) {
|
|
@@ -80,8 +107,11 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
80
107
|
const ret = {};
|
|
81
108
|
for (const index of allIndexes) {
|
|
82
109
|
const key = this.getTableKey(index);
|
|
110
|
+
const partialMatch = !index.column_name && typeof index.expression === 'string'
|
|
111
|
+
? MySqlSchemaHelper.PARTIAL_INDEX_RE.exec(index.expression)
|
|
112
|
+
: null;
|
|
83
113
|
const indexDef = {
|
|
84
|
-
columnNames: [index.column_name],
|
|
114
|
+
columnNames: [partialMatch ? partialMatch[2] : index.column_name],
|
|
85
115
|
keyName: index.index_name,
|
|
86
116
|
unique: !index.non_unique,
|
|
87
117
|
primary: index.index_name === 'PRIMARY',
|
|
@@ -109,7 +139,10 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
109
139
|
if (index.is_visible === 'NO') {
|
|
110
140
|
indexDef.invisible = true;
|
|
111
141
|
}
|
|
112
|
-
if (
|
|
142
|
+
if (partialMatch) {
|
|
143
|
+
indexDef.where = partialMatch[1].trim();
|
|
144
|
+
}
|
|
145
|
+
else if (!index.column_name || index.expression?.match(/ where /i)) {
|
|
113
146
|
indexDef.expression = index.expression; // required for the `getCreateIndexSQL()` call
|
|
114
147
|
indexDef.expression = this.getCreateIndexSQL(index.table_name, indexDef, !!index.expression);
|
|
115
148
|
}
|
|
@@ -146,40 +179,43 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
146
179
|
return this.appendMySqlIndexSuffix(sql, index);
|
|
147
180
|
}
|
|
148
181
|
/**
|
|
149
|
-
* Build the column list for a MySQL index
|
|
150
|
-
*
|
|
182
|
+
* Build the column list for a MySQL index. MySQL requires collation via an expression:
|
|
183
|
+
* `(column COLLATE collation_name)`. Partial indexes (`where`) are emulated via functional
|
|
184
|
+
* indexes — requires MySQL 8.0.13+. MariaDB does not support inline functional indexes
|
|
185
|
+
* and overrides to throw at a higher level.
|
|
151
186
|
*/
|
|
152
187
|
getIndexColumns(index) {
|
|
153
|
-
if (index.
|
|
154
|
-
return index
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return expr;
|
|
167
|
-
}
|
|
168
|
-
// Standard column definition without collation
|
|
169
|
-
let colDef = quotedName;
|
|
170
|
-
// MySQL supports prefix length
|
|
171
|
-
if (col.length) {
|
|
172
|
-
colDef += `(${col.length})`;
|
|
173
|
-
}
|
|
174
|
-
// MySQL supports sort order
|
|
188
|
+
if (index.where) {
|
|
189
|
+
return this.emulatePartialIndexColumns(index);
|
|
190
|
+
}
|
|
191
|
+
return index.columnNames
|
|
192
|
+
.map(name => {
|
|
193
|
+
const col = index.columns?.find(c => c.name === name);
|
|
194
|
+
const quotedName = this.quote(name);
|
|
195
|
+
// MySQL supports collation via expression: (column_name COLLATE collation_name)
|
|
196
|
+
// When collation is specified, wrap in parentheses as an expression
|
|
197
|
+
if (col?.collation) {
|
|
198
|
+
let expr = col.length ? `${quotedName}(${col.length})` : quotedName;
|
|
199
|
+
expr = `(${expr} collate ${col.collation})`;
|
|
200
|
+
// Sort order comes after the expression
|
|
175
201
|
if (col.sort) {
|
|
176
|
-
|
|
202
|
+
expr += ` ${col.sort}`;
|
|
177
203
|
}
|
|
178
|
-
return
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
204
|
+
return expr;
|
|
205
|
+
}
|
|
206
|
+
// Standard column definition without collation
|
|
207
|
+
let colDef = quotedName;
|
|
208
|
+
// MySQL supports prefix length
|
|
209
|
+
if (col?.length) {
|
|
210
|
+
colDef += `(${col.length})`;
|
|
211
|
+
}
|
|
212
|
+
// MySQL supports sort order
|
|
213
|
+
if (col?.sort) {
|
|
214
|
+
colDef += ` ${col.sort}`;
|
|
215
|
+
}
|
|
216
|
+
return colDef;
|
|
217
|
+
})
|
|
218
|
+
.join(', ');
|
|
183
219
|
}
|
|
184
220
|
/**
|
|
185
221
|
* Append MySQL-specific index suffixes like INVISIBLE.
|
|
@@ -192,22 +228,25 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
192
228
|
return sql;
|
|
193
229
|
}
|
|
194
230
|
async getAllColumns(connection, tables, ctx) {
|
|
195
|
-
const sql = `select table_name as table_name,
|
|
196
|
-
nullif(table_schema, schema()) as schema_name,
|
|
197
|
-
column_name as column_name,
|
|
198
|
-
column_default as column_default,
|
|
199
|
-
nullif(column_comment, '') as column_comment,
|
|
200
|
-
is_nullable as is_nullable,
|
|
201
|
-
data_type as data_type,
|
|
202
|
-
column_type as column_type,
|
|
203
|
-
column_key as column_key,
|
|
204
|
-
extra as extra,
|
|
205
|
-
generation_expression as generation_expression,
|
|
206
|
-
numeric_precision as numeric_precision,
|
|
207
|
-
numeric_scale as numeric_scale,
|
|
208
|
-
ifnull(datetime_precision, character_maximum_length) length
|
|
209
|
-
|
|
210
|
-
|
|
231
|
+
const sql = `select c.table_name as table_name,
|
|
232
|
+
nullif(c.table_schema, schema()) as schema_name,
|
|
233
|
+
c.column_name as column_name,
|
|
234
|
+
c.column_default as column_default,
|
|
235
|
+
nullif(c.column_comment, '') as column_comment,
|
|
236
|
+
c.is_nullable as is_nullable,
|
|
237
|
+
c.data_type as data_type,
|
|
238
|
+
c.column_type as column_type,
|
|
239
|
+
c.column_key as column_key,
|
|
240
|
+
c.extra as extra,
|
|
241
|
+
c.generation_expression as generation_expression,
|
|
242
|
+
c.numeric_precision as numeric_precision,
|
|
243
|
+
c.numeric_scale as numeric_scale,
|
|
244
|
+
ifnull(c.datetime_precision, c.character_maximum_length) length,
|
|
245
|
+
nullif(c.collation_name, t.table_collation) as collation_name
|
|
246
|
+
from information_schema.columns c
|
|
247
|
+
join information_schema.tables t on t.table_schema = c.table_schema and t.table_name = c.table_name
|
|
248
|
+
where c.table_schema = database() and c.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name))})
|
|
249
|
+
order by c.ordinal_position`;
|
|
211
250
|
const allColumns = await connection.execute(sql, [], 'all', ctx);
|
|
212
251
|
const str = (val) => (val != null ? '' + val : val);
|
|
213
252
|
const extra = (val) => val.replace(/auto_increment|default_generated|(stored|virtual) generated/i, '').trim() || undefined;
|
|
@@ -238,6 +277,7 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
238
277
|
precision: col.numeric_precision,
|
|
239
278
|
scale: col.numeric_scale,
|
|
240
279
|
comment: col.column_comment,
|
|
280
|
+
collation: col.collation_name ?? undefined,
|
|
241
281
|
extra: extra(col.extra),
|
|
242
282
|
generated,
|
|
243
283
|
});
|
|
@@ -264,6 +304,194 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
264
304
|
}
|
|
265
305
|
return ret;
|
|
266
306
|
}
|
|
307
|
+
/** Generates SQL to create MySQL triggers. MySQL requires one trigger per event. */
|
|
308
|
+
createTrigger(table, trigger) {
|
|
309
|
+
if (trigger.expression) {
|
|
310
|
+
return trigger.expression;
|
|
311
|
+
}
|
|
312
|
+
/* v8 ignore next 3 */
|
|
313
|
+
if (trigger.timing === 'instead of') {
|
|
314
|
+
throw new Error(`MySQL does not support INSTEAD OF triggers. Use BEFORE or AFTER for trigger "${trigger.name}".`);
|
|
315
|
+
}
|
|
316
|
+
/* v8 ignore next 5 */
|
|
317
|
+
if (trigger.forEach === 'statement') {
|
|
318
|
+
throw new Error(`MySQL does not support FOR EACH STATEMENT triggers. Use FOR EACH ROW for trigger "${trigger.name}".`);
|
|
319
|
+
}
|
|
320
|
+
const timing = trigger.timing.toUpperCase();
|
|
321
|
+
const ret = [];
|
|
322
|
+
for (const event of trigger.events) {
|
|
323
|
+
const name = trigger.events.length > 1 ? `${trigger.name}_${event}` : trigger.name;
|
|
324
|
+
ret.push(`create trigger ${this.quote(name)} ${timing} ${event.toUpperCase()} on ${table.getQuotedName()} for each ROW begin ${trigger.body}; end`);
|
|
325
|
+
}
|
|
326
|
+
return ret.join(';\n');
|
|
327
|
+
}
|
|
328
|
+
createRoutine(routine) {
|
|
329
|
+
if (routine.expression) {
|
|
330
|
+
return routine.expression;
|
|
331
|
+
}
|
|
332
|
+
const name = this.platform.quoteIdentifier(routine.name);
|
|
333
|
+
// MySQL functions reject direction prefixes (function params are always IN); procedures use them.
|
|
334
|
+
const params = routine.params
|
|
335
|
+
.map(p => {
|
|
336
|
+
const dir = routine.type === 'procedure' ? `${p.direction.toUpperCase()} ` : '';
|
|
337
|
+
return `${dir}${this.platform.quoteIdentifier(p.name)} ${p.type}`;
|
|
338
|
+
})
|
|
339
|
+
.join(', ');
|
|
340
|
+
const determinism = routine.deterministic === true ? ' deterministic' : routine.deterministic === false ? ' not deterministic' : '';
|
|
341
|
+
const dataAccess = this.formatDataAccess(routine.dataAccess);
|
|
342
|
+
const security = routine.security === 'definer'
|
|
343
|
+
? ' sql security definer'
|
|
344
|
+
: routine.security === 'invoker'
|
|
345
|
+
? ' sql security invoker'
|
|
346
|
+
: '';
|
|
347
|
+
const comment = routine.comment ? ` comment ${this.platform.quoteValue(routine.comment)}` : '';
|
|
348
|
+
const body = this.wrapRoutineBody(routine.body ?? '');
|
|
349
|
+
if (routine.type === 'procedure') {
|
|
350
|
+
return `create procedure ${name}(${params})${determinism}${dataAccess}${security}${comment} ${body}`;
|
|
351
|
+
}
|
|
352
|
+
const returnType = routine.returns?.type ?? 'text';
|
|
353
|
+
return `create function ${name}(${params}) returns ${returnType}${determinism}${dataAccess}${security}${comment} ${body}`;
|
|
354
|
+
}
|
|
355
|
+
dropRoutine(routine) {
|
|
356
|
+
const kind = routine.type === 'procedure' ? 'procedure' : 'function';
|
|
357
|
+
return `drop ${kind} if exists ${this.platform.quoteIdentifier(routine.name)}`;
|
|
358
|
+
}
|
|
359
|
+
async getAllRoutines(connection) {
|
|
360
|
+
const sql = `
|
|
361
|
+
select
|
|
362
|
+
r.routine_name as name,
|
|
363
|
+
r.routine_schema as schema_name,
|
|
364
|
+
lower(r.routine_type) as type,
|
|
365
|
+
r.routine_definition as body,
|
|
366
|
+
r.dtd_identifier as return_type,
|
|
367
|
+
r.is_deterministic as is_deterministic,
|
|
368
|
+
r.sql_data_access as sql_data_access,
|
|
369
|
+
r.security_type as security_type,
|
|
370
|
+
r.routine_comment as comment
|
|
371
|
+
from information_schema.routines r
|
|
372
|
+
where r.routine_schema = database()
|
|
373
|
+
and r.routine_type in ('PROCEDURE', 'FUNCTION')
|
|
374
|
+
`;
|
|
375
|
+
const [rows, params] = await Promise.all([
|
|
376
|
+
connection.execute(sql),
|
|
377
|
+
this.getAllRoutineParams(connection),
|
|
378
|
+
]);
|
|
379
|
+
return rows.map(row => ({
|
|
380
|
+
name: row.name,
|
|
381
|
+
type: row.type,
|
|
382
|
+
// MySQL has no schema namespace for routines — undefined matches the metadata side.
|
|
383
|
+
schema: undefined,
|
|
384
|
+
body: this.stripRoutineBody(row.body ?? ''),
|
|
385
|
+
deterministic: row.is_deterministic === 'YES',
|
|
386
|
+
dataAccess: this.parseDataAccess(row.sql_data_access),
|
|
387
|
+
security: row.security_type === 'DEFINER' ? 'definer' : 'invoker',
|
|
388
|
+
comment: row.comment || undefined,
|
|
389
|
+
params: params.get(row.name) ?? [],
|
|
390
|
+
returns: row.type === 'function' && row.return_type ? { type: row.return_type } : undefined,
|
|
391
|
+
}));
|
|
392
|
+
}
|
|
393
|
+
async getAllRoutineParams(connection) {
|
|
394
|
+
const sql = `
|
|
395
|
+
select
|
|
396
|
+
specific_name as routine_name,
|
|
397
|
+
parameter_name as name,
|
|
398
|
+
parameter_mode as direction,
|
|
399
|
+
dtd_identifier as type,
|
|
400
|
+
ordinal_position as position
|
|
401
|
+
from information_schema.parameters
|
|
402
|
+
where specific_schema = database()
|
|
403
|
+
and parameter_name is not null
|
|
404
|
+
order by specific_name, ordinal_position
|
|
405
|
+
`;
|
|
406
|
+
const rows = await connection.execute(sql);
|
|
407
|
+
const out = new Map();
|
|
408
|
+
for (const row of rows) {
|
|
409
|
+
if (!out.has(row.routine_name)) {
|
|
410
|
+
out.set(row.routine_name, []);
|
|
411
|
+
}
|
|
412
|
+
out.get(row.routine_name).push({
|
|
413
|
+
name: row.name,
|
|
414
|
+
type: row.type,
|
|
415
|
+
direction: row.direction.toLowerCase(),
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
return out;
|
|
419
|
+
}
|
|
420
|
+
formatDataAccess(access) {
|
|
421
|
+
switch (access) {
|
|
422
|
+
case 'no-sql':
|
|
423
|
+
return ' no sql';
|
|
424
|
+
case 'reads-sql-data':
|
|
425
|
+
return ' reads sql data';
|
|
426
|
+
case 'modifies-sql-data':
|
|
427
|
+
return ' modifies sql data';
|
|
428
|
+
case 'contains-sql':
|
|
429
|
+
return ' contains sql';
|
|
430
|
+
default:
|
|
431
|
+
return '';
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
parseDataAccess(access) {
|
|
435
|
+
return access.toLowerCase().replace(/\s+/g, '-');
|
|
436
|
+
}
|
|
437
|
+
async getAllTriggers(connection, tables) {
|
|
438
|
+
const names = tables.map(t => this.platform.quoteValue(t.table_name)).join(', ');
|
|
439
|
+
const sql = `select trigger_name as trigger_name, event_object_table as table_name, nullif(event_object_schema, schema()) as schema_name,
|
|
440
|
+
event_manipulation as event, action_timing as timing,
|
|
441
|
+
action_orientation as for_each, action_statement as body
|
|
442
|
+
from information_schema.triggers
|
|
443
|
+
where event_object_schema = database()
|
|
444
|
+
and event_object_table in (${names})
|
|
445
|
+
order by trigger_name, event_manipulation`;
|
|
446
|
+
const allTriggers = await connection.execute(sql);
|
|
447
|
+
const ret = {};
|
|
448
|
+
// First pass: collect all raw trigger names per table to detect multi-event groups.
|
|
449
|
+
// A base name is only used for grouping if multiple triggers share it (e.g. trg_multi_insert + trg_multi_update).
|
|
450
|
+
const namesByTable = new Map();
|
|
451
|
+
for (const row of allTriggers) {
|
|
452
|
+
const key = this.getTableKey(row);
|
|
453
|
+
namesByTable.set(key, [...(namesByTable.get(key) ?? []), row.trigger_name]);
|
|
454
|
+
}
|
|
455
|
+
const triggerMap = new Map();
|
|
456
|
+
for (const row of allTriggers) {
|
|
457
|
+
const key = this.getTableKey(row);
|
|
458
|
+
const eventLower = row.event.toLowerCase();
|
|
459
|
+
const tableNames = namesByTable.get(key) ?? [];
|
|
460
|
+
// Only strip event suffix when another trigger with the same base exists for this table
|
|
461
|
+
const candidateBase = row.trigger_name.endsWith(`_${eventLower}`)
|
|
462
|
+
? row.trigger_name.slice(0, -eventLower.length - 1)
|
|
463
|
+
: null;
|
|
464
|
+
const baseName = candidateBase && tableNames.some(n => n !== row.trigger_name && n.startsWith(`${candidateBase}_`))
|
|
465
|
+
? candidateBase
|
|
466
|
+
: row.trigger_name;
|
|
467
|
+
const dedupeKey = `${key}:${baseName}`;
|
|
468
|
+
if (triggerMap.has(dedupeKey)) {
|
|
469
|
+
const existing = triggerMap.get(dedupeKey);
|
|
470
|
+
const event = eventLower;
|
|
471
|
+
if (!existing.events.includes(event)) {
|
|
472
|
+
existing.events.push(event);
|
|
473
|
+
}
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
ret[key] ??= [];
|
|
477
|
+
// Strip BEGIN/END wrapper from MySQL action_statement
|
|
478
|
+
let body = row.body ?? '';
|
|
479
|
+
const beginEndMatch = /^\s*begin\s+([\s\S]*)\s*end\s*$/i.exec(body);
|
|
480
|
+
if (beginEndMatch) {
|
|
481
|
+
body = beginEndMatch[1].trim().replace(/;\s*$/, '');
|
|
482
|
+
}
|
|
483
|
+
const trigger = {
|
|
484
|
+
name: baseName,
|
|
485
|
+
timing: row.timing.toLowerCase(),
|
|
486
|
+
events: [eventLower],
|
|
487
|
+
forEach: (row.for_each ?? 'row').toLowerCase(),
|
|
488
|
+
body,
|
|
489
|
+
};
|
|
490
|
+
ret[key].push(trigger);
|
|
491
|
+
triggerMap.set(dedupeKey, trigger);
|
|
492
|
+
}
|
|
493
|
+
return ret;
|
|
494
|
+
}
|
|
267
495
|
async getAllForeignKeys(connection, tables, ctx) {
|
|
268
496
|
const sql = `select k.constraint_name as constraint_name, nullif(k.table_schema, schema()) as schema_name, k.table_name as table_name, k.column_name as column_name, k.referenced_table_name as referenced_table_name, k.referenced_column_name as referenced_column_name, c.update_rule as update_rule, c.delete_rule as delete_rule
|
|
269
497
|
from information_schema.key_column_usage k
|
|
@@ -319,10 +547,13 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
319
547
|
const col = this.createTableColumn(column, table, changedProperties);
|
|
320
548
|
return [`alter table ${table.getQuotedName()} modify ${col}`];
|
|
321
549
|
}
|
|
550
|
+
// MySQL MODIFY/CHANGE resets omitted column attributes to the table default, so collation must
|
|
551
|
+
// be re-emitted on rename and comment-only paths to preserve a non-default column collation.
|
|
322
552
|
getColumnDeclarationSQL(col) {
|
|
323
553
|
let ret = col.type;
|
|
324
554
|
ret += col.unsigned ? ' unsigned' : '';
|
|
325
555
|
ret += col.autoincrement ? ' auto_increment' : '';
|
|
556
|
+
ret += col.collation ? ` ${this.getCollateSQL(col.collation)}` : '';
|
|
326
557
|
ret += ' ';
|
|
327
558
|
ret += col.nullable ? 'null' : 'not null';
|
|
328
559
|
ret += col.default ? ' default ' + col.default : '';
|
|
@@ -46,7 +46,7 @@ declare class OracleConnection implements DatabaseConnection {
|
|
|
46
46
|
sql: string;
|
|
47
47
|
bindParams: unknown[];
|
|
48
48
|
};
|
|
49
|
-
streamQuery<R>(compiledQuery: CompiledQuery,
|
|
49
|
+
streamQuery<R>(compiledQuery: CompiledQuery, chunkSize?: number): AsyncIterableIterator<QueryResult<R>>;
|
|
50
50
|
get connection(): OraclePoolConnection;
|
|
51
51
|
}
|
|
52
52
|
declare class OracleDriver implements Driver {
|
|
@@ -66,13 +66,14 @@ class OracleConnection {
|
|
|
66
66
|
bindParams: query.parameters,
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
|
-
async *streamQuery(compiledQuery,
|
|
69
|
+
async *streamQuery(compiledQuery, chunkSize) {
|
|
70
70
|
const { sql, bindParams } = this.formatQuery(compiledQuery);
|
|
71
71
|
const result = await this.#connection.execute(sql, bindParams, {
|
|
72
72
|
resultSet: true,
|
|
73
73
|
autoCommit: compiledQuery.autoCommit,
|
|
74
74
|
outFormat: OUT_FORMAT_OBJECT,
|
|
75
75
|
...this.#executeOptions,
|
|
76
|
+
...(chunkSize != null ? { fetchArraySize: chunkSize } : {}),
|
|
76
77
|
});
|
|
77
78
|
const rs = result.resultSet;
|
|
78
79
|
try {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type EntityName } from '@mikro-orm/core';
|
|
2
|
+
import { SqlEntityManager } from '../../SqlEntityManager.js';
|
|
3
|
+
import type { AbstractSqlDriver } from '../../AbstractSqlDriver.js';
|
|
4
|
+
/**
|
|
5
|
+
* Shared base class for PostgreSQL-flavoured entity managers (`pg`, `pglite`).
|
|
6
|
+
* Adds Postgres-only helpers on top of `SqlEntityManager`.
|
|
7
|
+
*/
|
|
8
|
+
export declare class BasePostgreSqlEntityManager<Driver extends AbstractSqlDriver = AbstractSqlDriver> extends SqlEntityManager<Driver> {
|
|
9
|
+
/**
|
|
10
|
+
* Refreshes a materialized view.
|
|
11
|
+
*
|
|
12
|
+
* @param entityName - The entity name or class of the materialized view
|
|
13
|
+
* @param options - Optional settings
|
|
14
|
+
* @param options.concurrently - If true, refreshes the view concurrently (requires a unique index on the view)
|
|
15
|
+
*/
|
|
16
|
+
refreshMaterializedView<Entity extends object>(entityName: EntityName<Entity>, options?: {
|
|
17
|
+
concurrently?: boolean;
|
|
18
|
+
}): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { SqlEntityManager } from '../../SqlEntityManager.js';
|
|
2
|
+
/**
|
|
3
|
+
* Shared base class for PostgreSQL-flavoured entity managers (`pg`, `pglite`).
|
|
4
|
+
* Adds Postgres-only helpers on top of `SqlEntityManager`.
|
|
5
|
+
*/
|
|
6
|
+
export class BasePostgreSqlEntityManager extends SqlEntityManager {
|
|
7
|
+
/**
|
|
8
|
+
* Refreshes a materialized view.
|
|
9
|
+
*
|
|
10
|
+
* @param entityName - The entity name or class of the materialized view
|
|
11
|
+
* @param options - Optional settings
|
|
12
|
+
* @param options.concurrently - If true, refreshes the view concurrently (requires a unique index on the view)
|
|
13
|
+
*/
|
|
14
|
+
async refreshMaterializedView(entityName, options) {
|
|
15
|
+
const meta = this.getMetadata(entityName);
|
|
16
|
+
if (!meta.view || !meta.materialized) {
|
|
17
|
+
throw new Error(`Entity ${meta.className} is not a materialized view`);
|
|
18
|
+
}
|
|
19
|
+
const helper = this.getDriver().getPlatform().getSchemaHelper();
|
|
20
|
+
const schema = meta.schema ?? this.config.get('schema');
|
|
21
|
+
const sql = helper.refreshMaterializedView(meta.tableName, schema, options?.concurrently);
|
|
22
|
+
await this.execute(sql);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -15,6 +15,7 @@ export declare class BasePostgreSqlPlatform extends AbstractSqlPlatform {
|
|
|
15
15
|
usesEnumCheckConstraints(): boolean;
|
|
16
16
|
getEnumArrayCheckConstraintExpression(column: string, items: string[]): string;
|
|
17
17
|
supportsMaterializedViews(): boolean;
|
|
18
|
+
supportsPartitionedTables(): boolean;
|
|
18
19
|
supportsCustomPrimaryKeyNames(): boolean;
|
|
19
20
|
getCurrentTimestampSQL(length: number): string;
|
|
20
21
|
getDateTimeTypeDeclarationSQL(column: {
|
|
@@ -46,6 +47,7 @@ export declare class BasePostgreSqlPlatform extends AbstractSqlPlatform {
|
|
|
46
47
|
precision?: number;
|
|
47
48
|
scale?: number;
|
|
48
49
|
autoincrement?: boolean;
|
|
50
|
+
columnTypes?: string[];
|
|
49
51
|
}): string;
|
|
50
52
|
getMappedType(type: string): Type<unknown>;
|
|
51
53
|
getRegExpOperator(val?: unknown, flags?: string): string;
|
|
@@ -69,6 +71,12 @@ export declare class BasePostgreSqlPlatform extends AbstractSqlPlatform {
|
|
|
69
71
|
}): string[];
|
|
70
72
|
marshallArray(values: string[]): string;
|
|
71
73
|
unmarshallArray(value: string): string[];
|
|
74
|
+
escape(value: any): string;
|
|
75
|
+
/**
|
|
76
|
+
* Ported from PostgreSQL 9.2.4 source code (`src/interfaces/libpq/fe-exec.c`),
|
|
77
|
+
* matching `pg.Client.prototype.escapeLiteral` so we don't need a `pg` dep here.
|
|
78
|
+
*/
|
|
79
|
+
private escapeLiteral;
|
|
72
80
|
getVarcharTypeDeclarationSQL(column: {
|
|
73
81
|
length?: number;
|
|
74
82
|
}): string;
|
|
@@ -92,11 +100,8 @@ export declare class BasePostgreSqlPlatform extends AbstractSqlPlatform {
|
|
|
92
100
|
getDefaultMappedType(type: string): Type<unknown>;
|
|
93
101
|
supportsSchemas(): boolean;
|
|
94
102
|
getDefaultSchemaName(): string | undefined;
|
|
95
|
-
/**
|
|
96
|
-
|
|
97
|
-
* cannot go past 63 character length for identifiers in MySQL
|
|
98
|
-
*/
|
|
99
|
-
getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
|
|
103
|
+
/** Postgres identifier limit (NAMEDATALEN - 1). */
|
|
104
|
+
getMaxIdentifierLength(): number;
|
|
100
105
|
getDefaultPrimaryName(tableName: string, columns: string[]): string;
|
|
101
106
|
/**
|
|
102
107
|
* @inheritDoc
|
|
@@ -110,4 +115,5 @@ export declare class BasePostgreSqlPlatform extends AbstractSqlPlatform {
|
|
|
110
115
|
}[]): string;
|
|
111
116
|
getJsonArrayElementPropertySQL(alias: string, property: string, type: string): string;
|
|
112
117
|
getDefaultClientUrl(): string;
|
|
118
|
+
caseInsensitiveCollationNames(): boolean;
|
|
113
119
|
}
|