drizzle-kit 0.19.4-befd1c5 → 0.19.5-0b8d69c

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/index.cjs +531 -157
  2. package/package.json +2 -2
  3. package/utils.js +110 -25
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-kit",
3
- "version": "0.19.4-befd1c5",
3
+ "version": "0.19.5-0b8d69c",
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-e9da18b",
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
@@ -12822,7 +12822,8 @@ var tableSquashed = objectType({
12822
12822
  columns: recordType(stringType(), column),
12823
12823
  indexes: recordType(stringType(), stringType()),
12824
12824
  foreignKeys: recordType(stringType(), stringType()),
12825
- compositePrimaryKeys: recordType(stringType(), stringType())
12825
+ compositePrimaryKeys: recordType(stringType(), stringType()),
12826
+ uniqueConstraints: recordType(stringType(), stringType()).default({})
12826
12827
  }).strict();
12827
12828
  var schemaSquashed = objectType({
12828
12829
  version: literalType("5"),
@@ -13237,12 +13238,17 @@ var tableV33 = objectType({
13237
13238
  indexes: recordType(stringType(), index3),
13238
13239
  foreignKeys: recordType(stringType(), fk3)
13239
13240
  }).strict();
13241
+ var uniqueConstraint3 = objectType({
13242
+ name: stringType(),
13243
+ columns: stringType().array()
13244
+ }).strict();
13240
13245
  var table3 = objectType({
13241
13246
  name: stringType(),
13242
13247
  columns: recordType(stringType(), column3),
13243
13248
  indexes: recordType(stringType(), index3),
13244
13249
  foreignKeys: recordType(stringType(), fk3),
13245
- compositePrimaryKeys: recordType(stringType(), compositePK3)
13250
+ compositePrimaryKeys: recordType(stringType(), compositePK3),
13251
+ uniqueConstraints: recordType(stringType(), uniqueConstraint3).default({})
13246
13252
  }).strict();
13247
13253
  var dialect2 = enumType(["sqlite"]);
13248
13254
  var schemaHash3 = objectType({
@@ -13280,7 +13286,8 @@ var tableSquashed3 = objectType({
13280
13286
  columns: recordType(stringType(), column3),
13281
13287
  indexes: recordType(stringType(), stringType()),
13282
13288
  foreignKeys: recordType(stringType(), stringType()),
13283
- compositePrimaryKeys: recordType(stringType(), stringType())
13289
+ compositePrimaryKeys: recordType(stringType(), stringType()),
13290
+ uniqueConstraints: recordType(stringType(), stringType()).default({})
13284
13291
  }).strict();
13285
13292
  var schemaSquashed2 = objectType({
13286
13293
  version: latestVersion,
@@ -13303,6 +13310,13 @@ var SQLiteSquasher = {
13303
13310
  });
13304
13311
  return result;
13305
13312
  },
13313
+ squashUnique: (unq) => {
13314
+ return `${unq.name};${unq.columns.join(",")}`;
13315
+ },
13316
+ unsquashUnique: (unq) => {
13317
+ const [name, columns] = unq.split(";");
13318
+ return { name, columns: columns.split(",") };
13319
+ },
13306
13320
  squashFK: (fk4) => {
13307
13321
  return `${fk4.name};${fk4.tableFrom};${fk4.columnsFrom.join(",")};${fk4.tableTo};${fk4.columnsTo.join(",")};${fk4.onUpdate ?? ""};${fk4.onDelete ?? ""}`;
13308
13322
  },
@@ -13666,9 +13680,9 @@ var PgCreateTableConvertor = class extends Convertor {
13666
13680
  const primaryKeyStatement = column4.primaryKey ? " PRIMARY KEY" : "";
13667
13681
  const notNullStatement = column4.notNull ? " NOT NULL" : "";
13668
13682
  const defaultStatement = column4.default !== void 0 ? ` DEFAULT ${column4.default}` : "";
13669
- const uniqueConstraint3 = column4.isUnique ? ` CONSTRAINT ${column4.uniqueName} UNIQUE${column4.nullsNotDistinct ? " NULLS NOT DISTINCT" : ""}` : "";
13683
+ const uniqueConstraint4 = column4.isUnique ? ` CONSTRAINT "${column4.uniqueName}" UNIQUE${column4.nullsNotDistinct ? " NULLS NOT DISTINCT" : ""}` : "";
13670
13684
  const type = isPgNativeType(column4.type) ? column4.type : `"${column4.type}"`;
13671
- statement += ` "${column4.name}" ${type}${primaryKeyStatement}${defaultStatement}${notNullStatement}${uniqueConstraint3}`;
13685
+ statement += ` "${column4.name}" ${type}${primaryKeyStatement}${defaultStatement}${notNullStatement}${uniqueConstraint4}`;
13672
13686
  statement += i === columns.length - 1 ? "" : ",\n";
13673
13687
  }
13674
13688
  if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
@@ -13677,10 +13691,10 @@ var PgCreateTableConvertor = class extends Convertor {
13677
13691
  statement += ` CONSTRAINT ${st.compositePkName} PRIMARY KEY("${compositePK4.columns.join(`","`)}")`;
13678
13692
  }
13679
13693
  if (typeof uniqueConstraints !== "undefined" && uniqueConstraints.length > 0) {
13680
- for (const uniqueConstraint3 of uniqueConstraints) {
13694
+ for (const uniqueConstraint4 of uniqueConstraints) {
13681
13695
  statement += ",\n";
13682
- const unsquashedUnique = PgSquasher.unsquashUnique(uniqueConstraint3);
13683
- statement += ` CONSTRAINT ${unsquashedUnique.name} UNIQUE("${unsquashedUnique.columns.join(`","`)}")`;
13696
+ const unsquashedUnique = PgSquasher.unsquashUnique(uniqueConstraint4);
13697
+ statement += ` CONSTRAINT "${unsquashedUnique.name}" UNIQUE${unsquashedUnique.nullsNotDistinct ? " NULLS NOT DISTINCT" : ""}("${unsquashedUnique.columns.join(`","`)}")`;
13684
13698
  }
13685
13699
  }
13686
13700
  statement += `
@@ -13713,13 +13727,13 @@ var MySqlCreateTableConvertor = class extends Convertor {
13713
13727
  if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
13714
13728
  statement += ",\n";
13715
13729
  const compositePK4 = MySqlSquasher.unsquashPK(compositePKs[0]);
13716
- statement += ` CONSTRAINT ${st.compositePkName} PRIMARY KEY("${compositePK4.columns.join(`","`)}")`;
13730
+ statement += ` CONSTRAINT \`${st.compositePkName}\` PRIMARY KEY(\`${compositePK4.columns.join(`\`,\``)}\`)`;
13717
13731
  }
13718
13732
  if (typeof uniqueConstraints !== "undefined" && uniqueConstraints.length > 0) {
13719
- for (const uniqueConstraint3 of uniqueConstraints) {
13733
+ for (const uniqueConstraint4 of uniqueConstraints) {
13720
13734
  statement += ",\n";
13721
- const unsquashedUnique = MySqlSquasher.unsquashUnique(uniqueConstraint3);
13722
- statement += ` CONSTRAINT ${unsquashedUnique.name} UNIQUE(\`${unsquashedUnique.columns.join(`\`,\``)}\`)`;
13735
+ const unsquashedUnique = MySqlSquasher.unsquashUnique(uniqueConstraint4);
13736
+ statement += ` CONSTRAINT \`${unsquashedUnique.name}\` UNIQUE(\`${unsquashedUnique.columns.join(`\`,\``)}\`)`;
13723
13737
  }
