• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

typeorm / typeorm / 12837043959

17 Jan 2025 09:20PM CUT coverage: 72.343% (-0.02%) from 72.359%
12837043959

Pull #11249

github

web-flow
Merge 80fb8699a into 9d1d3f100
Pull Request #11249: docs: fix missing DataSource in migrations config

8655 of 12649 branches covered (68.42%)

Branch coverage included in aggregate %.

17873 of 24021 relevant lines covered (74.41%)

122689.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.66
/src/driver/sqlserver/SqlServerQueryRunner.ts
1
import { ObjectLiteral } from "../../common/ObjectLiteral"
2
import { QueryResult } from "../../query-runner/QueryResult"
26✔
3
import { QueryFailedError } from "../../error/QueryFailedError"
26✔
4
import { QueryRunnerAlreadyReleasedError } from "../../error/QueryRunnerAlreadyReleasedError"
26✔
5
import { TransactionNotStartedError } from "../../error/TransactionNotStartedError"
26✔
6
import { ColumnType } from "../types/ColumnTypes"
7
import { ReadStream } from "../../platform/PlatformTools"
8
import { BaseQueryRunner } from "../../query-runner/BaseQueryRunner"
26✔
9
import { QueryRunner } from "../../query-runner/QueryRunner"
10
import { TableIndexOptions } from "../../schema-builder/options/TableIndexOptions"
11
import { Table } from "../../schema-builder/table/Table"
26✔
12
import { TableCheck } from "../../schema-builder/table/TableCheck"
26✔
13
import { TableColumn } from "../../schema-builder/table/TableColumn"
26✔
14
import { TableExclusion } from "../../schema-builder/table/TableExclusion"
15
import { TableForeignKey } from "../../schema-builder/table/TableForeignKey"
26✔
16
import { TableIndex } from "../../schema-builder/table/TableIndex"
26✔
17
import { TableUnique } from "../../schema-builder/table/TableUnique"
26✔
18
import { View } from "../../schema-builder/view/View"
26✔
19
import { Broadcaster } from "../../subscriber/Broadcaster"
26✔
20
import { OrmUtils } from "../../util/OrmUtils"
26✔
21
import { Query } from "../Query"
26✔
22
import { IsolationLevel } from "../types/IsolationLevel"
23
import { MssqlParameter } from "./MssqlParameter"
24
import { SqlServerDriver } from "./SqlServerDriver"
25
import { ReplicationMode } from "../types/ReplicationMode"
26
import { TypeORMError } from "../../error"
26✔
27
import { QueryLock } from "../../query-runner/QueryLock"
26✔
28
import { MetadataTableType } from "../types/MetadataTableType"
26✔
29
import { InstanceChecker } from "../../util/InstanceChecker"
26✔
30
import { BroadcasterResult } from "../../subscriber/BroadcasterResult"
26✔
31

32
/**
33
 * Runs queries on a single SQL Server database connection.
34
 */
35
export class SqlServerQueryRunner
26✔
36
    extends BaseQueryRunner
37
    implements QueryRunner
