drizzle-kit 0.16.9-fcedf83 → 0.17.0-16763e2

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.
Files changed (4) hide show
  1. package/index.js +19807 -18559
  2. package/package.json +13 -7
  3. package/readme.md +88 -14
  4. package/utils.js +465 -137
package/utils.js CHANGED
@@ -6669,7 +6669,7 @@ __export(utils_exports, {
6669
6669
  mapValues: () => mapValues,
6670
6670
  prepareMigrationFolder: () => prepareMigrationFolder,
6671
6671
  prepareMigrationMeta: () => prepareMigrationMeta,
6672
- prepareOutFolder: () => prepareOutFolder,
6672
+ prepareOutFolder: () => prepareOutFolder2,
6673
6673
  schemaRenameKey: () => schemaRenameKey,
6674
6674
  snapshotsPriorV4: () => snapshotsPriorV4,
6675
6675
  statementsForDiffs: () => statementsForDiffs,
@@ -7169,7 +7169,7 @@ var info = (msg, greyMsg = "") => {
7169
7169
  var originUUID = "00000000-0000-0000-0000-000000000000";
7170
7170
  var snapshotVersion = "5";
7171
7171
 
7172
- // node_modules/.pnpm/zod@3.20.2/node_modules/zod/lib/index.mjs
7172
+ // node_modules/.pnpm/zod@3.20.6/node_modules/zod/lib/index.mjs
7173
7173
  var util;
7174
7174
  (function(util2) {
7175
7175
  util2.assertEqual = (val) => val;
@@ -7584,7 +7584,7 @@ var OK = (value) => ({ status: "valid", value });
7584
7584
  var isAborted = (x) => x.status === "aborted";
7585
7585
  var isDirty = (x) => x.status === "dirty";
7586
7586
  var isValid = (x) => x.status === "valid";
7587
- var isAsync = (x) => typeof Promise !== void 0 && x instanceof Promise;
7587
+ var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
7588
7588
  var errorUtil;
7589
7589
  (function(errorUtil2) {
7590
7590
  errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
@@ -7800,28 +7800,29 @@ var ZodType = class {
7800
7800
  return this._refinement(refinement);
7801
7801
  }
7802
7802
  optional() {
7803
- return ZodOptional.create(this);
7803
+ return ZodOptional.create(this, this._def);
7804
7804
  }
7805
7805
  nullable() {
7806
- return ZodNullable.create(this);
7806
+ return ZodNullable.create(this, this._def);
7807
7807
  }
7808
7808
  nullish() {
7809
- return this.optional().nullable();
7809
+ return this.nullable().optional();
7810
7810
  }
7811
7811
  array() {
7812
- return ZodArray.create(this);
7812
+ return ZodArray.create(this, this._def);
7813
7813
  }
7814
7814
  promise() {
7815
- return ZodPromise.create(this);
7815
+ return ZodPromise.create(this, this._def);
7816
7816
  }
7817
7817
  or(option) {
7818
- return ZodUnion.create([this, option]);
7818
+ return ZodUnion.create([this, option], this._def);
7819
7819
  }
7820
7820
  and(incoming) {
7821
- return ZodIntersection.create(this, incoming);
7821
+ return ZodIntersection.create(this, incoming, this._def);
7822
7822
  }
7823
7823
  transform(transform) {
7824
7824
  return new ZodEffects({
7825
+ ...processCreateParams(this._def),
7825
7826
  schema: this,
7826
7827
  typeName: ZodFirstPartyTypeKind.ZodEffects,
7827
7828
  effect: { type: "transform", transform }
@@ -7830,6 +7831,7 @@ var ZodType = class {
7830
7831
  default(def) {
7831
7832
  const defaultValueFunc = typeof def === "function" ? def : () => def;
7832
7833
  return new ZodDefault({
7834
+ ...processCreateParams(this._def),
7833
7835
  innerType: this,
7834
7836
  defaultValue: defaultValueFunc,
7835
7837
  typeName: ZodFirstPartyTypeKind.ZodDefault
@@ -7839,14 +7841,15 @@ var ZodType = class {
7839
7841
  return new ZodBranded({
7840
7842
  typeName: ZodFirstPartyTypeKind.ZodBranded,
7841
7843
  type: this,
7842
- ...processCreateParams(void 0)
7844
+ ...processCreateParams(this._def)
7843
7845
  });
7844
7846
  }
7845
7847
  catch(def) {
7846
- const defaultValueFunc = typeof def === "function" ? def : () => def;
7848
+ const catchValueFunc = typeof def === "function" ? def : () => def;
7847
7849
  return new ZodCatch({
7850
+ ...processCreateParams(this._def),
7848
7851
  innerType: this,
7849
- defaultValue: defaultValueFunc,
7852
+ catchValue: catchValueFunc,
7850
7853
  typeName: ZodFirstPartyTypeKind.ZodCatch
7851
7854
  });
7852
7855
  }
@@ -7868,24 +7871,25 @@ var ZodType = class {
7868
7871
  }
7869
7872
  };
7870
7873
  var cuidRegex = /^c[^\s-]{8,}$/i;
7874
+ var cuid2Regex = /^[a-z][a-z0-9]*$/;
7871
7875
  var uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
7872
- var emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
7876
+ var emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|([^-]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,}))$/;
7873
7877
  var datetimeRegex = (args) => {
7874
7878
  if (args.precision) {
7875
7879
  if (args.offset) {
7876
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}(([+-]\\d{2}:\\d{2})|Z)$`);
7880
+ return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
7877
7881
  } else {
7878
7882
  return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}Z$`);
7879
7883
  }
7880
7884
  } else if (args.precision === 0) {
7881
7885
  if (args.offset) {
7882
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(([+-]\\d{2}:\\d{2})|Z)$`);
7886
+ return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
7883
7887
  } else {
7884
7888
  return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$`);
7885
7889
  }
7886
7890
  } else {
7887
7891
  if (args.offset) {
7888
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(([+-]\\d{2}:\\d{2})|Z)$`);
7892
+ return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
7889
7893
  } else {
7890
7894
  return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$`);
7891
7895
  }
@@ -8007,6 +8011,16 @@ var ZodString = class extends ZodType {
8007
8011
  });
8008
8012
  status.dirty();
8009
8013
  }
8014
+ } else if (check.kind === "cuid2") {
8015
+ if (!cuid2Regex.test(input.data)) {
8016
+ ctx = this._getOrReturnCtx(input, ctx);
8017
+ addIssueToContext(ctx, {
8018
+ validation: "cuid2",
8019
+ code: ZodIssueCode.invalid_string,
8020
+ message: check.message
8021
+ });
8022
+ status.dirty();
8023
+ }
8010
8024
  } else if (check.kind === "url") {
8011
8025
  try {
8012
8026
  new URL(input.data);
@@ -8088,6 +8102,9 @@ var ZodString = class extends ZodType {
8088
8102
  cuid(message) {
8089
8103
  return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
8090
8104
  }
8105
+ cuid2(message) {
8106
+ return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
8107
+ }
8091
8108
  datetime(options) {
8092
8109
  var _a;
8093
8110
  if (typeof options === "string") {
@@ -8162,6 +8179,9 @@ var ZodString = class extends ZodType {
8162
8179
  get isCUID() {
8163
8180
  return !!this._def.checks.find((ch) => ch.kind === "cuid");
8164
8181
  }
8182
+ get isCUID2() {
8183
+ return !!this._def.checks.find((ch) => ch.kind === "cuid2");
8184
+ }
8165
8185
  get minLength() {
8166
8186
  let min = null;
8167
8187
  for (const ch of this._def.checks) {
@@ -8392,7 +8412,22 @@ var ZodNumber = class extends ZodType {
8392
8412
  return max;
8393
8413
  }
8394
8414
  get isInt() {
8395
- return !!this._def.checks.find((ch) => ch.kind === "int");
8415
+ return !!this._def.checks.find((ch) => ch.kind === "int" || ch.kind === "multipleOf" && util.isInteger(ch.value));
8416
+ }
8417
+ get isFinite() {
8418
+ let max = null, min = null;
8419
+ for (const ch of this._def.checks) {
8420
+ if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
8421
+ return true;
8422
+ } else if (ch.kind === "min") {
8423
+ if (min === null || ch.value > min)
8424
+ min = ch.value;
8425
+ } else if (ch.kind === "max") {
8426
+ if (max === null || ch.value < max)
8427
+ max = ch.value;
8428
+ }
8429
+ }
8430
+ return Number.isFinite(min) && Number.isFinite(max);
8396
8431
  }
8397
8432
  };
8398
8433
  ZodNumber.create = (params) => {
@@ -8749,13 +8784,13 @@ var ZodArray = class extends ZodType {
8749
8784
  }
8750
8785
  }
8751
8786
  if (ctx.common.async) {
8752
- return Promise.all(ctx.data.map((item, i) => {
8787
+ return Promise.all([...ctx.data].map((item, i) => {
8753
8788
  return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
8754
8789
  })).then((result2) => {
8755
8790
  return ParseStatus.mergeArray(status, result2);
8756
8791
  });
8757
8792
  }
8758
- const result = ctx.data.map((item, i) => {
8793
+ const result = [...ctx.data].map((item, i) => {
8759
8794
  return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
8760
8795
  });
8761
8796
  return ParseStatus.mergeArray(status, result);
@@ -8804,15 +8839,6 @@ var objectUtil;
8804
8839
  };
8805
8840
  };
8806
8841
  })(objectUtil || (objectUtil = {}));
8807
- var AugmentFactory = (def) => (augmentation) => {
8808
- return new ZodObject({
8809
- ...def,
8810
- shape: () => ({
8811
- ...def.shape(),
8812
- ...augmentation
8813
- })
8814
- });
8815
- };
8816
8842
  function deepPartialify(schema3) {
8817
8843
  if (schema3 instanceof ZodObject) {
8818
8844
  const newShape = {};
@@ -8841,8 +8867,7 @@ var ZodObject = class extends ZodType {
8841
8867
  super(...arguments);
8842
8868
  this._cached = null;
8843
8869
  this.nonstrict = this.passthrough;
8844
- this.augment = AugmentFactory(this._def);
8845
- this.extend = AugmentFactory(this._def);
8870
+ this.augment = this.extend;
8846
8871
  }
8847
8872
  _getCached() {
8848
8873
  if (this._cached !== null)
@@ -8971,8 +8996,14 @@ var ZodObject = class extends ZodType {
8971
8996
  unknownKeys: "passthrough"
8972
8997
  });
8973
8998
  }
8974
- setKey(key, schema3) {
8975
- return this.augment({ [key]: schema3 });
8999
+ extend(augmentation) {
9000
+ return new ZodObject({
9001
+ ...this._def,
9002
+ shape: () => ({
9003
+ ...this._def.shape(),
9004
+ ...augmentation
9005
+ })
9006
+ });
8976
9007
  }
8977
9008
  merge(merging) {
8978
9009
  const merged = new ZodObject({
@@ -8983,6 +9014,9 @@ var ZodObject = class extends ZodType {
8983
9014
  });
8984
9015
  return merged;
8985
9016
  }
9017
+ setKey(key, schema3) {
9018
+ return this.augment({ [key]: schema3 });
9019
+ }
8986
9020
  catchall(index4) {
8987
9021
  return new ZodObject({
8988
9022
  ...this._def,
@@ -8991,9 +9025,10 @@ var ZodObject = class extends ZodType {
8991
9025
  }
8992
9026
  pick(mask) {
8993
9027
  const shape = {};
8994
- util.objectKeys(mask).map((key) => {
8995
- if (this.shape[key])
9028
+ util.objectKeys(mask).forEach((key) => {
9029
+ if (mask[key] && this.shape[key]) {
8996
9030
  shape[key] = this.shape[key];
9031
+ }
8997
9032
  });
8998
9033
  return new ZodObject({
8999
9034
  ...this._def,
@@ -9002,8 +9037,8 @@ var ZodObject = class extends ZodType {
9002
9037
  }
9003
9038
  omit(mask) {
9004
9039
  const shape = {};
9005
- util.objectKeys(this.shape).map((key) => {
9006
- if (util.objectKeys(mask).indexOf(key) === -1) {
9040
+ util.objectKeys(this.shape).forEach((key) => {
9041
+ if (!mask[key]) {
9007
9042
  shape[key] = this.shape[key];
9008
9043
  }
9009
9044
  });
@@ -9017,24 +9052,14 @@ var ZodObject = class extends ZodType {
9017
9052
  }
9018
9053
  partial(mask) {
9019
9054
  const newShape = {};
9020
- if (mask) {
9021
- util.objectKeys(this.shape).map((key) => {
9022
- if (util.objectKeys(mask).indexOf(key) === -1) {
9023
- newShape[key] = this.shape[key];
9024
- } else {
9025
- newShape[key] = this.shape[key].optional();
9026
- }
9027
- });
9028
- return new ZodObject({
9029
- ...this._def,
9030
- shape: () => newShape
9031
- });
9032
- } else {
9033
- for (const key in this.shape) {
9034
- const fieldSchema = this.shape[key];
9055
+ util.objectKeys(this.shape).forEach((key) => {
9056
+ const fieldSchema = this.shape[key];
9057
+ if (mask && !mask[key]) {
9058
+ newShape[key] = fieldSchema;
9059
+ } else {
9035
9060
  newShape[key] = fieldSchema.optional();
9036
9061
  }
9037
- }
9062
+ });
9038
9063
  return new ZodObject({
9039
9064
  ...this._def,
9040
9065
  shape: () => newShape
@@ -9042,21 +9067,10 @@ var ZodObject = class extends ZodType {
9042
9067
  }
9043
9068
  required(mask) {
9044
9069
  const newShape = {};
9045
- if (mask) {
9046
- util.objectKeys(this.shape).map((key) => {
9047
- if (util.objectKeys(mask).indexOf(key) === -1) {
9048
- newShape[key] = this.shape[key];
9049
- } else {
9050
- const fieldSchema = this.shape[key];
9051
- let newField = fieldSchema;
9052
- while (newField instanceof ZodOptional) {
9053
- newField = newField._def.innerType;
9054
- }
9055
- newShape[key] = newField;
9056
- }
9057
- });
9058
- } else {
9059
- for (const key in this.shape) {
9070
+ util.objectKeys(this.shape).forEach((key) => {
9071
+ if (mask && !mask[key]) {
9072
+ newShape[key] = this.shape[key];
9073
+ } else {
9060
9074
  const fieldSchema = this.shape[key];
9061
9075
  let newField = fieldSchema;
9062
9076
  while (newField instanceof ZodOptional) {
@@ -9064,7 +9078,7 @@ var ZodObject = class extends ZodType {
9064
9078
  }
9065
9079
  newShape[key] = newField;
9066
9080
  }
9067
- }
9081
+ });
9068
9082
  return new ZodObject({
9069
9083
  ...this._def,
9070
9084
  shape: () => newShape
@@ -9404,7 +9418,7 @@ var ZodTuple = class extends ZodType {
9404
9418
  });
9405
9419
  status.dirty();
9406
9420
  }
9407
- const items = ctx.data.map((item, itemIndex) => {
9421
+ const items = [...ctx.data].map((item, itemIndex) => {
9408
9422
  const schema3 = this._def.items[itemIndex] || this._def.rest;
9409
9423
  if (!schema3)
9410
9424
  return null;
@@ -9771,6 +9785,7 @@ var ZodLiteral = class extends ZodType {
9771
9785
  if (input.data !== this._def.value) {
9772
9786
  const ctx = this._getOrReturnCtx(input);
9773
9787
  addIssueToContext(ctx, {
9788
+ received: ctx.data,
9774
9789
  code: ZodIssueCode.invalid_literal,
9775
9790
  expected: this._def.value
9776
9791
  });
@@ -9844,6 +9859,12 @@ var ZodEnum = class extends ZodType {
9844
9859
  }
9845
9860
  return enumValues;
9846
9861
  }
9862
+ extract(values) {
9863
+ return ZodEnum.create(values);
9864
+ }
9865
+ exclude(values) {
9866
+ return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)));
9867
+ }
9847
9868
  };
9848
9869
  ZodEnum.create = createZodEnum;
9849
9870
  var ZodNativeEnum = class extends ZodType {
@@ -9882,6 +9903,9 @@ ZodNativeEnum.create = (values, params) => {
9882
9903
  });
9883
9904
  };
9884
9905
  var ZodPromise = class extends ZodType {
9906
+ unwrap() {
9907
+ return this._def.type;
9908
+ }
9885
9909
  _parse(input) {
9886
9910
  const { ctx } = this._processInputParams(input);
9887
9911
  if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
@@ -10095,23 +10119,29 @@ var ZodCatch = class extends ZodType {
10095
10119
  const result = this._def.innerType._parse({
10096
10120
  data: ctx.data,
10097
10121
  path: ctx.path,
10098
- parent: ctx
10122
+ parent: {
10123
+ ...ctx,
10124
+ common: {
10125
+ ...ctx.common,
10126
+ issues: []
10127
+ }
10128
+ }
10099
10129
  });
10100
10130
  if (isAsync(result)) {
10101
10131
  return result.then((result2) => {
10102
10132
  return {
10103
10133
  status: "valid",
10104
- value: result2.status === "valid" ? result2.value : this._def.defaultValue()
10134
+ value: result2.status === "valid" ? result2.value : this._def.catchValue()
10105
10135
  };
10106
10136
  });
10107
10137
  } else {
10108
10138
  return {
10109
10139
  status: "valid",
10110
- value: result.status === "valid" ? result.value : this._def.defaultValue()
10140
+ value: result.status === "valid" ? result.value : this._def.catchValue()
10111
10141
  };
10112
10142
  }
10113
10143
  }
10114
- removeDefault() {
10144
+ removeCatch() {
10115
10145
  return this._def.innerType;
10116
10146
  }
10117
10147
  };
@@ -10119,7 +10149,7 @@ ZodCatch.create = (type, params) => {
10119
10149
  return new ZodCatch({
10120
10150
  innerType: type,
10121
10151
  typeName: ZodFirstPartyTypeKind.ZodCatch,
10122
- defaultValue: typeof params.default === "function" ? params.default : () => params.default,
10152
+ catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
10123
10153
  ...processCreateParams(params)
10124
10154
  });
10125
10155
  };
@@ -10323,13 +10353,25 @@ var tableV3 = objectType({
10323
10353
  indexes: recordType(stringType(), index),
10324
10354
  foreignKeys: recordType(stringType(), fk)
10325
10355
  }).strict();
10326
- var table = objectType({
10356
+ var compositePK = objectType({
10357
+ name: stringType(),
10358
+ columns: stringType().array()
10359
+ }).strict();
10360
+ var tableV4 = objectType({
10327
10361
  name: stringType(),
10328
10362
  schema: stringType().optional(),
10329
10363
  columns: recordType(stringType(), column),
10330
10364
  indexes: recordType(stringType(), index),
10331
10365
  foreignKeys: recordType(stringType(), fk)
10332
10366
  }).strict();
10367
+ var table = objectType({
10368
+ name: stringType(),
10369
+ schema: stringType().optional(),
10370
+ columns: recordType(stringType(), column),
10371
+ indexes: recordType(stringType(), index),
10372
+ foreignKeys: recordType(stringType(), fk),
10373
+ compositePrimaryKeys: recordType(stringType(), compositePK)
10374
+ }).strict();
10333
10375
  var dialect = literalType("mysql");
10334
10376
  var schemaHash = objectType({
10335
10377
  id: stringType(),
@@ -10343,7 +10385,7 @@ var schemaInternalV3 = objectType({
10343
10385
  var schemaInternalV4 = objectType({
10344
10386
  version: literalType("4"),
10345
10387
  dialect,
10346
- tables: recordType(stringType(), table),
10388
+ tables: recordType(stringType(), tableV4),
10347
10389
  schemas: recordType(stringType(), stringType())
10348
10390
  }).strict();
10349
10391
  var schemaInternal = objectType({
@@ -10360,19 +10402,33 @@ var schemaInternal = objectType({
10360
10402
  var schemaV3 = schemaInternalV3.merge(schemaHash);
10361
10403
  var schemaV4 = schemaInternalV4.merge(schemaHash);
10362
10404
  var schema = schemaInternal.merge(schemaHash);
10363
- var tableSquashed = objectType({
10405
+ var tableSquashedV4 = objectType({
10364
10406
  name: stringType(),
10365
10407
  schema: stringType().optional(),
10366
10408
  columns: recordType(stringType(), column),
10367
10409
  indexes: recordType(stringType(), stringType()),
10368
10410
  foreignKeys: recordType(stringType(), stringType())
10369
10411
  }).strict();
10412
+ var tableSquashed = objectType({
10413
+ name: stringType(),
10414
+ schema: stringType().optional(),
10415
+ columns: recordType(stringType(), column),
10416
+ indexes: recordType(stringType(), stringType()),
10417
+ foreignKeys: recordType(stringType(), stringType()),
10418
+ compositePrimaryKeys: recordType(stringType(), stringType())
10419
+ }).strict();
10370
10420
  var schemaSquashed = objectType({
10371
10421
  version: literalType("5"),
10372
10422
  dialect,
10373
10423
  tables: recordType(stringType(), tableSquashed),
10374
10424
  schemas: recordType(stringType(), stringType())
10375
10425
  }).strict();
10426
+ var schemaSquashedV4 = objectType({
10427
+ version: literalType("4"),
10428
+ dialect,
10429
+ tables: recordType(stringType(), tableSquashedV4),
10430
+ schemas: recordType(stringType(), stringType())
10431
+ }).strict();
10376
10432
  var MySqlSquasher = {
10377
10433
  squashIdx: (idx) => {
10378
10434
  var _a, _b, _c;
@@ -10391,6 +10447,13 @@ var MySqlSquasher = {
10391
10447
  };
10392
10448
  return index.parse(destructed);
10393
10449
  },
10450
+ squashPK: (pk) => {
10451
+ return `${pk.name};${pk.columns.join(",")}`;
10452
+ },
10453
+ unsquashPK: (pk) => {
10454
+ const splitted = pk.split(";");
10455
+ return { name: splitted[0], columns: splitted[1].split(",") };
10456
+ },
10394
10457
  squashFK: (fk4) => {
10395
10458
  var _a, _b;
10396
10459
  return `${fk4.name};${fk4.tableFrom};${fk4.columnsFrom.join(",")};${fk4.tableTo};${fk4.columnsTo.join(",")};${(_a = fk4.onUpdate) != null ? _a : ""};${(_b = fk4.onDelete) != null ? _b : ""}`;
@@ -10518,13 +10581,25 @@ var tableV32 = objectType({
10518
10581
  indexes: recordType(stringType(), index2),
10519
10582
  foreignKeys: recordType(stringType(), fk2)
10520
10583
  }).strict();
10521
- var table2 = objectType({
10584
+ var compositePK2 = objectType({
10585
+ name: stringType(),
10586
+ columns: stringType().array()
10587
+ }).strict();
10588
+ var tableV42 = objectType({
10522
10589
  name: stringType(),
10523
10590
  schema: stringType(),
10524
10591
  columns: recordType(stringType(), column2),
10525
10592
  indexes: recordType(stringType(), index2),
10526
10593
  foreignKeys: recordType(stringType(), fk2)
10527
10594
  }).strict();
10595
+ var table2 = objectType({
10596
+ name: stringType(),
10597
+ schema: stringType(),
10598
+ columns: recordType(stringType(), column2),
10599
+ indexes: recordType(stringType(), index2),
10600
+ foreignKeys: recordType(stringType(), fk2),
10601
+ compositePrimaryKeys: recordType(stringType(), compositePK2)
10602
+ }).strict();
10528
10603
  var schemaHash2 = objectType({
10529
10604
  id: stringType(),
10530
10605
  prevId: stringType()
@@ -10538,7 +10613,7 @@ var pgSchemaInternalV3 = objectType({
10538
10613
  var pgSchemaInternalV4 = objectType({
10539
10614
  version: literalType("4"),
10540
10615
  dialect: literalType("pg"),
10541
- tables: recordType(stringType(), table2),
10616
+ tables: recordType(stringType(), tableV42),
10542
10617
  enums: recordType(stringType(), enumSchema),
10543
10618
  schemas: recordType(stringType(), stringType())
10544
10619
  }).strict();
@@ -10555,12 +10630,27 @@ var pgSchemaInternal = objectType({
10555
10630
  })
10556
10631
  }).strict();
10557
10632
  var tableSquashed2 = objectType({
10633
+ name: stringType(),
10634
+ schema: stringType(),
10635
+ columns: recordType(stringType(), column2),
10636
+ indexes: recordType(stringType(), stringType()),
10637
+ foreignKeys: recordType(stringType(), stringType()),
10638
+ compositePrimaryKeys: recordType(stringType(), stringType())
10639
+ }).strict();
10640
+ var tableSquashedV42 = objectType({
10558
10641
  name: stringType(),
10559
10642
  schema: stringType(),
10560
10643
  columns: recordType(stringType(), column2),
10561
10644
  indexes: recordType(stringType(), stringType()),
10562
10645
  foreignKeys: recordType(stringType(), stringType())
10563
10646
  }).strict();
10647
+ var pgSchemaSquashedV4 = objectType({
10648
+ version: literalType("4"),
10649
+ dialect: enumType(["pg"]),
10650
+ tables: recordType(stringType(), tableSquashedV42),
10651
+ enums: recordType(stringType(), enumSchema),
10652
+ schemas: recordType(stringType(), stringType())
10653
+ }).strict();
10564
10654
  var pgSchemaSquashed = objectType({
10565
10655
  version: literalType("5"),
10566
10656
  dialect: enumType(["pg"]),
@@ -10596,6 +10686,12 @@ var PgSquasher = {
10596
10686
  var _a, _b;
10597
10687
  return `${fk4.name};${fk4.tableFrom};${fk4.columnsFrom.join(",")};${fk4.tableTo};${fk4.columnsTo.join(",")};${(_a = fk4.onUpdate) != null ? _a : ""};${(_b = fk4.onDelete) != null ? _b : ""}`;
10598
10688
  },
10689
+ squashPK: (pk) => {
10690
+ return `${pk.columns.join(",")}`;
10691
+ },
10692
+ unsquashPK: (pk) => {
10693
+ return { name: "", columns: pk.split(",") };
10694
+ },
10599
10695
  unsquashFK: (input) => {
10600
10696
  const [
10601
10697
  name,
@@ -10627,6 +10723,9 @@ var squashPgScheme = (json) => {
10627
10723
  const squashedFKs = mapValues(it[1].foreignKeys, (fk4) => {
10628
10724
  return PgSquasher.squashFK(fk4);
10629
10725
  });
10726
+ const squashedPKs = mapValues(it[1].compositePrimaryKeys, (pk) => {
10727
+ return PgSquasher.squashPK(pk);
10728
+ });
10630
10729
  return [
10631
10730
  it[0],
10632
10731
  {
@@ -10634,7 +10733,8 @@ var squashPgScheme = (json) => {
10634
10733
  schema: it[1].schema,
10635
10734
  columns: it[1].columns,
10636
10735
  indexes: squashedIndexes,
10637
- foreignKeys: squashedFKs
10736
+ foreignKeys: squashedFKs,
10737
+ compositePrimaryKeys: squashedPKs
10638
10738
  }
10639
10739
  ];
10640
10740
  })
@@ -10678,7 +10778,7 @@ var fk3 = objectType({
10678
10778
  onUpdate: stringType().optional(),
10679
10779
  onDelete: stringType().optional()
10680
10780
  }).strict();
10681
- var compositePK = objectType({
10781
+ var compositePK3 = objectType({
10682
10782
  columns: stringType().array()
10683
10783
  }).strict();
10684
10784
  var column3 = objectType({
@@ -10700,7 +10800,7 @@ var table3 = objectType({
10700
10800
  columns: recordType(stringType(), column3),
10701
10801
  indexes: recordType(stringType(), index3),
10702
10802
  foreignKeys: recordType(stringType(), fk3),
10703
- compositePrimaryKeys: recordType(stringType(), compositePK)
10803
+ compositePrimaryKeys: recordType(stringType(), compositePK3)
10704
10804
  }).strict();
10705
10805
  var dialect2 = enumType(["sqlite"]);
10706
10806
  var schemaHash3 = objectType({
@@ -11052,13 +11152,26 @@ var pgNativeTypes = /* @__PURE__ */ new Set([
11052
11152
  "interval",
11053
11153
  "bigint",
11054
11154
  "bigserial",
11055
- "double precision"
11155
+ "double precision",
11156
+ "interval year",
11157
+ "interval month",
11158
+ "interval day",
11159
+ "interval hour",
11160
+ "interval minute",
11161
+ "interval second",
11162
+ "interval year to month",
11163
+ "interval day to hour",
11164
+ "interval day to minute",
11165
+ "interval day to second",
11166
+ "interval hour to minute",
11167
+ "interval hour to second",
11168
+ "interval minute to second"
11056
11169
  ]);
11057
11170
  var isPgNativeType = (it) => {
11058
11171
  if (pgNativeTypes.has(it))
11059
11172
  return true;
11060
11173
  const toCheck = it.replace(/ /g, "");
11061
- return toCheck.startsWith("varchar(") || toCheck.startsWith("numeric(") || toCheck.startsWith("timestamp(");
11174
+ return toCheck.startsWith("varchar(") || toCheck.startsWith("char(") || toCheck.startsWith("numeric(") || toCheck.startsWith("timestamp(");
11062
11175
  };
11063
11176
  var Convertor = class {
11064
11177
  };
@@ -11067,7 +11180,7 @@ var PgCreateTableConvertor = class extends Convertor {
11067
11180
  return statement.type === "create_table" && dialect3 === "pg";
11068
11181
  }
11069
11182
  convert(st) {
11070
- const { tableName, schema: schema3, columns } = st;
11183
+ const { tableName, schema: schema3, columns, compositePKs } = st;
11071
11184
  let statement = "";
11072
11185
  const name = schema3 ? `"${schema3}"."${tableName}"` : `"${tableName}"`;
11073
11186
  statement += `CREATE TABLE IF NOT EXISTS ${name} (
@@ -11077,13 +11190,19 @@ var PgCreateTableConvertor = class extends Convertor {
11077
11190
  const primaryKeyStatement = column4.primaryKey ? "PRIMARY KEY" : "";
11078
11191
  const notNullStatement = column4.notNull ? "NOT NULL" : "";
11079
11192
  const defaultStatement = column4.default !== void 0 ? `DEFAULT ${column4.default}` : "";
11080
- const type = isPgNativeType(column4.type) ? column4.type : `"${column4.type}"`;
11193
+ const type = isPgNativeType(column4.type) ? column4.type : `${column4.type}`;
11081
11194
  statement += " " + `"${column4.name}" ${type} ${primaryKeyStatement} ${defaultStatement} ${notNullStatement}`.replace(/ +/g, " ").trim();
11082
11195
  statement += (i === columns.length - 1 ? "" : ",") + "\n";
11083
11196
  }
11084
11197
  statement += `);`;
11085
11198
  statement += `
11086
11199
  `;
11200
+ if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
11201
+ const compositePK4 = PgSquasher.unsquashPK(compositePKs[0]);
11202
+ statement += `ALTER TABLE ${name} ADD CONSTRAINT "${st.compositePkName}" PRIMARY KEY("${compositePK4.columns.join('","')}");`;
11203
+ statement += `
11204
+ `;
11205
+ }
11087
11206
  return statement;
11088
11207
  }
11089
11208
  };
@@ -11092,7 +11211,7 @@ var MySqlCreateTableConvertor = class extends Convertor {
11092
11211
  return statement.type === "create_table" && dialect3 === "mysql";
11093
11212
  }
11094
11213
  convert(st) {
11095
- const { tableName, columns, schema: schema3 } = st;
11214
+ const { tableName, columns, schema: schema3, compositePKs } = st;
11096
11215
  let statement = "";
11097
11216
  const tName = schema3 ? `\`${schema3}\`.\`${tableName}\`` : `\`${tableName}\``;
11098
11217
  statement += `CREATE TABLE ${tName} (
@@ -11110,6 +11229,14 @@ var MySqlCreateTableConvertor = class extends Convertor {
11110
11229
  statement += `);`;
11111
11230
  statement += `
11112
11231
  `;
11232
+ if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
11233
+ const compositePK4 = MySqlSquasher.unsquashPK(compositePKs[0]);
11234
+ statement += `ALTER TABLE ${tName} ADD PRIMARY KEY(\`${compositePK4.columns.join(
11235
+ "`,`"
11236
+ )}\`);`;
11237
+ statement += `
11238
+ `;
11239
+ }
11113
11240
  return statement;
11114
11241
  }
11115
11242
  };
@@ -11200,7 +11327,7 @@ var DropTableConvertor = class extends Convertor {
11200
11327
  }
11201
11328
  convert(statement) {
11202
11329
  const { tableName } = statement;
11203
- return `DROP TABLE ${tableName};`;
11330
+ return `DROP TABLE \`${tableName}\`;`;
11204
11331
  }
11205
11332
  };
11206
11333
  var PgRenameTableConvertor = class extends Convertor {
@@ -11388,27 +11515,9 @@ var PgAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
11388
11515
  return `ALTER TABLE ${tableName} ALTER COLUMN "${columnName}" DROP DEFAULT;`;
11389
11516
  }
11390
11517
  };
11391
- var MySqlAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
11392
- can(statement, dialect3) {
11393
- return statement.type === "alter_table_alter_column_set_default" && dialect3 === "mysql";
11394
- }
11395
- convert(statement) {
11396
- const { tableName, columnName } = statement;
11397
- return `ALTER TABLE ${tableName} ALTER COLUMN \`${columnName}\` SET DEFAULT ${statement.newDefaultValue};`;
11398
- }
11399
- };
11400
- var MySqlAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
11401
- can(statement, dialect3) {
11402
- return statement.type === "alter_table_alter_column_drop_default" && dialect3 === "mysql";
11403
- }
11404
- convert(statement) {
11405
- const { tableName, columnName } = statement;
11406
- return `ALTER TABLE ${tableName} ALTER COLUMN \`${columnName}\` DROP DEFAULT;`;
11407
- }
11408
- };
11409
11518
  var MySqlModifyColumn = class extends Convertor {
11410
11519
  can(statement, dialect3) {
11411
- return (statement.type === "alter_table_alter_column_set_type" || statement.type === "alter_table_alter_column_set_notnull" || statement.type === "alter_table_alter_column_drop_notnull" || statement.type === "alter_table_alter_column_drop_on_update" || statement.type === "alter_table_alter_column_set_on_update" || statement.type === "alter_table_alter_column_set_autoincrement" || statement.type === "alter_table_alter_column_drop_autoincrement") && dialect3 === "mysql";
11520
+ return (statement.type === "alter_table_alter_column_set_type" || statement.type === "alter_table_alter_column_set_notnull" || statement.type === "alter_table_alter_column_drop_notnull" || statement.type === "alter_table_alter_column_drop_on_update" || statement.type === "alter_table_alter_column_set_on_update" || statement.type === "alter_table_alter_column_set_autoincrement" || statement.type === "alter_table_alter_column_drop_autoincrement" || statement.type === "alter_table_alter_column_set_default" || statement.type === "alter_table_alter_column_drop_default") && dialect3 === "mysql";
11412
11521
  }
11413
11522
  convert(statement) {
11414
11523
  const { tableName, columnName } = statement;
@@ -11453,6 +11562,18 @@ var MySqlModifyColumn = class extends Convertor {
11453
11562
  columnType = ` ${statement.newDataType}`;
11454
11563
  columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
11455
11564
  columnAutoincrement = "";
11565
+ } else if (statement.type === "alter_table_alter_column_set_default") {
11566
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
11567
+ columnOnUpdate = columnOnUpdate = statement.columnOnUpdate ? ` ON UPDATE CURRENT_TIMESTAMP` : "";
11568
+ columnType = ` ${statement.newDataType}`;
11569
+ columnDefault = ` DEFAULT ${statement.newDefaultValue}`;
11570
+ columnAutoincrement = statement.columnAutoIncrement ? " AUTO_INCREMENT" : "";
11571
+ } else if (statement.type === "alter_table_alter_column_drop_default") {
11572
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
11573
+ columnOnUpdate = columnOnUpdate = statement.columnOnUpdate ? ` ON UPDATE CURRENT_TIMESTAMP` : "";
11574
+ columnType = ` ${statement.newDataType}`;
11575
+ columnDefault = "";
11576
+ columnAutoincrement = statement.columnAutoIncrement ? " AUTO_INCREMENT" : "";
11456
11577
  } else {
11457
11578
  columnType = ` ${statement.newDataType}`;
11458
11579
  columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
@@ -11479,6 +11600,67 @@ var SqliteAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
11479
11600
  */`;
11480
11601
  }
11481
11602
  };
11603
+ var PgAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
11604
+ can(statement, dialect3) {
11605
+ return statement.type === "create_composite_pk" && dialect3 === "pg";
11606
+ }
11607
+ convert(statement) {
11608
+ const { name, columns } = PgSquasher.unsquashPK(statement.data);
11609
+ return `ALTER TABLE "${statement.tableName}" ADD CONSTRAINT "${statement.constraintName}" PRIMARY KEY("${columns.join('","')}");`;
11610
+ }
11611
+ };
11612
+ var PgAlterTableDeleteCompositePrimaryKeyConvertor = class extends Convertor {
11613
+ can(statement, dialect3) {
11614
+ return statement.type === "delete_composite_pk" && dialect3 === "pg";
11615
+ }
11616
+ convert(statement) {
11617
+ const { name, columns } = PgSquasher.unsquashPK(statement.data);
11618
+ return `ALTER TABLE "${statement.tableName}" DROP CONSTRAINT "${statement.constraintName}";`;
11619
+ }
11620
+ };
11621
+ var PgAlterTableAlterCompositePrimaryKeyConvertor = class extends Convertor {
11622
+ can(statement, dialect3) {
11623
+ return statement.type === "alter_composite_pk" && dialect3 === "pg";
11624
+ }
11625
+ convert(statement) {
11626
+ const { name, columns } = PgSquasher.unsquashPK(statement.old);
11627
+ const { name: newName, columns: newColumns } = PgSquasher.unsquashPK(
11628
+ statement.new
11629
+ );
11630
+ return `ALTER TABLE "${statement.tableName}" DROP CONSTRAINT ${statement.oldConstraintName};
11631
+ ALTER TABLE "${statement.tableName}" ADD CONSTRAINT ${statement.newConstraintName} PRIMARY KEY(${newColumns.join(",")});`;
11632
+ }
11633
+ };
11634
+ var MySqlAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
11635
+ can(statement, dialect3) {
11636
+ return statement.type === "create_composite_pk" && dialect3 === "mysql";
11637
+ }
11638
+ convert(statement) {
11639
+ const { name, columns } = MySqlSquasher.unsquashPK(statement.data);
11640
+ return `ALTER TABLE \`${statement.tableName}\` ADD PRIMARY KEY(\`${columns.join("`,`")}\`);`;
11641
+ }
11642
+ };
11643
+ var MySqlAlterTableDeleteCompositePrimaryKeyConvertor = class extends Convertor {
11644
+ can(statement, dialect3) {
11645
+ return statement.type === "delete_composite_pk" && dialect3 === "mysql";
11646
+ }
11647
+ convert(statement) {
11648
+ const { name, columns } = MySqlSquasher.unsquashPK(statement.data);
11649
+ return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY;`;
11650
+ }
11651
+ };
11652
+ var MySqlAlterTableAlterCompositePrimaryKeyConvertor = class extends Convertor {
11653
+ can(statement, dialect3) {
11654
+ return statement.type === "alter_composite_pk" && dialect3 === "mysql";
11655
+ }
11656
+ convert(statement) {
11657
+ const { name, columns } = MySqlSquasher.unsquashPK(statement.old);
11658
+ const { name: newName, columns: newColumns } = MySqlSquasher.unsquashPK(
11659
+ statement.new
11660
+ );
11661
+ return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY, ADD PRIMARY KEY(\`${newColumns.join("`,`")}\`);`;
11662
+ }
11663
+ };
11482
11664
  var SqliteAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
11483
11665
  can(statement, dialect3) {
11484
11666
  return statement.type === "create_composite_pk" && dialect3 === "sqlite";
@@ -11956,8 +12138,6 @@ convertors.push(new PgAlterTableAlterColumnDropNotNullConvertor());
11956
12138
  convertors.push(new PgAlterTableAlterColumnSetDefaultConvertor());
11957
12139
  convertors.push(new PgAlterTableAlterColumnDropDefaultConvertor());
11958
12140
  convertors.push(new MySqlModifyColumn());
11959
- convertors.push(new MySqlAlterTableAlterColumnSetDefaultConvertor());
11960
- convertors.push(new MySqlAlterTableAlterColumnDropDefaultConvertor());
11961
12141
  convertors.push(new PgCreateForeignKeyConvertor());
11962
12142
  convertors.push(new MySqlCreateForeignKeyConvertor());
11963
12143
  convertors.push(new PgAlterForeignKeyConvertor());
@@ -11985,6 +12165,12 @@ convertors.push(new SqliteAlterTableAlterColumnDropDefaultConvertor());
11985
12165
  convertors.push(new SqliteAlterTableCreateCompositePrimaryKeyConvertor());
11986
12166
  convertors.push(new SqliteAlterTableDeleteCompositePrimaryKeyConvertor());
11987
12167
  convertors.push(new SqliteAlterTableAlterCompositePrimaryKeyConvertor());
12168
+ convertors.push(new PgAlterTableCreateCompositePrimaryKeyConvertor());
12169
+ convertors.push(new PgAlterTableDeleteCompositePrimaryKeyConvertor());
12170
+ convertors.push(new PgAlterTableAlterCompositePrimaryKeyConvertor());
12171
+ convertors.push(new MySqlAlterTableCreateCompositePrimaryKeyConvertor());
12172
+ convertors.push(new MySqlAlterTableDeleteCompositePrimaryKeyConvertor());
12173
+ convertors.push(new MySqlAlterTableAlterCompositePrimaryKeyConvertor());
11988
12174
  var fromJson = (statements, dialect3) => {
11989
12175
  const result = statements.map((statement) => {
11990
12176
  const filtered = convertors.filter((it) => {
@@ -12025,13 +12211,30 @@ drop type __venum;
12025
12211
  `;
12026
12212
 
12027
12213
  // src/jsonStatements.ts
12028
- var prepareCreateTableJson = (table4) => {
12029
- const { name, schema: schema3, columns } = table4;
12214
+ var preparePgCreateTableJson = (table4, json2) => {
12215
+ const { name, schema: schema3, columns, compositePrimaryKeys } = table4;
12030
12216
  return {
12031
12217
  type: "create_table",
12032
12218
  tableName: name,
12033
12219
  schema: schema3,
12034
- columns: Object.values(columns)
12220
+ columns: Object.values(columns),
12221
+ compositePKs: Object.values(compositePrimaryKeys),
12222
+ compositePkName: Object.values(compositePrimaryKeys).length > 0 ? json2.tables[name].compositePrimaryKeys[`${name}_${PgSquasher.unsquashPK(
12223
+ Object.values(compositePrimaryKeys)[0]
12224
+ ).columns.join("_")}`].name : ""
12225
+ };
12226
+ };
12227
+ var prepareMySqlCreateTableJson = (table4, json2) => {
12228
+ const { name, schema: schema3, columns, compositePrimaryKeys } = table4;
12229
+ return {
12230
+ type: "create_table",
12231
+ tableName: name,
12232
+ schema: schema3,
12233
+ columns: Object.values(columns),
12234
+ compositePKs: Object.values(compositePrimaryKeys),
12235
+ compositePkName: Object.values(compositePrimaryKeys).length > 0 ? json2.tables[name].compositePrimaryKeys[`${name}_${MySqlSquasher.unsquashPK(
12236
+ Object.values(compositePrimaryKeys)[0]
12237
+ ).columns.join("_")}`].name : ""
12035
12238
  };
12036
12239
  };
12037
12240
  var prepareSQLiteCreateTable = (table4) => {
@@ -12207,7 +12410,11 @@ var _prepareAlterColumns = (tableName, schema3, columns, json2) => {
12207
12410
  tableName,
12208
12411
  columnName,
12209
12412
  newDefaultValue: column4.default.value,
12210
- schema: schema3
12413
+ schema: schema3,
12414
+ columnOnUpdate,
12415
+ columnNotNull,
12416
+ columnAutoIncrement,
12417
+ newDataType: columnType
12211
12418
  });
12212
12419
  }
12213
12420
  if (((_c = column4.default) == null ? void 0 : _c.type) === "changed") {
@@ -12216,7 +12423,11 @@ var _prepareAlterColumns = (tableName, schema3, columns, json2) => {
12216
12423
  tableName,
12217
12424
  columnName,
12218
12425
  newDefaultValue: column4.default.new,
12219
- schema: schema3
12426
+ schema: schema3,
12427
+ columnOnUpdate,
12428
+ columnNotNull,
12429
+ columnAutoIncrement,
12430
+ newDataType: columnType
12220
12431
  });
12221
12432
  }
12222
12433
  if (((_d = column4.default) == null ? void 0 : _d.type) === "deleted") {
@@ -12224,7 +12435,12 @@ var _prepareAlterColumns = (tableName, schema3, columns, json2) => {
12224
12435
  type: "alter_table_alter_column_drop_default",
12225
12436
  tableName,
12226
12437
  columnName,
12227
- schema: schema3
12438
+ schema: schema3,
12439
+ columnDefault,
12440
+ columnOnUpdate,
12441
+ columnNotNull,
12442
+ columnAutoIncrement,
12443
+ newDataType: columnType
12228
12444
  });
12229
12445
  }
12230
12446
  if (((_e = column4.notNull) == null ? void 0 : _e.type) === "added") {
@@ -12415,6 +12631,70 @@ var prepareAlterCompositePrimaryKeySqlite = (tableName, pks) => {
12415
12631
  };
12416
12632
  });
12417
12633
  };
12634
+ var prepareAddCompositePrimaryKeyPg = (tableName, pks, json2) => {
12635
+ return Object.values(pks).map((it) => {
12636
+ return {
12637
+ type: "create_composite_pk",
12638
+ tableName,
12639
+ data: it,
12640
+ constraintName: json2.tables[tableName].compositePrimaryKeys[`${tableName}_${PgSquasher.unsquashPK(it).columns.join("_")}`].name
12641
+ };
12642
+ });
12643
+ };
12644
+ var prepareDeleteCompositePrimaryKeyPg = (tableName, pks, json1) => {
12645
+ return Object.values(pks).map((it) => {
12646
+ return {
12647
+ type: "delete_composite_pk",
12648
+ tableName,
12649
+ data: it,
12650
+ constraintName: json1.tables[tableName].compositePrimaryKeys[`${tableName}_${PgSquasher.unsquashPK(it).columns.join("_")}`].name
12651
+ };
12652
+ });
12653
+ };
12654
+ var prepareAlterCompositePrimaryKeyPg = (tableName, pks, json1, json2) => {
12655
+ return Object.values(pks).map((it) => {
12656
+ return {
12657
+ type: "alter_composite_pk",
12658
+ tableName,
12659
+ old: it.__old,
12660
+ new: it.__new,
12661
+ oldConstraintName: json1.tables[tableName].compositePrimaryKeys[`${tableName}_${PgSquasher.unsquashPK(it.__old).columns.join("_")}`].name,
12662
+ newConstraintName: json2.tables[tableName].compositePrimaryKeys[`${tableName}_${PgSquasher.unsquashPK(it.__new).columns.join("_")}`].name
12663
+ };
12664
+ });
12665
+ };
12666
+ var prepareAddCompositePrimaryKeyMySql = (tableName, pks, json2) => {
12667
+ return Object.values(pks).map((it) => {
12668
+ return {
12669
+ type: "create_composite_pk",
12670
+ tableName,
12671
+ data: it,
12672
+ constraintName: json2.tables[tableName].compositePrimaryKeys[`${tableName}_${MySqlSquasher.unsquashPK(it).columns.join("_")}`].name
12673
+ };
12674
+ });
12675
+ };
12676
+ var prepareDeleteCompositePrimaryKeyMySql = (tableName, pks, json1) => {
12677
+ return Object.values(pks).map((it) => {
12678
+ return {
12679
+ type: "delete_composite_pk",
12680
+ tableName,
12681
+ data: it,
12682
+ constraintName: json1.tables[tableName].compositePrimaryKeys[`${tableName}_${MySqlSquasher.unsquashPK(it).columns.join("_")}`].name
12683
+ };
12684
+ });
12685
+ };
12686
+ var prepareAlterCompositePrimaryKeyMySql = (tableName, pks, json1, json2) => {
12687
+ return Object.values(pks).map((it) => {
12688
+ return {
12689
+ type: "alter_composite_pk",
12690
+ tableName,
12691
+ old: it.__old,
12692
+ new: it.__new,
12693
+ oldConstraintName: json1.tables[tableName].compositePrimaryKeys[`${tableName}_${MySqlSquasher.unsquashPK(it.__old).columns.join("_")}`].name,
12694
+ newConstraintName: json2.tables[tableName].compositePrimaryKeys[`${tableName}_${MySqlSquasher.unsquashPK(it.__new).columns.join("_")}`].name
12695
+ };
12696
+ });
12697
+ };
12418
12698
 
12419
12699
  // src/snapshotsDiffer.ts
12420
12700
  var makeChanged = (schema3) => {
@@ -12556,7 +12836,7 @@ var diffResultScheme = objectType({
12556
12836
  addedSchemas: stringType().array(),
12557
12837
  deletedSchemas: stringType().array()
12558
12838
  }).strict();
12559
- var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesResolver, columnsResolver) => {
12839
+ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesResolver, columnsResolver, prevFull, curFull) => {
12560
12840
  var _a, _b, _c, _d;
12561
12841
  const diffResult = applyJsonDiff(json1, json2);
12562
12842
  if (Object.keys(diffResult).length === 0) {
@@ -12586,9 +12866,6 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
12586
12866
  const jsonSQLiteCreateTables = created.map((it) => {
12587
12867
  return prepareSQLiteCreateTable(it);
12588
12868
  });
12589
- const jsonCreateTables = created.map((it) => {
12590
- return prepareCreateTableJson(it);
12591
- });
12592
12869
  const jsonCreateIndexesForCreatedTables = created.map((it) => {
12593
12870
  return prepareCreateIndexesJson(it.name, it.schema, it.indexes);
12594
12871
  }).flat();
@@ -12646,18 +12923,57 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
12646
12923
  const jsonRemoveTableFromSchemas = [];
12647
12924
  const jsonSetNewTableSchemas = [];
12648
12925
  allAlteredResolved.forEach((it) => {
12649
- const addedCompositePKs = prepareAddCompositePrimaryKeySqlite(
12650
- it.name,
12651
- it.addedCompositePKs
12652
- );
12653
- const deletedCompositePKs = prepareDeleteCompositePrimaryKeySqlite(
12654
- it.name,
12655
- it.deletedCompositePKs
12656
- );
12657
- const alteredCompositePKs = prepareAlterCompositePrimaryKeySqlite(
12658
- it.name,
12659
- it.alteredCompositePKs
12660
- );
12926
+ let addedCompositePKs;
12927
+ let deletedCompositePKs;
12928
+ let alteredCompositePKs;
12929
+ if (dialect3 === "sqlite") {
12930
+ addedCompositePKs = prepareAddCompositePrimaryKeySqlite(
12931
+ it.name,
12932
+ it.addedCompositePKs
12933
+ );
12934
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeySqlite(
12935
+ it.name,
12936
+ it.deletedCompositePKs
12937
+ );
12938
+ alteredCompositePKs = prepareAlterCompositePrimaryKeySqlite(
12939
+ it.name,
12940
+ it.alteredCompositePKs
12941
+ );
12942
+ } else if (dialect3 === "pg") {
12943
+ addedCompositePKs = prepareAddCompositePrimaryKeyPg(
12944
+ it.name,
12945
+ it.addedCompositePKs,
12946
+ curFull
12947
+ );
12948
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeyPg(
12949
+ it.name,
12950
+ it.deletedCompositePKs,
12951
+ prevFull
12952
+ );
12953
+ alteredCompositePKs = prepareAlterCompositePrimaryKeyPg(
12954
+ it.name,
12955
+ it.alteredCompositePKs,
12956
+ prevFull,
12957
+ curFull
12958
+ );
12959
+ } else {
12960
+ addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
12961
+ it.name,
12962
+ it.addedCompositePKs,
12963
+ curFull
12964
+ );
12965
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
12966
+ it.name,
12967
+ it.deletedCompositePKs,
12968
+ prevFull
12969
+ );
12970
+ alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
12971
+ it.name,
12972
+ it.alteredCompositePKs,
12973
+ prevFull,
12974
+ curFull
12975
+ );
12976
+ }
12661
12977
  if (it.schema && typeof it.schema !== "string") {
12662
12978
  switch (it.schema.type) {
12663
12979
  case "added": {
@@ -12780,8 +13096,16 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
12780
13096
  jsonStatements.push(...jsonAlterEnumsWithAddedValues);
12781
13097
  if (dialect3 === "sqlite") {
12782
13098
  jsonStatements.push(...jsonSQLiteCreateTables);
13099
+ } else if (dialect3 === "pg") {
13100
+ const jsonPgCreateTables = created.map((it) => {
13101
+ return preparePgCreateTableJson(it, curFull);
13102
+ });
13103
+ jsonStatements.push(...jsonPgCreateTables);
12783
13104
  } else {
12784
- jsonStatements.push(...jsonCreateTables);
13105
+ const jsonMySqlCreateTables = created.map((it) => {
13106
+ return prepareMySqlCreateTableJson(it, curFull);
13107
+ });
13108
+ jsonStatements.push(...jsonMySqlCreateTables);
12785
13109
  }
12786
13110
  jsonStatements.push(...jsonDropTables);
12787
13111
  jsonStatements.push(...jsonRenameTables);
@@ -12810,7 +13134,11 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
12810
13134
  }
12811
13135
  });
12812
13136
  const _meta = prepareMigrationMeta(rSchemas, rTables, rColumns);
12813
- return { statements: jsonStatements, sqlStatements: uniqueSqlStatements, _meta };
13137
+ return {
13138
+ statements: jsonStatements,
13139
+ sqlStatements: uniqueSqlStatements,
13140
+ _meta
13141
+ };
12814
13142
  };
12815
13143
 
12816
13144
  // src/cli/commands/upFolders.ts
@@ -12945,7 +13273,7 @@ var snapshotsPriorV4 = (out) => {
12945
13273
  return pathJson;
12946
13274
  });
12947
13275
  };
12948
- var prepareOutFolder = (out, dialect3) => {
13276
+ var prepareOutFolder2 = (out, dialect3) => {
12949
13277
  const meta = (0, import_path.join)(out, "meta");
12950
13278
  const journalPath = (0, import_path.join)(meta, "_journal.json");
12951
13279
  if (!(0, import_fs.existsSync)((0, import_path.join)(out, "meta"))) {
@@ -12969,7 +13297,7 @@ var validatorForDialect = (dialect3) => {
12969
13297
  case "pg":
12970
13298
  return { validator: backwardCompatiblePgSchema, version: 5 };
12971
13299
  case "sqlite":
12972
- return { validator: backwardCompatibleSqliteSchema, version: 4 };
13300
+ return { validator: backwardCompatibleSqliteSchema, version: 5 };
12973
13301
  case "mysql":
12974
13302
  return { validator: backwardCompatibleMysqlSchema, version: 5 };
12975
13303
  }
@@ -13018,7 +13346,7 @@ var validateWithReport = (snapshots, dialect3) => {
13018
13346
  return result;
13019
13347
  };
13020
13348
  var prepareMigrationFolder = (outFolder = "drizzle", dialect3) => {
13021
- const { snapshots, journal } = prepareOutFolder(outFolder, dialect3);
13349
+ const { snapshots, journal } = prepareOutFolder2(outFolder, dialect3);
13022
13350
  const report = validateWithReport(snapshots, dialect3);
13023
13351
  if (report.nonLatest.length > 0) {
13024
13352
  console.log(