drizzle-kit 0.24.0-6205f01 → 0.24.0-8d86512

Sign up to get free protection for your applications and to get access to all the features.
package/bin.cjs CHANGED
@@ -5687,11 +5687,30 @@ var init_mysqlSchema = __esm({
5687
5687
  }
5688
5688
  });
5689
5689
 
5690
+ // src/extensions/vector.ts
5691
+ var vectorOps;
5692
+ var init_vector = __esm({
5693
+ "src/extensions/vector.ts"() {
5694
+ "use strict";
5695
+ vectorOps = [
5696
+ "vector_l2_ops",
5697
+ "vector_ip_ops",
5698
+ "vector_cosine_ops",
5699
+ "vector_l1_ops",
5700
+ "bit_hamming_ops",
5701
+ "bit_jaccard_ops",
5702
+ "halfvec_l2_ops",
5703
+ "sparsevec_l2_ops"
5704
+ ];
5705
+ }
5706
+ });
5707
+
5690
5708
  // src/serializer/pgSchema.ts
5691
5709
  var indexV2, columnV2, tableV2, enumSchemaV1, enumSchema, pgSchemaV2, references, columnV1, tableV1, pgSchemaV1, indexColumn, index2, indexV4, indexV5, indexV6, fk2, sequenceSchema, sequenceSquashed, columnV7, column2, columnSquashed, tableV32, compositePK2, uniqueConstraint2, tableV42, tableV5, tableV6, tableV7, table2, schemaHash2, kitInternals2, pgSchemaInternalV3, pgSchemaInternalV4, pgSchemaInternalV5, pgSchemaInternalV6, pgSchemaExternal, pgSchemaInternalV7, pgSchemaInternal, tableSquashed2, tableSquashedV42, pgSchemaSquashedV4, pgSchemaSquashedV6, pgSchemaSquashed, pgSchemaV3, pgSchemaV4, pgSchemaV5, pgSchemaV6, pgSchemaV7, pgSchema, backwardCompatiblePgSchema, PgSquasher, squashPgScheme, dryPg;
