@mikro-orm/sql 7.1.2-dev.2 → 7.1.2-dev.4

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.
@@ -479,8 +479,11 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
479
479
  }
480
480
  seen.add(dedupeKey);
481
481
  ret[key] ??= [];
482
+ // CHECK: unwrap the `CHECK ((<predicate>))` shell and drop pg-added `::type` casts so the
483
+ // inner predicate matches the user's metadata. EXCLUDE bodies are kept verbatim — the user's
484
+ // `@Check` expression is the full body (see SchemaHelper.createCheck).
482
485
  const m = /^check \(\((.*)\)\)$/is.exec(check.expression);
483
- const def = m?.[1].replace(/\((.*?)\)::\w+/g, '$1');
486
+ const def = m ? m[1].replace(/\((.*?)\)::\w+/g, '$1') : check.expression;
484
487
  ret[key].push({
485
488
  name: check.name,
486
489
  columnName: check.column_name,
@@ -1073,16 +1076,31 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
1073
1076
  join pg_am as am on am.oid = i.relam
1074
1077
  left join pg_constraint as c on c.conname = i.relname
1075
1078
  where indrelid in (${tables.map(t => `${this.platform.quoteValue(`${this.quote(t.schema_name)}.${this.quote(t.table_name)}`)}::regclass`).join(', ')})
1079
+ and (c.contype is null or c.contype <> 'x')
1076
1080
  order by relname`;
1077
1081
  }
1078
1082
  getChecksSQL(tablesBySchemas) {
1083
+ const checkFilter = [...tablesBySchemas.entries()]
1084
+ .map(([schema, tables]) => `ccu.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}) and ccu.table_schema = ${this.platform.quoteValue(schema)}`)
1085
+ .join(' or ');
1086
+ // EXCLUDE constraints don't appear in information_schema.constraint_column_usage, so the second
1087
+ // branch resolves the table from pg_class/pg_namespace directly.
1088
+ const excludeFilter = [...tablesBySchemas.entries()]
1089
+ .map(([schema, tables]) => `cls.relname in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}) and nsp.nspname = ${this.platform.quoteValue(schema)}`)
1090
+ .join(' or ');
1079
1091
  return `select ccu.table_name as table_name, ccu.table_schema as schema_name, pgc.conname as name, conrelid::regclass as table_from, ccu.column_name as column_name, pg_get_constraintdef(pgc.oid) as expression
1080
1092
  from pg_constraint pgc
1081
1093
  join pg_namespace nsp on nsp.oid = pgc.connamespace
1082
1094
  join pg_class cls on pgc.conrelid = cls.oid
1083
1095
  join information_schema.constraint_column_usage ccu on pgc.conname = ccu.constraint_name and nsp.nspname = ccu.constraint_schema and cls.relname = ccu.table_name
1084
- where contype = 'c' and (${[...tablesBySchemas.entries()].map(([schema, tables]) => `ccu.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}) and ccu.table_schema = ${this.platform.quoteValue(schema)}`).join(' or ')})
1085
- order by pgc.conname`;
1096
+ where pgc.contype = 'c' and (${checkFilter})
1097
+ union all
1098
+ select cls.relname as table_name, nsp.nspname as schema_name, pgc.conname as name, conrelid::regclass as table_from, null as column_name, pg_get_constraintdef(pgc.oid) as expression
1099
+ from pg_constraint pgc
1100
+ join pg_namespace nsp on nsp.oid = pgc.connamespace
1101
+ join pg_class cls on pgc.conrelid = cls.oid
1102
+ where pgc.contype = 'x' and (${excludeFilter})
1103
+ order by name`;
1086
1104
  }
1087
1105
  inferLengthFromColumnType(type) {
1088
1106
  const match = /^(\w+(?:\s+\w+)*)\s*(?:\(\s*(\d+)\s*\)|$)/.exec(type);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.1.2-dev.2",
3
+ "version": "7.1.2-dev.4",
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.1.1"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.1.2-dev.2"
56
+ "@mikro-orm/core": "7.1.2-dev.4"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -138,6 +138,8 @@ export declare abstract class SchemaHelper {
138
138
  getReferencedTableName(referencedTableName: string, schema?: string): string;
139
139
  createIndex(index: IndexDef, table: DatabaseTable, createPrimary?: boolean): string;
140
140
  createCheck(table: DatabaseTable, check: CheckDef): string;
141
+ /** True for `@Check` expressions that are a full table-constraint body (e.g. PostgreSQL `exclude using gist (...)`) and must be emitted verbatim instead of wrapped in `check (...)`. */
142
+ private isRawConstraintBody;
141
143
  /**
142
144
  * Generates SQL to create a database trigger on a table.
143
145
  * Override in driver-specific helpers for custom DDL (e.g., PostgreSQL function wrapping).
@@ -813,7 +813,13 @@ export class SchemaHelper {
813
813
  return this.getCreateIndexSQL(table.getShortestName(), index);
814
814
  }
815
815
  createCheck(table, check) {
816
- return `alter table ${table.getQuotedName()} add constraint ${this.quote(check.name)} check (${check.expression})`;
816
+ const expression = check.expression;
817
+ const body = this.isRawConstraintBody(expression) ? expression : `check (${expression})`;
818
+ return `alter table ${table.getQuotedName()} add constraint ${this.quote(check.name)} ${body}`;
819
+ }
820
+ /** True for `@Check` expressions that are a full table-constraint body (e.g. PostgreSQL `exclude using gist (...)`) and must be emitted verbatim instead of wrapped in `check (...)`. */
821
+ isRawConstraintBody(expression) {
822
+ return /^\s*exclude\b/i.test(expression);
817
823
  }
818
824
  /**
819
825
  * Generates SQL to create a database trigger on a table.