13724
13738
  }
13725
13739
  statement += `
@@ -13734,22 +13748,29 @@ var SQLiteCreateTableConvertor = class extends Convertor {
13734
13748
  return statement.type === "sqlite_create_table" && dialect3 === "sqlite";
13735
13749
  }
13736
13750
  convert(st) {
13737
- const { tableName, columns, referenceData, compositePKs } = st;
13751
+ const {
13752
+ tableName,
13753
+ columns,
13754
+ referenceData,
13755
+ compositePKs,
13756
+ uniqueConstraints
13757
+ } = st;
13738
13758
  let statement = "";
13739
- statement += `CREATE TABLE \`${tableName}\` (`;
13759
+ statement += `CREATE TABLE \`${tableName}\` (
13760
+ `;
13740
13761
  for (let i = 0; i < columns.length; i++) {
13741
13762
  const column4 = columns[i];
13742
13763
  const primaryKeyStatement = column4.primaryKey ? " PRIMARY KEY" : "";
13743
13764
  const notNullStatement = column4.notNull ? " NOT NULL" : "";
13744
13765
  const defaultStatement = column4.default !== void 0 ? ` DEFAULT ${column4.default}` : "";
13745
13766
  const autoincrementStatement = column4.autoincrement ? " AUTOINCREMENT" : "";
13746
- statement += "\n ";
13767
+ statement += " ";
13747
13768
  statement += `\`${column4.name}\` ${column4.type}${primaryKeyStatement}${autoincrementStatement}${defaultStatement}${notNullStatement}`;
13748
- statement += ",";
13769
+ statement += i === columns.length - 1 ? "" : ",\n";
13749
13770
  }
