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

typeorm / typeorm / 20153290793

12 Dec 2025 01:29AM UTC coverage: 80.807% (+0.04%) from 80.764%
20153290793

push

github

alumni
refactor(mysql)!: drop support for mysql package and default to mysql2 (#11766)

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

26912 of 32666 branches covered (82.39%)

Branch coverage included in aggregate %.

13 of 15 new or added lines in 2 files covered. (86.67%)

694 existing lines in 23 files now uncovered.

91350 of 113685 relevant lines covered (80.35%)

68942.63 hits per line

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

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

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

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

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

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

26✔
52
    // -------------------------------------------------------------------------
26✔
53
    // Constructor
26✔
54
    // -------------------------------------------------------------------------
26✔
55

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

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

26✔
68
    /**
26✔
69
     * Creates/uses database connection from the connection pool to perform further operations.
26✔
70
     * Returns obtained database connection.
26✔
71
     */
26✔
72
    async connect(): Promise<any> {
26✔
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

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

26✔
96
    /**
26✔
97
     * Starts transaction.
26✔
98
     */
26✔
99
    async startTransaction(isolationLevel?: IsolationLevel): Promise<void> {
26✔
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

26✔
115
    /**
26✔
116
     * Commits transaction.
26✔
117
     * Error will be thrown if transaction was not started.
26✔
118
     */
26✔
119
    async commitTransaction(): Promise<void> {
26✔
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

26✔
132
    /**
26✔
133
     * Rollbacks transaction.
26✔
134
     * Error will be thrown if transaction was not started.
26✔
135
     */
26✔
136
    async rollbackTransaction(): Promise<void> {
26✔
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

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

×
159
        await this.connect()
×
160

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

×
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

26✔
275
    /**
26✔
276
     * Update database schema.
26✔
277
     * Used for creating/altering/dropping tables, columns, indexes, etc.
26✔
278
     *
26✔
279
     * DDL changing queries should be executed by `updateSchema()` method.
26✔
280
     */
26✔
281
    async updateDDL(query: string, parameters?: any[]): Promise<void> {
26✔
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] =
×
288
                await this.driver.instanceDatabase.updateSchema(query)
×
289
            await operation.promise()
×
290
            // log slow queries if maxQueryExecution time is set
×
291
            const maxQueryExecutionTime =
×
292
                this.driver.options.maxQueryExecutionTime
×
293
            const queryEndTime = Date.now()
×
294
            const queryExecutionTime = queryEndTime - queryStartTime
×
295
            if (
×
296
                maxQueryExecutionTime &&
×
297
                queryExecutionTime > maxQueryExecutionTime
×
298
            )
×
299
                this.driver.connection.logger.logQuerySlow(
×
300
                    queryExecutionTime,
×
301
                    query,
×
302
                    parameters,
×
303
                    this,
×
304
                )
×
305
        } catch (err) {
×
306
            this.driver.connection.logger.logQueryError(
×
307
                err,
×
308
                query,
×
309
                parameters,
×
310
                this,
×
311
            )
×
312
            throw new QueryFailedError(query, parameters, err)
×
313
        }
×
314
    }
×
315

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

26✔
1059
    /**
26✔
1060
     * Creates a new primary key.
26✔
1061
     *
26✔
1062
     * Not supported in Spanner.
26✔
1063
     * @see https://cloud.google.com/spanner/docs/schema-and-data-model#notes_about_key_columns
26✔
1064
     */
26✔
1065
    async createPrimaryKey(
26✔
UNCOV
1066
        tableOrName: Table | string,
×
1067
        columnNames: string[],
×
1068
    ): Promise<void> {
×
1069
        throw new Error(
×
1070
            "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.",
×
1071
        )
×
1072
    }
×
1073

26✔
1074
    /**
26✔
1075
     * Updates composite primary keys.
26✔
1076
     */
26✔
1077
    async updatePrimaryKeys(
26✔
UNCOV
1078
        tableOrName: Table | string,
×
1079
        columns: TableColumn[],
×
1080
    ): Promise<void> {
×
1081
        throw new Error(
×
1082
            "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.",
×
1083
        )
×
1084
    }
×
1085

26✔
1086
    /**
26✔
1087
     * Creates a new primary key.
26✔
1088
     *
26✔
1089
     * Not supported in Spanner.
26✔
1090
     * @see https://cloud.google.com/spanner/docs/schema-and-data-model#notes_about_key_columns
26✔
1091
     */
26✔
1092
    async dropPrimaryKey(tableOrName: Table | string): Promise<void> {
26✔
UNCOV
1093
        throw new Error(
×
1094
            "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.",
×
1095
        )
×
1096
    }
×
1097

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

×
1466
        if (
×
1467
            !dropIndexQueries.length &&
×
1468
            !dropFKQueries.length &&
×
1469
            // !dropViewQueries.length &&
×
1470
            !dropTableQueries.length
×
1471
        )
×
1472
            return
×
1473

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

×
1484
            // for (let query of dropViewQueries) {
×
1485
            //     await this.updateDDL(query["query"])
×
1486
            // }
×
1487

×
1488
            for (const query of dropTableQueries) {
×
1489
                await this.updateDDL(query["query"])
×
1490
            }
×
1491

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

26✔
1503
    // -------------------------------------------------------------------------
26✔
1504
    // Override Methods
26✔
1505
    // -------------------------------------------------------------------------
26✔
1506

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

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

26✔
1536
    // -------------------------------------------------------------------------
26✔
1537
    // Protected Methods
26✔
1538
    // -------------------------------------------------------------------------
26✔
1539

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

×
1573
        return Promise.resolve([])
×
1574
    }
×
1575

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

×
1584
        const dbTables: { TABLE_NAME: string }[] = []
×
1585

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

×
1602
            dbTables.push(...(await this.query(tablesSql)))
×
1603
        }
×
1604

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

×
1608
        const loadedTableNames = dbTables
×
1609
            .map((dbTable) => `'${dbTable.TABLE_NAME}'`)
×
1610
            .join(", ")
×
1611

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

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

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

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

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

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

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

×
1668
                table.name = this.driver.buildTableName(dbTable["TABLE_NAME"])
×
1669

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

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

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

×
1723
                            const tableColumn = new TableColumn()
×
1724
                            tableColumn.name = dbColumn["COLUMN_NAME"]
×
1725

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

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

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

×
1756
                            if (dbColumn["IS_GENERATED"] === "ALWAYS") {
×
1757
                                tableColumn.asExpression =
×
1758
                                    dbColumn["GENERATION_EXPRESSION"]
×
1759
                                tableColumn.generatedType = "STORED"
×
1760

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

×
1769
                                const results = await this.query(
×
1770
                                    asExpressionQuery.query,
×
1771
                                    asExpressionQuery.parameters,
×
1772
                                )
×
1773

×
1774
                                if (results[0] && results[0].value) {
×
1775
                                    tableColumn.asExpression = results[0].value
×
1776
                                } else {
×
1777
                                    tableColumn.asExpression = ""
×
1778
                                }
×
1779
                            }
×
1780

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

×
1798
                            return tableColumn
×
1799
                        }),
