@mikro-orm/sql 7.1.0-dev.24 → 7.1.0-dev.26

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.
@@ -13,6 +13,10 @@ export declare class MySqlSchemaHelper extends SchemaHelper {
13
13
  };
14
14
  private static readonly PARTIAL_INDEX_RE;
15
15
  getSchemaBeginning(charset: string, disableForeignKeys?: boolean): string;
16
+ getSetSchemaSQL(schema: string): string;
17
+ getResetSchemaSQL(defaultSchema: string): string;
18
+ supportsMigrationSchema(): boolean;
19
+ tableExists(connection: AbstractSqlConnection, tableName: string, schemaName: string | undefined, ctx?: Transaction): Promise<boolean>;
16
20
  disableForeignKeysSQL(): string;
17
21
  enableForeignKeysSQL(): string;
18
22
  finalizeTable(table: DatabaseTable, charset: string, collate?: string): string;
@@ -17,6 +17,24 @@ export class MySqlSchemaHelper extends SchemaHelper {
17
17
  }
18
18
  return `set names ${charset};\n\n`;
19
19
  }
20
+ getSetSchemaSQL(schema) {
21
+ return `use ${this.quote(schema)}`;
22
+ }
23
+ getResetSchemaSQL(defaultSchema) {
24
+ return `use ${this.quote(defaultSchema)}`;
25
+ }
26
+ supportsMigrationSchema() {
27
+ return true;
28
+ }
29
+ async tableExists(connection, tableName, schemaName, ctx) {
30
+ // MySQL "schema" = database — when none is requested, probe the connection's current DB
31
+ // via `schema()` rather than the base impl's `getDefaultSchemaName()` (which is undefined here).
32
+ const schemaClause = schemaName
33
+ ? `table_schema = ${this.platform.quoteValue(schemaName)}`
34
+ : `table_schema = schema()`;
35
+ const rows = await connection.execute(`select 1 from information_schema.tables where ${schemaClause} and table_name = ${this.platform.quoteValue(tableName)}`, [], 'all', ctx);
36
+ return rows.length > 0;
37
+ }
20
38
  disableForeignKeysSQL() {
21
39
  return 'set foreign_key_checks = 0;';
22
40
  }
@@ -17,6 +17,9 @@ export declare class PostgreSqlSchemaHelper extends SchemaHelper {
17
17
  private static readonly PARTIAL_WHERE_RE;
18
18
  private static readonly FUNCTIONAL_COL_RE;
19
19
  getSchemaBeginning(charset: string, disableForeignKeys?: boolean): string;
20
+ getSetSchemaSQL(schema: string): string;
21
+ getResetSchemaSQL(_defaultSchema: string): string;
22
+ supportsMigrationSchema(): boolean;
20
23
  getCreateDatabaseSQL(name: string): string;
21
24
  getListTablesSQL(): string;
22
25
  private getIgnoredViewsCondition;
@@ -21,6 +21,17 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
21
21
  }
22
22
  return `set names '${charset}';\n\n`;
23
23
  }
24
+ getSetSchemaSQL(schema) {
25
+ // session-level `SET` (not `SET LOCAL`) so it also works outside a transaction; the runner
26
+ // emits `getResetSchemaSQL` afterwards to avoid leaking onto the pooled connection
27
+ return `set search_path to ${this.quote(schema)}`;
28
+ }
29
+ getResetSchemaSQL(_defaultSchema) {
30
+ return 'reset search_path';
31
+ }
32
+ supportsMigrationSchema() {
33
+ return true;
34
+ }
24
35
  getCreateDatabaseSQL(name) {
25
36
  return `create database ${this.quote(name)}`;
26
37
  }