13750
13771
  compositePKs.forEach((it) => {
13751
- statement += "\n ";
13752
- statement += `PRIMARY KEY(${it.map((it2) => `\`${it2}\``).join(", ")}),`;
13772
+ statement += ",\n ";
13773
+ statement += `PRIMARY KEY(${it.map((it2) => `\`${it2}\``).join(", ")})`;
13753
13774
  });
13754
13775
  for (let i = 0; i < referenceData.length; i++) {
13755
13776
  const referenceAsString = referenceData[i];
@@ -13766,11 +13787,17 @@ var SQLiteCreateTableConvertor = class extends Convertor {
13766
13787
  const onUpdateStatement = onUpdate ? ` ON UPDATE ${onUpdate}` : "";
13767
13788
  const fromColumnsString = columnsFrom.map((it) => `\`${it}\``).join(",");
13768
13789
  const toColumnsString = columnsTo.map((it) => `\`${it}\``).join(",");
13790
+ statement += ",";
13769
13791
  statement += "\n ";
13770
13792
  statement += `FOREIGN KEY (${fromColumnsString}) REFERENCES \`${tableTo}\`(${toColumnsString})${onUpdateStatement}${onDeleteStatement}`;
13771
- statement += ",";
13772
13793
  }
13773
- statement = statement.trimChar(",");
13794
+ if (typeof uniqueConstraints !== "undefined" && uniqueConstraints.length > 0) {
13795
+ for (const uniqueConstraint4 of uniqueConstraints) {
13796
+ statement += ",\n";
13797
+ const unsquashedUnique = MySqlSquasher.unsquashUnique(uniqueConstraint4);
13798
+ statement += ` CONSTRAINT ${unsquashedUnique.name} UNIQUE(\`${unsquashedUnique.columns.join(`\`,\``)}\`)`;
13799
+ }
13800
+ }
13774
13801
  statement += `
13775
13802
  `;
13776
13803
  statement += `);`;
@@ -13786,7 +13813,7 @@ var PgAlterTableAddUniqueConstraintConvertor = class extends Convertor {
13786
13813
  convert(statement) {
13787
13814
  const unsquashed = PgSquasher.unsquashUnique(statement.data);
13788
13815
  const tableNameWithSchema = statement.schema ? `"${statement.schema}"."${statement.tableName}"` : `"${statement.tableName}"`;
13789
- return `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${unsquashed.name}" UNIQUE("${unsquashed.columns.join('","')}");`;
13816
+ return `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${unsquashed.name}" UNIQUE${unsquashed.nullsNotDistinct ? " NULLS NOT DISTINCT" : ""}("${unsquashed.columns.join('","')}");`;
13790
13817
  }
13791
13818
  };
13792
13819
  var PgAlterTableDropUniqueConstraintConvertor = class extends Convertor {
@@ -13819,6 +13846,32 @@ var MySQLAlterTableDropUniqueConstraintConvertor = class extends Convertor {
13819
13846
  return `ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT \`${unsquashed.name}\`;`;
13820
13847
  }
13821
13848
  };
