@mikro-orm/sql 7.2.9-dev.19 → 7.2.9-dev.20

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.
@@ -1,4 +1,4 @@
1
- import { DeferMode, EnumType, Type, Utils, } from '@mikro-orm/core';
1
+ import { DecimalType, DeferMode, DoubleType, EnumType, FloatType, Type, Utils, } from '@mikro-orm/core';
2
2
  import { SchemaHelper, stripStatementNewlines } from '../../schema/SchemaHelper.js';
3
3
  import { normalizePartitionBound, normalizePartitionDefinition } from '../../schema/partitioning.js';
4
4
  /** PostGIS system views that should be automatically ignored */
@@ -230,7 +230,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
230
230
  const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
231
231
  table.collation = dbCollation;
232
232
  const pks = await this.getPrimaryKeys(connection, indexes[key], table.name, table.schema);
233
- const enums = this.getEnumDefinitions(checks[key] ?? []);
233
+ const enums = this.getEnumDefinitions(checks[key] ?? [], columns[key] ?? []);
234
234
  if (columns[key]) {
235
235
  table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums);
236
236
  }
@@ -1051,7 +1051,7 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
1051
1051
  }
1052
1052
  return `alter type ${this.quote(name)} add value if not exists ${this.platform.quoteValue(value)}${suffix}`;
1053
1053
  }
1054
- getEnumDefinitions(checks) {
1054
+ getEnumDefinitions(checks, columns) {
1055
1055
  return checks.reduce((o, item) => {
1056
1056
  // check constraints are defined as one of:
1057
1057
  // `CHECK ((type = ANY (ARRAY['local'::text, 'global'::text])))`
@@ -1063,13 +1063,20 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
1063
1063
  // the single-value form must compare against a quoted literal (`= 'a'::text`); anything else
1064
1064
  // (e.g. a JSON `->>` extraction like `name = data->>'name'`) is not an enum constraint
1065
1065
  const m2 = item.definition?.match(/\(array\[(.*)]\)/i) || item.definition?.match(/ = ('(?:[^']|'')*'::[\w ]+)\)/i);
1066
- if (item.columnName && m1 && m2) {
1066
+ // enums with string items are emulated with a check, a check like `delta in (1, -1)` on a number is a plain one
1067
+ const column = columns.find(c => c.name === item.columnName);
1068
+ const numeric = column != null &&
1069
+ (this.platform.isNumericColumn(column.mappedType) ||
1070
+ [DecimalType, FloatType, DoubleType].some(t => column.mappedType instanceof t));
1071
+ if (item.columnName && m1 && m2 && !numeric) {
1067
1072
  /* v8 ignore next */
1068
1073
  const parts = m2[1].match(/('(?:[^']|'')*'::\w[\w ]*)/g) ?? m2[1].split(',');
1069
1074
  let items = parts.map((item) => /^\(?'((?:[^']|'')*)'/.exec(item.trim())?.[1]?.replace(/''/g, "'"));
1070
1075
  items = items.filter(item => item !== undefined);
1071
1076
  if (items.length > 0) {
1072
1077
  o[item.columnName] = items;
1078
+ // kept to compare with a plain check of the same values, e.g. `kind = 'manual'` is stored like a single item enum
1079
+ item.introspectedExpression = item.expression;
1073
1080
  item.expression = this.platform.getEnumCheckConstraintExpression(item.columnName, items);
1074
1081
  item.definition = `check (${item.expression})`;
1075
1082
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.2.9-dev.19",
3
+ "version": "7.2.9-dev.20",
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.2.8"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.9-dev.19"
56
+ "@mikro-orm/core": "7.2.9-dev.20"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -483,6 +483,12 @@ export class SchemaComparator {
483
483
  if (!this.diffExpression(check.expression, toTableCheck.expression)) {
484
484
  continue;
485
485
  }
486
+ // a check read as the one of an enum can be a plain check of the same values, e.g. `kind = 'manual'`
487
+ const introspected = check.introspectedExpression ?? toTableCheck.introspectedExpression;
488
+ const declared = check.introspectedExpression ? toTableCheck.expression : check.expression;
489
+ if (introspected && !this.diffExpression(introspected, declared)) {
490
+ continue;
491
+ }
486
492
  // only auto-generated enum checks, a user check tagged with an enum column must be diffed by expression
487
493
  if (fromColumn?.enumItems &&
488
494
  toColumn?.enumItems &&
@@ -925,12 +931,13 @@ export class SchemaComparator {
925
931
  // Protect dots inside string literals before the quote strip below, or the alias-prefix normalization
926
932
  // would mangle literal contents — `current_setting('app.tenant')` and `current_setting('req.tenant')`
927
933
  // must not both collapse to `current_settingtenant`
928
- .replace(/'([^']*)'/g, (_, inner) => `'${inner.replaceAll('.', '\u0000')}'`)
934
+ // numbers are left alone, postgres quotes the negative ones (`'-1.5'::numeric`)
935
+ .replace(/'([^']*)'/g, (m, inner) => /^-?\d+(\.\d+)?$/.test(inner) ? m : `'${inner.replaceAll('.', '\u0000')}'`)
929
936
  // Remove quotes first so we can process identifiers
930
937
  .replace(/['"`]/g, '')
931
938
  // MySQL adds table/alias prefixes to columns (e.g., a.name or table_name.column vs just column)
932
939
  // Strip these prefixes - match word.word patterns and keep only the last part
933
- .replace(/\b\w+\.(\w+)/g, '$1')
940
+ .replace(/\b(?!\d)\w+\.(\w+)/g, '$1')
934
941
  // PostgreSQL rewrites `x [not] between a and b` to `x >= a and x <= b` (`x < a or x > b`), cast targets are skipped
935
942
  .replace(/(?<!::\s*)\b(?!not\b)(\w+)\s+(not\s+)?between\s+(\S+)\s+and\s+(\S+)/gi, (_, x, not, a, b) => not ? `${x} < ${a} or ${x} > ${b}` : `${x} >= ${a} and ${x} <= ${b}`)
936
943
  // Normalize JOIN syntax: inner join -> join (equivalent in SQL)
package/typings.d.ts CHANGED
@@ -113,6 +113,8 @@ export interface CheckDef<T = unknown> {
113
113
  expression: string | CheckCallback<T>;
114
114
  definition?: string;
115
115
  columnName?: string;
116
+ /** @internal the expression as introspected, before it was normalized as the check of an enum */
117
+ introspectedExpression?: string;
116
118
  }
117
119
  /** Resolved row level security policy for schema operations (all callbacks resolved to strings). */
118
120
  export interface SqlPolicyDef {