@@ -11,6 +11,7 @@ export declare class SqliteSchemaHelper extends SchemaHelper {
11
11
  supportsSchemaConstraints(): boolean;
12
12
  getCreateNamespaceSQL(name: string): string;
13
13
  getDropNamespaceSQL(name: string): string;
14
+ tableExists(connection: AbstractSqlConnection, tableName: string, _schemaName: string | undefined, ctx?: Transaction): Promise<boolean>;
14
15
  getListTablesSQL(): string;
15
16
  getAllTables(connection: AbstractSqlConnection, schemas?: string[], ctx?: Transaction): Promise<Table[]>;
16
17
  getNamespaces(connection: AbstractSqlConnection, ctx?: Transaction): Promise<string[]>;
@@ -31,6 +31,10 @@ export class SqliteSchemaHelper extends SchemaHelper {
31
31
  getDropNamespaceSQL(name) {
32
32
  return '';
33
33
  }
34
+ async tableExists(connection, tableName, _schemaName, ctx) {
35
+ const rows = await connection.execute(`select name from sqlite_master where type = 'table' and name = ${this.platform.quoteValue(tableName)}`, [], 'all', ctx);
36
+ return rows.length > 0;
37
+ }
34
38
  getListTablesSQL() {
35
39
  return (`select name as table_name from sqlite_master where type = 'table' and name != 'sqlite_sequence' and name != 'geometry_columns' and name != 'spatial_ref_sys' ` +
36
40
  `union all select name as table_name from sqlite_temp_master where type = 'table' order by name`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.1.0-dev.24",
3
+ "version": "7.1.0-dev.26",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.0.13"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.1.0-dev.24"
56
+ "@mikro-orm/core": "7.1.0-dev.26"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -16,6 +16,14 @@ export declare abstract class SchemaHelper {
16
16
  enableForeignKeysSQL(): string;
17
17
  /** Returns SQL to append to schema migration scripts (e.g., re-enabling FK checks). */
18
18
  getSchemaEnd(disableForeignKeys?: boolean): string;
19
+ /** Sets the current schema for the session (e.g. `SET search_path`). */
20
+ getSetSchemaSQL(_schema: string): string;
21
+ /** Whether the driver supports setting a runtime schema per migration run. */
22
+ supportsMigrationSchema(): boolean;
23
+ /** Restores the session's schema to the connection's default after a migration. */
24
+ getResetSchemaSQL(_defaultSchema: string): string;
25
+ /** Returns `undefined` for schemaless drivers, throws for drivers that have schemas but no session switch. */
26
+ resolveMigrationSchema(schema: string | undefined): string | undefined;
19
27
  finalizeTable(table: DatabaseTable, charset: string, collate?: string): string;
20
28
  appendComments(table: DatabaseTable): string[];
21
29
  supportsSchemaConstraints(): boolean;
@@ -31,6 +39,8 @@ export declare abstract class SchemaHelper {
31
39
  getListTablesSQL(): string;
32
40
  /** Retrieves all tables from the database. */
33
41
  getAllTables(connection: AbstractSqlConnection, schemas?: string[], ctx?: Transaction): Promise<Table[]>;
42
+ /** Checks whether a specific table exists in a given schema (not the connection's current schema). */
43
+ tableExists(connection: AbstractSqlConnection, tableName: string, schemaName: string | undefined, ctx?: Transaction): Promise<boolean>;
34
44
  getListViewsSQL(): string;
35
45
  loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string, ctx?: Transaction): Promise<void>;
36
46
  /** Returns SQL to rename a column in a table. */
@@ -27,6 +27,32 @@ export class SchemaHelper {
27
27
  }
28
28
  return '';
29
29
  }
30
+ /** Sets the current schema for the session (e.g. `SET search_path`). */
31
+ getSetSchemaSQL(_schema) {
32
+ return '';
33
+ }
34
+ /** Whether the driver supports setting a runtime schema per migration run. */
35
+ supportsMigrationSchema() {
36
+ return false;
37
+ }
38
+ /** Restores the session's schema to the connection's default after a migration. */
39
+ getResetSchemaSQL(_defaultSchema) {
40
+ return '';
41
+ }
42
+ /** Returns `undefined` for schemaless drivers, throws for drivers that have schemas but no session switch. */
43
+ resolveMigrationSchema(schema) {
44
+ if (!schema) {
45
+ return undefined;
46
+ }
47
+ if (this.supportsMigrationSchema()) {
48
+ return schema;
49
+ }
50
+ if (!this.platform.supportsSchemas()) {
51
+ return undefined;
52
+ }
53
+ const driverName = this.platform.constructor.name.replace(/Platform$/, 'Driver');
54
+ throw new Error(`Runtime schema for migrations is not supported by the ${driverName}`);
55
+ }
30
56
  finalizeTable(table, charset, collate) {
31
57
  return '';
32
58
  }
@@ -75,6 +101,13 @@ export class SchemaHelper {
75
101
  async getAllTables(connection, schemas, ctx) {
76
102
  return connection.execute(this.getListTablesSQL(), [], 'all', ctx);
77
103
  }
104
+ /** Checks whether a specific table exists in a given schema (not the connection's current schema). */
105
+ async tableExists(connection, tableName, schemaName, ctx) {
106
+ const qv = (v) => this.platform.quoteValue(v ?? '');
107
+ const resolved = schemaName ?? this.platform.getDefaultSchemaName();
108
+ const rows = await connection.execute(`select 1 from information_schema.tables where table_schema = ${qv(resolved)} and table_name = ${qv(tableName)}`, [], 'all', ctx);
109
+ return rows.length > 0;
110
+ }
78
111
  getListViewsSQL() {
79
112
  throw new Error('Not supported by given driver');
80
113
  }
@@ -15,8 +15,8 @@ export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<Abstract
15
15
  * Returns true if the database was created.
16
16
  */
17
17
  ensureDatabase(options?: EnsureDatabaseOptions): Promise<boolean>;
18
- getTargetSchema(schema?: string): DatabaseSchema;
19
- protected getOrderedMetadata(schema?: string): EntityMetadata[];
18
+ getTargetSchema(schema?: string, includeWildcardSchema?: boolean): DatabaseSchema;
19
+ protected getOrderedMetadata(schema?: string, includeWildcardSchema?: boolean): EntityMetadata[];
20
20
  getCreateSchemaSQL(options?: CreateSchemaOptions): Promise<string>;
21
21
  drop(options?: DropSchemaOptions): Promise<void>;
22
22
  createNamespace(name: string): Promise<void>;
@@ -44,13 +44,13 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
44
44
  }
45
45
  return false;
46
46
  }
47
- getTargetSchema(schema) {
48
- const metadata = this.getOrderedMetadata(schema);
47
+ getTargetSchema(schema, includeWildcardSchema = false) {
48
+ const metadata = this.getOrderedMetadata(schema, includeWildcardSchema);
49
49
  const schemaName = schema ?? this.config.get('schema') ?? this.platform.getDefaultSchemaName();
50
50
  return DatabaseSchema.fromMetadata(metadata, this.platform, this.config, schemaName, this.em);
51
51
  }
52
- getOrderedMetadata(schema) {
53
- const metadata = super.getOrderedMetadata(schema);
52
+ getOrderedMetadata(schema, includeWildcardSchema = false) {
53
+ const metadata = super.getOrderedMetadata(schema, includeWildcardSchema);
54
54
  // Filter out skipped tables
55
55
  return metadata.filter(meta => {
56
56
  const tableName = meta.tableName;
@@ -59,7 +59,7 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
59
59
  });
60
60
  }
61
61
  async getCreateSchemaSQL(options = {}) {
62
- const toSchema = this.getTargetSchema(options.schema);
62
+ const toSchema = this.getTargetSchema(options.schema, options.includeWildcardSchema);
63
63
  const ret = [];
64
64
  for (const namespace of toSchema.getNamespaces()) {
65
65
  if (namespace === this.platform.getDefaultSchemaName()) {
@@ -225,13 +225,13 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
225
225
  async prepareSchemaForComparison(options) {
226
226
  options.safe ??= false;
227
227
  options.dropTables ??= true;
228
- const toSchema = this.getTargetSchema(options.schema);
228
+ const toSchema = this.getTargetSchema(options.schema, options.includeWildcardSchema);
229
229
  const schemas = toSchema.getNamespaces();
230
230
  const fromSchema = options.fromSchema ??
231
231
  (await DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas, undefined, this.options.skipTables, this.options.skipViews));
232
- const wildcardSchemaTables = [...this.metadata.getAll().values()]
233
- .filter(meta => meta.schema === '*')
234
- .map(meta => meta.tableName);
232
+ const wildcardSchemaTables = options.includeWildcardSchema
233
+ ? []
234
+ : [...this.metadata.getAll().values()].filter(meta => meta.schema === '*').map(meta => meta.tableName);
235
235
  fromSchema.prune(options.schema, wildcardSchemaTables);
236
236
  toSchema.prune(options.schema, wildcardSchemaTables);
237
237
  return { fromSchema, toSchema };