13849
+ var SQLiteAlterTableAddUniqueConstraintConvertor = class extends Convertor {
13850
+ can(statement, dialect3) {
13851
+ return statement.type === "create_unique_constraint" && dialect3 === "sqlite";
13852
+ }
13853
+ convert(statement) {
13854
+ return `/*
13855
+ 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
13856
+ Please refer to: https://www.techonthenet.com/sqlite/unique.php
13857
+
13858
+ Due to that we don't generate migration automatically and it has to be done manually
13859
+ */`;
13860
+ }
13861
+ };
13862
+ var SQLiteAlterTableDropUniqueConstraintConvertor = class extends Convertor {
13863
+ can(statement, dialect3) {
13864
+ return statement.type === "delete_unique_constraint" && dialect3 === "sqlite";
13865
+ }
13866
+ convert(statement) {
13867
+ return `/*
13868
+ 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
13869
+ Please refer to: https://www.techonthenet.com/sqlite/unique.php
13870
+
13871
+ Due to that we don't generate migration automatically and it has to be done manually
13872
+ */`;
13873
+ }
13874
+ };
13822
13875
  var CreateTypeEnumConvertor = class extends Convertor {
13823
13876
  can(statement) {
13824
13877
  return statement.type === "create_type_enum";
@@ -14812,6 +14865,8 @@ convertors.push(new SQLiteAlterTableAlterColumnSetTypeConvertor());
14812
14865
  convertors.push(new SqliteAlterForeignKeyConvertor());
14813
14866
  convertors.push(new SqliteDeleteForeignKeyConvertor());
14814
14867
  convertors.push(new SqliteCreateForeignKeyConvertor());
14868
+ convertors.push(new SQLiteAlterTableAddUniqueConstraintConvertor());
14869
+ convertors.push(new SQLiteAlterTableDropUniqueConstraintConvertor());
14815
14870
  convertors.push(new SqliteAlterTableAlterColumnSetNotNullConvertor());
14816
14871
  convertors.push(new SqliteAlterTableAlterColumnDropNotNullConvertor());
14817
14872
  convertors.push(new SqliteAlterTableAlterColumnSetDefaultConvertor());
@@ -14900,7 +14955,7 @@ var prepareMySqlCreateTableJson = (table4, json2) => {
14900
14955
  };
14901
14956
  };
14902
14957
  var prepareSQLiteCreateTable = (table4) => {
14903
- const { name, columns } = table4;
14958
+ const { name, columns, uniqueConstraints } = table4;
14904
14959
  const references2 = Object.values(table4.foreignKeys);
14905
14960
  const composites = Object.values(table4.compositePrimaryKeys).map(
14906
14961
  (it) => SQLiteSquasher.unsquashPK(it)
@@ -14910,7 +14965,8 @@ var prepareSQLiteCreateTable = (table4) => {
14910
14965
  tableName: name,
14911
14966
  columns: Object.values(columns),
14912
14967
  referenceData: references2,
14913
- compositePKs: composites
14968
+ compositePKs: composites,
14969
+ uniqueConstraints: Object.values(uniqueConstraints)
14914
14970
  };
14915
14971
  };
14916
14972
  var prepareDropTableJson = (table4) => {
@@ -15597,7 +15653,37 @@ var diffResultScheme = objectType({
15597
15653
  }).strict();
15598
15654
  var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesResolver, columnsResolver, prevFull, curFull) => {
15599
15655
  var _a, _b;
15600
- const diffResult = applyJsonDiff(json1, json2);
15656
+ let diffResult;
15657
+ if (dialect3 === "mysql") {
15658
+ for (const tableName in json1.tables) {
15659
+ const table4 = json1.tables[tableName];
15660
+ for (const indexName in table4.indexes) {
15661
+ const index4 = MySqlSquasher.unsquashIdx(table4.indexes[indexName]);
15662
+ if (index4.isUnique) {
15663
+ table4.uniqueConstraints[indexName] = MySqlSquasher.squashUnique({
15664
+ name: index4.name,
15665
+ columns: index4.columns
15666
+ });
15667
+ delete json1.tables[tableName].indexes[index4.name];
15668
+ }
15669
+ }
15670
+ }
15671
+ for (const tableName in json2.tables) {
15672
+ const table4 = json2.tables[tableName];
15673
+ for (const indexName in table4.indexes) {
15674
+ const index4 = MySqlSquasher.unsquashIdx(table4.indexes[indexName]);
15675
+ if (index4.isUnique) {
15676
+ table4.uniqueConstraints[indexName] = MySqlSquasher.squashUnique({
15677
+ name: index4.name,
15678
+ columns: index4.columns
15679
+ });
15680
+ delete json2.tables[tableName].indexes[index4.name];
15681
+ }
15682
+ }
15683
+ }
15684
+ diffResult = applyJsonDiff(json1, json2);
15685
+ }
15686
+ diffResult = applyJsonDiff(json1, json2);
15601
15687
  if (Object.keys(diffResult).length === 0) {
15602
15688
  return { statements: [], sqlStatements: [], _meta: void 0 };
15603
15689
  }
@@ -15768,7 +15854,6 @@ var applySnapshotsDiff = async (json1, json2, dialect3, schemasResolver, tablesR
15768
15854
  prevFull
15769
15855
  );
15770
15856
  }
15771
- ;
15772
15857
  alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
15773
15858
  it.name,
15774
15859
  it.alteredCompositePKs,