@steroidsjs/nest 3.0.0 → 3.0.2
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/infrastructure/adapters/QueryAdapterTypeORM.js +26 -2
- package/infrastructure/adapters/QueryAdapterTypeORM.js.map +1 -1
- package/infrastructure/applications/base/config.js +1 -1
- package/infrastructure/applications/base/config.js.map +1 -1
- package/infrastructure/applications/console/ConsoleApplication.js +2 -2
- package/infrastructure/applications/console/ConsoleApplication.js.map +1 -1
- package/infrastructure/applications/console/config.js +3 -0
- package/infrastructure/applications/console/config.js.map +1 -1
- package/infrastructure/applications/rest/RestApplication.js +7 -0
- package/infrastructure/applications/rest/RestApplication.js.map +1 -1
- package/infrastructure/applications/rest/exceptions/RequestExecutionException.d.ts +6 -0
- package/infrastructure/applications/rest/exceptions/RequestExecutionException.js +6 -0
- package/infrastructure/applications/rest/exceptions/RequestExecutionException.js.map +1 -1
- package/infrastructure/commands/MigrateCommand.js +1 -0
- package/infrastructure/commands/MigrateCommand.js.map +1 -1
- package/infrastructure/commands/dbml/dbml2code.js +8 -0
- package/infrastructure/commands/dbml/dbml2code.js.map +1 -1
- package/infrastructure/commands/entity-generator/EntityCodeGenerateCommand.d.ts +4 -0
- package/infrastructure/commands/entity-generator/EntityCodeGenerateCommand.js +4 -0
- package/infrastructure/commands/entity-generator/EntityCodeGenerateCommand.js.map +1 -1
- package/infrastructure/commands/entity-generator/EntityCodeGenerator.js +1 -0
- package/infrastructure/commands/entity-generator/EntityCodeGenerator.js.map +1 -1
- package/infrastructure/commands/generate/CustomPostgresQueryRunner.d.ts +77 -0
- package/infrastructure/commands/generate/CustomPostgresQueryRunner.js +149 -0
- package/infrastructure/commands/generate/CustomPostgresQueryRunner.js.map +1 -1
- package/infrastructure/commands/generate/CustomRdbmsSchemaBuilder.js +7 -1
- package/infrastructure/commands/generate/CustomRdbmsSchemaBuilder.js.map +1 -1
- package/infrastructure/commands/generate/index.js +14 -0
- package/infrastructure/commands/generate/index.js.map +1 -1
- package/infrastructure/commands/importClassesFromDirectories.d.ts +3 -0
- package/infrastructure/commands/importClassesFromDirectories.js +3 -0
- package/infrastructure/commands/importClassesFromDirectories.js.map +1 -1
- package/infrastructure/decorators/Module.js +6 -0
- package/infrastructure/decorators/Module.js.map +1 -1
- package/infrastructure/decorators/TableFromModel.js +3 -0
- package/infrastructure/decorators/TableFromModel.js.map +1 -1
- package/infrastructure/decorators/fields/BaseField.js +5 -0
- package/infrastructure/decorators/fields/BaseField.js.map +1 -1
- package/infrastructure/decorators/fields/CreateTimeField.js +1 -0
- package/infrastructure/decorators/fields/CreateTimeField.js.map +1 -1
- package/infrastructure/decorators/fields/DateTimeField.js +5 -0
- package/infrastructure/decorators/fields/DateTimeField.js.map +1 -1
- package/infrastructure/decorators/fields/ExtendField.js +2 -0
- package/infrastructure/decorators/fields/ExtendField.js.map +1 -1
- package/infrastructure/decorators/fields/RelationField.js +3 -0
- package/infrastructure/decorators/fields/RelationField.js.map +1 -1
- package/infrastructure/decorators/fields/RelationIdField.js +2 -0
- package/infrastructure/decorators/fields/RelationIdField.js.map +1 -1
- package/infrastructure/decorators/fields/UidField.js +1 -0
- package/infrastructure/decorators/fields/UidField.js.map +1 -1
- package/infrastructure/decorators/fields/UpdateTimeField.js +1 -0
- package/infrastructure/decorators/fields/UpdateTimeField.js.map +1 -1
- package/infrastructure/filters/ValidationExceptionFilter.js +1 -0
- package/infrastructure/filters/ValidationExceptionFilter.js.map +1 -1
- package/infrastructure/helpers/ModuleHelper.d.ts +1 -0
- package/infrastructure/helpers/ModuleHelper.js +1 -0
- package/infrastructure/helpers/ModuleHelper.js.map +1 -1
- package/infrastructure/helpers/typeORM/ConditionHelperTypeORM.js +21 -18
- package/infrastructure/helpers/typeORM/ConditionHelperTypeORM.js.map +1 -1
- package/infrastructure/helpers/typeORM/SearchHelperTypeORM.js +6 -0
- package/infrastructure/helpers/typeORM/SearchHelperTypeORM.js.map +1 -1
- package/infrastructure/pipes/CreateDtoPipe.js +1 -0
- package/infrastructure/pipes/CreateDtoPipe.js.map +1 -1
- package/infrastructure/repositories/CrudRepository.d.ts +88 -0
- package/infrastructure/repositories/CrudRepository.js +85 -0
- package/infrastructure/repositories/CrudRepository.js.map +1 -1
- package/infrastructure/tests/ModelTest.test.js +23 -0
- package/infrastructure/tests/ModelTest.test.js.map +1 -1
- package/infrastructure/tests/app/services/ArticleService.d.ts +4 -1
- package/infrastructure/tests/app/services/ArticleService.js +3 -1
- package/infrastructure/tests/app/services/ArticleService.js.map +1 -1
- package/infrastructure/tests/app/services/CommentService.d.ts +4 -1
- package/infrastructure/tests/app/services/CommentService.js +3 -1
- package/infrastructure/tests/app/services/CommentService.js.map +1 -1
- package/infrastructure/tests/app/services/FileService.d.ts +4 -1
- package/infrastructure/tests/app/services/FileService.js +3 -1
- package/infrastructure/tests/app/services/FileService.js.map +1 -1
- package/infrastructure/tests/app/services/ImageService.d.ts +4 -1
- package/infrastructure/tests/app/services/ImageService.js +3 -1
- package/infrastructure/tests/app/services/ImageService.js.map +1 -1
- package/infrastructure/tests/app/services/TagService.d.ts +4 -1
- package/infrastructure/tests/app/services/TagService.js +3 -1
- package/infrastructure/tests/app/services/TagService.js.map +1 -1
- package/infrastructure/tests/app/services/UserService.d.ts +4 -1
- package/infrastructure/tests/app/services/UserService.js +3 -1
- package/infrastructure/tests/app/services/UserService.js.map +1 -1
- package/infrastructure/tests/bootstrap.js +1 -1
- package/infrastructure/tests/bootstrap.js.map +1 -1
- package/package.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/usecases/base/SearchQuery.d.ts +12 -0
- package/usecases/base/SearchQuery.js +18 -0
- package/usecases/base/SearchQuery.js.map +1 -1
- package/usecases/helpers/DataMapper.js +6 -0
- package/usecases/helpers/DataMapper.js.map +1 -1
- package/usecases/helpers/ValidationHelper.d.ts +27 -0
- package/usecases/helpers/ValidationHelper.js +33 -0
- package/usecases/helpers/ValidationHelper.js.map +1 -1
- package/usecases/interfaces/IManualSchema.d.ts +4 -0
- package/usecases/services/CrudService.d.ts +47 -8
- package/usecases/services/CrudService.js +88 -0
- package/usecases/services/CrudService.js.map +1 -1
- package/usecases/services/ReadService.d.ts +31 -0
- package/usecases/services/ReadService.js +32 -0
- package/usecases/services/ReadService.js.map +1 -1
- package/usecases/validators/Validator.js +1 -0
- package/usecases/validators/Validator.js.map +1 -1
|
@@ -29,11 +29,20 @@ class TableSqlInMemory extends SqlInMemory_1.SqlInMemory {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
exports.TableSqlInMemory = TableSqlInMemory;
|
|
32
|
+
/**
|
|
33
|
+
* Runs queries on a single postgres database connection.
|
|
34
|
+
*/
|
|
32
35
|
class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunner {
|
|
33
36
|
constructor() {
|
|
34
37
|
super(...arguments);
|
|
38
|
+
/**
|
|
39
|
+
* Sql-s stored if "sql in memory" mode is enabled.
|
|
40
|
+
*/
|
|
35
41
|
this.sqlInMemory = new TableSqlInMemory();
|
|
36
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new table.
|
|
45
|
+
*/
|
|
37
46
|
async createTable(table, ifNotExist = false, createForeignKeys = true, createIndices = true) {
|
|
38
47
|
if (ifNotExist) {
|
|
39
48
|
const isTableExist = await this.hasTable(table);
|
|
@@ -42,17 +51,21 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
42
51
|
}
|
|
43
52
|
const upQueries = [];
|
|
44
53
|
const downQueries = [];
|
|
54
|
+
// if table have column with ENUM type, we must create this type in postgres.
|
|
45
55
|
const enumColumns = table.columns.filter(column => column.type === "enum" || column.type === "simple-enum");
|
|
46
56
|
const createdEnumTypes = [];
|
|
47
57
|
for (const column of enumColumns) {
|
|
58
|
+
// TODO: Should also check if values of existing type matches expected ones
|
|
48
59
|
const hasEnum = await this.hasEnumType(table, column);
|
|
49
60
|
const enumName = this.buildEnumName(table, column);
|
|
61
|
+
// if enum with the same "enumName" is defined more then once, me must prevent double creation
|
|
50
62
|
if (!hasEnum && createdEnumTypes.indexOf(enumName) === -1) {
|
|
51
63
|
createdEnumTypes.push(enumName);
|
|
52
64
|
upQueries.push(new TableQuery(table.name, this.createEnumTypeSql(table, column, enumName)));
|
|
53
65
|
downQueries.push(new TableQuery(table.name, this.dropEnumTypeSql(table, column, enumName)));
|
|
54
66
|
}
|
|
55
67
|
}
|
|
68
|
+
// if table have column with generated type, we must add the expression to the metadata table
|
|
56
69
|
const generatedColumns = table.columns.filter(column => column.generatedType === "STORED" && column.asExpression);
|
|
57
70
|
for (const column of generatedColumns) {
|
|
58
71
|
const tableNameWithSchema = (await this.getTableNameWithSchema(table.name)).split('.');
|
|
@@ -79,10 +92,13 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
79
92
|
}
|
|
80
93
|
upQueries.push(new TableQuery(table.name, this.createTableSql(table, createForeignKeys)));
|
|
81
94
|
downQueries.push(new TableQuery(table.name, this.dropTableSql(table)));
|
|
95
|
+
// if createForeignKeys is true, we must drop created foreign keys in down query.
|
|
96
|
+
// createTable does not need separate method to create foreign keys, because it create fk's in the same query with table creation.
|
|
82
97
|
if (createForeignKeys)
|
|
83
98
|
table.foreignKeys.forEach(foreignKey => downQueries.push(new TableQuery(table.name, this.dropForeignKeySql(table, foreignKey))));
|
|
84
99
|
if (createIndices) {
|
|
85
100
|
table.indices.forEach(index => {
|
|
101
|
+
// new index may be passed without name. In this case we generate index name manually.
|
|
86
102
|
if (!index.name)
|
|
87
103
|
index.name = this.connection.namingStrategy.indexName(table, index.columnNames, index.where);
|
|
88
104
|
upQueries.push(new TableQuery(table.name, this.createIndexSql(table, index)));
|
|
@@ -91,12 +107,17 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
91
107
|
}
|
|
92
108
|
await this.executeQueries(upQueries, downQueries);
|
|
93
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Drops the table.
|
|
112
|
+
*/
|
|
94
113
|
async dropTable(target, ifExist, dropForeignKeys = true, dropIndices = true) {
|
|
114
|
+
// to perform drop queries for foreign keys and indices.
|
|
95
115
|
if (ifExist) {
|
|
96
116
|
const isTableExist = await this.hasTable(target);
|
|
97
117
|
if (!isTableExist)
|
|
98
118
|
return Promise.resolve();
|
|
99
119
|
}
|
|
120
|
+
// if dropTable called with dropForeignKeys = true, we must create foreign keys in down query.
|
|
100
121
|
const createForeignKeys = dropForeignKeys;
|
|
101
122
|
const tablePath = this.getTablePath(target);
|
|
102
123
|
const table = await this.getCachedTable(tablePath);
|
|
@@ -114,6 +135,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
114
135
|
downQueries.push(new TableQuery(table.name, this.createTableSql(table, createForeignKeys)));
|
|
115
136
|
await this.executeQueries(upQueries, downQueries);
|
|
116
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Creates a new column from the column in the table.
|
|
140
|
+
*/
|
|
117
141
|
async addColumn(tableOrName, column) {
|
|
118
142
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
119
143
|
const clonedTable = table.clone();
|
|
@@ -130,8 +154,10 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
130
154
|
ADD ${this.buildCreateColumnSql(table, column)}`)));
|
|
131
155
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
132
156
|
DROP COLUMN "${column.name}"`)));
|
|
157
|
+
// create or update primary key constraint
|
|
133
158
|
if (column.isPrimary) {
|
|
134
159
|
const primaryColumns = clonedTable.primaryColumns;
|
|
160
|
+
// if table already have primary key, me must drop it and recreate again
|
|
135
161
|
if (primaryColumns.length > 0) {
|
|
136
162
|
const pkName = this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map(column => column.name));
|
|
137
163
|
const columnNames = primaryColumns.map(column => `"${column.name}"`).join(", ");
|
|
@@ -148,11 +174,13 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
148
174
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
149
175
|
DROP CONSTRAINT "${pkName}"`)));
|
|
150
176
|
}
|
|
177
|
+
// create column index
|
|
151
178
|
const columnIndex = clonedTable.indices.find(index => index.columnNames.length === 1 && index.columnNames[0] === column.name);
|
|
152
179
|
if (columnIndex) {
|
|
153
180
|
upQueries.push(new TableQuery(table.name, this.createIndexSql(table, columnIndex)));
|
|
154
181
|
downQueries.push(new TableQuery(table.name, this.dropIndexSql(table, columnIndex)));
|
|
155
182
|
}
|
|
183
|
+
// create unique constraint
|
|
156
184
|
if (column.isUnique) {
|
|
157
185
|
const uniqueConstraint = new TableUnique_1.TableUnique({
|
|
158
186
|
name: this.connection.namingStrategy.uniqueConstraintName(table, [column.name]),
|
|
@@ -187,6 +215,7 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
187
215
|
upQueries.push(new TableQuery(tableName, insertQuery));
|
|
188
216
|
downQueries.push(new TableQuery(tableName, deleteQuery));
|
|
189
217
|
}
|
|
218
|
+
// create column's comment
|
|
190
219
|
if (column.comment) {
|
|
191
220
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`COMMENT ON COLUMN ${this.escapePath(table)}."${column.name}" IS ${this.escapeComment(column.comment)}`)));
|
|
192
221
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`COMMENT ON COLUMN ${this.escapePath(table)}."${column.name}" IS ${this.escapeComment(column.comment)}`)));
|
|
@@ -195,6 +224,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
195
224
|
clonedTable.addColumn(column);
|
|
196
225
|
this.replaceCachedTable(table, clonedTable);
|
|
197
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* Renames column in the given table.
|
|
229
|
+
*/
|
|
198
230
|
async renameColumn(tableOrName, oldTableColumnOrName, newTableColumnOrName) {
|
|
199
231
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
200
232
|
const oldColumn = oldTableColumnOrName instanceof TableColumn_1.TableColumn ? oldTableColumnOrName : table.columns.find(c => c.name === oldTableColumnOrName);
|
|
@@ -210,6 +242,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
210
242
|
}
|
|
211
243
|
return this.changeColumn(table, oldColumn, newColumn);
|
|
212
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* Changes a column in the table.
|
|
247
|
+
*/
|
|
213
248
|
async changeColumn(tableOrName, oldTableColumnOrName, newColumn) {
|
|
214
249
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
215
250
|
let clonedTable = table.clone();
|
|
@@ -226,33 +261,42 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
226
261
|
|| newColumn.isArray !== oldColumn.isArray
|
|
227
262
|
|| (!oldColumn.generatedType && newColumn.generatedType === "STORED")
|
|
228
263
|
|| (oldColumn.asExpression !== newColumn.asExpression && newColumn.generatedType === "STORED")) {
|
|
264
|
+
// To avoid data conversion, we just recreate column
|
|
229
265
|
await this.dropColumn(table, oldColumn);
|
|
230
266
|
await this.addColumn(table, newColumn);
|
|
267
|
+
// update cloned table
|
|
231
268
|
clonedTable = table.clone();
|
|
232
269
|
}
|
|
233
270
|
else {
|
|
234
271
|
if (oldColumn.name !== newColumn.name) {
|
|
272
|
+
// rename column
|
|
235
273
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
236
274
|
RENAME COLUMN "${oldColumn.name}" TO "${newColumn.name}"`)));
|
|
237
275
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
238
276
|
RENAME COLUMN "${newColumn.name}" TO "${oldColumn.name}"`)));
|
|
277
|
+
// rename ENUM type
|
|
239
278
|
if (oldColumn.type === "enum" || oldColumn.type === "simple-enum") {
|
|
240
279
|
const oldEnumType = await this.getUserDefinedTypeName(table, oldColumn);
|
|
241
280
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TYPE "${oldEnumType.schema}"."${oldEnumType.name}" RENAME TO ${this.buildEnumName(table, newColumn, false)}`)));
|
|
242
281
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TYPE ${this.buildEnumName(table, newColumn)} RENAME TO "${oldEnumType.name}"`)));
|
|
243
282
|
}
|
|
283
|
+
// rename column primary key constraint
|
|
244
284
|
if (oldColumn.isPrimary === true) {
|
|
245
285
|
const primaryColumns = clonedTable.primaryColumns;
|
|
286
|
+
// build old primary constraint name
|
|
246
287
|
const columnNames = primaryColumns.map(column => column.name);
|
|
247
288
|
const oldPkName = this.connection.namingStrategy.primaryKeyName(clonedTable, columnNames);
|
|
289
|
+
// replace old column name with new column name
|
|
248
290
|
columnNames.splice(columnNames.indexOf(oldColumn.name), 1);
|
|
249
291
|
columnNames.push(newColumn.name);
|
|
292
|
+
// build new primary constraint name
|
|
250
293
|
const newPkName = this.connection.namingStrategy.primaryKeyName(clonedTable, columnNames);
|
|
251
294
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
252
295
|
RENAME CONSTRAINT "${oldPkName}" TO "${newPkName}"`)));
|
|
253
296
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
254
297
|
RENAME CONSTRAINT "${newPkName}" TO "${oldPkName}"`)));
|
|
255
298
|
}
|
|
299
|
+
// rename column sequence
|
|
256
300
|
if (oldColumn.isGenerated === true && newColumn.generationStrategy === "increment") {
|
|
257
301
|
const sequencePath = this.buildSequencePath(table, oldColumn.name);
|
|
258
302
|
const sequenceName = this.buildSequenceName(table, oldColumn.name);
|
|
@@ -263,37 +307,50 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
263
307
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(up)));
|
|
264
308
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(down)));
|
|
265
309
|
}
|
|
310
|
+
// rename unique constraints
|
|
266
311
|
clonedTable.findColumnUniques(oldColumn).forEach(unique => {
|
|
312
|
+
// build new constraint name
|
|
267
313
|
unique.columnNames.splice(unique.columnNames.indexOf(oldColumn.name), 1);
|
|
268
314
|
unique.columnNames.push(newColumn.name);
|
|
269
315
|
const newUniqueName = this.connection.namingStrategy.uniqueConstraintName(clonedTable, unique.columnNames);
|
|
316
|
+
// build queries
|
|
270
317
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
271
318
|
RENAME CONSTRAINT "${unique.name}" TO "${newUniqueName}"`)));
|
|
272
319
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
273
320
|
RENAME CONSTRAINT "${newUniqueName}" TO "${unique.name}"`)));
|
|
321
|
+
// replace constraint name
|
|
274
322
|
unique.name = newUniqueName;
|
|
275
323
|
});
|
|
324
|
+
// rename index constraints
|
|
276
325
|
clonedTable.findColumnIndices(oldColumn).forEach(index => {
|
|
326
|
+
// build new constraint name
|
|
277
327
|
index.columnNames.splice(index.columnNames.indexOf(oldColumn.name), 1);
|
|
278
328
|
index.columnNames.push(newColumn.name);
|
|
279
329
|
const { schema } = this.driver.parseTableName(table);
|
|
280
330
|
const newIndexName = this.connection.namingStrategy.indexName(clonedTable, index.columnNames, index.where);
|
|
331
|
+
// build queries
|
|
281
332
|
const up = schema ? `ALTER INDEX "${schema}"."${index.name}" RENAME TO "${newIndexName}"` : `ALTER INDEX "${index.name}" RENAME TO "${newIndexName}"`;
|
|
282
333
|
const down = schema ? `ALTER INDEX "${schema}"."${newIndexName}" RENAME TO "${index.name}"` : `ALTER INDEX "${newIndexName}" RENAME TO "${index.name}"`;
|
|
283
334
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(up)));
|
|
284
335
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(down)));
|
|
336
|
+
// replace constraint name
|
|
285
337
|
index.name = newIndexName;
|
|
286
338
|
});
|
|
339
|
+
// rename foreign key constraints
|
|
287
340
|
clonedTable.findColumnForeignKeys(oldColumn).forEach(foreignKey => {
|
|
341
|
+
// build new constraint name
|
|
288
342
|
foreignKey.columnNames.splice(foreignKey.columnNames.indexOf(oldColumn.name), 1);
|
|
289
343
|
foreignKey.columnNames.push(newColumn.name);
|
|
290
344
|
const newForeignKeyName = this.connection.namingStrategy.foreignKeyName(clonedTable, foreignKey.columnNames, this.getTablePath(foreignKey), foreignKey.referencedColumnNames);
|
|
345
|
+
// build queries
|
|
291
346
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
292
347
|
RENAME CONSTRAINT "${foreignKey.name}" TO "${newForeignKeyName}"`)));
|
|
293
348
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
294
349
|
RENAME CONSTRAINT "${newForeignKeyName}" TO "${foreignKey.name}"`)));
|
|
350
|
+
// replace constraint name
|
|
295
351
|
foreignKey.name = newForeignKeyName;
|
|
296
352
|
});
|
|
353
|
+
// rename old column in the Table object
|
|
297
354
|
const oldTableColumn = clonedTable.columns.find(column => column.name === oldColumn.name);
|
|
298
355
|
clonedTable.columns[clonedTable.columns.indexOf(oldTableColumn)].name = newColumn.name;
|
|
299
356
|
oldColumn.name = newColumn.name;
|
|
@@ -308,34 +365,47 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
308
365
|
&& (oldColumn.type === "enum" || oldColumn.type === "simple-enum")
|
|
309
366
|
&& (!OrmUtils_1.OrmUtils.isArraysEqual(newColumn.enum, oldColumn.enum) || newColumn.enumName !== oldColumn.enumName)) {
|
|
310
367
|
const arraySuffix = newColumn.isArray ? "[]" : "";
|
|
368
|
+
// "public"."new_enum"
|
|
311
369
|
const newEnumName = this.buildEnumName(table, newColumn);
|
|
370
|
+
// "public"."old_enum"
|
|
312
371
|
const oldEnumName = this.buildEnumName(table, oldColumn);
|
|
372
|
+
// "old_enum"
|
|
313
373
|
const oldEnumNameWithoutSchema = this.buildEnumName(table, oldColumn, false);
|
|
374
|
+
//"public"."old_enum_old"
|
|
314
375
|
const oldEnumNameWithSchema_old = this.buildEnumName(table, oldColumn, true, false, true);
|
|
376
|
+
//"old_enum_old"
|
|
315
377
|
const oldEnumNameWithoutSchema_old = this.buildEnumName(table, oldColumn, false, false, true);
|
|
378
|
+
// rename old ENUM
|
|
316
379
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TYPE ${oldEnumName} RENAME TO ${oldEnumNameWithoutSchema_old}`)));
|
|
317
380
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TYPE ${oldEnumNameWithSchema_old} RENAME TO ${oldEnumNameWithoutSchema}`)));
|
|
381
|
+
// create new ENUM
|
|
318
382
|
upQueries.push(new TableQuery(table.name, this.createEnumTypeSql(table, newColumn, newEnumName)));
|
|
319
383
|
downQueries.push(new TableQuery(table.name, this.dropEnumTypeSql(table, newColumn, newEnumName)));
|
|
384
|
+
// if column have default value, we must drop it to avoid issues with type casting
|
|
320
385
|
if (oldColumn.default !== null && oldColumn.default !== undefined) {
|
|
386
|
+
// mark default as changed to prevent double update
|
|
321
387
|
defaultValueChanged = true;
|
|
322
388
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
323
389
|
ALTER COLUMN "${oldColumn.name}" DROP DEFAULT`)));
|
|
324
390
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
325
391
|
ALTER COLUMN "${oldColumn.name}" SET DEFAULT ${oldColumn.default}`)));
|
|
326
392
|
}
|
|
393
|
+
// build column types
|
|
327
394
|
const upType = `${newEnumName}${arraySuffix} USING "${newColumn.name}"::"text"::${newEnumName}${arraySuffix}`;
|
|
328
395
|
const downType = `${oldEnumNameWithSchema_old}${arraySuffix} USING "${newColumn.name}"::"text"::${oldEnumNameWithSchema_old}${arraySuffix}`;
|
|
396
|
+
// update column to use new type
|
|
329
397
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
330
398
|
ALTER COLUMN "${newColumn.name}" TYPE ${upType}`)));
|
|
331
399
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
332
400
|
ALTER COLUMN "${newColumn.name}" TYPE ${downType}`)));
|
|
401
|
+
// restore column default or create new one
|
|
333
402
|
if (newColumn.default !== null && newColumn.default !== undefined) {
|
|
334
403
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
335
404
|
ALTER COLUMN "${newColumn.name}" SET DEFAULT ${newColumn.default}`)));
|
|
336
405
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
337
406
|
ALTER COLUMN "${newColumn.name}" DROP DEFAULT`)));
|
|
338
407
|
}
|
|
408
|
+
// remove old ENUM
|
|
339
409
|
upQueries.push(new TableQuery(table.name, this.dropEnumTypeSql(table, oldColumn, oldEnumNameWithSchema_old)));
|
|
340
410
|
downQueries.push(new TableQuery(table.name, this.createEnumTypeSql(table, oldColumn, oldEnumNameWithSchema_old)));
|
|
341
411
|
}
|
|
@@ -359,6 +429,7 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
359
429
|
}
|
|
360
430
|
if (newColumn.isPrimary !== oldColumn.isPrimary) {
|
|
361
431
|
const primaryColumns = clonedTable.primaryColumns;
|
|
432
|
+
// if primary column state changed, we must always drop existed constraint.
|
|
362
433
|
if (primaryColumns.length > 0) {
|
|
363
434
|
const pkName = this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map(column => column.name));
|
|
364
435
|
const columnNames = primaryColumns.map(column => `"${column.name}"`).join(", ");
|
|
@@ -369,6 +440,7 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
369
440
|
}
|
|
370
441
|
if (newColumn.isPrimary === true) {
|
|
371
442
|
primaryColumns.push(newColumn);
|
|
443
|
+
// update column in table
|
|
372
444
|
const column = clonedTable.columns.find(column => column.name === newColumn.name);
|
|
373
445
|
column.isPrimary = true;
|
|
374
446
|
const pkName = this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map(column => column.name));
|
|
@@ -381,8 +453,10 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
381
453
|
else {
|
|
382
454
|
const primaryColumn = primaryColumns.find(c => c.name === newColumn.name);
|
|
383
455
|
primaryColumns.splice(primaryColumns.indexOf(primaryColumn), 1);
|
|
456
|
+
// update column in table
|
|
384
457
|
const column = clonedTable.columns.find(column => column.name === newColumn.name);
|
|
385
458
|
column.isPrimary = false;
|
|
459
|
+
// if we have another primary keys, we must recreate constraint.
|
|
386
460
|
if (primaryColumns.length > 0) {
|
|
387
461
|
const pkName = this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map(column => column.name));
|
|
388
462
|
const columnNames = primaryColumns.map(column => `"${column.name}"`).join(", ");
|
|
@@ -436,6 +510,7 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
436
510
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`CREATE SEQUENCE IF NOT EXISTS ${this.escapePath(this.buildSequencePath(table, newColumn))} OWNED BY ${this.escapePath(table)}."${newColumn.name}"`)));
|
|
437
511
|
}
|
|
438
512
|
}
|
|
513
|
+
// the default might have changed when the enum changed
|
|
439
514
|
if (newColumn.default !== oldColumn.default && !defaultValueChanged) {
|
|
440
515
|
if (newColumn.default !== null && newColumn.default !== undefined) {
|
|
441
516
|
upQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
@@ -463,7 +538,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
463
538
|
ALTER COLUMN "${newColumn.name}" TYPE ${this.driver.createFullType(oldColumn)}`)));
|
|
464
539
|
}
|
|
465
540
|
if (newColumn.generatedType !== oldColumn.generatedType) {
|
|
541
|
+
// Convert generated column data to normal column
|
|
466
542
|
if (!newColumn.generatedType || newColumn.generatedType === "VIRTUAL") {
|
|
543
|
+
// We can copy the generated data to the new column
|
|
467
544
|
const tableNameWithSchema = (await this.getTableNameWithSchema(table.name)).split('.');
|
|
468
545
|
const tableName = tableNameWithSchema[1];
|
|
469
546
|
const schema = tableNameWithSchema[0];
|
|
@@ -482,6 +559,7 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
482
559
|
type: MetadataTableType_1.MetadataTableType.GENERATED_COLUMN,
|
|
483
560
|
name: oldColumn.name
|
|
484
561
|
})));
|
|
562
|
+
// However, we can't copy it back on downgrade. It needs to regenerate.
|
|
485
563
|
downQueries.push(new TableQuery(tableName, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
486
564
|
DROP COLUMN "${newColumn.name}"`)));
|
|
487
565
|
downQueries.push(new TableQuery(tableName, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
@@ -507,6 +585,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
507
585
|
await this.executeQueries(upQueries, downQueries);
|
|
508
586
|
this.replaceCachedTable(table, clonedTable);
|
|
509
587
|
}
|
|
588
|
+
/**
|
|
589
|
+
* Drops column in the table.
|
|
590
|
+
*/
|
|
510
591
|
async dropColumn(tableOrName, columnOrName) {
|
|
511
592
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
512
593
|
const column = columnOrName instanceof TableColumn_1.TableColumn ? columnOrName : table.findColumnByName(columnOrName);
|
|
@@ -515,6 +596,7 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
515
596
|
const clonedTable = table.clone();
|
|
516
597
|
const upQueries = [];
|
|
517
598
|
const downQueries = [];
|
|
599
|
+
// drop primary key constraint
|
|
518
600
|
if (column.isPrimary) {
|
|
519
601
|
const pkName = this.connection.namingStrategy.primaryKeyName(clonedTable, clonedTable.primaryColumns.map(column => column.name));
|
|
520
602
|
const columnNames = clonedTable.primaryColumns.map(primaryColumn => `"${primaryColumn.name}"`).join(", ");
|
|
@@ -522,8 +604,10 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
522
604
|
DROP CONSTRAINT "${pkName}"`)));
|
|
523
605
|
downQueries.push(new TableQuery(clonedTable.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(clonedTable)}
|
|
524
606
|
ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`)));
|
|
607
|
+
// update column in table
|
|
525
608
|
const tableColumn = clonedTable.findColumnByName(column.name);
|
|
526
609
|
tableColumn.isPrimary = false;
|
|
610
|
+
// if primary key have multiple columns, we must recreate it without dropped column
|
|
527
611
|
if (clonedTable.primaryColumns.length > 0) {
|
|
528
612
|
const pkName = this.connection.namingStrategy.primaryKeyName(clonedTable, clonedTable.primaryColumns.map(column => column.name));
|
|
529
613
|
const columnNames = clonedTable.primaryColumns.map(primaryColumn => `"${primaryColumn.name}"`).join(", ");
|
|
@@ -533,18 +617,21 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
533
617
|
DROP CONSTRAINT "${pkName}"`)));
|
|
534
618
|
}
|
|
535
619
|
}
|
|
620
|
+
// drop column index
|
|
536
621
|
const columnIndex = clonedTable.indices.find(index => index.columnNames.length === 1 && index.columnNames[0] === column.name);
|
|
537
622
|
if (columnIndex) {
|
|
538
623
|
clonedTable.indices.splice(clonedTable.indices.indexOf(columnIndex), 1);
|
|
539
624
|
upQueries.push(new TableQuery(table.name, this.dropIndexSql(table, columnIndex)));
|
|
540
625
|
downQueries.push(new TableQuery(table.name, this.createIndexSql(table, columnIndex)));
|
|
541
626
|
}
|
|
627
|
+
// drop column check
|
|
542
628
|
const columnCheck = clonedTable.checks.find(check => !!check.columnNames && check.columnNames.length === 1 && check.columnNames[0] === column.name);
|
|
543
629
|
if (columnCheck) {
|
|
544
630
|
clonedTable.checks.splice(clonedTable.checks.indexOf(columnCheck), 1);
|
|
545
631
|
upQueries.push(new TableQuery(table.name, this.dropCheckConstraintSql(table, columnCheck)));
|
|
546
632
|
downQueries.push(new TableQuery(table.name, this.createCheckConstraintSql(table, columnCheck)));
|
|
547
633
|
}
|
|
634
|
+
// drop column unique
|
|
548
635
|
const columnUnique = clonedTable.uniques.find(unique => unique.columnNames.length === 1 && unique.columnNames[0] === column.name);
|
|
549
636
|
if (columnUnique) {
|
|
550
637
|
clonedTable.uniques.splice(clonedTable.uniques.indexOf(columnUnique), 1);
|
|
@@ -555,6 +642,7 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
555
642
|
DROP COLUMN "${column.name}"`)));
|
|
556
643
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
557
644
|
ADD ${this.buildCreateColumnSql(table, column)}`)));
|
|
645
|
+
// drop enum type
|
|
558
646
|
if (column.type === "enum" || column.type === "simple-enum") {
|
|
559
647
|
const hasEnum = await this.hasEnumType(table, column);
|
|
560
648
|
if (hasEnum) {
|
|
@@ -590,12 +678,16 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
590
678
|
clonedTable.removeColumn(column);
|
|
591
679
|
this.replaceCachedTable(table, clonedTable);
|
|
592
680
|
}
|
|
681
|
+
/**
|
|
682
|
+
* Updates composite primary keys.
|
|
683
|
+
*/
|
|
593
684
|
async updatePrimaryKeys(tableOrName, columns) {
|
|
594
685
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
595
686
|
const clonedTable = table.clone();
|
|
596
687
|
const columnNames = columns.map(column => column.name);
|
|
597
688
|
const upQueries = [];
|
|
598
689
|
const downQueries = [];
|
|
690
|
+
// if table already have primary columns, we must drop them.
|
|
599
691
|
const primaryColumns = clonedTable.primaryColumns;
|
|
600
692
|
if (primaryColumns.length > 0) {
|
|
601
693
|
const pkName = this.connection.namingStrategy.primaryKeyName(clonedTable, primaryColumns.map(column => column.name));
|
|
@@ -605,6 +697,7 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
605
697
|
downQueries.push(new TableQuery(table.name, new Query_1.Query(`ALTER TABLE ${this.escapePath(table)}
|
|
606
698
|
ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNamesString})`)));
|
|
607
699
|
}
|
|
700
|
+
// update columns in table.
|
|
608
701
|
clonedTable.columns
|
|
609
702
|
.filter(column => columnNames.indexOf(column.name) !== -1)
|
|
610
703
|
.forEach(column => column.isPrimary = true);
|
|
@@ -617,8 +710,12 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
617
710
|
await this.executeQueries(upQueries, downQueries);
|
|
618
711
|
this.replaceCachedTable(table, clonedTable);
|
|
619
712
|
}
|
|
713
|
+
/**
|
|
714
|
+
* Creates new unique constraint.
|
|
715
|
+
*/
|
|
620
716
|
async createUniqueConstraint(tableOrName, uniqueConstraint) {
|
|
621
717
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
718
|
+
// new unique constraint may be passed without name. In this case we generate unique name manually.
|
|
622
719
|
if (!uniqueConstraint.name)
|
|
623
720
|
uniqueConstraint.name = this.connection.namingStrategy.uniqueConstraintName(table, uniqueConstraint.columnNames);
|
|
624
721
|
const up = this.createUniqueConstraintSql(table, uniqueConstraint);
|
|
@@ -626,6 +723,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
626
723
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
627
724
|
table.addUniqueConstraint(uniqueConstraint);
|
|
628
725
|
}
|
|
726
|
+
/**
|
|
727
|
+
* Drops unique constraint.
|
|
728
|
+
*/
|
|
629
729
|
async dropUniqueConstraint(tableOrName, uniqueOrName) {
|
|
630
730
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
631
731
|
const uniqueConstraint = uniqueOrName instanceof TableUnique_1.TableUnique ? uniqueOrName : table.uniques.find(u => u.name === uniqueOrName);
|
|
@@ -636,8 +736,12 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
636
736
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
637
737
|
table.removeUniqueConstraint(uniqueConstraint);
|
|
638
738
|
}
|
|
739
|
+
/**
|
|
740
|
+
* Creates new check constraint.
|
|
741
|
+
*/
|
|
639
742
|
async createCheckConstraint(tableOrName, checkConstraint) {
|
|
640
743
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
744
|
+
// new unique constraint may be passed without name. In this case we generate unique name manually.
|
|
641
745
|
if (!checkConstraint.name)
|
|
642
746
|
checkConstraint.name = this.connection.namingStrategy.checkConstraintName(table, checkConstraint.expression);
|
|
643
747
|
const up = this.createCheckConstraintSql(table, checkConstraint);
|
|
@@ -645,6 +749,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
645
749
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
646
750
|
table.addCheckConstraint(checkConstraint);
|
|
647
751
|
}
|
|
752
|
+
/**
|
|
753
|
+
* Drops check constraint.
|
|
754
|
+
*/
|
|
648
755
|
async dropCheckConstraint(tableOrName, checkOrName) {
|
|
649
756
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
650
757
|
const checkConstraint = checkOrName instanceof TableCheck_1.TableCheck ? checkOrName : table.checks.find(c => c.name === checkOrName);
|
|
@@ -655,8 +762,12 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
655
762
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
656
763
|
table.removeCheckConstraint(checkConstraint);
|
|
657
764
|
}
|
|
765
|
+
/**
|
|
766
|
+
* Creates new exclusion constraint.
|
|
767
|
+
*/
|
|
658
768
|
async createExclusionConstraint(tableOrName, exclusionConstraint) {
|
|
659
769
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
770
|
+
// new unique constraint may be passed without name. In this case we generate unique name manually.
|
|
660
771
|
if (!exclusionConstraint.name)
|
|
661
772
|
exclusionConstraint.name = this.connection.namingStrategy.exclusionConstraintName(table, exclusionConstraint.expression);
|
|
662
773
|
const up = this.createExclusionConstraintSql(table, exclusionConstraint);
|
|
@@ -664,6 +775,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
664
775
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
665
776
|
table.addExclusionConstraint(exclusionConstraint);
|
|
666
777
|
}
|
|
778
|
+
/**
|
|
779
|
+
* Drops exclusion constraint.
|
|
780
|
+
*/
|
|
667
781
|
async dropExclusionConstraint(tableOrName, exclusionOrName) {
|
|
668
782
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
669
783
|
const exclusionConstraint = exclusionOrName instanceof TableExclusion_1.TableExclusion ? exclusionOrName : table.exclusions.find(c => c.name === exclusionOrName);
|
|
@@ -674,8 +788,12 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
674
788
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
675
789
|
table.removeExclusionConstraint(exclusionConstraint);
|
|
676
790
|
}
|
|
791
|
+
/**
|
|
792
|
+
* Creates a new foreign key.
|
|
793
|
+
*/
|
|
677
794
|
async createForeignKey(tableOrName, foreignKey) {
|
|
678
795
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
796
|
+
// new FK may be passed without name. In this case we generate FK name manually.
|
|
679
797
|
if (!foreignKey.name)
|
|
680
798
|
foreignKey.name = this.connection.namingStrategy.foreignKeyName(table, foreignKey.columnNames, this.getTablePath(foreignKey), foreignKey.referencedColumnNames);
|
|
681
799
|
const up = this.createForeignKeySql(table, foreignKey);
|
|
@@ -683,6 +801,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
683
801
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
684
802
|
table.addForeignKey(foreignKey);
|
|
685
803
|
}
|
|
804
|
+
/**
|
|
805
|
+
* Drops a foreign key from the table.
|
|
806
|
+
*/
|
|
686
807
|
async dropForeignKey(tableOrName, foreignKeyOrName) {
|
|
687
808
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
688
809
|
const foreignKey = foreignKeyOrName instanceof TableForeignKey_1.TableForeignKey ? foreignKeyOrName : table.foreignKeys.find(fk => fk.name === foreignKeyOrName);
|
|
@@ -693,8 +814,12 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
693
814
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
694
815
|
table.removeForeignKey(foreignKey);
|
|
695
816
|
}
|
|
817
|
+
/**
|
|
818
|
+
* Creates a new index.
|
|
819
|
+
*/
|
|
696
820
|
async createIndex(tableOrName, index) {
|
|
697
821
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
822
|
+
// new index may be passed without name. In this case we generate index name manually.
|
|
698
823
|
if (!index.name)
|
|
699
824
|
index.name = this.connection.namingStrategy.indexName(table, index.columnNames, index.where);
|
|
700
825
|
const up = this.createIndexSql(table, index);
|
|
@@ -702,6 +827,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
702
827
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
703
828
|
table.addIndex(index);
|
|
704
829
|
}
|
|
830
|
+
/**
|
|
831
|
+
* Drops an index from the table.
|
|
832
|
+
*/
|
|
705
833
|
async dropIndex(tableOrName, indexOrName) {
|
|
706
834
|
const table = tableOrName instanceof Table_1.Table ? tableOrName : await this.getCachedTable(tableOrName);
|
|
707
835
|
const index = indexOrName instanceof TableIndex_1.TableIndex ? indexOrName : table.indices.find(i => i.name === indexOrName);
|
|
@@ -712,6 +840,9 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
712
840
|
await this.executeQueries(new TableQuery(table.name, up), new TableQuery(table.name, down));
|
|
713
841
|
table.removeIndex(index);
|
|
714
842
|
}
|
|
843
|
+
/**
|
|
844
|
+
* Executes sql used special for schema build.
|
|
845
|
+
*/
|
|
715
846
|
async executeQueries(upQueries, downQueries) {
|
|
716
847
|
const resultUpQueries = upQueries instanceof Query_1.Query || upQueries instanceof TableQuery
|
|
717
848
|
? [upQueries]
|
|
@@ -742,6 +873,7 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
742
873
|
const originalUpQueries = resultUpQueries.map(item => item instanceof TableQuery ? item.query : item);
|
|
743
874
|
this.sqlInMemory.upQueries.push(...originalUpQueries);
|
|
744
875
|
this.sqlInMemory.downQueries.push(...(resultDownQueries.map(item => item instanceof TableQuery ? item.query : item)));
|
|
876
|
+
// if sql-in-memory mode is enabled then simply store sql in memory and return
|
|
745
877
|
if (this.sqlMemoryMode === true) {
|
|
746
878
|
return Promise.resolve();
|
|
747
879
|
}
|
|
@@ -749,17 +881,34 @@ class CustomPostgresQueryRunner extends PostgresQueryRunner_1.PostgresQueryRunne
|
|
|
749
881
|
await this.query(query, parameters);
|
|
750
882
|
}
|
|
751
883
|
}
|
|
884
|
+
/**
|
|
885
|
+
* Enables special query runner mode in which sql queries won't be executed,
|
|
886
|
+
* instead they will be memorized into a special variable inside query runner.
|
|
887
|
+
* You can get memorized sql using getMemorySql() method.
|
|
888
|
+
*/
|
|
752
889
|
enableSqlMemory() {
|
|
753
890
|
this.sqlInMemory = new TableSqlInMemory();
|
|
754
891
|
this.sqlMemoryMode = true;
|
|
755
892
|
}
|
|
893
|
+
/**
|
|
894
|
+
* Disables special query runner mode in which sql queries won't be executed
|
|
895
|
+
* started by calling enableSqlMemory() method.
|
|
896
|
+
*
|
|
897
|
+
* Previously memorized sql will be flushed.
|
|
898
|
+
*/
|
|
756
899
|
disableSqlMemory() {
|
|
757
900
|
this.sqlInMemory = new TableSqlInMemory();
|
|
758
901
|
this.sqlMemoryMode = false;
|
|
759
902
|
}
|
|
903
|
+
/**
|
|
904
|
+
* Flushes all memorized sqls.
|
|
905
|
+
*/
|
|
760
906
|
clearSqlMemory() {
|
|
761
907
|
this.sqlInMemory = new TableSqlInMemory();
|
|
762
908
|
}
|
|
909
|
+
/**
|
|
910
|
+
* Gets sql stored in the memory. Parameters in the sql are already replaced.
|
|
911
|
+
*/
|
|
763
912
|
getMemorySql() {
|
|
764
913
|
return this.sqlInMemory;
|
|
765
914
|
}
|