@mikro-orm/knex 6.1.13-dev.8 → 6.2.0
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.js +8 -2
- package/AbstractSqlDriver.d.ts +5 -5
- package/AbstractSqlDriver.js +21 -10
- package/MonkeyPatchable.d.ts +5 -0
- package/MonkeyPatchable.js +15 -0
- package/README.md +17 -11
- package/SqlEntityManager.d.ts +1 -1
- package/SqlEntityManager.js +1 -2
- package/dialects/index.d.ts +4 -0
- package/dialects/index.js +20 -0
- package/dialects/mssql/MsSqlColumnCompiler.d.ts +4 -0
- package/dialects/mssql/MsSqlColumnCompiler.js +10 -0
- package/dialects/mssql/MsSqlKnexDialect.d.ts +6 -0
- package/dialects/mssql/MsSqlKnexDialect.js +22 -0
- package/dialects/mssql/MsSqlQueryCompiler.d.ts +12 -0
- package/dialects/mssql/MsSqlQueryCompiler.js +94 -0
- package/dialects/mssql/MsSqlTableCompiler.d.ts +9 -0
- package/dialects/mssql/MsSqlTableCompiler.js +40 -0
- package/dialects/mssql/index.d.ts +1 -0
- package/dialects/mssql/index.js +17 -0
- package/dialects/mysql/MariaDbKnexDialect.d.ts +6 -0
- package/dialects/mysql/MariaDbKnexDialect.js +16 -0
- package/dialects/mysql/MySqlColumnCompiler.d.ts +9 -0
- package/dialects/mysql/MySqlColumnCompiler.js +19 -0
- package/dialects/mysql/MySqlConnection.d.ts +8 -0
- package/dialects/mysql/MySqlConnection.js +43 -0
- package/dialects/mysql/MySqlExceptionConverter.d.ts +9 -0
- package/dialects/mysql/MySqlExceptionConverter.js +83 -0
- package/dialects/mysql/MySqlKnexDialect.d.ts +5 -0
- package/dialects/mysql/MySqlKnexDialect.js +21 -0
- package/dialects/mysql/MySqlPlatform.d.ts +31 -0
- package/dialects/mysql/MySqlPlatform.js +88 -0
- package/dialects/mysql/MySqlQueryCompiler.d.ts +5 -0
- package/dialects/mysql/MySqlQueryCompiler.js +23 -0
- package/dialects/mysql/MySqlSchemaHelper.d.ts +43 -0
- package/dialects/mysql/MySqlSchemaHelper.js +297 -0
- package/dialects/mysql/index.d.ts +6 -0
- package/dialects/mysql/index.js +22 -0
- package/dialects/postgresql/PostgreSqlKnexDialect.d.ts +6 -0
- package/dialects/postgresql/PostgreSqlKnexDialect.js +19 -0
- package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +11 -0
- package/dialects/postgresql/PostgreSqlTableCompiler.js +89 -0
- package/dialects/postgresql/index.d.ts +1 -0
- package/dialects/postgresql/index.js +17 -0
- package/dialects/sqlite/BaseSqliteConnection.d.ts +10 -0
- package/dialects/sqlite/BaseSqliteConnection.js +56 -0
- package/dialects/sqlite/BaseSqlitePlatform.d.ts +49 -0
- package/dialects/sqlite/BaseSqlitePlatform.js +78 -0
- package/dialects/sqlite/BaseSqliteSchemaHelper.d.ts +27 -0
- package/dialects/sqlite/BaseSqliteSchemaHelper.js +172 -0
- package/dialects/sqlite/BetterSqliteKnexDialect.d.ts +5 -0
- package/dialects/sqlite/BetterSqliteKnexDialect.js +15 -0
- package/dialects/sqlite/LibSqlKnexDialect.d.ts +6 -0
- package/dialects/sqlite/LibSqlKnexDialect.js +18 -0
- package/dialects/sqlite/SqliteKnexDialect.d.ts +8 -0
- package/dialects/sqlite/SqliteKnexDialect.js +67 -0
- package/dialects/sqlite/SqliteTableCompiler.d.ts +5 -0
- package/dialects/sqlite/SqliteTableCompiler.js +45 -0
- package/dialects/sqlite/index.d.ts +7 -0
- package/dialects/sqlite/index.js +23 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.mjs +16 -0
- package/package.json +3 -3
- package/query/QueryBuilder.d.ts +59 -41
- package/query/QueryBuilder.js +61 -23
- package/query/QueryBuilderHelper.js +8 -3
- package/schema/DatabaseTable.js +16 -4
- package/schema/SchemaComparator.js +13 -3
- package/schema/SchemaHelper.d.ts +5 -1
- package/schema/SchemaHelper.js +19 -2
- package/schema/SqlSchemaGenerator.d.ts +12 -4
- package/schema/SqlSchemaGenerator.js +51 -29
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ExceptionConverter, type Dictionary, type DriverException } from '@mikro-orm/core';
|
|
2
|
+
export declare class MySqlExceptionConverter extends ExceptionConverter {
|
|
3
|
+
/**
|
|
4
|
+
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
|
|
5
|
+
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
|
|
6
|
+
* @link https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractMySQLDriver.php
|
|
7
|
+
*/
|
|
8
|
+
convertException(exception: Error & Dictionary): DriverException;
|
|
9
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MySqlExceptionConverter = void 0;
|
|
4
|
+
const core_1 = require("@mikro-orm/core");
|
|
5
|
+
class MySqlExceptionConverter extends core_1.ExceptionConverter {
|
|
6
|
+
/* istanbul ignore next */
|
|
7
|
+
/**
|
|
8
|
+
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
|
|
9
|
+
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
|
|
10
|
+
* @link https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractMySQLDriver.php
|
|
11
|
+
*/
|
|
12
|
+
convertException(exception) {
|
|
13
|
+
switch (exception.errno) {
|
|
14
|
+
case 1213:
|
|
15
|
+
return new core_1.DeadlockException(exception);
|
|
16
|
+
case 1205:
|
|
17
|
+
return new core_1.LockWaitTimeoutException(exception);
|
|
18
|
+
case 1050:
|
|
19
|
+
return new core_1.TableExistsException(exception);
|
|
20
|
+
case 1051:
|
|
21
|
+
case 1146:
|
|
22
|
+
return new core_1.TableNotFoundException(exception);
|
|
23
|
+
case 1216:
|
|
24
|
+
case 1217:
|
|
25
|
+
case 1451:
|
|
26
|
+
case 1452:
|
|
27
|
+
case 1701:
|
|
28
|
+
return new core_1.ForeignKeyConstraintViolationException(exception);
|
|
29
|
+
case 3819:
|
|
30
|
+
return new core_1.CheckConstraintViolationException(exception);
|
|
31
|
+
case 1062:
|
|
32
|
+
case 1557:
|
|
33
|
+
case 1569:
|
|
34
|
+
case 1586:
|
|
35
|
+
return new core_1.UniqueConstraintViolationException(exception);
|
|
36
|
+
case 1054:
|
|
37
|
+
case 1166:
|
|
38
|
+
case 1611:
|
|
39
|
+
return new core_1.InvalidFieldNameException(exception);
|
|
40
|
+
case 1052:
|
|
41
|
+
case 1060:
|
|
42
|
+
case 1110:
|
|
43
|
+
return new core_1.NonUniqueFieldNameException(exception);
|
|
44
|
+
case 1064:
|
|
45
|
+
case 1149:
|
|
46
|
+
case 1287:
|
|
47
|
+
case 1341:
|
|
48
|
+
case 1342:
|
|
49
|
+
case 1343:
|
|
50
|
+
case 1344:
|
|
51
|
+
case 1382:
|
|
52
|
+
case 1479:
|
|
53
|
+
case 1541:
|
|
54
|
+
case 1554:
|
|
55
|
+
case 1626:
|
|
56
|
+
return new core_1.SyntaxErrorException(exception);
|
|
57
|
+
case 1044:
|
|
58
|
+
case 1045:
|
|
59
|
+
case 1046:
|
|
60
|
+
case 1049:
|
|
61
|
+
case 1095:
|
|
62
|
+
case 1142:
|
|
63
|
+
case 1143:
|
|
64
|
+
case 1227:
|
|
65
|
+
case 1370:
|
|
66
|
+
case 1429:
|
|
67
|
+
case 2002:
|
|
68
|
+
case 2005:
|
|
69
|
+
return new core_1.ConnectionException(exception);
|
|
70
|
+
case 1048:
|
|
71
|
+
case 1121:
|
|
72
|
+
case 1138:
|
|
73
|
+
case 1171:
|
|
74
|
+
case 1252:
|
|
75
|
+
case 1263:
|
|
76
|
+
case 1364:
|
|
77
|
+
case 1566:
|
|
78
|
+
return new core_1.NotNullConstraintViolationException(exception);
|
|
79
|
+
}
|
|
80
|
+
return super.convertException(exception);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.MySqlExceptionConverter = MySqlExceptionConverter;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MySqlKnexDialect = void 0;
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const mysql2_1 = __importDefault(require("knex/lib/dialects/mysql2"));
|
|
9
|
+
const MySqlQueryCompiler_1 = require("./MySqlQueryCompiler");
|
|
10
|
+
const MySqlColumnCompiler_1 = require("./MySqlColumnCompiler");
|
|
11
|
+
class MySqlKnexDialect extends mysql2_1.default {
|
|
12
|
+
queryCompiler() {
|
|
13
|
+
// eslint-disable-next-line prefer-rest-params
|
|
14
|
+
return new MySqlQueryCompiler_1.MySqlQueryCompiler(this, ...arguments);
|
|
15
|
+
}
|
|
16
|
+
columnCompiler() {
|
|
17
|
+
// eslint-disable-next-line prefer-rest-params
|
|
18
|
+
return new MySqlColumnCompiler_1.MySqlColumnCompiler(this, ...arguments);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.MySqlKnexDialect = MySqlKnexDialect;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type SimpleColumnMeta, type Type, type TransformContext } from '@mikro-orm/core';
|
|
2
|
+
import { MySqlSchemaHelper } from './MySqlSchemaHelper';
|
|
3
|
+
import { MySqlExceptionConverter } from './MySqlExceptionConverter';
|
|
4
|
+
import { AbstractSqlPlatform } from '../../AbstractSqlPlatform';
|
|
5
|
+
import type { IndexDef } from '../../typings';
|
|
6
|
+
export declare class MySqlPlatform extends AbstractSqlPlatform {
|
|
7
|
+
protected readonly schemaHelper: MySqlSchemaHelper;
|
|
8
|
+
protected readonly exceptionConverter: MySqlExceptionConverter;
|
|
9
|
+
protected readonly ORDER_BY_NULLS_TRANSLATE: {
|
|
10
|
+
readonly "asc nulls first": "is not null";
|
|
11
|
+
readonly "asc nulls last": "is null";
|
|
12
|
+
readonly "desc nulls first": "is not null";
|
|
13
|
+
readonly "desc nulls last": "is null";
|
|
14
|
+
};
|
|
15
|
+
getDefaultCharset(): string;
|
|
16
|
+
convertJsonToDatabaseValue(value: unknown, context?: TransformContext): unknown;
|
|
17
|
+
getJsonIndexDefinition(index: IndexDef): string[];
|
|
18
|
+
getBooleanTypeDeclarationSQL(): string;
|
|
19
|
+
getDefaultMappedType(type: string): Type<unknown>;
|
|
20
|
+
supportsUnsigned(): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the default name of index for the given columns
|
|
23
|
+
* cannot go past 64 character length for identifiers in MySQL
|
|
24
|
+
*/
|
|
25
|
+
getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
|
|
26
|
+
getDefaultPrimaryName(tableName: string, columns: string[]): string;
|
|
27
|
+
supportsCreatingFullTextIndex(): boolean;
|
|
28
|
+
getFullTextWhereClause(): string;
|
|
29
|
+
getFullTextIndexExpression(indexName: string, schemaName: string | undefined, tableName: string, columns: SimpleColumnMeta[]): string;
|
|
30
|
+
getOrderByExpression(column: string, direction: string): string[];
|
|
31
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MySqlPlatform = void 0;
|
|
4
|
+
const core_1 = require("@mikro-orm/core");
|
|
5
|
+
const MySqlSchemaHelper_1 = require("./MySqlSchemaHelper");
|
|
6
|
+
const MySqlExceptionConverter_1 = require("./MySqlExceptionConverter");
|
|
7
|
+
const AbstractSqlPlatform_1 = require("../../AbstractSqlPlatform");
|
|
8
|
+
class MySqlPlatform extends AbstractSqlPlatform_1.AbstractSqlPlatform {
|
|
9
|
+
schemaHelper = new MySqlSchemaHelper_1.MySqlSchemaHelper(this);
|
|
10
|
+
exceptionConverter = new MySqlExceptionConverter_1.MySqlExceptionConverter();
|
|
11
|
+
ORDER_BY_NULLS_TRANSLATE = {
|
|
12
|
+
[core_1.QueryOrder.asc_nulls_first]: 'is not null',
|
|
13
|
+
[core_1.QueryOrder.asc_nulls_last]: 'is null',
|
|
14
|
+
[core_1.QueryOrder.desc_nulls_first]: 'is not null',
|
|
15
|
+
[core_1.QueryOrder.desc_nulls_last]: 'is null',
|
|
16
|
+
};
|
|
17
|
+
getDefaultCharset() {
|
|
18
|
+
return 'utf8mb4';
|
|
19
|
+
}
|
|
20
|
+
convertJsonToDatabaseValue(value, context) {
|
|
21
|
+
if (context?.mode === 'query') {
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
return JSON.stringify(value);
|
|
25
|
+
}
|
|
26
|
+
getJsonIndexDefinition(index) {
|
|
27
|
+
return index.columnNames
|
|
28
|
+
.map(column => {
|
|
29
|
+
if (!column.includes('.')) {
|
|
30
|
+
return column;
|
|
31
|
+
}
|
|
32
|
+
const [root, ...path] = column.split('.');
|
|
33
|
+
return `(json_value(${this.quoteIdentifier(root)}, '$.${path.join('.')}' returning ${index.options?.returning ?? 'char(255)'}))`;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
getBooleanTypeDeclarationSQL() {
|
|
37
|
+
return 'tinyint(1)';
|
|
38
|
+
}
|
|
39
|
+
getDefaultMappedType(type) {
|
|
40
|
+
if (type === 'tinyint(1)') {
|
|
41
|
+
return super.getDefaultMappedType('boolean');
|
|
42
|
+
}
|
|
43
|
+
return super.getDefaultMappedType(type);
|
|
44
|
+
}
|
|
45
|
+
supportsUnsigned() {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns the default name of index for the given columns
|
|
50
|
+
* cannot go past 64 character length for identifiers in MySQL
|
|
51
|
+
*/
|
|
52
|
+
getIndexName(tableName, columns, type) {
|
|
53
|
+
if (type === 'primary') {
|
|
54
|
+
return this.getDefaultPrimaryName(tableName, columns);
|
|
55
|
+
}
|
|
56
|
+
const indexName = super.getIndexName(tableName, columns, type);
|
|
57
|
+
if (indexName.length > 64) {
|
|
58
|
+
return `${indexName.substring(0, 56 - type.length)}_${core_1.Utils.hash(indexName, 5)}_${type}`;
|
|
59
|
+
}
|
|
60
|
+
return indexName;
|
|
61
|
+
}
|
|
62
|
+
getDefaultPrimaryName(tableName, columns) {
|
|
63
|
+
return 'PRIMARY'; // https://dev.mysql.com/doc/refman/8.0/en/create-table.html#create-table-indexes-keys
|
|
64
|
+
}
|
|
65
|
+
supportsCreatingFullTextIndex() {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
getFullTextWhereClause() {
|
|
69
|
+
return `match(:column:) against (:query in boolean mode)`;
|
|
70
|
+
}
|
|
71
|
+
getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
|
|
72
|
+
/* istanbul ignore next */
|
|
73
|
+
const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
|
|
74
|
+
const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
|
|
75
|
+
const quotedIndexName = this.quoteIdentifier(indexName);
|
|
76
|
+
return `alter table ${quotedTableName} add fulltext index ${quotedIndexName}(${quotedColumnNames.join(',')})`;
|
|
77
|
+
}
|
|
78
|
+
getOrderByExpression(column, direction) {
|
|
79
|
+
const ret = [];
|
|
80
|
+
const dir = direction.toLowerCase();
|
|
81
|
+
if (dir in this.ORDER_BY_NULLS_TRANSLATE) {
|
|
82
|
+
ret.push(`${column} ${this.ORDER_BY_NULLS_TRANSLATE[dir]}`);
|
|
83
|
+
}
|
|
84
|
+
ret.push(`${column} ${dir.replace(/(\s|nulls|first|last)*/gi, '')}`);
|
|
85
|
+
return ret;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.MySqlPlatform = MySqlPlatform;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MySqlQueryCompiler = void 0;
|
|
7
|
+
/* istanbul ignore file */
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
const mysql_querycompiler_1 = __importDefault(require("knex/lib/dialects/mysql/query/mysql-querycompiler"));
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
const querycompiler_1 = __importDefault(require("knex/lib/query/querycompiler"));
|
|
12
|
+
// upsert support from https://github.com/knex/knex/pull/6050
|
|
13
|
+
class MySqlQueryCompiler extends mysql_querycompiler_1.default {
|
|
14
|
+
// mysql dialect disallows query non scalar params, but we dont use it to execute the query, it always goes through the `platform.formatQuery()`
|
|
15
|
+
whereBasic(statement) {
|
|
16
|
+
return querycompiler_1.default.prototype.whereBasic.call(this, statement);
|
|
17
|
+
}
|
|
18
|
+
// mysql dialect disallows query non scalar params, but we dont use it to execute the query, it always goes through the `platform.formatQuery()`
|
|
19
|
+
whereRaw(statement) {
|
|
20
|
+
return querycompiler_1.default.prototype.whereRaw.call(this, statement);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.MySqlQueryCompiler = MySqlQueryCompiler;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
import type { CheckDef, Column, IndexDef, TableDifference, Table, ForeignKey } from '../../typings';
|
|
3
|
+
import { type Dictionary, type Type } from '@mikro-orm/core';
|
|
4
|
+
import type { AbstractSqlConnection } from '../../AbstractSqlConnection';
|
|
5
|
+
import { SchemaHelper } from '../../schema/SchemaHelper';
|
|
6
|
+
import type { DatabaseSchema } from '../../schema/DatabaseSchema';
|
|
7
|
+
import type { DatabaseTable } from '../../schema/DatabaseTable';
|
|
8
|
+
export declare class MySqlSchemaHelper extends SchemaHelper {
|
|
9
|
+
private readonly _cache;
|
|
10
|
+
static readonly DEFAULT_VALUES: {
|
|
11
|
+
'now()': string[];
|
|
12
|
+
'current_timestamp(?)': string[];
|
|
13
|
+
'0': string[];
|
|
14
|
+
};
|
|
15
|
+
getSchemaBeginning(charset: string): string;
|
|
16
|
+
disableForeignKeysSQL(): string;
|
|
17
|
+
enableForeignKeysSQL(): string;
|
|
18
|
+
finalizeTable(table: Knex.CreateTableBuilder, charset: string, collate?: string): void;
|
|
19
|
+
getListTablesSQL(): string;
|
|
20
|
+
loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[]): Promise<void>;
|
|
21
|
+
getAllIndexes(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<IndexDef[]>>;
|
|
22
|
+
getAllColumns(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Column[]>>;
|
|
23
|
+
getAllChecks(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<CheckDef[]>>;
|
|
24
|
+
getAllForeignKeys(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<ForeignKey>>>;
|
|
25
|
+
getPreAlterTable(tableDiff: TableDifference, safe: boolean): string;
|
|
26
|
+
configureColumnDefault(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
27
|
+
getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column): string;
|
|
28
|
+
getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string;
|
|
29
|
+
getChangeColumnCommentSQL(tableName: string, to: Column, schemaName?: string): string;
|
|
30
|
+
createTableColumn(table: Knex.TableBuilder, column: Column, fromTable: DatabaseTable, changedProperties?: Set<string>): Knex.ColumnBuilder | undefined;
|
|
31
|
+
configureColumn(column: Column, col: Knex.ColumnBuilder, knex: Knex, changedProperties?: Set<string>): Knex.ColumnBuilder;
|
|
32
|
+
private getColumnDeclarationSQL;
|
|
33
|
+
getForeignKeysSQL(tableName: string, schemaName?: string): string;
|
|
34
|
+
getAllEnumDefinitions(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<string[]>>>;
|
|
35
|
+
private supportsCheckConstraints;
|
|
36
|
+
protected getChecksSQL(tables: Table[]): string;
|
|
37
|
+
getChecks(connection: AbstractSqlConnection, tableName: string, schemaName: string, columns?: Column[]): Promise<CheckDef[]>;
|
|
38
|
+
getEnumDefinitions(connection: AbstractSqlConnection, checks: CheckDef[], tableName: string, schemaName?: string): Promise<Dictionary<string[]>>;
|
|
39
|
+
getColumns(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<Column[]>;
|
|
40
|
+
getIndexes(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<IndexDef[]>;
|
|
41
|
+
normalizeDefaultValue(defaultValue: string, length: number): string | number;
|
|
42
|
+
protected wrap(val: string | null | undefined, type: Type<unknown>): string | null | undefined;
|
|
43
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MySqlSchemaHelper = void 0;
|
|
4
|
+
const core_1 = require("@mikro-orm/core");
|
|
5
|
+
const SchemaHelper_1 = require("../../schema/SchemaHelper");
|
|
6
|
+
class MySqlSchemaHelper extends SchemaHelper_1.SchemaHelper {
|
|
7
|
+
_cache = {};
|
|
8
|
+
static DEFAULT_VALUES = {
|
|
9
|
+
'now()': ['now()', 'current_timestamp'],
|
|
10
|
+
'current_timestamp(?)': ['current_timestamp(?)'],
|
|
11
|
+
'0': ['0', 'false'],
|
|
12
|
+
};
|
|
13
|
+
getSchemaBeginning(charset) {
|
|
14
|
+
return `set names ${charset};\n${this.disableForeignKeysSQL()}\n\n`;
|
|
15
|
+
}
|
|
16
|
+
disableForeignKeysSQL() {
|
|
17
|
+
return 'set foreign_key_checks = 0;';
|
|
18
|
+
}
|
|
19
|
+
enableForeignKeysSQL() {
|
|
20
|
+
return 'set foreign_key_checks = 1;';
|
|
21
|
+
}
|
|
22
|
+
finalizeTable(table, charset, collate) {
|
|
23
|
+
table.engine('InnoDB');
|
|
24
|
+
table.charset(charset);
|
|
25
|
+
if (collate) {
|
|
26
|
+
table.collate(collate);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
getListTablesSQL() {
|
|
30
|
+
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()`;
|
|
31
|
+
}
|
|
32
|
+
async loadInformationSchema(schema, connection, tables) {
|
|
33
|
+
if (tables.length === 0) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const columns = await this.getAllColumns(connection, tables);
|
|
37
|
+
const indexes = await this.getAllIndexes(connection, tables);
|
|
38
|
+
const checks = await this.getAllChecks(connection, tables);
|
|
39
|
+
const fks = await this.getAllForeignKeys(connection, tables);
|
|
40
|
+
const enums = await this.getAllEnumDefinitions(connection, tables);
|
|
41
|
+
for (const t of tables) {
|
|
42
|
+
const key = this.getTableKey(t);
|
|
43
|
+
const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
|
|
44
|
+
const pks = await this.getPrimaryKeys(connection, indexes[key], table.name, table.schema);
|
|
45
|
+
table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums[key]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async getAllIndexes(connection, tables) {
|
|
49
|
+
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, expression as expression
|
|
50
|
+
from information_schema.statistics where table_schema = database()
|
|
51
|
+
and table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(', ')})
|
|
52
|
+
order by schema_name, table_name, index_name, seq_in_index`;
|
|
53
|
+
const allIndexes = await connection.execute(sql);
|
|
54
|
+
const ret = {};
|
|
55
|
+
for (const index of allIndexes) {
|
|
56
|
+
const key = this.getTableKey(index);
|
|
57
|
+
const indexDef = {
|
|
58
|
+
columnNames: [index.column_name],
|
|
59
|
+
keyName: index.index_name,
|
|
60
|
+
unique: !index.non_unique,
|
|
61
|
+
primary: index.index_name === 'PRIMARY',
|
|
62
|
+
constraint: !index.non_unique,
|
|
63
|
+
};
|
|
64
|
+
if (!index.column_name || index.expression?.match(/ where /i)) {
|
|
65
|
+
indexDef.expression = index.expression; // required for the `getCreateIndexSQL()` call
|
|
66
|
+
indexDef.expression = this.getCreateIndexSQL(index.table_name, indexDef, !!index.expression);
|
|
67
|
+
}
|
|
68
|
+
ret[key] ??= [];
|
|
69
|
+
ret[key].push(indexDef);
|
|
70
|
+
}
|
|
71
|
+
for (const key of Object.keys(ret)) {
|
|
72
|
+
ret[key] = await this.mapIndexes(ret[key]);
|
|
73
|
+
}
|
|
74
|
+
return ret;
|
|
75
|
+
}
|
|
76
|
+
async getAllColumns(connection, tables) {
|
|
77
|
+
const sql = `select table_name as table_name,
|
|
78
|
+
nullif(table_schema, schema()) as schema_name,
|
|
79
|
+
column_name as column_name,
|
|
80
|
+
column_default as column_default,
|
|
81
|
+
column_comment as column_comment,
|
|
82
|
+
is_nullable as is_nullable,
|
|
83
|
+
data_type as data_type,
|
|
84
|
+
column_type as column_type,
|
|
85
|
+
column_key as column_key,
|
|
86
|
+
extra as extra,
|
|
87
|
+
generation_expression as generation_expression,
|
|
88
|
+
numeric_precision as numeric_precision,
|
|
89
|
+
numeric_scale as numeric_scale,
|
|
90
|
+
ifnull(datetime_precision, character_maximum_length) length
|
|
91
|
+
from information_schema.columns where table_schema = database() and table_name in (${tables.map(t => this.platform.quoteValue(t.table_name))})
|
|
92
|
+
order by ordinal_position`;
|
|
93
|
+
const allColumns = await connection.execute(sql);
|
|
94
|
+
const str = (val) => val != null ? '' + val : val;
|
|
95
|
+
const extra = (val) => val.replace(/auto_increment|default_generated|(stored|virtual) generated/i, '').trim();
|
|
96
|
+
const ret = {};
|
|
97
|
+
for (const col of allColumns) {
|
|
98
|
+
const mappedType = this.platform.getMappedType(col.column_type);
|
|
99
|
+
const defaultValue = str(this.normalizeDefaultValue((mappedType.compareAsType() === 'boolean' && ['0', '1'].includes(col.column_default))
|
|
100
|
+
? ['false', 'true'][+col.column_default]
|
|
101
|
+
: col.column_default, col.length));
|
|
102
|
+
const key = this.getTableKey(col);
|
|
103
|
+
const generated = col.generation_expression ? `(${col.generation_expression.replaceAll(`\\'`, `'`)}) ${col.extra.match(/stored generated/i) ? 'stored' : 'virtual'}` : undefined;
|
|
104
|
+
ret[key] ??= [];
|
|
105
|
+
ret[key].push({
|
|
106
|
+
name: col.column_name,
|
|
107
|
+
type: this.platform.isNumericColumn(mappedType) ? col.column_type.replace(/ unsigned$/, '').replace(/\(\d+\)$/, '') : col.column_type,
|
|
108
|
+
mappedType,
|
|
109
|
+
unsigned: col.column_type.endsWith(' unsigned'),
|
|
110
|
+
length: col.length,
|
|
111
|
+
default: this.wrap(defaultValue, mappedType),
|
|
112
|
+
nullable: col.is_nullable === 'YES',
|
|
113
|
+
primary: col.column_key === 'PRI',
|
|
114
|
+
unique: col.column_key === 'UNI',
|
|
115
|
+
autoincrement: col.extra === 'auto_increment',
|
|
116
|
+
precision: col.numeric_precision,
|
|
117
|
+
scale: col.numeric_scale,
|
|
118
|
+
comment: col.column_comment,
|
|
119
|
+
extra: extra(col.extra),
|
|
120
|
+
generated,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return ret;
|
|
124
|
+
}
|
|
125
|
+
async getAllChecks(connection, tables) {
|
|
126
|
+
/* istanbul ignore next */
|
|
127
|
+
if (!await this.supportsCheckConstraints(connection)) {
|
|
128
|
+
return {};
|
|
129
|
+
}
|
|
130
|
+
const sql = this.getChecksSQL(tables);
|
|
131
|
+
const allChecks = await connection.execute(sql);
|
|
132
|
+
const ret = {};
|
|
133
|
+
for (const check of allChecks) {
|
|
134
|
+
const key = this.getTableKey(check);
|
|
135
|
+
ret[key] ??= [];
|
|
136
|
+
ret[key].push({
|
|
137
|
+
name: check.name,
|
|
138
|
+
columnName: check.column_name,
|
|
139
|
+
definition: `check ${check.expression}`,
|
|
140
|
+
expression: check.expression.replace(/^\((.*)\)$/, '$1'),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return ret;
|
|
144
|
+
}
|
|
145
|
+
async getAllForeignKeys(connection, tables) {
|
|
146
|
+
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
|
|
147
|
+
from information_schema.key_column_usage k
|
|
148
|
+
inner join information_schema.referential_constraints c on c.constraint_name = k.constraint_name and c.table_name = k.table_name
|
|
149
|
+
where k.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(', ')})
|
|
150
|
+
and k.table_schema = database() and c.constraint_schema = database() and k.referenced_column_name is not null
|
|
151
|
+
order by constraint_name, k.ordinal_position`;
|
|
152
|
+
const allFks = await connection.execute(sql);
|
|
153
|
+
const ret = {};
|
|
154
|
+
for (const fk of allFks) {
|
|
155
|
+
const key = this.getTableKey(fk);
|
|
156
|
+
ret[key] ??= [];
|
|
157
|
+
ret[key].push(fk);
|
|
158
|
+
}
|
|
159
|
+
Object.keys(ret).forEach(key => {
|
|
160
|
+
const parts = key.split('.');
|
|
161
|
+
/* istanbul ignore next */
|
|
162
|
+
const schemaName = parts.length > 1 ? parts[0] : undefined;
|
|
163
|
+
ret[key] = this.mapForeignKeys(ret[key], key, schemaName);
|
|
164
|
+
});
|
|
165
|
+
return ret;
|
|
166
|
+
}
|
|
167
|
+
getPreAlterTable(tableDiff, safe) {
|
|
168
|
+
// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
|
|
169
|
+
const pk = Object.values(tableDiff.removedIndexes).find(idx => idx.primary);
|
|
170
|
+
if (!pk || safe) {
|
|
171
|
+
return '';
|
|
172
|
+
}
|
|
173
|
+
return pk.columnNames
|
|
174
|
+
.filter(col => tableDiff.fromTable.hasColumn(col))
|
|
175
|
+
.map(col => tableDiff.fromTable.getColumn(col))
|
|
176
|
+
.filter(col => col.autoincrement)
|
|
177
|
+
.map(col => `alter table \`${tableDiff.name}\` modify \`${col.name}\` ${this.getColumnDeclarationSQL({ ...col, autoincrement: false })}`)
|
|
178
|
+
.join(';\n');
|
|
179
|
+
}
|
|
180
|
+
configureColumnDefault(column, col, knex, changedProperties) {
|
|
181
|
+
if (changedProperties || column.default !== undefined) {
|
|
182
|
+
if (column.default == null) {
|
|
183
|
+
col.defaultTo(null);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
col.defaultTo(knex.raw(column.default + (column.extra ? ' ' + column.extra : '')));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return col;
|
|
190
|
+
}
|
|
191
|
+
getRenameColumnSQL(tableName, oldColumnName, to) {
|
|
192
|
+
tableName = this.platform.quoteIdentifier(tableName);
|
|
193
|
+
oldColumnName = this.platform.quoteIdentifier(oldColumnName);
|
|
194
|
+
const columnName = this.platform.quoteIdentifier(to.name);
|
|
195
|
+
return `alter table ${tableName} change ${oldColumnName} ${columnName} ${this.getColumnDeclarationSQL(to)}`;
|
|
196
|
+
}
|
|
197
|
+
getRenameIndexSQL(tableName, index, oldIndexName) {
|
|
198
|
+
tableName = this.platform.quoteIdentifier(tableName);
|
|
199
|
+
oldIndexName = this.platform.quoteIdentifier(oldIndexName);
|
|
200
|
+
const keyName = this.platform.quoteIdentifier(index.keyName);
|
|
201
|
+
return `alter table ${tableName} rename index ${oldIndexName} to ${keyName}`;
|
|
202
|
+
}
|
|
203
|
+
getChangeColumnCommentSQL(tableName, to, schemaName) {
|
|
204
|
+
tableName = this.platform.quoteIdentifier(tableName);
|
|
205
|
+
const columnName = this.platform.quoteIdentifier(to.name);
|
|
206
|
+
return `alter table ${tableName} modify ${columnName} ${this.getColumnDeclarationSQL(to)}`;
|
|
207
|
+
}
|
|
208
|
+
createTableColumn(table, column, fromTable, changedProperties) {
|
|
209
|
+
if (column.mappedType instanceof core_1.MediumIntType) {
|
|
210
|
+
return table.specificType(column.name, this.getColumnDeclarationSQL(column, true));
|
|
211
|
+
}
|
|
212
|
+
return super.createTableColumn(table, column, fromTable, changedProperties);
|
|
213
|
+
}
|
|
214
|
+
configureColumn(column, col, knex, changedProperties) {
|
|
215
|
+
if (column.mappedType instanceof core_1.MediumIntType) {
|
|
216
|
+
return col;
|
|
217
|
+
}
|
|
218
|
+
return super.configureColumn(column, col, knex, changedProperties);
|
|
219
|
+
}
|
|
220
|
+
getColumnDeclarationSQL(col, addPrimary = false) {
|
|
221
|
+
let ret = col.type;
|
|
222
|
+
ret += col.unsigned ? ' unsigned' : '';
|
|
223
|
+
ret += col.autoincrement ? ' auto_increment' : '';
|
|
224
|
+
ret += ' ';
|
|
225
|
+
ret += col.nullable ? 'null' : 'not null';
|
|
226
|
+
ret += col.default ? ' default ' + col.default : '';
|
|
227
|
+
if (addPrimary && col.primary) {
|
|
228
|
+
ret += ' primary key';
|
|
229
|
+
}
|
|
230
|
+
ret += col.comment ? ` comment ${this.platform.quoteValue(col.comment)}` : '';
|
|
231
|
+
return ret;
|
|
232
|
+
}
|
|
233
|
+
/* istanbul ignore next kept for BC */
|
|
234
|
+
getForeignKeysSQL(tableName, schemaName) {
|
|
235
|
+
return `select distinct k.constraint_name as constraint_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 `
|
|
236
|
+
+ `from information_schema.key_column_usage k `
|
|
237
|
+
+ `inner join information_schema.referential_constraints c on c.constraint_name = k.constraint_name and c.table_name = '${tableName}' `
|
|
238
|
+
+ `where k.table_name = '${tableName}' and k.table_schema = database() and c.constraint_schema = database() and k.referenced_column_name is not null`;
|
|
239
|
+
}
|
|
240
|
+
async getAllEnumDefinitions(connection, tables) {
|
|
241
|
+
const sql = `select column_name as column_name, column_type as column_type, table_name as table_name
|
|
242
|
+
from information_schema.columns
|
|
243
|
+
where data_type = 'enum' and table_name in (${tables.map(t => `'${t.table_name}'`).join(', ')}) and table_schema = database()`;
|
|
244
|
+
const enums = await connection.execute(sql);
|
|
245
|
+
return enums.reduce((o, item) => {
|
|
246
|
+
o[item.table_name] ??= {};
|
|
247
|
+
o[item.table_name][item.column_name] = item.column_type.match(/enum\((.*)\)/)[1].split(',').map((item) => item.match(/'(.*)'/)[1]);
|
|
248
|
+
return o;
|
|
249
|
+
}, {});
|
|
250
|
+
}
|
|
251
|
+
async supportsCheckConstraints(connection) {
|
|
252
|
+
if (this._cache.supportsCheckConstraints != null) {
|
|
253
|
+
return this._cache.supportsCheckConstraints;
|
|
254
|
+
}
|
|
255
|
+
const sql = `select 1 from information_schema.tables where table_name = 'CHECK_CONSTRAINTS' and table_schema = 'information_schema'`;
|
|
256
|
+
const res = await connection.execute(sql);
|
|
257
|
+
return this._cache.supportsCheckConstraints = res.length > 0;
|
|
258
|
+
}
|
|
259
|
+
getChecksSQL(tables) {
|
|
260
|
+
return `select cc.constraint_schema as table_schema, tc.table_name as table_name, cc.constraint_name as name, cc.check_clause as expression
|
|
261
|
+
from information_schema.check_constraints cc
|
|
262
|
+
join information_schema.table_constraints tc
|
|
263
|
+
on tc.constraint_schema = cc.constraint_schema
|
|
264
|
+
and tc.constraint_name = cc.constraint_name
|
|
265
|
+
and constraint_type = 'CHECK'
|
|
266
|
+
where tc.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name))}) and tc.constraint_schema = database()
|
|
267
|
+
order by tc.constraint_name`;
|
|
268
|
+
}
|
|
269
|
+
/* istanbul ignore next */
|
|
270
|
+
async getChecks(connection, tableName, schemaName, columns) {
|
|
271
|
+
const res = await this.getAllChecks(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
272
|
+
return res[tableName];
|
|
273
|
+
}
|
|
274
|
+
/* istanbul ignore next */
|
|
275
|
+
async getEnumDefinitions(connection, checks, tableName, schemaName) {
|
|
276
|
+
const res = await this.getAllEnumDefinitions(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
277
|
+
return res[tableName];
|
|
278
|
+
}
|
|
279
|
+
/* istanbul ignore next */
|
|
280
|
+
async getColumns(connection, tableName, schemaName) {
|
|
281
|
+
const res = await this.getAllColumns(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
282
|
+
return res[tableName];
|
|
283
|
+
}
|
|
284
|
+
/* istanbul ignore next */
|
|
285
|
+
async getIndexes(connection, tableName, schemaName) {
|
|
286
|
+
const res = await this.getAllIndexes(connection, [{ table_name: tableName, schema_name: schemaName }]);
|
|
287
|
+
return res[tableName];
|
|
288
|
+
}
|
|
289
|
+
normalizeDefaultValue(defaultValue, length) {
|
|
290
|
+
return super.normalizeDefaultValue(defaultValue, length, MySqlSchemaHelper.DEFAULT_VALUES);
|
|
291
|
+
}
|
|
292
|
+
wrap(val, type) {
|
|
293
|
+
const stringType = type instanceof core_1.StringType || type instanceof core_1.TextType || type instanceof core_1.EnumType;
|
|
294
|
+
return typeof val === 'string' && val.length > 0 && stringType ? this.platform.quoteValue(val) : val;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
exports.MySqlSchemaHelper = MySqlSchemaHelper;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./MariaDbKnexDialect"), exports);
|
|
18
|
+
__exportStar(require("./MySqlKnexDialect"), exports);
|
|
19
|
+
__exportStar(require("./MySqlConnection"), exports);
|
|
20
|
+
__exportStar(require("./MySqlExceptionConverter"), exports);
|
|
21
|
+
__exportStar(require("./MySqlSchemaHelper"), exports);
|
|
22
|
+
__exportStar(require("./MySqlPlatform"), exports);
|