@entity-access/entity-access 1.0.488 → 1.0.489
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/drivers/base/BaseDriver.d.ts +2 -1
- package/dist/drivers/base/BaseDriver.d.ts.map +1 -1
- package/dist/drivers/base/BaseDriver.js.map +1 -1
- package/dist/drivers/postgres/PostgreSqlDriver.d.ts.map +1 -1
- package/dist/drivers/postgres/PostgreSqlDriver.js +2 -2
- package/dist/drivers/postgres/PostgreSqlDriver.js.map +1 -1
- package/dist/drivers/sql-server/SqlServerDriver.d.ts +2 -1
- package/dist/drivers/sql-server/SqlServerDriver.d.ts.map +1 -1
- package/dist/drivers/sql-server/SqlServerDriver.js +2 -2
- package/dist/drivers/sql-server/SqlServerDriver.js.map +1 -1
- package/dist/migrations/Migrations.d.ts +9 -2
- package/dist/migrations/Migrations.d.ts.map +1 -1
- package/dist/migrations/Migrations.js +16 -2
- package/dist/migrations/Migrations.js.map +1 -1
- package/dist/migrations/postgres/PostgresAutomaticMigrations.d.ts +1 -1
- package/dist/migrations/postgres/PostgresAutomaticMigrations.d.ts.map +1 -1
- package/dist/migrations/postgres/PostgresAutomaticMigrations.js +7 -7
- package/dist/migrations/postgres/PostgresAutomaticMigrations.js.map +1 -1
- package/dist/migrations/sql-server/SqlServerAutomaticMigrations.d.ts +1 -1
- package/dist/migrations/sql-server/SqlServerAutomaticMigrations.d.ts.map +1 -1
- package/dist/migrations/sql-server/SqlServerAutomaticMigrations.js +7 -7
- package/dist/migrations/sql-server/SqlServerAutomaticMigrations.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/workflows/WorkflowStorage.js +1 -1
- package/dist/workflows/WorkflowStorage.js.map +1 -1
- package/package.json +1 -1
- package/src/drivers/base/BaseDriver.ts +2 -1
- package/src/drivers/postgres/PostgreSqlDriver.ts +3 -2
- package/src/drivers/sql-server/SqlServerDriver.ts +3 -2
- package/src/migrations/Migrations.ts +24 -4
- package/src/migrations/postgres/PostgresAutomaticMigrations.ts +8 -9
- package/src/migrations/sql-server/SqlServerAutomaticMigrations.ts +9 -10
- package/src/workflows/WorkflowStorage.ts +1 -1
|
@@ -6,6 +6,7 @@ import QueryCompiler from "../../compiler/QueryCompiler.js";
|
|
|
6
6
|
import EntityType from "../../entity-query/EntityType.js";
|
|
7
7
|
import Migrations from "../../migrations/Migrations.js";
|
|
8
8
|
import PostgresAutomaticMigrations from "../../migrations/postgres/PostgresAutomaticMigrations.js";
|
|
9
|
+
import EntityContext from "../../model/EntityContext.js";
|
|
9
10
|
import DateTime from "../../types/DateTime.js";
|
|
10
11
|
import { BaseConnection, BaseDriver, EntityTransaction, IDbConnectionString, IDbReader, IQuery, toQuery } from "../base/BaseDriver.js";
|
|
11
12
|
import pg from "pg";
|
|
@@ -215,8 +216,8 @@ class PostgreSqlConnection extends BaseConnection {
|
|
|
215
216
|
}
|
|
216
217
|
|
|
217
218
|
|
|
218
|
-
public automaticMigrations(): Migrations {
|
|
219
|
-
return new PostgresAutomaticMigrations(
|
|
219
|
+
public automaticMigrations(context: EntityContext): Migrations {
|
|
220
|
+
return new PostgresAutomaticMigrations(context);
|
|
220
221
|
}
|
|
221
222
|
|
|
222
223
|
async getColumnSchema(schema: string, table: string): Promise<IColumnSchema[]> {
|
|
@@ -10,6 +10,7 @@ import TimedCache from "../../common/cache/TimedCache.js";
|
|
|
10
10
|
import EntityType from "../../entity-query/EntityType.js";
|
|
11
11
|
import DateTime from "../../types/DateTime.js";
|
|
12
12
|
import IColumnSchema from "../../common/IColumnSchema.js";
|
|
13
|
+
import type EntityContext from "../../model/EntityContext.js";
|
|
13
14
|
|
|
14
15
|
export type ISqlServerConnectionString = IDbConnectionString & sql.config;
|
|
15
16
|
|
|
@@ -237,8 +238,8 @@ export class SqlServerConnection extends BaseConnection {
|
|
|
237
238
|
return value;
|
|
238
239
|
}
|
|
239
240
|
|
|
240
|
-
public automaticMigrations(): Migrations {
|
|
241
|
-
return new SqlServerAutomaticMigrations(
|
|
241
|
+
public automaticMigrations(context: EntityContext): Migrations {
|
|
242
|
+
return new SqlServerAutomaticMigrations(context);
|
|
242
243
|
}
|
|
243
244
|
|
|
244
245
|
protected async createDbTransaction(): Promise<EntityTransaction> {
|
|
@@ -1,37 +1,51 @@
|
|
|
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";
|
|
4
5
|
import { IColumn } from "../decorators/IColumn.js";
|
|
5
6
|
import type { IForeignKeyConstraint } from "../decorators/IForeignKeyConstraint.js";
|
|
6
7
|
import type { IIndex } from "../decorators/IIndex.js";
|
|
8
|
+
import type { BaseConnection, IQuery, IQueryResult } from "../drivers/base/BaseDriver.js";
|
|
7
9
|
import type EntityType from "../entity-query/EntityType.js";
|
|
8
10
|
import type EntityContext from "../model/EntityContext.js";
|
|
9
11
|
import type EntityQuery from "../model/EntityQuery.js";
|
|
10
12
|
|
|
11
13
|
export default abstract class Migrations {
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
logger: Logger;
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
constructor(
|
|
18
|
+
private context: EntityContext,
|
|
19
|
+
private connection: BaseConnection = context.connection,
|
|
20
|
+
protected compiler: QueryCompiler = context.driver.compiler
|
|
21
|
+
) {
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public async migrate({
|
|
16
26
|
version,
|
|
17
27
|
name = "default",
|
|
18
28
|
historyTableName = "migrations",
|
|
29
|
+
log = new ConsoleLogger(false),
|
|
19
30
|
seed,
|
|
20
31
|
createIndexForForeignKeys = true
|
|
21
32
|
}: {
|
|
22
33
|
version?: string,
|
|
23
34
|
name?: string,
|
|
24
|
-
historyTableName?:
|
|
25
|
-
|
|
35
|
+
historyTableName?: string,
|
|
36
|
+
log?: Logger,
|
|
26
37
|
seed?: (c: EntityContext) => Promise<any>,
|
|
27
38
|
createIndexForForeignKeys?: boolean
|
|
28
39
|
} = {} ) {
|
|
40
|
+
const { context } = this;
|
|
29
41
|
const { model } = context;
|
|
42
|
+
this.logger = log ?? context.logger;
|
|
30
43
|
const postMigration = [] as (() => Promise<void>)[];
|
|
31
44
|
|
|
32
45
|
if (version) {
|
|
33
46
|
// check if we have already stored this version...
|
|
34
47
|
if(await this.hasVersion(context, name, version, historyTableName)) {
|
|
48
|
+
// eslint-disable-next-line no-console
|
|
35
49
|
console.warn(`Skipping migration, migration already exists for ${version}`);
|
|
36
50
|
return false;
|
|
37
51
|
}
|
|
@@ -185,4 +199,10 @@ export default abstract class Migrations {
|
|
|
185
199
|
|
|
186
200
|
abstract migrateCheckConstraint(context: EntityContext, checkConstraint: ICheckConstraint, type: EntityType);
|
|
187
201
|
|
|
202
|
+
protected executeQuery(command: IQuery, signal?: AbortSignal): Promise<IQueryResult> {
|
|
203
|
+
const text = typeof command === "string" ? command : command.text;
|
|
204
|
+
this.logger?.log(text);
|
|
205
|
+
return this.connection.executeQuery(command, signal);
|
|
206
|
+
}
|
|
207
|
+
|
|
188
208
|
}
|
|
@@ -5,8 +5,7 @@ import { IForeignKeyConstraint } from "../../decorators/IForeignKeyConstraint.js
|
|
|
5
5
|
import { IIndex } from "../../decorators/IIndex.js";
|
|
6
6
|
import { BaseConnection, BaseDriver } from "../../drivers/base/BaseDriver.js";
|
|
7
7
|
import EntityType from "../../entity-query/EntityType.js";
|
|
8
|
-
import EntityContext from "../../model/EntityContext.js";
|
|
9
|
-
import Migrations from "../Migrations.js";
|
|
8
|
+
import type EntityContext from "../../model/EntityContext.js";
|
|
10
9
|
import PostgresMigrations from "./PostgresMigrations.js";
|
|
11
10
|
|
|
12
11
|
export default class PostgresAutomaticMigrations extends PostgresMigrations {
|
|
@@ -83,7 +82,7 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
|
|
|
83
82
|
def += " DEFAULT " + iterator.default;
|
|
84
83
|
}
|
|
85
84
|
|
|
86
|
-
await
|
|
85
|
+
await this.executeQuery(def + ";");
|
|
87
86
|
}
|
|
88
87
|
|
|
89
88
|
}
|
|
@@ -119,7 +118,7 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
|
|
|
119
118
|
fields.push(def);
|
|
120
119
|
}
|
|
121
120
|
|
|
122
|
-
await
|
|
121
|
+
await this.executeQuery(`CREATE TABLE IF NOT EXISTS ${name} (${fields.join(",")}
|
|
123
122
|
,CONSTRAINT PK_${name} PRIMARY KEY (${keys.map((x) => x.columnName).join(",")})
|
|
124
123
|
)`);
|
|
125
124
|
|
|
@@ -142,7 +141,7 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
|
|
|
142
141
|
if (index.filter) {
|
|
143
142
|
query += ` WHERE (${index.filter})`;
|
|
144
143
|
}
|
|
145
|
-
await
|
|
144
|
+
await this.executeQuery(query);
|
|
146
145
|
}
|
|
147
146
|
|
|
148
147
|
async constraintExists(context: EntityContext, name: string, schema: string, table = "referential_constraints") {
|
|
@@ -159,7 +158,7 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
|
|
|
159
158
|
|
|
160
159
|
const driver = context.connection;
|
|
161
160
|
|
|
162
|
-
const r = await
|
|
161
|
+
const r = await this.executeQuery({ text, values });
|
|
163
162
|
if (r.rows?.length) {
|
|
164
163
|
return true;
|
|
165
164
|
}
|
|
@@ -218,9 +217,9 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
|
|
|
218
217
|
|
|
219
218
|
await using tx = await driver.createTransaction();
|
|
220
219
|
if (constraint.clearExisting && prepare) {
|
|
221
|
-
await
|
|
220
|
+
await this.executeQuery(prepare);
|
|
222
221
|
}
|
|
223
|
-
await
|
|
222
|
+
await this.executeQuery(text);
|
|
224
223
|
await tx.commit();
|
|
225
224
|
} catch (error) {
|
|
226
225
|
// we will simply ignore this
|
|
@@ -245,7 +244,7 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
|
|
|
245
244
|
const text = `ALTER TABLE ${name} ADD CONSTRAINT ${constraint.name} CHECK (${constraint.filter})`;
|
|
246
245
|
|
|
247
246
|
try {
|
|
248
|
-
await
|
|
247
|
+
await this.executeQuery(text);
|
|
249
248
|
} catch (error) {
|
|
250
249
|
// we will simply ignore this
|
|
251
250
|
console.warn(`Failed adding constraint ${constraint.name}`);
|
|
@@ -3,11 +3,10 @@ import ICheckConstraint from "../../decorators/ICheckConstraint.js";
|
|
|
3
3
|
import { IColumn } from "../../decorators/IColumn.js";
|
|
4
4
|
import { IForeignKeyConstraint } from "../../decorators/IForeignKeyConstraint.js";
|
|
5
5
|
import { IIndex } from "../../decorators/IIndex.js";
|
|
6
|
-
import { BaseConnection
|
|
6
|
+
import { BaseConnection } from "../../drivers/base/BaseDriver.js";
|
|
7
7
|
import { SqlServerLiteral } from "../../drivers/sql-server/SqlServerLiteral.js";
|
|
8
8
|
import EntityType from "../../entity-query/EntityType.js";
|
|
9
|
-
import EntityContext from "../../model/EntityContext.js";
|
|
10
|
-
import Migrations from "../Migrations.js";
|
|
9
|
+
import type EntityContext from "../../model/EntityContext.js";
|
|
11
10
|
import SqlServerMigrations from "./SqlServerMigrations.js";
|
|
12
11
|
|
|
13
12
|
export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
@@ -75,7 +74,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
75
74
|
|
|
76
75
|
if (iterator.computed) {
|
|
77
76
|
def += ` AS ${iterator.computed} ${iterator.stored ? "PERSISTED" : ""}`;
|
|
78
|
-
await
|
|
77
|
+
await this.executeQuery(def + ";");
|
|
79
78
|
continue;
|
|
80
79
|
}
|
|
81
80
|
|
|
@@ -91,7 +90,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
91
90
|
if (typeof iterator.default === "string") {
|
|
92
91
|
def += " DEFAULT " + iterator.default;
|
|
93
92
|
}
|
|
94
|
-
await
|
|
93
|
+
await this.executeQuery(def + ";");
|
|
95
94
|
}
|
|
96
95
|
|
|
97
96
|
}
|
|
@@ -123,7 +122,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
123
122
|
fields.push(def);
|
|
124
123
|
}
|
|
125
124
|
|
|
126
|
-
await
|
|
125
|
+
await this.executeQuery(`IF OBJECT_ID(${ SqlServerLiteral.escapeLiteral(name)}) IS NULL BEGIN
|
|
127
126
|
CREATE TABLE ${name} (${fields.join(",")}
|
|
128
127
|
, CONSTRAINT PK_${name} PRIMARY KEY(${keys.map((x) => x.quotedColumnName)})
|
|
129
128
|
);
|
|
@@ -149,7 +148,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
149
148
|
query += ` WHERE (${index.filter})`;
|
|
150
149
|
}
|
|
151
150
|
query += `\nEND`;
|
|
152
|
-
await
|
|
151
|
+
await this.executeQuery(query);
|
|
153
152
|
}
|
|
154
153
|
|
|
155
154
|
async constraintExists(context: EntityContext, name: string, schema: string, type: EntityType) {
|
|
@@ -164,7 +163,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
164
163
|
|
|
165
164
|
const driver = context.connection;
|
|
166
165
|
|
|
167
|
-
const r = await
|
|
166
|
+
const r = await this.executeQuery(text);
|
|
168
167
|
if (r.rows?.length === 0) {
|
|
169
168
|
if (r.rows["c1"] > 0) {
|
|
170
169
|
return true;
|
|
@@ -215,7 +214,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
215
214
|
}
|
|
216
215
|
|
|
217
216
|
try {
|
|
218
|
-
await
|
|
217
|
+
await this.executeQuery(text);
|
|
219
218
|
} catch (error) {
|
|
220
219
|
// we will simply ignore this
|
|
221
220
|
console.warn(`Failed adding constraint ${constraint.name}`);
|
|
@@ -238,7 +237,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
238
237
|
const text = `ALTER TABLE ${name} ADD CONSTRAINT ${constraint.name} CHECK (${constraint.filter})`;
|
|
239
238
|
|
|
240
239
|
try {
|
|
241
|
-
await
|
|
240
|
+
await this.executeQuery(text);
|
|
242
241
|
} catch (error) {
|
|
243
242
|
// we will simply ignore this
|
|
244
243
|
console.warn(`Failed adding constraint ${constraint.name}`);
|
|
@@ -234,7 +234,7 @@ export default class WorkflowStorage {
|
|
|
234
234
|
async seed(version?) {
|
|
235
235
|
const db = new WorkflowDbContext(this.driver);
|
|
236
236
|
await db.connection.ensureDatabase();
|
|
237
|
-
await db.connection.automaticMigrations().migrate(
|
|
237
|
+
await db.connection.automaticMigrations(db).migrate({ log: null, version, name: "workflows" });
|
|
238
238
|
}
|
|
239
239
|
|
|
240
240
|
}
|