5692
5710
  var init_pgSchema = __esm({
5693
5711
  "src/serializer/pgSchema.ts"() {
5694
5712
  "use strict";
5713
+ init_vector();
5695
5714
  init_global();
5696
5715
  init_lib();
5697
5716
  indexV2 = objectType({
@@ -5938,7 +5957,8 @@ var init_pgSchema = __esm({
5938
5957
  objectType({
5939
5958
  isArray: booleanType().optional(),
5940
5959
  dimensions: numberType().optional(),
5941
- rawType: stringType().optional()
5960
+ rawType: stringType().optional(),
5961
+ isDefaultAnExpression: booleanType().optional()
5942
5962
  }).optional()
5943
5963
  )
5944
5964
  }).optional()
@@ -6076,7 +6096,7 @@ var init_pgSchema = __esm({
6076
6096
  squashIdx: (idx) => {
6077
6097
  index2.parse(idx);
6078
6098
  return `${idx.name};${idx.columns.map(
6079
- (c) => `${c.expression}--${c.isExpression}--${c.asc}--${c.nulls}--${c.opclass}`
6099
+ (c) => `${c.expression}--${c.isExpression}--${c.asc}--${c.nulls}--${c.opclass && vectorOps.includes(c.opclass) ? c.opclass : ""}`
6080
6100
  ).join(",,")};${idx.isUnique};${idx.concurrently};${idx.method};${idx.where};${JSON.stringify(idx.with)}`;
6081
6101
  },
6082
6102
  unsquashIdx: (input) => {
@@ -11372,7 +11392,7 @@ var init_cli = __esm({
11372
11392
  extensionsFilters: literalType("postgis").array().optional(),
11373
11393
  introspectCasing: casing,
11374
11394
  breakpoints: booleanType().optional().default(true),
11375
- database: objectType({
11395
+ migrations: objectType({
11376
11396
  prefix: prefix.optional().default("index")
11377
11397
  }).optional()
11378
11398
  }).passthrough();
@@ -17510,7 +17530,7 @@ var init_utils4 = __esm({
17510
17530
  credentials: parsed2.data,
17511
17531
  tablesFilter,
17512
17532
  schemasFilter,
17513
- prefix: ((_a = config.database) == null ? void 0 : _a.prefix) || "index"
17533
+ prefix: ((_a = config.migrations) == null ? void 0 : _a.prefix) || "index"
17514
17534
  };
17515
17535
  }
17516
17536
  if (dialect7 === "mysql") {
@@ -17527,7 +17547,7 @@ var init_utils4 = __esm({
17527
17547
  credentials: parsed2.data,
17528
17548
  tablesFilter,
17529
17549
  schemasFilter,
17530
- prefix: ((_b = config.database) == null ? void 0 : _b.prefix) || "index"
17550
+ prefix: ((_b = config.migrations) == null ? void 0 : _b.prefix) || "index"
17531
17551
  };
17532
17552
  }
17533
17553
  if (dialect7 === "sqlite") {
@@ -17544,7 +17564,7 @@ var init_utils4 = __esm({
17544
17564
  credentials: parsed2.data,
17545
17565
  tablesFilter,
17546
17566
  schemasFilter,
17547
- prefix: ((_c = config.database) == null ? void 0 : _c.prefix) || "index"
17567
+ prefix: ((_c = config.migrations) == null ? void 0 : _c.prefix) || "index"
17548
17568
  };
17549
17569
  }
17550
17570
  assertUnreachable(dialect7);
@@ -18372,24 +18392,6 @@ var init_pgImports = __esm({
18372
18392
  }
18373
18393
  });
18374
18394
 
18375
- // src/extensions/vector.ts
18376
- var vectorOps;
18377
- var init_vector = __esm({
18378
- "src/extensions/vector.ts"() {
18379
- "use strict";
18380
- vectorOps = [
18381
- "vector_l2_ops",
18382
- "vector_ip_ops",
18383
- "vector_cosine_ops",
18384
- "vector_l1_ops",
18385
- "bit_hamming_ops",
18386
- "bit_jaccard_ops",
18387
- "halfvec_l2_ops",
18388
- "sparsevec_l2_ops"
18389
- ];
18390
- }
18391
- });
18392
-
18393
18395
  // src/serializer/pgSerializer.ts
18394
18396
  var pgSerializer_exports = {};
18395
18397
  __export(pgSerializer_exports, {
@@ -18433,7 +18435,7 @@ function buildArrayString(array, sqlType) {
18433
18435
  }).join(",");
18434
18436
  return `{${values}}`;
18435
18437
  }
18436
- var import_drizzle_orm5, import_pg_core2, import_pg_core3, dialect5, indexName2, generatePgSnapshot, trimChar, fromDatabase2, columnToDefault, defaultForColumn;
18438
+ var import_drizzle_orm5, import_pg_core2, import_pg_core3, dialect5, indexName2, generatePgSnapshot, trimChar, fromDatabase2, columnToDefault, columnEnumNameToDefault, defaultForColumn;
18437
18439
  var init_pgSerializer = __esm({
18438
18440
  "src/serializer/pgSerializer.ts"() {
18439
18441
  "use strict";
@@ -18908,7 +18910,7 @@ ${withStyle.errorWarning(
18908
18910
  WHEN 'int2'::regtype THEN 'smallserial'
18909
18911
  END
18910
18912
  ELSE format_type(a.atttypid, a.atttypmod)
18911
- END AS data_type, INFORMATION_SCHEMA.COLUMNS.table_name,
18913
+ END AS data_type, INFORMATION_SCHEMA.COLUMNS.table_name, ns.nspname as type_schema,
18912
18914
  pg_get_serial_sequence('"${tableSchema}"."${tableName}"', a.attname)::regclass as seq_name, INFORMATION_SCHEMA.COLUMNS.column_name,
18913
18915
  INFORMATION_SCHEMA.COLUMNS.column_default, INFORMATION_SCHEMA.COLUMNS.data_type as additional_dt,
18914
18916
  INFORMATION_SCHEMA.COLUMNS.udt_name as enum_name,
@@ -18919,6 +18921,7 @@ ${withStyle.errorWarning(
18919
18921
  INFORMATION_SCHEMA.COLUMNS.identity_cycle
18920
18922
  FROM pg_attribute a
18921
18923
  JOIN INFORMATION_SCHEMA.COLUMNS ON INFORMATION_SCHEMA.COLUMNS.column_name = a.attname
18924
+ JOIN pg_type t ON t.oid = a.atttypid LEFT JOIN pg_namespace ns ON ns.oid = t.typnamespace
18922
18925
  WHERE a.attrelid = '"${tableSchema}"."${tableName}"'::regclass and INFORMATION_SCHEMA.COLUMNS.table_name = '${tableName}' and INFORMATION_SCHEMA.COLUMNS.table_schema = '${tableSchema}'
18923
18926
  AND a.attnum > 0
18924
18927
  AND NOT a.attisdropped
@@ -18938,30 +18941,42 @@ ${withStyle.errorWarning(
18938
18941
  }
18939
18942
  const tableForeignKeys = await db.query(
18940
18943
  `SELECT
18941
- tc.table_schema,
18942
- tc.constraint_name,
18943
- tc.table_name,
18944
- kcu.column_name,
18945
- (
18946
- SELECT ccu.table_schema
18947
- FROM information_schema.constraint_column_usage ccu
18948
- WHERE ccu.constraint_name = tc.constraint_name
18949
- LIMIT 1
18950
- ) AS foreign_table_schema,
18951
- ccu.table_name AS foreign_table_name,
18952
- ccu.column_name AS foreign_column_name,
18953
- rc.delete_rule,
18954
- rc.update_rule
18955
- FROM
18956
- information_schema.table_constraints AS tc
18957
- JOIN information_schema.key_column_usage AS kcu
18958
- ON tc.constraint_name = kcu.constraint_name
18959
- AND tc.table_schema = kcu.table_schema
18960
- JOIN information_schema.constraint_column_usage AS ccu
18961
- ON ccu.constraint_name = tc.constraint_name
18962
- JOIN information_schema.referential_constraints AS rc
18963
- ON ccu.constraint_name = rc.constraint_name
18964
- WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='${tableName}' and tc.table_schema='${tableSchema}';`
18944
+ con.contype AS constraint_type,
18945
+ nsp.nspname AS constraint_schema,
18946
+ con.conname AS constraint_name,
18947
+ rel.relname AS table_name,
18948
+ att.attname AS column_name,
18949
+ fnsp.nspname AS foreign_table_schema,
18950
+ frel.relname AS foreign_table_name,
18951
+ fatt.attname AS foreign_column_name,
18952
+ CASE con.confupdtype
18953
+ WHEN 'a' THEN 'NO ACTION'
18954
+ WHEN 'r' THEN 'RESTRICT'
18955
+ WHEN 'n' THEN 'SET NULL'
18956
+ WHEN 'c' THEN 'CASCADE'
18957
+ WHEN 'd' THEN 'SET DEFAULT'
18958
+ END AS update_rule,
18959
+ CASE con.confdeltype
18960
+ WHEN 'a' THEN 'NO ACTION'
18961
+ WHEN 'r' THEN 'RESTRICT'
18962
+ WHEN 'n' THEN 'SET NULL'
18963
+ WHEN 'c' THEN 'CASCADE'
18964
+ WHEN 'd' THEN 'SET DEFAULT'
18965
+ END AS delete_rule
18966
+ FROM
18967
+ pg_catalog.pg_constraint con
18968
+ JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
18969
+ JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace
18970
+ LEFT JOIN pg_catalog.pg_attribute att ON att.attnum = ANY (con.conkey)
18971
+ AND att.attrelid = con.conrelid
18972
+ LEFT JOIN pg_catalog.pg_class frel ON frel.oid = con.confrelid
18973
+ LEFT JOIN pg_catalog.pg_namespace fnsp ON fnsp.oid = frel.relnamespace
18974
+ LEFT JOIN pg_catalog.pg_attribute fatt ON fatt.attnum = ANY (con.confkey)
18975
+ AND fatt.attrelid = con.confrelid
18976
+ WHERE
18977
+ nsp.nspname = '${tableSchema}'
18978
+ AND rel.relname = '${tableName}'
18979
+ AND con.contype IN ('f');`
18965
18980
  );
18966
18981
  foreignKeysCount += tableForeignKeys.length;
18967
18982
  if (progressCallback) {
@@ -19019,6 +19034,7 @@ ${withStyle.errorWarning(
19019
19034
  const columnDimensions = columnResponse.array_dimensions;
19020
19035
  const enumType2 = columnResponse.enum_name;
19021
19036
  let columnType = columnResponse.data_type;
19037
+ const typeSchema = columnResponse.type_schema;
19022
19038
  const isGenerated = columnResponse.is_generated === "ALWAYS";
19023
19039
  const generationExpression = columnResponse.generation_expression;
19024
19040
  const isIdentity = columnResponse.is_identity === "YES";
@@ -19050,6 +19066,25 @@ ${withStyle.errorWarning(
19050
19066
  };
19051
19067
  }
19052
19068
  const defaultValue = defaultForColumn(columnResponse);
19069
+ if (defaultValue === "NULL") {
19070
+ if (typeof internals.tables[tableName] === "undefined") {
19071
+ internals.tables[tableName] = {
19072
+ columns: {
19073
+ [columnName]: {
19074
+ isDefaultAnExpression: true
19075
+ }
19076
+ }
19077
+ };
19078
+ } else {
19079
+ if (typeof internals.tables[tableName].columns[columnName] === "undefined") {
19080
+ internals.tables[tableName].columns[columnName] = {
19081
+ isDefaultAnExpression: true
19082
+ };
19083
+ } else {
19084
+ internals.tables[tableName].columns[columnName].isDefaultAnExpression = true;
19085
+ }
19086
+ }
19087
+ }
19053
19088
  const isSerial = columnType === "serial";
19054
19089
  let columnTypeMapped = columnType;
19055
19090
  if (columnTypeMapped.startsWith("numeric(")) {
@@ -19095,7 +19130,7 @@ ${withStyle.errorWarning(
19095
19130
  // filter vectors, but in future we should filter any extension that was installed by user
19096
19131
  columnAdditionalDT === "USER-DEFINED" && !["vector", "geometry"].includes(enumType2) ? enumType2 : columnTypeMapped
19097
19132
  ),
19098
- typeSchema: enumsToReturn[`${tableSchema}.${enumType2}`] !== void 0 ? enumsToReturn[`${tableSchema}.${enumType2}`].schema : void 0,
19133
+ typeSchema: enumsToReturn[`${typeSchema}.${enumType2}`] !== void 0 ? enumsToReturn[`${typeSchema}.${enumType2}`].schema : void 0,
19099
19134
  primaryKey: primaryKey.length === 1 && cprimaryKey.length < 2,
19100
19135
  // default: isSerial ? undefined : defaultValue,
19101
19136
  notNull: columnResponse.is_nullable === "NO",
@@ -19270,11 +19305,11 @@ ${withStyle.errorWarning(
19270
19305
  columnToDefault = {
19271
19306
  "numeric(": "::numeric",
19272
19307
  // text: "::text",
19273
- // "character varying": "::character varying",
19308
+ "character varying": "::character varying",
19274
19309
  // "double precision": "::double precision",
19275
19310
  // "time with time zone": "::time with time zone",
19276
19311
  "time without time zone": "::time without time zone",
19277
- // "timestamp with time zone": "::timestamp with time zone",
19312
+ // 'timestamp with time zone': '::timestamp with time zone',
19278
19313
  "timestamp without time zone": "::timestamp without time zone",
19279
19314
  "timestamp(": "::timestamp without time zone",
19280
19315
  // date: "::date",
@@ -19288,6 +19323,12 @@ ${withStyle.errorWarning(
19288
19323
  // json: "::json",
19289
19324
  "character(": "::bpchar"
19290
19325
  };
19326
+ columnEnumNameToDefault = {
19327
+ timestamptz: "::timestamp with time zone",
19328
+ timestmap: "::time without time zone",
19329
+ time: "::time without time zone",
19330
+ timetz: "::time with time zone"
19331
+ };
19291
19332
  defaultForColumn = (column7) => {
19292
19333
  if (column7.column_default === null) {
19293
19334
  return void 0;
@@ -19296,19 +19337,33 @@ ${withStyle.errorWarning(
19296
19337
  return void 0;
19297
19338
  }
19298
19339
  const hasDifferentDefaultCast = Object.keys(columnToDefault).find((it) => column7.data_type.startsWith(it));
19340
+ const hasDifferentDefaultCastForEnum = Object.keys(columnEnumNameToDefault).find(
19341
+ (it) => column7.enum_name.startsWith(it)
19342
+ );
19299
19343
  const columnDefaultAsString = column7.column_default.toString();
19300
- if (columnDefaultAsString.endsWith(
19344
+ const endsWithEnumName = columnDefaultAsString.endsWith(
19345
+ hasDifferentDefaultCastForEnum ? columnEnumNameToDefault[hasDifferentDefaultCastForEnum] : column7.data_type
19346
+ );
19347
+ const endsWithTypeName = columnDefaultAsString.endsWith(
19301
19348
  hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : column7.data_type
19302
- )) {
19349
+ );
19350
+ if (endsWithTypeName || endsWithEnumName) {
19303
19351
  const nonPrefixPart = column7.column_default.length - (hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column7.data_type}`).length - 1;
19304
- const rt = column7.column_default.toString().substring(1, nonPrefixPart);
19352
+ let rt = column7.column_default.toString().substring(0, nonPrefixPart + 1);
19305
19353
  if (/^-?[\d.]+(?:e-?\d+)?$/.test(rt) && !column7.data_type.startsWith("numeric")) {
19306
19354
  return Number(rt);
19307
19355
  } else if (column7.data_type === "json" || column7.data_type === "jsonb") {
19356
+ if (rt.startsWith("'")) {
19357
+ rt = rt.slice(1, -1);
19358
+ }
19308
19359
  const jsonWithoutSpaces = JSON.stringify(JSON.parse(rt));
19309
19360
  return `'${jsonWithoutSpaces}'${hasDifferentDefaultCast ? columnToDefault[hasDifferentDefaultCast] : `::${column7.data_type}`}`;
19310
19361
  } else if (column7.data_type === "boolean") {
19311
19362
  return column7.column_default === "true";
19363
+ } else if (rt === "NULL") {
19364
+ return `NULL`;
19365
+ } else if (rt.startsWith("'") && rt.endsWith("'")) {
19366
+ return rt;
19312
19367
  } else {
19313
19368
  return `'${rt}'`;
19314
19369
  }
@@ -19317,8 +19372,10 @@ ${withStyle.errorWarning(
19317
19372
  return Number(columnDefaultAsString);
19318
19373
  } else if (column7.data_type === "boolean") {
19319
19374
  return column7.column_default === "true";
19375
+ } else if (columnDefaultAsString === "NULL") {
19376
+ return `NULL`;
19320
19377
  } else {
19321
- return `${columnDefaultAsString}`;
19378
+ return `${columnDefaultAsString.replace(/\\/g, "`\\")}`;
19322
19379
  }
19323
19380
  }
19324
19381
  };
@@ -74154,7 +74211,7 @@ var init_pgIntrospect = __esm({
74154
74211
  });
74155
74212
 
74156
74213
  // src/introspect-sqlite.ts
74157
- var sqliteImportsList, indexName3, objToStatement2, relations, withCasing, schemaToTypeScript, isCyclic, isSelf, mapColumnDefault, column4, createTableColumns, createTableIndexes, createTableUniques, createTablePKs, createTableFKs;
74214
+ var sqliteImportsList, indexName3, objToStatement2, relations, escapeColumnKey, withCasing, schemaToTypeScript, isCyclic, isSelf, mapColumnDefault, column4, createTableColumns, createTableIndexes, createTableUniques, createTablePKs, createTableFKs;
74158
74215
  var init_introspect_sqlite = __esm({
74159
74216
  "src/introspect-sqlite.ts"() {
74160
74217
  "use strict";
@@ -74181,12 +74238,18 @@ var init_introspect_sqlite = __esm({
74181
74238
  return statement;
74182
74239
  };
74183
74240
  relations = /* @__PURE__ */ new Set();
74241
+ escapeColumnKey = (value) => {
74242
+ if (/^(?![a-zA-Z_$][a-zA-Z0-9_$]*$).+$/.test(value)) {
74243
+ return `"${value}"`;
74244
+ }
74245
+ return value;
74246
+ };
74184
74247
  withCasing = (value, casing2) => {
74185
- if (typeof casing2 === "undefined") {
74186
- return value;
74248
+ if (casing2 === "preserve") {
74249
+ return escapeColumnKey(value);
74187
74250
  }
74188
74251
  if (casing2 === "camel") {
74189
- return value.camelCase();
74252
+ return escapeColumnKey(value.camelCase());
74190
74253
  }
74191
74254
  return value;
74192
74255
  };
@@ -75184,7 +75247,7 @@ var require_pluralize = __commonJS({
75184
75247
  });
75185
75248
 
75186
75249
  // src/introspect-mysql.ts
75187
- var mysqlImportsList, objToStatement22, timeConfig, binaryConfig, importsPatch, relations2, prepareCasing, schemaToTypeScript2, isCyclic2, isSelf2, mapColumnDefault2, mapColumnDefaultForJson, column5, createTableColumns2, createTableIndexes2, createTableUniques2, createTablePKs2, createTableFKs2;
75250
+ var mysqlImportsList, objToStatement22, timeConfig, binaryConfig, importsPatch, relations2, escapeColumnKey2, prepareCasing, schemaToTypeScript2, isCyclic2, isSelf2, mapColumnDefault2, mapColumnDefaultForJson, column5, createTableColumns2, createTableIndexes2, createTableUniques2, createTablePKs2, createTableFKs2;
75188
75251
  var init_introspect_mysql = __esm({
75189
75252
  "src/introspect-mysql.ts"() {
75190
75253
  "use strict";
@@ -75255,14 +75318,20 @@ var init_introspect_mysql = __esm({
75255
75318
  "timestamp without time zone": "timestamp"
75256
75319
  };
75257
75320
  relations2 = /* @__PURE__ */ new Set();
75321
+ escapeColumnKey2 = (value) => {
75322
+ if (/^(?![a-zA-Z_$][a-zA-Z0-9_$]*$).+$/.test(value)) {
75323
+ return `"${value}"`;
75324
+ }
75325
+ return value;
75326
+ };
75258
75327
  prepareCasing = (casing2) => (value) => {
75259
- if (typeof casing2 === "undefined") {
75260
- return value;
75328
+ if (casing2 === "preserve") {
75329
+ return escapeColumnKey2(value);
75261
75330
  }
75262
75331
  if (casing2 === "camel") {
75263
- return value.camelCase();
75332
+ return escapeColumnKey2(value.camelCase());
75264
75333
  }
75265
- return value;
75334
+ return escapeColumnKey2(value);
75266
75335
  };
75267
75336
  schemaToTypeScript2 = (schema5, casing2) => {
75268
75337
  const withCasing4 = prepareCasing(casing2);
@@ -75728,7 +75797,7 @@ import { sql } from "drizzle-orm"
75728
75797
  });
75729
75798
 
75730
75799
  // src/introspect-pg.ts
75731
- var import_drizzle_orm8, import_relations, pgImportsList, objToStatement23, timeConfig2, possibleIntervals, intervalStrToObj, intervalConfig, importsPatch2, relations3, withCasing2, paramNameFor, schemaToTypeScript3, isCyclic3, isSelf3, column6, dimensionsInArray, createTableColumns3, createTableIndexes3, createTablePKs3, createTableUniques3, createTableFKs3;
75800
+ var import_drizzle_orm8, import_relations, pgImportsList, timeConfig2, possibleIntervals, intervalStrToObj, intervalConfig, mapColumnDefault3, importsPatch2, relations3, escapeColumnKey3, withCasing2, paramNameFor, schemaToTypeScript3, isCyclic3, isSelf3, column6, dimensionsInArray, createTableColumns3, createTableIndexes3, createTablePKs3, createTableUniques3, createTableFKs3;
75732
75801
  var init_introspect_pg = __esm({
75733
75802
  "src/introspect-pg.ts"() {
75734
75803
  "use strict";
@@ -75772,16 +75841,6 @@ var init_introspect_pg = __esm({
75772
75841
  "line",
75773
75842
  "geometry"
75774
75843
  ]);
75775
- objToStatement23 = (json) => {
75776
- json = Object.fromEntries(Object.entries(json).filter((it) => it[1]));
75777
- const keys = Object.keys(json);
75778
- if (keys.length === 0)
75779
- return;
75780
- let statement = "{ ";
75781
- statement += keys.map((it) => `${it}: "${json[it]}"`).join(", ");
75782
- statement += " }";
75783
- return statement;
75784
- };
75785
75844
  timeConfig2 = (json) => {
75786
75845
  json = Object.fromEntries(Object.entries(json).filter((it) => it[1]));
75787
75846
  const keys = Object.keys(json);
@@ -75841,6 +75900,12 @@ var init_introspect_pg = __esm({
75841
75900
  statement += " }";
75842
75901
  return statement;
75843
75902
  };
75903
+ mapColumnDefault3 = (defaultValue, isExpression) => {
75904
+ if (isExpression) {
75905
+ return `sql\`${defaultValue}\``;
75906
+ }
75907
+ return defaultValue;
75908
+ };
75844
75909
  importsPatch2 = {
75845
75910
  "double precision": "doublePrecision",
75846
75911
  "timestamp without time zone": "timestamp",
@@ -75849,12 +75914,18 @@ var init_introspect_pg = __esm({
75849
75914
  "time with time zone": "time"
75850
75915
  };
75851
75916
  relations3 = /* @__PURE__ */ new Set();
75917
+ escapeColumnKey3 = (value) => {
75918
+ if (/^(?![a-zA-Z_$][a-zA-Z0-9_$]*$).+$/.test(value)) {
75919
+ return `"${value}"`;
75920
+ }
75921
+ return value;
75922
+ };
75852
75923
  withCasing2 = (value, casing2) => {
75853
75924
  if (casing2 === "preserve") {
75854
- return value;
75925
+ return escapeColumnKey3(value);
75855
75926
  }
75856
75927
  if (casing2 === "camel") {
75857
- return value.camelCase();
75928
+ return escapeColumnKey3(value.camelCase());
75858
75929
  }
75859
75930
  assertUnreachable(casing2);
75860
75931
  };
@@ -75874,7 +75945,13 @@ var init_introspect_pg = __esm({
75874
75945
  return [it[0], withCasing2(it[1], casing2)];
75875
75946
  })
75876
75947
  );
75877
- const enumTypes = new Set(Object.values(schema5.enums).map((it) => it.name));
75948
+ const enumTypes = Object.values(schema5.enums).reduce(
75949
+ (acc, cur) => {
75950
+ acc.add(`${cur.schema}.${cur.name}`);
75951
+ return acc;
75952
+ },
75953
+ /* @__PURE__ */ new Set()
75954
+ );
75878
75955
  const imports = Object.values(schema5.tables).reduce(
75879
75956
  (res, it) => {
75880
75957
  const idxImports = Object.values(it.indexes).map((idx) => idx.isUnique ? "uniqueIndex" : "index");
@@ -75895,9 +75972,6 @@ var init_introspect_pg = __esm({
75895
75972
  res.pg.push(...fkImpots);
75896
75973
  res.pg.push(...pkImports);
75897
75974
  res.pg.push(...uniqueImports);
75898
- if (enumTypes.size > 0) {
75899
- res.pg.push("pgEnum");
75900
- }
75901
75975
  const columnImports = Object.values(it.columns).map((col) => {
75902
75976
  let patched = importsPatch2[col.type] || col.type;
75903
75977
  patched = patched.startsWith("varchar(") ? "varchar" : patched;
@@ -75916,6 +75990,13 @@ var init_introspect_pg = __esm({
75916
75990
  },
75917
75991
  { pg: [] }
75918
75992
  );
75993
+ Object.values(schema5.enums).forEach((it) => {
75994
+ if (it.schema && it.schema !== "public" && it.schema !== "") {
75995
+ imports.pg.push("pgSchema");
75996
+ } else if (it.schema === "public") {
75997
+ imports.pg.push("pgEnum");
75998
+ }
75999
+ });
75919
76000
  const enumStatements = Object.values(schema5.enums).map((it) => {
75920
76001
  const enumSchema3 = schemas[it.schema];
75921
76002
  const paramName = paramNameFor(it.name, enumSchema3);
@@ -75944,10 +76025,7 @@ var init_introspect_pg = __esm({
75944
76025
  schema5.internal
75945
76026
  );
75946
76027
  statement += "}";
75947
- const filteredFKs = Object.values(table4.foreignKeys).filter((it) => {
75948
- return it.columnsFrom.length > 1 || isSelf3(it);
75949
- });
75950
- if (Object.keys(table4.indexes).length > 0 || filteredFKs.length > 0 || Object.keys(table4.compositePrimaryKeys).length > 0 || Object.keys(table4.uniqueConstraints).length > 0) {
76028
+ if (Object.keys(table4.indexes).length > 0 || Object.values(table4.foreignKeys).length > 0 || Object.keys(table4.compositePrimaryKeys).length > 0 || Object.keys(table4.uniqueConstraints).length > 0) {
75951
76029
  statement += ",\n";
75952
76030
  statement += "(table) => {\n";
75953
76031
  statement += " return {\n";
@@ -75956,7 +76034,7 @@ var init_introspect_pg = __esm({
75956
76034
  Object.values(table4.indexes),
75957
76035
  casing2
75958
76036
  );
75959
- statement += createTableFKs3(Object.values(filteredFKs), schemas, casing2);
76037
+ statement += createTableFKs3(Object.values(table4.foreignKeys), schemas, casing2);
75960
76038
  statement += createTablePKs3(
75961
76039
  Object.values(table4.compositePrimaryKeys),
75962
76040
  casing2
@@ -75998,9 +76076,18 @@ var init_introspect_pg = __esm({
75998
76076
  isSelf3 = (fk4) => {
75999
76077
  return fk4.tableFrom === fk4.tableTo;
76000
76078
  };
76001
- column6 = (tableName, type, name, enumTypes, casing2, defaultValue, internals) => {
76002
- var _a;
76079
+ column6 = (tableName, type, name, enumTypes, typeSchema, casing2, defaultValue, internals) => {
76080
+ var _a, _b, _c;
76081
+ const isExpression = ((_b = (_a = internals == null ? void 0 : internals.tables[tableName]) == null ? void 0 : _a.columns[name]) == null ? void 0 : _b.isDefaultAnExpression) ?? false;
76003
76082
  const lowered = type.toLowerCase();
76083
+ if (enumTypes.has(`${typeSchema}.${type}`)) {
76084
+ let out = `${withCasing2(name, casing2)}: ${withCasing2(
76085
+ paramNameFor(type, typeSchema),
76086
+ casing2
76087
+ )}("${name}")`;
76088
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76089
+ return out;
76090
+ }
76004
76091
  if (lowered.startsWith("serial")) {
76005
76092
  return `${withCasing2(name, casing2)}: serial("${name}")`;
76006
76093
  }
@@ -76015,34 +76102,34 @@ var init_introspect_pg = __esm({
76015
76102
  }
76016
76103
  if (lowered.startsWith("integer")) {
76017
76104
  let out = `${withCasing2(name, casing2)}: integer("${name}")`;
76018
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76105
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76019
76106
  return out;
76020
76107
  }
76021
76108
  if (lowered.startsWith("smallint")) {
76022
76109
  let out = `${withCasing2(name, casing2)}: smallint("${name}")`;
76023
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76110
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76024
76111
  return out;
76025
76112
  }
76026
76113
  if (lowered.startsWith("bigint")) {
76027
76114
  let out = `// You can use { mode: "bigint" } if numbers are exceeding js number limitations
76028
76115
  `;
76029
76116
  out += `${withCasing2(name, casing2)}: bigint("${name}", { mode: "number" })`;
76030
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76117
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76031
76118
  return out;
76032
76119
  }
76033
76120
  if (lowered.startsWith("boolean")) {
76034
76121
  let out = `${withCasing2(name, casing2)}: boolean("${name}")`;
76035
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76122
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76036
76123
  return out;
76037
76124
  }
76038
76125
  if (lowered.startsWith("double precision")) {
76039
76126
  let out = `${withCasing2(name, casing2)}: doublePrecision("${name}")`;
76040
- out += defaultValue ? `.default(${defaultValue})` : "";
76127
+ out += defaultValue ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76041
76128
  return out;
76042
76129
  }
76043
76130
  if (lowered.startsWith("real")) {
76044
76131
  let out = `${withCasing2(name, casing2)}: real("${name}")`;
76045
- out += defaultValue ? `.default(${defaultValue})` : "";
76132
+ out += defaultValue ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76046
76133
  return out;
76047
76134
  }
76048
76135
  if (lowered.startsWith("uuid")) {
@@ -76058,7 +76145,7 @@ var init_introspect_pg = __esm({
76058
76145
  }
76059
76146
  let out = params ? `${withCasing2(name, casing2)}: numeric("${name}", ${timeConfig2(params)})` : `${withCasing2(name, casing2)}: numeric("${name}")`;
76060
76147
  defaultValue = defaultValue ? defaultValue.startsWith(`'`) && defaultValue.endsWith(`'`) ? defaultValue.substring(1, defaultValue.length - 1) : defaultValue : void 0;
76061
- out += defaultValue ? `.default('${defaultValue}')` : "";
76148
+ out += defaultValue ? `.default('${mapColumnDefault3(defaultValue, isExpression)}')` : "";
76062
76149
  return out;
76063
76150
  }
76064
76151
  if (lowered.startsWith("timestamp")) {
@@ -76073,7 +76160,7 @@ var init_introspect_pg = __esm({
76073
76160
  mode: "'string'"
76074
76161
  });
76075
76162
  let out = params ? `${withCasing2(name, casing2)}: timestamp("${name}", ${params})` : `${withCasing2(name, casing2)}: timestamp("${name}")`;
76076
- defaultValue = defaultValue === "now()" || defaultValue === "CURRENT_TIMESTAMP" ? ".defaultNow()" : defaultValue ? `.default(${defaultValue})` : "";
76163
+ defaultValue = defaultValue === "now()" ? ".defaultNow()" : defaultValue === "CURRENT_TIMESTAMP" ? ".default(sql`CURRENT_TIMESTAMP`)" : defaultValue ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76077
76164
  out += defaultValue;
76078
76165
  return out;
76079
76166
  }
@@ -76085,14 +76172,14 @@ var init_introspect_pg = __esm({
76085
76172
  precision = precision ? precision : null;
76086
76173
  const params = timeConfig2({ precision, withTimezone });
76087
76174
  let out = params ? `${withCasing2(name, casing2)}: time("${name}", ${params})` : `${withCasing2(name, casing2)}: time("${name}")`;
76088
- defaultValue = defaultValue === "now()" ? ".defaultNow()" : defaultValue ? `.default(${defaultValue})` : "";
76175
+ defaultValue = defaultValue === "now()" ? ".defaultNow()" : defaultValue ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76089
76176
  out += defaultValue;
76090
76177
  return out;
76091
76178
  }
76092
76179
  if (lowered.startsWith("interval")) {
76093
76180
  const params = intervalConfig(lowered);
76094
76181
  let out = params ? `${withCasing2(name, casing2)}: interval("${name}", ${params})` : `${withCasing2(name, casing2)}: interval("${name}")`;
76095
- out += defaultValue ? `.default(${defaultValue})` : "";
76182
+ out += defaultValue ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76096
76183
  return out;
76097
76184
  }
76098
76185
  if (lowered === "date") {
@@ -76122,22 +76209,22 @@ var init_introspect_pg = __esm({
76122
76209
  }
76123
76210
  if (lowered.startsWith("inet")) {
76124
76211
  let out = `${withCasing2(name, casing2)}: inet("${name}")`;
76125
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76212
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76126
76213
  return out;
76127
76214
  }
76128
76215
  if (lowered.startsWith("cidr")) {
76129
76216
  let out = `${withCasing2(name, casing2)}: cidr("${name}")`;
76130
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76217
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76131
76218
  return out;
76132
76219
  }
76133
76220
  if (lowered.startsWith("macaddr")) {
76134
76221
  let out = `${withCasing2(name, casing2)}: macaddr("${name}")`;
76135
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76222
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76136
76223
  return out;
76137
76224
  }
76138
76225
  if (lowered.startsWith("macaddr8")) {
76139
76226
  let out = `${withCasing2(name, casing2)}: macaddr8("${name}")`;
76140
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76227
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76141
76228
  return out;
76142
76229
  }
76143
76230
  if (lowered.startsWith("varchar")) {
@@ -76154,17 +76241,17 @@ var init_introspect_pg = __esm({
76154
76241
  } else {
76155
76242
  out = `${withCasing2(name, casing2)}: varchar("${name}")`;
76156
76243
  }
76157
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76244
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76158
76245
  return out;
76159
76246
  }
76160
76247
  if (lowered.startsWith("point")) {
76161
76248
  let out = `${withCasing2(name, casing2)}: point("${name}")`;
76162
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76249
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76163
76250
  return out;
76164
76251
  }
76165
76252
  if (lowered.startsWith("line")) {
76166
76253
  let out = `${withCasing2(name, casing2)}: point("${name}")`;
76167
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76254
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76168
76255
  return out;
76169
76256
  }
76170
76257
  if (lowered.startsWith("geometry")) {
@@ -76182,7 +76269,7 @@ var init_introspect_pg = __esm({
76182
76269
  } else {
76183
76270
  out = `${withCasing2(name, casing2)}: geometry("${name}")`;
76184
76271
  }
76185
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76272
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76186
76273
  if (isGeoUnknown) {
76187
76274
  let unknown2 = `// TODO: failed to parse geometry type because found more than 2 options inside geometry function '${type}'
76188
76275
  // Introspect is currently supporting only type and srid options
@@ -76206,7 +76293,7 @@ var init_introspect_pg = __esm({
76206
76293
  } else {
76207
76294
  out = `${withCasing2(name, casing2)}: vector("${name}")`;
76208
76295
  }
76209
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76296
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76210
76297
  return out;
76211
76298
  }
76212
76299
  if (lowered.startsWith("char")) {
@@ -76222,28 +76309,20 @@ var init_introspect_pg = __esm({
76222
76309
  } else {
76223
76310
  out = `${withCasing2(name, casing2)}: char("${name}")`;
76224
76311
  }
76225
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76312
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76226
76313
  return out;
76227
76314
  }
76228
- const columnInternals = (_a = internals == null ? void 0 : internals.tables[tableName]) == null ? void 0 : _a.columns[name];
76315
+ const columnInternals = (_c = internals == null ? void 0 : internals.tables[tableName]) == null ? void 0 : _c.columns[name];
76229
76316
  if (typeof columnInternals !== "undefined") {
76230
76317
  if (columnInternals.isArray && columnInternals.rawType && enumTypes.has(columnInternals.rawType)) {
76231
76318
  let out = `${withCasing2(columnInternals.rawType, casing2)}: ${withCasing2(
76232
76319
  columnInternals.rawType,
76233
76320
  casing2
76234
76321
  )}("${name}")`;
76235
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76322
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault3(defaultValue, isExpression)})` : "";
76236
76323
  return out;
76237
76324
  }
76238
76325
  }
76239
- if (enumTypes.has(type)) {
76240
- let out = `${withCasing2(name, casing2)}: ${withCasing2(
76241
- type,
76242
- casing2
76243
- )}("${name}")`;
76244
- out += typeof defaultValue !== "undefined" ? `.default(${defaultValue})` : "";
76245
- return out;
76246
- }
76247
76326
  let unknown = `// TODO: failed to parse database type '${type}'
76248
76327
  `;
76249
76328
  unknown += ` ${withCasing2(name, casing2)}: unknown("${name}")`;
@@ -76276,6 +76355,7 @@ var init_introspect_pg = __esm({
76276
76355
  it.type,
76277
76356
  it.name,
76278
76357
  enumTypes,
76358
+ it.typeSchema ?? "public",
76279
76359
  casing2,
76280
76360
  it.default,
76281
76361
  internals
@@ -76317,29 +76397,6 @@ var init_introspect_pg = __esm({
76317
76397
  }
76318
76398
  statement += it.identity ? generateIdentityParams(it.identity) : "";
76319
76399
  statement += it.generated ? `.generatedAlwaysAs(sql\`${it.generated.as}\`)` : "";
76320
- const fks2 = fkByColumnName[it.name];
76321
- if (fks2) {
76322
- const fksStatement = fks2.map((it2) => {
76323
- const onDelete = it2.onDelete && it2.onDelete !== "no action" ? it2.onDelete : null;
76324
- const onUpdate = it2.onUpdate && it2.onUpdate !== "no action" ? it2.onUpdate : null;
76325
- const params = { onDelete, onUpdate };
76326
- const typeSuffix = isCyclic3(it2) ? ": AnyPgColumn" : "";
76327
- const paramsStr = objToStatement23(params);
76328
- const tableSchema = schemas[it2.schemaTo || ""];
76329
- const paramName = paramNameFor(it2.tableTo, tableSchema);
76330
- if (paramsStr) {
76331
- return `.references(()${typeSuffix} => ${withCasing2(
76332
- paramName,
76333
- casing2
76334
- )}.${withCasing2(it2.columnsTo[0], casing2)}, ${paramsStr} )`;
76335
- }
76336
- return `.references(()${typeSuffix} => ${withCasing2(
76337
- paramName,
76338
- casing2
76339
- )}.${withCasing2(it2.columnsTo[0], casing2)})`;
76340
- }).join("");
76341
- statement += fksStatement;
76342
- }
76343
76400
  statement += ",\n";
76344
76401
  });
76345
76402
  return statement;
@@ -76363,7 +76420,7 @@ var init_introspect_pg = __esm({
76363
76420
  if (it2.isExpression) {
76364
76421
  return `sql\`${it2.expression}\``;
76365
76422
  } else {
76366
- return `table.${withCasing2(it2.expression, casing2)}${it2.opclass && vectorOps.includes(it2.opclass) ? `.op("${it2.opclass}")` : ""}`;
76423
+ return `table.${withCasing2(it2.expression, casing2)}${it2.asc ? ".asc()" : ".desc()"}${it2.nulls === "first" ? ".nullsFirst()" : ".nullsLast()"}${it2.opclass && vectorOps.includes(it2.opclass) ? `.op("${it2.opclass}")` : ""}`;
76367
76424
  }
76368
76425
  }).join(", ")})`;
76369
76426
  statement += it.where ? `.where(sql\`${it.where}\`)` : "";
@@ -83402,7 +83459,7 @@ init_utils2();
83402
83459
  var version2 = async () => {
83403
83460
  const { npmVersion } = await ormCoreVersions();
83404
83461
  const ormVersion = npmVersion ? `drizzle-orm: v${npmVersion}` : "";
83405
- const envVersion = "0.24.0-6205f01";
83462
+ const envVersion = "0.24.0-8d86512";
83406
83463
  const kitVersion = envVersion ? `v${envVersion}` : "--";
83407
83464
  const versions = `drizzle-kit: ${kitVersion}
83408
83465
  ${ormVersion}`;