Skip to content

Commit b937421

Browse files
committed
migration steps reduced
1 parent d2a9477 commit b937421

8 files changed

Lines changed: 70 additions & 12 deletions

File tree

src/common/IColumnSchema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ export default interface IColumnSchema {
66
default: string;
77
key: boolean;
88
computed?: any;
9+
ownerName?: string;
10+
ownerType?: string;
911
}

src/drivers/base/BaseDriver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export abstract class BaseConnection {
210210
return tx;
211211
}
212212

213-
abstract getColumnSchema(schema: string, table: string): Promise<IColumnSchema[]>;
213+
abstract getColumnSchema(schema: string): Promise<IColumnSchema[]>;
214214

215215

216216
protected abstract createDbTransaction(): Promise<EntityTransaction>;

src/drivers/postgres/PostgreSqlDriver.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class PostgreSqlConnection extends BaseConnection {
220220
return new PostgresAutomaticMigrations(context);
221221
}
222222

223-
async getColumnSchema(schema: string, table: string): Promise<IColumnSchema[]> {
223+
async getColumnSchema(schema: string): Promise<IColumnSchema[]> {
224224
const text = `
225225
select
226226
column_name as "columnName",
@@ -243,11 +243,12 @@ class PostgreSqlConnection extends BaseConnection {
243243
else null end as "identity",
244244
case
245245
when is_generated = 'YES' then '() => 1'
246-
else null end as "computed"
246+
else null end as "computed",
247+
table_name as "ownerName",
248+
'table' as "ownerType"
247249
from information_schema.columns
248-
where table_schema = $1
249-
and table_name = $2`;
250-
const r = await this.executeQuery({ text, values: [ schema, table ]});
250+
where table_schema = $1`;
251+
const r = await this.executeQuery({ text, values: [ schema ]});
251252
return r.rows;
252253
}
253254

src/drivers/sql-server/SqlServerDriver.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class SqlServerConnection extends BaseConnection {
129129
super(driver);
130130
}
131131

132-
async getColumnSchema(schema: string, table: string): Promise<IColumnSchema[]> {
132+
async getColumnSchema(schema: string): Promise<IColumnSchema[]> {
133133
const text = `
134134
SELECT
135135
COLUMN_NAME as [name],
@@ -161,12 +161,13 @@ export class SqlServerConnection extends BaseConnection {
161161
WHEN COLUMN_DEFAULT is NULL THEN ''
162162
ELSE '() => ' + COLUMN_DEFAULT
163163
END as [default],
164-
ColumnProperty(OBJECT_ID(TABLE_SCHEMA+'.'+TABLE_NAME),COLUMN_NAME,'IsComputed') as [computed]
164+
ColumnProperty(OBJECT_ID(TABLE_SCHEMA+'.'+TABLE_NAME),COLUMN_NAME,'IsComputed') as [computed],
165+
TABLE_NAME as [ownerName],
166+
'table' as [ownerType]
165167
FROM INFORMATION_SCHEMA.COLUMNS
166168
WHERE TABLE_SCHEMA = $1
167-
AND TABLE_NAME = $2
168169
`;
169-
const r = await this.executeQuery({ text, values: [schema, table] });
170+
const r = await this.executeQuery({ text, values: [schema] });
170171
return r.rows;
171172
}
172173

src/migrations/ExistingSchema.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import IColumnSchema from "../common/IColumnSchema.js";
2+
import type { BaseConnection } from "../drivers/base/BaseDriver.js";
3+
4+
export default class ExistingSchema {
5+
6+
7+
public static async getSchema(connection: BaseConnection, schema: string, table: string, caseInsensitive = false) {
8+
let s = this.cache.get(schema);
9+
if (!s) {
10+
const columns = await connection.getColumnSchema(schema);
11+
s = new ExistingSchema(columns, caseInsensitive);
12+
this.cache.set(schema, s);
13+
}
14+
if (caseInsensitive) {
15+
table = table.toLowerCase();
16+
}
17+
return s.tables.get(table) ?? [];
18+
}
19+
20+
private static cache = new Map<string, ExistingSchema>();
21+
22+
public tables = new Map<string,IColumnSchema[]>();
23+
24+
constructor(columns: IColumnSchema[], caseInsensitive = false) {
25+
for (const c of columns) {
26+
let tableName = c.ownerName;
27+
if (caseInsensitive) {
28+
tableName = tableName.toLowerCase();
29+
}
30+
let table = this.tables.get(tableName);
31+
if (!table) {
32+
table = [];
33+
this.tables.set(tableName, table);
34+
}
35+
table.push(c);
36+
}
37+
}
38+
39+
}

src/migrations/Migrations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { BaseConnection, IQuery, IQueryResult } from "../drivers/base/BaseD
99
import type EntityType from "../entity-query/EntityType.js";
1010
import type EntityContext from "../model/EntityContext.js";
1111
import type EntityQuery from "../model/EntityQuery.js";
12+
import ExistingSchema from "./ExistingSchema.js";
1213

1314
export default abstract class Migrations {
1415

src/migrations/postgres/PostgresAutomaticMigrations.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IIndex } from "../../decorators/IIndex.js";
66
import { BaseConnection, BaseDriver } from "../../drivers/base/BaseDriver.js";
77
import EntityType from "../../entity-query/EntityType.js";
88
import type EntityContext from "../../model/EntityContext.js";
9+
import ExistingSchema from "../ExistingSchema.js";
910
import PostgresMigrations from "./PostgresMigrations.js";
1011

1112
export default class PostgresAutomaticMigrations extends PostgresMigrations {
@@ -58,7 +59,7 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
5859
nonKeyColumns.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
5960
}
6061

61-
const columns = await driver.getColumnSchema(type.schema || "public", type.name);
62+
const columns = await ExistingSchema.getSchema(driver, type.schema || "public", type.name, true);
6263

6364
const columnSet = new Set(columns.map((x) => x.name));
6465

@@ -89,6 +90,12 @@ export default class PostgresAutomaticMigrations extends PostgresMigrations {
8990

9091
async createTable(driver: BaseConnection, type: EntityType, keys: IColumn[]) {
9192

93+
const columns = await ExistingSchema.getSchema(driver, type.schema || "public", type.name, true);
94+
95+
if (columns.length) {
96+
return;
97+
}
98+
9299
const name = type.schema
93100
? type.schema + "." + type.name
94101
: type.name;

src/migrations/sql-server/SqlServerAutomaticMigrations.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { BaseConnection } from "../../drivers/base/BaseDriver.js";
77
import { SqlServerLiteral } from "../../drivers/sql-server/SqlServerLiteral.js";
88
import EntityType from "../../entity-query/EntityType.js";
99
import type EntityContext from "../../model/EntityContext.js";
10+
import ExistingSchema from "../ExistingSchema.js";
1011
import SqlServerMigrations from "./SqlServerMigrations.js";
1112

1213
export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
@@ -61,7 +62,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
6162
nonKeyColumns.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
6263
}
6364

64-
const columns = await driver.getColumnSchema(type.schema || "dbo", type.name);
65+
const columns = await ExistingSchema.getSchema(driver, type.schema || "dbo", type.name);
6566

6667
const columnSet = new Set(columns.map((x) => x.name.toLowerCase()));
6768

@@ -97,6 +98,12 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
9798

9899
async createTable(driver: BaseConnection, type: EntityType, keys: IColumn[]) {
99100

101+
const columns = await ExistingSchema.getSchema(driver, type.schema || "public", type.name);
102+
103+
if (columns.length) {
104+
return;
105+
}
106+
100107
const name = type.schema
101108
? type.schema + "." + type.name
102109
: type.name;

0 commit comments

Comments
 (0)