38
{
39
    // -------------------------------------------------------------------------
40
    // Public Implemented Properties
41
    // -------------------------------------------------------------------------
42

43
    /**
44
     * Database driver used by connection.
45
     */
46
    driver: SqlServerDriver
47

48
    // -------------------------------------------------------------------------
49
    // Private Properties
50
    // -------------------------------------------------------------------------
51

52
    private lock: QueryLock = new QueryLock()
35,343✔
53

54
    // -------------------------------------------------------------------------
55
    // Constructor
56
    // -------------------------------------------------------------------------
57

58
    constructor(driver: SqlServerDriver, mode: ReplicationMode) {
59
        super()
35,343✔
60
        this.driver = driver
35,343✔
61
        this.connection = driver.connection
35,343✔
62
        this.broadcaster = new Broadcaster(this)
35,343✔
63
        this.mode = mode
35,343✔
64
    }
65

66
    // -------------------------------------------------------------------------
67
    // Public Methods
68
    // -------------------------------------------------------------------------
69

70
    /**
71
     * Creates/uses database connection from the connection pool to perform further operations.
72
     * Returns obtained database connection.
73
     */
74
    connect(): Promise<void> {
75
        return Promise.resolve()
3✔
76
    }
77

78
    /**
79
     * Releases used database connection.
80
     * You cannot use query runner methods once its released.
81
     */
82
    release(): Promise<void> {
83
        this.isReleased = true
35,343✔
84
        return Promise.resolve()
35,343✔
85
    }
86

87
    /**
88
     * Starts transaction.
89
     */
90
    async startTransaction(isolationLevel?: IsolationLevel): Promise<void> {
91
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
26,628!
92

93
        this.isTransactionActive = true
26,628✔
94
        try {
26,628✔
95
            await this.broadcaster.broadcast("BeforeTransactionStart")
26,628✔
96
        } catch (err) {
97
            this.isTransactionActive = false
×
98
            throw err
×
99
        }
100
        await new Promise<void>(async (ok, fail) => {
26,628✔
101
            const transactionCallback = (err: any) => {
26,628✔
102
                if (err) {
26,571!
103
                    this.isTransactionActive = false
×
104
                    return fail(err)
×
105
                }
106
                ok()
26,571✔
107
            }
108

109
            if (this.transactionDepth === 0) {
26,628✔
110
                this.transactionDepth += 1
26,571✔
111
                const pool = await (this.mode === "slave"
26,571!
112
                    ? this.driver.obtainSlaveConnection()
113
                    : this.driver.obtainMasterConnection())
114
                this.databaseConnection = pool.transaction()
26,571✔
115
                this.connection.logger.logQuery("BEGIN TRANSACTION")
26,571✔
116
                if (isolationLevel) {
26,571✔
117
                    this.databaseConnection.begin(
18✔
118
                        this.convertIsolationLevel(isolationLevel),
119
                        transactionCallback,
120
                    )
121
                    this.connection.logger.logQuery(
18✔
122
                        "SET TRANSACTION ISOLATION LEVEL " + isolationLevel,
123
                    )
124
                } else {
125
                    this.databaseConnection.begin(transactionCallback)
26,553✔
126
                }
127
            } else {
128
                this.transactionDepth += 1
57✔
129
                await this.query(
57✔
130
                    `SAVE TRANSACTION typeorm_${this.transactionDepth - 1}`,
131
                )
132
                ok()
57✔
133
            }
134
        })
135

136
        await this.broadcaster.broadcast("AfterTransactionStart")
26,628✔
137
    }
138

139
    /**
140
     * Commits transaction.
141
     * Error will be thrown if transaction was not started.
142
     */
143
    async commitTransaction(): Promise<void> {
144
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
26,550!
145

146
        if (!this.isTransactionActive) throw new TransactionNotStartedError()
26,550!
147

148
        await this.broadcaster.broadcast("BeforeTransactionCommit")
26,550✔
149

150
        if (this.transactionDepth === 1) {
26,550✔
151
            return new Promise<void>((ok, fail) => {
26,511✔
152
                this.transactionDepth -= 1
26,511✔
153
                this.databaseConnection.commit(async (err: any) => {
26,511✔
154
                    if (err) return fail(err)
26,511!
155
                    this.isTransactionActive = false
26,511✔
156
                    this.databaseConnection = null
26,511✔
157

158
                    await this.broadcaster.broadcast("AfterTransactionCommit")
26,511✔
159

160
                    ok()
26,511✔
161
                    this.connection.logger.logQuery("COMMIT")
26,511✔
162
                })
163
            })
164
        }
165
        this.transactionDepth -= 1
39✔
166
    }
167

168
    /**
169
     * Rollbacks transaction.
170
     * Error will be thrown if transaction was not started.
171
     */
172
    async rollbackTransaction(): Promise<void> {
173
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
78!
174

175
        if (!this.isTransactionActive) throw new TransactionNotStartedError()
78!
176

177
        await this.broadcaster.broadcast("BeforeTransactionRollback")
78✔
178

179
        if (this.transactionDepth > 1) {
78✔
180
            this.transactionDepth -= 1
18✔
181
            await this.query(
18✔
182
                `ROLLBACK TRANSACTION typeorm_${this.transactionDepth}`,
183
            )
184
        } else {
185
            return new Promise<void>((ok, fail) => {
60✔
186
                this.transactionDepth -= 1
60✔
187
                this.databaseConnection.rollback(async (err: any) => {
60✔
188
                    if (err) return fail(err)
60✔
189
                    this.isTransactionActive = false
57✔
190
                    this.databaseConnection = null
57✔
191

192
                    await this.broadcaster.broadcast("AfterTransactionRollback")
57✔
193

194
                    ok()
57✔
195
                    this.connection.logger.logQuery("ROLLBACK")
57✔
196
                })
197
            })
198
        }
199
    }
200

201
    /**
202
     * Executes a given SQL query.
203
     */
204
    async query(
205
        query: string,
206
        parameters?: any[],
207
        useStructuredResult = false,
107,679✔
208
    ): Promise<any> {
209
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
153,024!
210

211
        const release = await this.lock.acquire()
153,024✔
212

213
        const broadcasterResult = new BroadcasterResult()
153,024✔
214

215
        try {
153,024✔
216
            this.driver.connection.logger.logQuery(query, parameters, this)
153,024✔
217
            this.broadcaster.broadcastBeforeQueryEvent(
153,024✔
218
                broadcasterResult,
219
                query,
220
                parameters,
221
            )
222

223
            const pool = await (this.mode === "slave"
153,024✔
224
                ? this.driver.obtainSlaveConnection()
225
                : this.driver.obtainMasterConnection())
226
            const request = new this.driver.mssql.Request(
153,024✔
227
                this.isTransactionActive ? this.databaseConnection : pool,
153,024✔
228
            )
229
            if (parameters && parameters.length) {
153,024✔
230
                parameters.forEach((parameter, index) => {
43,911✔
231
                    const parameterName = index.toString()
103,548✔
232
                    if (InstanceChecker.isMssqlParameter(parameter)) {
103,548✔
233
                        const mssqlParameter =
234
                            this.mssqlParameterToNativeParameter(parameter)
74,124✔
235
                        if (mssqlParameter) {
74,124✔
236
                            request.input(
74,073✔
237
                                parameterName,
238
                                mssqlParameter,
239
                                parameter.value,
240
                            )
241
                        } else {
242
                            request.input(parameterName, parameter.value)
51✔
243
                        }
244
                    } else {
245
                        request.input(parameterName, parameter)
29,424✔
246
                    }
247
                })
248
            }
249
            const queryStartTime = +new Date()
153,024✔
250

251
            const raw = await new Promise<any>((ok, fail) => {
153,024✔
252
                request.query(query, (err: any, raw: any) => {
153,024✔
253
                    // log slow queries if maxQueryExecution time is set
254
                    const maxQueryExecutionTime =
255
                        this.driver.options.maxQueryExecutionTime
153,024✔
256
                    const queryEndTime = +new Date()
153,024✔
257
                    const queryExecutionTime = queryEndTime - queryStartTime
153,024✔
258

259
                    this.broadcaster.broadcastAfterQueryEvent(
153,024✔
260
                        broadcasterResult,
261
                        query,
262
                        parameters,
263
                        true,
264
                        queryExecutionTime,
265
                        raw,
266
                        undefined,
267
                    )
268

269
                    if (
153,024!
270
                        maxQueryExecutionTime &&
153,024!
271
                        queryExecutionTime > maxQueryExecutionTime
272
                    ) {
273
                        this.driver.connection.logger.logQuerySlow(
×
274
                            queryExecutionTime,
275
                            query,
276
                            parameters,
277
                            this,
278
                        )
279
                    }
280

281
                    if (err) {
153,024✔
282
                        fail(new QueryFailedError(query, parameters, err))
24✔
283
                    }
284

285
                    ok(raw)
153,024✔
286
                })
287
            })
288

289
            const result = new QueryResult()
153,000✔
290

291
            if (raw?.hasOwnProperty("recordset")) {
153,000✔
292
                result.records = raw.recordset
153,000✔
293
            }
294

295
            if (raw?.hasOwnProperty("rowsAffected")) {
153,000✔
296
                result.affected = raw.rowsAffected[0]
153,000✔
297
            }
298

299
            const queryType = query.slice(0, query.indexOf(" "))
153,000✔
300
            switch (queryType) {
153,000✔
301
                case "DELETE":
302
                    // for DELETE query additionally return number of affected rows
303
                    result.raw = [raw.recordset, raw.rowsAffected[0]]
378✔
304
                    break
378✔
305
                default:
306
                    result.raw = raw.recordset
152,622✔
307
            }
308

309
            if (useStructuredResult) {
153,000✔
310
                return result
45,327✔
311
            } else {
312
                return result.raw
107,673✔
313
            }
314
        } catch (err) {
315
            this.driver.connection.logger.logQueryError(
24✔
316
                err,
317
                query,
318
                parameters,
319
                this,
320
            )
321
            this.broadcaster.broadcastAfterQueryEvent(
24✔
322
                broadcasterResult,
323
                query,
324
                parameters,
325
                false,
326
                undefined,
327
                undefined,
328
                err,
329
            )
330

331
            throw err
24✔
332
        } finally {
333
            await broadcasterResult.wait()
153,024✔
334

335
            release()
153,024✔
336
        }
337
    }
338

339
    /**
340
     * Returns raw data stream.
341
     */
342
    async stream(
343
        query: string,
344
        parameters?: any[],
345
        onEnd?: Function,
346
        onError?: Function,
347
    ): Promise<ReadStream> {
348
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
3!
349

350
        const release = await this.lock.acquire()
3✔
351

352
        this.driver.connection.logger.logQuery(query, parameters, this)
3✔
353
        const pool = await (this.mode === "slave"
3!
354
            ? this.driver.obtainSlaveConnection()
355
            : this.driver.obtainMasterConnection())
356
        const request = new this.driver.mssql.Request(
3✔
357
            this.isTransactionActive ? this.databaseConnection : pool,
3!
358
        )
359
        if (parameters && parameters.length) {
3!
360
            parameters.forEach((parameter, index) => {
×
361
                const parameterName = index.toString()
×
362
                if (InstanceChecker.isMssqlParameter(parameter)) {
×
363
                    request.input(
×
364
                        parameterName,
365
                        this.mssqlParameterToNativeParameter(parameter),
366
                        parameter.value,
367
                    )
368
                } else {
369
                    request.input(parameterName, parameter)
×
370
                }
371
            })
372
        }
373

374
        request.query(query)
3✔
375

376
        const streamRequest = request.toReadableStream()
3✔
377

378
        streamRequest.on("error", (err: any) => {
3✔
379
            release()
×
380
            this.driver.connection.logger.logQueryError(
×
381
                err,
382
                query,
383
                parameters,
384
                this,
385
            )
386
        })
387

388
        streamRequest.on("end", () => {
3✔
389
            release()
3✔
390
        })
391

392
        if (onEnd) {
3!
393
            streamRequest.on("end", onEnd)
×
394
        }
395

396
        if (onError) {
3!
397
            streamRequest.on("error", onError)
×
398
        }
399

400
        return streamRequest
3✔
401
    }
402

403
    /**
404
     * Returns all available database names including system databases.
405
     */
406
    async getDatabases(): Promise<string[]> {
407
        const results: ObjectLiteral[] = await this.query(`EXEC sp_databases`)
×
408
        return results.map((result) => result["DATABASE_NAME"])
×
409
    }
410

411
    /**
412
     * Returns all available schema names including system schemas.
413
     * If database parameter specified, returns schemas of that database.
414
     */
415
    async getSchemas(database?: string): Promise<string[]> {
416
        const query = database
×
417
            ? `SELECT * FROM "${database}"."sys"."schema"`
418
            : `SELECT * FROM "sys"."schemas"`
419
        const results: ObjectLiteral[] = await this.query(query)
×
420
        return results.map((result) => result["name"])
×
421
    }
422

423
    /**
424
     * Checks if database with the given name exist.
425
     */
426
    async hasDatabase(database: string): Promise<boolean> {
427
        const result = await this.query(
4,062✔
428
            `SELECT DB_ID('${database}') as "db_id"`,
429
        )
430
        const dbId = result[0]["db_id"]
4,062✔
431
        return !!dbId
4,062✔
432
    }
433

434
    /**
435
     * Loads currently using database
436
     */
437
    async getCurrentDatabase(): Promise<string> {
438
        const currentDBQuery = await this.query(`SELECT DB_NAME() AS "db_name"`)
5,136✔
439
        return currentDBQuery[0]["db_name"]
5,136✔
440
    }
441

442
    /**
443
     * Checks if schema with the given name exist.
444
     */
445
    async hasSchema(schema: string): Promise<boolean> {
446
        const result = await this.query(
9✔
447
            `SELECT SCHEMA_ID('${schema}') as "schema_id"`,
448
        )
449
        const schemaId = result[0]["schema_id"]
9✔
450
        return !!schemaId
9✔
451
    }
452

453
    /**
454
     * Loads currently using database schema
455
     */
456
    async getCurrentSchema(): Promise<string> {
457
        const currentSchemaQuery = await this.query(
6,429✔
458
            `SELECT SCHEMA_NAME() AS "schema_name"`,
459
        )
460
        return currentSchemaQuery[0]["schema_name"]
6,429✔
461
    }
462

463
    /**
464
     * Checks if table with the given name exist in the database.
465
     */
466
    async hasTable(tableOrName: Table | string): Promise<boolean> {
467
        const parsedTableName = this.driver.parseTableName(tableOrName)
4,386✔
468

469
        if (!parsedTableName.database) {
4,386!
470
            parsedTableName.database = await this.getCurrentDatabase()
×
471
        }
472

473
        if (!parsedTableName.schema) {
4,386!
474
            parsedTableName.schema = await this.getCurrentSchema()
×
475
        }
476

477
        const sql = `SELECT * FROM "${parsedTableName.database}"."INFORMATION_SCHEMA"."TABLES" WHERE "TABLE_NAME" = '${parsedTableName.tableName}' AND "TABLE_SCHEMA" = '${parsedTableName.schema}'`
4,386✔
478
        const result = await this.query(sql)
4,386✔
479
        return result.length ? true : false
4,386✔
480
    }
481

482
    /**
483
     * Checks if column exist in the table.
484
     */
485
    async hasColumn(
486
        tableOrName: Table | string,
487
        columnName: string,
488
    ): Promise<boolean> {
489
        const parsedTableName = this.driver.parseTableName(tableOrName)
12✔
490

491
        if (!parsedTableName.database) {
12!
492
            parsedTableName.database = await this.getCurrentDatabase()
×
493
        }
494

495
        if (!parsedTableName.schema) {
12!
496
            parsedTableName.schema = await this.getCurrentSchema()
×
497
        }
498

499
        const sql = `SELECT * FROM "${parsedTableName.database}"."INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = '${parsedTableName.tableName}' AND "TABLE_SCHEMA" = '${parsedTableName.schema}' AND "COLUMN_NAME" = '${columnName}'`
12✔
500
        const result = await this.query(sql)
12✔
501
        return result.length ? true : false
12✔
502
    }
503

504
    /**
505
     * Creates a new database.
506
     */
507
    async createDatabase(
508
        database: string,
509
        ifNotExist?: boolean,
510
    ): Promise<void> {
511
        const up = ifNotExist
27!
512
            ? `IF DB_ID('${database}') IS NULL CREATE DATABASE "${database}"`
513
            : `CREATE DATABASE "${database}"`
514
        const down = `DROP DATABASE "${database}"`
27✔
515
        await this.executeQueries(new Query(up), new Query(down))
27✔
516
    }
517

518
    /**
519
     * Drops database.
520
     */
521
    async dropDatabase(database: string, ifExist?: boolean): Promise<void> {
522
        const up = ifExist
6✔
523
            ? `IF DB_ID('${database}') IS NOT NULL DROP DATABASE "${database}"`
524
            : `DROP DATABASE "${database}"`
525
        const down = `CREATE DATABASE "${database}"`
6✔
526
        await this.executeQueries(new Query(up), new Query(down))
6✔
527
    }
528

529
    /**
530
     * Creates table schema.
531
     * If database name also specified (e.g. 'dbName.schemaName') schema will be created in specified database.
532
     */
533
    async createSchema(
534
        schemaPath: string,
535
        ifNotExist?: boolean,
536
    ): Promise<void> {
537
        const upQueries: Query[] = []
42✔
538
        const downQueries: Query[] = []
42✔
539

540
        if (schemaPath.indexOf(".") === -1) {
42✔
541
            const upQuery = ifNotExist
21!
542
                ? `IF SCHEMA_ID('${schemaPath}') IS NULL BEGIN EXEC ('CREATE SCHEMA "${schemaPath}"') END`
543
                : `CREATE SCHEMA "${schemaPath}"`
544
            upQueries.push(new Query(upQuery))
21✔
545
            downQueries.push(new Query(`DROP SCHEMA "${schemaPath}"`))
21✔
546
        } else {
547
            const dbName = schemaPath.split(".")[0]
21✔
548
            const schema = schemaPath.split(".")[1]
21✔
549
            const currentDB = await this.getCurrentDatabase()
21✔
550
            upQueries.push(new Query(`USE "${dbName}"`))
21✔
551
            downQueries.push(new Query(`USE "${currentDB}"`))
21✔
552

553
            const upQuery = ifNotExist
21!
554
                ? `IF SCHEMA_ID('${schema}') IS NULL BEGIN EXEC ('CREATE SCHEMA "${schema}"') END`
555
                : `CREATE SCHEMA "${schema}"`
556
            upQueries.push(new Query(upQuery))
21✔
557
            downQueries.push(new Query(`DROP SCHEMA "${schema}"`))
21✔
558

559
            upQueries.push(new Query(`USE "${currentDB}"`))
21✔
560
            downQueries.push(new Query(`USE "${dbName}"`))
21✔
561
        }
562

563
        await this.executeQueries(upQueries, downQueries)
42✔
564
    }
565

566
    /**
567
     * Drops table schema.
568
     * If database name also specified (e.g. 'dbName.schemaName') schema will be dropped in specified database.
569
     */
570
    async dropSchema(schemaPath: string, ifExist?: boolean): Promise<void> {
571
        const upQueries: Query[] = []
3✔
572
        const downQueries: Query[] = []
3✔
573

574
        if (schemaPath.indexOf(".") === -1) {
3!
575
            const upQuery = ifExist
3!
576
                ? `IF SCHEMA_ID('${schemaPath}') IS NULL BEGIN EXEC ('DROP SCHEMA "${schemaPath}"') END`
577
                : `DROP SCHEMA "${schemaPath}"`
578
            upQueries.push(new Query(upQuery))
3✔
579
            downQueries.push(new Query(`CREATE SCHEMA "${schemaPath}"`))
3✔
580
        } else {
581
            const dbName = schemaPath.split(".")[0]
×
582
            const schema = schemaPath.split(".")[1]
×
583
            const currentDB = await this.getCurrentDatabase()
×
584
            upQueries.push(new Query(`USE "${dbName}"`))
×
585
            downQueries.push(new Query(`USE "${currentDB}"`))
×
586

587
            const upQuery = ifExist
×
588
                ? `IF SCHEMA_ID('${schema}') IS NULL BEGIN EXEC ('DROP SCHEMA "${schema}"') END`
589
                : `DROP SCHEMA "${schema}"`
590
            upQueries.push(new Query(upQuery))
×
591
            downQueries.push(new Query(`CREATE SCHEMA "${schema}"`))
×
592

593
            upQueries.push(new Query(`USE "${currentDB}"`))
×
594
            downQueries.push(new Query(`USE "${dbName}"`))
×
595
        }
596

597
        await this.executeQueries(upQueries, downQueries)
3✔
598
    }
599

600
    /**
601
     * Creates a new table.
602
     */
603
    async createTable(
604
        table: Table,
605
        ifNotExist: boolean = false,
69✔
606
        createForeignKeys: boolean = true,
156✔
607
        createIndices: boolean = true,
14,139✔
608
    ): Promise<void> {
609
        if (ifNotExist) {
14,139✔
610
            const isTableExist = await this.hasTable(table)
87✔
611
            if (isTableExist) return Promise.resolve()
87✔
612
        }
613
        const upQueries: Query[] = []
14,133✔
614
        const downQueries: Query[] = []
14,133✔
615

616
        upQueries.push(this.createTableSql(table, createForeignKeys))
14,133✔
617
        downQueries.push(this.dropTableSql(table))
14,133✔
618

619
        // if createForeignKeys is true, we must drop created foreign keys in down query.
620
        // createTable does not need separate method to create foreign keys, because it create fk's in the same query with table creation.
621
        if (createForeignKeys)
14,133✔
622
            table.foreignKeys.forEach((foreignKey) =>
150✔
623
                downQueries.push(this.dropForeignKeySql(table, foreignKey)),
9✔
624
            )
625

626
        if (createIndices) {
14,133✔
627
            table.indices.forEach((index) => {
14,133✔
628
                // new index may be passed without name. In this case we generate index name manually.
629
                if (!index.name)
6,357✔
630
                    index.name = this.connection.namingStrategy.indexName(
9✔
631
                        table,
632
                        index.columnNames,
633
                        index.where,
634
                    )
635
                upQueries.push(this.createIndexSql(table, index))
6,357✔
636
                downQueries.push(this.dropIndexSql(table, index))
6,357✔
637
            })
638
        }
639

640
        // if table have column with generated type, we must add the expression to the metadata table
641
        const generatedColumns = table.columns.filter(
14,133✔
642
            (column) => column.generatedType && column.asExpression,
45,705✔
643
        )
644

645
        for (const column of generatedColumns) {
14,133✔
646
            const parsedTableName = this.driver.parseTableName(table)
84✔
647

648
            if (!parsedTableName.schema) {
84!
649
                parsedTableName.schema = await this.getCurrentSchema()
×
650
            }
651

652
            const insertQuery = this.insertTypeormMetadataSql({
84✔
653
                database: parsedTableName.database,
654
                schema: parsedTableName.schema,
655
                table: parsedTableName.tableName,
656
                type: MetadataTableType.GENERATED_COLUMN,
657
                name: column.name,
658
                value: column.asExpression,
659
            })
660

661
            const deleteQuery = this.deleteTypeormMetadataSql({
84✔
662
                database: parsedTableName.database,
663
                schema: parsedTableName.schema,
664
                table: parsedTableName.tableName,
665
                type: MetadataTableType.GENERATED_COLUMN,
666
                name: column.name,
667
            })
668

669
            upQueries.push(insertQuery)
84✔
670
            downQueries.push(deleteQuery)
84✔
671
        }
672

673
        await this.executeQueries(upQueries, downQueries)
14,133✔
674
    }
675

676
    /**
677
     * Drops the table.
678
     */
679
    async dropTable(
680
        tableOrName: Table | string,
681
        ifExist?: boolean,
682
        dropForeignKeys: boolean = true,
15✔
683
        dropIndices: boolean = true,
21✔
684
    ): Promise<void> {
685
        if (ifExist) {
21✔
686
            const isTableExist = await this.hasTable(tableOrName)
6✔
687
            if (!isTableExist) return Promise.resolve()
6!
688
        }
689

690
        // if dropTable called with dropForeignKeys = true, we must create foreign keys in down query.
691
        const createForeignKeys: boolean = dropForeignKeys
21✔
692
        const table = InstanceChecker.isTable(tableOrName)
21✔
693
            ? tableOrName
694
            : await this.getCachedTable(tableOrName)
695
        const upQueries: Query[] = []
21✔
696
        const downQueries: Query[] = []
21✔
697

698
        // It needs because if table does not exist and dropForeignKeys or dropIndices is true, we don't need
699
        // to perform drop queries for foreign keys and indices.
700

701
        if (dropIndices) {
21✔
702
            table.indices.forEach((index) => {
21✔
703
                upQueries.push(this.dropIndexSql(table, index))
3✔
704
                downQueries.push(this.createIndexSql(table, index))
3✔
705
            })
706
        }
707

708
        // if dropForeignKeys is true, we just drop the table, otherwise we also drop table foreign keys.
709
        // createTable does not need separate method to create foreign keys, because it create fk's in the same query with table creation.
710
        if (dropForeignKeys)
21✔
711
            table.foreignKeys.forEach((foreignKey) =>
15✔
712
                upQueries.push(this.dropForeignKeySql(table, foreignKey)),
6✔
713
            )
714

715
        upQueries.push(this.dropTableSql(table))
21✔
716
        downQueries.push(this.createTableSql(table, createForeignKeys))
21✔
717

718
        // if table had columns with generated type, we must remove the expression from the metadata table
719
        const generatedColumns = table.columns.filter(
21✔
720
            (column) => column.generatedType && column.asExpression,
81✔
721
        )
722

723
        for (const column of generatedColumns) {
21✔
724
            const parsedTableName = this.driver.parseTableName(table)
12✔
725

726
            if (!parsedTableName.schema) {
12!
727
                parsedTableName.schema = await this.getCurrentSchema()
×
728
            }
729

730
            const deleteQuery = this.deleteTypeormMetadataSql({
12✔
731
                database: parsedTableName.database,
732
                schema: parsedTableName.schema,
733
                table: parsedTableName.tableName,
734
                type: MetadataTableType.GENERATED_COLUMN,
735
                name: column.name,
736
            })
737

738
            const insertQuery = this.insertTypeormMetadataSql({
12✔
739
                database: parsedTableName.database,
740
                schema: parsedTableName.schema,
741
                table: parsedTableName.tableName,
742
                type: MetadataTableType.GENERATED_COLUMN,
743
                name: column.name,
744
                value: column.asExpression,
745
            })
746

747
            upQueries.push(deleteQuery)
12✔
748
            downQueries.push(insertQuery)
12✔
749
        }
750

751
        await this.executeQueries(upQueries, downQueries)
21✔
752
    }
753

754
    /**
755
     * Creates a new view.
756
     */
757
    async createView(
758
        view: View,
759
        syncWithMetadata: boolean = false,
×
760
    ): Promise<void> {
761
        const upQueries: Query[] = []
24✔
762
        const downQueries: Query[] = []
24✔
763
        upQueries.push(this.createViewSql(view))
24✔
764
        if (syncWithMetadata)
24✔
765
            upQueries.push(await this.insertViewDefinitionSql(view))
24✔
766
        downQueries.push(this.dropViewSql(view))
24✔
767
        if (syncWithMetadata)
24✔
768
            downQueries.push(await this.deleteViewDefinitionSql(view))
24✔
769
        await this.executeQueries(upQueries, downQueries)
24✔
770
    }
771

772
    /**
773
     * Drops the view.
774
     */
775
    async dropView(target: View | string): Promise<void> {
776
        const viewName = InstanceChecker.isView(target) ? target.name : target
×
777
        const view = await this.getCachedView(viewName)
×
778

779
        const upQueries: Query[] = []
×
780
        const downQueries: Query[] = []
×
781
        upQueries.push(await this.deleteViewDefinitionSql(view))
×
782
        upQueries.push(this.dropViewSql(view))
×
783
        downQueries.push(await this.insertViewDefinitionSql(view))
×
784
        downQueries.push(this.createViewSql(view))
×
785
        await this.executeQueries(upQueries, downQueries)
×
786
    }
787

788
    /**
789
     * Renames a table.
790
     */
791
    async renameTable(
792
        oldTableOrName: Table | string,
793
        newTableName: string,
794
    ): Promise<void> {
795
        const upQueries: Query[] = []
51✔
796
        const downQueries: Query[] = []
51✔
797
        const oldTable = InstanceChecker.isTable(oldTableOrName)
51✔
798
            ? oldTableOrName
799
            : await this.getCachedTable(oldTableOrName)
800
        let newTable = oldTable.clone()
51✔
801

802
        // we need database name and schema name to rename FK constraints
803
        let dbName: string | undefined = undefined
51✔
804
        let schemaName: string | undefined = undefined
51✔
805
        let oldTableName: string = oldTable.name
51✔
806
        const splittedName = oldTable.name.split(".")
51✔
807
        if (splittedName.length === 3) {
51✔
808
            dbName = splittedName[0]
6✔
809
            oldTableName = splittedName[2]
6✔
810
            if (splittedName[1] !== "") schemaName = splittedName[1]
6✔
811
        } else if (splittedName.length === 2) {
45!
812
            schemaName = splittedName[0]
×
813
            oldTableName = splittedName[1]
×
814
        }
815

816
        newTable.name = this.driver.buildTableName(
51✔
817
            newTableName,
818
            schemaName,
819
            dbName,
820
        )
821

822
        // if we have tables with database which differs from database specified in config, we must change currently used database.
823
        // This need because we can not rename objects from another database.
824
        const currentDB = await this.getCurrentDatabase()
51✔
825
        if (dbName && dbName !== currentDB) {
51✔
826
            upQueries.push(new Query(`USE "${dbName}"`))
6✔
827
            downQueries.push(new Query(`USE "${currentDB}"`))
6✔
828
        }
829

830
        // rename table
831
        upQueries.push(
51✔
832
            new Query(
833
                `EXEC sp_rename "${this.getTablePath(
834
                    oldTable,
835
                )}", "${newTableName}"`,
836
            ),
837
        )
838
        downQueries.push(
51✔
839
            new Query(
840
                `EXEC sp_rename "${this.getTablePath(
841
                    newTable,
842
                )}", "${oldTableName}"`,
843
            ),
844
        )
845

846
        // rename primary key constraint
847
        if (
51✔
848
            newTable.primaryColumns.length > 0 &&
102✔
849
            !newTable.primaryColumns[0].primaryKeyConstraintName
850
        ) {
851
            const columnNames = newTable.primaryColumns.map(
39✔
852
                (column) => column.name,
45✔
853
            )
854

855
            const oldPkName = this.connection.namingStrategy.primaryKeyName(
39✔
856
                oldTable,
857
                columnNames,
858
            )
859
            const newPkName = this.connection.namingStrategy.primaryKeyName(
39✔
860
                newTable,
861
                columnNames,
862
            )
863

864
            // rename primary constraint
865
            upQueries.push(
39✔
866
                new Query(
867
                    `EXEC sp_rename "${this.getTablePath(
868
                        newTable,
869
                    )}.${oldPkName}", "${newPkName}"`,
870
                ),
871
            )
872
            downQueries.push(
39✔
873
                new Query(
874
                    `EXEC sp_rename "${this.getTablePath(
875
                        newTable,
876
                    )}.${newPkName}", "${oldPkName}"`,
877
                ),
878
            )
879
        }
880

881
        // rename unique constraints
882
        newTable.uniques.forEach((unique) => {
51✔
883
            const oldUniqueName =
884
                this.connection.namingStrategy.uniqueConstraintName(
15✔
885
                    oldTable,
886
                    unique.columnNames,
887
                )
888

889
            // Skip renaming if Unique has user defined constraint name
890
            if (unique.name !== oldUniqueName) return
15✔
891

892
            // build new constraint name
893
            const newUniqueName =
894
                this.connection.namingStrategy.uniqueConstraintName(
9✔
895
                    newTable,
896
                    unique.columnNames,
897
                )
898

899
            // build queries
900
            upQueries.push(
9✔
901
                new Query(
902
                    `EXEC sp_rename "${this.getTablePath(newTable)}.${
903
                        unique.name
904
                    }", "${newUniqueName}"`,
905
                ),
906
            )
907
            downQueries.push(
9✔
908
                new Query(
909
                    `EXEC sp_rename "${this.getTablePath(
910
                        newTable,
911
                    )}.${newUniqueName}", "${unique.name}"`,
912
                ),
913
            )
914

915
            // replace constraint name
916
            unique.name = newUniqueName
9✔
917
        })
918

919
        // rename index constraints
920
        newTable.indices.forEach((index) => {
51✔
921
            const oldIndexName = this.connection.namingStrategy.indexName(
33✔
922
                oldTable,
923
                index.columnNames,
924
                index.where,
925
            )
926

927
            // Skip renaming if Index has user defined constraint name
928
            if (index.name !== oldIndexName) return
33✔
929

930
            // build new constraint name
931
            const newIndexName = this.connection.namingStrategy.indexName(
15✔
932
                newTable,
933
                index.columnNames,
934
                index.where,
935
            )
936

937
            // build queries
938
            upQueries.push(
15✔
939
                new Query(
940
                    `EXEC sp_rename "${this.getTablePath(newTable)}.${
941
                        index.name
942
                    }", "${newIndexName}", "INDEX"`,
943
                ),
944
            )
945
            downQueries.push(
15✔
946
                new Query(
947
                    `EXEC sp_rename "${this.getTablePath(
948
                        newTable,
949
                    )}.${newIndexName}", "${index.name}", "INDEX"`,
950
                ),
951
            )
952

953
            // replace constraint name
954
            index.name = newIndexName
15✔
955
        })
956

957
        // rename foreign key constraints
958
        newTable.foreignKeys.forEach((foreignKey) => {
51✔
959
            const oldForeignKeyName =
960
                this.connection.namingStrategy.foreignKeyName(
27✔
961
                    oldTable,
962
                    foreignKey.columnNames,
963
                    this.getTablePath(foreignKey),
964
                    foreignKey.referencedColumnNames,
965
                )
966

967
            // Skip renaming if foreign key has user defined constraint name
968
            if (foreignKey.name !== oldForeignKeyName) return
27✔
969

970
            // build new constraint name
971
            const newForeignKeyName =
972
                this.connection.namingStrategy.foreignKeyName(
3✔
973
                    newTable,
974
                    foreignKey.columnNames,
975
                    this.getTablePath(foreignKey),
976
                    foreignKey.referencedColumnNames,
977
                )
978

979
            // build queries
980
            upQueries.push(
3✔
981
                new Query(
982
                    `EXEC sp_rename "${this.buildForeignKeyName(
983
                        foreignKey.name!,
984
                        schemaName,
985
                        dbName,
986
                    )}", "${newForeignKeyName}"`,
987
                ),
988
            )
989
            downQueries.push(
3✔
990
                new Query(
991
                    `EXEC sp_rename "${this.buildForeignKeyName(
992
                        newForeignKeyName,
993
                        schemaName,
994
                        dbName,
995
                    )}", "${foreignKey.name}"`,
996
                ),
997
            )
998

999
            // replace constraint name
1000
            foreignKey.name = newForeignKeyName
3✔
1001
        })
1002

1003
        // change currently used database back to default db.
1004
        if (dbName && dbName !== currentDB) {
51✔
1005
            upQueries.push(new Query(`USE "${currentDB}"`))
6✔
1006
            downQueries.push(new Query(`USE "${dbName}"`))
6✔
1007
        }
1008

1009
        await this.executeQueries(upQueries, downQueries)
51✔
1010

1011
        // rename old table and replace it in cached tabled;
1012
        oldTable.name = newTable.name
51✔
1013
        this.replaceCachedTable(oldTable, newTable)
51✔
1014
    }
1015

1016
    /**
1017
     * Creates a new column from the column in the table.
1018
     */
1019
    async addColumn(
1020
        tableOrName: Table | string,
1021
        column: TableColumn,
1022
    ): Promise<void> {
1023
        const table = InstanceChecker.isTable(tableOrName)
111✔
1024
            ? tableOrName
1025
            : await this.getCachedTable(tableOrName)
1026
        const clonedTable = table.clone()
111✔
1027
        const upQueries: Query[] = []
111✔
1028
        const downQueries: Query[] = []
111✔
1029

1030
        upQueries.push(
111✔
1031
            new Query(
1032
                `ALTER TABLE ${this.escapePath(
1033
                    table,
1034
                )} ADD ${this.buildCreateColumnSql(
1035
                    table,
1036
                    column,
1037
                    false,
1038
                    true,
1039
                )}`,
1040
            ),
1041
        )
1042
        downQueries.push(
111✔
1043
            new Query(
1044
                `ALTER TABLE ${this.escapePath(table)} DROP COLUMN "${
1045
                    column.name
1046
                }"`,
1047
            ),
1048
        )
1049

1050
        // create or update primary key constraint
1051
        if (column.isPrimary) {
111✔
1052
            const primaryColumns = clonedTable.primaryColumns
21✔
1053
            // if table already have primary key, me must drop it and recreate again
1054
            if (primaryColumns.length > 0) {
21✔
1055
                const pkName = primaryColumns[0].primaryKeyConstraintName
6!
1056
                    ? primaryColumns[0].primaryKeyConstraintName
1057
                    : this.connection.namingStrategy.primaryKeyName(
1058
                          clonedTable,
1059
                          primaryColumns.map((column) => column.name),
6✔
1060
                      )
1061

1062
                const columnNames = primaryColumns
6✔
1063
                    .map((column) => `"${column.name}"`)
6✔
1064
                    .join(", ")
1065

1066
                upQueries.push(
6✔
1067
                    new Query(
1068
                        `ALTER TABLE ${this.escapePath(
1069
                            table,
1070
                        )} DROP CONSTRAINT "${pkName}"`,
1071
                    ),
1072
                )
1073
                downQueries.push(
6✔
1074
                    new Query(
1075
                        `ALTER TABLE ${this.escapePath(
1076
                            table,
1077
                        )} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`,
1078
                    ),
1079
                )
1080
            }
1081

1082
            primaryColumns.push(column)
21✔
1083
            const pkName = primaryColumns[0].primaryKeyConstraintName
21!
1084
                ? primaryColumns[0].primaryKeyConstraintName
1085
                : this.connection.namingStrategy.primaryKeyName(
1086
                      clonedTable,
1087
                      primaryColumns.map((column) => column.name),
27✔
1088
                  )
1089

1090
            const columnNames = primaryColumns
21✔
1091
                .map((column) => `"${column.name}"`)
27✔
1092
                .join(", ")
1093
            upQueries.push(
21✔
1094
                new Query(
1095
                    `ALTER TABLE ${this.escapePath(
1096
                        table,
1097
                    )} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`,
1098
                ),
1099
            )
1100
            downQueries.push(
21✔
1101
                new Query(
1102
                    `ALTER TABLE ${this.escapePath(
1103
                        table,
1104
                    )} DROP CONSTRAINT "${pkName}"`,
1105
                ),
1106
            )
1107
        }
1108

1109
        // create column index
1110
        const columnIndex = clonedTable.indices.find(
111✔
1111
            (index) =>
1112
                index.columnNames.length === 1 &&
3✔
1113
                index.columnNames[0] === column.name,
1114
        )
1115
        if (columnIndex) {
111!
1116
            upQueries.push(this.createIndexSql(table, columnIndex))
×
1117
            downQueries.push(this.dropIndexSql(table, columnIndex))
×
1118
        }
1119

1120
        // create unique constraint
1121
        if (column.isUnique) {
111✔
1122
            const uniqueConstraint = new TableUnique({
9✔
1123
                name: this.connection.namingStrategy.uniqueConstraintName(
1124
                    table,
1125
                    [column.name],
1126
                ),
1127
                columnNames: [column.name],
1128
            })
1129
            clonedTable.uniques.push(uniqueConstraint)
9✔
1130
            upQueries.push(
9✔
1131
                new Query(
1132
                    `ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${
1133
                        uniqueConstraint.name
1134
                    }" UNIQUE ("${column.name}")`,
1135
                ),
1136
            )
1137
            downQueries.push(
9✔
1138
                new Query(
1139
                    `ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${
1140
                        uniqueConstraint.name
1141
                    }"`,
1142
                ),
1143
            )
1144
        }
1145

1146
        // remove default constraint
1147
        if (column.default !== null && column.default !== undefined) {
111✔
1148
            const defaultName =
1149
                this.connection.namingStrategy.defaultConstraintName(
15✔
1150
                    table,
1151
                    column.name,
1152
                )
1153
            downQueries.push(
15✔
1154
                new Query(
1155
                    `ALTER TABLE ${this.escapePath(
1156
                        table,
1157
                    )} DROP CONSTRAINT "${defaultName}"`,
1158
                ),
1159
            )
1160
        }
1161

1162
        if (column.generatedType && column.asExpression) {
111✔
1163
            const parsedTableName = this.driver.parseTableName(table)
9✔
1164

1165
            if (!parsedTableName.schema) {
9!
1166
                parsedTableName.schema = await this.getCurrentSchema()
×
1167
            }
1168

1169
            const insertQuery = this.insertTypeormMetadataSql({
9✔
1170
                database: parsedTableName.database,
1171
                schema: parsedTableName.schema,
1172
                table: parsedTableName.tableName,
1173
                type: MetadataTableType.GENERATED_COLUMN,
1174
                name: column.name,
1175
                value: column.asExpression,
1176
            })
1177

1178
            const deleteQuery = this.deleteTypeormMetadataSql({
9✔
1179
                database: parsedTableName.database,
1180
                schema: parsedTableName.schema,
1181
                table: parsedTableName.tableName,
1182
                type: MetadataTableType.GENERATED_COLUMN,
1183
                name: column.name,
1184
            })
1185

1186
            upQueries.push(insertQuery)
9✔
1187
            downQueries.push(deleteQuery)
9✔
1188
        }
1189

1190
        await this.executeQueries(upQueries, downQueries)
111✔
1191

1192
        clonedTable.addColumn(column)
105✔
1193
        this.replaceCachedTable(table, clonedTable)
105✔
1194
    }
1195

1196
    /**
1197
     * Creates a new columns from the column in the table.
1198
     */
1199
    async addColumns(
1200
        tableOrName: Table | string,
1201
        columns: TableColumn[],
1202
    ): Promise<void> {
1203
        for (const column of columns) {
18✔
1204
            await this.addColumn(tableOrName, column)
21✔
1205
        }
1206
    }
1207

1208
    /**
1209
     * Renames column in the given table.
1210
     */
1211
    async renameColumn(
1212
        tableOrName: Table | string,
1213
        oldTableColumnOrName: TableColumn | string,
1214
        newTableColumnOrName: TableColumn | string,
1215
    ): Promise<void> {
1216
        const table = InstanceChecker.isTable(tableOrName)
42✔
1217
            ? tableOrName
1218
            : await this.getCachedTable(tableOrName)
1219
        const oldColumn = InstanceChecker.isTableColumn(oldTableColumnOrName)
42✔
1220
            ? oldTableColumnOrName
1221
            : table.columns.find((c) => c.name === oldTableColumnOrName)
33✔
1222
        if (!oldColumn)
42!
1223
            throw new TypeORMError(
×
1224
                `Column "${oldTableColumnOrName}" was not found in the "${table.name}" table.`,
1225
            )
1226

1227
        let newColumn: TableColumn | undefined = undefined
42✔
1228
        if (InstanceChecker.isTableColumn(newTableColumnOrName)) {
42✔
1229
            newColumn = newTableColumnOrName
27✔
1230
        } else {
1231
            newColumn = oldColumn.clone()
15✔
1232
            newColumn.name = newTableColumnOrName
15✔
1233
        }
1234

1235
        await this.changeColumn(table, oldColumn, newColumn)
42✔
1236
    }
1237

1238
    /**
1239
     * Changes a column in the table.
1240
     */
1241
    async changeColumn(
1242
        tableOrName: Table | string,
1243
        oldTableColumnOrName: TableColumn | string,
1244
        newColumn: TableColumn,
1245
    ): Promise<void> {
1246
        const table = InstanceChecker.isTable(tableOrName)
153!
1247
            ? tableOrName
1248
            : await this.getCachedTable(tableOrName)
1249
        let clonedTable = table.clone()
153✔
1250
        const upQueries: Query[] = []
153✔
1251
        const downQueries: Query[] = []
153✔
1252

1253
        const oldColumn = InstanceChecker.isTableColumn(oldTableColumnOrName)
153!
1254
            ? oldTableColumnOrName
1255
            : table.columns.find(
1256
                  (column) => column.name === oldTableColumnOrName,
×
1257
              )
1258
        if (!oldColumn)
153!
1259
            throw new TypeORMError(
×
1260
                `Column "${oldTableColumnOrName}" was not found in the "${table.name}" table.`,
1261
            )
1262

1263
        if (
153✔
1264
            (newColumn.isGenerated !== oldColumn.isGenerated &&
597✔
1265
                newColumn.generationStrategy !== "uuid") ||
1266
            newColumn.type !== oldColumn.type ||
1267
            newColumn.length !== oldColumn.length ||
1268
            newColumn.asExpression !== oldColumn.asExpression ||
1269
            newColumn.generatedType !== oldColumn.generatedType
1270
        ) {
1271
            // SQL Server does not support changing of IDENTITY column, so we must drop column and recreate it again.
1272
            // Also, we recreate column if column type changed
1273
            await this.dropColumn(table, oldColumn)
75✔
1274
            await this.addColumn(table, newColumn)
75✔
1275

1276
            // update cloned table
1277
            clonedTable = table.clone()
75✔
1278
        } else {
1279
            if (newColumn.name !== oldColumn.name) {
78✔
1280
                // we need database name and schema name to rename FK constraints
1281
                let dbName: string | undefined = undefined
42✔
1282
                let schemaName: string | undefined = undefined
42✔
1283
                const splittedName = table.name.split(".")
42✔
1284
                if (splittedName.length === 3) {
42✔
1285
                    dbName = splittedName[0]
6✔
1286
                    if (splittedName[1] !== "") schemaName = splittedName[1]
6✔
1287
                } else if (splittedName.length === 2) {
36!
1288
                    schemaName = splittedName[0]
×
1289
                }
1290

1291
                // if we have tables with database which differs from database specified in config, we must change currently used database.
1292
                // This need because we can not rename objects from another database.
1293
                const currentDB = await this.getCurrentDatabase()
42✔
1294
                if (dbName && dbName !== currentDB) {
42✔
1295
                    upQueries.push(new Query(`USE "${dbName}"`))
6✔
1296
                    downQueries.push(new Query(`USE "${currentDB}"`))
6✔
1297
                }
1298

1299
                // rename the column
1300
                upQueries.push(
42✔
1301
                    new Query(
1302
                        `EXEC sp_rename "${this.getTablePath(table)}.${
1303
                            oldColumn.name
1304
                        }", "${newColumn.name}"`,
1305
                    ),
1306
                )
1307
                downQueries.push(
42✔
1308
                    new Query(
1309
                        `EXEC sp_rename "${this.getTablePath(table)}.${
1310
                            newColumn.name
1311
                        }", "${oldColumn.name}"`,
1312
                    ),
1313
                )
1314

1315
                // rename column primary key constraint
1316
                if (
42✔
1317
                    oldColumn.isPrimary === true &&
51✔
1318
                    !oldColumn.primaryKeyConstraintName
1319
                ) {
1320
                    const primaryColumns = clonedTable.primaryColumns
3✔
1321

1322
                    // build old primary constraint name
1323
                    const columnNames = primaryColumns.map(
3✔
1324
                        (column) => column.name,
3✔
1325
                    )
1326
                    const oldPkName =
1327
                        this.connection.namingStrategy.primaryKeyName(
3✔
1328
                            clonedTable,
1329
                            columnNames,
1330
                        )
1331

1332
                    // replace old column name with new column name
1333
                    columnNames.splice(columnNames.indexOf(oldColumn.name), 1)
3✔
1334
                    columnNames.push(newColumn.name)
3✔
1335

1336
                    // build new primary constraint name
1337
                    const newPkName =
1338
                        this.connection.namingStrategy.primaryKeyName(
3✔
1339
                            clonedTable,
1340
                            columnNames,
1341
                        )
1342

1343
                    // rename primary constraint
1344
                    upQueries.push(
3✔
1345
                        new Query(
1346
                            `EXEC sp_rename "${this.getTablePath(
1347
                                clonedTable,
1348
                            )}.${oldPkName}", "${newPkName}"`,
1349
                        ),
1350
                    )
1351
                    downQueries.push(
3✔
1352
                        new Query(
1353
                            `EXEC sp_rename "${this.getTablePath(
1354
                                clonedTable,
1355
                            )}.${newPkName}", "${oldPkName}"`,
1356
                        ),
1357
                    )
1358
                }
1359

1360
                // rename index constraints
1361
                clonedTable.findColumnIndices(oldColumn).forEach((index) => {
42✔
1362
                    const oldIndexName =
1363
                        this.connection.namingStrategy.indexName(
9✔
1364
                            clonedTable,
1365
                            index.columnNames,
1366
                            index.where,
1367
                        )
1368

1369
                    // Skip renaming if Index has user defined constraint name
1370
                    if (index.name !== oldIndexName) return
9✔
1371

1372
                    // build new constraint name
1373
                    index.columnNames.splice(
3✔
1374
                        index.columnNames.indexOf(oldColumn.name),
1375
                        1,
1376
                    )
1377
                    index.columnNames.push(newColumn.name)
3✔
1378
                    const newIndexName =
1379
                        this.connection.namingStrategy.indexName(
3✔
1380
                            clonedTable,
1381
                            index.columnNames,
1382
                            index.where,
1383
                        )
1384

1385
                    // build queries
1386
                    upQueries.push(
3✔
1387
                        new Query(
1388
                            `EXEC sp_rename "${this.getTablePath(
1389
                                clonedTable,
1390
                            )}.${index.name}", "${newIndexName}", "INDEX"`,
1391
                        ),
1392
                    )
1393
                    downQueries.push(
3✔
1394
                        new Query(
1395
                            `EXEC sp_rename "${this.getTablePath(
1396
                                clonedTable,
1397
                            )}.${newIndexName}", "${index.name}", "INDEX"`,
1398
                        ),
1399
                    )
1400

1401
                    // replace constraint name
1402
                    index.name = newIndexName
3✔
1403
                })
1404

1405
                // rename foreign key constraints
1406
                clonedTable
42✔
1407
                    .findColumnForeignKeys(oldColumn)
1408
                    .forEach((foreignKey) => {
1409
                        const foreignKeyName =
1410
                            this.connection.namingStrategy.foreignKeyName(
3✔
1411
                                clonedTable,
1412
                                foreignKey.columnNames,
1413
                                this.getTablePath(foreignKey),
1414
                                foreignKey.referencedColumnNames,
1415
                            )
1416

1417
                        // Skip renaming if foreign key has user defined constraint name
1418
                        if (foreignKey.name !== foreignKeyName) return
3!
1419

1420
                        // build new constraint name
1421
                        foreignKey.columnNames.splice(
3✔
1422
                            foreignKey.columnNames.indexOf(oldColumn.name),
1423
                            1,
1424
                        )
1425
                        foreignKey.columnNames.push(newColumn.name)
3✔
1426
                        const newForeignKeyName =
1427
                            this.connection.namingStrategy.foreignKeyName(
3✔
1428
                                clonedTable,
1429
                                foreignKey.columnNames,
1430
                                this.getTablePath(foreignKey),
1431
                                foreignKey.referencedColumnNames,
1432
                            )
1433

1434
                        // build queries
1435
                        upQueries.push(
3✔
1436
                            new Query(
1437
                                `EXEC sp_rename "${this.buildForeignKeyName(
1438
                                    foreignKey.name!,
1439
                                    schemaName,
1440
                                    dbName,
1441
                                )}", "${newForeignKeyName}"`,
1442
                            ),
1443
                        )
1444
                        downQueries.push(
3✔
1445
                            new Query(
1446
                                `EXEC sp_rename "${this.buildForeignKeyName(
1447
                                    newForeignKeyName,
1448
                                    schemaName,
1449
                                    dbName,
1450
                                )}", "${foreignKey.name}"`,
1451
                            ),
1452
                        )
1453

1454
                        // replace constraint name
1455
                        foreignKey.name = newForeignKeyName
3✔
1456
                    })
1457

1458
                // rename check constraints
1459
                clonedTable.findColumnChecks(oldColumn).forEach((check) => {
42✔
1460
                    // build new constraint name
1461
                    check.columnNames!.splice(
×
1462
                        check.columnNames!.indexOf(oldColumn.name),
1463
                        1,
1464
                    )
1465
                    check.columnNames!.push(newColumn.name)
×
1466
                    const newCheckName =
1467
                        this.connection.namingStrategy.checkConstraintName(
×
1468
                            clonedTable,
1469
                            check.expression!,
1470
                        )
1471

1472
                    // build queries
1473
                    upQueries.push(
×
1474
                        new Query(
1475
                            `EXEC sp_rename "${this.getTablePath(
1476
                                clonedTable,
1477
                            )}.${check.name}", "${newCheckName}"`,
1478
                        ),
1479
                    )
1480
                    downQueries.push(
×
1481
                        new Query(
1482
                            `EXEC sp_rename "${this.getTablePath(
1483
                                clonedTable,
1484
                            )}.${newCheckName}", "${check.name}"`,
1485
                        ),
1486
                    )
1487

1488
                    // replace constraint name
1489
                    check.name = newCheckName
×
1490
                })
1491

1492
                // rename unique constraints
1493
                clonedTable.findColumnUniques(oldColumn).forEach((unique) => {
42✔
1494
                    const oldUniqueName =
1495
                        this.connection.namingStrategy.uniqueConstraintName(
18✔
1496
                            clonedTable,
1497
                            unique.columnNames,
1498
                        )
1499

1500
                    // Skip renaming if Unique has user defined constraint name
1501
                    if (unique.name !== oldUniqueName) return
18✔
1502

1503
                    // build new constraint name
1504
                    unique.columnNames.splice(
12✔
1505
                        unique.columnNames.indexOf(oldColumn.name),
1506
                        1,
1507
                    )
1508
                    unique.columnNames.push(newColumn.name)
12✔
1509
                    const newUniqueName =
1510
                        this.connection.namingStrategy.uniqueConstraintName(
12✔
1511
                            clonedTable,
1512
                            unique.columnNames,
1513
                        )
1514

1515
                    // build queries
1516
                    upQueries.push(
12✔
1517
                        new Query(
1518
                            `EXEC sp_rename "${this.getTablePath(
1519
                                clonedTable,
1520
                            )}.${unique.name}", "${newUniqueName}"`,
1521
                        ),
1522
                    )
1523
                    downQueries.push(
12✔
1524
                        new Query(
1525
                            `EXEC sp_rename "${this.getTablePath(
1526
                                clonedTable,
1527
                            )}.${newUniqueName}", "${unique.name}"`,
1528
                        ),
1529
                    )
1530

1531
                    // replace constraint name
1532
                    unique.name = newUniqueName
12✔
1533
                })
1534

1535
                // rename default constraints
1536
                if (
42✔
1537
                    oldColumn.default !== null &&
84✔
1538
                    oldColumn.default !== undefined
1539
                ) {
1540
                    const oldDefaultName =
1541
                        this.connection.namingStrategy.defaultConstraintName(
6✔
1542
                            table,
1543
                            oldColumn.name,
1544
                        )
1545
                    const newDefaultName =
1546
                        this.connection.namingStrategy.defaultConstraintName(
6✔
1547
                            table,
1548
                            newColumn.name,
1549
                        )
1550

1551
                    upQueries.push(
6✔
1552
                        new Query(
1553
                            `ALTER TABLE ${this.escapePath(
1554
                                table,
1555
                            )} DROP CONSTRAINT "${oldDefaultName}"`,
1556
                        ),
1557
                    )
1558
                    downQueries.push(
6✔
1559
                        new Query(
1560
                            `ALTER TABLE ${this.escapePath(
1561
                                table,
1562
                            )} ADD CONSTRAINT "${oldDefaultName}" DEFAULT ${
1563
                                oldColumn.default
1564
                            } FOR "${newColumn.name}"`,
1565
                        ),
1566
                    )
1567

1568
                    upQueries.push(
6✔
1569
                        new Query(
1570
                            `ALTER TABLE ${this.escapePath(
1571
                                table,
1572
                            )} ADD CONSTRAINT "${newDefaultName}" DEFAULT ${
1573
                                oldColumn.default
1574
                            } FOR "${newColumn.name}"`,
1575
                        ),
1576
                    )
1577
                    downQueries.push(
6✔
1578
                        new Query(
1579
                            `ALTER TABLE ${this.escapePath(
1580
                                table,
1581
                            )} DROP CONSTRAINT "${newDefaultName}"`,
1582
                        ),
1583
                    )
1584
                }
1585

1586
                // change currently used database back to default db.
1587
                if (dbName && dbName !== currentDB) {
42✔
1588
                    upQueries.push(new Query(`USE "${currentDB}"`))
6✔
1589
                    downQueries.push(new Query(`USE "${dbName}"`))
6✔
1590
                }
1591

1592
                // rename old column in the Table object
1593
                const oldTableColumn = clonedTable.columns.find(
42✔
1594
                    (column) => column.name === oldColumn.name,
96✔
1595
                )
1596
                clonedTable.columns[
42✔
1597
                    clonedTable.columns.indexOf(oldTableColumn!)
1598
                ].name = newColumn.name
1599
                oldColumn.name = newColumn.name
42✔
1600
            }
1601

1602
            if (
78!
1603
                this.isColumnChanged(oldColumn, newColumn, false, false, false)
1604
            ) {
1605
                upQueries.push(
×
1606
                    new Query(
1607
                        `ALTER TABLE ${this.escapePath(
1608
                            table,
1609
                        )} ALTER COLUMN ${this.buildCreateColumnSql(
1610
                            table,
1611
                            newColumn,
1612
                            true,
1613
                            false,
1614
                            true,
1615
                        )}`,
1616
                    ),
1617
                )
1618
                downQueries.push(
×
1619
                    new Query(
1620
                        `ALTER TABLE ${this.escapePath(
1621
                            table,
1622
                        )} ALTER COLUMN ${this.buildCreateColumnSql(
1623
                            table,
1624
                            oldColumn,
1625
                            true,
1626
                            false,
1627
                            true,
1628
                        )}`,
1629
                    ),
1630
                )
1631
            }
1632

1633
            if (this.isEnumChanged(oldColumn, newColumn)) {
78✔
1634
                const oldExpression = this.getEnumExpression(oldColumn)
3✔
1635
                const oldCheck = new TableCheck({
3✔
1636
                    name: this.connection.namingStrategy.checkConstraintName(
1637
                        table,
1638
                        oldExpression,
1639
                        true,
1640
                    ),
1641
                    expression: oldExpression,
1642
                })
1643

1644
                const newExpression = this.getEnumExpression(newColumn)
3✔
1645
                const newCheck = new TableCheck({
3✔
1646
                    name: this.connection.namingStrategy.checkConstraintName(
1647
                        table,
1648
                        newExpression,
1649
                        true,
1650
                    ),
1651
                    expression: newExpression,
1652
                })
1653

1654
                upQueries.push(this.dropCheckConstraintSql(table, oldCheck))
3✔
1655
                upQueries.push(this.createCheckConstraintSql(table, newCheck))
3✔
1656

1657
                downQueries.push(this.dropCheckConstraintSql(table, newCheck))
3✔
1658
                downQueries.push(this.createCheckConstraintSql(table, oldCheck))
3✔
1659
            }
1660

1661
            if (newColumn.isPrimary !== oldColumn.isPrimary) {
78✔
1662
                const primaryColumns = clonedTable.primaryColumns
9✔
1663

1664
                // if primary column state changed, we must always drop existed constraint.
1665
                if (primaryColumns.length > 0) {
9✔
1666
                    const pkName = primaryColumns[0].primaryKeyConstraintName
9!
1667
                        ? primaryColumns[0].primaryKeyConstraintName
1668
                        : this.connection.namingStrategy.primaryKeyName(
1669
                              clonedTable,
1670
                              primaryColumns.map((column) => column.name),
12✔
1671
                          )
1672

1673
                    const columnNames = primaryColumns
9✔
1674
                        .map((column) => `"${column.name}"`)
12✔
1675
                        .join(", ")
1676
                    upQueries.push(
9✔
1677
                        new Query(
1678
                            `ALTER TABLE ${this.escapePath(
1679
                                table,
1680
                            )} DROP CONSTRAINT "${pkName}"`,
1681
                        ),
1682
                    )
1683
                    downQueries.push(
9✔
1684
                        new Query(
1685
                            `ALTER TABLE ${this.escapePath(
1686
                                table,
1687
                            )} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`,
1688
                        ),
1689
                    )
1690
                }
1691

1692
                if (newColumn.isPrimary === true) {
9✔
1693
                    primaryColumns.push(newColumn)
3✔
1694
                    // update column in table
1695
                    const column = clonedTable.columns.find(
3✔
1696
                        (column) => column.name === newColumn.name,
9✔
1697
                    )
1698
                    column!.isPrimary = true
3✔
1699
                    const pkName = primaryColumns[0].primaryKeyConstraintName
3!
1700
                        ? primaryColumns[0].primaryKeyConstraintName
1701
                        : this.connection.namingStrategy.primaryKeyName(
1702
                              clonedTable,
1703
                              primaryColumns.map((column) => column.name),
6✔
1704
                          )
1705

1706
                    const columnNames = primaryColumns
3✔
1707
                        .map((column) => `"${column.name}"`)
6✔
1708
                        .join(", ")
1709
                    upQueries.push(
3✔
1710
                        new Query(
1711
                            `ALTER TABLE ${this.escapePath(
1712
                                table,
1713
                            )} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`,
1714
                        ),
1715
                    )
1716
                    downQueries.push(
3✔
1717
                        new Query(
1718
                            `ALTER TABLE ${this.escapePath(
1719
                                table,
1720
                            )} DROP CONSTRAINT "${pkName}"`,
1721
                        ),
1722
                    )
1723
                } else {
1724
                    const primaryColumn = primaryColumns.find(
6✔
1725
                        (c) => c.name === newColumn.name,
6✔
1726
                    )
1727
                    primaryColumns.splice(
6✔
1728
                        primaryColumns.indexOf(primaryColumn!),
1729
                        1,
1730
                    )
1731

1732
                    // update column in table
1733
                    const column = clonedTable.columns.find(
6✔
1734
                        (column) => column.name === newColumn.name,
18✔
1735
                    )
1736
                    column!.isPrimary = false
6✔
1737

1738
                    // if we have another primary keys, we must recreate constraint.
1739
                    if (primaryColumns.length > 0) {
6✔
1740
                        const pkName = primaryColumns[0]
3!
1741
                            .primaryKeyConstraintName
1742
                            ? primaryColumns[0].primaryKeyConstraintName
1743
                            : this.connection.namingStrategy.primaryKeyName(
1744
                                  clonedTable,
1745
                                  primaryColumns.map((column) => column.name),
3✔
1746
                              )
1747

1748
                        const columnNames = primaryColumns
3✔
1749
                            .map((column) => `"${column.name}"`)
3✔
1750
                            .join(", ")
1751
                        upQueries.push(
3✔
1752
                            new Query(
1753
                                `ALTER TABLE ${this.escapePath(
1754
                                    table,
1755
                                )} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`,
1756
                            ),
1757
                        )
1758
                        downQueries.push(
3✔
1759
                            new Query(
1760
                                `ALTER TABLE ${this.escapePath(
1761
                                    table,
1762
                                )} DROP CONSTRAINT "${pkName}"`,
1763
                            ),
1764
                        )
1765
                    }
1766
                }
1767
            }
1768

1769
            if (newColumn.isUnique !== oldColumn.isUnique) {
78✔
1770
                if (newColumn.isUnique === true) {
3!
1771
                    const uniqueConstraint = new TableUnique({
3✔
1772
                        name: this.connection.namingStrategy.uniqueConstraintName(
1773
                            table,
1774
                            [newColumn.name],
1775
                        ),
1776
                        columnNames: [newColumn.name],
1777
                    })
1778
                    clonedTable.uniques.push(uniqueConstraint)
3✔
1779
                    upQueries.push(
3✔
1780
                        new Query(
1781
                            `ALTER TABLE ${this.escapePath(
1782
                                table,
1783
                            )} ADD CONSTRAINT "${
1784
                                uniqueConstraint.name
1785
                            }" UNIQUE ("${newColumn.name}")`,
1786
                        ),
1787
                    )
1788
                    downQueries.push(
3✔
1789
                        new Query(
1790
                            `ALTER TABLE ${this.escapePath(
1791
                                table,
1792
                            )} DROP CONSTRAINT "${uniqueConstraint.name}"`,
1793
                        ),
1794
                    )
1795
                } else {
1796
                    const uniqueConstraint = clonedTable.uniques.find(
×
1797
                        (unique) => {
1798
                            return (
×
1799
                                unique.columnNames.length === 1 &&
×
1800
                                !!unique.columnNames.find(
1801
                                    (columnName) =>
1802
                                        columnName === newColumn.name,
×
1803
                                )
1804
                            )
1805
                        },
1806
                    )
1807
                    clonedTable.uniques.splice(
×
1808
                        clonedTable.uniques.indexOf(uniqueConstraint!),
1809
                        1,
1810
                    )
1811
                    upQueries.push(
×
1812
                        new Query(
1813
                            `ALTER TABLE ${this.escapePath(
1814
                                table,
1815
                            )} DROP CONSTRAINT "${uniqueConstraint!.name}"`,
1816
                        ),
1817
                    )
1818
                    downQueries.push(
×
1819
                        new Query(
1820
                            `ALTER TABLE ${this.escapePath(
1821
                                table,
1822
                            )} ADD CONSTRAINT "${
1823
                                uniqueConstraint!.name
1824
                            }" UNIQUE ("${newColumn.name}")`,
1825
                        ),
1826
                    )
1827
                }
1828
            }
1829

1830
            if (newColumn.default !== oldColumn.default) {
78✔
1831
                // (note) if there is a previous default, we need to drop its constraint first
1832
                if (
6✔
1833
                    oldColumn.default !== null &&
12✔
1834
                    oldColumn.default !== undefined
1835
                ) {
1836
                    const defaultName =
1837
                        this.connection.namingStrategy.defaultConstraintName(
3✔
1838
                            table,
1839
                            oldColumn.name,
1840
                        )
1841
                    upQueries.push(
3✔
1842
                        new Query(
1843
                            `ALTER TABLE ${this.escapePath(
1844
                                table,
1845
                            )} DROP CONSTRAINT "${defaultName}"`,
1846
                        ),
1847
                    )
1848
                    downQueries.push(
3✔
1849
                        new Query(
1850
                            `ALTER TABLE ${this.escapePath(
1851
                                table,
1852
                            )} ADD CONSTRAINT "${defaultName}" DEFAULT ${
1853
                                oldColumn.default
1854
                            } FOR "${oldColumn.name}"`,
1855
                        ),
1856
                    )
1857
                }
1858

1859
                if (
6✔
1860
                    newColumn.default !== null &&
12✔
1861
                    newColumn.default !== undefined
1862
                ) {
1863
                    const defaultName =
1864
                        this.connection.namingStrategy.defaultConstraintName(
6✔
1865
                            table,
1866
                            newColumn.name,
1867
                        )
1868
                    upQueries.push(
6✔
1869
                        new Query(
1870
                            `ALTER TABLE ${this.escapePath(
1871
                                table,
1872
                            )} ADD CONSTRAINT "${defaultName}" DEFAULT ${
1873
                                newColumn.default
1874
                            } FOR "${newColumn.name}"`,
1875
                        ),
1876
                    )
1877
                    downQueries.push(
6✔
1878
                        new Query(
1879
                            `ALTER TABLE ${this.escapePath(
1880
                                table,
1881
                            )} DROP CONSTRAINT "${defaultName}"`,
1882
                        ),
1883
                    )
1884
                }
1885
            }
1886

1887
            await this.executeQueries(upQueries, downQueries)
78✔
1888
            this.replaceCachedTable(table, clonedTable)
78✔
1889
        }
1890
    }
1891

1892
    /**
1893
     * Changes a column in the table.
1894
     */
1895
    async changeColumns(
1896
        tableOrName: Table | string,
1897
        changedColumns: { newColumn: TableColumn; oldColumn: TableColumn }[],
1898
    ): Promise<void> {
1899
        for (const { oldColumn, newColumn } of changedColumns) {
66✔
1900
            await this.changeColumn(tableOrName, oldColumn, newColumn)
93✔
1901
        }
1902
    }
1903

1904
    /**
1905
     * Drops column in the table.
1906
     */
1907
    async dropColumn(
1908
        tableOrName: Table | string,
1909
        columnOrName: TableColumn | string,
1910
    ): Promise<void> {
1911
        const table = InstanceChecker.isTable(tableOrName)
117✔
1912
            ? tableOrName
1913
            : await this.getCachedTable(tableOrName)
1914
        const column = InstanceChecker.isTableColumn(columnOrName)
117✔
1915
            ? columnOrName
1916
            : table.findColumnByName(columnOrName)
1917
        if (!column)
117✔
1918
            throw new TypeORMError(
3✔
1919
                `Column "${columnOrName}" was not found in table "${table.name}"`,
1920
            )
1921

1922
        const clonedTable = table.clone()
114✔
1923
        const upQueries: Query[] = []
114✔
1924
        const downQueries: Query[] = []
114✔
1925

1926
        // drop primary key constraint
1927
        if (column.isPrimary) {
114✔
1928
            const pkName = column.primaryKeyConstraintName
21!
1929
                ? column.primaryKeyConstraintName
1930
                : this.connection.namingStrategy.primaryKeyName(
1931
                      clonedTable,
1932
                      clonedTable.primaryColumns.map((column) => column.name),
24✔
1933
                  )
1934

1935
            const columnNames = clonedTable.primaryColumns
21✔
1936
                .map((primaryColumn) => `"${primaryColumn.name}"`)
24✔
1937
                .join(", ")
1938

1939
            upQueries.push(
21✔
1940
                new Query(
1941
                    `ALTER TABLE ${this.escapePath(
1942
                        clonedTable,
1943
                    )} DROP CONSTRAINT "${pkName}"`,
1944
                ),
1945
            )
1946
            downQueries.push(
21✔
1947
                new Query(
1948
                    `ALTER TABLE ${this.escapePath(
1949
                        clonedTable,
1950
                    )} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`,
1951
                ),
1952
            )
1953

1954
            // update column in table
1955
            const tableColumn = clonedTable.findColumnByName(column.name)
21✔
1956
            tableColumn!.isPrimary = false
21✔
1957

1958
            // if primary key have multiple columns, we must recreate it without dropped column
1959
            if (clonedTable.primaryColumns.length > 0) {
21✔
1960
                const pkName = clonedTable.primaryColumns[0]
3!
1961
                    .primaryKeyConstraintName
1962
                    ? clonedTable.primaryColumns[0].primaryKeyConstraintName
1963
                    : this.connection.namingStrategy.primaryKeyName(
1964
                          clonedTable,
1965
                          clonedTable.primaryColumns.map(
1966
                              (column) => column.name,
3✔
1967
                          ),
1968
                      )
1969

1970
                const columnNames = clonedTable.primaryColumns
3✔
1971
                    .map((primaryColumn) => `"${primaryColumn.name}"`)
3✔
1972
                    .join(", ")
1973
                upQueries.push(
3✔
1974
                    new Query(
1975
                        `ALTER TABLE ${this.escapePath(
1976
                            clonedTable,
1977
                        )} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNames})`,
1978
                    ),
1979
                )
1980
                downQueries.push(
3✔
1981
                    new Query(
1982
                        `ALTER TABLE ${this.escapePath(
1983
                            clonedTable,
1984
                        )} DROP CONSTRAINT "${pkName}"`,
1985
                    ),
1986
                )
1987
            }
1988
        }
1989

1990
        // drop column index
1991
        const columnIndex = clonedTable.indices.find(
114✔
1992
            (index) =>
1993
                index.columnNames.length === 1 &&
3✔
1994
                index.columnNames[0] === column.name,
1995
        )
1996
        if (columnIndex) {
114!
1997
            clonedTable.indices.splice(
×
1998
                clonedTable.indices.indexOf(columnIndex),
1999
                1,
2000
            )
2001
            upQueries.push(this.dropIndexSql(table, columnIndex))
×
2002
            downQueries.push(this.createIndexSql(table, columnIndex))
×
2003
        }
2004

2005
        // drop column check
2006
        const columnCheck = clonedTable.checks.find(
114✔
2007
            (check) =>
2008
                !!check.columnNames &&
48✔
2009
                check.columnNames.length === 1 &&
2010
                check.columnNames[0] === column.name,
2011
        )
2012
        if (columnCheck) {
114✔
2013
            clonedTable.checks.splice(
6✔
2014
                clonedTable.checks.indexOf(columnCheck),
2015
                1,
2016
            )
2017
            upQueries.push(this.dropCheckConstraintSql(table, columnCheck))
6✔
2018
            downQueries.push(this.createCheckConstraintSql(table, columnCheck))
6✔
2019
        }
2020

2021
        // drop column unique
2022
        const columnUnique = clonedTable.uniques.find(
114✔
2023
            (unique) =>
2024
                unique.columnNames.length === 1 &&
90✔
2025
                unique.columnNames[0] === column.name,
2026
        )
2027
        if (columnUnique) {
114✔
2028
            clonedTable.uniques.splice(
9✔
2029
                clonedTable.uniques.indexOf(columnUnique),
2030
                1,
2031
            )
2032
            upQueries.push(this.dropUniqueConstraintSql(table, columnUnique))
9✔
2033
            downQueries.push(
9✔
2034
                this.createUniqueConstraintSql(table, columnUnique),
2035
            )
2036
        }
2037

2038
        // drop default constraint
2039
        if (column.default !== null && column.default !== undefined) {
114✔
2040
            const defaultName =
2041
                this.connection.namingStrategy.defaultConstraintName(
18✔
2042
                    table,
2043
                    column.name,
2044
                )
2045
            upQueries.push(
18✔
2046
                new Query(
2047
                    `ALTER TABLE ${this.escapePath(
2048
                        table,
2049
                    )} DROP CONSTRAINT "${defaultName}"`,
2050
                ),
2051
            )
2052
            downQueries.push(
18✔
2053
                new Query(
2054
                    `ALTER TABLE ${this.escapePath(
2055
                        table,
2056
                    )} ADD CONSTRAINT "${defaultName}" DEFAULT ${
2057
                        column.default
2058
                    } FOR "${column.name}"`,
2059
                ),
2060
            )
2061
        }
2062

2063
        if (column.generatedType && column.asExpression) {
114✔
2064
            const parsedTableName = this.driver.parseTableName(table)
12✔
2065

2066
            if (!parsedTableName.schema) {
12!
2067
                parsedTableName.schema = await this.getCurrentSchema()
×
2068
            }
2069

2070
            const deleteQuery = this.deleteTypeormMetadataSql({
12✔
2071
                database: parsedTableName.database,
2072
                schema: parsedTableName.schema,
2073
                table: parsedTableName.tableName,
2074
                type: MetadataTableType.GENERATED_COLUMN,
2075
                name: column.name,
2076
            })
2077
            const insertQuery = this.insertTypeormMetadataSql({
12✔
2078
                database: parsedTableName.database,
2079
                schema: parsedTableName.schema,
2080
                table: parsedTableName.tableName,
2081
                type: MetadataTableType.GENERATED_COLUMN,
2082
                name: column.name,
2083
                value: column.asExpression,
2084
            })
2085

2086
            upQueries.push(deleteQuery)
12✔
2087
            downQueries.push(insertQuery)
12✔
2088
        }
2089

2090
        upQueries.push(
114✔
2091
            new Query(
2092
                `ALTER TABLE ${this.escapePath(table)} DROP COLUMN "${
2093
                    column.name
2094
                }"`,
2095
            ),
2096
        )
2097
        downQueries.push(
114✔
2098
            new Query(
2099
                `ALTER TABLE ${this.escapePath(
2100
                    table,
2101
                )} ADD ${this.buildCreateColumnSql(
2102
                    table,
2103
                    column,
2104
                    false,
2105
                    false,
2106
                )}`,
2107
            ),
2108
        )
2109

2110
        await this.executeQueries(upQueries, downQueries)
114✔
2111

2112
        clonedTable.removeColumn(column)
114✔
2113
        this.replaceCachedTable(table, clonedTable)
114✔
2114
    }
2115

2116
    /**
2117
     * Drops the columns in the table.
2118
     */
2119
    async dropColumns(
2120
        tableOrName: Table | string,
2121
        columns: TableColumn[] | string[],
2122
    ): Promise<void> {
2123
        for (const column of columns) {
12✔
2124
            await this.dropColumn(tableOrName, column)
27✔
2125
        }
2126
    }
2127

2128
    /**
2129
     * Creates a new primary key.
2130
     */
2131
    async createPrimaryKey(
2132
        tableOrName: Table | string,
2133
        columnNames: string[],
2134
        constraintName?: string,
2135
    ): Promise<void> {
2136
        const table = InstanceChecker.isTable(tableOrName)
6!
2137
            ? tableOrName
2138
            : await this.getCachedTable(tableOrName)
2139
        const clonedTable = table.clone()
6✔
2140

2141
        const up = this.createPrimaryKeySql(table, columnNames, constraintName)
6✔
2142

2143
        // mark columns as primary, because dropPrimaryKeySql build constraint name from table primary column names.
2144
        clonedTable.columns.forEach((column) => {
6✔
2145
            if (columnNames.find((columnName) => columnName === column.name))
21✔
2146
                column.isPrimary = true
9✔
2147
        })
2148
        const down = this.dropPrimaryKeySql(clonedTable)
6✔
2149

2150
        await this.executeQueries(up, down)
6✔
2151
        this.replaceCachedTable(table, clonedTable)
6✔
2152
    }
2153

2154
    /**
2155
     * Updates composite primary keys.
2156
     */
2157
    async updatePrimaryKeys(
2158
        tableOrName: Table | string,
2159
        columns: TableColumn[],
2160
    ): Promise<void> {
2161
        const table = InstanceChecker.isTable(tableOrName)
9!
2162
            ? tableOrName
2163
            : await this.getCachedTable(tableOrName)
2164
        const clonedTable = table.clone()
9✔
2165
        const columnNames = columns.map((column) => column.name)
18✔
2166
        const upQueries: Query[] = []
9✔
2167
        const downQueries: Query[] = []
9✔
2168

2169
        // if table already have primary columns, we must drop them.
2170
        const primaryColumns = clonedTable.primaryColumns
9✔
2171
        if (primaryColumns.length > 0) {
9✔
2172
            const pkName = primaryColumns[0].primaryKeyConstraintName
9!
2173
                ? primaryColumns[0].primaryKeyConstraintName
2174
                : this.connection.namingStrategy.primaryKeyName(
2175
                      clonedTable,
2176
                      primaryColumns.map((column) => column.name),
9✔
2177
                  )
2178

2179
            const columnNamesString = primaryColumns
9✔
2180
                .map((column) => `"${column.name}"`)
9✔
2181
                .join(", ")
2182

2183
            upQueries.push(
9✔
2184
                new Query(
2185
                    `ALTER TABLE ${this.escapePath(
2186
                        table,
2187
                    )} DROP CONSTRAINT "${pkName}"`,
2188
                ),
2189
            )
2190
            downQueries.push(
9✔
2191
                new Query(
2192
                    `ALTER TABLE ${this.escapePath(
2193
                        table,
2194
                    )} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNamesString})`,
2195
                ),
2196
            )
2197
        }
2198

2199
        // update columns in table.
2200
        clonedTable.columns
9✔
2201
            .filter((column) => columnNames.indexOf(column.name) !== -1)
36✔
2202
            .forEach((column) => (column.isPrimary = true))
18✔
2203

2204
        const pkName = primaryColumns[0].primaryKeyConstraintName
9!
2205
            ? primaryColumns[0].primaryKeyConstraintName
2206
            : this.connection.namingStrategy.primaryKeyName(
2207
                  clonedTable,
2208
                  columnNames,
2209
              )
2210

2211
        const columnNamesString = columnNames
9✔
2212
            .map((columnName) => `"${columnName}"`)
18✔
2213
            .join(", ")
2214

2215
        upQueries.push(
9✔
2216
            new Query(
2217
                `ALTER TABLE ${this.escapePath(
2218
                    table,
2219
                )} ADD CONSTRAINT "${pkName}" PRIMARY KEY (${columnNamesString})`,
2220
            ),
2221
        )
2222
        downQueries.push(
9✔
2223
            new Query(
2224
                `ALTER TABLE ${this.escapePath(
2225
                    table,
2226
                )} DROP CONSTRAINT "${pkName}"`,
2227
            ),
2228
        )
2229

2230
        await this.executeQueries(upQueries, downQueries)
9✔
2231
        this.replaceCachedTable(table, clonedTable)
9✔
2232
    }
2233

2234
    /**
2235
     * Drops a primary key.
2236
     */
2237
    async dropPrimaryKey(
2238
        tableOrName: Table | string,
2239
        constraintName?: string,
2240
    ): Promise<void> {
2241
        const table = InstanceChecker.isTable(tableOrName)
9!
2242
            ? tableOrName
2243
            : await this.getCachedTable(tableOrName)
2244
        const up = this.dropPrimaryKeySql(table)
9✔
2245
        const down = this.createPrimaryKeySql(
9✔
2246
            table,
2247
            table.primaryColumns.map((column) => column.name),
9✔
2248
            constraintName,
2249
        )
2250
        await this.executeQueries(up, down)
9✔
2251
        table.primaryColumns.forEach((column) => {
9✔
2252
            column.isPrimary = false
9✔
2253
        })
2254
    }
2255

2256
    /**
2257
     * Creates a new unique constraint.
2258
     */
2259
    async createUniqueConstraint(
2260
        tableOrName: Table | string,
2261
        uniqueConstraint: TableUnique,
2262
    ): Promise<void> {
2263
        const table = InstanceChecker.isTable(tableOrName)
12✔
2264
            ? tableOrName
2265
            : await this.getCachedTable(tableOrName)
2266

2267
        // new unique constraint may be passed without name. In this case we generate unique name manually.
2268
        if (!uniqueConstraint.name)
12✔
2269
            uniqueConstraint.name =
6✔
2270
                this.connection.namingStrategy.uniqueConstraintName(
2271
                    table,
2272
                    uniqueConstraint.columnNames,
2273
                )
2274

2275
        const up = this.createUniqueConstraintSql(table, uniqueConstraint)
12✔
2276
        const down = this.dropUniqueConstraintSql(table, uniqueConstraint)
12✔
2277
        await this.executeQueries(up, down)
12✔
2278
        table.addUniqueConstraint(uniqueConstraint)
12✔
2279
    }
2280

2281
    /**
2282
     * Creates a new unique constraints.
2283
     */
2284
    async createUniqueConstraints(
2285
        tableOrName: Table | string,
2286
        uniqueConstraints: TableUnique[],
2287
    ): Promise<void> {
2288
        const promises = uniqueConstraints.map((uniqueConstraint) =>
6✔
2289
            this.createUniqueConstraint(tableOrName, uniqueConstraint),
6✔
2290
        )
2291
        await Promise.all(promises)
6✔
2292
    }
2293

2294
    /**
2295
     * Drops unique constraint.
2296
     */
2297
    async dropUniqueConstraint(
2298
        tableOrName: Table | string,
2299
        uniqueOrName: TableUnique | string,
2300
    ): Promise<void> {
2301
        const table = InstanceChecker.isTable(tableOrName)
12!
2302
            ? tableOrName
2303
            : await this.getCachedTable(tableOrName)
2304
        const uniqueConstraint = InstanceChecker.isTableUnique(uniqueOrName)
12!
2305
            ? uniqueOrName
2306
            : table.uniques.find((u) => u.name === uniqueOrName)
×
2307
        if (!uniqueConstraint)
12!
2308
            throw new TypeORMError(
×
2309
                `Supplied unique constraint was not found in table ${table.name}`,
2310
            )
2311

2312
        const up = this.dropUniqueConstraintSql(table, uniqueConstraint)
12✔
2313
        const down = this.createUniqueConstraintSql(table, uniqueConstraint)
12✔
2314
        await this.executeQueries(up, down)
12✔
2315
        table.removeUniqueConstraint(uniqueConstraint)
12✔
2316
    }
2317

2318
    /**
2319
     * Drops an unique constraints.
2320
     */
2321
    async dropUniqueConstraints(
2322
        tableOrName: Table | string,
2323
        uniqueConstraints: TableUnique[],
2324
    ): Promise<void> {
2325
        const promises = uniqueConstraints.map((uniqueConstraint) =>
9✔
2326
            this.dropUniqueConstraint(tableOrName, uniqueConstraint),
9✔
2327
        )
2328
        await Promise.all(promises)
9✔
2329
    }
2330

2331
    /**
2332
     * Creates a new check constraint.
2333
     */
2334
    async createCheckConstraint(
2335
        tableOrName: Table | string,
2336
        checkConstraint: TableCheck,
2337
    ): Promise<void> {
2338
        const table = InstanceChecker.isTable(tableOrName)
15✔
2339
            ? tableOrName
2340
            : await this.getCachedTable(tableOrName)
2341

2342
        // new unique constraint may be passed without name. In this case we generate unique name manually.
2343
        if (!checkConstraint.name)
15✔
2344
            checkConstraint.name =
9✔
2345
                this.connection.namingStrategy.checkConstraintName(
2346
                    table,
2347
                    checkConstraint.expression!,
2348
                )
2349

2350
        const up = this.createCheckConstraintSql(table, checkConstraint)
15✔
2351
        const down = this.dropCheckConstraintSql(table, checkConstraint)
15✔
2352
        await this.executeQueries(up, down)
15✔
2353
        table.addCheckConstraint(checkConstraint)
15✔
2354
    }
2355

2356
    /**
2357
     * Creates a new check constraints.
2358
     */
2359
    async createCheckConstraints(
2360
        tableOrName: Table | string,
2361
        checkConstraints: TableCheck[],
2362
    ): Promise<void> {
2363
        const promises = checkConstraints.map((checkConstraint) =>
6✔
2364
            this.createCheckConstraint(tableOrName, checkConstraint),
6✔
2365
        )
2366
        await Promise.all(promises)
6✔
2367
    }
2368

2369
    /**
2370
     * Drops check constraint.
2371
     */
2372
    async dropCheckConstraint(
2373
        tableOrName: Table | string,
2374
        checkOrName: TableCheck | string,
2375
    ): Promise<void> {
2376
        const table = InstanceChecker.isTable(tableOrName)
9!
2377
            ? tableOrName
2378
            : await this.getCachedTable(tableOrName)
2379
        const checkConstraint = InstanceChecker.isTableCheck(checkOrName)
9!
2380
            ? checkOrName
2381
            : table.checks.find((c) => c.name === checkOrName)
×
2382
        if (!checkConstraint)
9!
2383
            throw new TypeORMError(
×
2384
                `Supplied check constraint was not found in table ${table.name}`,
2385
            )
2386

2387
        const up = this.dropCheckConstraintSql(table, checkConstraint)
9✔
2388
        const down = this.createCheckConstraintSql(table, checkConstraint)
9✔
2389
        await this.executeQueries(up, down)
9✔
2390
        table.removeCheckConstraint(checkConstraint)
9✔
2391
    }
2392

2393
    /**
2394
     * Drops check constraints.
2395
     */
2396
    async dropCheckConstraints(
2397
        tableOrName: Table | string,
2398
        checkConstraints: TableCheck[],
2399
    ): Promise<void> {
2400
        const promises = checkConstraints.map((checkConstraint) =>
6✔
2401
            this.dropCheckConstraint(tableOrName, checkConstraint),
6✔
2402
        )
2403
        await Promise.all(promises)
6✔
2404
    }
2405

2406
    /**
2407
     * Creates a new exclusion constraint.
2408
     */
2409
    async createExclusionConstraint(
2410
        tableOrName: Table | string,
2411
        exclusionConstraint: TableExclusion,
2412
    ): Promise<void> {
2413
        throw new TypeORMError(
×
2414
            `SqlServer does not support exclusion constraints.`,
2415
        )
2416
    }
2417

2418
    /**
2419
     * Creates a new exclusion constraints.
2420
     */
2421
    async createExclusionConstraints(
2422
        tableOrName: Table | string,
2423
        exclusionConstraints: TableExclusion[],
2424
    ): Promise<void> {
2425
        throw new TypeORMError(
×
2426
            `SqlServer does not support exclusion constraints.`,
2427
        )
2428
    }
2429

2430
    /**
2431
     * Drops exclusion constraint.
2432
     */
2433
    async dropExclusionConstraint(
2434
        tableOrName: Table | string,
2435
        exclusionOrName: TableExclusion | string,
2436
    ): Promise<void> {
2437
        throw new TypeORMError(
×
2438
            `SqlServer does not support exclusion constraints.`,
2439
        )
2440
    }
2441

2442
    /**
2443
     * Drops exclusion constraints.
2444
     */
2445
    async dropExclusionConstraints(
2446
        tableOrName: Table | string,
2447
        exclusionConstraints: TableExclusion[],
2448
    ): Promise<void> {
2449
        throw new TypeORMError(
×
2450
            `SqlServer does not support exclusion constraints.`,
2451
        )
2452
    }
2453

2454
    /**
2455
     * Creates a new foreign key.
2456
     */
2457
    async createForeignKey(
2458
        tableOrName: Table | string,
2459
        foreignKey: TableForeignKey,
2460
    ): Promise<void> {
2461
        const table = InstanceChecker.isTable(tableOrName)
9,405✔
2462
            ? tableOrName
2463
            : await this.getCachedTable(tableOrName)
2464
        const metadata = this.connection.hasMetadata(table.name)
9,405✔
2465
            ? this.connection.getMetadata(table.name)
2466
            : undefined
2467

2468
        if (
9,405!
2469
            metadata &&
21,147✔
2470
            metadata.treeParentRelation &&
2471
            metadata.treeParentRelation!.isTreeParent &&
2472
            metadata.foreignKeys.find(
2473
                (foreignKey) => foreignKey.onDelete !== "NO ACTION",
1,392✔
2474
            )
2475
        )
2476
            throw new TypeORMError(
×
2477
                "SqlServer does not support options in TreeParent.",
2478
            )
2479

2480
        // new FK may be passed without name. In this case we generate FK name manually.
2481
        if (!foreignKey.name)
9,405✔
2482
            foreignKey.name = this.connection.namingStrategy.foreignKeyName(
3✔
2483
                table,
2484
                foreignKey.columnNames,
2485
                this.getTablePath(foreignKey),
2486
                foreignKey.referencedColumnNames,
2487
            )
2488

2489
        const up = this.createForeignKeySql(table, foreignKey)
9,405✔
2490
        const down = this.dropForeignKeySql(table, foreignKey)
9,405✔
2491
        await this.executeQueries(up, down)
9,405✔
2492
        table.addForeignKey(foreignKey)
9,405✔
2493
    }
2494

2495
    /**
2496
     * Creates a new foreign keys.
2497
     */
2498
    async createForeignKeys(
2499
        tableOrName: Table | string,
2500
        foreignKeys: TableForeignKey[],
2501
    ): Promise<void> {
2502
        const promises = foreignKeys.map((foreignKey) =>
5,715✔
2503
            this.createForeignKey(tableOrName, foreignKey),
9,402✔
2504
        )
2505
        await Promise.all(promises)
5,715✔
2506
    }
2507

2508
    /**
2509
     * Drops a foreign key from the table.
2510
     */
2511
    async dropForeignKey(
2512
        tableOrName: Table | string,
2513
        foreignKeyOrName: TableForeignKey | string,
2514
    ): Promise<void> {
2515
        const table = InstanceChecker.isTable(tableOrName)
21!
2516
            ? tableOrName
2517
            : await this.getCachedTable(tableOrName)
2518
        const foreignKey = InstanceChecker.isTableForeignKey(foreignKeyOrName)
21!
2519
            ? foreignKeyOrName
2520
            : table.foreignKeys.find((fk) => fk.name === foreignKeyOrName)
×
2521
        if (!foreignKey)
21!
2522
            throw new TypeORMError(
×
2523
                `Supplied foreign key was not found in table ${table.name}`,
2524
            )
2525

2526
        const up = this.dropForeignKeySql(table, foreignKey)
21✔
2527
        const down = this.createForeignKeySql(table, foreignKey)
21✔
2528
        await this.executeQueries(up, down)
21✔
2529
        table.removeForeignKey(foreignKey)
21✔
2530
    }
2531

2532
    /**
2533
     * Drops a foreign keys from the table.
2534
     */
2535
    async dropForeignKeys(
2536
        tableOrName: Table | string,
2537
        foreignKeys: TableForeignKey[],
2538
    ): Promise<void> {
2539
        const promises = foreignKeys.map((foreignKey) =>
18✔
2540
            this.dropForeignKey(tableOrName, foreignKey),
18✔
2541
        )
2542
        await Promise.all(promises)
18✔
2543
    }
2544

2545
    /**
2546
     * Creates a new index.
2547
     */
2548
    async createIndex(
2549
        tableOrName: Table | string,
2550
        index: TableIndex,
2551
    ): Promise<void> {
2552
        const table = InstanceChecker.isTable(tableOrName)
30✔
2553
            ? tableOrName
2554
            : await this.getCachedTable(tableOrName)
2555

2556
        // new index may be passed without name. In this case we generate index name manually.
2557
        if (!index.name) index.name = this.generateIndexName(table, index)
30✔
2558

2559
        const up = this.createIndexSql(table, index)
30✔
2560
        const down = this.dropIndexSql(table, index)
30✔
2561
        await this.executeQueries(up, down)
30✔
2562
        table.addIndex(index)
30✔
2563
    }
2564

2565
    /**
2566
     * Creates a new indices
2567
     */
2568
    async createIndices(
2569
        tableOrName: Table | string,
2570
        indices: TableIndex[],
2571
    ): Promise<void> {
2572
        const promises = indices.map((index) =>
18✔
2573
            this.createIndex(tableOrName, index),
18✔
2574
        )
2575
        await Promise.all(promises)
18✔
2576
    }
2577

2578
    /**
2579
     * Drops an index.
2580
     */
2581
    async dropIndex(
2582
        tableOrName: Table | string,
2583
        indexOrName: TableIndex | string,
2584
    ): Promise<void> {
2585
        const table = InstanceChecker.isTable(tableOrName)
36✔
2586
            ? tableOrName
2587
            : await this.getCachedTable(tableOrName)
2588
        const index = InstanceChecker.isTableIndex(indexOrName)
36!
2589
            ? indexOrName
2590
            : table.indices.find((i) => i.name === indexOrName)
×
2591
        if (!index)
36!
2592
            throw new TypeORMError(
×
2593
                `Supplied index was not found in table ${table.name}`,
2594
            )
2595

2596
        // old index may be passed without name. In this case we generate index name manually.
2597
        if (!index.name) index.name = this.generateIndexName(table, index)
36✔
2598

2599
        const up = this.dropIndexSql(table, index)
36✔
2600
        const down = this.createIndexSql(table, index)
36✔
2601
        await this.executeQueries(up, down)
36✔
2602
        table.removeIndex(index)
36✔
2603
    }
2604

2605
    /**
2606
     * Drops an indices from the table.
2607
     */
2608
    async dropIndices(
2609
        tableOrName: Table | string,
2610
        indices: TableIndex[],
2611
    ): Promise<void> {
2612
        const promises = indices.map((index) =>
3✔
2613
            this.dropIndex(tableOrName, index),
6✔
2614
        )
2615
        await Promise.all(promises)
3✔
2616
    }
2617

2618
    /**
2619
     * Clears all table contents.
2620
     * Note: this operation uses SQL's TRUNCATE query which cannot be reverted in transactions.
2621
     */
2622
    async clearTable(tablePath: string): Promise<void> {
2623
        await this.query(`TRUNCATE TABLE ${this.escapePath(tablePath)}`)
6✔
2624
    }
2625

2626
    /**
2627
     * Removes all tables from the currently connected database.
2628
     */
2629
    async clearDatabase(database?: string): Promise<void> {
2630
        if (database) {
4,053✔
2631
            const isDatabaseExist = await this.hasDatabase(database)
4,053✔
2632
            if (!isDatabaseExist) return Promise.resolve()
4,053!
2633
        }
2634

2635
        const isAnotherTransactionActive = this.isTransactionActive
4,053✔
2636
        if (!isAnotherTransactionActive) await this.startTransaction()
4,053✔
2637
        try {
4,053✔
2638
            let allViewsSql = database
4,053!
2639
                ? `SELECT * FROM "${database}"."INFORMATION_SCHEMA"."VIEWS"`
2640
                : `SELECT * FROM "INFORMATION_SCHEMA"."VIEWS"`
2641
            const allViewsResults: ObjectLiteral[] = await this.query(
4,053✔
2642
                allViewsSql,
2643
            )
2644

2645
            await Promise.all(
4,053✔
2646
                allViewsResults.map((viewResult) => {
2647
                    // 'DROP VIEW' does not allow specifying the database name as a prefix to the object name.
2648
                    const dropTableSql = `DROP VIEW "${viewResult["TABLE_SCHEMA"]}"."${viewResult["TABLE_NAME"]}"`
24✔
2649
                    return this.query(dropTableSql)
24✔
2650
                }),
2651
            )
2652

2653
            let allTablesSql = database
4,053!
2654
                ? `SELECT * FROM "${database}"."INFORMATION_SCHEMA"."TABLES" WHERE "TABLE_TYPE" = 'BASE TABLE'`
2655
                : `SELECT * FROM "INFORMATION_SCHEMA"."TABLES" WHERE "TABLE_TYPE" = 'BASE TABLE'`
2656
            const allTablesResults: ObjectLiteral[] = await this.query(
4,053✔
2657
                allTablesSql,
2658
            )
2659

2660
            if (allTablesResults.length > 0) {
4,053✔
2661
                const tablesByCatalog: {
2662
                    [key: string]: {
2663
                        TABLE_NAME: string
2664
                        TABLE_SCHEMA: string
2665
                    }[]
2666
                } = allTablesResults.reduce(
3,999✔
2667
                    (c, { TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME }) => {
2668
                        c[TABLE_CATALOG] = c[TABLE_CATALOG] || []
14,067✔
2669
                        c[TABLE_CATALOG].push({ TABLE_SCHEMA, TABLE_NAME })
14,067✔
2670
                        return c
14,067✔
2671
                    },
2672
                    {},
2673
                )
2674

2675
                const foreignKeysSql = Object.entries(tablesByCatalog)
3,999✔
2676
                    .map(([TABLE_CATALOG, tables]) => {
2677
                        const conditions = tables
3,999✔
2678
                            .map(({ TABLE_SCHEMA, TABLE_NAME }) => {
2679
                                return `("fk"."referenced_object_id" = OBJECT_ID('"${TABLE_CATALOG}"."${TABLE_SCHEMA}"."${TABLE_NAME}"'))`
14,067✔
2680
                            })
2681
                            .join(" OR ")
2682

2683
                        return `
3,999✔
2684
                        SELECT DISTINCT '${TABLE_CATALOG}' AS                                              "TABLE_CATALOG",
2685
                                        OBJECT_SCHEMA_NAME("fk"."parent_object_id",
2686
                                                           DB_ID('${TABLE_CATALOG}')) AS                   "TABLE_SCHEMA",
2687
                                        OBJECT_NAME("fk"."parent_object_id", DB_ID('${TABLE_CATALOG}')) AS "TABLE_NAME",
2688
                                        "fk"."name" AS                                                     "CONSTRAINT_NAME"
2689
                        FROM "${TABLE_CATALOG}"."sys"."foreign_keys" AS "fk"
2690
                        WHERE (${conditions})
2691
                    `
2692
                    })
2693
                    .join(" UNION ALL ")
2694

2695
                const foreignKeys: {
2696
                    TABLE_CATALOG: string
2697
                    TABLE_SCHEMA: string
2698
                    TABLE_NAME: string
2699
                    CONSTRAINT_NAME: string
2700
                }[] = await this.query(foreignKeysSql)
3,999✔
2701

2702
                await Promise.all(
3,999✔
2703
                    foreignKeys.map(
2704
                        async ({
2705
                            TABLE_CATALOG,
2706
                            TABLE_SCHEMA,
2707
                            TABLE_NAME,
2708
                            CONSTRAINT_NAME,
2709
                        }) => {
2710
                            // Disable the constraint first.
2711
                            await this.query(
9,372✔
2712
                                `ALTER TABLE "${TABLE_CATALOG}"."${TABLE_SCHEMA}"."${TABLE_NAME}" ` +
2713
                                    `NOCHECK CONSTRAINT "${CONSTRAINT_NAME}"`,
2714
                            )
2715

2716
                            await this.query(
9,372✔
2717
                                `ALTER TABLE "${TABLE_CATALOG}"."${TABLE_SCHEMA}"."${TABLE_NAME}" ` +
2718
                                    `DROP CONSTRAINT "${CONSTRAINT_NAME}" -- FROM CLEAR`,
2719
                            )
2720
                        },
2721
                    ),
2722
                )
2723

2724
                await Promise.all(
3,999✔
2725
                    allTablesResults.map((tablesResult) => {
2726
                        if (tablesResult["TABLE_NAME"].startsWith("#")) {
14,067!
2727
                            // don't try to drop temporary tables
2728
                            return
×
2729
                        }
2730

2731
                        const dropTableSql = `DROP TABLE "${tablesResult["TABLE_CATALOG"]}"."${tablesResult["TABLE_SCHEMA"]}"."${tablesResult["TABLE_NAME"]}"`
14,067✔
2732
                        return this.query(dropTableSql)
14,067✔
2733
                    }),
2734
                )
2735
            }
2736

2737
            if (!isAnotherTransactionActive) await this.commitTransaction()
4,053✔
2738
        } catch (error) {
2739
            try {
×
2740
                // we throw original error even if rollback thrown an error
2741
                if (!isAnotherTransactionActive)
×
2742
                    await this.rollbackTransaction()
×
2743
            } catch (rollbackError) {}
2744
            throw error
×
2745
        }
2746
    }
2747

2748
    // -------------------------------------------------------------------------
2749
    // Protected Methods
2750
    // -------------------------------------------------------------------------
2751

2752
    protected async loadViews(viewPaths?: string[]): Promise<View[]> {
2753
        const hasTable = await this.hasTable(this.getTypeormMetadataTableName())
4,218✔
2754
        if (!hasTable) {
4,218✔
2755
            return []
4,173✔
2756
        }
2757

2758
        if (!viewPaths) {
45!
2759
            viewPaths = []
×
2760
        }
2761

2762
        const currentSchema = await this.getCurrentSchema()
45✔
2763
        const currentDatabase = await this.getCurrentDatabase()
45✔
2764

2765
        const dbNames = viewPaths
45✔
2766
            .map((viewPath) => this.driver.parseTableName(viewPath).database)
33✔
2767
            .filter((database) => database)
33✔
2768

2769
        if (
45✔
2770
            this.driver.database &&
90✔
2771
            !dbNames.find((dbName) => dbName === this.driver.database)
21✔
2772
        )
2773
            dbNames.push(this.driver.database)
24✔
2774

2775
        const viewsCondition = viewPaths
45✔
2776
            .map((viewPath) => {
2777
                let { schema, tableName: name } =
2778
                    this.driver.parseTableName(viewPath)
33✔
2779

2780
                if (!schema) {
33!
2781
                    schema = currentSchema
×
2782
                }
2783
                return `("T"."SCHEMA" = '${schema}' AND "T"."NAME" = '${name}')`
33✔
2784
            })
2785
            .join(" OR ")
2786

2787
        const query = dbNames
45✔
2788
            .map((dbName) => {
2789
                return (
57✔
2790
                    `SELECT "T".*, "V"."CHECK_OPTION" FROM ${this.escapePath(
2791
                        this.getTypeormMetadataTableName(),
2792
                    )} "t" ` +
2793
                    `INNER JOIN "${dbName}"."INFORMATION_SCHEMA"."VIEWS" "V" ON "V"."TABLE_SCHEMA" = "T"."SCHEMA" AND "v"."TABLE_NAME" = "T"."NAME" WHERE "T"."TYPE" = '${
2794
                        MetadataTableType.VIEW
2795
                    }' ${viewsCondition ? `AND (${viewsCondition})` : ""}`
57✔
2796
                )
2797
            })
2798
            .join(" UNION ALL ")
2799

2800
        const dbViews = await this.query(query)
45✔
2801
        return dbViews.map((dbView: any) => {
45✔
2802
            const view = new View()
9✔
2803
            const db =
2804
                dbView["TABLE_CATALOG"] === currentDatabase
9!
2805
                    ? undefined
2806
                    : dbView["TABLE_CATALOG"]
2807
            const schema =
2808
                dbView["schema"] === currentSchema &&
9!
2809
                !this.driver.options.schema
2810
                    ? undefined
2811
                    : dbView["schema"]
2812
            view.database = dbView["TABLE_CATALOG"]
9✔
2813
            view.schema = dbView["schema"]
9✔
2814
            view.name = this.driver.buildTableName(dbView["name"], schema, db)
9✔
2815
            view.expression = dbView["value"]
9✔
2816
            return view
9✔
2817
        })
2818
    }
2819

2820
    /**
2821
     * Loads all tables (with given names) from the database and creates a Table from them.
2822
     */
2823
    protected async loadTables(tableNames?: string[]): Promise<Table[]> {
2824
        // if no tables given then no need to proceed
2825
        if (tableNames && tableNames.length === 0) {
4,998✔
2826
            return []
21✔
2827
        }
2828

2829
        const currentSchema = await this.getCurrentSchema()
4,977✔
2830
        const currentDatabase = await this.getCurrentDatabase()
4,977✔
2831

2832
        const dbTables: {
2833
            TABLE_CATALOG: string
2834
            TABLE_SCHEMA: string
2835
            TABLE_NAME: string
2836
        }[] = []
4,977✔
2837

2838
        if (!tableNames) {
4,977!
2839
            const databasesSql =
2840
                `SELECT DISTINCT "name" ` +
×
2841
                `FROM "master"."dbo"."sysdatabases" ` +
2842
                `WHERE "name" NOT IN ('master', 'model', 'msdb')`
2843
            const dbDatabases: { name: string }[] = await this.query(
×
2844
                databasesSql,
2845
            )
2846

2847
            const tablesSql = dbDatabases
×
2848
                .map(({ name }) => {
2849
                    return `
×
2850
                    SELECT DISTINCT
2851
                        "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME"
2852
                    FROM "${name}"."INFORMATION_SCHEMA"."TABLES"
2853
                    WHERE
2854
                      "TABLE_TYPE" = 'BASE TABLE'
2855
                      AND
2856
                      "TABLE_CATALOG" = '${name}'
2857
                      AND
2858
                      ISNULL(Objectproperty(Object_id("TABLE_CATALOG" + '.' + "TABLE_SCHEMA" + '.' + "TABLE_NAME"), 'IsMSShipped'), 0) = 0
2859
                `
2860
                })
2861
                .join(" UNION ALL ")
2862

2863
            dbTables.push(...(await this.query(tablesSql)))
×
2864
        } else {
2865
            const tableNamesByCatalog = tableNames
4,977✔
2866
                .map((tableName) => this.driver.parseTableName(tableName))
15,450✔
2867
                .reduce((c, { database, ...other }) => {
2868
                    database = database || currentDatabase
15,450!
2869
                    c[database] = c[database] || []
15,450✔
2870
                    c[database].push({
15,450✔
2871
                        schema: other.schema || currentSchema,
15,450!
2872
                        tableName: other.tableName,
2873
                    })
2874
                    return c
15,450✔
2875
                }, {} as { [key: string]: { schema: string; tableName: string }[] })
2876

2877
            const tablesSql = Object.entries(tableNamesByCatalog)
4,977✔
2878
                .map(([database, tables]) => {
2879
                    const tablesCondition = tables
4,989✔
2880
                        .map(({ schema, tableName }) => {
2881
                            return `("TABLE_SCHEMA" = '${schema}' AND "TABLE_NAME" = '${tableName}')`
15,450✔
2882
                        })
2883
                        .join(" OR ")
2884

2885
                    return `
4,989✔
2886
                    SELECT DISTINCT
2887
                        "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME"
2888
                    FROM "${database}"."INFORMATION_SCHEMA"."TABLES"
2889
                    WHERE
2890
                          "TABLE_TYPE" = 'BASE TABLE' AND
2891
                          "TABLE_CATALOG" = '${database}' AND
2892
                          ${tablesCondition}
2893
                `
2894
                })
2895
                .join(" UNION ALL ")
2896

2897
            dbTables.push(...(await this.query(tablesSql)))
4,977✔
2898
        }
2899

2900
        // if tables were not found in the db, no need to proceed
2901
        if (dbTables.length === 0) {
4,977✔
2902
            return []
4,062✔
2903
        }
2904

2905
        const dbTablesByCatalog = dbTables.reduce(
915✔
2906
            (c, { TABLE_CATALOG, ...other }) => {
2907
                c[TABLE_CATALOG] = c[TABLE_CATALOG] || []
1,392✔
2908
                c[TABLE_CATALOG].push(other)
1,392✔
2909
                return c
1,392✔
2910
            },
2911
            {} as {
2912
                [key: string]: { TABLE_NAME: string; TABLE_SCHEMA: string }[]
2913
            },
2914
        )
2915

2916
        const columnsSql = Object.entries(dbTablesByCatalog)
915✔
2917
            .map(([TABLE_CATALOG, tables]) => {
2918
                const condition = tables
915✔
2919
                    .map(
2920
                        ({ TABLE_SCHEMA, TABLE_NAME }) =>
2921
                            `("TABLE_SCHEMA" = '${TABLE_SCHEMA}' AND "TABLE_NAME" = '${TABLE_NAME}')`,
1,392✔
2922
                    )
2923
                    .join("OR")
2924

2925
                return (
915✔
2926
                    `SELECT "COLUMNS".*, "cc"."is_persisted", "cc"."definition" ` +
2927
                    `FROM "${TABLE_CATALOG}"."INFORMATION_SCHEMA"."COLUMNS" ` +
2928
                    `LEFT JOIN "sys"."computed_columns" "cc" ON COL_NAME("cc"."object_id", "cc"."column_id") = "column_name" ` +
2929
                    `WHERE (${condition})`
2930
                )
2931
            })
2932
            .join(" UNION ALL ")
2933

2934
        const constraintsSql = Object.entries(dbTablesByCatalog)
915✔
2935
            .map(([TABLE_CATALOG, tables]) => {
2936
                const conditions = tables
915✔
2937
                    .map(
2938
                        ({ TABLE_NAME, TABLE_SCHEMA }) =>
2939
                            `("columnUsages"."TABLE_SCHEMA" = '${TABLE_SCHEMA}' AND "columnUsages"."TABLE_NAME" = '${TABLE_NAME}')`,
1,392✔
2940
                    )
2941
                    .join(" OR ")
2942

2943
                return (
915✔
2944
                    `SELECT "columnUsages".*, "tableConstraints"."CONSTRAINT_TYPE", "chk"."definition" ` +
2945
                    `FROM "${TABLE_CATALOG}"."INFORMATION_SCHEMA"."CONSTRAINT_COLUMN_USAGE" "columnUsages" ` +
2946
                    `INNER JOIN "${TABLE_CATALOG}"."INFORMATION_SCHEMA"."TABLE_CONSTRAINTS" "tableConstraints" ` +
2947
                    `ON ` +
2948
                    `"tableConstraints"."CONSTRAINT_NAME" = "columnUsages"."CONSTRAINT_NAME" AND ` +
2949
                    `"tableConstraints"."TABLE_SCHEMA" = "columnUsages"."TABLE_SCHEMA" AND ` +
2950
                    `"tableConstraints"."TABLE_NAME" = "columnUsages"."TABLE_NAME" ` +
2951
                    `LEFT JOIN "${TABLE_CATALOG}"."sys"."check_constraints" "chk" ` +
2952
                    `ON ` +
2953
                    `"chk"."object_id" = OBJECT_ID("columnUsages"."TABLE_CATALOG" + '.' + "columnUsages"."TABLE_SCHEMA" + '.' + "columnUsages"."CONSTRAINT_NAME") ` +
2954
                    `WHERE ` +
2955
                    `(${conditions}) AND ` +
2956
                    `"tableConstraints"."CONSTRAINT_TYPE" IN ('PRIMARY KEY', 'UNIQUE', 'CHECK')`
2957
                )
2958
            })
2959
            .join(" UNION ALL ")
2960

2961
        const foreignKeysSql = Object.entries(dbTablesByCatalog)
915✔
2962
            .map(([TABLE_CATALOG, tables]) => {
2963
                const conditions = tables
915✔
2964
                    .map(
2965
                        ({ TABLE_NAME, TABLE_SCHEMA }) =>
2966
                            `("s1"."name" = '${TABLE_SCHEMA}' AND "t1"."name" = '${TABLE_NAME}')`,
1,392✔
2967
                    )
2968
                    .join(" OR ")
2969

2970
                return (
915✔
2971
                    `SELECT "fk"."name" AS "FK_NAME", '${TABLE_CATALOG}' AS "TABLE_CATALOG", "s1"."name" AS "TABLE_SCHEMA", "t1"."name" AS "TABLE_NAME", ` +
2972
                    `"col1"."name" AS "COLUMN_NAME", "s2"."name" AS "REF_SCHEMA", "t2"."name" AS "REF_TABLE", "col2"."name" AS "REF_COLUMN", ` +
2973
                    `"fk"."delete_referential_action_desc" AS "ON_DELETE", "fk"."update_referential_action_desc" AS "ON_UPDATE" ` +
2974
                    `FROM "${TABLE_CATALOG}"."sys"."foreign_keys" "fk" ` +
2975
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."foreign_key_columns" "fkc" ON "fkc"."constraint_object_id" = "fk"."object_id" ` +
2976
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."tables" "t1" ON "t1"."object_id" = "fk"."parent_object_id" ` +
2977
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."schemas" "s1" ON "s1"."schema_id" = "t1"."schema_id" ` +
2978
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."tables" "t2" ON "t2"."object_id" = "fk"."referenced_object_id" ` +
2979
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."schemas" "s2" ON "s2"."schema_id" = "t2"."schema_id" ` +
2980
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."columns" "col1" ON "col1"."column_id" = "fkc"."parent_column_id" AND "col1"."object_id" = "fk"."parent_object_id" ` +
2981
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."columns" "col2" ON "col2"."column_id" = "fkc"."referenced_column_id" AND "col2"."object_id" = "fk"."referenced_object_id" ` +
2982
                    `WHERE (${conditions})`
2983
                )
2984
            })
2985
            .join(" UNION ALL ")
2986

2987
        const identityColumnsSql = Object.entries(dbTablesByCatalog)
915✔
2988
            .map(([TABLE_CATALOG, tables]) => {
2989
                const conditions = tables
915✔
2990
                    .map(
2991
                        ({ TABLE_NAME, TABLE_SCHEMA }) =>
2992
                            `("TABLE_SCHEMA" = '${TABLE_SCHEMA}' AND "TABLE_NAME" = '${TABLE_NAME}')`,
1,392✔
2993
                    )
2994
                    .join(" OR ")
2995

2996
                return (
915✔
2997
                    `SELECT "TABLE_CATALOG", "TABLE_SCHEMA", "COLUMN_NAME", "TABLE_NAME" ` +
2998
                    `FROM "${TABLE_CATALOG}"."INFORMATION_SCHEMA"."COLUMNS" ` +
2999
                    `WHERE ` +
3000
                    `EXISTS(SELECT 1 FROM "${TABLE_CATALOG}"."sys"."columns" "S" WHERE OBJECT_ID("TABLE_CATALOG" + '.' + "TABLE_SCHEMA" + '.' + "TABLE_NAME") = "S"."OBJECT_ID" AND "COLUMN_NAME" = "S"."NAME" AND "S"."is_identity" = 1) AND ` +
3001
                    `(${conditions})`
3002
                )
3003
            })
3004
            .join(" UNION ALL ")
3005

3006
        const dbCollationsSql = `SELECT "NAME", "COLLATION_NAME" FROM "sys"."databases"`
915✔
3007

3008
        const indicesSql = Object.entries(dbTablesByCatalog)
915✔
3009
            .map(([TABLE_CATALOG, tables]) => {
3010
                const conditions = tables
915✔
3011
                    .map(
3012
                        ({ TABLE_NAME, TABLE_SCHEMA }) =>
3013
                            `("s"."name" = '${TABLE_SCHEMA}' AND "t"."name" = '${TABLE_NAME}')`,
1,392✔
3014
                    )
3015
                    .join(" OR ")
3016

3017
                return (
915✔
3018
                    `SELECT '${TABLE_CATALOG}' AS "TABLE_CATALOG", "s"."name" AS "TABLE_SCHEMA", "t"."name" AS "TABLE_NAME", ` +
3019
                    `"ind"."name" AS "INDEX_NAME", "col"."name" AS "COLUMN_NAME", "ind"."is_unique" AS "IS_UNIQUE", "ind"."filter_definition" as "CONDITION" ` +
3020
                    `FROM "${TABLE_CATALOG}"."sys"."indexes" "ind" ` +
3021
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."index_columns" "ic" ON "ic"."object_id" = "ind"."object_id" AND "ic"."index_id" = "ind"."index_id" ` +
3022
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."columns" "col" ON "col"."object_id" = "ic"."object_id" AND "col"."column_id" = "ic"."column_id" ` +
3023
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."tables" "t" ON "t"."object_id" = "ind"."object_id" ` +
3024
                    `INNER JOIN "${TABLE_CATALOG}"."sys"."schemas" "s" ON "s"."schema_id" = "t"."schema_id" ` +
3025
                    `WHERE ` +
3026
                    `"ind"."is_primary_key" = 0 AND "ind"."is_unique_constraint" = 0 AND "t"."is_ms_shipped" = 0 AND ` +
3027
                    `(${conditions})`
3028
                )
3029
            })
3030
            .join(" UNION ALL ")
3031

3032
        const [
3033
            dbColumns,
3034
            dbConstraints,
3035
            dbForeignKeys,
3036
            dbIdentityColumns,
3037
            dbCollations,
3038
            dbIndices,
3039
        ]: ObjectLiteral[][] = await Promise.all([
915✔
3040
            this.query(columnsSql),
3041
            this.query(constraintsSql),
3042
            this.query(foreignKeysSql),
3043
            this.query(identityColumnsSql),
3044
            this.query(dbCollationsSql),
3045
            this.query(indicesSql),
3046
        ])
3047

3048
        // create table schemas for loaded tables
3049
        return await Promise.all(
915✔
3050
            dbTables.map(async (dbTable) => {
3051
                const table = new Table()
1,392✔
3052

3053
                const getSchemaFromKey = (dbObject: any, key: string) => {
1,392✔
3054
                    return dbObject[key] === currentSchema &&
1,842✔
3055
                        (!this.driver.options.schema ||
3056
                            this.driver.options.schema === currentSchema)
3057
                        ? undefined
3058
                        : dbObject[key]
3059
                }
3060

3061
                // We do not need to join schema and database names, when db or schema is by default.
3062
                const db =
3063
                    dbTable["TABLE_CATALOG"] === currentDatabase
1,392✔
3064
                        ? undefined
3065
                        : dbTable["TABLE_CATALOG"]
3066
                const schema = getSchemaFromKey(dbTable, "TABLE_SCHEMA")
1,392✔
3067
                table.database = dbTable["TABLE_CATALOG"]
1,392✔
3068
                table.schema = dbTable["TABLE_SCHEMA"]
1,392✔
3069
                table.name = this.driver.buildTableName(
1,392✔
3070
                    dbTable["TABLE_NAME"],
3071
                    schema,
3072
                    db,
3073
                )
3074

3075
                const defaultCollation = dbCollations.find(
1,392✔
3076
                    (dbCollation) =>
3077
                        dbCollation["NAME"] === dbTable["TABLE_CATALOG"],
3,141✔
3078
                )!
3079

3080
                // create columns from the loaded columns
3081
                table.columns = await Promise.all(
1,392✔
3082
                    dbColumns
3083
                        .filter(
3084
                            (dbColumn) =>
3085
                                dbColumn["TABLE_NAME"] ===
15,408✔
3086
                                    dbTable["TABLE_NAME"] &&
3087
                                dbColumn["TABLE_SCHEMA"] ===
3088
                                    dbTable["TABLE_SCHEMA"] &&
3089
                                dbColumn["TABLE_CATALOG"] ===
3090
                                    dbTable["TABLE_CATALOG"],
3091
                        )
3092
                        .map(async (dbColumn) => {
3093
                            const columnConstraints = dbConstraints.filter(
4,929✔
3094
                                (dbConstraint) =>
3095
                                    dbConstraint["TABLE_NAME"] ===
27,243✔
3096
                                        dbColumn["TABLE_NAME"] &&
3097
                                    dbConstraint["TABLE_SCHEMA"] ===
3098
                                        dbColumn["TABLE_SCHEMA"] &&
3099
                                    dbConstraint["TABLE_CATALOG"] ===
3100
                                        dbColumn["TABLE_CATALOG"] &&
3101
                                    dbConstraint["COLUMN_NAME"] ===
3102
                                        dbColumn["COLUMN_NAME"],
3103
                            )
3104

3105
                            const uniqueConstraints = columnConstraints.filter(
4,929✔
3106
                                (constraint) =>
3107
                                    constraint["CONSTRAINT_TYPE"] === "UNIQUE",
2,601✔
3108
                            )
3109
                            const isConstraintComposite =
3110
                                uniqueConstraints.every((uniqueConstraint) => {
4,929✔
3111
                                    return dbConstraints.some(
855✔
3112
                                        (dbConstraint) =>
3113
                                            dbConstraint["CONSTRAINT_TYPE"] ===
3,655✔
3114
                                                "UNIQUE" &&
3115
                                            dbConstraint["CONSTRAINT_NAME"] ===
3116
                                                uniqueConstraint[
3117
                                                    "CONSTRAINT_NAME"
3118
                                                ] &&
3119
                                            dbConstraint["TABLE_SCHEMA"] ===
3120
                                                dbColumn["TABLE_SCHEMA"] &&
3121
                                            dbConstraint["TABLE_CATALOG"] ===
3122
                                                dbColumn["TABLE_CATALOG"] &&
3123
                                            dbConstraint["COLUMN_NAME"] !==
3124
                                                dbColumn["COLUMN_NAME"],
3125
                                    )
3126
                                })
3127

3128
                            const isGenerated = !!dbIdentityColumns.find(
4,929✔
3129
                                (column) =>
3130
                                    column["TABLE_NAME"] ===
8,040✔
3131
                                        dbColumn["TABLE_NAME"] &&
3132
                                    column["TABLE_SCHEMA"] ===
3133
                                        dbColumn["TABLE_SCHEMA"] &&
3134
                                    column["TABLE_CATALOG"] ===
3135
                                        dbColumn["TABLE_CATALOG"] &&
3136
                                    column["COLUMN_NAME"] ===
3137
                                        dbColumn["COLUMN_NAME"],
3138
                            )
3139

3140
                            const tableColumn = new TableColumn()
4,929✔
3141
                            tableColumn.name = dbColumn["COLUMN_NAME"]
4,929✔
3142
                            tableColumn.type =
4,929✔
3143
                                dbColumn["DATA_TYPE"].toLowerCase()
3144

3145
                            // check only columns that have length property
3146
                            if (
4,929✔
3147
                                this.driver.withLengthColumnTypes.indexOf(
7,617✔
3148
                                    tableColumn.type as ColumnType,
3149
                                ) !== -1 &&
3150
                                dbColumn["CHARACTER_MAXIMUM_LENGTH"]
3151
                            ) {
3152
                                const length =
3153
                                    dbColumn[
2,688✔
3154
                                        "CHARACTER_MAXIMUM_LENGTH"
3155
                                    ].toString()
3156
                                if (length === "-1") {
2,688✔
3157
                                    tableColumn.length = "MAX"
12✔
3158
                                } else {
3159
                                    tableColumn.length =
2,676✔
3160
                                        !this.isDefaultColumnLength(
2,676✔
3161
                                            table,
3162
                                            tableColumn,
3163
                                            length,
3164
                                        )
3165
                                            ? length
3166
                                            : ""
3167
                                }
3168
                            }
3169

3170
                            if (
4,929✔
3171
                                tableColumn.type === "decimal" ||
9,843✔
3172
                                tableColumn.type === "numeric"
3173
                            ) {
3174
                                if (
21✔
3175
                                    dbColumn["NUMERIC_PRECISION"] !== null &&
42✔
3176
                                    !this.isDefaultColumnPrecision(
3177
                                        table,
3178
                                        tableColumn,
3179
                                        dbColumn["NUMERIC_PRECISION"],
3180
                                    )
3181
                                )
3182
                                    tableColumn.precision =
12✔
3183
                                        dbColumn["NUMERIC_PRECISION"]
3184
                                if (
21✔
3185
                                    dbColumn["NUMERIC_SCALE"] !== null &&
42✔
3186
                                    !this.isDefaultColumnScale(
3187
                                        table,
3188
                                        tableColumn,
3189
                                        dbColumn["NUMERIC_SCALE"],
3190
                                    )
3191
                                )
3192
                                    tableColumn.scale =
12✔
3193
                                        dbColumn["NUMERIC_SCALE"]
3194
                            }
3195

3196
                            if (tableColumn.type === "nvarchar") {
4,929✔
3197
                                // Check if this is an enum
3198
                                const columnCheckConstraints =
3199
                                    columnConstraints.filter(
2,355✔
3200
                                        (constraint) =>
3201
                                            constraint["CONSTRAINT_TYPE"] ===
765✔
3202
                                            "CHECK",
3203
                                    )
3204
                                if (columnCheckConstraints.length) {
2,355✔
3205
                                    // const isEnumRegexp = new RegExp("^\\(\\[" + tableColumn.name + "\\]='[^']+'(?: OR \\[" + tableColumn.name + "\\]='[^']+')*\\)$");
3206
                                    for (const checkConstraint of columnCheckConstraints) {
33✔
3207
                                        if (
33✔
3208
                                            this.isEnumCheckConstraint(
3209
                                                checkConstraint[
3210
                                                    "CONSTRAINT_NAME"
3211
                                                ],
3212
                                            )
3213
                                        ) {
3214
                                            // This is an enum constraint, make column into an enum
3215
                                            tableColumn.enum = []
15✔
3216
                                            const enumValueRegexp = new RegExp(
15✔
3217
                                                "\\[" +
3218
                                                    tableColumn.name +
3219
                                                    "\\]='([^']+)'",
3220
                                                "g",
3221
                                            )
3222
                                            let result
3223
                                            while (
15✔
3224
                                                (result = enumValueRegexp.exec(
3225
                                                    checkConstraint[
3226
                                                        "definition"
3227
                                                    ],
3228
                                                )) !== null
3229
                                            ) {
3230
                                                tableColumn.enum.unshift(
39✔
3231
                                                    result[1],
3232
                                                )
3233
                                            }
3234
                                            // Skip other column constraints
3235
                                            break
15✔
3236
                                        }
3237
                                    }
3238
                                }
3239
                            }
3240

3241
                            const primaryConstraint = columnConstraints.find(
4,929✔
3242
                                (constraint) =>
3243
                                    constraint["CONSTRAINT_TYPE"] ===
2,577✔
3244
                                    "PRIMARY KEY",
3245
                            )
3246
                            if (primaryConstraint) {
4,929✔
3247
                                tableColumn.isPrimary = true
1,449✔
3248
                                // find another columns involved in primary key constraint
3249
                                const anotherPrimaryConstraints =
3250
                                    dbConstraints.filter(
1,449✔
3251
                                        (constraint) =>
3252
                                            constraint["TABLE_NAME"] ===
7,941✔
3253
                                                dbColumn["TABLE_NAME"] &&
3254
                                            constraint["TABLE_SCHEMA"] ===
3255
                                                dbColumn["TABLE_SCHEMA"] &&
3256
                                            constraint["TABLE_CATALOG"] ===
3257
                                                dbColumn["TABLE_CATALOG"] &&
3258
                                            constraint["COLUMN_NAME"] !==
3259
                                                dbColumn["COLUMN_NAME"] &&
3260
                                            constraint["CONSTRAINT_TYPE"] ===
3261
                                                "PRIMARY KEY",
3262
                                    )
3263

3264
                                // collect all column names
3265
                                const columnNames =
3266
                                    anotherPrimaryConstraints.map(
1,449✔
3267
                                        (constraint) =>
3268
                                            constraint["COLUMN_NAME"],
174✔
3269
                                    )
3270
                                columnNames.push(dbColumn["COLUMN_NAME"])
1,449✔
3271

3272
                                // build default primary key constraint name
3273
                                const pkName =
3274
                                    this.connection.namingStrategy.primaryKeyName(
1,449✔
3275
                                        table,
3276
                                        columnNames,
3277
                                    )
3278

3279
                                // if primary key has user-defined constraint name, write it in table column
3280
                                if (
1,449✔
3281
                                    primaryConstraint["CONSTRAINT_NAME"] !==
3282
                                    pkName
3283
                                ) {
3284
                                    tableColumn.primaryKeyConstraintName =
78✔
3285
                                        primaryConstraint["CONSTRAINT_NAME"]
3286
                                }
3287
                            }
3288

3289
                            tableColumn.default =
4,929✔
3290
                                dbColumn["COLUMN_DEFAULT"] !== null &&
10,176✔
3291
                                dbColumn["COLUMN_DEFAULT"] !== undefined
3292
                                    ? this.removeParenthesisFromDefault(
3293
                                          dbColumn["COLUMN_DEFAULT"],
3294
                                      )
3295
                                    : undefined
3296
                            tableColumn.isNullable =
4,929✔
3297
                                dbColumn["IS_NULLABLE"] === "YES"
3298
                            tableColumn.isUnique =
4,929✔
3299
                                uniqueConstraints.length > 0 &&
5,784✔
3300
                                !isConstraintComposite
3301
                            tableColumn.isGenerated = isGenerated
4,929✔
3302
                            if (isGenerated)
4,929✔
3303
                                tableColumn.generationStrategy = "increment"
762✔
3304
                            if (tableColumn.default === "newsequentialid()") {
4,929✔
3305
                                tableColumn.isGenerated = true
21✔
3306
                                tableColumn.generationStrategy = "uuid"
21✔
3307
                                tableColumn.default = undefined
21✔
3308
                            }
3309

3310
                            // todo: unable to get default charset
3311
                            // tableColumn.charset = dbColumn["CHARACTER_SET_NAME"];
3312
                            if (dbColumn["COLLATION_NAME"])
4,929✔
3313
                                tableColumn.collation =
2,619✔
3314
                                    dbColumn["COLLATION_NAME"] ===
2,619✔
3315
                                    defaultCollation["COLLATION_NAME"]
3316
                                        ? undefined
3317
                                        : dbColumn["COLLATION_NAME"]
3318

3319
                            if (
4,929✔
3320
                                tableColumn.type === "datetime2" ||
14,712✔
3321
                                tableColumn.type === "time" ||
3322
                                tableColumn.type === "datetimeoffset"
3323
                            ) {
3324
                                tableColumn.precision =
48✔
3325
                                    !this.isDefaultColumnPrecision(
48✔
3326
                                        table,
3327
                                        tableColumn,
3328
                                        dbColumn["DATETIME_PRECISION"],
3329
                                    )
3330
                                        ? dbColumn["DATETIME_PRECISION"]
3331
                                        : undefined
3332
                            }
3333

3334
                            if (
4,929✔
3335
                                dbColumn["is_persisted"] !== null &&
5,211✔
3336
                                dbColumn["is_persisted"] !== undefined &&
3337
                                dbColumn["definition"]
3338
                            ) {
3339
                                tableColumn.generatedType =
141✔
3340
                                    dbColumn["is_persisted"] === true
141✔
3341
                                        ? "STORED"
3342
                                        : "VIRTUAL"
3343
                                // We cannot relay on information_schema.columns.generation_expression, because it is formatted different.
3344
                                const asExpressionQuery =
3345
                                    this.selectTypeormMetadataSql({
141✔
3346
                                        database: dbTable["TABLE_CATALOG"],
3347
                                        schema: dbTable["TABLE_SCHEMA"],
3348
                                        table: dbTable["TABLE_NAME"],
3349
                                        type: MetadataTableType.GENERATED_COLUMN,
3350
                                        name: tableColumn.name,
3351
                                    })
3352

3353
                                const results = await this.query(
141✔
3354
                                    asExpressionQuery.query,
3355
                                    asExpressionQuery.parameters,
3356
                                )
3357
                                if (results[0] && results[0].value) {
141!
3358
                                    tableColumn.asExpression = results[0].value
141✔
3359
                                } else {
3360
                                    tableColumn.asExpression = ""
×
3361
                                }
3362
                            }
3363

3364
                            return tableColumn
4,929✔
3365
                        }),
3366
                )
3367

3368
                // find unique constraints of table, group them by constraint name and build TableUnique.
3369
                const tableUniqueConstraints = OrmUtils.uniq(
1,392✔
3370
                    dbConstraints.filter(
3371
                        (dbConstraint) =>
3372
                            dbConstraint["TABLE_NAME"] ===
7,734✔
3373
                                dbTable["TABLE_NAME"] &&
3374
                            dbConstraint["TABLE_SCHEMA"] ===
3375
                                dbTable["TABLE_SCHEMA"] &&
3376
                            dbConstraint["TABLE_CATALOG"] ===
3377
                                dbTable["TABLE_CATALOG"] &&
3378
                            dbConstraint["CONSTRAINT_TYPE"] === "UNIQUE",
3379
                    ),
3380
                    (dbConstraint) => dbConstraint["CONSTRAINT_NAME"],
1,449✔
3381
                )
3382

3383
                table.uniques = tableUniqueConstraints.map((constraint) => {
1,392✔
3384
                    const uniques = dbConstraints.filter(
597✔
3385
                        (dbC) =>
3386
                            dbC["CONSTRAINT_NAME"] ===
3,777✔
3387
                            constraint["CONSTRAINT_NAME"],
3388
                    )
3389
                    return new TableUnique({
597✔
3390
                        name: constraint["CONSTRAINT_NAME"],
3391
                        columnNames: uniques.map((u) => u["COLUMN_NAME"]),
864✔
3392
                    })
3393
                })
3394

3395
                // find check constraints of table, group them by constraint name and build TableCheck.
3396
                const tableCheckConstraints = OrmUtils.uniq(
1,392✔
3397
                    dbConstraints.filter(
3398
                        (dbConstraint) =>
3399
                            dbConstraint["TABLE_NAME"] ===
7,734✔
3400
                                dbTable["TABLE_NAME"] &&
3401
                            dbConstraint["TABLE_SCHEMA"] ===
3402
                                dbTable["TABLE_SCHEMA"] &&
3403
                            dbConstraint["TABLE_CATALOG"] ===
3404
                                dbTable["TABLE_CATALOG"] &&
3405
                            dbConstraint["CONSTRAINT_TYPE"] === "CHECK",
3406
                    ),
3407
                    (dbConstraint) => dbConstraint["CONSTRAINT_NAME"],
327✔
3408
                )
3409

3410
                table.checks = tableCheckConstraints
1,392✔
3411
                    .filter(
3412
                        (constraint) =>
3413
                            !this.isEnumCheckConstraint(
276✔
3414
                                constraint["CONSTRAINT_NAME"],
3415
                            ),
3416
                    )
3417
                    .map((constraint) => {
3418
                        const checks = dbConstraints.filter(
261✔
3419
                            (dbC) =>
3420
                                dbC["CONSTRAINT_NAME"] ===
1,824✔
3421
                                constraint["CONSTRAINT_NAME"],
3422
                        )
3423
                        return new TableCheck({
261✔
3424
                            name: constraint["CONSTRAINT_NAME"],
3425
                            columnNames: checks.map((c) => c["COLUMN_NAME"]),
273✔
3426
                            expression: constraint["definition"],
3427
                        })
3428
                    })
3429

3430
                // find foreign key constraints of table, group them by constraint name and build TableForeignKey.
3431
                const tableForeignKeyConstraints = OrmUtils.uniq(
1,392✔
3432
                    dbForeignKeys.filter(
3433
                        (dbForeignKey) =>
3434
                            dbForeignKey["TABLE_NAME"] ===
1,821✔
3435
                                dbTable["TABLE_NAME"] &&
3436
                            dbForeignKey["TABLE_SCHEMA"] ===
3437
                                dbTable["TABLE_SCHEMA"] &&
3438
                            dbForeignKey["TABLE_CATALOG"] ===
3439
                                dbTable["TABLE_CATALOG"],
3440
                    ),
3441
                    (dbForeignKey) => dbForeignKey["FK_NAME"],
612✔
3442
                )
3443

3444
                table.foreignKeys = tableForeignKeyConstraints.map(
1,392✔
3445
                    (dbForeignKey) => {
3446
                        const foreignKeys = dbForeignKeys.filter(
450✔
3447
                            (dbFk) =>
3448
                                dbFk["FK_NAME"] === dbForeignKey["FK_NAME"],
1,050✔
3449
                        )
3450

3451
                        // if referenced table located in currently used db and schema, we don't need to concat db and schema names to table name.
3452
                        const db =
3453
                            dbForeignKey["TABLE_CATALOG"] === currentDatabase
450✔
3454
                                ? undefined
3455
                                : dbForeignKey["TABLE_CATALOG"]
3456
                        const schema = getSchemaFromKey(
450✔
3457
                            dbForeignKey,
3458
                            "REF_SCHEMA",
3459
                        )
3460
                        const referencedTableName = this.driver.buildTableName(
450✔
3461
                            dbForeignKey["REF_TABLE"],
3462
                            schema,
3463
                            db,
3464
                        )
3465

3466
                        return new TableForeignKey({
450✔
3467
                            name: dbForeignKey["FK_NAME"],
3468
                            columnNames: foreignKeys.map(
3469
                                (dbFk) => dbFk["COLUMN_NAME"],
456✔
3470
                            ),
3471
                            referencedDatabase: dbForeignKey["TABLE_CATALOG"],
3472
                            referencedSchema: dbForeignKey["REF_SCHEMA"],
3473
                            referencedTableName: referencedTableName,
3474
                            referencedColumnNames: foreignKeys.map(
3475
                                (dbFk) => dbFk["REF_COLUMN"],
456✔
3476
                            ),
3477
                            onDelete: dbForeignKey["ON_DELETE"].replace(
3478
                                "_",
3479
                                " ",
3480
                            ), // SqlServer returns NO_ACTION, instead of NO ACTION
3481
                            onUpdate: dbForeignKey["ON_UPDATE"].replace(
3482
                                "_",
3483
                                " ",
3484
                            ), // SqlServer returns NO_ACTION, instead of NO ACTION
3485
                        })
3486
                    },
3487
                )
3488

3489
                // find index constraints of table, group them by constraint name and build TableIndex.
3490
                const tableIndexConstraints = OrmUtils.uniq(
1,392✔
3491
                    dbIndices.filter(
3492
                        (dbIndex) =>
3493
                            dbIndex["TABLE_NAME"] === dbTable["TABLE_NAME"] &&
1,053✔
3494
                            dbIndex["TABLE_SCHEMA"] ===
3495
                                dbTable["TABLE_SCHEMA"] &&
3496
                            dbIndex["TABLE_CATALOG"] ===
3497
                                dbTable["TABLE_CATALOG"],
3498
                    ),
3499
                    (dbIndex) => dbIndex["INDEX_NAME"],
864✔
3500
                )
3501

3502
                table.indices = tableIndexConstraints.map((constraint) => {
1,392✔
3503
                    const indices = dbIndices.filter((index) => {
420✔
3504
                        return (
1,398✔
3505
                            index["TABLE_CATALOG"] ===
5,187✔
3506
                                constraint["TABLE_CATALOG"] &&
3507
                            index["TABLE_SCHEMA"] ===
3508
                                constraint["TABLE_SCHEMA"] &&
3509
                            index["TABLE_NAME"] === constraint["TABLE_NAME"] &&
3510
                            index["INDEX_NAME"] === constraint["INDEX_NAME"]
3511
                        )
3512
                    })
3513
                    return new TableIndex(<TableIndexOptions>{
420✔
3514
                        table: table,
3515
                        name: constraint["INDEX_NAME"],
3516
                        columnNames: indices.map((i) => i["COLUMN_NAME"]),
486✔
3517
                        isUnique: constraint["IS_UNIQUE"],
3518
                        where: constraint["CONDITION"],
3519
                    })
3520
                })
3521

3522
                return table
1,392✔
3523
            }),
3524
        )
3525
    }
3526

3527
    /**
3528
     * Builds and returns SQL for create table.
3529
     */
3530
    protected createTableSql(table: Table, createForeignKeys?: boolean): Query {
3531
        const columnDefinitions = table.columns
14,154✔
3532
            .map((column) =>
3533
                this.buildCreateColumnSql(table, column, false, true),
45,786✔
3534
            )
3535
            .join(", ")
3536
        let sql = `CREATE TABLE ${this.escapePath(table)} (${columnDefinitions}`
14,154✔
3537

3538
        table.columns
14,154✔
3539
            .filter((column) => column.isUnique)
45,786✔
3540
            .forEach((column) => {
3541
                const isUniqueExist = table.uniques.some(
906✔
3542
                    (unique) =>
3543
                        unique.columnNames.length === 1 &&
1,098✔
3544
                        unique.columnNames[0] === column.name,
3545
                )
3546
                if (!isUniqueExist)
906✔
3547
                    table.uniques.push(
12✔
3548
                        new TableUnique({
3549
                            name: this.connection.namingStrategy.uniqueConstraintName(
3550
                                table,
3551
                                [column.name],
3552
                            ),
3553
                            columnNames: [column.name],
3554
                        }),
3555
                    )
3556
            })
3557

3558
        if (table.uniques.length > 0) {
14,154✔
3559
            const uniquesSql = table.uniques
828✔
3560
                .map((unique) => {
3561
                    const uniqueName = unique.name
1,218✔
3562
                        ? unique.name
3563
                        : this.connection.namingStrategy.uniqueConstraintName(
3564
                              table,
3565
                              unique.columnNames,
3566
                          )
3567
                    const columnNames = unique.columnNames
1,218✔
3568
                        .map((columnName) => `"${columnName}"`)
1,635✔
3569
                        .join(", ")
3570
                    return `CONSTRAINT "${uniqueName}" UNIQUE (${columnNames})`
1,218✔
3571
                })
3572
                .join(", ")
3573

3574
            sql += `, ${uniquesSql}`
828✔
3575
        }
3576

3577
        if (table.checks.length > 0) {
14,154✔
3578
            const checksSql = table.checks
210✔
3579
                .map((check) => {
3580
                    const checkName = check.name
213✔
3581
                        ? check.name
3582
                        : this.connection.namingStrategy.checkConstraintName(
3583
                              table,
3584
                              check.expression!,
3585
                          )
3586
                    return `CONSTRAINT "${checkName}" CHECK (${check.expression})`
213✔
3587
                })
3588
                .join(", ")
3589

3590
            sql += `, ${checksSql}`
210✔
3591
        }
3592

3593
        if (table.foreignKeys.length > 0 && createForeignKeys) {
14,154✔
3594
            const foreignKeysSql = table.foreignKeys
12✔
3595
                .map((fk) => {
3596
                    const columnNames = fk.columnNames
15✔
3597
                        .map((columnName) => `"${columnName}"`)
18✔
3598
                        .join(", ")
3599
                    if (!fk.name)
15✔
3600
                        fk.name = this.connection.namingStrategy.foreignKeyName(
9✔
3601
                            table,
3602
                            fk.columnNames,
3603
                            this.getTablePath(fk),
3604
                            fk.referencedColumnNames,
3605
                        )
3606
                    const referencedColumnNames = fk.referencedColumnNames
15✔
3607
                        .map((columnName) => `"${columnName}"`)
18✔
3608
                        .join(", ")
3609

3610
                    let constraint = `CONSTRAINT "${
15✔
3611
                        fk.name
3612
                    }" FOREIGN KEY (${columnNames}) REFERENCES ${this.escapePath(
3613
                        this.getTablePath(fk),
3614
                    )} (${referencedColumnNames})`
3615
                    if (fk.onDelete) constraint += ` ON DELETE ${fk.onDelete}`
15✔
3616
                    if (fk.onUpdate) constraint += ` ON UPDATE ${fk.onUpdate}`
15✔
3617

3618
                    return constraint
15✔
3619
                })
3620
                .join(", ")
3621

3622
            sql += `, ${foreignKeysSql}`
12✔
3623
        }
3624

3625
        const primaryColumns = table.columns.filter(
14,154✔
3626
            (column) => column.isPrimary,
45,786✔
3627
        )
3628
        if (primaryColumns.length > 0) {
14,154✔
3629
            const primaryKeyName = primaryColumns[0].primaryKeyConstraintName
14,115✔
3630
                ? primaryColumns[0].primaryKeyConstraintName
3631
                : this.connection.namingStrategy.primaryKeyName(
3632
                      table,
3633
                      primaryColumns.map((column) => column.name),
17,769✔
3634
                  )
3635

3636
            const columnNames = primaryColumns
14,115✔
3637
                .map((column) => `"${column.name}"`)
17,841✔
3638
                .join(", ")
3639
            sql += `, CONSTRAINT "${primaryKeyName}" PRIMARY KEY (${columnNames})`
14,115✔
3640
        }
3641

3642
        sql += `)`
14,154✔
3643

3644
        return new Query(sql)
14,154✔
3645
    }
3646

3647
    /**
3648
     * Builds drop table sql.
3649
     */
3650
    protected dropTableSql(
3651
        tableOrName: Table | string,
3652
        ifExist?: boolean,
3653
    ): Query {
3654
        const query = ifExist
14,154!
3655
            ? `DROP TABLE IF EXISTS ${this.escapePath(tableOrName)}`
3656
            : `DROP TABLE ${this.escapePath(tableOrName)}`
3657
        return new Query(query)
14,154✔
3658
    }
3659

3660
    protected createViewSql(view: View): Query {
3661
        const parsedName = this.driver.parseTableName(view)
24✔
3662

3663
        // Can't use `escapePath` here because `CREATE VIEW` does not accept database names.
3664
        const viewIdentifier = parsedName.schema
24!
3665
            ? `"${parsedName.schema}"."${parsedName.tableName}"`
3666
            : `"${parsedName.tableName}"`
3667

3668
        if (typeof view.expression === "string") {
24✔
3669
            return new Query(
6✔
3670
                `CREATE VIEW ${viewIdentifier} AS ${view.expression}`,
3671
            )
3672
        } else {
3673
            return new Query(
18✔
3674
                `CREATE VIEW ${viewIdentifier} AS ${view
3675
                    .expression(this.connection)
3676
                    .getQuery()}`,
3677
            )
3678
        }
3679
    }
3680

3681
    protected async insertViewDefinitionSql(view: View): Promise<Query> {
3682
        const parsedTableName = this.driver.parseTableName(view)
24✔
3683

3684
        if (!parsedTableName.schema) {
24!
3685
            parsedTableName.schema = await this.getCurrentSchema()
×
3686
        }
3687

3688
        const expression =
3689
            typeof view.expression === "string"
24✔
3690
                ? view.expression.trim()
3691
                : view.expression(this.connection).getQuery()
3692
        return this.insertTypeormMetadataSql({
24✔
3693
            type: MetadataTableType.VIEW,
3694
            database: parsedTableName.database,
3695
            schema: parsedTableName.schema,
3696
            name: parsedTableName.tableName,
3697
            value: expression,
3698
        })
3699
    }
3700

3701
    /**
3702
     * Builds drop view sql.
3703
     */
3704
    protected dropViewSql(viewOrPath: View | string): Query {
3705
        return new Query(`DROP VIEW ${this.escapePath(viewOrPath)}`)
24✔
3706
    }
3707

3708
    /**
3709
     * Builds remove view sql.
3710
     */
3711
    protected async deleteViewDefinitionSql(
3712
        viewOrPath: View | string,
3713
    ): Promise<Query> {
3714
        const parsedTableName = this.driver.parseTableName(viewOrPath)
24✔
3715

3716
        if (!parsedTableName.schema) {
24!
3717
            parsedTableName.schema = await this.getCurrentSchema()
×
3718
        }
3719

3720
        return this.deleteTypeormMetadataSql({
24✔
3721
            type: MetadataTableType.VIEW,
3722
            database: parsedTableName.database,
3723
            schema: parsedTableName.schema,
3724
            name: parsedTableName.tableName,
3725
        })
3726
    }
3727

3728
    /**
3729
     * Builds create index sql.
3730
     */
3731
    protected createIndexSql(table: Table, index: TableIndex): Query {
3732
        const columns = index.columnNames
6,426✔
3733
            .map((columnName) => `"${columnName}"`)
7,494✔
3734
            .join(", ")
3735
        return new Query(
6,426✔
3736
            `CREATE ${index.isUnique ? "UNIQUE " : ""}INDEX "${
6,426✔
3737
                index.name
3738
            }" ON ${this.escapePath(table)} (${columns}) ${
3739
                index.where ? "WHERE " + index.where : ""
6,426✔
3740
            }`,
3741
        )
3742
    }
3743

3744
    /**
3745
     * Builds drop index sql.
3746
     */
3747
    protected dropIndexSql(
3748
        table: Table,
3749
        indexOrName: TableIndex | string,
3750
    ): Query {
3751
        let indexName = InstanceChecker.isTableIndex(indexOrName)
6,426!
3752
            ? indexOrName.name
3753
            : indexOrName
3754
        return new Query(
6,426✔
3755
            `DROP INDEX "${indexName}" ON ${this.escapePath(table)}`,
3756
        )
3757
    }
3758

3759
    /**
3760
     * Builds create primary key sql.
3761
     */
3762
    protected createPrimaryKeySql(
3763
        table: Table,
3764
        columnNames: string[],
3765
        constraintName?: string,
3766
    ): Query {
3767
        const primaryKeyName = constraintName
15!
3768
            ? constraintName
3769
            : this.connection.namingStrategy.primaryKeyName(table, columnNames)
3770

3771
        const columnNamesString = columnNames
15✔
3772
            .map((columnName) => `"${columnName}"`)
18✔
3773
            .join(", ")
3774
        return new Query(
15✔
3775
            `ALTER TABLE ${this.escapePath(
3776
                table,
3777
            )} ADD CONSTRAINT "${primaryKeyName}" PRIMARY KEY (${columnNamesString})`,
3778
        )
3779
    }
3780

3781
    /**
3782
     * Builds drop primary key sql.
3783
     */
3784
    protected dropPrimaryKeySql(table: Table): Query {
3785
        const columnNames = table.primaryColumns.map((column) => column.name)
18✔
3786
        const constraintName = table.primaryColumns[0].primaryKeyConstraintName
15✔
3787
        const primaryKeyName = constraintName
15!
3788
            ? constraintName
3789
            : this.connection.namingStrategy.primaryKeyName(table, columnNames)
3790

3791
        return new Query(
15✔
3792
            `ALTER TABLE ${this.escapePath(
3793
                table,
3794
            )} DROP CONSTRAINT "${primaryKeyName}"`,
3795
        )
3796
    }
3797

3798
    /**
3799
     * Builds create unique constraint sql.
3800
     */
3801
    protected createUniqueConstraintSql(
3802
        table: Table,
3803
        uniqueConstraint: TableUnique,
3804
    ): Query {
3805
        const columnNames = uniqueConstraint.columnNames
33✔
3806
            .map((column) => `"` + column + `"`)
54✔
3807
            .join(", ")
3808
        return new Query(
33✔
3809
            `ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${
3810
                uniqueConstraint.name
3811
            }" UNIQUE (${columnNames})`,
3812
        )
3813
    }
3814

3815
    /**
3816
     * Builds drop unique constraint sql.
3817
     */
3818
    protected dropUniqueConstraintSql(
3819
        table: Table,
3820
        uniqueOrName: TableUnique | string,
3821
    ): Query {
3822
        const uniqueName = InstanceChecker.isTableUnique(uniqueOrName)
33!
3823
            ? uniqueOrName.name
3824
            : uniqueOrName
3825
        return new Query(
33✔
3826
            `ALTER TABLE ${this.escapePath(
3827
                table,
3828
            )} DROP CONSTRAINT "${uniqueName}"`,
3829
        )
3830
    }
3831

3832
    /**
3833
     * Builds create check constraint sql.
3834
     */
3835
    protected createCheckConstraintSql(
3836
        table: Table,
3837
        checkConstraint: TableCheck,
3838
    ): Query {
3839
        return new Query(
36✔
3840
            `ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${
3841
                checkConstraint.name
3842
            }" CHECK (${checkConstraint.expression})`,
3843
        )
3844
    }
3845

3846
    /**
3847
     * Builds drop check constraint sql.
3848
     */
3849
    protected dropCheckConstraintSql(
3850
        table: Table,
3851
        checkOrName: TableCheck | string,
3852
    ): Query {
3853
        const checkName = InstanceChecker.isTableCheck(checkOrName)
36!
3854
            ? checkOrName.name
3855
            : checkOrName
3856
        return new Query(
36✔
3857
            `ALTER TABLE ${this.escapePath(
3858
                table,
3859
            )} DROP CONSTRAINT "${checkName}"`,
3860
        )
3861
    }
3862

3863
    /**
3864
     * Builds create foreign key sql.
3865
     */
3866
    protected createForeignKeySql(
3867
        table: Table,
3868
        foreignKey: TableForeignKey,
3869
    ): Query {
3870
        const columnNames = foreignKey.columnNames
9,426✔
3871
            .map((column) => `"` + column + `"`)
10,890✔
3872
            .join(", ")
3873
        const referencedColumnNames = foreignKey.referencedColumnNames
9,426✔
3874
            .map((column) => `"` + column + `"`)
10,890✔
3875
            .join(",")
3876
        let sql =
3877
            `ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${
9,426✔
3878
                foreignKey.name
3879
            }" FOREIGN KEY (${columnNames}) ` +
3880
            `REFERENCES ${this.escapePath(
3881
                this.getTablePath(foreignKey),
3882
            )}(${referencedColumnNames})`
3883
        if (foreignKey.onDelete) sql += ` ON DELETE ${foreignKey.onDelete}`
9,426✔
3884
        if (foreignKey.onUpdate) sql += ` ON UPDATE ${foreignKey.onUpdate}`
9,426✔
3885

3886
        return new Query(sql)
9,426✔
3887
    }
3888

3889
    /**
3890
     * Builds drop foreign key sql.
3891
     */
3892
    protected dropForeignKeySql(
3893
        table: Table,
3894
        foreignKeyOrName: TableForeignKey | string,
3895
    ): Query {
3896
        const foreignKeyName = InstanceChecker.isTableForeignKey(
9,441!
3897
            foreignKeyOrName,
3898
        )
3899
            ? foreignKeyOrName.name
3900
            : foreignKeyOrName
3901
        return new Query(
9,441✔
3902
            `ALTER TABLE ${this.escapePath(
3903
                table,
3904
            )} DROP CONSTRAINT "${foreignKeyName}"`,
3905
        )
3906
    }
3907

3908
    /**
3909
     * Escapes given table or View path.
3910
     */
3911
    protected escapePath(target: Table | View | string): string {
3912
        const { database, schema, tableName } =
3913
            this.driver.parseTableName(target)
70,458✔
3914

3915
        if (database && database !== this.driver.database) {
70,458✔
3916
            if (schema && schema !== this.driver.searchSchema) {
144✔
3917
                return `"${database}"."${schema}"."${tableName}"`
126✔
3918
            }
3919

3920
            return `"${database}".."${tableName}"`
18✔
3921
        }
3922

3923
        if (schema && schema !== this.driver.searchSchema) {
70,314✔
3924
            return `"${schema}"."${tableName}"`
210✔
3925
        }
3926

3927
        return `"${tableName}"`
70,104✔
3928
    }
3929

3930
    /**
3931
     * Concat database name and schema name to the foreign key name.
3932
     * Needs because FK name is relevant to the schema and database.
3933
     */
3934
    protected buildForeignKeyName(
3935
        fkName: string,
3936
        schemaName: string | undefined,
3937
        dbName: string | undefined,
3938
    ): string {
3939
        let joinedFkName = fkName
12✔
3940
        if (schemaName && schemaName !== this.driver.searchSchema)
12✔
3941
            joinedFkName = schemaName + "." + joinedFkName
12✔
3942
        if (dbName && dbName !== this.driver.database)
12✔
3943
            joinedFkName = dbName + "." + joinedFkName
12✔
3944

3945
        return joinedFkName
12✔
3946
    }
3947

3948
    /**
3949
     * Removes parenthesis around default value.
3950
     * Sql server returns default value with parenthesis around, e.g.
3951
     *  ('My text') - for string
3952
     *  ((1)) - for number
3953
     *  (newsequentialId()) - for function
3954
     */
3955
    protected removeParenthesisFromDefault(defaultValue: string): any {
3956
        if (defaultValue.substr(0, 1) !== "(") return defaultValue
642✔
3957
        const normalizedDefault = defaultValue.substr(
324✔
3958
            1,
3959
            defaultValue.lastIndexOf(")") - 1,
3960
        )
3961
        return this.removeParenthesisFromDefault(normalizedDefault)
324✔
3962
    }
3963

3964
    /**
3965
     * Builds a query for create column.
3966
     */
3967
    protected buildCreateColumnSql(
3968
        table: Table,
3969
        column: TableColumn,
3970
        skipIdentity: boolean,
3971
        createDefault: boolean,
3972
        skipEnum?: boolean,
3973
    ) {
3974
        let c = `"${column.name}" ${this.connection.driver.createFullType(
46,011✔
3975
            column,
3976
        )}`
3977

3978
        if (!skipEnum && column.enum) {
46,011✔
3979
            const expression = this.getEnumExpression(column)
72✔
3980
            const checkName =
3981
                this.connection.namingStrategy.checkConstraintName(
72✔
3982
                    table,
3983
                    expression,
3984
                    true,
3985
                )
3986
            c += ` CONSTRAINT ${checkName} CHECK(${expression})`
72✔
3987
        }
3988

3989
        if (column.collation) c += " COLLATE " + column.collation
46,011✔
3990

3991
        if (column.asExpression) {
46,011✔
3992
            c += ` AS (${column.asExpression})`
117✔
3993
            if (column.generatedType === "STORED") {
117✔
3994
                c += ` PERSISTED`
63✔
3995

3996
                // NOT NULL can be specified for computed columns only if PERSISTED is also specified
3997
                if (column.isNullable !== true) c += " NOT NULL"
63✔
3998
            }
3999
        } else {
4000
            if (column.isNullable !== true) c += " NOT NULL"
45,894✔
4001
        }
4002

4003
        if (
46,011✔
4004
            column.isGenerated === true &&
62,829✔
4005
            column.generationStrategy === "increment" &&
4006
            !skipIdentity
4007
        )
4008
            // don't use skipPrimary here since updates can update already exist primary without auto inc.
4009
            c += " IDENTITY(1,1)"
8,208✔
4010

4011
        if (
46,011✔
4012
            column.default !== undefined &&
51,999✔
4013
            column.default !== null &&
4014
            createDefault
4015
        ) {
4016
            // we create named constraint to be able to delete this constraint when column been dropped
4017
            const defaultName =
4018
                this.connection.namingStrategy.defaultConstraintName(
2,976✔
4019
                    table,
4020
                    column.name,
4021
                )
4022
            c += ` CONSTRAINT "${defaultName}" DEFAULT ${column.default}`
2,976✔
4023
        }
4024

4025
        if (
46,011✔
4026
            column.isGenerated &&
55,023✔
4027
            column.generationStrategy === "uuid" &&
4028
            !column.default
4029
        ) {
4030
            // we create named constraint to be able to delete this constraint when column been dropped
4031
            const defaultName =
4032
                this.connection.namingStrategy.defaultConstraintName(
402✔
4033
                    table,
4034
                    column.name,
4035
                )
4036
            c += ` CONSTRAINT "${defaultName}" DEFAULT NEWSEQUENTIALID()`
402✔
4037
        }
4038
        return c
46,011✔
4039
    }
4040

4041
    private getEnumExpression(column: TableColumn) {
4042
        if (!column.enum) {
78!
4043
            throw new Error(`Enum is not defined in column ${column.name}`)
×
4044
        }
4045
        return (
78✔
4046
            column.name +
4047
            " IN (" +
4048
            column.enum.map((val) => "'" + val + "'").join(",") +
243✔
4049
            ")"
4050
        )
4051
    }
4052

4053
    protected isEnumCheckConstraint(name: string): boolean {
4054
        return name.indexOf("CHK_") !== -1 && name.indexOf("_ENUM") !== -1
309✔
4055
    }
4056

4057
    /**
4058
     * Converts MssqlParameter into real mssql parameter type.
4059
     */
4060
    protected mssqlParameterToNativeParameter(parameter: MssqlParameter): any {
4061
        switch (this.driver.normalizeType({ type: parameter.type as any })) {
74,124!
4062
            case "bit":
4063
                return this.driver.mssql.Bit
6,012✔
4064
            case "bigint":
4065
                return this.driver.mssql.BigInt
99✔
4066
            case "decimal":
4067
                return this.driver.mssql.Decimal(...parameter.params)
12✔
4068
            case "float":
4069
                return this.driver.mssql.Float
90✔
4070
            case "int":
4071
                return this.driver.mssql.Int
32,877✔
4072
            case "money":
4073
                return this.driver.mssql.Money
3✔
4074
            case "numeric":
4075
                return this.driver.mssql.Numeric(...parameter.params)
6✔
4076
            case "smallint":
4077
                return this.driver.mssql.SmallInt
3✔
4078
            case "smallmoney":
4079
                return this.driver.mssql.SmallMoney
3✔
4080
            case "real":
4081
                return this.driver.mssql.Real
3✔
4082
            case "tinyint":
4083
                return this.driver.mssql.TinyInt
3✔
4084
            case "char":
4085
                if (
15✔
4086
                    this.driver.options.options
4087
                        ?.disableAsciiToUnicodeParamConversion
4088
                ) {
4089
                    return this.driver.mssql.Char(...parameter.params)
3✔
4090
                }
4091
                return this.driver.mssql.NChar(...parameter.params)
12✔
4092
            case "nchar":
4093
                return this.driver.mssql.NChar(...parameter.params)
6✔
4094
            case "text":
4095
                if (
15!
4096
                    this.driver.options.options
4097
                        ?.disableAsciiToUnicodeParamConversion
4098
                ) {
4099
                    return this.driver.mssql.Text
×
4100
                }
4101
                return this.driver.mssql.Ntext
15✔
4102
            case "ntext":
4103
                return this.driver.mssql.Ntext
9✔
4104
            case "varchar":
4105
                if (
96✔
4106
                    this.driver.options.options
4107
                        ?.disableAsciiToUnicodeParamConversion
4108
                ) {
4109
                    return this.driver.mssql.VarChar(...parameter.params)
3✔
4110
                }
4111
                return this.driver.mssql.NVarChar(...parameter.params)
93✔
4112
            case "nvarchar":
4113
                return this.driver.mssql.NVarChar(...parameter.params)
33,915✔
4114
            case "xml":
4115
                return this.driver.mssql.Xml
×
4116
            case "time":
4117
                return this.driver.mssql.Time(...parameter.params)
18✔
4118
            case "date":
4119
                return this.driver.mssql.Date
9✔
4120
            case "datetime":
4121
                return this.driver.mssql.DateTime
51✔
4122
            case "datetime2":
4123
                return this.driver.mssql.DateTime2(...parameter.params)
36✔
4124
            case "datetimeoffset":
4125
                return this.driver.mssql.DateTimeOffset(...parameter.params)
12✔
4126
            case "smalldatetime":
4127
                return this.driver.mssql.SmallDateTime
3✔
4128
            case "uniqueidentifier":
4129
                return this.driver.mssql.UniqueIdentifier
753✔
4130
            case "variant":
4131
                return this.driver.mssql.Variant
×
4132
            case "binary":
4133
                return this.driver.mssql.Binary
39✔
4134
            case "varbinary":
4135
                return this.driver.mssql.VarBinary(...parameter.params)
6✔
4136
            case "image":
4137
                return this.driver.mssql.Image
3✔
4138
            case "udt":
4139
                return this.driver.mssql.UDT
×
4140
            case "rowversion":
4141
                return this.driver.mssql.RowVersion
×
4142
        }
4143
    }
4144

4145
    /**
4146
     * Converts string literal of isolation level to enum.
4147
     * The underlying mssql driver requires an enum for the isolation level.
4148
     */
4149
    convertIsolationLevel(isolation: IsolationLevel) {
4150
        const ISOLATION_LEVEL = this.driver.mssql.ISOLATION_LEVEL
18✔
4151
        switch (isolation) {
18✔
4152
            case "READ UNCOMMITTED":
4153
                return ISOLATION_LEVEL.READ_UNCOMMITTED
6✔
4154
            case "REPEATABLE READ":
4155
                return ISOLATION_LEVEL.REPEATABLE_READ
3✔
4156
            case "SERIALIZABLE":
4157
                return ISOLATION_LEVEL.SERIALIZABLE
6✔
4158

4159
            case "READ COMMITTED":
4160
            default:
4161
                return ISOLATION_LEVEL.READ_COMMITTED
3✔
4162
        }
4163
    }
4164

4165
    /**
4166
     * Change table comment.
4167
     */
4168
    changeTableComment(
4169
        tableOrName: Table | string,
4170
        comment?: string,
4171
    ): Promise<void> {
4172
        throw new TypeORMError(
×
4173
            `sqlserver driver does not support change table comment.`,
4174
        )
4175
    }
4176
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc