@mikro-orm/sql 7.2.13-dev.9 → 7.2.14-dev.0

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.
@@ -99,10 +99,10 @@ export class AbstractSqlDriver extends DatabaseDriver {
99
99
  }
100
100
  }
101
101
  if (options.collation != null && typeof options.collation !== 'string') {
102
- throw new Error('Collation option for SQL drivers must be a string (collation name). Use a CollationOptions object only with MongoDB.');
102
+ throw new ValidationError('Collation option for SQL drivers must be a string (collation name). Use a CollationOptions object only with MongoDB.');
103
103
  }
104
104
  if (options.indexHint != null && typeof options.indexHint !== 'string') {
105
- throw new Error("indexHint for SQL drivers must be a string (e.g. 'force index(my_index)'). Use an object only with MongoDB.");
105
+ throw new ValidationError("indexHint for SQL drivers must be a string (e.g. 'force index(my_index)'). Use an object only with MongoDB.");
106
106
  }
107
107
  }
108
108
  createEntityManager(useContext) {
@@ -730,8 +730,10 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
730
730
  }
731
731
  dropRoutine(routine) {
732
732
  const qualifiedName = this.qualifiedRoutineName(routine);
733
- const argTypes = routine.params.map(p => p.type).join(', ');
734
733
  const kind = routine.type === 'procedure' ? 'procedure' : 'function';
734
+ // OUT parameters are not part of a function's identity, only of a procedure's
735
+ const identityParams = routine.params.filter(p => kind === 'procedure' || p.direction !== 'out');
736
+ const argTypes = identityParams.map(p => p.type).join(', ');
735
737
  return `drop ${kind} if exists ${qualifiedName}(${argTypes})`;
736
738
  }
737
739
  async getAllRoutines(connection, schemas = []) {
@@ -804,7 +806,8 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
804
806
  // INOUT before IN/OUT, with required trailing space so identifiers like `input` aren't
805
807
  // mis-parsed as a direction keyword. The name is optional — `pg_get_function_identity_arguments`
806
808
  // omits it for unnamed parameters (e.g. `text`), leaving just the type.
807
- const match = /^(?:(INOUT|VARIADIC|IN|OUT)\s+)?(?:(?:"((?:[^"]|"")+)"|([\w$]+))\s+)?(.+)$/is.exec(trimmed);
809
+ // An unquoted name is never the first word of a multi-word type: postgres quotes those keywords in names, and `double` is only one before `precision`.
810
+ const match = /^(?:(INOUT|VARIADIC|IN|OUT)\s+)?(?:(?:"((?:[^"]|"")+)"|(?!(?:double\s+precision|character|bit|timestamp|time)(?![\w$]))([\w$]+))\s+)?(.+)$/is.exec(trimmed);
808
811
  /* v8 ignore next 3: defensive guard for unexpected `pg_get_function_identity_arguments` output shapes */
809
812
  if (!match) {
810
813
  throw new Error(`Could not parse PostgreSQL routine parameter signature: ${JSON.stringify(trimmed)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.2.13-dev.9",
3
+ "version": "7.2.14-dev.0",
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",
@@ -50,10 +50,10 @@
50
50
  "kysely": "0.29.9"
51
51
  },
52
52
  "devDependencies": {
53
- "@mikro-orm/core": "^7.2.12"
53
+ "@mikro-orm/core": "^7.2.13"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.13-dev.9"
56
+ "@mikro-orm/core": "7.2.14-dev.0"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -33,8 +33,11 @@ export class DatabaseTable {
33
33
  this.#platform = platform;
34
34
  }
35
35
  getQuotedName() {
36
- const schema = this.getShortestName() === this.name ? undefined : this.schema;
37
- return this.#platform.quoteQualifiedName(this.name, schema);
36
+ // a dot in the name can be a `database.schema.table` qualifier, it is kept inside the identifier in the default schema only
37
+ if (this.schema && this.schema !== this.#platform.getDefaultSchemaName()) {
38
+ return this.#platform.quoteIdentifier(this.getShortestName()).replaceAll('\\?', '?');
39
+ }
40
+ return this.#platform.quoteQualifiedName(this.name);
38
41
  }
39
42
  getColumns() {
40
43
  return Object.values(this.#columns);
@@ -175,8 +175,26 @@ export class SchemaComparator {
175
175
  const ignoreRoutines = this.#platform.getConfig().get('schemaGenerator').ignoreRoutines;
176
176
  // Case-fold so user-written `'sql_hash'` matches Oracle's introspected `'SQL_HASH'`.
177
177
  const routineKey = (r) => ((r.schema ? `${r.schema}.` : '') + r.name).toLowerCase();
178
- const fromByKey = new Map(fromSchema.getRoutines().map(r => [routineKey(r), r]));
179
- const toByKey = new Map(toSchema.getRoutines().map(r => [routineKey(r), r]));
178
+ // Overloads share a name: the one matching the other side keeps the plain key, the rest get a signature-qualified one.
179
+ const indexRoutines = (routines, others) => {
180
+ const map = new Map();
181
+ for (const routine of routines) {
182
+ const key = routineKey(routine);
183
+ const prev = map.get(key);
184
+ if (!prev) {
185
+ map.set(key, routine);
186
+ continue;
187
+ }
188
+ const other = others.find(o => routineKey(o) === key);
189
+ const keep = other && !this.diffRoutineParams(routine.params, other.params) ? routine : prev;
190
+ const extra = keep === routine ? prev : routine;
191
+ map.set(key, keep);
192
+ map.set(`${key}(${extra.params.map(p => `${p.direction} ${p.type}`).join(', ')})`, extra);
193
+ }
194
+ return map;
195
+ };
196
+ const fromByKey = indexRoutines(fromSchema.getRoutines(), toSchema.getRoutines());
197
+ const toByKey = indexRoutines(toSchema.getRoutines(), fromSchema.getRoutines());
180
198
  for (const [key, toRoutine] of toByKey) {
181
199
  const fromRoutine = fromByKey.get(key);
182
200
  if (!fromRoutine) {
@@ -283,12 +301,15 @@ export class SchemaComparator {
283
301
  }
284
302
  /** Engines drop length/precision modifiers and use different aliases for the same logical type — canonicalise both sides. */
285
303
  normaliseParamType(type) {
286
- const lengthMatch = /^([^()]+)\(([^)]*)\)\s*$/.exec(type);
287
- const base = (lengthMatch ? lengthMatch[1] : type).trim().toLowerCase();
304
+ const lower = type.trim().toLowerCase();
305
+ const array = /\[\d*\]$/.test(lower);
306
+ const element = array ? lower.slice(0, lower.indexOf('[')) : lower;
307
+ const open = element.indexOf('(');
308
+ const close = element.indexOf(')', open);
309
+ const stripped = open < 0 || close < 0 ? element : element.slice(0, open) + element.slice(close + 1);
310
+ const base = stripped.replace(/\s+/g, ' ').trim();
288
311
  const aliased = SchemaComparator.PARAM_TYPE_ALIASES[base] ?? base;
289
- const length = lengthMatch ? Number.parseInt(lengthMatch[2].split(',')[0].trim(), 10) : NaN;
290
- const options = Number.isFinite(length) ? { length } : {};
291
- return this.#platform.normalizeColumnType(aliased, options).toLowerCase();
312
+ return this.#platform.normalizeColumnType(aliased, {}).toLowerCase() + (array ? '[]' : '');
292
313
  }
293
314
  static PARAM_TYPE_ALIASES = {
294
315
  int: 'integer',
@@ -297,7 +318,11 @@ export class SchemaComparator {
297
318
  int2: 'smallint',
298
319
  bool: 'boolean',
299
320
  'character varying': 'varchar',
321
+ character: 'char',
300
322
  bpchar: 'char',
323
+ 'bit varying': 'varbit',
324
+ 'time without time zone': 'time',
325
+ 'timestamp without time zone': 'timestamp',
301
326
  float8: 'double precision',
302
327
  float4: 'real',
303
328
  // Oracle's USER_ARGUMENTS reports `REF CURSOR`; users declare `sys_refcursor`.
@@ -189,6 +189,8 @@ export declare abstract class SchemaHelper {
189
189
  /** Returns SQL to create a foreign key constraint on a table. */
190
190
  createForeignKey(table: DatabaseTable, foreignKey: ForeignKey, alterTable?: boolean, inline?: boolean): string;
191
191
  splitTableName(name: string, skipDefaultSchema?: boolean): [string | undefined, string];
192
+ /** Splits a developer-supplied name on the last dot, the qualifier can have more parts (`database.schema`). */
193
+ splitQualifiedName(name: string): [string | undefined, string];
192
194
  getReferencedTableName(referencedTableName: string, schema?: string): string;
193
195
  createIndex(index: IndexDef, table: DatabaseTable, createPrimary?: boolean): string;
194
196
  createCheck(table: DatabaseTable, check: CheckDef): string;
@@ -224,7 +226,7 @@ export declare abstract class SchemaHelper {
224
226
  get options(): NonNullable<Options['schemaGenerator']>;
225
227
  protected processComment(comment: string): string;
226
228
  protected quote(...keys: (string | undefined)[]): string;
227
- /** Quotes a table or view name held separately from its schema, a dot in either stays inside the identifier. */
229
+ /** A dot in the name stays inside the identifier in the default schema only, elsewhere it can be a `database.schema.table` qualifier. */
228
230
  protected quoteTableName(name: string, schema?: string): string;
229
231
  dropForeignKey(tableName: string, constraintName: string): string;
230
232
  dropIndex(table: string, index: IndexDef, oldIndexName?: string): string;
@@ -885,6 +885,11 @@ export class SchemaHelper {
885
885
  }
886
886
  return [schemaName, tableName];
887
887
  }
888
+ /** Splits a developer-supplied name on the last dot, the qualifier can have more parts (`database.schema`). */
889
+ splitQualifiedName(name) {
890
+ const index = name.lastIndexOf('.');
891
+ return index === -1 ? [undefined, name] : [name.substring(0, index), name.substring(index + 1)];
892
+ }
888
893
  getReferencedTableName(referencedTableName, schema) {
889
894
  const [schemaName, tableName] = this.splitTableName(referencedTableName);
890
895
  schema = schemaName ?? schema ?? this.platform.getConfig().get('schema');
@@ -1025,9 +1030,12 @@ export class SchemaHelper {
1025
1030
  // DDL is not a query template, so question marks need no escaping there
1026
1031
  return this.platform.quoteIdentifier(keys.filter(Boolean).join('.')).replaceAll('\\?', '?');
1027
1032
  }
1028
- /** Quotes a table or view name held separately from its schema, a dot in either stays inside the identifier. */
1033
+ /** A dot in the name stays inside the identifier in the default schema only, elsewhere it can be a `database.schema.table` qualifier. */
1029
1034
  quoteTableName(name, schema) {
1030
- return this.platform.quoteQualifiedName(name, schema === this.platform.getDefaultSchemaName() ? undefined : schema);
1035
+ if (schema && schema !== this.platform.getDefaultSchemaName()) {
1036
+ return this.quote(schema, name);
1037
+ }
1038
+ return this.platform.quoteQualifiedName(name);
1031
1039
  }
1032
1040
  dropForeignKey(tableName, constraintName) {
1033
1041
  return `alter table ${this.quote(tableName)} drop foreign key ${this.quote(constraintName)}`;
@@ -201,7 +201,7 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
201
201
  }
202
202
  }
203
203
  if (options.dropMigrationsTable) {
204
- const [schema, name] = this.helper.splitTableName(this.config.get('migrations').tableName);
204
+ const [schema, name] = this.helper.splitQualifiedName(this.config.get('migrations').tableName);
205
205
  this.append(ret, this.helper.dropTableIfExists(name, schema ?? this.config.get('schema')));
206
206
  }
207
207
  return this.wrapSchema(ret, options);
@@ -521,7 +521,7 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
521
521
  return false;
522
522
  }
523
523
  async dropTableIfExists(name, schema) {
524
- const [schemaName, tableName] = schema ? [schema, name] : this.helper.splitTableName(name);
524
+ const [schemaName, tableName] = schema ? [schema, name] : this.helper.splitQualifiedName(name);
525
525
  const sql = this.helper.dropTableIfExists(tableName, schemaName);
526
526
  return this.execute(sql);
527
527
  }