×
1800
                )
×
1801

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

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

×
1838
                const tableIndices = dbIndices.filter(
×
1839
                    (dbIndex) =>
×
1840
                        dbIndex["TABLE_NAME"] === dbTable["TABLE_NAME"],
×
1841
                )
×
1842

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

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

×
1860
                const tableChecks = dbChecks.filter(
×
1861
                    (dbCheck) =>
×
1862
                        dbCheck["TABLE_NAME"] === dbTable["TABLE_NAME"],
×
1863
                )
×
1864

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

×
1881
                return table
×
1882
            }),
×
1883
        )
×
1884
    }
×
1885

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

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

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

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

×
1957
            sql += `, ${checksSql}`
×
1958
        }
×
1959

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

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

×
1985
            sql += `, ${foreignKeysSql}`
×
1986
        }
×
1987

×
1988
        sql += `)`
×
1989

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

×
2000
        return new Query(sql)
×
2001
    }
×
2002

26✔
2003
    /**
26✔
2004
     * Builds drop table sql.
26✔
2005
     */
26✔
2006
    protected dropTableSql(tableOrPath: Table | string): Query {
26✔
UNCOV
2007
        return new Query(`DROP TABLE ${this.escapePath(tableOrPath)}`)
×
UNCOV
2008
    }
×
2009

26✔
2010
    protected createViewSql(view: View): Query {
26✔
2011
        const materializedClause = view.materialized ? "MATERIALIZED " : ""
×
UNCOV
2012
        const viewName = this.escapePath(view)
×
UNCOV
2013

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

26✔
2023
    protected async insertViewDefinitionSql(view: View): Promise<Query> {
26✔
2024
        const { schema, tableName: name } = this.driver.parseTableName(view)
×
UNCOV
2025

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

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

26✔
2051
    /**
26✔
2052
     * Builds remove view sql.
26✔
2053
     */
26✔
2054
    protected async deleteViewDefinitionSql(view: View): Promise<Query> {
26✔
UNCOV
2055
        const { schema, tableName: name } = this.driver.parseTableName(view)
×
UNCOV
2056

×
UNCOV
2057
        const type = view.materialized
×
2058
            ? MetadataTableType.MATERIALIZED_VIEW
×
2059
            : MetadataTableType.VIEW
×
2060
        return this.deleteTypeormMetadataSql({ type, schema, name })
×
2061
    }
×
2062

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

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

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

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

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

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

×
2144
        return new Query(sql)
×
2145
    }
×
2146

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

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

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

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

×
2188
        return c
×
2189
    }
×
2190

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

×
2201
        this.sqlInMemory.upQueries.push(...upQueries)
×
2202
        this.sqlInMemory.downQueries.push(...downQueries)
×
2203

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

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

26✔
2217
    protected isDMLQuery(query: string): boolean {
26✔
2218
        return (
×
UNCOV
2219
            query.startsWith("INSERT") ||
×
UNCOV
2220
            query.startsWith("UPDATE") ||
×
2221
            query.startsWith("DELETE")
×
2222
        )
×
2223
    }
×
2224

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

© 2026 Coveralls, Inc