drizzle-pg-extras 0.5.0 → 0.6.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.
package/dist/index.cjs CHANGED
@@ -97,11 +97,45 @@ var PgExtrasError = class extends Error {
97
97
  };
98
98
 
99
99
  // src/version.ts
100
- var VERSION = "0.5.0";
100
+ var VERSION = "0.6.0";
101
+
102
+ // src/defs.ts
103
+ var import_drizzle_orm3 = require("drizzle-orm");
104
+
105
+ // src/compat.ts
106
+ var import_drizzle_orm = require("drizzle-orm");
107
+ var casingModule = __toESM(require("drizzle-orm/casing"), 1);
108
+ var pgCore = __toESM(require("drizzle-orm/pg-core"), 1);
109
+ var toSnakeCase2 = casingModule.toSnakeCase;
110
+ var toCamelCase2 = casingModule.toCamelCase;
111
+ function columnDbName(column, casing) {
112
+ if (!column.keyAsName || !casing) return column.name;
113
+ return casing === "snake_case" ? toSnakeCase2(column.name) : toCamelCase2(column.name);
114
+ }
115
+ var ColumnCasing = class {
116
+ constructor(casing) {
117
+ this.casing = casing;
118
+ }
119
+ casing;
120
+ getColumnCasing(column) {
121
+ return columnDbName(column, this.casing);
122
+ }
123
+ };
124
+ function columnTable(column) {
125
+ return column.table;
126
+ }
127
+ var PgArrayClass = pgCore["PgArray"];
128
+ function unwrapArrayColumn(column) {
129
+ let current = column;
130
+ if (!PgArrayClass) return current;
131
+ while ((0, import_drizzle_orm.is)(current, PgArrayClass)) current = current.baseColumn;
132
+ return current;
133
+ }
134
+ function dialectConfig(casing) {
135
+ return casing ? { casing } : void 0;
136
+ }
101
137
 
102
138
  // src/defs.ts
103
- var import_drizzle_orm2 = require("drizzle-orm");
104
- var import_casing = require("drizzle-orm/casing");
105
139
  var import_pg_core2 = require("drizzle-orm/pg-core");
106
140
 
107
141
  // src/quote.ts
