@mikro-orm/sql 7.0.7-dev.11 → 7.0.7-dev.13
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/dialects/mysql/MySqlSchemaHelper.d.ts +8 -8
- package/dialects/mysql/MySqlSchemaHelper.js +22 -22
- package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +10 -10
- package/dialects/postgresql/PostgreSqlSchemaHelper.js +23 -23
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +6 -6
- package/dialects/sqlite/SqliteSchemaHelper.js +38 -38
- package/package.json +2 -2
- package/query/QueryBuilderHelper.js +1 -1
- package/schema/DatabaseSchema.d.ts +2 -2
- package/schema/DatabaseSchema.js +5 -5
- package/schema/SchemaHelper.d.ts +6 -6
- package/schema/SchemaHelper.js +6 -6
- package/typings.d.ts +5 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Dictionary, type Type } from '@mikro-orm/core';
|
|
1
|
+
import { type Dictionary, type Transaction, type Type } from '@mikro-orm/core';
|
|
2
2
|
import type { CheckDef, Column, ForeignKey, IndexDef, Table, TableDifference } from '../../typings.js';
|
|
3
3
|
import type { AbstractSqlConnection } from '../../AbstractSqlConnection.js';
|
|
4
4
|
import { SchemaHelper } from '../../schema/SchemaHelper.js';
|
|
@@ -17,9 +17,9 @@ export declare class MySqlSchemaHelper extends SchemaHelper {
|
|
|
17
17
|
finalizeTable(table: DatabaseTable, charset: string, collate?: string): string;
|
|
18
18
|
getListTablesSQL(): string;
|
|
19
19
|
getListViewsSQL(): string;
|
|
20
|
-
loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string): Promise<void>;
|
|
21
|
-
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[]): Promise<void>;
|
|
22
|
-
getAllIndexes(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<IndexDef[]>>;
|
|
20
|
+
loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string, ctx?: Transaction): Promise<void>;
|
|
21
|
+
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[], ctx?: Transaction): Promise<void>;
|
|
22
|
+
getAllIndexes(connection: AbstractSqlConnection, tables: Table[], ctx?: Transaction): Promise<Dictionary<IndexDef[]>>;
|
|
23
23
|
getCreateIndexSQL(tableName: string, index: IndexDef, partialExpression?: boolean): string;
|
|
24
24
|
/**
|
|
25
25
|
* Build the column list for a MySQL index, with MySQL-specific handling for collation.
|
|
@@ -30,16 +30,16 @@ export declare class MySqlSchemaHelper extends SchemaHelper {
|
|
|
30
30
|
* Append MySQL-specific index suffixes like INVISIBLE.
|
|
31
31
|
*/
|
|
32
32
|
protected appendMySqlIndexSuffix(sql: string, index: IndexDef): string;
|
|
33
|
-
getAllColumns(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Column[]>>;
|
|
34
|
-
getAllChecks(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<CheckDef[]>>;
|
|
35
|
-
getAllForeignKeys(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<ForeignKey>>>;
|
|
33
|
+
getAllColumns(connection: AbstractSqlConnection, tables: Table[], ctx?: Transaction): Promise<Dictionary<Column[]>>;
|
|
34
|
+
getAllChecks(connection: AbstractSqlConnection, tables: Table[], ctx?: Transaction): Promise<Dictionary<CheckDef[]>>;
|
|
35
|
+
getAllForeignKeys(connection: AbstractSqlConnection, tables: Table[], ctx?: Transaction): Promise<Dictionary<Dictionary<ForeignKey>>>;
|
|
36
36
|
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string[];
|
|
37
37
|
getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column): string;
|
|
38
38
|
getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string[];
|
|
39
39
|
getChangeColumnCommentSQL(tableName: string, to: Column, schemaName?: string): string;
|
|
40
40
|
alterTableColumn(column: Column, table: DatabaseTable, changedProperties: Set<string>): string[];
|
|
41
41
|
private getColumnDeclarationSQL;
|
|
42
|
-
getAllEnumDefinitions(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<string[]>>>;
|
|
42
|
+
getAllEnumDefinitions(connection: AbstractSqlConnection, tables: Table[], ctx?: Transaction): Promise<Dictionary<Dictionary<string[]>>>;
|
|
43
43
|
private supportsCheckConstraints;
|
|
44
44
|
protected getChecksSQL(tables: Table[]): string;
|
|
45
45
|
normalizeDefaultValue(defaultValue: string, length: number): string | number;
|
|
@@ -36,14 +36,14 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
36
36
|
getListViewsSQL() {
|
|
37
37
|
return `select table_name as view_name, nullif(table_schema, schema()) as schema_name, view_definition from information_schema.views where table_schema = schema()`;
|
|
38
38
|
}
|
|
39
|
-
async loadViews(schema, connection, schemaName) {
|
|
40
|
-
const views = await connection.execute(this.getListViewsSQL());
|
|
39
|
+
async loadViews(schema, connection, schemaName, ctx) {
|
|
40
|
+
const views = await connection.execute(this.getListViewsSQL(), [], 'all', ctx);
|
|
41
41
|
for (const view of views) {
|
|
42
42
|
// MySQL information_schema.views.view_definition requires SHOW VIEW privilege
|
|
43
43
|
// and may return NULL. Use SHOW CREATE VIEW as fallback.
|
|
44
44
|
let definition = view.view_definition?.trim();
|
|
45
45
|
if (!definition) {
|
|
46
|
-
const createView = await connection.execute(`show create view \`${view.view_name}
|
|
46
|
+
const createView = await connection.execute(`show create view \`${view.view_name}\``, [], 'all', ctx);
|
|
47
47
|
if (createView[0]?.['Create View']) {
|
|
48
48
|
// Extract SELECT statement from CREATE VIEW ... AS SELECT ...
|
|
49
49
|
const match = /\bAS\s+(.+)$/is.exec(createView[0]['Create View']);
|
|
@@ -55,15 +55,15 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
async loadInformationSchema(schema, connection, tables) {
|
|
58
|
+
async loadInformationSchema(schema, connection, tables, schemas, ctx) {
|
|
59
59
|
if (tables.length === 0) {
|
|
60
60
|
return;
|
|
61
61
|
}
|
|
62
|
-
const columns = await this.getAllColumns(connection, tables);
|
|
63
|
-
const indexes = await this.getAllIndexes(connection, tables);
|
|
64
|
-
const checks = await this.getAllChecks(connection, tables);
|
|
65
|
-
const fks = await this.getAllForeignKeys(connection, tables);
|
|
66
|
-
const enums = await this.getAllEnumDefinitions(connection, tables);
|
|
62
|
+
const columns = await this.getAllColumns(connection, tables, ctx);
|
|
63
|
+
const indexes = await this.getAllIndexes(connection, tables, ctx);
|
|
64
|
+
const checks = await this.getAllChecks(connection, tables, ctx);
|
|
65
|
+
const fks = await this.getAllForeignKeys(connection, tables, ctx);
|
|
66
|
+
const enums = await this.getAllEnumDefinitions(connection, tables, ctx);
|
|
67
67
|
for (const t of tables) {
|
|
68
68
|
const key = this.getTableKey(t);
|
|
69
69
|
const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
|
|
@@ -71,12 +71,12 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
71
71
|
table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums[key]);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
async getAllIndexes(connection, tables) {
|
|
74
|
+
async getAllIndexes(connection, tables, ctx) {
|
|
75
75
|
const sql = `select table_name as table_name, nullif(table_schema, schema()) as schema_name, index_name as index_name, non_unique as non_unique, column_name as column_name, index_type as index_type, sub_part as sub_part, collation as sort_order /*!80013 , expression as expression, is_visible as is_visible */
|
|
76
76
|
from information_schema.statistics where table_schema = database()
|
|
77
77
|
and table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(', ')})
|
|
78
78
|
order by schema_name, table_name, index_name, seq_in_index`;
|
|
79
|
-
const allIndexes = await connection.execute(sql);
|
|
79
|
+
const allIndexes = await connection.execute(sql, [], 'all', ctx);
|
|
80
80
|
const ret = {};
|
|
81
81
|
for (const index of allIndexes) {
|
|
82
82
|
const key = this.getTableKey(index);
|
|
@@ -191,7 +191,7 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
191
191
|
}
|
|
192
192
|
return sql;
|
|
193
193
|
}
|
|
194
|
-
async getAllColumns(connection, tables) {
|
|
194
|
+
async getAllColumns(connection, tables, ctx) {
|
|
195
195
|
const sql = `select table_name as table_name,
|
|
196
196
|
nullif(table_schema, schema()) as schema_name,
|
|
197
197
|
column_name as column_name,
|
|
@@ -208,7 +208,7 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
208
208
|
ifnull(datetime_precision, character_maximum_length) length
|
|
209
209
|
from information_schema.columns where table_schema = database() and table_name in (${tables.map(t => this.platform.quoteValue(t.table_name))})
|
|
210
210
|
order by ordinal_position`;
|
|
211
|
-
const allColumns = await connection.execute(sql);
|
|
211
|
+
const allColumns = await connection.execute(sql, [], 'all', ctx);
|
|
212
212
|
const str = (val) => (val != null ? '' + val : val);
|
|
213
213
|
const extra = (val) => val.replace(/auto_increment|default_generated|(stored|virtual) generated/i, '').trim() || undefined;
|
|
214
214
|
const ret = {};
|
|
@@ -244,13 +244,13 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
244
244
|
}
|
|
245
245
|
return ret;
|
|
246
246
|
}
|
|
247
|
-
async getAllChecks(connection, tables) {
|
|
247
|
+
async getAllChecks(connection, tables, ctx) {
|
|
248
248
|
/* v8 ignore next */
|
|
249
|
-
if (!(await this.supportsCheckConstraints(connection))) {
|
|
249
|
+
if (!(await this.supportsCheckConstraints(connection, ctx))) {
|
|
250
250
|
return {};
|
|
251
251
|
}
|
|
252
252
|
const sql = this.getChecksSQL(tables);
|
|
253
|
-
const allChecks = await connection.execute(sql);
|
|
253
|
+
const allChecks = await connection.execute(sql, [], 'all', ctx);
|
|
254
254
|
const ret = {};
|
|
255
255
|
for (const check of allChecks) {
|
|
256
256
|
const key = this.getTableKey(check);
|
|
@@ -264,14 +264,14 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
264
264
|
}
|
|
265
265
|
return ret;
|
|
266
266
|
}
|
|
267
|
-
async getAllForeignKeys(connection, tables) {
|
|
267
|
+
async getAllForeignKeys(connection, tables, ctx) {
|
|
268
268
|
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
269
|
from information_schema.key_column_usage k
|
|
270
270
|
inner join information_schema.referential_constraints c on c.constraint_name = k.constraint_name and c.table_name = k.table_name
|
|
271
271
|
where k.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(', ')})
|
|
272
272
|
and k.table_schema = database() and c.constraint_schema = database() and k.referenced_column_name is not null
|
|
273
273
|
order by constraint_name, k.ordinal_position`;
|
|
274
|
-
const allFks = await connection.execute(sql);
|
|
274
|
+
const allFks = await connection.execute(sql, [], 'all', ctx);
|
|
275
275
|
const ret = {};
|
|
276
276
|
for (const fk of allFks) {
|
|
277
277
|
const key = this.getTableKey(fk);
|
|
@@ -329,11 +329,11 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
329
329
|
ret += col.comment ? ` comment ${this.platform.quoteValue(col.comment)}` : '';
|
|
330
330
|
return ret;
|
|
331
331
|
}
|
|
332
|
-
async getAllEnumDefinitions(connection, tables) {
|
|
332
|
+
async getAllEnumDefinitions(connection, tables, ctx) {
|
|
333
333
|
const sql = `select column_name as column_name, column_type as column_type, table_name as table_name
|
|
334
334
|
from information_schema.columns
|
|
335
335
|
where data_type = 'enum' and table_name in (${tables.map(t => `'${t.table_name}'`).join(', ')}) and table_schema = database()`;
|
|
336
|
-
const enums = await connection.execute(sql);
|
|
336
|
+
const enums = await connection.execute(sql, [], 'all', ctx);
|
|
337
337
|
return enums.reduce((o, item) => {
|
|
338
338
|
o[item.table_name] ??= {};
|
|
339
339
|
o[item.table_name][item.column_name] = item.column_type
|
|
@@ -343,12 +343,12 @@ export class MySqlSchemaHelper extends SchemaHelper {
|
|
|
343
343
|
return o;
|
|
344
344
|
}, {});
|
|
345
345
|
}
|
|
346
|
-
async supportsCheckConstraints(connection) {
|
|
346
|
+
async supportsCheckConstraints(connection, ctx) {
|
|
347
347
|
if (this.#cache.supportsCheckConstraints != null) {
|
|
348
348
|
return this.#cache.supportsCheckConstraints;
|
|
349
349
|
}
|
|
350
350
|
const sql = `select 1 from information_schema.tables where table_name = 'CHECK_CONSTRAINTS' and table_schema = 'information_schema'`;
|
|
351
|
-
const res = await connection.execute(sql);
|
|
351
|
+
const res = await connection.execute(sql, [], 'all', ctx);
|
|
352
352
|
return (this.#cache.supportsCheckConstraints = res.length > 0);
|
|
353
353
|
}
|
|
354
354
|
getChecksSQL(tables) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Dictionary } from '@mikro-orm/core';
|
|
1
|
+
import { type Dictionary, type Transaction } from '@mikro-orm/core';
|
|
2
2
|
import { SchemaHelper } from '../../schema/SchemaHelper.js';
|
|
3
3
|
import type { AbstractSqlConnection } from '../../AbstractSqlConnection.js';
|
|
4
4
|
import type { CheckDef, Column, ForeignKey, IndexDef, Table, TableDifference } from '../../typings.js';
|
|
@@ -19,16 +19,16 @@ export declare class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
19
19
|
getListTablesSQL(): string;
|
|
20
20
|
private getIgnoredViewsCondition;
|
|
21
21
|
getListViewsSQL(): string;
|
|
22
|
-
loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection): Promise<void>;
|
|
22
|
+
loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string, ctx?: Transaction): Promise<void>;
|
|
23
23
|
getListMaterializedViewsSQL(): string;
|
|
24
|
-
loadMaterializedViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string): Promise<void>;
|
|
24
|
+
loadMaterializedViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string, ctx?: Transaction): Promise<void>;
|
|
25
25
|
createMaterializedView(name: string, schema: string | undefined, definition: string, withData?: boolean): string;
|
|
26
26
|
dropMaterializedViewIfExists(name: string, schema?: string): string;
|
|
27
27
|
refreshMaterializedView(name: string, schema?: string, concurrently?: boolean): string;
|
|
28
|
-
getNamespaces(connection: AbstractSqlConnection): Promise<string[]>;
|
|
28
|
+
getNamespaces(connection: AbstractSqlConnection, ctx?: Transaction): Promise<string[]>;
|
|
29
29
|
private getIgnoredNamespacesConditionSQL;
|
|
30
|
-
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[]): Promise<void>;
|
|
31
|
-
getAllIndexes(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<IndexDef[]>>;
|
|
30
|
+
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[], ctx?: Transaction): Promise<void>;
|
|
31
|
+
getAllIndexes(connection: AbstractSqlConnection, tables: Table[], ctx?: Transaction): Promise<Dictionary<IndexDef[]>>;
|
|
32
32
|
/**
|
|
33
33
|
* Parses column definitions from the full CREATE INDEX expression.
|
|
34
34
|
* Since pg_get_indexdef(oid, col_num, true) doesn't include sort modifiers,
|
|
@@ -47,10 +47,10 @@ export declare class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
47
47
|
name: string;
|
|
48
48
|
schema?: string;
|
|
49
49
|
items: string[];
|
|
50
|
-
}
|
|
51
|
-
getAllChecks(connection: AbstractSqlConnection, tablesBySchemas: Map<string | undefined, Table[]
|
|
52
|
-
getAllForeignKeys(connection: AbstractSqlConnection, tablesBySchemas: Map<string | undefined, Table[]
|
|
53
|
-
getNativeEnumDefinitions(connection: AbstractSqlConnection, schemas: string[]): Promise<Dictionary<{
|
|
50
|
+
}>, ctx?: Transaction): Promise<Dictionary<Column[]>>;
|
|
51
|
+
getAllChecks(connection: AbstractSqlConnection, tablesBySchemas: Map<string | undefined, Table[]>, ctx?: Transaction): Promise<Dictionary<CheckDef[]>>;
|
|
52
|
+
getAllForeignKeys(connection: AbstractSqlConnection, tablesBySchemas: Map<string | undefined, Table[]>, ctx?: Transaction): Promise<Dictionary<Dictionary<ForeignKey>>>;
|
|
53
|
+
getNativeEnumDefinitions(connection: AbstractSqlConnection, schemas: string[], ctx?: Transaction): Promise<Dictionary<{
|
|
54
54
|
name: string;
|
|
55
55
|
schema?: string;
|
|
56
56
|
items: string[];
|
|
@@ -41,8 +41,8 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
41
41
|
`and ${this.getIgnoredViewsCondition()} ` +
|
|
42
42
|
`order by table_name`);
|
|
43
43
|
}
|
|
44
|
-
async loadViews(schema, connection) {
|
|
45
|
-
const views = await connection.execute(this.getListViewsSQL());
|
|
44
|
+
async loadViews(schema, connection, schemaName, ctx) {
|
|
45
|
+
const views = await connection.execute(this.getListViewsSQL(), [], 'all', ctx);
|
|
46
46
|
for (const view of views) {
|
|
47
47
|
const definition = view.view_definition?.trim().replace(/;$/, '') ?? '';
|
|
48
48
|
if (definition) {
|
|
@@ -56,13 +56,13 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
56
56
|
`where ${this.getIgnoredNamespacesConditionSQL('schemaname')} ` +
|
|
57
57
|
`order by matviewname`);
|
|
58
58
|
}
|
|
59
|
-
async loadMaterializedViews(schema, connection, schemaName) {
|
|
60
|
-
const views = await connection.execute(this.getListMaterializedViewsSQL());
|
|
59
|
+
async loadMaterializedViews(schema, connection, schemaName, ctx) {
|
|
60
|
+
const views = await connection.execute(this.getListMaterializedViewsSQL(), [], 'all', ctx);
|
|
61
61
|
if (views.length === 0) {
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
64
|
const tables = views.map(v => ({ table_name: v.view_name, schema_name: v.schema_name }));
|
|
65
|
-
const indexes = await this.getAllIndexes(connection, tables);
|
|
65
|
+
const indexes = await this.getAllIndexes(connection, tables, ctx);
|
|
66
66
|
for (let i = 0; i < views.length; i++) {
|
|
67
67
|
const definition = views[i].view_definition?.trim().replace(/;$/, '') ?? '';
|
|
68
68
|
if (definition) {
|
|
@@ -86,11 +86,11 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
86
86
|
const concurrent = concurrently ? ' concurrently' : '';
|
|
87
87
|
return `refresh materialized view${concurrent} ${this.quote(this.getTableName(name, schema))}`;
|
|
88
88
|
}
|
|
89
|
-
async getNamespaces(connection) {
|
|
89
|
+
async getNamespaces(connection, ctx) {
|
|
90
90
|
const sql = `select schema_name from information_schema.schemata ` +
|
|
91
91
|
`where ${this.getIgnoredNamespacesConditionSQL()} ` +
|
|
92
92
|
`order by schema_name`;
|
|
93
|
-
const res = await connection.execute(sql);
|
|
93
|
+
const res = await connection.execute(sql, [], 'all', ctx);
|
|
94
94
|
return res.map(row => row.schema_name);
|
|
95
95
|
}
|
|
96
96
|
getIgnoredNamespacesConditionSQL(column = 'schema_name') {
|
|
@@ -106,18 +106,18 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
106
106
|
const ignoredPrefixes = ['pg_', 'crdb_', '_timescaledb_'].map(p => `"${column}" not like '${p}%'`).join(' and ');
|
|
107
107
|
return `${ignoredPrefixes} and "${column}" not in (${ignored})`;
|
|
108
108
|
}
|
|
109
|
-
async loadInformationSchema(schema, connection, tables, schemas) {
|
|
109
|
+
async loadInformationSchema(schema, connection, tables, schemas, ctx) {
|
|
110
110
|
schemas ??= tables.length === 0 ? [schema.name] : tables.map(t => t.schema_name);
|
|
111
|
-
const nativeEnums = await this.getNativeEnumDefinitions(connection, schemas);
|
|
111
|
+
const nativeEnums = await this.getNativeEnumDefinitions(connection, schemas, ctx);
|
|
112
112
|
schema.setNativeEnums(nativeEnums);
|
|
113
113
|
if (tables.length === 0) {
|
|
114
114
|
return;
|
|
115
115
|
}
|
|
116
116
|
const tablesBySchema = this.getTablesGroupedBySchemas(tables);
|
|
117
|
-
const columns = await this.getAllColumns(connection, tablesBySchema, nativeEnums);
|
|
118
|
-
const indexes = await this.getAllIndexes(connection, tables);
|
|
119
|
-
const checks = await this.getAllChecks(connection, tablesBySchema);
|
|
120
|
-
const fks = await this.getAllForeignKeys(connection, tablesBySchema);
|
|
117
|
+
const columns = await this.getAllColumns(connection, tablesBySchema, nativeEnums, ctx);
|
|
118
|
+
const indexes = await this.getAllIndexes(connection, tables, ctx);
|
|
119
|
+
const checks = await this.getAllChecks(connection, tablesBySchema, ctx);
|
|
120
|
+
const fks = await this.getAllForeignKeys(connection, tablesBySchema, ctx);
|
|
121
121
|
for (const t of tables) {
|
|
122
122
|
const key = this.getTableKey(t);
|
|
123
123
|
const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
|
|
@@ -128,10 +128,10 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
|
-
async getAllIndexes(connection, tables) {
|
|
131
|
+
async getAllIndexes(connection, tables, ctx) {
|
|
132
132
|
const sql = this.getIndexesSQL(tables);
|
|
133
133
|
const unquote = (str) => str.replace(/['"`]/g, '');
|
|
134
|
-
const allIndexes = await connection.execute(sql);
|
|
134
|
+
const allIndexes = await connection.execute(sql, [], 'all', ctx);
|
|
135
135
|
const ret = {};
|
|
136
136
|
for (const index of allIndexes) {
|
|
137
137
|
const key = this.getTableKey(index);
|
|
@@ -252,7 +252,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
252
252
|
/* v8 ignore next - pg_get_indexdef always returns balanced parentheses */
|
|
253
253
|
return '';
|
|
254
254
|
}
|
|
255
|
-
async getAllColumns(connection, tablesBySchemas, nativeEnums) {
|
|
255
|
+
async getAllColumns(connection, tablesBySchemas, nativeEnums, ctx) {
|
|
256
256
|
const sql = `select table_schema as schema_name, table_name, column_name,
|
|
257
257
|
column_default,
|
|
258
258
|
is_nullable,
|
|
@@ -272,7 +272,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
272
272
|
join pg_attribute pga on pgc.oid = pga.attrelid and cols.column_name = pga.attname
|
|
273
273
|
where (${[...tablesBySchemas.entries()].map(([schema, tables]) => `(table_schema = ${this.platform.quoteValue(schema)} and table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}))`).join(' or ')})
|
|
274
274
|
order by ordinal_position`;
|
|
275
|
-
const allColumns = await connection.execute(sql);
|
|
275
|
+
const allColumns = await connection.execute(sql, [], 'all', ctx);
|
|
276
276
|
const str = (val) => (val != null ? '' + val : val);
|
|
277
277
|
const ret = {};
|
|
278
278
|
for (const col of allColumns) {
|
|
@@ -351,9 +351,9 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
351
351
|
}
|
|
352
352
|
return ret;
|
|
353
353
|
}
|
|
354
|
-
async getAllChecks(connection, tablesBySchemas) {
|
|
354
|
+
async getAllChecks(connection, tablesBySchemas, ctx) {
|
|
355
355
|
const sql = this.getChecksSQL(tablesBySchemas);
|
|
356
|
-
const allChecks = await connection.execute(sql);
|
|
356
|
+
const allChecks = await connection.execute(sql, [], 'all', ctx);
|
|
357
357
|
const ret = {};
|
|
358
358
|
const seen = new Set();
|
|
359
359
|
for (const check of allChecks) {
|
|
@@ -375,7 +375,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
375
375
|
}
|
|
376
376
|
return ret;
|
|
377
377
|
}
|
|
378
|
-
async getAllForeignKeys(connection, tablesBySchemas) {
|
|
378
|
+
async getAllForeignKeys(connection, tablesBySchemas, ctx) {
|
|
379
379
|
const sql = `select nsp1.nspname schema_name, cls1.relname table_name, nsp2.nspname referenced_schema_name,
|
|
380
380
|
cls2.relname referenced_table_name, a.attname column_name, af.attname referenced_column_name, conname constraint_name,
|
|
381
381
|
confupdtype update_rule, confdeltype delete_rule, array_position(con.conkey,a.attnum) as ord, condeferrable, condeferred,
|
|
@@ -390,7 +390,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
390
390
|
where (${[...tablesBySchemas.entries()].map(([schema, tables]) => `(cls1.relname in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}) and nsp1.nspname = ${this.platform.quoteValue(schema)})`).join(' or ')})
|
|
391
391
|
and confrelid > 0
|
|
392
392
|
order by nsp1.nspname, cls1.relname, constraint_name, ord`;
|
|
393
|
-
const allFks = await connection.execute(sql);
|
|
393
|
+
const allFks = await connection.execute(sql, [], 'all', ctx);
|
|
394
394
|
const ret = {};
|
|
395
395
|
function mapReferentialIntegrity(value, def) {
|
|
396
396
|
const match = ['n', 'd'].includes(value) && /ON DELETE (SET (NULL|DEFAULT) \(.*?\))/.exec(def);
|
|
@@ -428,14 +428,14 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
|
|
|
428
428
|
});
|
|
429
429
|
return ret;
|
|
430
430
|
}
|
|
431
|
-
async getNativeEnumDefinitions(connection, schemas) {
|
|
431
|
+
async getNativeEnumDefinitions(connection, schemas, ctx) {
|
|
432
432
|
const uniqueSchemas = Utils.unique(schemas);
|
|
433
433
|
const res = await connection.execute(`select t.typname as enum_name, n.nspname as schema_name, array_agg(e.enumlabel order by e.enumsortorder) as enum_value
|
|
434
434
|
from pg_type t
|
|
435
435
|
join pg_enum e on t.oid = e.enumtypid
|
|
436
436
|
join pg_catalog.pg_namespace n on n.oid = t.typnamespace
|
|
437
437
|
where n.nspname in (${Array(uniqueSchemas.length).fill('?').join(', ')})
|
|
438
|
-
group by t.typname, n.nspname`, uniqueSchemas);
|
|
438
|
+
group by t.typname, n.nspname`, uniqueSchemas, 'all', ctx);
|
|
439
439
|
return res.reduce((o, row) => {
|
|
440
440
|
let name = row.enum_name;
|
|
441
441
|
if (row.schema_name && row.schema_name !== this.platform.getDefaultSchemaName()) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Connection } from '@mikro-orm/core';
|
|
1
|
+
import { type Connection, type Transaction } from '@mikro-orm/core';
|
|
2
2
|
import type { AbstractSqlConnection } from '../../AbstractSqlConnection.js';
|
|
3
3
|
import { SchemaHelper } from '../../schema/SchemaHelper.js';
|
|
4
4
|
import type { Column, IndexDef, Table, TableDifference } from '../../typings.js';
|
|
@@ -11,13 +11,13 @@ export declare class SqliteSchemaHelper extends SchemaHelper {
|
|
|
11
11
|
getCreateNamespaceSQL(name: string): string;
|
|
12
12
|
getDropNamespaceSQL(name: string): string;
|
|
13
13
|
getListTablesSQL(): string;
|
|
14
|
-
getAllTables(connection: AbstractSqlConnection, schemas?: string[]): Promise<Table[]>;
|
|
15
|
-
getNamespaces(connection: AbstractSqlConnection): Promise<string[]>;
|
|
14
|
+
getAllTables(connection: AbstractSqlConnection, schemas?: string[], ctx?: Transaction): Promise<Table[]>;
|
|
15
|
+
getNamespaces(connection: AbstractSqlConnection, ctx?: Transaction): Promise<string[]>;
|
|
16
16
|
private getIgnoredViewsCondition;
|
|
17
17
|
getListViewsSQL(): string;
|
|
18
|
-
loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string): Promise<void>;
|
|
18
|
+
loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string, ctx?: Transaction): Promise<void>;
|
|
19
19
|
getDropDatabaseSQL(name: string): string;
|
|
20
|
-
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[]): Promise<void>;
|
|
20
|
+
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[], ctx?: Transaction): Promise<void>;
|
|
21
21
|
createTable(table: DatabaseTable, alter?: boolean): string[];
|
|
22
22
|
createTableColumn(column: Column, table: DatabaseTable, _changedProperties?: Set<string>): string | undefined;
|
|
23
23
|
getAddColumnsSQL(table: DatabaseTable, columns: Column[], diff?: TableDifference): string[];
|
|
@@ -45,7 +45,7 @@ export declare class SqliteSchemaHelper extends SchemaHelper {
|
|
|
45
45
|
*/
|
|
46
46
|
private wrapExpressionDefault;
|
|
47
47
|
private getEnumDefinitions;
|
|
48
|
-
getPrimaryKeys(connection: AbstractSqlConnection, indexes: IndexDef[], tableName: string, schemaName?: string): Promise<string[]>;
|
|
48
|
+
getPrimaryKeys(connection: AbstractSqlConnection, indexes: IndexDef[], tableName: string, schemaName?: string, ctx?: Transaction): Promise<string[]>;
|
|
49
49
|
private getIndexes;
|
|
50
50
|
private getChecks;
|
|
51
51
|
private getColumnDefinitions;
|
|
@@ -34,12 +34,12 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
34
34
|
return (`select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' ` +
|
|
35
35
|
`union all select name as table_name from sqlite_temp_master where type = 'table' order by name`);
|
|
36
36
|
}
|
|
37
|
-
async getAllTables(connection, schemas) {
|
|
38
|
-
const databases = await this.getDatabaseList(connection);
|
|
37
|
+
async getAllTables(connection, schemas, ctx) {
|
|
38
|
+
const databases = await this.getDatabaseList(connection, ctx);
|
|
39
39
|
const hasAttachedDbs = databases.length > 1; // More than just 'main'
|
|
40
40
|
// If no attached databases, use original behavior
|
|
41
41
|
if (!hasAttachedDbs && !schemas?.length) {
|
|
42
|
-
return connection.execute(this.getListTablesSQL());
|
|
42
|
+
return connection.execute(this.getListTablesSQL(), [], 'all', ctx);
|
|
43
43
|
}
|
|
44
44
|
// With attached databases, query each one
|
|
45
45
|
const targetSchemas = schemas?.length ? schemas : databases;
|
|
@@ -47,15 +47,15 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
47
47
|
for (const dbName of targetSchemas) {
|
|
48
48
|
const prefix = this.getSchemaPrefix(dbName);
|
|
49
49
|
const tables = await connection.execute(`select name from ${prefix}sqlite_master where type = 'table' ` +
|
|
50
|
-
`and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys'
|
|
50
|
+
`and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys'`, [], 'all', ctx);
|
|
51
51
|
for (const t of tables) {
|
|
52
52
|
allTables.push({ table_name: t.name, schema_name: dbName });
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
return allTables;
|
|
56
56
|
}
|
|
57
|
-
async getNamespaces(connection) {
|
|
58
|
-
return this.getDatabaseList(connection);
|
|
57
|
+
async getNamespaces(connection, ctx) {
|
|
58
|
+
return this.getDatabaseList(connection, ctx);
|
|
59
59
|
}
|
|
60
60
|
getIgnoredViewsCondition() {
|
|
61
61
|
return SPATIALITE_VIEWS.map(v => `name != '${v}'`).join(' and ');
|
|
@@ -63,12 +63,12 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
63
63
|
getListViewsSQL() {
|
|
64
64
|
return `select name as view_name, sql as view_definition from sqlite_master where type = 'view' and ${this.getIgnoredViewsCondition()} order by name`;
|
|
65
65
|
}
|
|
66
|
-
async loadViews(schema, connection, schemaName) {
|
|
67
|
-
const databases = await this.getDatabaseList(connection);
|
|
66
|
+
async loadViews(schema, connection, schemaName, ctx) {
|
|
67
|
+
const databases = await this.getDatabaseList(connection, ctx);
|
|
68
68
|
const hasAttachedDbs = databases.length > 1; // More than just 'main'
|
|
69
69
|
// If no attached databases and no specific schema, use original behavior
|
|
70
70
|
if (!hasAttachedDbs && !schemaName) {
|
|
71
|
-
const views = await connection.execute(this.getListViewsSQL());
|
|
71
|
+
const views = await connection.execute(this.getListViewsSQL(), [], 'all', ctx);
|
|
72
72
|
for (const view of views) {
|
|
73
73
|
schema.addView(view.view_name, schemaName, this.extractViewDefinition(view.view_definition));
|
|
74
74
|
}
|
|
@@ -79,7 +79,7 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
79
79
|
const targetDbs = schemaName ? [schemaName] : databases;
|
|
80
80
|
for (const dbName of targetDbs) {
|
|
81
81
|
const prefix = this.getSchemaPrefix(dbName);
|
|
82
|
-
const views = await connection.execute(`select name as view_name, sql as view_definition from ${prefix}sqlite_master where type = 'view' and ${this.getIgnoredViewsCondition()} order by name
|
|
82
|
+
const views = await connection.execute(`select name as view_name, sql as view_definition from ${prefix}sqlite_master where type = 'view' and ${this.getIgnoredViewsCondition()} order by name`, [], 'all', ctx);
|
|
83
83
|
for (const view of views) {
|
|
84
84
|
schema.addView(view.view_name, dbName, this.extractViewDefinition(view.view_definition));
|
|
85
85
|
}
|
|
@@ -92,15 +92,15 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
92
92
|
/* v8 ignore next */
|
|
93
93
|
return `drop database if exists ${this.quote(name)}`;
|
|
94
94
|
}
|
|
95
|
-
async loadInformationSchema(schema, connection, tables, schemas) {
|
|
95
|
+
async loadInformationSchema(schema, connection, tables, schemas, ctx) {
|
|
96
96
|
for (const t of tables) {
|
|
97
97
|
const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
|
|
98
|
-
const cols = await this.getColumns(connection, table.name, table.schema);
|
|
99
|
-
const indexes = await this.getIndexes(connection, table.name, table.schema);
|
|
100
|
-
const checks = await this.getChecks(connection, table.name, table.schema);
|
|
101
|
-
const pks = await this.getPrimaryKeys(connection, indexes, table.name, table.schema);
|
|
102
|
-
const fks = await this.getForeignKeys(connection, table.name, table.schema);
|
|
103
|
-
const enums = await this.getEnumDefinitions(connection, table.name, table.schema);
|
|
98
|
+
const cols = await this.getColumns(connection, table.name, table.schema, ctx);
|
|
99
|
+
const indexes = await this.getIndexes(connection, table.name, table.schema, ctx);
|
|
100
|
+
const checks = await this.getChecks(connection, table.name, table.schema, ctx);
|
|
101
|
+
const pks = await this.getPrimaryKeys(connection, indexes, table.name, table.schema, ctx);
|
|
102
|
+
const fks = await this.getForeignKeys(connection, table.name, table.schema, ctx);
|
|
103
|
+
const enums = await this.getEnumDefinitions(connection, table.name, table.schema, ctx);
|
|
104
104
|
table.init(cols, indexes, checks, pks, fks, enums);
|
|
105
105
|
}
|
|
106
106
|
}
|
|
@@ -252,8 +252,8 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
252
252
|
/**
|
|
253
253
|
* Returns all database names excluding 'temp'.
|
|
254
254
|
*/
|
|
255
|
-
async getDatabaseList(connection) {
|
|
256
|
-
const databases = await connection.execute('pragma database_list');
|
|
255
|
+
async getDatabaseList(connection, ctx) {
|
|
256
|
+
const databases = await connection.execute('pragma database_list', [], 'all', ctx);
|
|
257
257
|
return databases.filter(d => d.name !== 'temp').map(d => d.name);
|
|
258
258
|
}
|
|
259
259
|
/**
|
|
@@ -264,11 +264,11 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
264
264
|
/* v8 ignore next - fallback for non-standard view definitions */
|
|
265
265
|
return match ? match[1] : viewDefinition;
|
|
266
266
|
}
|
|
267
|
-
async getColumns(connection, tableName, schemaName) {
|
|
267
|
+
async getColumns(connection, tableName, schemaName, ctx) {
|
|
268
268
|
const prefix = this.getSchemaPrefix(schemaName);
|
|
269
|
-
const columns = await connection.execute(`pragma ${prefix}table_xinfo('${tableName}')
|
|
269
|
+
const columns = await connection.execute(`pragma ${prefix}table_xinfo('${tableName}')`, [], 'all', ctx);
|
|
270
270
|
const sql = `select sql from ${prefix}sqlite_master where type = ? and name = ?`;
|
|
271
|
-
const tableDefinition = await connection.execute(sql, ['table', tableName], 'get');
|
|
271
|
+
const tableDefinition = await connection.execute(sql, ['table', tableName], 'get', ctx);
|
|
272
272
|
const composite = columns.reduce((count, col) => count + (col.pk ? 1 : 0), 0) > 1;
|
|
273
273
|
// there can be only one, so naive check like this should be enough
|
|
274
274
|
const hasAutoincrement = tableDefinition.sql.toLowerCase().includes('autoincrement');
|
|
@@ -321,10 +321,10 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
321
321
|
// everything else is an expression that had its outer parens stripped
|
|
322
322
|
return `(${value})`;
|
|
323
323
|
}
|
|
324
|
-
async getEnumDefinitions(connection, tableName, schemaName) {
|
|
324
|
+
async getEnumDefinitions(connection, tableName, schemaName, ctx) {
|
|
325
325
|
const prefix = this.getSchemaPrefix(schemaName);
|
|
326
326
|
const sql = `select sql from ${prefix}sqlite_master where type = ? and name = ?`;
|
|
327
|
-
const tableDefinition = await connection.execute(sql, ['table', tableName], 'get');
|
|
327
|
+
const tableDefinition = await connection.execute(sql, ['table', tableName], 'get', ctx);
|
|
328
328
|
const checkConstraints = [...(tableDefinition.sql.match(/[`["'][^`\]"']+[`\]"'] text check \(.*?\)/gi) ?? [])];
|
|
329
329
|
return checkConstraints.reduce((o, item) => {
|
|
330
330
|
// check constraints are defined as (note that last closing paren is missing):
|
|
@@ -339,17 +339,17 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
339
339
|
return o;
|
|
340
340
|
}, {});
|
|
341
341
|
}
|
|
342
|
-
async getPrimaryKeys(connection, indexes, tableName, schemaName) {
|
|
342
|
+
async getPrimaryKeys(connection, indexes, tableName, schemaName, ctx) {
|
|
343
343
|
const prefix = this.getSchemaPrefix(schemaName);
|
|
344
344
|
const sql = `pragma ${prefix}table_info(\`${tableName}\`)`;
|
|
345
|
-
const cols = await connection.execute(sql);
|
|
345
|
+
const cols = await connection.execute(sql, [], 'all', ctx);
|
|
346
346
|
return cols.filter(col => !!col.pk).map(col => col.name);
|
|
347
347
|
}
|
|
348
|
-
async getIndexes(connection, tableName, schemaName) {
|
|
348
|
+
async getIndexes(connection, tableName, schemaName, ctx) {
|
|
349
349
|
const prefix = this.getSchemaPrefix(schemaName);
|
|
350
350
|
const sql = `pragma ${prefix}table_info(\`${tableName}\`)`;
|
|
351
|
-
const cols = await connection.execute(sql);
|
|
352
|
-
const indexes = await connection.execute(`pragma ${prefix}index_list(\`${tableName}\`)
|
|
351
|
+
const cols = await connection.execute(sql, [], 'all', ctx);
|
|
352
|
+
const indexes = await connection.execute(`pragma ${prefix}index_list(\`${tableName}\`)`, [], 'all', ctx);
|
|
353
353
|
const ret = [];
|
|
354
354
|
for (const col of cols.filter(c => c.pk)) {
|
|
355
355
|
ret.push({
|
|
@@ -361,7 +361,7 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
361
361
|
});
|
|
362
362
|
}
|
|
363
363
|
for (const index of indexes.filter(index => !this.isImplicitIndex(index.name))) {
|
|
364
|
-
const res = await connection.execute(`pragma ${prefix}index_info(\`${index.name}\`)
|
|
364
|
+
const res = await connection.execute(`pragma ${prefix}index_info(\`${index.name}\`)`, [], 'all', ctx);
|
|
365
365
|
ret.push(...res.map(row => ({
|
|
366
366
|
columnNames: [row.name],
|
|
367
367
|
keyName: index.name,
|
|
@@ -372,8 +372,8 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
372
372
|
}
|
|
373
373
|
return this.mapIndexes(ret);
|
|
374
374
|
}
|
|
375
|
-
async getChecks(connection, tableName, schemaName) {
|
|
376
|
-
const { columns, constraints } = await this.getColumnDefinitions(connection, tableName, schemaName);
|
|
375
|
+
async getChecks(connection, tableName, schemaName, ctx) {
|
|
376
|
+
const { columns, constraints } = await this.getColumnDefinitions(connection, tableName, schemaName, ctx);
|
|
377
377
|
const checks = [];
|
|
378
378
|
for (const key of Object.keys(columns)) {
|
|
379
379
|
const column = columns[key];
|
|
@@ -399,17 +399,17 @@ export class SqliteSchemaHelper extends SchemaHelper {
|
|
|
399
399
|
}
|
|
400
400
|
return checks;
|
|
401
401
|
}
|
|
402
|
-
async getColumnDefinitions(connection, tableName, schemaName) {
|
|
402
|
+
async getColumnDefinitions(connection, tableName, schemaName, ctx) {
|
|
403
403
|
const prefix = this.getSchemaPrefix(schemaName);
|
|
404
|
-
const columns = await connection.execute(`pragma ${prefix}table_xinfo('${tableName}')
|
|
404
|
+
const columns = await connection.execute(`pragma ${prefix}table_xinfo('${tableName}')`, [], 'all', ctx);
|
|
405
405
|
const sql = `select sql from ${prefix}sqlite_master where type = ? and name = ?`;
|
|
406
|
-
const tableDefinition = await connection.execute(sql, ['table', tableName], 'get');
|
|
406
|
+
const tableDefinition = await connection.execute(sql, ['table', tableName], 'get', ctx);
|
|
407
407
|
return this.parseTableDefinition(tableDefinition.sql, columns);
|
|
408
408
|
}
|
|
409
|
-
async getForeignKeys(connection, tableName, schemaName) {
|
|
410
|
-
const { constraints } = await this.getColumnDefinitions(connection, tableName, schemaName);
|
|
409
|
+
async getForeignKeys(connection, tableName, schemaName, ctx) {
|
|
410
|
+
const { constraints } = await this.getColumnDefinitions(connection, tableName, schemaName, ctx);
|
|
411
411
|
const prefix = this.getSchemaPrefix(schemaName);
|
|
412
|
-
const fks = await connection.execute(`pragma ${prefix}foreign_key_list(\`${tableName}\`)
|
|
412
|
+
const fks = await connection.execute(`pragma ${prefix}foreign_key_list(\`${tableName}\`)`, [], 'all', ctx);
|
|
413
413
|
const qualifiedTableName = schemaName ? `${schemaName}.${tableName}` : tableName;
|
|
414
414
|
return fks.reduce((ret, fk) => {
|
|
415
415
|
const constraintName = this.platform.getIndexName(tableName, [fk.from], 'foreign');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/sql",
|
|
3
|
-
"version": "7.0.7-dev.
|
|
3
|
+
"version": "7.0.7-dev.13",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"@mikro-orm/core": "^7.0.6"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
|
-
"@mikro-orm/core": "7.0.7-dev.
|
|
56
|
+
"@mikro-orm/core": "7.0.7-dev.13"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">= 22.17.0"
|
|
@@ -572,7 +572,7 @@ export class QueryBuilderHelper {
|
|
|
572
572
|
else if (value[op] instanceof Raw || typeof value[op]?.toRaw === 'function') {
|
|
573
573
|
const query = value[op] instanceof Raw ? value[op] : value[op].toRaw();
|
|
574
574
|
const mappedKey = this.mapper(key, type, query, null);
|
|
575
|
-
let sql = query.sql;
|
|
575
|
+
let sql = query.sql.replaceAll(ALIAS_REPLACEMENT, this.#alias);
|
|
576
576
|
if (['$in', '$nin'].includes(op)) {
|
|
577
577
|
sql = `(${sql})`;
|
|
578
578
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Configuration, type Dictionary, type EntityMetadata } from '@mikro-orm/core';
|
|
1
|
+
import { type Configuration, type Dictionary, type EntityMetadata, type Transaction } from '@mikro-orm/core';
|
|
2
2
|
import { DatabaseTable } from './DatabaseTable.js';
|
|
3
3
|
import type { AbstractSqlConnection } from '../AbstractSqlConnection.js';
|
|
4
4
|
import type { DatabaseView } from '../typings.js';
|
|
@@ -42,7 +42,7 @@ export declare class DatabaseSchema {
|
|
|
42
42
|
hasNamespace(namespace: string): boolean;
|
|
43
43
|
hasNativeEnum(name: string): boolean;
|
|
44
44
|
getNamespaces(): string[];
|
|
45
|
-
static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, schemas?: string[], takeTables?: (string | RegExp)[], skipTables?: (string | RegExp)[], skipViews?: (string | RegExp)[]): Promise<DatabaseSchema>;
|
|
45
|
+
static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, schemas?: string[], takeTables?: (string | RegExp)[], skipTables?: (string | RegExp)[], skipViews?: (string | RegExp)[], ctx?: Transaction): Promise<DatabaseSchema>;
|
|
46
46
|
static fromMetadata(metadata: EntityMetadata[], platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, em?: any): DatabaseSchema;
|
|
47
47
|
private static getViewDefinition;
|
|
48
48
|
private static getSchemaName;
|
package/schema/DatabaseSchema.js
CHANGED
|
@@ -87,9 +87,9 @@ export class DatabaseSchema {
|
|
|
87
87
|
getNamespaces() {
|
|
88
88
|
return [...this.#namespaces];
|
|
89
89
|
}
|
|
90
|
-
static async create(connection, platform, config, schemaName, schemas, takeTables, skipTables, skipViews) {
|
|
90
|
+
static async create(connection, platform, config, schemaName, schemas, takeTables, skipTables, skipViews, ctx) {
|
|
91
91
|
const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema') ?? platform.getDefaultSchemaName());
|
|
92
|
-
const allTables = await platform.getSchemaHelper().getAllTables(connection, schemas);
|
|
92
|
+
const allTables = await platform.getSchemaHelper().getAllTables(connection, schemas, ctx);
|
|
93
93
|
const parts = config.get('migrations').tableName.split('.');
|
|
94
94
|
const migrationsTableName = parts[1] ?? parts[0];
|
|
95
95
|
const migrationsSchemaName = parts.length > 1 ? parts[0] : config.get('schema', platform.getDefaultSchemaName());
|
|
@@ -97,12 +97,12 @@ export class DatabaseSchema {
|
|
|
97
97
|
(t.table_name !== migrationsTableName || (t.schema_name && t.schema_name !== migrationsSchemaName)));
|
|
98
98
|
await platform
|
|
99
99
|
.getSchemaHelper()
|
|
100
|
-
.loadInformationSchema(schema, connection, tables, schemas && schemas.length > 0 ? schemas : undefined);
|
|
100
|
+
.loadInformationSchema(schema, connection, tables, schemas && schemas.length > 0 ? schemas : undefined, ctx);
|
|
101
101
|
// Load views from database
|
|
102
|
-
await platform.getSchemaHelper().loadViews(schema, connection);
|
|
102
|
+
await platform.getSchemaHelper().loadViews(schema, connection, schemaName, ctx);
|
|
103
103
|
// Load materialized views (PostgreSQL only)
|
|
104
104
|
if (platform.supportsMaterializedViews()) {
|
|
105
|
-
await platform.getSchemaHelper().loadMaterializedViews(schema, connection, schemaName);
|
|
105
|
+
await platform.getSchemaHelper().loadMaterializedViews(schema, connection, schemaName, ctx);
|
|
106
106
|
}
|
|
107
107
|
// Filter out skipped views
|
|
108
108
|
if (skipViews && skipViews.length > 0) {
|
package/schema/SchemaHelper.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Connection, type Dictionary, type Options, RawQueryFragment } from '@mikro-orm/core';
|
|
1
|
+
import { type Connection, type Dictionary, type Options, type Transaction, RawQueryFragment } from '@mikro-orm/core';
|
|
2
2
|
import type { AbstractSqlConnection } from '../AbstractSqlConnection.js';
|
|
3
3
|
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform.js';
|
|
4
4
|
import type { CheckDef, Column, ForeignKey, IndexDef, Table, TableDifference } from '../typings.js';
|
|
@@ -26,13 +26,13 @@ export declare abstract class SchemaHelper {
|
|
|
26
26
|
getDropNativeEnumSQL(name: string, schema?: string): string;
|
|
27
27
|
getAlterNativeEnumSQL(name: string, schema?: string, value?: string, items?: string[], oldItems?: string[]): string;
|
|
28
28
|
/** Loads table metadata (columns, indexes, foreign keys) from the database information schema. */
|
|
29
|
-
abstract loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[]): Promise<void>;
|
|
29
|
+
abstract loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[], ctx?: Transaction): Promise<void>;
|
|
30
30
|
/** Returns the SQL query to list all tables in the database. */
|
|
31
31
|
getListTablesSQL(): string;
|
|
32
32
|
/** Retrieves all tables from the database. */
|
|
33
|
-
getAllTables(connection: AbstractSqlConnection, schemas?: string[]): Promise<Table[]>;
|
|
33
|
+
getAllTables(connection: AbstractSqlConnection, schemas?: string[], ctx?: Transaction): Promise<Table[]>;
|
|
34
34
|
getListViewsSQL(): string;
|
|
35
|
-
loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string): Promise<void>;
|
|
35
|
+
loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string, ctx?: Transaction): Promise<void>;
|
|
36
36
|
/** Returns SQL to rename a column in a table. */
|
|
37
37
|
getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column, schemaName?: string): string;
|
|
38
38
|
/** Returns SQL to create an index on a table. */
|
|
@@ -61,7 +61,7 @@ export declare abstract class SchemaHelper {
|
|
|
61
61
|
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string[];
|
|
62
62
|
getPostAlterTable(tableDiff: TableDifference, safe: boolean): string[];
|
|
63
63
|
getChangeColumnCommentSQL(tableName: string, to: Column, schemaName?: string): string;
|
|
64
|
-
getNamespaces(connection: AbstractSqlConnection): Promise<string[]>;
|
|
64
|
+
getNamespaces(connection: AbstractSqlConnection, ctx?: Transaction): Promise<string[]>;
|
|
65
65
|
protected mapIndexes(indexes: IndexDef[]): Promise<IndexDef[]>;
|
|
66
66
|
mapForeignKeys(fks: any[], tableName: string, schemaName?: string): Dictionary;
|
|
67
67
|
normalizeDefaultValue(defaultValue: string | RawQueryFragment, length?: number, defaultValues?: Dictionary<string[]>): string | number;
|
|
@@ -101,5 +101,5 @@ export declare abstract class SchemaHelper {
|
|
|
101
101
|
dropMaterializedViewIfExists(name: string, schema?: string): string;
|
|
102
102
|
refreshMaterializedView(name: string, schema?: string, concurrently?: boolean): string;
|
|
103
103
|
getListMaterializedViewsSQL(): string;
|
|
104
|
-
loadMaterializedViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string): Promise<void>;
|
|
104
|
+
loadMaterializedViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string, ctx?: Transaction): Promise<void>;
|
|
105
105
|
}
|
package/schema/SchemaHelper.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RawQueryFragment, Utils } from '@mikro-orm/core';
|
|
1
|
+
import { RawQueryFragment, Utils, } from '@mikro-orm/core';
|
|
2
2
|
/** Base class for database-specific schema helpers. Provides SQL generation for DDL operations. */
|
|
3
3
|
export class SchemaHelper {
|
|
4
4
|
platform;
|
|
@@ -72,13 +72,13 @@ export class SchemaHelper {
|
|
|
72
72
|
throw new Error('Not supported by given driver');
|
|
73
73
|
}
|
|
74
74
|
/** Retrieves all tables from the database. */
|
|
75
|
-
async getAllTables(connection, schemas) {
|
|
76
|
-
return connection.execute(this.getListTablesSQL());
|
|
75
|
+
async getAllTables(connection, schemas, ctx) {
|
|
76
|
+
return connection.execute(this.getListTablesSQL(), [], 'all', ctx);
|
|
77
77
|
}
|
|
78
78
|
getListViewsSQL() {
|
|
79
79
|
throw new Error('Not supported by given driver');
|
|
80
80
|
}
|
|
81
|
-
async loadViews(schema, connection, schemaName) {
|
|
81
|
+
async loadViews(schema, connection, schemaName, ctx) {
|
|
82
82
|
throw new Error('Not supported by given driver');
|
|
83
83
|
}
|
|
84
84
|
/** Returns SQL to rename a column in a table. */
|
|
@@ -362,7 +362,7 @@ export class SchemaHelper {
|
|
|
362
362
|
getChangeColumnCommentSQL(tableName, to, schemaName) {
|
|
363
363
|
return '';
|
|
364
364
|
}
|
|
365
|
-
async getNamespaces(connection) {
|
|
365
|
+
async getNamespaces(connection, ctx) {
|
|
366
366
|
return [];
|
|
367
367
|
}
|
|
368
368
|
async mapIndexes(indexes) {
|
|
@@ -663,7 +663,7 @@ export class SchemaHelper {
|
|
|
663
663
|
getListMaterializedViewsSQL() {
|
|
664
664
|
throw new Error('Not supported by given driver');
|
|
665
665
|
}
|
|
666
|
-
async loadMaterializedViews(schema, connection, schemaName) {
|
|
666
|
+
async loadMaterializedViews(schema, connection, schemaName, ctx) {
|
|
667
667
|
throw new Error('Not supported by given driver');
|
|
668
668
|
}
|
|
669
669
|
}
|
package/typings.d.ts
CHANGED
|
@@ -252,8 +252,9 @@ export type MapTableName<T extends {
|
|
|
252
252
|
}, TOptions extends MikroKyselyPluginOptions = {}> = {
|
|
253
253
|
[P in T as TOptions['tableNamingStrategy'] extends 'entity' ? P['name'] : PreferStringLiteral<NonNullable<P['tableName']>, P['name']>]: P;
|
|
254
254
|
};
|
|
255
|
+
type ResolveTableNaming<TOptions extends MikroKyselyPluginOptions> = TOptions['tableNamingStrategy'] extends 'entity' ? 'entity' : 'underscore';
|
|
255
256
|
export type MapValueAsTable<TMap extends Record<string, any>, TOptions extends MikroKyselyPluginOptions = {}> = {
|
|
256
|
-
[K in keyof TMap as TransformName<K, TOptions
|
|
257
|
+
[K in keyof TMap as TransformName<K, ResolveTableNaming<TOptions>>]: InferKyselyTable<TMap[K], TOptions>;
|
|
257
258
|
};
|
|
258
259
|
export type InferKyselyTable<TSchema extends EntitySchemaWithMeta, TOptions extends MikroKyselyPluginOptions = {}> = ExcludeNever<{
|
|
259
260
|
-readonly [K in keyof InferEntityProperties<TSchema> as TransformColumnName<K, TOptions['columnNamingStrategy'] extends 'property' ? 'property' : 'underscore', MaybeReturnType<InferEntityProperties<TSchema>[K]>>]: InferColumnValue<MaybeReturnType<InferEntityProperties<TSchema>[K]>, TOptions['processOnCreateHooks'] extends true ? true : false>;
|
|
@@ -320,7 +321,9 @@ export type InferClassEntityDB<TEntities, TOptions extends MikroKyselyPluginOpti
|
|
|
320
321
|
type ClassEntityDBMap<TEntities, TOptions extends MikroKyselyPluginOptions = {}> = {
|
|
321
322
|
[T in TEntities as ClassEntityTableName<T, TOptions>]: ClassEntityColumns<T, TOptions>;
|
|
322
323
|
};
|
|
323
|
-
type ClassEntityTableName<T, TOptions extends MikroKyselyPluginOptions = {}> = T extends
|
|
324
|
+
type ClassEntityTableName<T, TOptions extends MikroKyselyPluginOptions = {}> = T extends {
|
|
325
|
+
'~entityName'?: infer Name extends string;
|
|
326
|
+
} ? TransformName<Name, ResolveTableNaming<TOptions>> : T extends abstract new (...args: any[]) => infer Instance ? TransformName<InferEntityName<Instance>, ResolveTableNaming<TOptions>> : never;
|
|
324
327
|
type ClassEntityColumns<T, TOptions extends MikroKyselyPluginOptions = {}> = T extends abstract new (...args: any[]) => infer Instance ? {
|
|
325
328
|
[K in keyof Instance as ClassEntityColumnName<K, Instance[K], TOptions>]: ClassEntityColumnValue<Instance[K]>;
|
|
326
329
|
} : never;
|