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

typeorm / typeorm / 14764529297

30 Apr 2025 09:16PM UTC coverage: 76.309% (+0.002%) from 76.307%
14764529297

push

github

web-flow
fix: beforeQuery promises not awaited before query execution (#11086)

* fix: beforeQuery promises not awaited before query execution

Closes: #11085

* fix: run format

Closes: #11085

* fix: apply same beforeQuery & afterQuery logic to all drivers

* fix: use a different broadcaster for BeforeQuery / AfterQuery

* fix: BeforeQuery / AfterQuery event types

* fix: move broadCasterResult.wait in finally block

* fix: remove duplicated broadcasterResult.wait in ReactNativeQueryRunner

* fix: fix prettier issue

* fix: implemented requested changes

* fix: broken sqlite tests

* Revert "fix: broken sqlite tests"

This reverts commit 4bacd5f4b.

* Revert "fix: implemented requested changes"

This reverts commit 1d2f59bf2.

* review: undefined type at the end

* fix: move database connection logic outside of the promise bloc

---------

Co-authored-by: Lucian Mocanu <alumni@users.noreply.github.com>

9201 of 12761 branches covered (72.1%)

Branch coverage included in aggregate %.

100 of 142 new or added lines in 14 files covered. (70.42%)

4 existing lines in 3 files now uncovered.

18802 of 23936 relevant lines covered (78.55%)

197469.14 hits per line

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

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

29
/**
30
 * Runs queries on a single postgres database connection.
31
 */
32
export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner {
40✔
33
    // -------------------------------------------------------------------------
34
    // Public Implemented Properties
35
    // -------------------------------------------------------------------------
36

37
    /**
38
     * Database driver used by connection.
39
     */
40
    driver: SpannerDriver
41

42
    /**
43
     * Real database connection from a connection pool used to perform queries.
44
     */
45
    protected session?: any
46

47
    /**
48
     * Transaction currently executed by this session.
49
     */
50
    protected sessionTransaction?: any
51

52
    // -------------------------------------------------------------------------
53
    // Constructor
54
    // -------------------------------------------------------------------------
55

56
    constructor(driver: SpannerDriver, mode: ReplicationMode) {
57
        super()
×
58
        this.driver = driver
×
59
        this.connection = driver.connection
×
60
        this.mode = mode
×
61
        this.broadcaster = new Broadcaster(this)
×
62
    }
63

64
    // -------------------------------------------------------------------------
65
    // Public Methods
66
    // -------------------------------------------------------------------------
67

68
    /**
69
     * Creates/uses database connection from the connection pool to perform further operations.
70
     * Returns obtained database connection.
71
     */
72
    async connect(): Promise<any> {
73
        if (this.session) {
×
74
            return Promise.resolve(this.session)
×
75
        }
76

77
        const [session] = await this.driver.instanceDatabase.createSession({})
×
78
        this.session = session
×
79
        this.sessionTransaction = await session.transaction()
×
80
        return this.session
×
81
    }
82

83
    /**
84
     * Releases used database connection.
85
     * You cannot use query runner methods once its released.
86
     */
87
    async release(): Promise<void> {
88
        this.isReleased = true
×
89
        if (this.session) {
×
90
            await this.session.delete()
×
91
        }
92
        this.session = undefined
×
93
        return Promise.resolve()
×
94
    }
95

96
    /**
97
     * Starts transaction.
98
     */
99
    async startTransaction(isolationLevel?: IsolationLevel): Promise<void> {
100
        this.isTransactionActive = true
×
101
        try {
×
102
            await this.broadcaster.broadcast("BeforeTransactionStart")
×
103
        } catch (err) {
104
            this.isTransactionActive = false
×
105
            throw err
×
106
        }
107

108
        await this.connect()
×
109
        await this.sessionTransaction.begin()
×
110
        this.connection.logger.logQuery("START TRANSACTION")
×
111

112
        await this.broadcaster.broadcast("AfterTransactionStart")
×
113
    }
114

115
    /**
116
     * Commits transaction.
117
     * Error will be thrown if transaction was not started.
118
     */
119
    async commitTransaction(): Promise<void> {
120
        if (!this.isTransactionActive || !this.sessionTransaction)
×
121
            throw new TransactionNotStartedError()
×
122

123
        await this.broadcaster.broadcast("BeforeTransactionCommit")
×
124

125
        await this.sessionTransaction.commit()
×
126
        this.connection.logger.logQuery("COMMIT")
×
127
        this.isTransactionActive = false
×
128

129
        await this.broadcaster.broadcast("AfterTransactionCommit")
×
130
    }
131

132
    /**
133
     * Rollbacks transaction.
134
     * Error will be thrown if transaction was not started.
135
     */
136
    async rollbackTransaction(): Promise<void> {
137
        if (!this.isTransactionActive || !this.sessionTransaction)
×
138
            throw new TransactionNotStartedError()
×
139

140
        await this.broadcaster.broadcast("BeforeTransactionRollback")
×
141

142
        await this.sessionTransaction.rollback()
×
143
        this.connection.logger.logQuery("ROLLBACK")
×
144
        this.isTransactionActive = false
×
145

146
        await this.broadcaster.broadcast("AfterTransactionRollback")
×
147
    }
148

149
    /**
150
     * Executes a given SQL query.
151
     */
152
    async query(
153
        query: string,
154
        parameters?: any[],
155
        useStructuredResult: boolean = false,
×
156
    ): Promise<any> {
157
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
×
158

NEW
159
        await this.connect()
×
160

NEW
161
        this.driver.connection.logger.logQuery(query, parameters, this)
×
NEW
162
        await this.broadcaster.broadcast("BeforeQuery", query, parameters)
×
163

UNCOV
164
        const broadcasterResult = new BroadcasterResult()
×
165

166
        try {
×
167
            const queryStartTime = Date.now()
×
168
            let rawResult:
169
                | [
170
                      any[],
171
                      {
172
                          queryPlan: null
173
                          queryStats: null
174
                          rowCountExact: string
175
                          rowCount: string
176
                      },
177
                      { rowType: { fields: [] }; transaction: null },
178
                  ]
179
                | undefined = undefined
×
180
            const isSelect = query.startsWith("SELECT")
×
181
            const executor =
182
                isSelect && !this.isTransactionActive
×
183
                    ? this.driver.instanceDatabase
184
                    : this.sessionTransaction
185

186
            if (!this.isTransactionActive && !isSelect) {
×
187
                await this.sessionTransaction.begin()
×
188
            }
189

190
            try {
×
191
                rawResult = await executor.run({
×
192
                    sql: query,
193
                    params: parameters
×
194
                        ? parameters.reduce((params, value, index) => {
195
                              params["param" + index] = value
×
196
                              return params
×
197
                          }, {} as ObjectLiteral)
198
                        : undefined,
199
                    json: true,
200
                })
201
                if (!this.isTransactionActive && !isSelect) {
×
202
                    await this.sessionTransaction.commit()
×
203
                }
204
            } catch (error) {
205
                try {
×
206
                    // we throw original error even if rollback thrown an error
207
                    if (!this.isTransactionActive && !isSelect)
×
208
                        await this.sessionTransaction.rollback()
×
209
                } catch (rollbackError) {}
210
                throw error
×
211
            }
212

213
            // log slow queries if maxQueryExecution time is set
214
            const maxQueryExecutionTime =
215
                this.driver.options.maxQueryExecutionTime
×
216
            const queryEndTime = Date.now()
×
217
            const queryExecutionTime = queryEndTime - queryStartTime
×
218

219
            this.broadcaster.broadcastAfterQueryEvent(
×
220
                broadcasterResult,
221
                query,
222
                parameters,
223
                true,
224
                queryExecutionTime,
225
                rawResult,
226
                undefined,
227
            )
228

229
            if (
×
230
                maxQueryExecutionTime &&
×
231
                queryExecutionTime > maxQueryExecutionTime
232
            )
233
                this.driver.connection.logger.logQuerySlow(
×
234
                    queryExecutionTime,
235
                    query,
236
                    parameters,
237
                    this,
238
                )
239

240
            const result = new QueryResult()
×
241

242
            result.raw = rawResult
×
243
            result.records = rawResult ? rawResult[0] : []
×
244
            if (rawResult && rawResult[1] && rawResult[1].rowCountExact) {
×
245
                result.affected = parseInt(rawResult[1].rowCountExact)
×
246
            }
247

248
            if (!useStructuredResult) {
×
249
                return result.records
×
250
            }
251

252
            return result
×
253
        } catch (err) {
254
            this.driver.connection.logger.logQueryError(
×
255
                err,
256
                query,
257
                parameters,
258
                this,
259
            )
260
            this.broadcaster.broadcastAfterQueryEvent(
×
261
                broadcasterResult,
262
                query,
263
                parameters,
264
                false,
265
                undefined,
266
                undefined,
267
                err,
268
            )
269
            throw new QueryFailedError(query, parameters, err)
×
270
        } finally {
271
            await broadcasterResult.wait()
×
272
        }
273
    }
274

275
    /**
276
     * Update database schema.
277
     * Used for creating/altering/dropping tables, columns, indexes, etc.
278
     *
279
     * DDL changing queries should be executed by `updateSchema()` method.
280
     */
281
    async updateDDL(query: string, parameters?: any[]): Promise<void> {
282
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
×
283

284
        this.driver.connection.logger.logQuery(query, parameters, this)
×
285
        try {
×
286
            const queryStartTime = Date.now()
×
287
            const [operation] = await this.driver.instanceDatabase.updateSchema(
×
288
                query,
289
            )
290
            await operation.promise()
×
291
            // log slow queries if maxQueryExecution time is set
292
            const maxQueryExecutionTime =
293
                this.driver.options.maxQueryExecutionTime
×
294
            const queryEndTime = Date.now()
×
295
            const queryExecutionTime = queryEndTime - queryStartTime
×
296
            if (
×
297
                maxQueryExecutionTime &&
×
298
                queryExecutionTime > maxQueryExecutionTime
299
            )
300
                this.driver.connection.logger.logQuerySlow(
×
301
                    queryExecutionTime,
302
                    query,
303
                    parameters,
304
                    this,
305
                )
306
        } catch (err) {
307
            this.driver.connection.logger.logQueryError(
×
308
                err,
309
                query,
310
                parameters,
311
                this,
312
            )
313
            throw new QueryFailedError(query, parameters, err)
×
314
        }
315
    }
316

317
    /**
318
     * Returns raw data stream.
319
     */
320
    async stream(
321
        query: string,
322
        parameters?: any[],
323
        onEnd?: Function,
324
        onError?: Function,
325
    ): Promise<ReadStream> {
326
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
×
327

328
        try {
×
329
            this.driver.connection.logger.logQuery(query, parameters, this)
×
330
            const request = {
×
331
                sql: query,
332
                params: parameters
×
333
                    ? parameters.reduce((params, value, index) => {
334
                          params["param" + index] = value
×
335
                          return params
×
336
                      }, {} as ObjectLiteral)
337
                    : undefined,
338
                json: true,
339
            }
340
            const stream = this.driver.instanceDatabase.runStream(request)
×
341

342
            if (onEnd) {
×
343
                stream.on("end", onEnd)
×
344
            }
345

346
            if (onError) {
×
347
                stream.on("error", onError)
×
348
            }
349

350
            return stream
×
351
        } catch (err) {
352
            this.driver.connection.logger.logQueryError(
×
353
                err,
354
                query,
355
                parameters,
356
                this,
357
            )
358
            throw new QueryFailedError(query, parameters, err)
×
359
        }
360
    }
361

362
    /**
363
     * Returns all available database names including system databases.
364
     */
365
    async getDatabases(): Promise<string[]> {
366
        return Promise.resolve([])
×
367
    }
368

369
    /**
370
     * Returns all available schema names including system schemas.
371
     * If database parameter specified, returns schemas of that database.
372
     */
373
    async getSchemas(database?: string): Promise<string[]> {
374
        return Promise.resolve([])
×
375
    }
376

377
    /**
378
     * Checks if database with the given name exist.
379
     */
380
    async hasDatabase(database: string): Promise<boolean> {
381
        throw new TypeORMError(
×
382
            `Check database queries are not supported by Spanner driver.`,
383
        )
384
    }
385

386
    /**
387
     * Loads currently using database
388
     */
389
    async getCurrentDatabase(): Promise<string> {
390
        throw new TypeORMError(
×
391
            `Check database queries are not supported by Spanner driver.`,
392
        )
393
    }
394

395
    /**
396
     * Checks if schema with the given name exist.
397
     */
398
    async hasSchema(schema: string): Promise<boolean> {
399
        const result = await this.query(
×
400
            `SELECT * FROM "information_schema"."schemata" WHERE "schema_name" = '${schema}'`,
401
        )
402
        return result.length ? true : false
×
403
    }
404

405
    /**
406
     * Loads currently using database schema
407
     */
408
    async getCurrentSchema(): Promise<string> {
409
        throw new TypeORMError(
×
410
            `Check schema queries are not supported by Spanner driver.`,
411
        )
412
    }
413

414
    /**
415
     * Checks if table with the given name exist in the database.
416
     */
417
    async hasTable(tableOrName: Table | string): Promise<boolean> {
418
        const tableName =
419
            tableOrName instanceof Table ? tableOrName.name : tableOrName
×
420
        const sql =
421
            `SELECT * FROM \`INFORMATION_SCHEMA\`.\`TABLES\` ` +
×
422
            `WHERE \`TABLE_CATALOG\` = '' AND \`TABLE_SCHEMA\` = '' AND \`TABLE_TYPE\` = 'BASE TABLE' ` +
423
            `AND \`TABLE_NAME\` = '${tableName}'`
424
        const result = await this.query(sql)
×
425
        return result.length ? true : false
×
426
    }
427

428
    /**
429
     * Checks if column with the given name exist in the given table.
430
     */
431
    async hasColumn(
432
        tableOrName: Table | string,
433
        columnName: string,
434
    ): Promise<boolean> {
435
        const tableName =
436
            tableOrName instanceof Table ? tableOrName.name : tableOrName
×
437
        const sql =
438
            `SELECT * FROM \`INFORMATION_SCHEMA\`.\`COLUMNS\` ` +
×
439
            `WHERE \`TABLE_CATALOG\` = '' AND \`TABLE_SCHEMA\` = '' ` +
440
            `AND \`TABLE_NAME\` = '${tableName}' AND \`COLUMN_NAME\` = '${columnName}'`
441
        const result = await this.query(sql)
×
442
        return result.length ? true : false
×
443
    }
444

445
    /**
446
     * Creates a new database.
447
     * Note: Spanner does not support database creation inside a transaction block.
448
     */
449
    async createDatabase(
450
        database: string,
451
        ifNotExist?: boolean,
452
    ): Promise<void> {
453
        if (ifNotExist) {
×
454
            const databaseAlreadyExists = await this.hasDatabase(database)
×
455

456
            if (databaseAlreadyExists) return Promise.resolve()
×
457
        }
458

459
        const up = `CREATE DATABASE "${database}"`
×
460
        const down = `DROP DATABASE "${database}"`
×
461
        await this.executeQueries(new Query(up), new Query(down))
×
462
    }
463

464
    /**
465
     * Drops database.
466
     * Note: Spanner does not support database dropping inside a transaction block.
467
     */
468
    async dropDatabase(database: string, ifExist?: boolean): Promise<void> {
469
        const up = ifExist
×
470
            ? `DROP DATABASE IF EXISTS "${database}"`
471
            : `DROP DATABASE "${database}"`
472
        const down = `CREATE DATABASE "${database}"`
×
473
        await this.executeQueries(new Query(up), new Query(down))
×
474
    }
475

476
    /**
477
     * Creates a new table schema.
478
     */
479
    async createSchema(
480
        schemaPath: string,
481
        ifNotExist?: boolean,
482
    ): Promise<void> {
483
        return Promise.resolve()
×
484
    }
485

486
    /**
487
     * Drops table schema.
488
     */
489
    async dropSchema(
490
        schemaPath: string,
491
        ifExist?: boolean,
492
        isCascade?: boolean,
493
    ): Promise<void> {
494
        return Promise.resolve()
×
495
    }
496

497
    /**
498
     * Creates a new table.
499
     */
500
    async createTable(
501
        table: Table,
502
        ifNotExist: boolean = false,
×
503
        createForeignKeys: boolean = true,
×
504
        createIndices: boolean = true,
×
505
    ): Promise<void> {
506
        if (ifNotExist) {
×
507
            const isTableExist = await this.hasTable(table)
×
508
            if (isTableExist) return Promise.resolve()
×
509
        }
510
        const upQueries: Query[] = []
×
511
        const downQueries: Query[] = []
×
512

513
        upQueries.push(this.createTableSql(table, createForeignKeys))
×
514
        downQueries.push(this.dropTableSql(table))
×
515

516
        // if createForeignKeys is true, we must drop created foreign keys in down query.
517
        // createTable does not need separate method to create foreign keys, because it create fk's in the same query with table creation.
518
        if (createForeignKeys)
×
519
            table.foreignKeys.forEach((foreignKey) =>
×
520
                downQueries.push(this.dropForeignKeySql(table, foreignKey)),
×
521
            )
522

523
        if (createIndices) {
×
524
            table.indices.forEach((index) => {
×
525
                // new index may be passed without name. In this case we generate index name manually.
526
                if (!index.name)
×
527
                    index.name = this.connection.namingStrategy.indexName(
×
528
                        table,
529
                        index.columnNames,
530
                        index.where,
531
                    )
532
                upQueries.push(this.createIndexSql(table, index))
×
533
                downQueries.push(this.dropIndexSql(table, index))
×
534
            })
535
        }
536

537
        // if table has column with generated type, we must add the expression to the metadata table
538
        const generatedColumns = table.columns.filter(
×
539
            (column) => column.generatedType && column.asExpression,
×
540
        )
541

542
        for (const column of generatedColumns) {
×
543
            const insertQuery = this.insertTypeormMetadataSql({
×
544
                table: table.name,
545
                type: MetadataTableType.GENERATED_COLUMN,
546
                name: column.name,
547
                value: column.asExpression,
548
            })
549

550
            const deleteQuery = this.deleteTypeormMetadataSql({
×
551
                table: table.name,
552
                type: MetadataTableType.GENERATED_COLUMN,
553
                name: column.name,
554
            })
555

556
            upQueries.push(insertQuery)
×
557
            downQueries.push(deleteQuery)
×
558
        }
559

560
        await this.executeQueries(upQueries, downQueries)
×
561
    }
562

563
    /**
564
     * Drops the table.
565
     */
566
    async dropTable(
567
        target: Table | string,
568
        ifExist?: boolean,
569
        dropForeignKeys: boolean = true,
×
570
        dropIndices: boolean = true,
×
571
    ): Promise<void> {
572
        // It needs because if table does not exist and dropForeignKeys or dropIndices is true, we don't need
573
        // to perform drop queries for foreign keys and indices.
574
        if (ifExist) {
×
575
            const isTableExist = await this.hasTable(target)
×
576
            if (!isTableExist) return Promise.resolve()
×
577
        }
578

579
        // if dropTable called with dropForeignKeys = true, we must create foreign keys in down query.
580
        const createForeignKeys: boolean = dropForeignKeys
×
581
        const tablePath = this.getTablePath(target)
×
582
        const table = await this.getCachedTable(tablePath)
×
583
        const upQueries: Query[] = []
×
584
        const downQueries: Query[] = []
×
585

586
        if (dropIndices) {
×
587
            table.indices.forEach((index) => {
×
588
                upQueries.push(this.dropIndexSql(table, index))
×
589
                downQueries.push(this.createIndexSql(table, index))
×
590
            })
591
        }
592

593
        if (dropForeignKeys)
×
594
            table.foreignKeys.forEach((foreignKey) =>
×
595
                upQueries.push(this.dropForeignKeySql(table, foreignKey)),
×
596
            )
597

598
        upQueries.push(this.dropTableSql(table))
×
599
        downQueries.push(this.createTableSql(table, createForeignKeys))
×
600

601
        // if table had columns with generated type, we must remove the expression from the metadata table
602
        const generatedColumns = table.columns.filter(
×
603
            (column) => column.generatedType && column.asExpression,
×
604
        )
605

606
        for (const column of generatedColumns) {
×
607
            const deleteQuery = this.deleteTypeormMetadataSql({
×
608
                table: table.name,
609
                type: MetadataTableType.GENERATED_COLUMN,
610
                name: column.name,
611
            })
612

613
            const insertQuery = this.insertTypeormMetadataSql({
×
614
                table: table.name,
615
                type: MetadataTableType.GENERATED_COLUMN,
616
                name: column.name,
617
                value: column.asExpression,
618
            })
619

620
            upQueries.push(deleteQuery)
×
621
            downQueries.push(insertQuery)
×
622
        }
623

624
        await this.executeQueries(upQueries, downQueries)
×
625
    }
626

627
    /**
628
     * Creates a new view.
629
     */
630
    async createView(view: View): Promise<void> {
631
        const upQueries: Query[] = []
×
632
        const downQueries: Query[] = []
×
633
        upQueries.push(this.createViewSql(view))
×
634
        upQueries.push(await this.insertViewDefinitionSql(view))
×
635
        downQueries.push(this.dropViewSql(view))
×
636
        downQueries.push(await this.deleteViewDefinitionSql(view))
×
637
        await this.executeQueries(upQueries, downQueries)
×
638
    }
639

640
    /**
641
     * Drops the view.
642
     */
643
    async dropView(target: View | string): Promise<void> {
644
        const viewName = target instanceof View ? target.name : target
×
645
        const view = await this.getCachedView(viewName)
×
646

647
        const upQueries: Query[] = []
×
648
        const downQueries: Query[] = []
×
649
        upQueries.push(await this.deleteViewDefinitionSql(view))
×
650
        upQueries.push(this.dropViewSql(view))
×
651
        downQueries.push(await this.insertViewDefinitionSql(view))
×
652
        downQueries.push(this.createViewSql(view))
×
653
        await this.executeQueries(upQueries, downQueries)
×
654
    }
655

656
    /**
657
     * Renames the given table.
658
     */
659
    async renameTable(
660
        oldTableOrName: Table | string,
661
        newTableName: string,
662
    ): Promise<void> {
663
        throw new TypeORMError(
×
664
            `Rename table queries are not supported by Spanner driver.`,
665
        )
666
    }
667

668
    /**
669
     * Creates a new column from the column in the table.
670
     */
671
    async addColumn(
672
        tableOrName: Table | string,
673
        column: TableColumn,
674
    ): Promise<void> {
675
        const table =
676
            tableOrName instanceof Table
×
677
                ? tableOrName
678
                : await this.getCachedTable(tableOrName)
679
        const clonedTable = table.clone()
×
680
        const upQueries: Query[] = []
×
681
        const downQueries: Query[] = []
×
682

683
        upQueries.push(
×
684
            new Query(
685
                `ALTER TABLE ${this.escapePath(
686
                    table,
687
                )} ADD ${this.buildCreateColumnSql(column)}`,
688
            ),
689
        )
690
        downQueries.push(
×
691
            new Query(
692
                `ALTER TABLE ${this.escapePath(
693
                    table,
694
                )} DROP COLUMN ${this.driver.escape(column.name)}`,
695
            ),
696
        )
697

698
        // create column index
699
        const columnIndex = clonedTable.indices.find(
×
700
            (index) =>
701
                index.columnNames.length === 1 &&
×
702
                index.columnNames[0] === column.name,
703
        )
704
        if (columnIndex) {
×
705
            upQueries.push(this.createIndexSql(table, columnIndex))
×
706
            downQueries.push(this.dropIndexSql(table, columnIndex))
×
707
        } else if (column.isUnique) {
×
708
            const uniqueIndex = new TableIndex({
×
709
                name: this.connection.namingStrategy.indexName(table, [
710
                    column.name,
711
                ]),
712
                columnNames: [column.name],
713
                isUnique: true,
714
            })
715
            clonedTable.indices.push(uniqueIndex)
×
716
            clonedTable.uniques.push(
×
717
                new TableUnique({
718
                    name: uniqueIndex.name,
719
                    columnNames: uniqueIndex.columnNames,
720
                }),
721
            )
722

723
            upQueries.push(this.createIndexSql(table, uniqueIndex))
×
724
            downQueries.push(this.dropIndexSql(table, uniqueIndex))
×
725
        }
726

727
        if (column.generatedType && column.asExpression) {
×
728
            const insertQuery = this.insertTypeormMetadataSql({
×
729
                table: table.name,
730
                type: MetadataTableType.GENERATED_COLUMN,
731
                name: column.name,
732
                value: column.asExpression,
733
            })
734

735
            const deleteQuery = this.deleteTypeormMetadataSql({
×
736
                table: table.name,
737
                type: MetadataTableType.GENERATED_COLUMN,
738
                name: column.name,
739
            })
740

741
            upQueries.push(insertQuery)
×
742
            downQueries.push(deleteQuery)
×
743
        }
744

745
        await this.executeQueries(upQueries, downQueries)
×
746

747
        clonedTable.addColumn(column)
×
748
        this.replaceCachedTable(table, clonedTable)
×
749
    }
750

751
    /**
752
     * Creates a new columns from the column in the table.
753
     */
754
    async addColumns(
755
        tableOrName: Table | string,
756
        columns: TableColumn[],
757
    ): Promise<void> {
758
        for (const column of columns) {
×
759
            await this.addColumn(tableOrName, column)
×
760
        }
761
    }
762

763
    /**
764
     * Renames column in the given table.
765
     */
766
    async renameColumn(
767
        tableOrName: Table | string,
768
        oldTableColumnOrName: TableColumn | string,
769
        newTableColumnOrName: TableColumn | string,
770
    ): Promise<void> {
771
        const table =
772
            tableOrName instanceof Table
×
773
                ? tableOrName
774
                : await this.getCachedTable(tableOrName)
775
        const oldColumn =
776
            oldTableColumnOrName instanceof TableColumn
×
777
                ? oldTableColumnOrName
778
                : table.columns.find((c) => c.name === oldTableColumnOrName)
×
779
        if (!oldColumn)
×
780
            throw new TypeORMError(
×
781
                `Column "${oldTableColumnOrName}" was not found in the "${table.name}" table.`,
782
            )
783

784
        let newColumn
785
        if (newTableColumnOrName instanceof TableColumn) {
×
786
            newColumn = newTableColumnOrName
×
787
        } else {
788
            newColumn = oldColumn.clone()
×
789
            newColumn.name = newTableColumnOrName
×
790
        }
791

792
        return this.changeColumn(table, oldColumn, newColumn)
×
793
    }
794

795
    /**
796
     * Changes a column in the table.
797
     */
798
    async changeColumn(
799
        tableOrName: Table | string,
800
        oldTableColumnOrName: TableColumn | string,
801
        newColumn: TableColumn,
802
    ): Promise<void> {
803
        const table =
804
            tableOrName instanceof Table
×
805
                ? tableOrName
806
                : await this.getCachedTable(tableOrName)
807
        let clonedTable = table.clone()
×
808
        const upQueries: Query[] = []
×
809
        const downQueries: Query[] = []
×
810

811
        const oldColumn =
812
            oldTableColumnOrName instanceof TableColumn
×
813
                ? oldTableColumnOrName
814
                : table.columns.find(
815
                      (column) => column.name === oldTableColumnOrName,
×
816
                  )
817
        if (!oldColumn)
×
818
            throw new TypeORMError(
×
819
                `Column "${oldTableColumnOrName}" was not found in the "${table.name}" table.`,
820
            )
821

822
        if (
×
823
            oldColumn.name !== newColumn.name ||
×
824
            oldColumn.type !== newColumn.type ||
825
            oldColumn.length !== newColumn.length ||
826
            oldColumn.isArray !== newColumn.isArray ||
827
            oldColumn.generatedType !== newColumn.generatedType ||
828
            oldColumn.asExpression !== newColumn.asExpression
829
        ) {
830
            // To avoid data conversion, we just recreate column
831
            await this.dropColumn(table, oldColumn)
×
832
            await this.addColumn(table, newColumn)
×
833

834
            // update cloned table
835
            clonedTable = table.clone()
×
836
        } else {
837
            if (
×
838
                newColumn.precision !== oldColumn.precision ||
×
839
                newColumn.scale !== oldColumn.scale
840
            ) {
841
                upQueries.push(
×
842
                    new Query(
843
                        `ALTER TABLE ${this.escapePath(table)} ALTER COLUMN "${
844
                            newColumn.name
845
                        }" TYPE ${this.driver.createFullType(newColumn)}`,
846
                    ),
847
                )
848
                downQueries.push(
×
849
                    new Query(
850
                        `ALTER TABLE ${this.escapePath(table)} ALTER COLUMN "${
851
                            newColumn.name
852
                        }" TYPE ${this.driver.createFullType(oldColumn)}`,
853
                    ),
854
                )
855
            }
856

857
            if (oldColumn.isNullable !== newColumn.isNullable) {
×
858
                if (newColumn.isNullable) {
×
859
                    upQueries.push(
×
860
                        new Query(
861
                            `ALTER TABLE ${this.escapePath(
862
                                table,
863
                            )} ALTER COLUMN "${oldColumn.name}" DROP NOT NULL`,
864
                        ),
865
                    )
866
                    downQueries.push(
×
867
                        new Query(
868
                            `ALTER TABLE ${this.escapePath(
869
                                table,
870
                            )} ALTER COLUMN "${oldColumn.name}" SET NOT NULL`,
871
                        ),
872
                    )
873
                } else {
874
                    upQueries.push(
×
875
                        new Query(
876
                            `ALTER TABLE ${this.escapePath(
877
                                table,
878
                            )} ALTER COLUMN "${oldColumn.name}" SET NOT NULL`,
879
                        ),
880
                    )
881
                    downQueries.push(
×
882
                        new Query(
883
                            `ALTER TABLE ${this.escapePath(
884
                                table,
885
                            )} ALTER COLUMN "${oldColumn.name}" DROP NOT NULL`,
886
                        ),
887
                    )
888
                }
889
            }
890

891
            if (newColumn.isUnique !== oldColumn.isUnique) {
×
892
                if (newColumn.isUnique === true) {
×
893
                    const uniqueIndex = new TableIndex({
×
894
                        name: this.connection.namingStrategy.indexName(table, [
895
                            newColumn.name,
896
                        ]),
897
                        columnNames: [newColumn.name],
898
                        isUnique: true,
899
                    })
900
                    clonedTable.indices.push(uniqueIndex)
×
901
                    clonedTable.uniques.push(
×
902
                        new TableUnique({
903
                            name: uniqueIndex.name,
904
                            columnNames: uniqueIndex.columnNames,
905
                        }),
906
                    )
907

908
                    upQueries.push(this.createIndexSql(table, uniqueIndex))
×
909
                    downQueries.push(this.dropIndexSql(table, uniqueIndex))
×
910
                } else {
911
                    const uniqueIndex = clonedTable.indices.find((index) => {
×
912
                        return (
×
913
                            index.columnNames.length === 1 &&
×
914
                            index.isUnique === true &&
915
                            !!index.columnNames.find(
916
                                (columnName) => columnName === newColumn.name,
×
917
                            )
918
                        )
919
                    })
920
                    clonedTable.indices.splice(
×
921
                        clonedTable.indices.indexOf(uniqueIndex!),
922
                        1,
923
                    )
924

925
                    const tableUnique = clonedTable.uniques.find(
×
926
                        (unique) => unique.name === uniqueIndex!.name,
×
927
                    )
928
                    clonedTable.uniques.splice(
×
929
                        clonedTable.uniques.indexOf(tableUnique!),
930
                        1,
931
                    )
932

933
                    upQueries.push(this.dropIndexSql(table, uniqueIndex!))
×
934
                    downQueries.push(this.createIndexSql(table, uniqueIndex!))
×
935
                }
936
            }
937
        }
938

939
        await this.executeQueries(upQueries, downQueries)
×
940
        this.replaceCachedTable(table, clonedTable)
×
941
    }
942

943
    /**
944
     * Changes a column in the table.
945
     */
946
    async changeColumns(
947
        tableOrName: Table | string,
948
        changedColumns: { newColumn: TableColumn; oldColumn: TableColumn }[],
949
    ): Promise<void> {
950
        for (const { oldColumn, newColumn } of changedColumns) {
×
951
            await this.changeColumn(tableOrName, oldColumn, newColumn)
×
952
        }
953
    }
954

955
    /**
956
     * Drops column in the table.
957
     */
958
    async dropColumn(
959
        tableOrName: Table | string,
960
        columnOrName: TableColumn | string,
961
    ): Promise<void> {
962
        const table =
963
            tableOrName instanceof Table
×
964
                ? tableOrName
965
                : await this.getCachedTable(tableOrName)
966
        const column =
967
            columnOrName instanceof TableColumn
×
968
                ? columnOrName
969
                : table.findColumnByName(columnOrName)
970
        if (!column)
×
971
            throw new TypeORMError(
×
972
                `Column "${columnOrName}" was not found in table "${table.name}"`,
973
            )
974

975
        const clonedTable = table.clone()
×
976
        const upQueries: Query[] = []
×
977
        const downQueries: Query[] = []
×
978

979
        // drop column index
980
        const columnIndex = clonedTable.indices.find(
×
981
            (index) =>
982
                index.columnNames.length === 1 &&
×
983
                index.columnNames[0] === column.name,
984
        )
985
        if (columnIndex) {
×
986
            clonedTable.indices.splice(
×
987
                clonedTable.indices.indexOf(columnIndex),
988
                1,
989
            )
990
            upQueries.push(this.dropIndexSql(table, columnIndex))
×
991
            downQueries.push(this.createIndexSql(table, columnIndex))
×
992
        }
993

994
        // drop column check
995
        const columnCheck = clonedTable.checks.find(
×
996
            (check) =>
997
                !!check.columnNames &&
×
998
                check.columnNames.length === 1 &&
999
                check.columnNames[0] === column.name,
1000
        )
1001
        if (columnCheck) {
×
1002
            clonedTable.checks.splice(
×
1003
                clonedTable.checks.indexOf(columnCheck),
1004
                1,
1005
            )
1006
            upQueries.push(this.dropCheckConstraintSql(table, columnCheck))
×
1007
            downQueries.push(this.createCheckConstraintSql(table, columnCheck))
×
1008
        }
1009

1010
        upQueries.push(
×
1011
            new Query(
1012
                `ALTER TABLE ${this.escapePath(
1013
                    table,
1014
                )} DROP COLUMN ${this.driver.escape(column.name)}`,
1015
            ),
1016
        )
1017
        downQueries.push(
×
1018
            new Query(
1019
                `ALTER TABLE ${this.escapePath(
1020
                    table,
1021
                )} ADD ${this.buildCreateColumnSql(column)}`,
1022
            ),
1023
        )
1024

1025
        if (column.generatedType && column.asExpression) {
×
1026
            const deleteQuery = this.deleteTypeormMetadataSql({
×
1027
                table: table.name,
1028
                type: MetadataTableType.GENERATED_COLUMN,
1029
                name: column.name,
1030
            })
1031
            const insertQuery = this.insertTypeormMetadataSql({
×
1032
                table: table.name,
1033
                type: MetadataTableType.GENERATED_COLUMN,
1034
                name: column.name,
1035
                value: column.asExpression,
1036
            })
1037

1038
            upQueries.push(deleteQuery)
×
1039
            downQueries.push(insertQuery)
×
1040
        }
1041

1042
        await this.executeQueries(upQueries, downQueries)
×
1043

1044
        clonedTable.removeColumn(column)
×
1045
        this.replaceCachedTable(table, clonedTable)
×
1046
    }
1047

1048
    /**
1049
     * Drops the columns in the table.
1050
     */
1051
    async dropColumns(
1052
        tableOrName: Table | string,
1053
        columns: TableColumn[] | string[],
1054
    ): Promise<void> {
1055
        for (const column of columns) {
×
1056
            await this.dropColumn(tableOrName, column)
×
1057
        }
1058
    }
1059

1060
    /**
1061
     * Creates a new primary key.
1062
     *
1063
     * Not supported in Spanner.
1064
     * @see https://cloud.google.com/spanner/docs/schema-and-data-model#notes_about_key_columns
1065
     */
1066
    async createPrimaryKey(
1067
        tableOrName: Table | string,
1068
        columnNames: string[],
1069
    ): Promise<void> {
1070
        throw new Error(
×
1071
            "The keys of a table can't change; you can't add a key column to an existing table or remove a key column from an existing table.",
1072
        )
1073
    }
1074

1075
    /**
1076
     * Updates composite primary keys.
1077
     */
1078
    async updatePrimaryKeys(
1079
        tableOrName: Table | string,
1080
        columns: TableColumn[],
1081
    ): Promise<void> {
1082
        throw new Error(
×
1083
            "The keys of a table can't change; you can't add a key column to an existing table or remove a key column from an existing table.",
1084
        )
1085
    }
1086

1087
    /**
1088
     * Creates a new primary key.
1089
     *
1090
     * Not supported in Spanner.
1091
     * @see https://cloud.google.com/spanner/docs/schema-and-data-model#notes_about_key_columns
1092
     */
1093
    async dropPrimaryKey(tableOrName: Table | string): Promise<void> {
1094
        throw new Error(
×
1095
            "The keys of a table can't change; you can't add a key column to an existing table or remove a key column from an existing table.",
1096
        )
1097
    }
1098

1099
    /**
1100
     * Creates new unique constraint.
1101
     */
1102
    async createUniqueConstraint(
1103
        tableOrName: Table | string,
1104
        uniqueConstraint: TableUnique,
1105
    ): Promise<void> {
1106
        throw new TypeORMError(
×
1107
            `Spanner does not support unique constraints. Use unique index instead.`,
1108
        )
1109
    }
1110

1111
    /**
1112
     * Creates new unique constraints.
1113
     */
1114
    async createUniqueConstraints(
1115
        tableOrName: Table | string,
1116
        uniqueConstraints: TableUnique[],
1117
    ): Promise<void> {
1118
        throw new TypeORMError(
×
1119
            `Spanner does not support unique constraints. Use unique index instead.`,
1120
        )
1121
    }
1122

1123
    /**
1124
     * Drops unique constraint.
1125
     */
1126
    async dropUniqueConstraint(
1127
        tableOrName: Table | string,
1128
        uniqueOrName: TableUnique | string,
1129
    ): Promise<void> {
1130
        throw new TypeORMError(
×
1131
            `Spanner does not support unique constraints. Use unique index instead.`,
1132
        )
1133
    }
1134

1135
    /**
1136
     * Drops unique constraints.
1137
     */
1138
    async dropUniqueConstraints(
1139
        tableOrName: Table | string,
1140
        uniqueConstraints: TableUnique[],
1141
    ): Promise<void> {
1142
        throw new TypeORMError(
×
1143
            `Spanner does not support unique constraints. Use unique index instead.`,
1144
        )
1145
    }
1146

1147
    /**
1148
     * Creates new check constraint.
1149
     */
1150
    async createCheckConstraint(
1151
        tableOrName: Table | string,
1152
        checkConstraint: TableCheck,
1153
    ): Promise<void> {
1154
        const table =
1155
            tableOrName instanceof Table
×
1156
                ? tableOrName
1157
                : await this.getCachedTable(tableOrName)
1158

1159
        // new check constraint may be passed without name. In this case we generate unique name manually.
1160
        if (!checkConstraint.name)
×
1161
            checkConstraint.name =
×
1162
                this.connection.namingStrategy.checkConstraintName(
1163
                    table,
1164
                    checkConstraint.expression!,
1165
                )
1166

1167
        const up = this.createCheckConstraintSql(table, checkConstraint)
×
1168
        const down = this.dropCheckConstraintSql(table, checkConstraint)
×
1169
        await this.executeQueries(up, down)
×
1170
        table.addCheckConstraint(checkConstraint)
×
1171
    }
1172

1173
    /**
1174
     * Creates new check constraints.
1175
     */
1176
    async createCheckConstraints(
1177
        tableOrName: Table | string,
1178
        checkConstraints: TableCheck[],
1179
    ): Promise<void> {
1180
        const promises = checkConstraints.map((checkConstraint) =>
×
1181
            this.createCheckConstraint(tableOrName, checkConstraint),
×
1182
        )
1183
        await Promise.all(promises)
×
1184
    }
1185

1186
    /**
1187
     * Drops check constraint.
1188
     */
1189
    async dropCheckConstraint(
1190
        tableOrName: Table | string,
1191
        checkOrName: TableCheck | string,
1192
    ): Promise<void> {
1193
        const table =
1194
            tableOrName instanceof Table
×
1195
                ? tableOrName
1196
                : await this.getCachedTable(tableOrName)
1197
        const checkConstraint =
1198
            checkOrName instanceof TableCheck
×
1199
                ? checkOrName
1200
                : table.checks.find((c) => c.name === checkOrName)
×
1201
        if (!checkConstraint)
×
1202
            throw new TypeORMError(
×
1203
                `Supplied check constraint was not found in table ${table.name}`,
1204
            )
1205

1206
        const up = this.dropCheckConstraintSql(table, checkConstraint)
×
1207
        const down = this.createCheckConstraintSql(table, checkConstraint)
×
1208
        await this.executeQueries(up, down)
×
1209
        table.removeCheckConstraint(checkConstraint)
×
1210
    }
1211

1212
    /**
1213
     * Drops check constraints.
1214
     */
1215
    async dropCheckConstraints(
1216
        tableOrName: Table | string,
1217
        checkConstraints: TableCheck[],
1218
    ): Promise<void> {
1219
        const promises = checkConstraints.map((checkConstraint) =>
×
1220
            this.dropCheckConstraint(tableOrName, checkConstraint),
×
1221
        )
1222
        await Promise.all(promises)
×
1223
    }
1224

1225
    /**
1226
     * Creates new exclusion constraint.
1227
     */
1228
    async createExclusionConstraint(
1229
        tableOrName: Table | string,
1230
        exclusionConstraint: TableExclusion,
1231
    ): Promise<void> {
1232
        throw new TypeORMError(
×
1233
            `Spanner does not support exclusion constraints.`,
1234
        )
1235
    }
1236

1237
    /**
1238
     * Creates new exclusion constraints.
1239
     */
1240
    async createExclusionConstraints(
1241
        tableOrName: Table | string,
1242
        exclusionConstraints: TableExclusion[],
1243
    ): Promise<void> {
1244
        throw new TypeORMError(
×
1245
            `Spanner does not support exclusion constraints.`,
1246
        )
1247
    }
1248

1249
    /**
1250
     * Drops exclusion constraint.
1251
     */
1252
    async dropExclusionConstraint(
1253
        tableOrName: Table | string,
1254
        exclusionOrName: TableExclusion | string,
1255
    ): Promise<void> {
1256
        throw new TypeORMError(
×
1257
            `Spanner does not support exclusion constraints.`,
1258
        )
1259
    }
1260

1261
    /**
1262
     * Drops exclusion constraints.
1263
     */
1264
    async dropExclusionConstraints(
1265
        tableOrName: Table | string,
1266
        exclusionConstraints: TableExclusion[],
1267
    ): Promise<void> {
1268
        throw new TypeORMError(
×
1269
            `Spanner does not support exclusion constraints.`,
1270
        )
1271
    }
1272

1273
    /**
1274
     * Creates a new foreign key.
1275
     */
1276
    async createForeignKey(
1277
        tableOrName: Table | string,
1278
        foreignKey: TableForeignKey,
1279
    ): Promise<void> {
1280
        const table =
1281
            tableOrName instanceof Table
×
1282
                ? tableOrName
1283
                : await this.getCachedTable(tableOrName)
1284

1285
        // new FK may be passed without name. In this case we generate FK name manually.
1286
        if (!foreignKey.name)
×
1287
            foreignKey.name = this.connection.namingStrategy.foreignKeyName(
×
1288
                table,
1289
                foreignKey.columnNames,
1290
                this.getTablePath(foreignKey),
1291
                foreignKey.referencedColumnNames,
1292
            )
1293

1294
        const up = this.createForeignKeySql(table, foreignKey)
×
1295
        const down = this.dropForeignKeySql(table, foreignKey)
×
1296
        await this.executeQueries(up, down)
×
1297
        table.addForeignKey(foreignKey)
×
1298
    }
1299

1300
    /**
1301
     * Creates a new foreign keys.
1302
     */
1303
    async createForeignKeys(
1304
        tableOrName: Table | string,
1305
        foreignKeys: TableForeignKey[],
1306
    ): Promise<void> {
1307
        for (const foreignKey of foreignKeys) {
×
1308
            await this.createForeignKey(tableOrName, foreignKey)
×
1309
        }
1310
    }
1311

1312
    /**
1313
     * Drops a foreign key from the table.
1314
     */
1315
    async dropForeignKey(
1316
        tableOrName: Table | string,
1317
        foreignKeyOrName: TableForeignKey | string,
1318
    ): Promise<void> {
1319
        const table =
1320
            tableOrName instanceof Table
×
1321
                ? tableOrName
1322
                : await this.getCachedTable(tableOrName)
1323
        const foreignKey =
1324
            foreignKeyOrName instanceof TableForeignKey
×
1325
                ? foreignKeyOrName
1326
                : table.foreignKeys.find((fk) => fk.name === foreignKeyOrName)
×
1327
        if (!foreignKey)
×
1328
            throw new TypeORMError(
×
1329
                `Supplied foreign key was not found in table ${table.name}`,
1330
            )
1331

1332
        const up = this.dropForeignKeySql(table, foreignKey)
×
1333
        const down = this.createForeignKeySql(table, foreignKey)
×
1334
        await this.executeQueries(up, down)
×
1335
        table.removeForeignKey(foreignKey)
×
1336
    }
1337

1338
    /**
1339
     * Drops a foreign keys from the table.
1340
     */
1341
    async dropForeignKeys(
1342
        tableOrName: Table | string,
1343
        foreignKeys: TableForeignKey[],
1344
    ): Promise<void> {
1345
        for (const foreignKey of foreignKeys) {
×
1346
            await this.dropForeignKey(tableOrName, foreignKey)
×
1347
        }
1348
    }
1349

1350
    /**
1351
     * Creates a new index.
1352
     */
1353
    async createIndex(
1354
        tableOrName: Table | string,
1355
        index: TableIndex,
1356
    ): Promise<void> {
1357
        const table =
1358
            tableOrName instanceof Table
×
1359
                ? tableOrName
1360
                : await this.getCachedTable(tableOrName)
1361

1362
        // new index may be passed without name. In this case we generate index name manually.
1363
        if (!index.name) index.name = this.generateIndexName(table, index)
×
1364

1365
        const up = this.createIndexSql(table, index)
×
1366
        const down = this.dropIndexSql(table, index)
×
1367
        await this.executeQueries(up, down)
×
1368
        table.addIndex(index)
×
1369
    }
1370

1371
    /**
1372
     * Creates a new indices
1373
     */
1374
    async createIndices(
1375
        tableOrName: Table | string,
1376
        indices: TableIndex[],
1377
    ): Promise<void> {
1378
        for (const index of indices) {
×
1379
            await this.createIndex(tableOrName, index)
×
1380
        }
1381
    }
1382

1383
    /**
1384
     * Drops an index from the table.
1385
     */
1386
    async dropIndex(
1387
        tableOrName: Table | string,
1388
        indexOrName: TableIndex | string,
1389
    ): Promise<void> {
1390
        const table =
1391
            tableOrName instanceof Table
×
1392
                ? tableOrName
1393
                : await this.getCachedTable(tableOrName)
1394
        const index =
1395
            indexOrName instanceof TableIndex
×
1396
                ? indexOrName
1397
                : table.indices.find((i) => i.name === indexOrName)
×
1398
        if (!index)
×
1399
            throw new TypeORMError(
×
1400
                `Supplied index ${indexOrName} was not found in table ${table.name}`,
1401
            )
1402

1403
        // new index may be passed without name. In this case we generate index name manually.
1404
        if (!index.name) index.name = this.generateIndexName(table, index)
×
1405

1406
        const up = this.dropIndexSql(table, index)
×
1407
        const down = this.createIndexSql(table, index)
×
1408
        await this.executeQueries(up, down)
×
1409
        table.removeIndex(index)
×
1410
    }
1411

1412
    /**
1413
     * Drops an indices from the table.
1414
     */
1415
    async dropIndices(
1416
        tableOrName: Table | string,
1417
        indices: TableIndex[],
1418
    ): Promise<void> {
1419
        for (const index of indices) {
×
1420
            await this.dropIndex(tableOrName, index)
×
1421
        }
1422
    }
1423

1424
    /**
1425
     * Clears all table contents.
1426
     * Spanner does not support TRUNCATE TABLE statement, so we use DELETE FROM.
1427
     */
1428
    async clearTable(tableName: string): Promise<void> {
1429
        await this.query(`DELETE FROM ${this.escapePath(tableName)} WHERE true`)
×
1430
    }
1431

1432
    /**
1433
     * Removes all tables from the currently connected database.
1434
     */
1435
    async clearDatabase(): Promise<void> {
1436
        // drop index queries
1437
        const selectIndexDropsQuery =
1438
            `SELECT concat('DROP INDEX \`', INDEX_NAME, '\`') AS \`query\` ` +
×
1439
            `FROM \`INFORMATION_SCHEMA\`.\`INDEXES\` ` +
1440
            `WHERE \`TABLE_CATALOG\` = '' AND \`TABLE_SCHEMA\` = '' AND \`INDEX_TYPE\` = 'INDEX' AND \`SPANNER_IS_MANAGED\` = false`
1441
        const dropIndexQueries: ObjectLiteral[] = await this.query(
×
1442
            selectIndexDropsQuery,
1443
        )
1444

1445
        // drop foreign key queries
1446
        const selectFKDropsQuery =
1447
            `SELECT concat('ALTER TABLE \`', TABLE_NAME, '\`', ' DROP CONSTRAINT \`', CONSTRAINT_NAME, '\`') AS \`query\` ` +
×
1448
            `FROM \`INFORMATION_SCHEMA\`.\`TABLE_CONSTRAINTS\` ` +
1449
            `WHERE \`TABLE_CATALOG\` = '' AND \`TABLE_SCHEMA\` = '' AND \`CONSTRAINT_TYPE\` = 'FOREIGN KEY'`
1450
        const dropFKQueries: ObjectLiteral[] = await this.query(
×
1451
            selectFKDropsQuery,
1452
        )
1453

1454
        // drop view queries
1455
        // const selectViewDropsQuery = `SELECT concat('DROP VIEW \`', TABLE_NAME, '\`') AS \`query\` FROM \`INFORMATION_SCHEMA\`.\`VIEWS\``
1456
        // const dropViewQueries: ObjectLiteral[] = await this.query(
1457
        //     selectViewDropsQuery,
1458
        // )
1459

1460
        // drop table queries
1461
        const dropTablesQuery =
1462
            `SELECT concat('DROP TABLE \`', TABLE_NAME, '\`') AS \`query\` ` +
×
1463
            `FROM \`INFORMATION_SCHEMA\`.\`TABLES\` ` +
1464
            `WHERE \`TABLE_CATALOG\` = '' AND \`TABLE_SCHEMA\` = '' AND \`TABLE_TYPE\` = 'BASE TABLE'`
1465
        const dropTableQueries: ObjectLiteral[] = await this.query(
×
1466
            dropTablesQuery,
1467
        )
1468

1469
        if (
×
1470
            !dropIndexQueries.length &&
×
1471
            !dropFKQueries.length &&
1472
            // !dropViewQueries.length &&
1473
            !dropTableQueries.length
1474
        )
1475
            return
×
1476

1477
        const isAnotherTransactionActive = this.isTransactionActive
×
1478
        if (!isAnotherTransactionActive) await this.startTransaction()
×
1479
        try {
×
1480
            for (const query of dropIndexQueries) {
×
1481
                await this.updateDDL(query["query"])
×
1482
            }
1483
            for (const query of dropFKQueries) {
×
1484
                await this.updateDDL(query["query"])
×
1485
            }
1486

1487
            // for (let query of dropViewQueries) {
1488
            //     await this.updateDDL(query["query"])
1489
            // }
1490

1491
            for (const query of dropTableQueries) {
×
1492
                await this.updateDDL(query["query"])
×
1493
            }
1494

1495
            await this.commitTransaction()
×
1496
        } catch (error) {
1497
            try {
×
1498
                // we throw original error even if rollback thrown an error
1499
                if (!isAnotherTransactionActive)
×
1500
                    await this.rollbackTransaction()
×
1501
            } catch (rollbackError) {}
1502
            throw error
×
1503
        }
1504
    }
1505

1506
    // -------------------------------------------------------------------------
1507
    // Override Methods
1508
    // -------------------------------------------------------------------------
1509

1510
    /**
1511
     * Executes up sql queries.
1512
     */
1513
    async executeMemoryUpSql(): Promise<void> {
1514
        for (const { query, parameters } of this.sqlInMemory.upQueries) {
×
1515
            if (this.isDMLQuery(query)) {
×
1516
                await this.query(query, parameters)
×
1517
            } else {
1518
                await this.updateDDL(query, parameters)
×
1519
            }
1520
        }
1521
    }
1522

1523
    /**
1524
     * Executes down sql queries.
1525
     */
1526
    async executeMemoryDownSql(): Promise<void> {
1527
        for (const {
×
1528
            query,
1529
            parameters,
1530
        } of this.sqlInMemory.downQueries.reverse()) {
1531
            if (this.isDMLQuery(query)) {
×
1532
                await this.query(query, parameters)
×
1533
            } else {
1534
                await this.updateDDL(query, parameters)
×
1535
            }
1536
        }
1537
    }
1538

1539
    // -------------------------------------------------------------------------
1540
    // Protected Methods
1541
    // -------------------------------------------------------------------------
1542

1543
    protected async loadViews(viewNames?: string[]): Promise<View[]> {
1544
        // const hasTable = await this.hasTable(this.getTypeormMetadataTableName())
1545
        // if (!hasTable) {
1546
        //     return []
1547
        // }
1548
        //
1549
        // if (!viewNames) {
1550
        //     viewNames = []
1551
        // }
1552
        //
1553
        // const escapedViewNames = viewNames
1554
        //     .map((viewName) => `'${viewName}'`)
1555
        //     .join(", ")
1556
        //
1557
        // const query =
1558
        //     `SELECT \`T\`.*, \`V\`.\`VIEW_DEFINITION\` FROM ${this.escapePath(
1559
        //         this.getTypeormMetadataTableName(),
1560
        //     )} \`T\` ` +
1561
        //     `INNER JOIN \`INFORMATION_SCHEMA\`.\`VIEWS\` \`V\` ON \`V\`.\`TABLE_NAME\` = \`T\`.\`NAME\` ` +
1562
        //     `WHERE \`T\`.\`TYPE\` = '${MetadataTableType.VIEW}' ${
1563
        //         viewNames.length
1564
        //             ? ` AND \`T\`.\`NAME\` IN (${escapedViewNames})`
1565
        //             : ""
1566
        //     }`
1567
        // const dbViews = await this.query(query)
1568
        // return dbViews.map((dbView: any) => {
1569
        //     const view = new View()
1570
        //     view.database = dbView["NAME"]
1571
        //     view.name = this.driver.buildTableName(dbView["NAME"])
1572
        //     view.expression = dbView["NAME"]
1573
        //     return view
1574
        // })
1575

1576
        return Promise.resolve([])
×
1577
    }
1578

1579
    /**
1580
     * Loads all tables (with given names) from the database and creates a Table from them.
1581
     */
1582
    protected async loadTables(tableNames?: string[]): Promise<Table[]> {
1583
        if (tableNames && tableNames.length === 0) {
×
1584
            return []
×
1585
        }
1586

1587
        const dbTables: { TABLE_NAME: string }[] = []
×
1588

1589
        if (!tableNames || !tableNames.length) {
×
1590
            // Since we don't have any of this data we have to do a scan
1591
            const tablesSql =
1592
                `SELECT \`TABLE_NAME\` ` +
×
1593
                `FROM \`INFORMATION_SCHEMA\`.\`TABLES\` ` +
1594
                `WHERE \`TABLE_CATALOG\` = '' AND \`TABLE_SCHEMA\` = '' AND \`TABLE_TYPE\` = 'BASE TABLE'`
1595
            dbTables.push(...(await this.query(tablesSql)))
×
1596
        } else {
1597
            const tablesSql =
1598
                `SELECT \`TABLE_NAME\` ` +
×
1599
                `FROM \`INFORMATION_SCHEMA\`.\`TABLES\` ` +
1600
                `WHERE \`TABLE_CATALOG\` = '' AND \`TABLE_SCHEMA\` = '' AND \`TABLE_TYPE\` = 'BASE TABLE' ` +
1601
                `AND \`TABLE_NAME\` IN (${tableNames
1602
                    .map((tableName) => `'${tableName}'`)
×
1603
                    .join(", ")})`
1604

1605
            dbTables.push(...(await this.query(tablesSql)))
×
1606
        }
1607

1608
        // if tables were not found in the db, no need to proceed
1609
        if (!dbTables.length) return []
×
1610

1611
        const loadedTableNames = dbTables
×
1612
            .map((dbTable) => `'${dbTable.TABLE_NAME}'`)
×
1613
            .join(", ")
1614

1615
        const columnsSql = `SELECT * FROM \`INFORMATION_SCHEMA\`.\`COLUMNS\` WHERE \`TABLE_CATALOG\` = '' AND \`TABLE_SCHEMA\` = '' AND \`TABLE_NAME\` IN (${loadedTableNames})`
×
1616

1617
        const primaryKeySql =
1618
            `SELECT \`KCU\`.\`TABLE_NAME\`, \`KCU\`.\`COLUMN_NAME\` ` +
×
1619
            `FROM \`INFORMATION_SCHEMA\`.\`TABLE_CONSTRAINTS\` \`TC\` ` +
1620
            `INNER JOIN \`INFORMATION_SCHEMA\`.\`KEY_COLUMN_USAGE\` \`KCU\` ON \`KCU\`.\`CONSTRAINT_NAME\` = \`TC\`.\`CONSTRAINT_NAME\` ` +
1621
            `WHERE \`TC\`.\`TABLE_CATALOG\` = '' AND \`TC\`.\`TABLE_SCHEMA\` = '' AND \`TC\`.\`CONSTRAINT_TYPE\` = 'PRIMARY KEY' ` +
1622
            `AND \`TC\`.\`TABLE_NAME\` IN (${loadedTableNames})`
1623

1624
        const indicesSql =
1625
            `SELECT \`I\`.\`TABLE_NAME\`, \`I\`.\`INDEX_NAME\`, \`I\`.\`IS_UNIQUE\`, \`I\`.\`IS_NULL_FILTERED\`, \`IC\`.\`COLUMN_NAME\` ` +
×
1626
            `FROM \`INFORMATION_SCHEMA\`.\`INDEXES\` \`I\` ` +
1627
            `INNER JOIN \`INFORMATION_SCHEMA\`.\`INDEX_COLUMNS\` \`IC\` ON \`IC\`.\`INDEX_NAME\` = \`I\`.\`INDEX_NAME\` ` +
1628
            `AND \`IC\`.\`TABLE_NAME\` = \`I\`.\`TABLE_NAME\` ` +
1629
            `WHERE \`I\`.\`TABLE_CATALOG\` = '' AND \`I\`.\`TABLE_SCHEMA\` = '' AND \`I\`.\`TABLE_NAME\` IN (${loadedTableNames}) ` +
1630
            `AND \`I\`.\`INDEX_TYPE\` = 'INDEX' AND \`I\`.\`SPANNER_IS_MANAGED\` = false`
1631

1632
        const checksSql =
1633
            `SELECT \`TC\`.\`TABLE_NAME\`, \`TC\`.\`CONSTRAINT_NAME\`, \`CC\`.\`CHECK_CLAUSE\`, \`CCU\`.\`COLUMN_NAME\`` +
×
1634
            `FROM \`INFORMATION_SCHEMA\`.\`TABLE_CONSTRAINTS\` \`TC\` ` +
1635
            `INNER JOIN \`INFORMATION_SCHEMA\`.\`CONSTRAINT_COLUMN_USAGE\` \`CCU\` ON \`CCU\`.\`CONSTRAINT_NAME\` = \`TC\`.\`CONSTRAINT_NAME\` ` +
1636
            `INNER JOIN \`INFORMATION_SCHEMA\`.\`CHECK_CONSTRAINTS\` \`CC\` ON \`CC\`.\`CONSTRAINT_NAME\` = \`TC\`.\`CONSTRAINT_NAME\` ` +
1637
            `WHERE \`TC\`.\`TABLE_CATALOG\` = '' AND \`TC\`.\`TABLE_SCHEMA\` = '' AND \`TC\`.\`CONSTRAINT_TYPE\` = 'CHECK' ` +
1638
            `AND \`TC\`.\`TABLE_NAME\` IN (${loadedTableNames}) AND \`TC\`.\`CONSTRAINT_NAME\` NOT LIKE 'CK_IS_NOT_NULL%'`
1639

1640
        const foreignKeysSql =
1641
            `SELECT \`TC\`.\`TABLE_NAME\`, \`TC\`.\`CONSTRAINT_NAME\`, \`KCU\`.\`COLUMN_NAME\`, ` +
×
1642
            `\`CTU\`.\`TABLE_NAME\` AS \`REFERENCED_TABLE_NAME\`, \`CCU\`.\`COLUMN_NAME\` AS \`REFERENCED_COLUMN_NAME\`, ` +
1643
            `\`RC\`.\`UPDATE_RULE\`, \`RC\`.\`DELETE_RULE\` ` +
1644
            `FROM \`INFORMATION_SCHEMA\`.\`TABLE_CONSTRAINTS\` \`TC\` ` +
1645
            `INNER JOIN \`INFORMATION_SCHEMA\`.\`KEY_COLUMN_USAGE\` \`KCU\` ON \`KCU\`.\`CONSTRAINT_NAME\` = \`TC\`.\`CONSTRAINT_NAME\` ` +
1646
            `INNER JOIN \`INFORMATION_SCHEMA\`.\`CONSTRAINT_TABLE_USAGE\` \`CTU\` ON \`CTU\`.\`CONSTRAINT_NAME\` = \`TC\`.\`CONSTRAINT_NAME\` ` +
1647
            `INNER JOIN \`INFORMATION_SCHEMA\`.\`REFERENTIAL_CONSTRAINTS\` \`RC\` ON \`RC\`.\`CONSTRAINT_NAME\` = \`TC\`.\`CONSTRAINT_NAME\` ` +
1648
            `INNER JOIN \`INFORMATION_SCHEMA\`.\`CONSTRAINT_COLUMN_USAGE\` \`CCU\` ON \`CCU\`.\`CONSTRAINT_NAME\` = \`TC\`.\`CONSTRAINT_NAME\` ` +
1649
            `WHERE \`TC\`.\`TABLE_CATALOG\` = '' AND \`TC\`.\`TABLE_SCHEMA\` = '' AND \`TC\`.\`CONSTRAINT_TYPE\` = 'FOREIGN KEY' ` +
1650
            `AND \`TC\`.\`TABLE_NAME\` IN (${loadedTableNames})`
1651

1652
        const [
1653
            dbColumns,
1654
            dbPrimaryKeys,
1655
            dbIndices,
1656
            dbChecks,
1657
            dbForeignKeys,
1658
        ]: ObjectLiteral[][] = await Promise.all([
×
1659
            this.query(columnsSql),
1660
            this.query(primaryKeySql),
1661
            this.query(indicesSql),
1662
            this.query(checksSql),
1663
            this.query(foreignKeysSql),
1664
        ])
1665

1666
        // create tables for loaded tables
1667
        return Promise.all(
×
1668
            dbTables.map(async (dbTable) => {
1669
                const table = new Table()
×
1670

1671
                table.name = this.driver.buildTableName(dbTable["TABLE_NAME"])
×
1672

1673
                // create columns from the loaded columns
1674
                table.columns = await Promise.all(
×
1675
                    dbColumns
1676
                        .filter(
1677
                            (dbColumn) =>
1678
                                dbColumn["TABLE_NAME"] ===
×
1679
                                dbTable["TABLE_NAME"],
1680
                        )
1681
                        .map(async (dbColumn) => {
1682
                            const columnUniqueIndices = dbIndices.filter(
×
1683
                                (dbIndex) => {
1684
                                    return (
×
1685
                                        dbIndex["TABLE_NAME"] ===
×
1686
                                            dbTable["TABLE_NAME"] &&
1687
                                        dbIndex["COLUMN_NAME"] ===
1688
                                            dbColumn["COLUMN_NAME"] &&
1689
                                        dbIndex["IS_UNIQUE"] === true
1690
                                    )
1691
                                },
1692
                            )
1693

1694
                            const tableMetadata =
1695
                                this.connection.entityMetadatas.find(
×
1696
                                    (metadata) =>
1697
                                        this.getTablePath(table) ===
×
1698
                                        this.getTablePath(metadata),
1699
                                )
1700
                            const hasIgnoredIndex =
1701
                                columnUniqueIndices.length > 0 &&
×
1702
                                tableMetadata &&
1703
                                tableMetadata.indices.some((index) => {
1704
                                    return columnUniqueIndices.some(
×
1705
                                        (uniqueIndex) => {
1706
                                            return (
×
1707
                                                index.name ===
×
1708
                                                    uniqueIndex["INDEX_NAME"] &&
1709
                                                index.synchronize === false
1710
                                            )
1711
                                        },
1712
                                    )
1713
                                })
1714

1715
                            const isConstraintComposite =
1716
                                columnUniqueIndices.every((uniqueIndex) => {
×
1717
                                    return dbIndices.some(
×
1718
                                        (dbIndex) =>
1719
                                            dbIndex["INDEX_NAME"] ===
×
1720
                                                uniqueIndex["INDEX_NAME"] &&
1721
                                            dbIndex["COLUMN_NAME"] !==
1722
                                                dbColumn["COLUMN_NAME"],
1723
                                    )
1724
                                })
1725

1726
                            const tableColumn = new TableColumn()
×
1727
                            tableColumn.name = dbColumn["COLUMN_NAME"]
×
1728

1729
                            let fullType =
1730
                                dbColumn["SPANNER_TYPE"].toLowerCase()
×
1731
                            if (fullType.indexOf("array") !== -1) {
×
1732
                                tableColumn.isArray = true
×
1733
                                fullType = fullType.substring(
×
1734
                                    fullType.indexOf("<") + 1,
1735
                                    fullType.indexOf(">"),
1736
                                )
1737
                            }
1738

1739
                            if (fullType.indexOf("(") !== -1) {
×
1740
                                tableColumn.type = fullType.substring(
×
1741
                                    0,
1742
                                    fullType.indexOf("("),
1743
                                )
1744
                            } else {
1745
                                tableColumn.type = fullType
×
1746
                            }
1747

1748
                            if (
×
1749
                                this.driver.withLengthColumnTypes.indexOf(
1750
                                    tableColumn.type as ColumnType,
1751
                                ) !== -1
1752
                            ) {
1753
                                tableColumn.length = fullType.substring(
×
1754
                                    fullType.indexOf("(") + 1,
1755
                                    fullType.indexOf(")"),
1756
                                )
1757
                            }
1758

1759
                            if (dbColumn["IS_GENERATED"] === "ALWAYS") {
×
1760
                                tableColumn.asExpression =
×
1761
                                    dbColumn["GENERATION_EXPRESSION"]
1762
                                tableColumn.generatedType = "STORED"
×
1763

1764
                                // We cannot relay on information_schema.columns.generation_expression, because it is formatted different.
1765
                                const asExpressionQuery =
1766
                                    this.selectTypeormMetadataSql({
×
1767
                                        table: dbTable["TABLE_NAME"],
1768
                                        type: MetadataTableType.GENERATED_COLUMN,
1769
                                        name: tableColumn.name,
1770
                                    })
1771

1772
                                const results = await this.query(
×
1773
                                    asExpressionQuery.query,
1774
                                    asExpressionQuery.parameters,
1775
                                )
1776

1777
                                if (results[0] && results[0].value) {
×
1778
                                    tableColumn.asExpression = results[0].value
×
1779
                                } else {
1780
                                    tableColumn.asExpression = ""
×
1781
                                }
1782
                            }
1783

1784
                            tableColumn.isUnique =
×
1785
                                columnUniqueIndices.length > 0 &&
×
1786
                                !hasIgnoredIndex &&
1787
                                !isConstraintComposite
1788
                            tableColumn.isNullable =
×
1789
                                dbColumn["IS_NULLABLE"] === "YES"
1790
                            tableColumn.isPrimary = dbPrimaryKeys.some(
×
1791
                                (dbPrimaryKey) => {
1792
                                    return (
×
1793
                                        dbPrimaryKey["TABLE_NAME"] ===
×
1794
                                            dbColumn["TABLE_NAME"] &&
1795
                                        dbPrimaryKey["COLUMN_NAME"] ===
1796
                                            dbColumn["COLUMN_NAME"]
1797
                                    )
1798
                                },
1799
                            )
1800

1801
                            return tableColumn
×
1802
                        }),
1803
                )
1804

1805
                const tableForeignKeys = dbForeignKeys.filter(
×
1806
                    (dbForeignKey) => {
1807
                        return (
×
1808
                            dbForeignKey["TABLE_NAME"] === dbTable["TABLE_NAME"]
1809
                        )
1810
                    },
1811
                )
1812

1813
                table.foreignKeys = OrmUtils.uniq(
×
1814
                    tableForeignKeys,
1815
                    (dbForeignKey) => dbForeignKey["CONSTRAINT_NAME"],
×
1816
                ).map((dbForeignKey) => {
1817
                    const foreignKeys = tableForeignKeys.filter(
×
1818
                        (dbFk) =>
1819
                            dbFk["CONSTRAINT_NAME"] ===
×
1820
                            dbForeignKey["CONSTRAINT_NAME"],
1821
                    )
1822
                    return new TableForeignKey({
×
1823
                        name: dbForeignKey["CONSTRAINT_NAME"],
1824
                        columnNames: OrmUtils.uniq(
1825
                            foreignKeys.map((dbFk) => dbFk["COLUMN_NAME"]),
×
1826
                        ),
1827
                        referencedDatabase:
1828
                            dbForeignKey["REFERENCED_TABLE_SCHEMA"],
1829
                        referencedTableName:
1830
                            dbForeignKey["REFERENCED_TABLE_NAME"],
1831
                        referencedColumnNames: OrmUtils.uniq(
1832
                            foreignKeys.map(
1833
                                (dbFk) => dbFk["REFERENCED_COLUMN_NAME"],
×
1834
                            ),
1835
                        ),
1836
                        onDelete: dbForeignKey["DELETE_RULE"],
1837
                        onUpdate: dbForeignKey["UPDATE_RULE"],
1838
                    })
1839
                })
1840

1841
                const tableIndices = dbIndices.filter(
×
1842
                    (dbIndex) =>
1843
                        dbIndex["TABLE_NAME"] === dbTable["TABLE_NAME"],
×
1844
                )
1845

1846
                table.indices = OrmUtils.uniq(
×
1847
                    tableIndices,
1848
                    (dbIndex) => dbIndex["INDEX_NAME"],
×
1849
                ).map((constraint) => {
1850
                    const indices = tableIndices.filter((index) => {
×
1851
                        return index["INDEX_NAME"] === constraint["INDEX_NAME"]
×
1852
                    })
1853

1854
                    return new TableIndex(<TableIndexOptions>{
×
1855
                        table: table,
1856
                        name: constraint["INDEX_NAME"],
1857
                        columnNames: indices.map((i) => i["COLUMN_NAME"]),
×
1858
                        isUnique: constraint["IS_UNIQUE"],
1859
                        isNullFiltered: constraint["IS_NULL_FILTERED"],
1860
                    })
1861
                })
1862

1863
                const tableChecks = dbChecks.filter(
×
1864
                    (dbCheck) =>
1865
                        dbCheck["TABLE_NAME"] === dbTable["TABLE_NAME"],
×
1866
                )
1867

1868
                table.checks = OrmUtils.uniq(
×
1869
                    tableChecks,
1870
                    (dbIndex) => dbIndex["CONSTRAINT_NAME"],
×
1871
                ).map((constraint) => {
1872
                    const checks = tableChecks.filter(
×
1873
                        (dbC) =>
1874
                            dbC["CONSTRAINT_NAME"] ===
×
1875
                            constraint["CONSTRAINT_NAME"],
1876
                    )
1877
                    return new TableCheck({
×
1878
                        name: constraint["CONSTRAINT_NAME"],
1879
                        columnNames: checks.map((c) => c["COLUMN_NAME"]),
×
1880
                        expression: constraint["CHECK_CLAUSE"],
1881
                    })
1882
                })
1883

1884
                return table
×
1885
            }),
1886
        )
1887
    }
1888

1889
    /**
1890
     * Builds create table sql.
1891
     */
1892
    protected createTableSql(table: Table, createForeignKeys?: boolean): Query {
1893
        const columnDefinitions = table.columns
×
1894
            .map((column) => this.buildCreateColumnSql(column))
×
1895
            .join(", ")
1896
        let sql = `CREATE TABLE ${this.escapePath(table)} (${columnDefinitions}`
×
1897

1898
        // we create unique indexes instead of unique constraints, because Spanner does not have unique constraints.
1899
        // if we mark column as Unique, it means that we create UNIQUE INDEX.
1900
        table.columns
×
1901
            .filter((column) => column.isUnique)
×
1902
            .forEach((column) => {
1903
                const isUniqueIndexExist = table.indices.some((index) => {
×
1904
                    return (
×
1905
                        index.columnNames.length === 1 &&
×
1906
                        !!index.isUnique &&
1907
                        index.columnNames.indexOf(column.name) !== -1
1908
                    )
1909
                })
1910
                const isUniqueConstraintExist = table.uniques.some((unique) => {
×
1911
                    return (
×
1912
                        unique.columnNames.length === 1 &&
×
1913
                        unique.columnNames.indexOf(column.name) !== -1
1914
                    )
1915
                })
1916
                if (!isUniqueIndexExist && !isUniqueConstraintExist)
×
1917
                    table.indices.push(
×
1918
                        new TableIndex({
1919
                            name: this.connection.namingStrategy.uniqueConstraintName(
1920
                                table,
1921
                                [column.name],
1922
                            ),
1923
                            columnNames: [column.name],
1924
                            isUnique: true,
1925
                        }),
1926
                    )
1927
            })
1928

1929
        // as Spanner does not have unique constraints, we must create table indices from table uniques and mark them as unique.
1930
        if (table.uniques.length > 0) {
×
1931
            table.uniques.forEach((unique) => {
×
1932
                const uniqueExist = table.indices.some(
×
1933
                    (index) => index.name === unique.name,
×
1934
                )
1935
                if (!uniqueExist) {
×
1936
                    table.indices.push(
×
1937
                        new TableIndex({
1938
                            name: unique.name,
1939
                            columnNames: unique.columnNames,
1940
                            isUnique: true,
1941
                        }),
1942
                    )
1943
                }
1944
            })
1945
        }
1946

1947
        if (table.checks.length > 0) {
×
1948
            const checksSql = table.checks
×
1949
                .map((check) => {
1950
                    const checkName = check.name
×
1951
                        ? check.name
1952
                        : this.connection.namingStrategy.checkConstraintName(
1953
                              table,
1954
                              check.expression!,
1955
                          )
1956
                    return `CONSTRAINT \`${checkName}\` CHECK (${check.expression})`
×
1957
                })
1958
                .join(", ")
1959

1960
            sql += `, ${checksSql}`
×
1961
        }
1962

1963
        if (table.foreignKeys.length > 0 && createForeignKeys) {
×
1964
            const foreignKeysSql = table.foreignKeys
×
1965
                .map((fk) => {
1966
                    const columnNames = fk.columnNames
×
1967
                        .map((columnName) => `\`${columnName}\``)
×
1968
                        .join(", ")
1969
                    if (!fk.name)
×
1970
                        fk.name = this.connection.namingStrategy.foreignKeyName(
×
1971
                            table,
1972
                            fk.columnNames,
1973
                            this.getTablePath(fk),
1974
                            fk.referencedColumnNames,
1975
                        )
1976
                    const referencedColumnNames = fk.referencedColumnNames
×
1977
                        .map((columnName) => `\`${columnName}\``)
×
1978
                        .join(", ")
1979

1980
                    return `CONSTRAINT \`${
×
1981
                        fk.name
1982
                    }\` FOREIGN KEY (${columnNames}) REFERENCES ${this.escapePath(
1983
                        this.getTablePath(fk),
1984
                    )} (${referencedColumnNames})`
1985
                })
1986
                .join(", ")
1987

1988
            sql += `, ${foreignKeysSql}`
×
1989
        }
1990

1991
        sql += `)`
×
1992

1993
        const primaryColumns = table.columns.filter(
×
1994
            (column) => column.isPrimary,
×
1995
        )
1996
        if (primaryColumns.length > 0) {
×
1997
            const columnNames = primaryColumns
×
1998
                .map((column) => this.driver.escape(column.name))
×
1999
                .join(", ")
2000
            sql += ` PRIMARY KEY (${columnNames})`
×
2001
        }
2002

2003
        return new Query(sql)
×
2004
    }
2005

2006
    /**
2007
     * Builds drop table sql.
2008
     */
2009
    protected dropTableSql(tableOrPath: Table | string): Query {
2010
        return new Query(`DROP TABLE ${this.escapePath(tableOrPath)}`)
×
2011
    }
2012

2013
    protected createViewSql(view: View): Query {
2014
        const materializedClause = view.materialized ? "MATERIALIZED " : ""
×
2015
        const viewName = this.escapePath(view)
×
2016

2017
        const expression =
2018
            typeof view.expression === "string"
×
2019
                ? view.expression
2020
                : view.expression(this.connection).getQuery()
2021
        return new Query(
×
2022
            `CREATE ${materializedClause}VIEW ${viewName} SQL SECURITY INVOKER AS ${expression}`,
2023
        )
2024
    }
2025

2026
    protected async insertViewDefinitionSql(view: View): Promise<Query> {
2027
        const { schema, tableName: name } = this.driver.parseTableName(view)
×
2028

2029
        const type = view.materialized
×
2030
            ? MetadataTableType.MATERIALIZED_VIEW
2031
            : MetadataTableType.VIEW
2032
        const expression =
2033
            typeof view.expression === "string"
×
2034
                ? view.expression.trim()
2035
                : view.expression(this.connection).getQuery()
2036
        return this.insertTypeormMetadataSql({
×
2037
            type,
2038
            schema,
2039
            name,
2040
            value: expression,
2041
        })
2042
    }
2043

2044
    /**
2045
     * Builds drop view sql.
2046
     */
2047
    protected dropViewSql(view: View): Query {
2048
        const materializedClause = view.materialized ? "MATERIALIZED " : ""
×
2049
        return new Query(
×
2050
            `DROP ${materializedClause}VIEW ${this.escapePath(view)}`,
2051
        )
2052
    }
2053

2054
    /**
2055
     * Builds remove view sql.
2056
     */
2057
    protected async deleteViewDefinitionSql(view: View): Promise<Query> {
2058
        const { schema, tableName: name } = this.driver.parseTableName(view)
×
2059

2060
        const type = view.materialized
×
2061
            ? MetadataTableType.MATERIALIZED_VIEW
2062
            : MetadataTableType.VIEW
2063
        return this.deleteTypeormMetadataSql({ type, schema, name })
×
2064
    }
2065

2066
    /**
2067
     * Builds create index sql.
2068
     */
2069
    protected createIndexSql(table: Table, index: TableIndex): Query {
2070
        const columns = index.columnNames
×
2071
            .map((columnName) => this.driver.escape(columnName))
×
2072
            .join(", ")
2073
        let indexType = ""
×
2074
        if (index.isUnique) indexType += "UNIQUE "
×
2075
        if (index.isNullFiltered) indexType += "NULL_FILTERED "
×
2076

2077
        return new Query(
×
2078
            `CREATE ${indexType}INDEX \`${index.name}\` ON ${this.escapePath(
2079
                table,
2080
            )} (${columns})`,
2081
        )
2082
    }
2083

2084
    /**
2085
     * Builds drop index sql.
2086
     */
2087
    protected dropIndexSql(
2088
        table: Table,
2089
        indexOrName: TableIndex | string,
2090
    ): Query {
2091
        const indexName =
2092
            indexOrName instanceof TableIndex ? indexOrName.name : indexOrName
×
2093
        return new Query(`DROP INDEX \`${indexName}\``)
×
2094
    }
2095

2096
    /**
2097
     * Builds create check constraint sql.
2098
     */
2099
    protected createCheckConstraintSql(
2100
        table: Table,
2101
        checkConstraint: TableCheck,
2102
    ): Query {
2103
        return new Query(
×
2104
            `ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT \`${
2105
                checkConstraint.name
2106
            }\` CHECK (${checkConstraint.expression})`,
2107
        )
2108
    }
2109

2110
    /**
2111
     * Builds drop check constraint sql.
2112
     */
2113
    protected dropCheckConstraintSql(
2114
        table: Table,
2115
        checkOrName: TableCheck | string,
2116
    ): Query {
2117
        const checkName =
2118
            checkOrName instanceof TableCheck ? checkOrName.name : checkOrName
×
2119
        return new Query(
×
2120
            `ALTER TABLE ${this.escapePath(
2121
                table,
2122
            )} DROP CONSTRAINT \`${checkName}\``,
2123
        )
2124
    }
2125

2126
    /**
2127
     * Builds create foreign key sql.
2128
     */
2129
    protected createForeignKeySql(
2130
        table: Table,
2131
        foreignKey: TableForeignKey,
2132
    ): Query {
2133
        const columnNames = foreignKey.columnNames
×
2134
            .map((column) => this.driver.escape(column))
×
2135
            .join(", ")
2136
        const referencedColumnNames = foreignKey.referencedColumnNames
×
2137
            .map((column) => this.driver.escape(column))
×
2138
            .join(",")
2139
        const sql =
2140
            `ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT \`${
×
2141
                foreignKey.name
2142
            }\` FOREIGN KEY (${columnNames}) ` +
2143
            `REFERENCES ${this.escapePath(
2144
                this.getTablePath(foreignKey),
2145
            )} (${referencedColumnNames})`
2146

2147
        return new Query(sql)
×
2148
    }
2149

2150
    /**
2151
     * Builds drop foreign key sql.
2152
     */
2153
    protected dropForeignKeySql(
2154
        table: Table,
2155
        foreignKeyOrName: TableForeignKey | string,
2156
    ): Query {
2157
        const foreignKeyName =
2158
            foreignKeyOrName instanceof TableForeignKey
×
2159
                ? foreignKeyOrName.name
2160
                : foreignKeyOrName
2161
        return new Query(
×
2162
            `ALTER TABLE ${this.escapePath(
2163
                table,
2164
            )} DROP CONSTRAINT \`${foreignKeyName}\``,
2165
        )
2166
    }
2167

2168
    /**
2169
     * Escapes given table or view path.
2170
     */
2171
    protected escapePath(target: Table | View | string): string {
2172
        const { tableName } = this.driver.parseTableName(target)
×
2173
        return `\`${tableName}\``
×
2174
    }
2175

2176
    /**
2177
     * Builds a part of query to create/change a column.
2178
     */
2179
    protected buildCreateColumnSql(column: TableColumn) {
2180
        let c = `${this.driver.escape(
×
2181
            column.name,
2182
        )} ${this.connection.driver.createFullType(column)}`
2183

2184
        // Spanner supports only STORED generated column type
2185
        if (column.generatedType === "STORED" && column.asExpression) {
×
2186
            c += ` AS (${column.asExpression}) STORED`
×
2187
        } else {
2188
            if (!column.isNullable) c += " NOT NULL"
×
2189
        }
2190

2191
        return c
×
2192
    }
2193

2194
    /**
2195
     * Executes sql used special for schema build.
2196
     */
2197
    protected async executeQueries(
2198
        upQueries: Query | Query[],
2199
        downQueries: Query | Query[],
2200
    ): Promise<void> {
2201
        if (upQueries instanceof Query) upQueries = [upQueries]
×
2202
        if (downQueries instanceof Query) downQueries = [downQueries]
×
2203

2204
        this.sqlInMemory.upQueries.push(...upQueries)
×
2205
        this.sqlInMemory.downQueries.push(...downQueries)
×
2206

2207
        // if sql-in-memory mode is enabled then simply store sql in memory and return
2208
        if (this.sqlMemoryMode === true)
×
2209
            return Promise.resolve() as Promise<any>
×
2210

2211
        for (const { query, parameters } of upQueries) {
×
2212
            if (this.isDMLQuery(query)) {
×
2213
                await this.query(query, parameters)
×
2214
            } else {
2215
                await this.updateDDL(query, parameters)
×
2216
            }
2217
        }
2218
    }
2219

2220
    protected isDMLQuery(query: string): boolean {
2221
        return (
×
2222
            query.startsWith("INSERT") ||
×
2223
            query.startsWith("UPDATE") ||
2224
            query.startsWith("DELETE")
2225
        )
2226
    }
2227

2228
    /**
2229
     * Change table comment.
2230
     */
2231
    changeTableComment(
2232
        tableOrName: Table | string,
2233
        comment?: string,
2234
    ): Promise<void> {
2235
        throw new TypeORMError(
×
2236
            `spanner driver does not support change table comment.`,
2237
        )
2238
    }
2239
}
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