@@ -170,20 +204,20 @@ $${tag}$`;
170
204
  }
171
205
 
172
206
  // src/render-sql.ts
173
- var import_drizzle_orm = require("drizzle-orm");
207
+ var import_drizzle_orm2 = require("drizzle-orm");
174
208
  var import_pg_core = require("drizzle-orm/pg-core");
175
209
  var dialects = /* @__PURE__ */ new Map();
176
210
  function dialectFor(casing) {
177
211
  const key = casing ?? "";
178
212
  let dialect = dialects.get(key);
179
213
  if (!dialect) {
180
- dialect = new import_pg_core.PgDialect(casing ? { casing } : void 0);
214
+ dialect = new import_pg_core.PgDialect(dialectConfig(casing));
181
215
  dialects.set(key, dialect);
182
216
  }
183
217
  return dialect;
184
218
  }
185
219
  function isSqlFragment(value) {
186
- return (0, import_drizzle_orm.is)(value, import_drizzle_orm.SQL);
220
+ return (0, import_drizzle_orm2.is)(value, import_drizzle_orm2.SQL);
187
221
  }
188
222
  function renderSql(query, casing) {
189
223
  return dialectFor(casing).sqlToQuery(query.inlineParams()).sql;
@@ -223,11 +257,11 @@ function stampConfig(target, comment) {
223
257
  }
224
258
  function withComment(target, comment) {
225
259
  const text = requireComment(comment, "withComment()");
226
- if ((0, import_drizzle_orm2.is)(target, import_drizzle_orm2.Table)) {
260
+ if ((0, import_drizzle_orm3.is)(target, import_drizzle_orm3.Table)) {
227
261
  stampDirect(target, text);
228
262
  return target;
229
263
  }
230
- if ((0, import_drizzle_orm2.is)(target, import_drizzle_orm2.Column) || (0, import_drizzle_orm2.is)(target, import_drizzle_orm2.ColumnBuilder)) {
264
+ if ((0, import_drizzle_orm3.is)(target, import_drizzle_orm3.Column) || (0, import_drizzle_orm3.is)(target, import_drizzle_orm3.ColumnBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.PgColumnBuilder)) {
231
265
  stampConfig(target, text);
232
266
  return target;
233
267
  }
@@ -240,14 +274,14 @@ function withComment(target, comment) {
240
274
  stampDirect(target, text);
241
275
  return target;
242
276
  }
243
- if ((0, import_drizzle_orm2.is)(target, import_pg_core2.IndexBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.UniqueConstraintBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.CheckBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.ForeignKeyBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.PrimaryKeyBuilder)) {
277
+ if ((0, import_drizzle_orm3.is)(target, import_pg_core2.IndexBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.UniqueConstraintBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.CheckBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.ForeignKeyBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.PrimaryKeyBuilder)) {
244
278
  stampDirect(target, text);
245
279
  return target;
246
280
  }
247
- if ((0, import_drizzle_orm2.is)(target, import_pg_core2.IndexBuilderOn) || (0, import_drizzle_orm2.is)(target, import_pg_core2.UniqueOnConstraintBuilder)) {
281
+ if ((0, import_drizzle_orm3.is)(target, import_pg_core2.IndexBuilderOn) || (0, import_drizzle_orm3.is)(target, import_pg_core2.UniqueOnConstraintBuilder)) {
248
282
  throw new PgExtrasError("withComment() on an index or unique constraint needs .on(...) first");
249
283
  }
250
- if ((0, import_drizzle_orm2.is)(target, import_pg_core2.ViewBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.ManualViewBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.MaterializedViewBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.ManualMaterializedViewBuilder)) {
284
+ if ((0, import_drizzle_orm3.is)(target, import_pg_core2.ViewBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.ManualViewBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.MaterializedViewBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.ManualMaterializedViewBuilder)) {
251
285
  throw new PgExtrasError("withComment() on a view needs .as() or .existing() first");
252
286
  }
253
287
  throw new PgExtrasError(
@@ -331,7 +365,7 @@ function pgTrigger(name, config) {
331
365
  });
332
366
  }
333
367
  function updatedAtTrigger(table, column, options) {
334
- if (!(0, import_drizzle_orm2.is)(table, import_pg_core2.PgTable)) {
368
+ if (!(0, import_drizzle_orm3.is)(table, import_pg_core2.PgTable)) {
335
369
  throw new PgExtrasError("updatedAtTrigger() table must be a Drizzle Postgres table");
336
370
  }
337
371
  assertColumnOfTable(table, column);
@@ -482,7 +516,7 @@ function triggerError(label, message) {
482
516
  throw new PgExtrasError(`pgTrigger("${label}"): ${message}`);
483
517
  }
484
518
  function relationIdentity(relation2) {
485
- if ((0, import_drizzle_orm2.is)(relation2, import_pg_core2.PgTable)) {
519
+ if ((0, import_drizzle_orm3.is)(relation2, import_pg_core2.PgTable)) {
486
520
  const config = (0, import_pg_core2.getTableConfig)(relation2);
487
521
  return { schema: schemaName(config.schema), name: config.name, kind: "table" };
488
522
  }
@@ -502,16 +536,16 @@ function relationIdentity(relation2) {
502
536
  function viewOwnsColumn(view, column, viewName) {
503
537
  const entries = relationColumnEntries(view);
504
538
  if (entries.some(([, owned]) => owned === column)) return true;
505
- const parent = column.table;
506
- if (!(0, import_drizzle_orm2.is)(parent, import_pg_core2.PgTable) || (0, import_pg_core2.getTableConfig)(parent).name !== viewName) return false;
539
+ const parent = columnTable(column);
540
+ if (!(0, import_drizzle_orm3.is)(parent, import_pg_core2.PgTable) || (0, import_pg_core2.getTableConfig)(parent).name !== viewName) return false;
507
541
  return entries.some(([, owned]) => owned.name === column.name);
508
542
  }
509
543
  function relationColumnEntries(relation2) {
510
- if ((0, import_drizzle_orm2.is)(relation2, import_pg_core2.PgTable)) return Object.entries((0, import_drizzle_orm2.getTableColumns)(relation2));
544
+ if ((0, import_drizzle_orm3.is)(relation2, import_pg_core2.PgTable)) return Object.entries((0, import_drizzle_orm3.getTableColumns)(relation2));
511
545
  const config = (0, import_pg_core2.isPgMaterializedView)(relation2) ? (0, import_pg_core2.getMaterializedViewConfig)(relation2) : (0, import_pg_core2.getViewConfig)(relation2);
512
546
  const entries = [];
513
547
  for (const [key, field] of Object.entries(config.selectedFields ?? {})) {
514
- if ((0, import_drizzle_orm2.is)(field, import_drizzle_orm2.Column)) entries.push([key, field]);
548
+ if ((0, import_drizzle_orm3.is)(field, import_drizzle_orm3.Column)) entries.push([key, field]);
515
549
  }
516
550
  return entries;
517
551
  }
@@ -543,7 +577,7 @@ function readReferencedTable(label, from) {
543
577
  if ((0, import_pg_core2.isPgView)(from) || (0, import_pg_core2.isPgMaterializedView)(from)) {
544
578
  triggerError(label, "FROM must reference a table");
545
579
  }
546
- if ((0, import_drizzle_orm2.is)(from, import_pg_core2.PgTable)) return from;
580
+ if ((0, import_drizzle_orm3.is)(from, import_pg_core2.PgTable)) return from;
547
581
  if (typeof from === "object" && from !== null && "name" in from && typeof from.name === "string") {
548
582
  assertIdentifier("referenced table", from.name);
549
583
  if (from.schema !== void 0) {
@@ -596,7 +630,7 @@ function renderWhenInput(label, table, when, casing) {
596
630
  if (!when) return null;
597
631
  let value;
598
632
  if (typeof when === "function") {
599
- if (!(0, import_drizzle_orm2.is)(table, import_pg_core2.PgTable) && !(0, import_pg_core2.isPgView)(table) && !(0, import_pg_core2.isPgMaterializedView)(table)) {
633
+ if (!(0, import_drizzle_orm3.is)(table, import_pg_core2.PgTable) && !(0, import_pg_core2.isPgView)(table) && !(0, import_pg_core2.isPgMaterializedView)(table)) {
600
634
  triggerError(label, "WHEN callback needs a Drizzle table or view so NEW and OLD can be typed");
601
635
  }
602
636
  const entries = relationColumnEntries(table);
@@ -611,10 +645,10 @@ function renderWhenInput(label, table, when, casing) {
611
645
  return text.length === 0 ? null : text;
612
646
  }
613
647
  function rowProxy(columns, alias, casing) {
614
- const cache = new import_casing.CasingCache(casing);
648
+ const cache = new ColumnCasing(casing);
615
649
  const refs = {};
616
650
  for (const [key, column] of columns) {
617
- refs[key] = import_drizzle_orm2.sql.raw(`${alias}.${quoteIdent(cache.getColumnCasing(column))}`);
651
+ refs[key] = import_drizzle_orm3.sql.raw(`${alias}.${quoteIdent(cache.getColumnCasing(column))}`);
618
652
  }
619
653
  return refs;
620
654
  }
@@ -715,7 +749,7 @@ function commentOn(target, text) {
715
749
  if (isCommentTarget(target)) {
716
750
  return brand({ target, text: comment }, "comment");
717
751
  }
718
- if ((0, import_drizzle_orm2.is)(target, import_pg_core2.IndexBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.IndexBuilderOn) || (0, import_drizzle_orm2.is)(target, import_pg_core2.UniqueConstraintBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.UniqueOnConstraintBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.CheckBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.ForeignKeyBuilder) || (0, import_drizzle_orm2.is)(target, import_pg_core2.PrimaryKeyBuilder)) {
752
+ if ((0, import_drizzle_orm3.is)(target, import_pg_core2.IndexBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.IndexBuilderOn) || (0, import_drizzle_orm3.is)(target, import_pg_core2.UniqueConstraintBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.UniqueOnConstraintBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.CheckBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.ForeignKeyBuilder) || (0, import_drizzle_orm3.is)(target, import_pg_core2.PrimaryKeyBuilder)) {
719
753
  throw new PgExtrasError(
720
754
  "commentOn() does not take an index or constraint. Wrap the builder with withComment() inside the table extra config."
721
755
  );
@@ -723,14 +757,14 @@ function commentOn(target, text) {
723
757
  throw new PgExtrasError("commentOn() takes a table, column, enum, view, materialized view, function, trigger, or extension");
724
758
  }
725
759
  function isCommentTarget(target) {
726
- if ((0, import_drizzle_orm2.is)(target, import_pg_core2.PgTable) || (0, import_drizzle_orm2.is)(target, import_drizzle_orm2.Column)) return true;
760
+ if ((0, import_drizzle_orm3.is)(target, import_pg_core2.PgTable) || (0, import_drizzle_orm3.is)(target, import_drizzle_orm3.Column)) return true;
727
761
  if (isEnumTarget(target)) return true;
728
762
  if ((0, import_pg_core2.isPgView)(target) || (0, import_pg_core2.isPgMaterializedView)(target)) return true;
729
763
  const kind = extrasKind(target);
730
764
  return kind === "function" || kind === "trigger" || kind === "extension";
731
765
  }
732
766
  function assertColumnOfTable(table, column) {
733
- const columns = Object.values((0, import_drizzle_orm2.getTableColumns)(table));
767
+ const columns = Object.values((0, import_drizzle_orm3.getTableColumns)(table));
734
768
  if (!columns.includes(column)) {
735
769
  throw new PgExtrasError(
736
770
  `column "${column.name}" does not belong to table "${(0, import_pg_core2.getTableConfig)(table).name}"`
@@ -738,7 +772,7 @@ function assertColumnOfTable(table, column) {
738
772
  }
739
773
  }
740
774
  function tableIdentity(table) {
741
- if ((0, import_drizzle_orm2.is)(table, import_pg_core2.PgTable)) {
775
+ if ((0, import_drizzle_orm3.is)(table, import_pg_core2.PgTable)) {
742
776
  const config = (0, import_pg_core2.getTableConfig)(table);
743
777
  return { schema: config.schema && config.schema.length > 0 ? config.schema : "public", name: config.name };
744
778
  }
@@ -752,7 +786,7 @@ function partitionedTable(table, config) {
752
786
  if (!config.columns || config.columns.length === 0) {
753
787
  throw new PgExtrasError("partitionedTable(): columns must include the partition key");
754
788
  }
755
- if ((0, import_drizzle_orm2.is)(table, import_pg_core2.PgTable)) {
789
+ if ((0, import_drizzle_orm3.is)(table, import_pg_core2.PgTable)) {
756
790
  const seen = /* @__PURE__ */ new Set();
757
791
  const columns = [];
758
792
  for (const column of config.columns) {
@@ -834,7 +868,7 @@ function pgDetachPartition(table, config) {
834
868
  }
835
869
  function requireParent(parent, where) {
836
870
  if (extrasKind(parent) === "partitionedTable") return parent;
837
- if ((0, import_drizzle_orm2.is)(parent, import_pg_core2.PgTable) || isPlainTableRef(parent)) return parent;
871
+ if ((0, import_drizzle_orm3.is)(parent, import_pg_core2.PgTable) || isPlainTableRef(parent)) return parent;
838
872
  throw new PgExtrasError(`${where}: parent must be a Drizzle table, partitionedTable(), or { name, schema }`);
839
873
  }
840
874
  function isPlainTableRef(value) {
@@ -913,7 +947,7 @@ function assertKeyCoveredByUniques(table, keyColumns, options) {
913
947
  if (!index.config.unique) continue;
914
948
  const names = [];
915
949
  for (const column of index.config.columns) {
916
- if (!(0, import_drizzle_orm2.is)(column, import_drizzle_orm2.Column)) {
950
+ if (!(0, import_drizzle_orm3.is)(column, import_drizzle_orm3.Column)) {
917
951
  const indexName = index.config.name ? ` "${index.config.name}"` : "";
918
952
  throw new PgExtrasError(`${where}: unique index${indexName} includes an expression. ${expressionRequirement}`);
919
953
  }
@@ -937,7 +971,7 @@ var INTERVAL_PATTERN = new RegExp(`^${INTERVAL_PART}(?:\\s+${INTERVAL_PART})*$`,
937
971
  function hypertable(table, config) {
938
972
  const identity = tableIdentity(table);
939
973
  const where = `hypertable(${identity.schema}.${identity.name})`;
940
- const drizzleTable = (0, import_drizzle_orm2.is)(table, import_pg_core2.PgTable) ? table : null;
974
+ const drizzleTable = (0, import_drizzle_orm3.is)(table, import_pg_core2.PgTable) ? table : null;
941
975
  const timeColumn = resolveHypertableColumn(drizzleTable, config.timeColumn, `${where} timeColumn`);
942
976
  if (drizzleTable && typeof timeColumn !== "string") {
943
977
  if (!isHypertableTimeType(timeColumn.getSQLType())) {
@@ -1005,7 +1039,7 @@ function readCompression(table, compression, where) {
1005
1039
  return { segmentBy, orderBy };
1006
1040
  }
1007
1041
  function readOrder(table, entry, where) {
1008
- if (typeof entry === "string" || (0, import_drizzle_orm2.is)(entry, import_drizzle_orm2.Column)) {
1042
+ if (typeof entry === "string" || (0, import_drizzle_orm3.is)(entry, import_drizzle_orm3.Column)) {
1009
1043
  return { column: resolveHypertableColumn(table, entry, where), direction: null, nulls: null };
1010
1044
  }
1011
1045
  if (!entry || typeof entry !== "object" || !("column" in entry)) {
@@ -1099,7 +1133,7 @@ function isPlainRecord(value) {
1099
1133
  }
1100
1134
 
1101
1135
  // src/collect.ts
1102
- var import_drizzle_orm3 = require("drizzle-orm");
1136
+ var import_drizzle_orm4 = require("drizzle-orm");
1103
1137
  var import_pg_core3 = require("drizzle-orm/pg-core");
1104
1138
  function collectExtras(modules) {
1105
1139
  const extras = {
@@ -1170,7 +1204,7 @@ function walk(value, extras, seen) {
1170
1204
  if (kind === "partitionedTable") {
1171
1205
  const partitioned = value;
1172
1206
  extras.partitionedTables.push(partitioned);
1173
- if ((0, import_drizzle_orm3.is)(partitioned.table, import_pg_core3.PgTable)) walk(partitioned.table, extras, seen);
1207
+ if ((0, import_drizzle_orm4.is)(partitioned.table, import_pg_core3.PgTable)) walk(partitioned.table, extras, seen);
1174
1208
  return;
1175
1209
  }
1176
1210
  if (kind === "partition") {
@@ -1184,14 +1218,14 @@ function walk(value, extras, seen) {
1184
1218
  if (kind === "hypertable") {
1185
1219
  const hypertable2 = value;
1186
1220
  extras.hypertables.push(hypertable2);
1187
- if ((0, import_drizzle_orm3.is)(hypertable2.table, import_pg_core3.PgTable)) walk(hypertable2.table, extras, seen);
1221
+ if ((0, import_drizzle_orm4.is)(hypertable2.table, import_pg_core3.PgTable)) walk(hypertable2.table, extras, seen);
1188
1222
  return;
1189
1223
  }
1190
1224
  if ((0, import_pg_core3.isPgView)(value) || (0, import_pg_core3.isPgMaterializedView)(value)) {
1191
1225
  pushView(extras, value);
1192
1226
  return;
1193
1227
  }
1194
- if ((0, import_drizzle_orm3.is)(value, import_pg_core3.PgTable)) {
1228
+ if ((0, import_drizzle_orm4.is)(value, import_pg_core3.PgTable)) {
1195
1229
  if (!extras.tables.includes(value)) extras.tables.push(value);
1196
1230
  return;
1197
1231
  }
@@ -1212,8 +1246,8 @@ function pushView(extras, value) {
1212
1246
  }
1213
1247
 
1214
1248
  // src/snapshot.ts
1215
- var import_drizzle_orm4 = require("drizzle-orm");
1216
- var import_casing2 = require("drizzle-orm/casing");
1249
+ var import_drizzle_orm5 = require("drizzle-orm");
1250
+ var import_casing = require("drizzle-orm/casing");
1217
1251
  var import_pg_core4 = require("drizzle-orm/pg-core");
1218
1252
  function emptySnapshot() {
1219
1253
  return {
@@ -1234,7 +1268,7 @@ function functionSignature(schema, name, argTypes) {
1234
1268
  }
1235
1269
  function snapshotExtras(extras, options) {
1236
1270
  const casing = options?.casing;
1237
- const cache = new import_casing2.CasingCache(casing);
1271
+ const cache = new ColumnCasing(casing);
1238
1272
  const nameOf = (column) => cache.getColumnCasing(column);
1239
1273
  const functions = [];
1240
1274
  const fnByObject = /* @__PURE__ */ new Map();
@@ -1316,7 +1350,7 @@ function snapshotExtras(extras, options) {
1316
1350
  return {
1317
1351
  schema: identity.schema,
1318
1352
  name: identity.name,
1319
- columns: Object.values((0, import_drizzle_orm4.getTableColumns)(table)).map((column) => nameOf(column)).sort()
1353
+ columns: Object.values((0, import_drizzle_orm5.getTableColumns)(table)).map((column) => nameOf(column)).sort()
1320
1354
  };
1321
1355
  });
1322
1356
  inventoryTables.sort((a, b) => `${a.schema}.${a.name}`.localeCompare(`${b.schema}.${b.name}`));
@@ -1523,7 +1557,7 @@ function normalizeBody(body, casing) {
1523
1557
  function harvest(table, nameOf) {
1524
1558
  const identity = tableIdentity(table);
1525
1559
  const columns = [];
1526
- for (const column of Object.values((0, import_drizzle_orm4.getTableColumns)(table))) {
1560
+ for (const column of Object.values((0, import_drizzle_orm5.getTableColumns)(table))) {
1527
1561
  const text = readComment(column);
1528
1562
  if (!text) continue;
1529
1563
  columns.push(columnComment(identity.schema, identity.name, nameOf(column), text));
@@ -1536,11 +1570,11 @@ function harvest(table, nameOf) {
1536
1570
  }
1537
1571
  function snapshotComment(comment, nameOf, casing, fnByObject, triggerByObject, extras) {
1538
1572
  const target = comment.target;
1539
- if ((0, import_drizzle_orm4.is)(target, import_pg_core4.PgTable)) {
1573
+ if ((0, import_drizzle_orm5.is)(target, import_pg_core4.PgTable)) {
1540
1574
  const identity = tableIdentity(target);
1541
1575
  return tableComment(identity.schema, identity.name, comment.text);
1542
1576
  }
1543
- if ((0, import_drizzle_orm4.is)(target, import_drizzle_orm4.Column)) {
1577
+ if ((0, import_drizzle_orm5.is)(target, import_drizzle_orm5.Column)) {
1544
1578
  return columnCommentFrom(target, comment.text, nameOf, extras);
1545
1579
  }
1546
1580
  if ((0, import_pg_core4.isPgEnum)(target)) {
@@ -1827,10 +1861,11 @@ function commentLabel(comment) {
1827
1861
  return `${comment.kind} ${comment.schema}.${comment.name}`;
1828
1862
  }
1829
1863
  function columnCommentFrom(column, text, nameOf, extras) {
1830
- if (!column.table || !(0, import_drizzle_orm4.is)(column.table, import_pg_core4.PgTable)) {
1864
+ const owner = columnTable(column);
1865
+ if (!owner || !(0, import_drizzle_orm5.is)(owner, import_pg_core4.PgTable)) {
1831
1866
  throw new PgExtrasError(`commentOn() column "${column.name}" is not attached to a Postgres table or view`);
1832
1867
  }
1833
- const identity = tableIdentity(column.table);
1868
+ const identity = tableIdentity(owner);
1834
1869
  const dbName = nameOf(column);
1835
1870
  const knownTable = extras.tables.find((table) => {
1836
1871
  const id = tableIdentity(table);
@@ -1845,7 +1880,7 @@ function columnCommentFrom(column, text, nameOf, extras) {
1845
1880
  const viewId = viewIdentity(view);
1846
1881
  return columnComment(viewId.schema, viewId.name, dbName, text);
1847
1882
  }
1848
- assertColumnOfTable(column.table, column);
1883
+ assertColumnOfTable(owner, column);
1849
1884
  return columnComment(identity.schema, identity.name, dbName, text);
1850
1885
  }
1851
1886
  var ExtraConfigBuilder = /* @__PURE__ */ Symbol.for("drizzle:ExtraConfigBuilder");
@@ -1869,7 +1904,7 @@ function harvestRelations(table, nameOf, casing) {
1869
1904
  "Return the index or constraint from the table extra config wrapped with withComment(). commentOn() is for tables, columns, enums, views, functions, triggers, and extensions."
1870
1905
  );
1871
1906
  }
1872
- if ((0, import_drizzle_orm4.is)(value, import_pg_core4.IndexBuilder)) {
1907
+ if ((0, import_drizzle_orm5.is)(value, import_pg_core4.IndexBuilder)) {
1873
1908
  const built = value.build(table);
1874
1909
  const text = readComment(value);
1875
1910
  let name;
@@ -1883,14 +1918,14 @@ function harvestRelations(table, nameOf, casing) {
1883
1918
  if (text) comments.push(indexComment(identity.schema, name, text));
1884
1919
  continue;
1885
1920
  }
1886
- if ((0, import_drizzle_orm4.is)(value, import_pg_core4.CheckBuilder)) {
1921
+ if ((0, import_drizzle_orm5.is)(value, import_pg_core4.CheckBuilder)) {
1887
1922
  const name = value.name;
1888
1923
  constraints.push({ schema: identity.schema, table: identity.name, name });
1889
1924
  const text = readComment(value);
1890
1925
  if (text) comments.push(constraintComment(identity.schema, identity.name, name, text));
1891
1926
  continue;
1892
1927
  }
1893
- if ((0, import_drizzle_orm4.is)(value, import_pg_core4.UniqueConstraintBuilder)) {
1928
+ if ((0, import_drizzle_orm5.is)(value, import_pg_core4.UniqueConstraintBuilder)) {
1894
1929
  const built = buildOf(value, table);
1895
1930
  const name = constraintName(built);
1896
1931
  constraints.push({ schema: identity.schema, table: identity.name, name });
@@ -1898,7 +1933,7 @@ function harvestRelations(table, nameOf, casing) {
1898
1933
  if (text) comments.push(constraintComment(identity.schema, identity.name, name, text));
1899
1934
  continue;
1900
1935
  }
1901
- if ((0, import_drizzle_orm4.is)(value, import_pg_core4.ForeignKeyBuilder)) {
1936
+ if ((0, import_drizzle_orm5.is)(value, import_pg_core4.ForeignKeyBuilder)) {
1902
1937
  const built = buildOf(value, table);
1903
1938
  const name = applyCasing(built.getName(), [...built.reference().columns, ...built.reference().foreignColumns], nameOf, casing);
1904
1939
  constraints.push({ schema: identity.schema, table: identity.name, name });
@@ -1906,7 +1941,7 @@ function harvestRelations(table, nameOf, casing) {
1906
1941
  if (text) comments.push(constraintComment(identity.schema, identity.name, name, text));
1907
1942
  continue;
1908
1943
  }
1909
- if ((0, import_drizzle_orm4.is)(value, import_pg_core4.PrimaryKeyBuilder)) {
1944
+ if ((0, import_drizzle_orm5.is)(value, import_pg_core4.PrimaryKeyBuilder)) {
1910
1945
  const built = buildOf(value, table);
1911
1946
  const name = applyCasing(built.getName(), built.columns, nameOf, casing);
1912
1947
  constraints.push({ schema: identity.schema, table: identity.name, name });
@@ -1937,7 +1972,7 @@ function resolveIndexName(index, tableName, casing) {
1937
1972
  if (index.config.name && index.config.name.length > 0) return index.config.name;
1938
1973
  const columns = [];
1939
1974
  for (const column of index.config.columns) {
1940
- if ((0, import_drizzle_orm4.is)(column, import_drizzle_orm4.SQL)) {
1975
+ if ((0, import_drizzle_orm5.is)(column, import_drizzle_orm5.SQL)) {
1941
1976
  throw new PgExtrasError(
1942
1977
  `index comment on "${tableName}" needs an index name. Drizzle names indexes made of columns only; name an index whose key is a SQL expression.`
1943
1978
  );
@@ -1952,7 +1987,7 @@ function resolveIndexName(index, tableName, casing) {
1952
1987
  }
1953
1988
  function casedIndexColumn(name, keyAsName, casing) {
1954
1989
  if (!keyAsName || !casing) return name;
1955
- return casing === "camelCase" ? (0, import_casing2.toCamelCase)(name) : (0, import_casing2.toSnakeCase)(name);
1990
+ return casing === "camelCase" ? (0, import_casing.toCamelCase)(name) : (0, import_casing.toSnakeCase)(name);
1956
1991
  }
1957
1992
  function harvestView(view, nameOf) {
1958
1993
  const identity = viewIdentity(view);
@@ -1971,7 +2006,7 @@ function viewIdentity(view) {
1971
2006
  const config = materialized ? (0, import_pg_core4.getMaterializedViewConfig)(view) : (0, import_pg_core4.getViewConfig)(view);
1972
2007
  const columns = [];
1973
2008
  for (const field of Object.values(config.selectedFields ?? {})) {
1974
- if ((0, import_drizzle_orm4.is)(field, import_drizzle_orm4.Column)) columns.push(field);
2009
+ if ((0, import_drizzle_orm5.is)(field, import_drizzle_orm5.Column)) columns.push(field);
1975
2010
  }
1976
2011
  return {
1977
2012
  schema: config.schema && config.schema.length > 0 ? config.schema : "public",
@@ -1989,7 +2024,7 @@ function enumObjectsIn(extras) {
1989
2024
  for (const enumObject of extras.enums) add(enumObject);
1990
2025
  for (const comment of extras.comments) add(comment.target);
1991
2026
  for (const table of extras.tables) {
1992
- for (const column of Object.values((0, import_drizzle_orm4.getTableColumns)(table))) add(enumFromColumn(column));
2027
+ for (const column of Object.values((0, import_drizzle_orm5.getTableColumns)(table))) add(enumFromColumn(column));
1993
2028
  }
1994
2029
  for (const view of extras.views) {
1995
2030
  for (const column of viewIdentity(view).columns) add(enumFromColumn(column));
@@ -1998,8 +2033,8 @@ function enumObjectsIn(extras) {
1998
2033
  }
1999
2034
  function enumFromColumn(column) {
2000
2035
  let current = column;
2001
- while ((0, import_drizzle_orm4.is)(current, import_pg_core4.PgArray)) current = current.baseColumn;
2002
- if ((0, import_drizzle_orm4.is)(current, import_pg_core4.PgEnumColumn) || (0, import_drizzle_orm4.is)(current, import_pg_core4.PgEnumObjectColumn)) return current.enum;
2036
+ current = unwrapArrayColumn(current);
2037
+ if ((0, import_drizzle_orm5.is)(current, import_pg_core4.PgEnumColumn) || (0, import_drizzle_orm5.is)(current, import_pg_core4.PgEnumObjectColumn)) return current.enum;
2003
2038
  return null;
2004
2039
  }
2005
2040
  function enumIdentity(value) {
@@ -3364,6 +3399,7 @@ function viewColumnGone(from, to, schema, relation2, column) {
3364
3399
  }
3365
3400
 
3366
3401
  // src/journal.ts
3402
+ var import_node_module = require("module");
3367
3403
  var import_node_crypto = require("crypto");
3368
3404
  var import_node_fs = require("fs");
3369
3405
  var import_node_path = __toESM(require("path"), 1);
@@ -3417,7 +3453,27 @@ function detectLayout(out) {
3417
3453
  `migrations folder ${out} has both meta/_journal.json and v1 migration folders. Keep one layout.`
3418
3454
  );
3419
3455
  }
3420
- return v1.length > 0 ? "v1" : "journal";
3456
+ if (v1.length > 0) return "v1";
3457
+ if (journal) return "journal";
3458
+ return installedKitMajor() >= 1 ? "v1" : "journal";
3459
+ }
3460
+ function installedKitMajor() {
3461
+ for (const name of ["drizzle-kit", "drizzle-orm"]) {
3462
+ try {
3463
+ const require2 = (0, import_node_module.createRequire)(import_node_path.default.join(process.cwd(), "noop.js"));
3464
+ let dir = import_node_path.default.dirname(require2.resolve(name));
3465
+ for (let i = 0; i < 6; i += 1) {
3466
+ const pkgPath = import_node_path.default.join(dir, "package.json");
3467
+ if ((0, import_node_fs.existsSync)(pkgPath)) {
3468
+ const pkg = JSON.parse((0, import_node_fs.readFileSync)(pkgPath, "utf8"));
3469
+ if (pkg.name === name && pkg.version) return Number.parseInt(pkg.version, 10);
3470
+ }
3471
+ dir = import_node_path.default.dirname(dir);
3472
+ }
3473
+ } catch {
3474
+ }
3475
+ }
3476
+ return 0;
3421
3477
  }
3422
3478
  function writeJournal(out, input) {
3423
3479
  const prefixMode = input.prefix ?? "index";
@@ -3457,9 +3513,11 @@ function writeV1(out, input) {
3457
3513
  const folders = (0, import_node_fs.readdirSync)(out, { withFileTypes: true }).filter((entry) => entry.isDirectory() && /^\d{14}_/.test(entry.name)).map((entry) => entry.name).sort();
3458
3514
  const suffix = sanitizeMigrationName(input.name);
3459
3515
  let stamp = utcStamp(/* @__PURE__ */ new Date());
3516
+ const lastStamp = folders.length > 0 ? folders[folders.length - 1].slice(0, 14) : null;
3517
+ if (lastStamp && stamp <= lastStamp) stamp = bumpStamp(lastStamp);
3460
3518
  let folder = `${stamp}_${suffix}`;
3461
3519
  while ((0, import_node_fs.existsSync)(import_node_path.default.join(out, folder))) {
3462
- stamp = String(Number(stamp) + 1);
3520
+ stamp = bumpStamp(stamp);
3463
3521
  folder = `${stamp}_${suffix}`;
3464
3522
  }
3465
3523
  const dir = import_node_path.default.join(out, folder);
@@ -3473,8 +3531,20 @@ function writeV1(out, input) {
3473
3531
  const parsed = JSON.parse((0, import_node_fs.readFileSync)(previous, "utf8"));
3474
3532
  let next = parsed;
3475
3533
  if (typeof parsed.id === "string") {
3476
- const { id, prevId: _previous, ...rest } = parsed;
3477
- next = { id: (0, import_node_crypto.randomUUID)(), prevId: id, ...rest };
3534
+ if (parsed.type === "base" || parsed.type === "diff") {
3535
+ next = {
3536
+ type: "diff",
3537
+ version: parsed.version,
3538
+ dialect: parsed.dialect,
3539
+ id: (0, import_node_crypto.randomUUID)(),
3540
+ prevIds: [parsed.id],
3541
+ footprints: [],
3542
+ deltas: []
3543
+ };
3544
+ } else {
3545
+ const { id, prevId: _previous, ...rest } = parsed;
3546
+ next = { id: (0, import_node_crypto.randomUUID)(), prevId: id, ...rest };
3547
+ }
3478
3548
  }
3479
3549
  (0, import_node_fs.writeFileSync)(import_node_path.default.join(dir, "snapshot.json"), `${JSON.stringify(next, null, 2)}
3480
3550
  `);
@@ -3532,6 +3602,17 @@ function utcStamp(date) {
3532
3602
  const pad = (value) => String(value).padStart(2, "0");
3533
3603
  return `${date.getUTCFullYear()}${pad(date.getUTCMonth() + 1)}${pad(date.getUTCDate())}${pad(date.getUTCHours())}${pad(date.getUTCMinutes())}${pad(date.getUTCSeconds())}`;
3534
3604
  }
3605
+ function bumpStamp(stamp) {
3606
+ const at = Date.UTC(
3607
+ Number(stamp.slice(0, 4)),
3608
+ Number(stamp.slice(4, 6)) - 1,
3609
+ Number(stamp.slice(6, 8)),
3610
+ Number(stamp.slice(8, 10)),
3611
+ Number(stamp.slice(10, 12)),
3612
+ Number(stamp.slice(12, 14))
3613
+ );
3614
+ return utcStamp(new Date(at + 1e3));
3615
+ }
3535
3616
 
3536
3617
  // src/generate.ts
3537
3618
  var UP_HEADER = `-- Generated by drizzle-pg-extras ${VERSION}. Review before applying.`;