@mikro-orm/sql 7.0.3-dev.9 → 7.0.3
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 +94 -58
- package/AbstractSqlConnection.js +235 -238
- package/AbstractSqlDriver.d.ts +410 -155
- package/AbstractSqlDriver.js +2064 -1937
- package/AbstractSqlPlatform.d.ts +83 -73
- package/AbstractSqlPlatform.js +162 -158
- package/PivotCollectionPersister.d.ts +33 -15
- package/PivotCollectionPersister.js +158 -160
- package/README.md +2 -2
- package/SqlEntityManager.d.ts +67 -22
- package/SqlEntityManager.js +54 -38
- package/SqlEntityRepository.d.ts +14 -14
- package/SqlEntityRepository.js +23 -23
- package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -12
- package/dialects/mssql/MsSqlNativeQueryBuilder.js +192 -194
- package/dialects/mysql/BaseMySqlPlatform.d.ts +64 -45
- package/dialects/mysql/BaseMySqlPlatform.js +134 -131
- package/dialects/mysql/MySqlExceptionConverter.d.ts +6 -6
- package/dialects/mysql/MySqlExceptionConverter.js +91 -77
- package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +3 -3
- package/dialects/mysql/MySqlNativeQueryBuilder.js +66 -69
- package/dialects/mysql/MySqlSchemaHelper.d.ts +39 -39
- package/dialects/mysql/MySqlSchemaHelper.js +327 -319
- package/dialects/oracledb/OracleDialect.d.ts +81 -52
- package/dialects/oracledb/OracleDialect.js +155 -149
- package/dialects/oracledb/OracleNativeQueryBuilder.d.ts +12 -12
- package/dialects/oracledb/OracleNativeQueryBuilder.js +232 -236
- package/dialects/postgresql/BasePostgreSqlPlatform.d.ts +108 -105
- package/dialects/postgresql/BasePostgreSqlPlatform.js +351 -350
- package/dialects/postgresql/FullTextType.d.ts +10 -6
- package/dialects/postgresql/FullTextType.js +51 -51
- package/dialects/postgresql/PostgreSqlExceptionConverter.d.ts +5 -5
- package/dialects/postgresql/PostgreSqlExceptionConverter.js +55 -43
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +4 -4
- package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +102 -82
- package/dialects/postgresql/PostgreSqlSchemaHelper.js +733 -683
- package/dialects/sqlite/BaseSqliteConnection.d.ts +3 -5
- package/dialects/sqlite/BaseSqliteConnection.js +21 -19
- package/dialects/sqlite/NodeSqliteDialect.d.ts +1 -1
- package/dialects/sqlite/NodeSqliteDialect.js +23 -23
- package/dialects/sqlite/SqliteDriver.d.ts +1 -1
- package/dialects/sqlite/SqliteDriver.js +3 -3
- package/dialects/sqlite/SqliteExceptionConverter.d.ts +6 -6
- package/dialects/sqlite/SqliteExceptionConverter.js +67 -51
- package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +2 -2
- package/dialects/sqlite/SqliteNativeQueryBuilder.js +7 -7
- package/dialects/sqlite/SqlitePlatform.d.ts +63 -72
- package/dialects/sqlite/SqlitePlatform.js +139 -139
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +70 -60
- package/dialects/sqlite/SqliteSchemaHelper.js +533 -520
- package/package.json +3 -3
- package/plugin/index.d.ts +42 -35
- package/plugin/index.js +43 -36
- package/plugin/transformer.d.ts +117 -94
- package/plugin/transformer.js +890 -881
- package/query/ArrayCriteriaNode.d.ts +4 -4
- package/query/ArrayCriteriaNode.js +18 -18
- package/query/CriteriaNode.d.ts +35 -25
- package/query/CriteriaNode.js +133 -123
- package/query/CriteriaNodeFactory.d.ts +49 -6
- package/query/CriteriaNodeFactory.js +97 -94
- package/query/NativeQueryBuilder.d.ts +118 -118
- package/query/NativeQueryBuilder.js +484 -480
- package/query/ObjectCriteriaNode.d.ts +12 -12
- package/query/ObjectCriteriaNode.js +298 -282
- package/query/QueryBuilder.d.ts +1546 -904
- package/query/QueryBuilder.js +2294 -2144
- package/query/QueryBuilderHelper.d.ts +153 -72
- package/query/QueryBuilderHelper.js +1079 -1028
- package/query/ScalarCriteriaNode.d.ts +3 -3
- package/query/ScalarCriteriaNode.js +53 -46
- package/query/enums.d.ts +14 -14
- package/query/enums.js +14 -14
- package/query/raw.d.ts +16 -6
- package/query/raw.js +10 -10
- package/schema/DatabaseSchema.d.ts +73 -50
- package/schema/DatabaseSchema.js +331 -307
- package/schema/DatabaseTable.d.ts +96 -73
- package/schema/DatabaseTable.js +1012 -927
- package/schema/SchemaComparator.d.ts +70 -66
- package/schema/SchemaComparator.js +766 -740
- package/schema/SchemaHelper.d.ts +109 -95
- package/schema/SchemaHelper.js +675 -659
- package/schema/SqlSchemaGenerator.d.ts +78 -58
- package/schema/SqlSchemaGenerator.js +535 -501
- package/typings.d.ts +380 -266
|
@@ -1,519 +1,553 @@
|
|
|
1
|
-
import { CommitOrderCalculator, TableNotFoundException, Utils
|
|
1
|
+
import { CommitOrderCalculator, TableNotFoundException, Utils } from '@mikro-orm/core';
|
|
2
2
|
import { AbstractSchemaGenerator } from '@mikro-orm/core/schema';
|
|
3
3
|
import { DatabaseSchema } from './DatabaseSchema.js';
|
|
4
4
|
import { SchemaComparator } from './SchemaComparator.js';
|
|
5
5
|
/** Generates and manages SQL database schemas based on entity metadata. Supports create, update, and drop operations. */
|
|
6
6
|
export class SqlSchemaGenerator extends AbstractSchemaGenerator {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
const exists = await this.helper.databaseExists(this.connection, dbName);
|
|
28
|
-
this.lastEnsuredDatabase = dbName;
|
|
29
|
-
if (!exists) {
|
|
30
|
-
const managementDbName = this.helper.getManagementDbName();
|
|
31
|
-
if (managementDbName) {
|
|
32
|
-
this.config.set('dbName', managementDbName);
|
|
33
|
-
await this.driver.reconnect({ skipOnConnect: true });
|
|
34
|
-
await this.createDatabase(dbName, { skipOnConnect: true });
|
|
35
|
-
}
|
|
36
|
-
if (options?.create) {
|
|
37
|
-
await this.create(options);
|
|
38
|
-
}
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
/* v8 ignore next */
|
|
42
|
-
if (options?.clear) {
|
|
43
|
-
await this.clear({ ...options, clearIdentityMap: false });
|
|
44
|
-
}
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
getTargetSchema(schema) {
|
|
48
|
-
const metadata = this.getOrderedMetadata(schema);
|
|
49
|
-
const schemaName = schema ?? this.config.get('schema') ?? this.platform.getDefaultSchemaName();
|
|
50
|
-
return DatabaseSchema.fromMetadata(metadata, this.platform, this.config, schemaName, this.em);
|
|
51
|
-
}
|
|
52
|
-
getOrderedMetadata(schema) {
|
|
53
|
-
const metadata = super.getOrderedMetadata(schema);
|
|
54
|
-
// Filter out skipped tables
|
|
55
|
-
return metadata.filter(meta => {
|
|
56
|
-
const tableName = meta.tableName;
|
|
57
|
-
const tableSchema = meta.schema ?? schema ?? this.config.get('schema');
|
|
58
|
-
return !this.isTableSkipped(tableName, tableSchema);
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
async getCreateSchemaSQL(options = {}) {
|
|
62
|
-
const toSchema = this.getTargetSchema(options.schema);
|
|
63
|
-
const ret = [];
|
|
64
|
-
for (const namespace of toSchema.getNamespaces()) {
|
|
65
|
-
if (namespace === this.platform.getDefaultSchemaName()) {
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
const sql = this.helper.getCreateNamespaceSQL(namespace);
|
|
69
|
-
this.append(ret, sql);
|
|
70
|
-
}
|
|
71
|
-
if (this.platform.supportsNativeEnums()) {
|
|
72
|
-
const created = [];
|
|
73
|
-
for (const [enumName, enumOptions] of Object.entries(toSchema.getNativeEnums())) {
|
|
74
|
-
/* v8 ignore next */
|
|
75
|
-
if (created.includes(enumName)) {
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
created.push(enumName);
|
|
79
|
-
const sql = this.helper.getCreateNativeEnumSQL(enumOptions.name, enumOptions.items, this.getSchemaName(enumOptions, options));
|
|
80
|
-
this.append(ret, sql);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
for (const table of toSchema.getTables()) {
|
|
84
|
-
this.append(ret, this.helper.createTable(table), true);
|
|
85
|
-
}
|
|
86
|
-
if (this.helper.supportsSchemaConstraints()) {
|
|
87
|
-
for (const table of toSchema.getTables()) {
|
|
88
|
-
const fks = Object.values(table.getForeignKeys()).map(fk => this.helper.createForeignKey(table, fk));
|
|
89
|
-
this.append(ret, fks, true);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
// Create views after tables (views may depend on tables)
|
|
93
|
-
// Sort views by dependencies (views depending on other views come later)
|
|
94
|
-
const sortedViews = this.sortViewsByDependencies(toSchema.getViews());
|
|
95
|
-
for (const view of sortedViews) {
|
|
96
|
-
if (view.materialized) {
|
|
97
|
-
this.append(ret, this.helper.createMaterializedView(view.name, view.schema, view.definition, view.withData ?? true));
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
this.append(ret, this.helper.createView(view.name, view.schema, view.definition), true);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return this.wrapSchema(ret, options);
|
|
7
|
+
helper = this.platform.getSchemaHelper();
|
|
8
|
+
options = this.config.get('schemaGenerator');
|
|
9
|
+
lastEnsuredDatabase;
|
|
10
|
+
static register(orm) {
|
|
11
|
+
orm.config.registerExtension('@mikro-orm/schema-generator', () => new SqlSchemaGenerator(orm.em));
|
|
12
|
+
}
|
|
13
|
+
async create(options) {
|
|
14
|
+
await this.ensureDatabase();
|
|
15
|
+
const sql = await this.getCreateSchemaSQL(options);
|
|
16
|
+
await this.execute(sql);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Returns true if the database was created.
|
|
20
|
+
*/
|
|
21
|
+
async ensureDatabase(options) {
|
|
22
|
+
await this.connection.ensureConnection();
|
|
23
|
+
const dbName = this.config.get('dbName');
|
|
24
|
+
if (this.lastEnsuredDatabase === dbName && !options?.forceCheck) {
|
|
25
|
+
return true;
|
|
104
26
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
await this.
|
|
27
|
+
const exists = await this.helper.databaseExists(this.connection, dbName);
|
|
28
|
+
this.lastEnsuredDatabase = dbName;
|
|
29
|
+
if (!exists) {
|
|
30
|
+
const managementDbName = this.helper.getManagementDbName();
|
|
31
|
+
if (managementDbName) {
|
|
32
|
+
this.config.set('dbName', managementDbName);
|
|
33
|
+
await this.driver.reconnect({ skipOnConnect: true });
|
|
34
|
+
await this.createDatabase(dbName, { skipOnConnect: true });
|
|
35
|
+
}
|
|
36
|
+
if (options?.create) {
|
|
37
|
+
await this.create(options);
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
112
40
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
41
|
+
/* v8 ignore next */
|
|
42
|
+
if (options?.clear) {
|
|
43
|
+
await this.clear({ ...options, clearIdentityMap: false });
|
|
116
44
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
getTargetSchema(schema) {
|
|
48
|
+
const metadata = this.getOrderedMetadata(schema);
|
|
49
|
+
const schemaName = schema ?? this.config.get('schema') ?? this.platform.getDefaultSchemaName();
|
|
50
|
+
return DatabaseSchema.fromMetadata(metadata, this.platform, this.config, schemaName, this.em);
|
|
51
|
+
}
|
|
52
|
+
getOrderedMetadata(schema) {
|
|
53
|
+
const metadata = super.getOrderedMetadata(schema);
|
|
54
|
+
// Filter out skipped tables
|
|
55
|
+
return metadata.filter(meta => {
|
|
56
|
+
const tableName = meta.tableName;
|
|
57
|
+
const tableSchema = meta.schema ?? schema ?? this.config.get('schema');
|
|
58
|
+
return !this.isTableSkipped(tableName, tableSchema);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async getCreateSchemaSQL(options = {}) {
|
|
62
|
+
const toSchema = this.getTargetSchema(options.schema);
|
|
63
|
+
const ret = [];
|
|
64
|
+
for (const namespace of toSchema.getNamespaces()) {
|
|
65
|
+
if (namespace === this.platform.getDefaultSchemaName()) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const sql = this.helper.getCreateNamespaceSQL(namespace);
|
|
69
|
+
this.append(ret, sql);
|
|
120
70
|
}
|
|
121
|
-
|
|
122
|
-
|
|
71
|
+
if (this.platform.supportsNativeEnums()) {
|
|
72
|
+
const created = [];
|
|
73
|
+
for (const [enumName, enumOptions] of Object.entries(toSchema.getNativeEnums())) {
|
|
123
74
|
/* v8 ignore next */
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
.withSchema(schema)
|
|
136
|
-
.truncate()
|
|
137
|
-
.execute();
|
|
138
|
-
}
|
|
139
|
-
catch (e) {
|
|
140
|
-
if (this.platform.getExceptionConverter().convertException(e) instanceof TableNotFoundException) {
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
throw e;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
if (this.options.disableForeignKeysForClear) {
|
|
147
|
-
await this.execute(this.helper.enableForeignKeysSQL());
|
|
148
|
-
}
|
|
149
|
-
if (options?.clearIdentityMap ?? true) {
|
|
150
|
-
this.clearIdentityMap();
|
|
151
|
-
}
|
|
75
|
+
if (created.includes(enumName)) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
created.push(enumName);
|
|
79
|
+
const sql = this.helper.getCreateNativeEnumSQL(
|
|
80
|
+
enumOptions.name,
|
|
81
|
+
enumOptions.items,
|
|
82
|
+
this.getSchemaName(enumOptions, options),
|
|
83
|
+
);
|
|
84
|
+
this.append(ret, sql);
|
|
85
|
+
}
|
|
152
86
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
for (const meta of metadata) {
|
|
185
|
-
this.append(ret, this.helper.dropTableIfExists(meta.tableName, this.getSchemaName(meta, options)));
|
|
186
|
-
}
|
|
187
|
-
if (this.platform.supportsNativeEnums()) {
|
|
188
|
-
for (const columnName of Object.keys(schema.getNativeEnums())) {
|
|
189
|
-
const sql = this.helper.getDropNativeEnumSQL(columnName, options.schema ?? this.config.get('schema'));
|
|
190
|
-
this.append(ret, sql);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
if (options.dropMigrationsTable) {
|
|
194
|
-
this.append(ret, this.helper.dropTableIfExists(this.config.get('migrations').tableName, this.config.get('schema')));
|
|
195
|
-
}
|
|
196
|
-
return this.wrapSchema(ret, options);
|
|
87
|
+
for (const table of toSchema.getTables()) {
|
|
88
|
+
this.append(ret, this.helper.createTable(table), true);
|
|
89
|
+
}
|
|
90
|
+
if (this.helper.supportsSchemaConstraints()) {
|
|
91
|
+
for (const table of toSchema.getTables()) {
|
|
92
|
+
const fks = Object.values(table.getForeignKeys()).map(fk => this.helper.createForeignKey(table, fk));
|
|
93
|
+
this.append(ret, fks, true);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Create views after tables (views may depend on tables)
|
|
97
|
+
// Sort views by dependencies (views depending on other views come later)
|
|
98
|
+
const sortedViews = this.sortViewsByDependencies(toSchema.getViews());
|
|
99
|
+
for (const view of sortedViews) {
|
|
100
|
+
if (view.materialized) {
|
|
101
|
+
this.append(
|
|
102
|
+
ret,
|
|
103
|
+
this.helper.createMaterializedView(view.name, view.schema, view.definition, view.withData ?? true),
|
|
104
|
+
);
|
|
105
|
+
} else {
|
|
106
|
+
this.append(ret, this.helper.createView(view.name, view.schema, view.definition), true);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return this.wrapSchema(ret, options);
|
|
110
|
+
}
|
|
111
|
+
async drop(options = {}) {
|
|
112
|
+
if (options.dropDb) {
|
|
113
|
+
const name = this.config.get('dbName');
|
|
114
|
+
return this.dropDatabase(name);
|
|
197
115
|
}
|
|
198
|
-
|
|
199
|
-
|
|
116
|
+
const sql = await this.getDropSchemaSQL(options);
|
|
117
|
+
await this.execute(sql);
|
|
118
|
+
}
|
|
119
|
+
async createNamespace(name) {
|
|
120
|
+
const sql = this.helper.getCreateNamespaceSQL(name);
|
|
121
|
+
await this.execute(sql);
|
|
122
|
+
}
|
|
123
|
+
async dropNamespace(name) {
|
|
124
|
+
const sql = this.helper.getDropNamespaceSQL(name);
|
|
125
|
+
await this.execute(sql);
|
|
126
|
+
}
|
|
127
|
+
async clear(options) {
|
|
128
|
+
// truncate by default, so no value is considered as true
|
|
129
|
+
/* v8 ignore next */
|
|
130
|
+
if (options?.truncate === false) {
|
|
131
|
+
return super.clear(options);
|
|
132
|
+
}
|
|
133
|
+
if (this.options.disableForeignKeysForClear) {
|
|
134
|
+
await this.execute(this.helper.disableForeignKeysSQL());
|
|
135
|
+
}
|
|
136
|
+
const schema = options?.schema ?? this.config.get('schema', this.platform.getDefaultSchemaName());
|
|
137
|
+
for (const meta of this.getOrderedMetadata(schema).reverse()) {
|
|
138
|
+
try {
|
|
139
|
+
await this.driver
|
|
140
|
+
.createQueryBuilder(meta.class, this.em?.getTransactionContext(), 'write', false)
|
|
141
|
+
.withSchema(schema)
|
|
142
|
+
.truncate()
|
|
143
|
+
.execute();
|
|
144
|
+
} catch (e) {
|
|
145
|
+
if (this.platform.getExceptionConverter().convertException(e) instanceof TableNotFoundException) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
throw e;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (this.options.disableForeignKeysForClear) {
|
|
152
|
+
await this.execute(this.helper.enableForeignKeysSQL());
|
|
153
|
+
}
|
|
154
|
+
if (options?.clearIdentityMap ?? true) {
|
|
155
|
+
this.clearIdentityMap();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async getDropSchemaSQL(options = {}) {
|
|
159
|
+
await this.ensureDatabase();
|
|
160
|
+
const metadata = this.getOrderedMetadata(options.schema).reverse();
|
|
161
|
+
const schemas = this.getTargetSchema(options.schema).getNamespaces();
|
|
162
|
+
const schema = await DatabaseSchema.create(
|
|
163
|
+
this.connection,
|
|
164
|
+
this.platform,
|
|
165
|
+
this.config,
|
|
166
|
+
options.schema,
|
|
167
|
+
schemas,
|
|
168
|
+
undefined,
|
|
169
|
+
this.options.skipTables,
|
|
170
|
+
this.options.skipViews,
|
|
171
|
+
);
|
|
172
|
+
const ret = [];
|
|
173
|
+
// Drop views first (views may depend on tables)
|
|
174
|
+
// Drop in reverse dependency order (dependent views first)
|
|
175
|
+
const targetSchema = this.getTargetSchema(options.schema);
|
|
176
|
+
const sortedViews = this.sortViewsByDependencies(targetSchema.getViews()).reverse();
|
|
177
|
+
for (const view of sortedViews) {
|
|
178
|
+
if (view.materialized) {
|
|
179
|
+
this.append(ret, this.helper.dropMaterializedViewIfExists(view.name, view.schema));
|
|
180
|
+
} else {
|
|
181
|
+
this.append(ret, this.helper.dropViewIfExists(view.name, view.schema));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// remove FKs explicitly if we can't use a cascading statement and we don't disable FK checks (we need this for circular relations)
|
|
185
|
+
for (const meta of metadata) {
|
|
186
|
+
if (!this.platform.usesCascadeStatement() && (!this.options.disableForeignKeys || options.dropForeignKeys)) {
|
|
187
|
+
const table = schema.getTable(meta.tableName);
|
|
188
|
+
if (!table) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const foreignKeys = Object.values(table.getForeignKeys());
|
|
192
|
+
for (const fk of foreignKeys) {
|
|
193
|
+
this.append(ret, this.helper.dropForeignKey(table.getShortestName(), fk.constraintName));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
for (const meta of metadata) {
|
|
198
|
+
this.append(ret, this.helper.dropTableIfExists(meta.tableName, this.getSchemaName(meta, options)));
|
|
199
|
+
}
|
|
200
|
+
if (this.platform.supportsNativeEnums()) {
|
|
201
|
+
for (const columnName of Object.keys(schema.getNativeEnums())) {
|
|
202
|
+
const sql = this.helper.getDropNativeEnumSQL(columnName, options.schema ?? this.config.get('schema'));
|
|
203
|
+
this.append(ret, sql);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (options.dropMigrationsTable) {
|
|
207
|
+
this.append(
|
|
208
|
+
ret,
|
|
209
|
+
this.helper.dropTableIfExists(this.config.get('migrations').tableName, this.config.get('schema')),
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return this.wrapSchema(ret, options);
|
|
213
|
+
}
|
|
214
|
+
getSchemaName(meta, options) {
|
|
215
|
+
const schemaName = options.schema ?? this.config.get('schema');
|
|
216
|
+
/* v8 ignore next */
|
|
217
|
+
const resolvedName = meta.schema && meta.schema === '*' ? schemaName : (meta.schema ?? schemaName);
|
|
218
|
+
// skip default schema name
|
|
219
|
+
if (resolvedName === this.platform.getDefaultSchemaName()) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
return resolvedName;
|
|
223
|
+
}
|
|
224
|
+
async update(options = {}) {
|
|
225
|
+
const sql = await this.getUpdateSchemaSQL(options);
|
|
226
|
+
await this.execute(sql);
|
|
227
|
+
}
|
|
228
|
+
async getUpdateSchemaSQL(options = {}) {
|
|
229
|
+
await this.ensureDatabase();
|
|
230
|
+
const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
|
|
231
|
+
const comparator = new SchemaComparator(this.platform);
|
|
232
|
+
const diffUp = comparator.compare(fromSchema, toSchema);
|
|
233
|
+
return this.diffToSQL(diffUp, options);
|
|
234
|
+
}
|
|
235
|
+
async getUpdateSchemaMigrationSQL(options = {}) {
|
|
236
|
+
if (!options.fromSchema) {
|
|
237
|
+
await this.ensureDatabase();
|
|
238
|
+
}
|
|
239
|
+
const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
|
|
240
|
+
const comparator = new SchemaComparator(this.platform);
|
|
241
|
+
const diffUp = comparator.compare(fromSchema, toSchema);
|
|
242
|
+
const diffDown = comparator.compare(toSchema, fromSchema, diffUp);
|
|
243
|
+
return {
|
|
244
|
+
up: this.diffToSQL(diffUp, options),
|
|
245
|
+
down: this.platform.supportsDownMigrations() ? this.diffToSQL(diffDown, options) : '',
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
async prepareSchemaForComparison(options) {
|
|
249
|
+
options.safe ??= false;
|
|
250
|
+
options.dropTables ??= true;
|
|
251
|
+
const toSchema = this.getTargetSchema(options.schema);
|
|
252
|
+
const schemas = toSchema.getNamespaces();
|
|
253
|
+
const fromSchema =
|
|
254
|
+
options.fromSchema ??
|
|
255
|
+
(await DatabaseSchema.create(
|
|
256
|
+
this.connection,
|
|
257
|
+
this.platform,
|
|
258
|
+
this.config,
|
|
259
|
+
options.schema,
|
|
260
|
+
schemas,
|
|
261
|
+
undefined,
|
|
262
|
+
this.options.skipTables,
|
|
263
|
+
this.options.skipViews,
|
|
264
|
+
));
|
|
265
|
+
const wildcardSchemaTables = [...this.metadata.getAll().values()]
|
|
266
|
+
.filter(meta => meta.schema === '*')
|
|
267
|
+
.map(meta => meta.tableName);
|
|
268
|
+
fromSchema.prune(options.schema, wildcardSchemaTables);
|
|
269
|
+
toSchema.prune(options.schema, wildcardSchemaTables);
|
|
270
|
+
return { fromSchema, toSchema };
|
|
271
|
+
}
|
|
272
|
+
diffToSQL(schemaDiff, options) {
|
|
273
|
+
const ret = [];
|
|
274
|
+
globalThis.idx = 0;
|
|
275
|
+
if (this.platform.supportsSchemas()) {
|
|
276
|
+
for (const newNamespace of schemaDiff.newNamespaces) {
|
|
277
|
+
const sql = this.helper.getCreateNamespaceSQL(newNamespace);
|
|
278
|
+
this.append(ret, sql);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (this.platform.supportsNativeEnums()) {
|
|
282
|
+
for (const newNativeEnum of schemaDiff.newNativeEnums) {
|
|
283
|
+
const sql = this.helper.getCreateNativeEnumSQL(
|
|
284
|
+
newNativeEnum.name,
|
|
285
|
+
newNativeEnum.items,
|
|
286
|
+
this.getSchemaName(newNativeEnum, options),
|
|
287
|
+
);
|
|
288
|
+
this.append(ret, sql);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
// Drop removed and changed views first (before modifying tables they may depend on)
|
|
292
|
+
// Drop in reverse dependency order (dependent views first)
|
|
293
|
+
if (options.dropTables && !options.safe) {
|
|
294
|
+
const sortedRemovedViews = this.sortViewsByDependencies(Object.values(schemaDiff.removedViews)).reverse();
|
|
295
|
+
for (const view of sortedRemovedViews) {
|
|
296
|
+
if (view.materialized) {
|
|
297
|
+
this.append(ret, this.helper.dropMaterializedViewIfExists(view.name, view.schema));
|
|
298
|
+
} else {
|
|
299
|
+
this.append(ret, this.helper.dropViewIfExists(view.name, view.schema));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
// Drop changed views (they will be recreated after table changes)
|
|
304
|
+
// Also in reverse dependency order
|
|
305
|
+
const changedViewsFrom = Object.values(schemaDiff.changedViews).map(v => v.from);
|
|
306
|
+
const sortedChangedViewsFrom = this.sortViewsByDependencies(changedViewsFrom).reverse();
|
|
307
|
+
for (const view of sortedChangedViewsFrom) {
|
|
308
|
+
if (view.materialized) {
|
|
309
|
+
this.append(ret, this.helper.dropMaterializedViewIfExists(view.name, view.schema));
|
|
310
|
+
} else {
|
|
311
|
+
this.append(ret, this.helper.dropViewIfExists(view.name, view.schema));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (!options.safe && this.options.createForeignKeyConstraints) {
|
|
315
|
+
for (const orphanedForeignKey of schemaDiff.orphanedForeignKeys) {
|
|
316
|
+
const [schemaName, tableName] = this.helper.splitTableName(orphanedForeignKey.localTableName, true);
|
|
200
317
|
/* v8 ignore next */
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
const lines = this.wrapSchema(sql, options).split('\n');
|
|
401
|
-
const groups = [];
|
|
402
|
-
let i = 0;
|
|
403
|
-
for (const line of lines) {
|
|
404
|
-
if (line.trim() === '') {
|
|
405
|
-
if (groups[i]?.length > 0) {
|
|
406
|
-
i++;
|
|
407
|
-
}
|
|
408
|
-
continue;
|
|
409
|
-
}
|
|
410
|
-
groups[i] ??= [];
|
|
411
|
-
groups[i].push(line.trim());
|
|
412
|
-
}
|
|
413
|
-
if (groups.length === 0) {
|
|
414
|
-
return;
|
|
415
|
-
}
|
|
416
|
-
if (this.platform.supportsMultipleStatements()) {
|
|
417
|
-
for (const group of groups) {
|
|
418
|
-
const query = group.join('\n');
|
|
419
|
-
await this.driver.execute(query);
|
|
420
|
-
}
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
|
-
const statements = groups.flatMap(group => {
|
|
424
|
-
return group
|
|
425
|
-
.join('\n')
|
|
426
|
-
.split(';\n')
|
|
427
|
-
.map(s => s.trim())
|
|
428
|
-
.filter(s => s);
|
|
429
|
-
});
|
|
430
|
-
await Utils.runSerial(statements, stmt => this.driver.execute(stmt));
|
|
431
|
-
}
|
|
432
|
-
async dropTableIfExists(name, schema) {
|
|
433
|
-
const sql = this.helper.dropTableIfExists(name, schema);
|
|
434
|
-
return this.execute(sql);
|
|
435
|
-
}
|
|
436
|
-
wrapSchema(sql, options) {
|
|
437
|
-
const array = Utils.asArray(sql);
|
|
438
|
-
if (array.length === 0) {
|
|
439
|
-
return '';
|
|
440
|
-
}
|
|
441
|
-
if (array[array.length - 1] === '') {
|
|
442
|
-
array.pop();
|
|
443
|
-
}
|
|
444
|
-
if (options.wrap === false) {
|
|
445
|
-
return array.join('\n') + '\n';
|
|
446
|
-
}
|
|
447
|
-
let ret = this.helper.getSchemaBeginning(this.config.get('charset'), this.options.disableForeignKeys);
|
|
448
|
-
ret += array.join('\n') + '\n';
|
|
449
|
-
ret += this.helper.getSchemaEnd(this.options.disableForeignKeys);
|
|
450
|
-
return ret;
|
|
451
|
-
}
|
|
452
|
-
append(array, sql, pad) {
|
|
453
|
-
return this.helper.append(array, sql, pad);
|
|
454
|
-
}
|
|
455
|
-
matchName(name, nameToMatch) {
|
|
456
|
-
return typeof nameToMatch === 'string'
|
|
457
|
-
? name.toLocaleLowerCase() === nameToMatch.toLocaleLowerCase()
|
|
458
|
-
: nameToMatch.test(name);
|
|
459
|
-
}
|
|
460
|
-
isTableSkipped(tableName, schemaName) {
|
|
461
|
-
const skipTables = this.options.skipTables;
|
|
462
|
-
if (!skipTables || skipTables.length === 0) {
|
|
463
|
-
return false;
|
|
464
|
-
}
|
|
465
|
-
const fullTableName = schemaName ? `${schemaName}.${tableName}` : tableName;
|
|
466
|
-
return skipTables.some(pattern => this.matchName(tableName, pattern) || this.matchName(fullTableName, pattern));
|
|
467
|
-
}
|
|
468
|
-
/**
|
|
469
|
-
* Sorts views by their dependencies so that views depending on other views are created after their dependencies.
|
|
470
|
-
* Uses topological sort based on view definition string matching.
|
|
471
|
-
*/
|
|
472
|
-
sortViewsByDependencies(views) {
|
|
473
|
-
if (views.length <= 1) {
|
|
474
|
-
return views;
|
|
475
|
-
}
|
|
476
|
-
// Use CommitOrderCalculator for topological sort
|
|
477
|
-
const calc = new CommitOrderCalculator();
|
|
478
|
-
// Map views to numeric indices for the calculator
|
|
479
|
-
const viewToIndex = new Map();
|
|
480
|
-
const indexToView = new Map();
|
|
481
|
-
for (let i = 0; i < views.length; i++) {
|
|
482
|
-
viewToIndex.set(views[i], i);
|
|
483
|
-
indexToView.set(i, views[i]);
|
|
484
|
-
calc.addNode(i);
|
|
485
|
-
}
|
|
486
|
-
// Check each view's definition for references to other view names
|
|
487
|
-
for (const view of views) {
|
|
488
|
-
const definition = view.definition.toLowerCase();
|
|
489
|
-
const viewIndex = viewToIndex.get(view);
|
|
490
|
-
for (const otherView of views) {
|
|
491
|
-
if (otherView === view) {
|
|
492
|
-
continue;
|
|
493
|
-
}
|
|
494
|
-
// Check if the definition references the other view's name
|
|
495
|
-
// Use word boundary matching to avoid false positives
|
|
496
|
-
const patterns = [new RegExp(`\\b${this.escapeRegExp(otherView.name.toLowerCase())}\\b`)];
|
|
497
|
-
if (otherView.schema) {
|
|
498
|
-
patterns.push(new RegExp(`\\b${this.escapeRegExp(`${otherView.schema}.${otherView.name}`.toLowerCase())}\\b`));
|
|
499
|
-
}
|
|
500
|
-
for (const pattern of patterns) {
|
|
501
|
-
if (pattern.test(definition)) {
|
|
502
|
-
// view depends on otherView, so otherView must come first
|
|
503
|
-
// addDependency(from, to) puts `from` before `to` in result
|
|
504
|
-
const otherIndex = viewToIndex.get(otherView);
|
|
505
|
-
calc.addDependency(otherIndex, viewIndex, 1);
|
|
506
|
-
break;
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
// Sort and map back to views
|
|
512
|
-
return calc.sort().map(index => indexToView.get(index));
|
|
318
|
+
const name = (schemaName ? schemaName + '.' : '') + tableName;
|
|
319
|
+
this.append(ret, this.helper.dropForeignKey(name, orphanedForeignKey.constraintName));
|
|
320
|
+
}
|
|
321
|
+
if (schemaDiff.orphanedForeignKeys.length > 0) {
|
|
322
|
+
ret.push('');
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
for (const newTable of Object.values(schemaDiff.newTables)) {
|
|
326
|
+
this.append(ret, this.helper.createTable(newTable, true), true);
|
|
327
|
+
}
|
|
328
|
+
if (this.helper.supportsSchemaConstraints()) {
|
|
329
|
+
for (const newTable of Object.values(schemaDiff.newTables)) {
|
|
330
|
+
const sql = [];
|
|
331
|
+
if (this.options.createForeignKeyConstraints) {
|
|
332
|
+
const fks = Object.values(newTable.getForeignKeys()).map(fk => this.helper.createForeignKey(newTable, fk));
|
|
333
|
+
this.append(sql, fks);
|
|
334
|
+
}
|
|
335
|
+
for (const check of newTable.getChecks()) {
|
|
336
|
+
this.append(sql, this.helper.createCheck(newTable, check));
|
|
337
|
+
}
|
|
338
|
+
this.append(ret, sql, true);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (options.dropTables && !options.safe) {
|
|
342
|
+
for (const table of Object.values(schemaDiff.removedTables)) {
|
|
343
|
+
this.append(ret, this.helper.dropTableIfExists(table.name, table.schema));
|
|
344
|
+
}
|
|
345
|
+
if (Utils.hasObjectKeys(schemaDiff.removedTables)) {
|
|
346
|
+
ret.push('');
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
for (const changedTable of Object.values(schemaDiff.changedTables)) {
|
|
350
|
+
this.append(ret, this.preAlterTable(changedTable, options.safe), true);
|
|
351
|
+
}
|
|
352
|
+
for (const changedTable of Object.values(schemaDiff.changedTables)) {
|
|
353
|
+
this.append(ret, this.helper.alterTable(changedTable, options.safe), true);
|
|
354
|
+
}
|
|
355
|
+
for (const changedTable of Object.values(schemaDiff.changedTables)) {
|
|
356
|
+
this.append(ret, this.helper.getPostAlterTable(changedTable, options.safe), true);
|
|
357
|
+
}
|
|
358
|
+
if (!options.safe && this.platform.supportsNativeEnums()) {
|
|
359
|
+
for (const removedNativeEnum of schemaDiff.removedNativeEnums) {
|
|
360
|
+
this.append(ret, this.helper.getDropNativeEnumSQL(removedNativeEnum.name, removedNativeEnum.schema));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (options.dropTables && !options.safe) {
|
|
364
|
+
for (const removedNamespace of schemaDiff.removedNamespaces) {
|
|
365
|
+
const sql = this.helper.getDropNamespaceSQL(removedNamespace);
|
|
366
|
+
this.append(ret, sql);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// Create new views after all table changes are done
|
|
370
|
+
// Sort views by dependencies (views depending on other views come later)
|
|
371
|
+
const sortedNewViews = this.sortViewsByDependencies(Object.values(schemaDiff.newViews));
|
|
372
|
+
for (const view of sortedNewViews) {
|
|
373
|
+
if (view.materialized) {
|
|
374
|
+
this.append(
|
|
375
|
+
ret,
|
|
376
|
+
this.helper.createMaterializedView(view.name, view.schema, view.definition, view.withData ?? true),
|
|
377
|
+
);
|
|
378
|
+
} else {
|
|
379
|
+
this.append(ret, this.helper.createView(view.name, view.schema, view.definition), true);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
// Recreate changed views (also sorted by dependencies)
|
|
383
|
+
const changedViews = Object.values(schemaDiff.changedViews).map(v => v.to);
|
|
384
|
+
const sortedChangedViews = this.sortViewsByDependencies(changedViews);
|
|
385
|
+
for (const view of sortedChangedViews) {
|
|
386
|
+
if (view.materialized) {
|
|
387
|
+
this.append(
|
|
388
|
+
ret,
|
|
389
|
+
this.helper.createMaterializedView(view.name, view.schema, view.definition, view.withData ?? true),
|
|
390
|
+
);
|
|
391
|
+
} else {
|
|
392
|
+
this.append(ret, this.helper.createView(view.name, view.schema, view.definition), true);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return this.wrapSchema(ret, options);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* We need to drop foreign keys first for all tables to allow dropping PK constraints.
|
|
399
|
+
*/
|
|
400
|
+
preAlterTable(diff, safe) {
|
|
401
|
+
const ret = [];
|
|
402
|
+
this.append(ret, this.helper.getPreAlterTable(diff, safe));
|
|
403
|
+
for (const foreignKey of Object.values(diff.removedForeignKeys)) {
|
|
404
|
+
ret.push(this.helper.dropForeignKey(diff.toTable.getShortestName(), foreignKey.constraintName));
|
|
405
|
+
}
|
|
406
|
+
for (const foreignKey of Object.values(diff.changedForeignKeys)) {
|
|
407
|
+
ret.push(this.helper.dropForeignKey(diff.toTable.getShortestName(), foreignKey.constraintName));
|
|
408
|
+
}
|
|
409
|
+
return ret;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* creates new database and connects to it
|
|
413
|
+
*/
|
|
414
|
+
async createDatabase(name, options) {
|
|
415
|
+
name ??= this.config.get('dbName');
|
|
416
|
+
const sql = this.helper.getCreateDatabaseSQL(name);
|
|
417
|
+
if (sql) {
|
|
418
|
+
await this.execute(sql);
|
|
419
|
+
}
|
|
420
|
+
this.config.set('dbName', name);
|
|
421
|
+
await this.driver.reconnect(options);
|
|
422
|
+
}
|
|
423
|
+
async dropDatabase(name) {
|
|
424
|
+
name ??= this.config.get('dbName');
|
|
425
|
+
this.config.set('dbName', this.helper.getManagementDbName());
|
|
426
|
+
await this.driver.reconnect();
|
|
427
|
+
await this.execute(this.helper.getDropDatabaseSQL(name));
|
|
428
|
+
this.config.set('dbName', name);
|
|
429
|
+
}
|
|
430
|
+
async execute(sql, options = {}) {
|
|
431
|
+
options.wrap ??= false;
|
|
432
|
+
const lines = this.wrapSchema(sql, options).split('\n');
|
|
433
|
+
const groups = [];
|
|
434
|
+
let i = 0;
|
|
435
|
+
for (const line of lines) {
|
|
436
|
+
if (line.trim() === '') {
|
|
437
|
+
if (groups[i]?.length > 0) {
|
|
438
|
+
i++;
|
|
439
|
+
}
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
groups[i] ??= [];
|
|
443
|
+
groups[i].push(line.trim());
|
|
444
|
+
}
|
|
445
|
+
if (groups.length === 0) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (this.platform.supportsMultipleStatements()) {
|
|
449
|
+
for (const group of groups) {
|
|
450
|
+
const query = group.join('\n');
|
|
451
|
+
await this.driver.execute(query);
|
|
452
|
+
}
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
const statements = groups.flatMap(group => {
|
|
456
|
+
return group
|
|
457
|
+
.join('\n')
|
|
458
|
+
.split(';\n')
|
|
459
|
+
.map(s => s.trim())
|
|
460
|
+
.filter(s => s);
|
|
461
|
+
});
|
|
462
|
+
await Utils.runSerial(statements, stmt => this.driver.execute(stmt));
|
|
463
|
+
}
|
|
464
|
+
async dropTableIfExists(name, schema) {
|
|
465
|
+
const sql = this.helper.dropTableIfExists(name, schema);
|
|
466
|
+
return this.execute(sql);
|
|
467
|
+
}
|
|
468
|
+
wrapSchema(sql, options) {
|
|
469
|
+
const array = Utils.asArray(sql);
|
|
470
|
+
if (array.length === 0) {
|
|
471
|
+
return '';
|
|
472
|
+
}
|
|
473
|
+
if (array[array.length - 1] === '') {
|
|
474
|
+
array.pop();
|
|
475
|
+
}
|
|
476
|
+
if (options.wrap === false) {
|
|
477
|
+
return array.join('\n') + '\n';
|
|
478
|
+
}
|
|
479
|
+
let ret = this.helper.getSchemaBeginning(this.config.get('charset'), this.options.disableForeignKeys);
|
|
480
|
+
ret += array.join('\n') + '\n';
|
|
481
|
+
ret += this.helper.getSchemaEnd(this.options.disableForeignKeys);
|
|
482
|
+
return ret;
|
|
483
|
+
}
|
|
484
|
+
append(array, sql, pad) {
|
|
485
|
+
return this.helper.append(array, sql, pad);
|
|
486
|
+
}
|
|
487
|
+
matchName(name, nameToMatch) {
|
|
488
|
+
return typeof nameToMatch === 'string'
|
|
489
|
+
? name.toLocaleLowerCase() === nameToMatch.toLocaleLowerCase()
|
|
490
|
+
: nameToMatch.test(name);
|
|
491
|
+
}
|
|
492
|
+
isTableSkipped(tableName, schemaName) {
|
|
493
|
+
const skipTables = this.options.skipTables;
|
|
494
|
+
if (!skipTables || skipTables.length === 0) {
|
|
495
|
+
return false;
|
|
496
|
+
}
|
|
497
|
+
const fullTableName = schemaName ? `${schemaName}.${tableName}` : tableName;
|
|
498
|
+
return skipTables.some(pattern => this.matchName(tableName, pattern) || this.matchName(fullTableName, pattern));
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Sorts views by their dependencies so that views depending on other views are created after their dependencies.
|
|
502
|
+
* Uses topological sort based on view definition string matching.
|
|
503
|
+
*/
|
|
504
|
+
sortViewsByDependencies(views) {
|
|
505
|
+
if (views.length <= 1) {
|
|
506
|
+
return views;
|
|
507
|
+
}
|
|
508
|
+
// Use CommitOrderCalculator for topological sort
|
|
509
|
+
const calc = new CommitOrderCalculator();
|
|
510
|
+
// Map views to numeric indices for the calculator
|
|
511
|
+
const viewToIndex = new Map();
|
|
512
|
+
const indexToView = new Map();
|
|
513
|
+
for (let i = 0; i < views.length; i++) {
|
|
514
|
+
viewToIndex.set(views[i], i);
|
|
515
|
+
indexToView.set(i, views[i]);
|
|
516
|
+
calc.addNode(i);
|
|
513
517
|
}
|
|
514
|
-
|
|
515
|
-
|
|
518
|
+
// Check each view's definition for references to other view names
|
|
519
|
+
for (const view of views) {
|
|
520
|
+
const definition = view.definition.toLowerCase();
|
|
521
|
+
const viewIndex = viewToIndex.get(view);
|
|
522
|
+
for (const otherView of views) {
|
|
523
|
+
if (otherView === view) {
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
// Check if the definition references the other view's name
|
|
527
|
+
// Use word boundary matching to avoid false positives
|
|
528
|
+
const patterns = [new RegExp(`\\b${this.escapeRegExp(otherView.name.toLowerCase())}\\b`)];
|
|
529
|
+
if (otherView.schema) {
|
|
530
|
+
patterns.push(
|
|
531
|
+
new RegExp(`\\b${this.escapeRegExp(`${otherView.schema}.${otherView.name}`.toLowerCase())}\\b`),
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
for (const pattern of patterns) {
|
|
535
|
+
if (pattern.test(definition)) {
|
|
536
|
+
// view depends on otherView, so otherView must come first
|
|
537
|
+
// addDependency(from, to) puts `from` before `to` in result
|
|
538
|
+
const otherIndex = viewToIndex.get(otherView);
|
|
539
|
+
calc.addDependency(otherIndex, viewIndex, 1);
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
516
544
|
}
|
|
545
|
+
// Sort and map back to views
|
|
546
|
+
return calc.sort().map(index => indexToView.get(index));
|
|
547
|
+
}
|
|
548
|
+
escapeRegExp(string) {
|
|
549
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
550
|
+
}
|
|
517
551
|
}
|
|
518
552
|
// for back compatibility
|
|
519
553
|
export { SqlSchemaGenerator as SchemaGenerator };
|