drizzle-kit 0.19.3-d338b71 → 0.19.4-280a402

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 (3) hide show
  1. package/index.cjs +745 -181
  2. package/package.json +2 -2
  3. package/utils.js +282 -44
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-kit",
3
- "version": "0.19.3-d338b71",
3
+ "version": "0.19.4-280a402",
4
4
  "repository": "https://github.com/drizzle-team/drizzle-kit-mirror",
5
5
  "author": "Drizzle Team",
6
6
  "license": "MIT",
@@ -79,7 +79,7 @@
79
79
  "better-sqlite3": "^8.4.0",
80
80
  "dockerode": "^3.3.4",
81
81
  "dotenv": "^16.0.3",
82
- "drizzle-orm": "0.27.0",
82
+ "drizzle-orm": "0.27.1-8a21e7b",
83
83
  "eslint": "^8.29.0",
84
84
  "eslint-config-prettier": "^8.5.0",
85
85
  "eslint-plugin-prettier": "^4.2.1",
package/utils.js CHANGED
@@ -12759,6 +12759,10 @@ var compositePK = objectType({
12759
12759
  name: stringType(),
12760
12760
  columns: stringType().array()
12761
12761
  }).strict();
12762
+ var uniqueConstraint = objectType({
12763
+ name: stringType(),
12764
+ columns: stringType().array()
12765
+ }).strict();
12762
12766
  var tableV4 = objectType({
12763
12767
  name: stringType(),
12764
12768
  schema: stringType().optional(),
@@ -12772,7 +12776,8 @@ var table = objectType({
12772
12776
  columns: recordType(stringType(), column),
12773
12777
  indexes: recordType(stringType(), index),
12774
12778
  foreignKeys: recordType(stringType(), fk),
12775
- compositePrimaryKeys: recordType(stringType(), compositePK)
12779
+ compositePrimaryKeys: recordType(stringType(), compositePK),
12780
+ uniqueConstraints: recordType(stringType(), uniqueConstraint).default({})
12776
12781
  }).strict();
12777
12782
  var dialect = literalType("mysql");
12778
12783
  var schemaHash = objectType({
@@ -12855,6 +12860,13 @@ var MySqlSquasher = {
12855
12860
  const splitted = pk.split(";");
12856
12861
  return { name: splitted[0], columns: splitted[1].split(",") };
12857
12862
  },
12863
+ squashUnique: (unq) => {
12864
+ return `${unq.name};${unq.columns.join(",")}`;
12865
+ },
12866
+ unsquashUnique: (unq) => {
12867
+ const [name, columns] = unq.split(";");
12868
+ return { name, columns: columns.split(",") };
12869
+ },
12858
12870
  squashFK: (fk4) => {
12859
12871
  return `${fk4.name};${fk4.tableFrom};${fk4.columnsFrom.join(",")};${fk4.tableTo};${fk4.columnsTo.join(",")};${fk4.onUpdate ?? ""};${fk4.onDelete ?? ""}`;
12860
12872
  },
@@ -12881,7 +12893,11 @@ var MySqlSquasher = {
12881
12893
  }
12882
12894
  };
12883
12895
  var mysqlSchema = schema;
12884
- var backwardCompatibleMysqlSchema = unionType([schemaV3, schemaV4, schema]);
12896
+ var backwardCompatibleMysqlSchema = unionType([
12897
+ schemaV3,
12898
+ schemaV4,
12899
+ schema
12900
+ ]);
12885
12901
  var dryMySql = mysqlSchema.parse({
12886
12902
  version: snapshotVersion,
12887
12903
  dialect: "mysql",
@@ -12974,7 +12990,10 @@ var column2 = objectType({
12974
12990
  type: stringType(),
12975
12991
  primaryKey: booleanType(),
12976
12992
  notNull: booleanType(),
12977
- default: anyType().optional()
12993
+ default: anyType().optional(),
12994
+ isUnique: anyType().optional(),
12995
+ uniqueName: stringType().optional(),
12996
+ nullsNotDistinct: booleanType().optional()
12978
12997
  }).strict();
12979
12998
  var tableV32 = objectType({
12980
12999
  name: stringType(),
@@ -12986,6 +13005,11 @@ var compositePK2 = objectType({
12986
13005
  name: stringType(),
12987
13006
  columns: stringType().array()
12988
13007
  }).strict();
13008
+ var uniqueConstraint2 = objectType({
13009
+ name: stringType(),
13010
+ columns: stringType().array(),
13011
+ nullsNotDistinct: booleanType()
13012
+ }).strict();
12989
13013
  var tableV42 = objectType({
12990
13014
  name: stringType(),
12991
13015
  schema: stringType(),
@@ -12999,7 +13023,8 @@ var table2 = objectType({
12999
13023
  columns: recordType(stringType(), column2),
13000
13024
  indexes: recordType(stringType(), index2),
13001
13025
  foreignKeys: recordType(stringType(), fk2),
13002
- compositePrimaryKeys: recordType(stringType(), compositePK2)
13026
+ compositePrimaryKeys: recordType(stringType(), compositePK2),
13027
+ uniqueConstraints: recordType(stringType(), uniqueConstraint2).default({})
13003
13028
  }).strict();
13004
13029
  var schemaHash2 = objectType({
13005
13030
  id: stringType(),
@@ -13036,7 +13061,8 @@ var tableSquashed2 = objectType({
13036
13061
  columns: recordType(stringType(), column2),
13037
13062
  indexes: recordType(stringType(), stringType()),
13038
13063
  foreignKeys: recordType(stringType(), stringType()),
13039
- compositePrimaryKeys: recordType(stringType(), stringType())
13064
+ compositePrimaryKeys: recordType(stringType(), stringType()),
13065
+ uniqueConstraints: recordType(stringType(), stringType())
13040
13066
  }).strict();
13041
13067
  var tableSquashedV42 = objectType({
13042
13068
  name: stringType(),
@@ -13092,6 +13118,17 @@ var PgSquasher = {
13092
13118
  unsquashPK: (pk) => {
13093
13119
  return { name: "", columns: pk.split(",") };
13094
13120
  },
13121
+ squashUnique: (unq) => {
13122
+ return `${unq.name};${unq.columns.join(",")};${unq.nullsNotDistinct}`;
13123
+ },
13124
+ unsquashUnique: (unq) => {
13125
+ const [
13126
+ name,
13127
+ columns,
13128
+ nullsNotDistinct
13129
+ ] = unq.split(";");
13130
+ return { name, columns: columns.split(","), nullsNotDistinct: nullsNotDistinct === "true" };
13131
+ },
13095
13132
  unsquashFK: (input) => {
13096
13133
  const [
13097
13134
  name,
@@ -13126,6 +13163,9 @@ var squashPgScheme = (json) => {
13126
13163
  const squashedPKs = mapValues(it[1].compositePrimaryKeys, (pk) => {
13127
13164
  return PgSquasher.squashPK(pk);
13128
13165
  });
13166
+ const squashedUniqueConstraints = mapValues(it[1].uniqueConstraints, (unq) => {
13167
+ return PgSquasher.squashUnique(unq);
13168
+ });
13129
13169
  return [
13130
13170
  it[0],
13131
13171
  {
@@ -13134,7 +13174,8 @@ var squashPgScheme = (json) => {
13134
13174
  columns: it[1].columns,
13135
13175
  indexes: squashedIndexes,
13136
13176
  foreignKeys: squashedFKs,
13137
- compositePrimaryKeys: squashedPKs
13177
+ compositePrimaryKeys: squashedPKs,
13178
+ uniqueConstraints: squashedUniqueConstraints
13138
13179
  }
13139
13180
  ];
13140
13181
  })
@@ -13196,12 +13237,17 @@ var tableV33 = objectType({
13196
13237
  indexes: recordType(stringType(), index3),
13197
13238
  foreignKeys: recordType(stringType(), fk3)
13198
13239
  }).strict();
13240
+ var uniqueConstraint3 = objectType({
13241
+ name: stringType(),
13242
+ columns: stringType().array()
13243
+ }).strict();
13199
13244
  var table3 = objectType({
13200
13245
  name: stringType(),
13201
13246
  columns: recordType(stringType(), column3),
13202
13247
  indexes: recordType(stringType(), index3),
13203
13248
  foreignKeys: recordType(stringType(), fk3),
13204
- compositePrimaryKeys: recordType(stringType(), compositePK3)
13249
+ compositePrimaryKeys: recordType(stringType(), compositePK3),
13250
+ uniqueConstraints: recordType(stringType(), uniqueConstraint3).default({})
13205
13251
  }).strict();
13206
13252
  var dialect2 = enumType(["sqlite"]);
13207
13253
  var schemaHash3 = objectType({
@@ -13262,6 +13308,13 @@ var SQLiteSquasher = {
13262
13308
  });
13263
13309
  return result;
13264
13310
  },
13311
+ squashUnique: (unq) => {
13312
+ return `${unq.name};${unq.columns.join(",")}`;
13313
+ },
13314
+ unsquashUnique: (unq) => {
13315
+ const [name, columns] = unq.split(";");
13316
+ return { name, columns: columns.split(",") };
13317
+ },
13265
13318
  squashFK: (fk4) => {
13266
13319
  return `${fk4.name};${fk4.tableFrom};${fk4.columnsFrom.join(",")};${fk4.tableTo};${fk4.columnsTo.join(",")};${fk4.onUpdate ?? ""};${fk4.onDelete ?? ""}`;
13267
13320
  },
@@ -13440,6 +13493,15 @@ var findAlternationsInTable = (table4, tableSchema) => {
13440
13493
  const alteredCompositePKs = Object.fromEntries(Object.entries(table4.compositePrimaryKeys || {}).filter((it) => {
13441
13494
  return !it[0].endsWith("__deleted") && !it[0].endsWith("__added");
13442
13495
  }));
13496
+ const addedUniqueConstraints = Object.fromEntries(Object.entries(table4.uniqueConstraints || {}).filter((it) => {
13497
+ return it[0].endsWith("__added");
13498
+ }));
13499
+ const deletedUniqueConstraints = Object.fromEntries(Object.entries(table4.uniqueConstraints || {}).filter((it) => {
13500
+ return it[0].endsWith("__deleted");
13501
+ }));
13502
+ const alteredUniqueConstraints = Object.fromEntries(Object.entries(table4.uniqueConstraints || {}).filter((it) => {
13503
+ return !it[0].endsWith("__deleted") && !it[0].endsWith("__added");
13504
+ }));
13443
13505
  const mappedAltered = altered.map((it) => alternationsInColumn(it));
13444
13506
  return {
13445
13507
  name: table4.name,
@@ -13455,7 +13517,10 @@ var findAlternationsInTable = (table4, tableSchema) => {
13455
13517
  alteredForeignKeys,
13456
13518
  addedCompositePKs,
13457
13519
  deletedCompositePKs,
13458
- alteredCompositePKs
13520
+ alteredCompositePKs,
13521
+ addedUniqueConstraints,
13522
+ deletedUniqueConstraints,
13523
+ alteredUniqueConstraints
13459
13524
  };
13460
13525
  };
13461
13526
  var alternationsInColumn = (column4) => {
@@ -13603,30 +13668,37 @@ var PgCreateTableConvertor = class extends Convertor {
13603
13668
  return statement.type === "create_table" && dialect3 === "pg";
13604
13669
  }
13605
13670
  convert(st) {
13606
- const { tableName, schema: schema4, columns, compositePKs } = st;
13671
+ const { tableName, schema: schema4, columns, compositePKs, uniqueConstraints } = st;
13607
13672
  let statement = "";
13608
13673
  const name = schema4 ? `"${schema4}"."${tableName}"` : `"${tableName}"`;
13609
13674
  statement += `CREATE TABLE IF NOT EXISTS ${name} (
13610
13675
  `;
13611
13676
  for (let i = 0; i < columns.length; i++) {
13612
13677
  const column4 = columns[i];
13613
- const primaryKeyStatement = column4.primaryKey ? "PRIMARY KEY" : "";
13614
- const notNullStatement = column4.notNull ? "NOT NULL" : "";
13615
- const defaultStatement = column4.default !== void 0 ? `DEFAULT ${column4.default}` : "";
13678
+ const primaryKeyStatement = column4.primaryKey ? " PRIMARY KEY" : "";
13679
+ const notNullStatement = column4.notNull ? " NOT NULL" : "";
13680
+ const defaultStatement = column4.default !== void 0 ? ` DEFAULT ${column4.default}` : "";
13681
+ const uniqueConstraint4 = column4.isUnique ? ` CONSTRAINT "${column4.uniqueName}" UNIQUE${column4.nullsNotDistinct ? " NULLS NOT DISTINCT" : ""}` : "";
13616
13682
  const type = isPgNativeType(column4.type) ? column4.type : `"${column4.type}"`;
13617
- statement += " " + `"${column4.name}" ${type} ${primaryKeyStatement} ${defaultStatement} ${notNullStatement}`.replace(/ +/g, " ").trim();
13618
- statement += (i === columns.length - 1 ? "" : ",") + "\n";
13683
+ statement += ` "${column4.name}" ${type}${primaryKeyStatement}${defaultStatement}${notNullStatement}${uniqueConstraint4}`;
13684
+ statement += i === columns.length - 1 ? "" : ",\n";
13619
13685
  }
13620
- statement += `);`;
13621
- statement += `
13622
- `;
13623
13686
  if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
13687
+ statement += ",\n";
13624
13688
  const compositePK4 = PgSquasher.unsquashPK(compositePKs[0]);
13625
- statement += BREAKPOINT;
13626
- statement += `ALTER TABLE ${name} ADD CONSTRAINT "${st.compositePkName}" PRIMARY KEY("${compositePK4.columns.join('","')}");`;
13627
- statement += `
13628
- `;
13689
+ statement += ` CONSTRAINT ${st.compositePkName} PRIMARY KEY("${compositePK4.columns.join(`","`)}")`;
13690
+ }
13691
+ if (typeof uniqueConstraints !== "undefined" && uniqueConstraints.length > 0) {
13692
+ for (const uniqueConstraint4 of uniqueConstraints) {
13693
+ statement += ",\n";
13694
+ const unsquashedUnique = PgSquasher.unsquashUnique(uniqueConstraint4);
13695
+ statement += ` CONSTRAINT "${unsquashedUnique.name}" UNIQUE${unsquashedUnique.nullsNotDistinct ? " NULLS NOT DISTINCT" : ""}("${unsquashedUnique.columns.join(`","`)}")`;
13696
+ }
13629
13697
  }
13698
+ statement += `
13699
+ );`;
13700
+ statement += `
13701
+ `;
13630
13702
  return statement;
13631
13703
  }
13632
13704
  };
@@ -13635,7 +13707,7 @@ var MySqlCreateTableConvertor = class extends Convertor {
13635
13707
  return statement.type === "create_table" && dialect3 === "mysql";
13636
13708
  }
13637
13709
  convert(st) {
13638
- const { tableName, columns, schema: schema4, compositePKs } = st;
13710
+ const { tableName, columns, schema: schema4, compositePKs, uniqueConstraints } = st;
13639
13711
  let statement = "";
13640
13712
  const tName = schema4 ? `\`${schema4}\`.\`${tableName}\`` : `\`${tableName}\``;
13641
13713
  statement += `CREATE TABLE ${tName} (
@@ -13647,17 +13719,23 @@ var MySqlCreateTableConvertor = class extends Convertor {
13647
13719
  const defaultStatement = column4.default !== void 0 ? ` DEFAULT ${column4.default}` : "";
13648
13720
  const onUpdateStatement = column4.onUpdate ? ` ON UPDATE CURRENT_TIMESTAMP` : "";
13649
13721
  const autoincrementStatement = column4.autoincrement ? " AUTO_INCREMENT" : "";
13650
- statement += " " + `\`${column4.name}\` ${column4.type}${autoincrementStatement}${primaryKeyStatement}${notNullStatement}${defaultStatement}${onUpdateStatement}`.trim();
13722
+ statement += ` \`${column4.name}\` ${column4.type}${autoincrementStatement}${primaryKeyStatement}${notNullStatement}${defaultStatement}${onUpdateStatement}`;
13651
13723
  statement += i === columns.length - 1 ? "" : ",\n";
13652
13724
  }
13653
13725
  if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
13654
13726
  statement += ",\n";
13655
13727
  const compositePK4 = MySqlSquasher.unsquashPK(compositePKs[0]);
13656
- statement += ` PRIMARY KEY(\`${compositePK4.columns.join("`,`")}\`)`;
13657
- statement += `
13658
- `;
13728
+ statement += ` CONSTRAINT \`${st.compositePkName}\` PRIMARY KEY("${compositePK4.columns.join(`","`)}")`;
13729
+ }
13730
+ if (typeof uniqueConstraints !== "undefined" && uniqueConstraints.length > 0) {
13731
+ for (const uniqueConstraint4 of uniqueConstraints) {
13732
+ statement += ",\n";
13733
+ const unsquashedUnique = MySqlSquasher.unsquashUnique(uniqueConstraint4);
13734
+ statement += ` CONSTRAINT \`${unsquashedUnique.name}\` UNIQUE(\`${unsquashedUnique.columns.join(`\`,\``)}\`)`;
13735
+ }
13659
13736
  }
13660
- statement += `);`;
13737
+ statement += `
13738
+ );`;
13661
13739
  statement += `
13662
13740
  `;
13663
13741
  return statement;
@@ -13668,21 +13746,28 @@ var SQLiteCreateTableConvertor = class extends Convertor {
13668
13746
  return statement.type === "sqlite_create_table" && dialect3 === "sqlite";
13669
13747
  }
13670
13748
  convert(st) {
13671
- const { tableName, columns, referenceData, compositePKs } = st;
13749
+ const {
13750
+ tableName,
13751
+ columns,
13752
+ referenceData,
13753
+ compositePKs,
13754
+ uniqueConstraints
13755
+ } = st;
13672
13756
  let statement = "";
13673
- statement += `CREATE TABLE \`${tableName}\` (`;
13757
+ statement += `CREATE TABLE \`${tableName}\` (
13758
+ `;
13674
13759
  for (let i = 0; i < columns.length; i++) {
13675
13760
  const column4 = columns[i];
13676
13761
  const primaryKeyStatement = column4.primaryKey ? " PRIMARY KEY" : "";
13677
13762
  const notNullStatement = column4.notNull ? " NOT NULL" : "";
13678
13763
  const defaultStatement = column4.default !== void 0 ? ` DEFAULT ${column4.default}` : "";
13679
13764
  const autoincrementStatement = column4.autoincrement ? " AUTOINCREMENT" : "";
13680
- statement += "\n ";
13765
+ statement += " ";
13681
13766
  statement += `\`${column4.name}\` ${column4.type}${primaryKeyStatement}${autoincrementStatement}${defaultStatement}${notNullStatement}`;
13682
- statement += ",";
13767
+ statement += i === columns.length - 1 ? "" : ",\n";
13683
13768
  }
13684
13769
  compositePKs.forEach((it) => {
13685
- statement += "\n ";
13770
+ statement += ",\n ";
13686
13771
  statement += `PRIMARY KEY(${it.map((it2) => `\`${it2}\``).join(", ")}),`;
13687
13772
  });
13688
13773
  for (let i = 0; i < referenceData.length; i++) {
@@ -13700,11 +13785,17 @@ var SQLiteCreateTableConvertor = class extends Convertor {
13700
13785
  const onUpdateStatement = onUpdate ? ` ON UPDATE ${onUpdate}` : "";
13701
13786
  const fromColumnsString = columnsFrom.map((it) => `\`${it}\``).join(",");
13702
13787
  const toColumnsString = columnsTo.map((it) => `\`${it}\``).join(",");
13788
+ statement += ",";
13703
13789
  statement += "\n ";
13704
13790
  statement += `FOREIGN KEY (${fromColumnsString}) REFERENCES \`${tableTo}\`(${toColumnsString})${onUpdateStatement}${onDeleteStatement}`;
13705
- statement += ",";
13706
13791
  }
13707
- statement = statement.trimChar(",");
13792
+ if (typeof uniqueConstraints !== "undefined" && uniqueConstraints.length > 0) {
13793
+ for (const uniqueConstraint4 of uniqueConstraints) {
13794
+ statement += ",\n";
13795
+ const unsquashedUnique = MySqlSquasher.unsquashUnique(uniqueConstraint4);
13796
+ statement += ` CONSTRAINT ${unsquashedUnique.name} UNIQUE(\`${unsquashedUnique.columns.join(`\`,\``)}\`)`;
13797
+ }
13798
+ }
13708
13799
  statement += `
13709
13800
  `;
13710
13801
  statement += `);`;
@@ -13713,6 +13804,72 @@ var SQLiteCreateTableConvertor = class extends Convertor {
13713
13804
  return statement;
13714
13805
  }
13715
13806
  };
13807
+ var PgAlterTableAddUniqueConstraintConvertor = class extends Convertor {
13808
+ can(statement, dialect3) {
13809
+ return statement.type === "create_unique_constraint" && dialect3 === "pg";
13810
+ }
13811
+ convert(statement) {
13812
+ const unsquashed = PgSquasher.unsquashUnique(statement.data);
13813
+ const tableNameWithSchema = statement.schema ? `"${statement.schema}"."${statement.tableName}"` : `"${statement.tableName}"`;
13814
+ return `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${unsquashed.name}" UNIQUE${unsquashed.nullsNotDistinct ? " NULLS NOT DISTINCT" : ""}("${unsquashed.columns.join('","')}");`;
13815
+ }
13816
+ };
13817
+ var PgAlterTableDropUniqueConstraintConvertor = class extends Convertor {
13818
+ can(statement, dialect3) {
13819
+ return statement.type === "delete_unique_constraint" && dialect3 === "pg";
13820
+ }
13821
+ convert(statement) {
13822
+ const unsquashed = PgSquasher.unsquashUnique(statement.data);
13823
+ const tableNameWithSchema = statement.schema ? `"${statement.schema}"."${statement.tableName}"` : `"${statement.tableName}"`;
13824
+ return `ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT "${unsquashed.name}";`;
13825
+ }
13826
+ };
13827
+ var MySQLAlterTableAddUniqueConstraintConvertor = class extends Convertor {
13828
+ can(statement, dialect3) {
13829
+ return statement.type === "create_unique_constraint" && dialect3 === "mysql";
13830
+ }
13831
+ convert(statement) {
13832
+ const unsquashed = MySqlSquasher.unsquashUnique(statement.data);
13833
+ const tableNameWithSchema = statement.schema ? `\`${statement.schema}\`.\`${statement.tableName}\`` : `\`${statement.tableName}\``;
13834
+ return `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT \`${unsquashed.name}\` UNIQUE(\`${unsquashed.columns.join("`,`")}\`);`;
13835
+ }
13836
+ };
13837
+ var MySQLAlterTableDropUniqueConstraintConvertor = class extends Convertor {
13838
+ can(statement, dialect3) {
13839
+ return statement.type === "delete_unique_constraint" && dialect3 === "mysql";
13840
+ }
13841
+ convert(statement) {
13842
+ const unsquashed = MySqlSquasher.unsquashUnique(statement.data);
13843
+ const tableNameWithSchema = statement.schema ? `\`${statement.schema}\`.\`${statement.tableName}\`` : `\`${statement.tableName}\``;
13844
+ return `ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT \`${unsquashed.name}\`;`;
13845
+ }
13846
+ };
13847
+ var SQLiteAlterTableAddUniqueConstraintConvertor = class extends Convertor {
13848
+ can(statement, dialect3) {
13849
+ return statement.type === "create_unique_constraint" && dialect3 === "sqlite";
13850
+ }
13851
+ convert(statement) {
13852
+ return `/*
13853
+ SQLite does not support "Adding unique constraint to an existing table" out of the box, we do not generate automatic migration for that, so it has to be done manually
13854
+ Please refer to: https://www.techonthenet.com/sqlite/unique.php
13855
+
13856
+ Due to that we don't generate migration automatically and it has to be done manually
13857
+ */`;
13858
+ }
13859
+ };
13860
+ var SQLiteAlterTableDropUniqueConstraintConvertor = class extends Convertor {
13861
+ can(statement, dialect3) {
13862
+ return statement.type === "delete_unique_constraint" && dialect3 === "sqlite";
13863
+ }
13864
+ convert(statement) {
13865
+ return `/*
13866
+ SQLite does not support "Dropping unique constraint from an existing table" out of the box, we do not generate automatic migration for that, so it has to be done manually
13867
+ Please refer to: https://www.techonthenet.com/sqlite/unique.php
13868
+
13869
+ Due to that we don't generate migration automatically and it has to be done manually
13870
+ */`;
13871
+ }
13872
+ };
13716
13873
  var CreateTypeEnumConvertor = class extends Convertor {
13717
13874
  can(statement) {
13718
13875
  return statement.type === "create_type_enum";
@@ -14668,6 +14825,10 @@ convertors.push(new PgAlterTableAddColumnConvertor());
14668
14825
  convertors.push(new MySqlAlterTableAddColumnConvertor());
14669
14826
  convertors.push(new SQLiteAlterTableAddColumnConvertor());
14670
14827
  convertors.push(new PgAlterTableAlterColumnSetTypeConvertor());
14828
+ convertors.push(new PgAlterTableAddUniqueConstraintConvertor());
14829
+ convertors.push(new PgAlterTableDropUniqueConstraintConvertor());
14830
+ convertors.push(new MySQLAlterTableAddUniqueConstraintConvertor());
14831
+ convertors.push(new MySQLAlterTableDropUniqueConstraintConvertor());
14671
14832
  convertors.push(new CreatePgIndexConvertor());
14672
14833
  convertors.push(new CreateMySqlIndexConvertor());
14673
14834
  convertors.push(new CreateSqliteIndexConvertor());
@@ -14702,6 +14863,8 @@ convertors.push(new SQLiteAlterTableAlterColumnSetTypeConvertor());
14702
14863
  convertors.push(new SqliteAlterForeignKeyConvertor());
14703
14864
  convertors.push(new SqliteDeleteForeignKeyConvertor());
14704
14865
  convertors.push(new SqliteCreateForeignKeyConvertor());
14866
+ convertors.push(new SQLiteAlterTableAddUniqueConstraintConvertor());
14867
+ convertors.push(new SQLiteAlterTableDropUniqueConstraintConvertor());
14705
14868
  convertors.push(new SqliteAlterTableAlterColumnSetNotNullConvertor());
14706
14869
  convertors.push(new SqliteAlterTableAlterColumnDropNotNullConvertor());
14707
14870
  convertors.push(new SqliteAlterTableAlterColumnSetDefaultConvertor());
@@ -14762,7 +14925,7 @@ init_lib();
14762
14925
 
14763
14926
  // src/jsonStatements.ts
14764
14927
  var preparePgCreateTableJson = (table4, json2) => {
14765
- const { name, schema: schema4, columns, compositePrimaryKeys } = table4;
14928
+ const { name, schema: schema4, columns, compositePrimaryKeys, uniqueConstraints } = table4;
14766
14929
  return {
14767
14930
  type: "create_table",
14768
14931
  tableName: name,
@@ -14771,11 +14934,12 @@ var preparePgCreateTableJson = (table4, json2) => {
14771
14934
  compositePKs: Object.values(compositePrimaryKeys),
14772
14935
  compositePkName: Object.values(compositePrimaryKeys).length > 0 ? json2.tables[name].compositePrimaryKeys[`${name}_${PgSquasher.unsquashPK(
14773
14936
  Object.values(compositePrimaryKeys)[0]
14774
- ).columns.join("_")}`].name : ""
14937
+ ).columns.join("_")}`].name : "",
14938
+ uniqueConstraints: Object.values(uniqueConstraints)
14775
14939
  };
14776
14940
  };
14777
14941
  var prepareMySqlCreateTableJson = (table4, json2) => {
14778
- const { name, schema: schema4, columns, compositePrimaryKeys } = table4;
14942
+ const { name, schema: schema4, columns, compositePrimaryKeys, uniqueConstraints } = table4;
14779
14943
  return {
14780
14944
  type: "create_table",
14781
14945
  tableName: name,
@@ -14784,11 +14948,12 @@ var prepareMySqlCreateTableJson = (table4, json2) => {
14784
14948
  compositePKs: Object.values(compositePrimaryKeys),
14785
14949
  compositePkName: Object.values(compositePrimaryKeys).length > 0 ? json2.tables[name].compositePrimaryKeys[`${name}_${MySqlSquasher.unsquashPK(
14786
14950
  Object.values(compositePrimaryKeys)[0]
14787
- ).columns.join("_")}`].name : ""
14951
+ ).columns.join("_")}`].name : "",
14952
+ uniqueConstraints: Object.values(uniqueConstraints)
14788
14953
  };
14789
14954
  };
14790
14955
  var prepareSQLiteCreateTable = (table4) => {
14791
- const { name, columns } = table4;
14956
+ const { name, columns, uniqueConstraints } = table4;
14792
14957
  const references2 = Object.values(table4.foreignKeys);
14793
14958
  const composites = Object.values(table4.compositePrimaryKeys).map(
14794
14959
  (it) => SQLiteSquasher.unsquashPK(it)
@@ -14798,7 +14963,8 @@ var prepareSQLiteCreateTable = (table4) => {
14798
14963
  tableName: name,
14799
14964
  columns: Object.values(columns),
14800
14965
  referenceData: references2,
14801
- compositePKs: composites
14966
+ compositePKs: composites,
14967
+ uniqueConstraints: Object.values(uniqueConstraints)
14802
14968
  };
14803
14969
  };
14804
14970
  var prepareDropTableJson = (table4) => {
@@ -15269,6 +15435,26 @@ var prepareAlterCompositePrimaryKeyPg = (tableName, schema4, pks, json1, json2)
15269
15435
  };
15270
15436
  });
15271
15437
  };
15438
+ var prepareAddUniqueConstraintPg = (tableName, schema4, unqs) => {
15439
+ return Object.values(unqs).map((it) => {
15440
+ return {
15441
+ type: "create_unique_constraint",
15442
+ tableName,
15443
+ data: it,
15444
+ schema: schema4
15445
+ };
15446
+ });
15447
+ };
15448
+ var prepareDeleteUniqueConstraintPg = (tableName, schema4, unqs) => {
15449
+ return Object.values(unqs).map((it) => {
15450
+ return {
15451
+ type: "delete_unique_constraint",
15452
+ tableName,
15453
+ data: it,
15454
+ schema: schema4
15455
+ };
15456
+ });
15457
+ };
15272
15458
  var prepareAddCompositePrimaryKeyMySql = (tableName, pks, json2) => {
15273
15459
  return Object.values(pks).map((it) => {
15274
15460
  return {
@@ -15374,12 +15560,14 @@ var columnSchema = objectType({
15374
15560
  name: stringType(),
15375
15561
  type: stringType(),
15376
15562
  primaryKey: booleanType().optional(),
15377
- unique: booleanType().optional(),
15378
15563
  default: anyType().optional(),
15379
15564
  notNull: booleanType().optional(),
15380
15565
  // should it be optional? should if be here?
15381
15566
  autoincrement: booleanType().optional(),
15382
- onUpdate: booleanType().optional()
15567
+ onUpdate: booleanType().optional(),
15568
+ isUnique: anyType().optional(),
15569
+ uniqueName: stringType().optional(),
15570
+ nullsNotDistinct: booleanType().optional()
15383
15571
  }).strict();
15384
15572
  var alteredColumnSchema = objectType({
15385
15573
  name: makeSelfOrChanged(stringType()),
@@ -15405,7 +15593,8 @@ var tableScheme = objectType({
15405
15593
  columns: recordType(stringType(), columnSchema),
15406
15594
  indexes: recordType(stringType(), stringType()),
15407
15595
  foreignKeys: recordType(stringType(), stringType()),
15408
- compositePrimaryKeys: recordType(stringType(), stringType()).default({})
15596
+ compositePrimaryKeys: recordType(stringType(), stringType()).default({}),
15597
+ uniqueConstraints: recordType(stringType(), stringType()).default({})
15409
15598
  }).strict();
15410
15599
  var alteredTableScheme = objectType({
15411
15600
  name: stringType(),
@@ -15439,6 +15628,15 @@ var alteredTableScheme = objectType({
15439
15628
  __new: stringType(),
15440
15629
  __old: stringType()
15441
15630
  })
15631
+ ),
15632
+ addedUniqueConstraints: recordType(stringType(), stringType()),
15633
+ deletedUniqueConstraints: recordType(stringType(), stringType()),
15634
+ alteredUniqueConstraints: recordType(
15635
+ stringType(),
15636
+ objectType({
15637
+ __new: stringType(),
15638
+ __old: stringType()
15639
+ })
15442
15640
  )
15443
15641
  }).strict();
15444
15642
  var diffResultScheme = objectType({
@@ -15528,13 +15726,19 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
15528
15726
  alteredForeignKeys: table4.alteredForeignKeys,
15529
15727
  addedCompositePKs: table4.addedCompositePKs,
15530
15728
  deletedCompositePKs: table4.deletedCompositePKs,
15531
- alteredCompositePKs: table4.alteredCompositePKs
15729
+ alteredCompositePKs: table4.alteredCompositePKs,
15730
+ addedUniqueConstraints: table4.addedUniqueConstraints,
15731
+ deletedUniqueConstraints: table4.deletedUniqueConstraints,
15732
+ alteredUniqueConstraints: table4.alteredUniqueConstraints
15532
15733
  };
15533
15734
  allAlteredResolved.push(resolved);
15534
15735
  }
15535
15736
  const jsonAddedCompositePKs = [];
15536
15737
  const jsonDeletedCompositePKs = [];
15537
15738
  const jsonAlteredCompositePKs = [];
15739
+ const jsonAddedUniqueConstraints = [];
15740
+ const jsonDeletedUniqueConstraints = [];
15741
+ const jsonAlteredUniqueConstraints = [];
15538
15742
  const jsonSetTableSchemas = [];
15539
15743
  const jsonRemoveTableFromSchemas = [];
15540
15744
  const jsonSetNewTableSchemas = [];
@@ -15626,6 +15830,33 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
15626
15830
  curFull
15627
15831
  );
15628
15832
  }
15833
+ let addedUniqueConstraints = [];
15834
+ let deletedUniqueConstraints = [];
15835
+ let alteredUniqueConstraints = [];
15836
+ addedUniqueConstraints = prepareAddUniqueConstraintPg(
15837
+ it.name,
15838
+ schemaUnwrapped,
15839
+ it.addedUniqueConstraints
15840
+ );
15841
+ deletedUniqueConstraints = prepareDeleteUniqueConstraintPg(
15842
+ it.name,
15843
+ schemaUnwrapped,
15844
+ it.deletedUniqueConstraints
15845
+ );
15846
+ if (it.alteredUniqueConstraints) {
15847
+ const added = {};
15848
+ const deleted2 = {};
15849
+ for (const k of Object.keys(it.alteredUniqueConstraints)) {
15850
+ added[k] = it.alteredUniqueConstraints[k].__new;
15851
+ deleted2[k] = it.alteredUniqueConstraints[k].__old;
15852
+ }
15853
+ addedUniqueConstraints.push(
15854
+ ...prepareAddUniqueConstraintPg(it.name, schemaUnwrapped, added)
15855
+ );
15856
+ deletedUniqueConstraints.push(
15857
+ ...prepareDeleteUniqueConstraintPg(it.name, schemaUnwrapped, deleted2)
15858
+ );
15859
+ }
15629
15860
  if (it.schema && typeof it.schema !== "string") {
15630
15861
  switch (it.schema.type) {
15631
15862
  case "added": {
@@ -15658,6 +15889,9 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
15658
15889
  jsonAddedCompositePKs.push(...addedCompositePKs);
15659
15890
  jsonDeletedCompositePKs.push(...deletedCompositePKs);
15660
15891
  jsonAlteredCompositePKs.push(...alteredCompositePKs);
15892
+ jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
15893
+ jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15894
+ jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15661
15895
  });
15662
15896
  const rColumns = jsonRenameColumnsStatements.map((it) => {
15663
15897
  const tableName = it.tableName;
@@ -15782,6 +16016,7 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
15782
16016
  jsonStatements.push(...jsonSQLiteCreateTables);
15783
16017
  } else if (dialect3 === "pg") {
15784
16018
  const jsonPgCreateTables = created.map((it) => {
16019
+ console.log(it);
15785
16020
  return preparePgCreateTableJson(it, curFull);
15786
16021
  });
15787
16022
  jsonStatements.push(...jsonPgCreateTables);
@@ -15795,6 +16030,7 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
15795
16030
  jsonStatements.push(...jsonRenameTables);
15796
16031
  jsonStatements.push(...jsonRenameColumnsStatements);
15797
16032
  jsonStatements.push(...jsonDeletedCompositePKs);
16033
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
15798
16034
  jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
15799
16035
  jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
15800
16036
  jsonStatements.push(...jsonTableAlternations.alterColumns);
@@ -15807,6 +16043,8 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
15807
16043
  jsonStatements.push(...jsonCreateReferences);
15808
16044
  jsonStatements.push(...jsonAddedCompositePKs);
15809
16045
  jsonStatements.push(...jsonAlteredCompositePKs);
16046
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16047
+ jsonStatements.push(...jsonAlteredUniqueConstraints);
15810
16048
  jsonStatements.push(...jsonSetTableSchemas);
15811
16049
  jsonStatements.push(...filteredJsonSetNewTableSchemas);
15812
16050
  jsonStatements.push(...jsonRemoveTableFromSchemas);