|
| 1 | +import Logger, { ConsoleLogger } from "../common/Logger.js"; |
1 | 2 | import { modelSymbol } from "../common/symbols/symbols.js"; |
2 | 3 | import type QueryCompiler from "../compiler/QueryCompiler.js"; |
3 | 4 | import ICheckConstraint from "../decorators/ICheckConstraint.js"; |
| 5 | +import { IColumn } from "../decorators/IColumn.js"; |
4 | 6 | import type { IForeignKeyConstraint } from "../decorators/IForeignKeyConstraint.js"; |
5 | 7 | import type { IIndex } from "../decorators/IIndex.js"; |
| 8 | +import type { BaseConnection, IQuery, IQueryResult } from "../drivers/base/BaseDriver.js"; |
6 | 9 | import type EntityType from "../entity-query/EntityType.js"; |
7 | 10 | import type EntityContext from "../model/EntityContext.js"; |
8 | 11 | import type EntityQuery from "../model/EntityQuery.js"; |
| 12 | +import ExistingSchema from "./ExistingSchema.js"; |
9 | 13 |
|
10 | 14 | export default abstract class Migrations { |
11 | 15 |
|
12 | | - constructor(protected compiler: QueryCompiler) {} |
| 16 | + logger: Logger; |
13 | 17 |
|
14 | | - public async migrate(context: EntityContext , { |
| 18 | + constructor( |
| 19 | + private context: EntityContext, |
| 20 | + private connection: BaseConnection = context.connection, |
| 21 | + protected compiler: QueryCompiler = context.driver.compiler |
| 22 | + ) { |
| 23 | + |
| 24 | + } |
| 25 | + |
| 26 | + public async migrate({ |
15 | 27 | version, |
16 | 28 | name = "default", |
17 | 29 | historyTableName = "migrations", |
| 30 | + log = new ConsoleLogger(false), |
18 | 31 | seed, |
19 | | - }: { version?: string, name?: string, historyTableName?: string, seed?: (c: EntityContext) => Promise<any>} = {} ) { |
| 32 | + createIndexForForeignKeys = true |
| 33 | + }: { |
| 34 | + version?: string, |
| 35 | + name?: string, |
| 36 | + historyTableName?: string, |
| 37 | + log?: Logger, |
| 38 | + seed?: (c: EntityContext) => Promise<any>, |
| 39 | + createIndexForForeignKeys?: boolean |
| 40 | + } = {} ) { |
| 41 | + const { context } = this; |
20 | 42 | const { model } = context; |
| 43 | + this.logger = log ?? context.logger; |
21 | 44 | const postMigration = [] as (() => Promise<void>)[]; |
22 | 45 |
|
23 | 46 | if (version) { |
24 | 47 | // check if we have already stored this version... |
25 | 48 | if(await this.hasVersion(context, name, version, historyTableName)) { |
| 49 | + // eslint-disable-next-line no-console |
26 | 50 | console.warn(`Skipping migration, migration already exists for ${version}`); |
27 | 51 | return false; |
28 | 52 | } |
@@ -68,6 +92,16 @@ export default abstract class Migrations { |
68 | 92 | } |
69 | 93 |
|
70 | 94 | for (const { isInverseRelation , foreignKeyConstraint, relatedTypeClass } of type.relations) { |
| 95 | + |
| 96 | + if (createIndexForForeignKeys) { |
| 97 | + postMigration.push(() => |
| 98 | + this.createIndexForForeignKeys(context, type, type.nonKeys.filter((x) => |
| 99 | + x.fkRelation |
| 100 | + && (!x.key || type.keys.indexOf(x) !== 0) |
| 101 | + && !x.fkRelation?.doNotCreateIndex)) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
71 | 105 | if (isInverseRelation) { |
72 | 106 | continue; |
73 | 107 | } |
@@ -120,6 +154,8 @@ export default abstract class Migrations { |
120 | 154 |
|
121 | 155 | abstract ensureVersionTable(context: EntityContext, table: string): Promise<any>; |
122 | 156 |
|
| 157 | + abstract createIndexForForeignKeys(context: EntityContext, type: EntityType, fkColumns: IColumn[]): Promise<void>; |
| 158 | + |
123 | 159 | async commitVersion(context: EntityContext, name, version, table) { |
124 | 160 | const { quote, escapeLiteral } = this.compiler; |
125 | 161 |
|
@@ -164,4 +200,10 @@ export default abstract class Migrations { |
164 | 200 |
|
165 | 201 | abstract migrateCheckConstraint(context: EntityContext, checkConstraint: ICheckConstraint, type: EntityType); |
166 | 202 |
|
| 203 | + protected executeQuery(command: IQuery, signal?: AbortSignal): Promise<IQueryResult> { |
| 204 | + const text = typeof command === "string" ? command : command.text; |
| 205 | + this.logger?.log(text); |
| 206 | + return this.connection.executeQuery(command, signal); |
| 207 | + } |
| 208 | + |
167 | 209 | } |
0 commit comments