drizzle-kit 0.23.2 → 0.24.0-38d6dab

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. package/api.d.mts +20 -4
  2. package/api.d.ts +20 -4
  3. package/api.js +1315 -404
  4. package/api.mjs +1315 -404
  5. package/bin.cjs +1658 -394
  6. package/package.json +1 -1
  7. package/utils.js +14 -0
  8. package/utils.mjs +12 -0
package/api.mjs CHANGED
@@ -8343,6 +8343,16 @@ var init_sqliteSchema = __esm({
8343
8343
  });
8344
8344
 
8345
8345
  // src/utils.ts
8346
+ function isPgArrayType(sqlType) {
8347
+ return sqlType.match(/.*\[\d*\].*|.*\[\].*/g) !== null;
8348
+ }
8349
+ function findAddedAndRemoved(columnNames1, columnNames2) {
8350
+ const set1 = new Set(columnNames1);
8351
+ const set2 = new Set(columnNames2);
8352
+ const addedColumns = columnNames2.filter((it) => !set1.has(it));
8353
+ const removedColumns = columnNames1.filter((it) => !set2.has(it));
8354
+ return { addedColumns, removedColumns };
8355
+ }
8346
8356
  var copy, prepareMigrationMeta, schemaRenameKey, tableRenameKey, columnRenameKey;
8347
8357
  var init_utils = __esm({
8348
8358
  "src/utils.ts"() {
@@ -8716,14 +8726,14 @@ var require_heap = __commonJS({
8716
8726
  return item;
8717
8727
  };
8718
8728
  heapify = function(array, cmp) {
8719
- var i, _i2, _j, _len, _ref, _ref1, _results, _results1;
8729
+ var i, _i2, _j2, _len, _ref, _ref1, _results, _results1;
8720
8730
  if (cmp == null) {
8721
8731
  cmp = defaultCmp;
8722
8732
  }
8723
8733
  _ref1 = function() {
8724
8734
  _results1 = [];
8725
- for (var _j2 = 0, _ref2 = floor(array.length / 2); 0 <= _ref2 ? _j2 < _ref2 : _j2 > _ref2; 0 <= _ref2 ? _j2++ : _j2--) {
8726
- _results1.push(_j2);
8735
+ for (var _j3 = 0, _ref2 = floor(array.length / 2); 0 <= _ref2 ? _j3 < _ref2 : _j3 > _ref2; 0 <= _ref2 ? _j3++ : _j3--) {
8736
+ _results1.push(_j3);
8727
8737
  }
8728
8738
  return _results1;
8729
8739
  }.apply(this).reverse();
@@ -8764,7 +8774,7 @@ var require_heap = __commonJS({
8764
8774
  return result.sort(cmp).reverse();
8765
8775
  };
8766
8776
  nsmallest = function(array, n, cmp) {
8767
- var elem, i, los, result, _i2, _j, _len, _ref, _ref1, _results;
8777
+ var elem, i, los, result, _i2, _j2, _len, _ref, _ref1, _results;
8768
8778
  if (cmp == null) {
8769
8779
  cmp = defaultCmp;
8770
8780
  }
@@ -8787,7 +8797,7 @@ var require_heap = __commonJS({
8787
8797
  }
8788
8798
  heapify(array, cmp);
8789
8799
  _results = [];
8790
- for (i = _j = 0, _ref1 = min2(n, array.length); 0 <= _ref1 ? _j < _ref1 : _j > _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
8800
+ for (i = _j2 = 0, _ref1 = min2(n, array.length); 0 <= _ref1 ? _j2 < _ref1 : _j2 > _ref1; i = 0 <= _ref1 ? ++_j2 : --_j2) {
8791
8801
  _results.push(heappop(array, cmp));
8792
8802
  }
8793
8803
  return _results;
@@ -11452,7 +11462,20 @@ var init_jsonDiffer = __esm({
11452
11462
  });
11453
11463
 
11454
11464
  // src/sqlgenerator.ts
11455
- var pgNativeTypes, isPgNativeType, Convertor, PgCreateTableConvertor, MySqlCreateTableConvertor, SQLiteCreateTableConvertor, PgAlterTableAlterColumnSetGenerated, PgAlterTableAlterColumnDropGenerated, PgAlterTableAlterColumnAlterGenerated, PgAlterTableAddUniqueConstraintConvertor, PgAlterTableDropUniqueConstraintConvertor, MySQLAlterTableAddUniqueConstraintConvertor, MySQLAlterTableDropUniqueConstraintConvertor, SQLiteAlterTableAddUniqueConstraintConvertor, SQLiteAlterTableDropUniqueConstraintConvertor, CreatePgSequenceConvertor, DropPgSequenceConvertor, RenamePgSequenceConvertor, MovePgSequenceConvertor, AlterPgSequenceConvertor, CreateTypeEnumConvertor, AlterTypeAddValueConvertor, PgDropTableConvertor, MySQLDropTableConvertor, SQLiteDropTableConvertor, PgRenameTableConvertor, SqliteRenameTableConvertor, MySqlRenameTableConvertor, PgAlterTableRenameColumnConvertor, MySqlAlterTableRenameColumnConvertor, SQLiteAlterTableRenameColumnConvertor, PgAlterTableDropColumnConvertor, MySqlAlterTableDropColumnConvertor, SQLiteAlterTableDropColumnConvertor, PgAlterTableAddColumnConvertor, MySqlAlterTableAddColumnConvertor, SQLiteAlterTableAddColumnConvertor, PgAlterTableAlterColumnSetTypeConvertor, SQLiteAlterTableAlterColumnSetTypeConvertor, PgAlterTableAlterColumnSetDefaultConvertor, SqliteAlterTableAlterColumnSetDefaultConvertor, PgAlterTableAlterColumnDropDefaultConvertor, PgAlterTableAlterColumnDropGeneratedConvertor, PgAlterTableAlterColumnSetExpressionConvertor, PgAlterTableAlterColumnAlterrGeneratedConvertor, SqliteAlterTableAlterColumnDropGeneratedConvertor, SqliteAlterTableAlterColumnSetExpressionConvertor, SqliteAlterTableAlterColumnAlterGeneratedConvertor, MySqlAlterTableAlterColumnAlterrGeneratedConvertor, MySqlAlterTableAddPk, MySqlAlterTableDropPk, MySqlModifyColumn, SqliteAlterTableAlterColumnDropDefaultConvertor, PgAlterTableCreateCompositePrimaryKeyConvertor, PgAlterTableDeleteCompositePrimaryKeyConvertor, PgAlterTableAlterCompositePrimaryKeyConvertor, MySqlAlterTableCreateCompositePrimaryKeyConvertor, MySqlAlterTableDeleteCompositePrimaryKeyConvertor, MySqlAlterTableAlterCompositePrimaryKeyConvertor, SqliteAlterTableCreateCompositePrimaryKeyConvertor, SqliteAlterTableDeleteCompositePrimaryKeyConvertor, SqliteAlterTableAlterCompositePrimaryKeyConvertor, PgAlterTableAlterColumnSetPrimaryKeyConvertor, PgAlterTableAlterColumnDropPrimaryKeyConvertor, PgAlterTableAlterColumnSetNotNullConvertor, SqliteAlterTableAlterColumnSetNotNullConvertor, SqliteAlterTableAlterColumnSetAutoincrementConvertor, SqliteAlterTableAlterColumnDropAutoincrementConvertor, PgAlterTableAlterColumnDropNotNullConvertor, SqliteAlterTableAlterColumnDropNotNullConvertor, PgCreateForeignKeyConvertor, SqliteCreateForeignKeyConvertor, MySqlCreateForeignKeyConvertor, PgAlterForeignKeyConvertor, SqliteAlterForeignKeyConvertor, PgDeleteForeignKeyConvertor, SqliteDeleteForeignKeyConvertor, MySqlDeleteForeignKeyConvertor, CreatePgIndexConvertor, CreateMySqlIndexConvertor, CreateSqliteIndexConvertor, PgDropIndexConvertor, PgCreateSchemaConvertor, PgRenameSchemaConvertor, PgDropSchemaConvertor, PgAlterTableSetSchemaConvertor, PgAlterTableSetNewSchemaConvertor, PgAlterTableRemoveFromSchemaConvertor, SqliteDropIndexConvertor, MySqlDropIndexConvertor, convertors, fromJson;
11465
+ function fromJson(statements, dialect7, action, driver2, json2) {
11466
+ const result = statements.flatMap((statement) => {
11467
+ const filtered = convertors.filter((it) => {
11468
+ return it.can(statement, dialect7, driver2);
11469
+ });
11470
+ const convertor = filtered.length === 1 ? filtered[0] : void 0;
11471
+ if (!convertor) {
11472
+ return "";
11473
+ }
11474
+ return convertor.convert(statement, json2, action);
11475
+ }).filter((it) => it !== "");
11476
+ return result;
11477
+ }
11478
+ var pgNativeTypes, isPgNativeType, Convertor, PgCreateTableConvertor, MySqlCreateTableConvertor, SQLiteCreateTableConvertor, PgAlterTableAlterColumnSetGenerated, PgAlterTableAlterColumnDropGenerated, PgAlterTableAlterColumnAlterGenerated, PgAlterTableAddUniqueConstraintConvertor, PgAlterTableDropUniqueConstraintConvertor, MySQLAlterTableAddUniqueConstraintConvertor, MySQLAlterTableDropUniqueConstraintConvertor, SQLiteAlterTableAddUniqueConstraintConvertor, SQLiteAlterTableDropUniqueConstraintConvertor, CreatePgSequenceConvertor, DropPgSequenceConvertor, RenamePgSequenceConvertor, MovePgSequenceConvertor, AlterPgSequenceConvertor, CreateTypeEnumConvertor, AlterTypeAddValueConvertor, PgDropTableConvertor, MySQLDropTableConvertor, SQLiteDropTableConvertor, PgRenameTableConvertor, SqliteRenameTableConvertor, MySqlRenameTableConvertor, PgAlterTableRenameColumnConvertor, MySqlAlterTableRenameColumnConvertor, SQLiteAlterTableRenameColumnConvertor, PgAlterTableDropColumnConvertor, MySqlAlterTableDropColumnConvertor, SQLiteAlterTableDropColumnConvertor, PgAlterTableAddColumnConvertor, MySqlAlterTableAddColumnConvertor, SQLiteAlterTableAddColumnConvertor, PgAlterTableAlterColumnSetTypeConvertor, SQLiteAlterTableAlterColumnSetTypeConvertor, PgAlterTableAlterColumnSetDefaultConvertor, SqliteAlterTableAlterColumnSetDefaultConvertor, PgAlterTableAlterColumnDropDefaultConvertor, PgAlterTableAlterColumnDropGeneratedConvertor, PgAlterTableAlterColumnSetExpressionConvertor, PgAlterTableAlterColumnAlterrGeneratedConvertor, SqliteAlterTableAlterColumnDropGeneratedConvertor, SqliteAlterTableAlterColumnSetExpressionConvertor, SqliteAlterTableAlterColumnAlterGeneratedConvertor, MySqlAlterTableAlterColumnAlterrGeneratedConvertor, MySqlAlterTableAddPk, MySqlAlterTableDropPk, LibSQLModifyColumn, MySqlModifyColumn, SqliteAlterTableAlterColumnDropDefaultConvertor, PgAlterTableCreateCompositePrimaryKeyConvertor, PgAlterTableDeleteCompositePrimaryKeyConvertor, PgAlterTableAlterCompositePrimaryKeyConvertor, MySqlAlterTableCreateCompositePrimaryKeyConvertor, MySqlAlterTableDeleteCompositePrimaryKeyConvertor, MySqlAlterTableAlterCompositePrimaryKeyConvertor, SqliteAlterTableCreateCompositePrimaryKeyConvertor, SqliteAlterTableDeleteCompositePrimaryKeyConvertor, SqliteAlterTableAlterCompositePrimaryKeyConvertor, PgAlterTableAlterColumnSetPrimaryKeyConvertor, PgAlterTableAlterColumnDropPrimaryKeyConvertor, PgAlterTableAlterColumnSetNotNullConvertor, SqliteAlterTableAlterColumnSetNotNullConvertor, SqliteAlterTableAlterColumnSetAutoincrementConvertor, SqliteAlterTableAlterColumnDropAutoincrementConvertor, PgAlterTableAlterColumnDropNotNullConvertor, SqliteAlterTableAlterColumnDropNotNullConvertor, PgCreateForeignKeyConvertor, SqliteCreateForeignKeyConvertor, LibSQLCreateForeignKeyConvertor, MySqlCreateForeignKeyConvertor, PgAlterForeignKeyConvertor, SqliteAlterForeignKeyConvertor, PgDeleteForeignKeyConvertor, SqliteDeleteForeignKeyConvertor, LibSQLDeleteForeignKeyConvertor, MySqlDeleteForeignKeyConvertor, CreatePgIndexConvertor, CreateMySqlIndexConvertor, CreateSqliteIndexConvertor, PgDropIndexConvertor, PgCreateSchemaConvertor, PgRenameSchemaConvertor, PgDropSchemaConvertor, PgAlterTableSetSchemaConvertor, PgAlterTableSetNewSchemaConvertor, PgAlterTableRemoveFromSchemaConvertor, SqliteDropIndexConvertor, MySqlDropIndexConvertor, SQLiteRecreateTableConvertor, LibSQLRecreateTableConvertor, convertors;
11456
11479
  var init_sqlgenerator = __esm({
11457
11480
  "src/sqlgenerator.ts"() {
11458
11481
  "use strict";
@@ -11968,7 +11991,7 @@ var init_sqlgenerator = __esm({
11968
11991
  }
11969
11992
  convert(statement) {
11970
11993
  const { tableName, oldColumnName, newColumnName } = statement;
11971
- return `ALTER TABLE \`${tableName}\` RENAME COLUMN \`${oldColumnName}\` TO \`${newColumnName}\`;`;
11994
+ return `ALTER TABLE \`${tableName}\` RENAME COLUMN "${oldColumnName}" TO "${newColumnName}";`;
11972
11995
  }
11973
11996
  };
11974
11997
  PgAlterTableDropColumnConvertor = class extends Convertor {
@@ -12070,8 +12093,8 @@ var init_sqlgenerator = __esm({
12070
12093
  }
12071
12094
  };
12072
12095
  SQLiteAlterTableAlterColumnSetTypeConvertor = class extends Convertor {
12073
- can(statement, dialect7) {
12074
- return statement.type === "alter_table_alter_column_set_type" && dialect7 === "sqlite";
12096
+ can(statement, dialect7, driver2) {
12097
+ return statement.type === "alter_table_alter_column_set_type" && dialect7 === "sqlite" && !driver2;
12075
12098
  }
12076
12099
  convert(statement) {
12077
12100
  return `/*
@@ -12382,6 +12405,46 @@ var init_sqlgenerator = __esm({
12382
12405
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY`;
12383
12406
  }
12384
12407
  };
12408
+ LibSQLModifyColumn = class extends Convertor {
12409
+ can(statement, dialect7, driver2) {
12410
+ return (statement.type === "alter_table_alter_column_set_type" || statement.type === "alter_table_alter_column_drop_notnull" || statement.type === "alter_table_alter_column_set_notnull" || statement.type === "alter_table_alter_column_set_default" || statement.type === "alter_table_alter_column_drop_default") && dialect7 === "sqlite" && driver2 === "turso";
12411
+ }
12412
+ convert(statement, json2) {
12413
+ const { tableName, columnName } = statement;
12414
+ let columnType = ``;
12415
+ let columnDefault = "";
12416
+ let columnNotNull = "";
12417
+ switch (statement.type) {
12418
+ case "alter_table_alter_column_set_type":
12419
+ columnType = ` ${statement.newDataType}`;
12420
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12421
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12422
+ break;
12423
+ case "alter_table_alter_column_drop_notnull":
12424
+ columnType = ` ${statement.newDataType}`;
12425
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12426
+ columnNotNull = "";
12427
+ break;
12428
+ case "alter_table_alter_column_set_notnull":
12429
+ columnType = ` ${statement.newDataType}`;
12430
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12431
+ columnNotNull = ` NOT NULL`;
12432
+ break;
12433
+ case "alter_table_alter_column_set_default":
12434
+ columnType = ` ${statement.newDataType}`;
12435
+ columnDefault = ` DEFAULT ${statement.newDefaultValue}`;
12436
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12437
+ break;
12438
+ case "alter_table_alter_column_drop_default":
12439
+ columnType = ` ${statement.newDataType}`;
12440
+ columnDefault = "";
12441
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12442
+ break;
12443
+ }
12444
+ columnDefault = columnDefault instanceof Date ? columnDefault.toISOString() : columnDefault;
12445
+ return `ALTER TABLE \`${tableName}\` ALTER COLUMN "${columnName}" TO "${columnName}"${columnType}${columnNotNull}${columnDefault};`;
12446
+ }
12447
+ };
12385
12448
  MySqlModifyColumn = class extends Convertor {
12386
12449
  can(statement, dialect7) {
12387
12450
  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" || statement.type === "alter_table_alter_column_set_generated" || statement.type === "alter_table_alter_column_drop_generated") && dialect7 === "mysql";
@@ -12722,8 +12785,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12722
12785
  }
12723
12786
  };
12724
12787
  SqliteAlterTableAlterColumnSetNotNullConvertor = class extends Convertor {
12725
- can(statement, dialect7) {
12726
- return statement.type === "alter_table_alter_column_set_notnull" && dialect7 === "sqlite";
12788
+ can(statement, dialect7, driver2) {
12789
+ return statement.type === "alter_table_alter_column_set_notnull" && dialect7 === "sqlite" && !driver2;
12727
12790
  }
12728
12791
  convert(statement) {
12729
12792
  return `/*
@@ -12737,8 +12800,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12737
12800
  }
12738
12801
  };
12739
12802
  SqliteAlterTableAlterColumnSetAutoincrementConvertor = class extends Convertor {
12740
- can(statement, dialect7) {
12741
- return statement.type === "alter_table_alter_column_set_autoincrement" && dialect7 === "sqlite";
12803
+ can(statement, dialect7, driver2) {
12804
+ return statement.type === "alter_table_alter_column_set_autoincrement" && dialect7 === "sqlite" && !driver2;
12742
12805
  }
12743
12806
  convert(statement) {
12744
12807
  return `/*
@@ -12752,8 +12815,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12752
12815
  }
12753
12816
  };
12754
12817
  SqliteAlterTableAlterColumnDropAutoincrementConvertor = class extends Convertor {
12755
- can(statement, dialect7) {
12756
- return statement.type === "alter_table_alter_column_drop_autoincrement" && dialect7 === "sqlite";
12818
+ can(statement, dialect7, driver2) {
12819
+ return statement.type === "alter_table_alter_column_drop_autoincrement" && dialect7 === "sqlite" && !driver2;
12757
12820
  }
12758
12821
  convert(statement) {
12759
12822
  return `/*
@@ -12777,8 +12840,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12777
12840
  }
12778
12841
  };
12779
12842
  SqliteAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12780
- can(statement, dialect7) {
12781
- return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "sqlite";
12843
+ can(statement, dialect7, driver2) {
12844
+ return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "sqlite" && !driver2;
12782
12845
  }
12783
12846
  convert(statement) {
12784
12847
  return `/*
@@ -12822,8 +12885,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12822
12885
  }
12823
12886
  };
12824
12887
  SqliteCreateForeignKeyConvertor = class extends Convertor {
12825
- can(statement, dialect7) {
12826
- return statement.type === "create_reference" && dialect7 === "sqlite";
12888
+ can(statement, dialect7, driver2) {
12889
+ return statement.type === "create_reference" && dialect7 === "sqlite" && !driver2;
12827
12890
  }
12828
12891
  convert(statement) {
12829
12892
  return `/*
@@ -12835,6 +12898,32 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12835
12898
  */`;
12836
12899
  }
12837
12900
  };
12901
+ LibSQLCreateForeignKeyConvertor = class extends Convertor {
12902
+ can(statement, dialect7, driver2) {
12903
+ return statement.type === "create_reference" && dialect7 === "sqlite" && driver2 === "turso";
12904
+ }
12905
+ convert(statement, json2, action) {
12906
+ const { columnsFrom, columnsTo, tableFrom, onDelete, onUpdate, tableTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
12907
+ const { columnDefault, columnNotNull, columnType, isMulticolumn } = statement;
12908
+ if (isMulticolumn) {
12909
+ return `/*
12910
+ LibSQL does not support "Creating foreign key on multiple columns" out of the box, we do not generate automatic migration for that, so it has to be done manually
12911
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12912
+ https://www.sqlite.org/lang_altertable.html
12913
+
12914
+ Due to that we don't generate migration automatically and it has to be done manually
12915
+ */`;
12916
+ }
12917
+ const onDeleteStatement = onDelete ? ` ON DELETE ${onDelete}` : "";
12918
+ const onUpdateStatement = onUpdate ? ` ON UPDATE ${onUpdate}` : "";
12919
+ const columnsDefaultValue = columnDefault ? ` DEFAULT ${columnDefault}` : "";
12920
+ const columnNotNullValue = columnNotNull ? ` NOT NULL` : "";
12921
+ const columnTypeValue = columnType ? ` ${columnType}` : "";
12922
+ const columnFrom = columnsFrom[0];
12923
+ const columnTo = columnsTo[0];
12924
+ return `ALTER TABLE \`${tableFrom}\` ALTER COLUMN "${columnFrom}" TO "${columnFrom}"${columnTypeValue}${columnNotNullValue}${columnsDefaultValue} REFERENCES ${tableTo}(${columnTo})${onDeleteStatement}${onUpdateStatement};`;
12925
+ }
12926
+ };
12838
12927
  MySqlCreateForeignKeyConvertor = class extends Convertor {
12839
12928
  can(statement, dialect7) {
12840
12929
  return statement.type === "create_reference" && dialect7 === "mysql";
@@ -12908,8 +12997,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12908
12997
  }
12909
12998
  };
12910
12999
  SqliteDeleteForeignKeyConvertor = class extends Convertor {
12911
- can(statement, dialect7) {
12912
- return statement.type === "delete_reference" && dialect7 === "sqlite";
13000
+ can(statement, dialect7, driver2) {
13001
+ return statement.type === "delete_reference" && dialect7 === "sqlite" && !driver2;
12913
13002
  }
12914
13003
  convert(statement) {
12915
13004
  return `/*
@@ -12921,6 +13010,29 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12921
13010
  */`;
12922
13011
  }
12923
13012
  };
13013
+ LibSQLDeleteForeignKeyConvertor = class extends Convertor {
13014
+ can(statement, dialect7, driver2) {
13015
+ return statement.type === "delete_reference" && dialect7 === "sqlite" && driver2 === "turso";
13016
+ }
13017
+ convert(statement, json2, action) {
13018
+ const { columnsFrom, tableFrom } = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
13019
+ const { columnDefault, columnNotNull, columnType, isMulticolumn } = statement;
13020
+ if (isMulticolumn) {
13021
+ return `/*
13022
+ LibSQL does not support "Creating foreign key on multiple columns" out of the box, we do not generate automatic migration for that, so it has to be done manually
13023
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
13024
+ https://www.sqlite.org/lang_altertable.html
13025
+
13026
+ Due to that we don't generate migration automatically and it has to be done manually
13027
+ */`;
13028
+ }
13029
+ const columnsDefaultValue = columnDefault ? ` DEFAULT ${columnDefault}` : "";
13030
+ const columnNotNullValue = columnNotNull ? ` NOT NULL` : "";
13031
+ const columnTypeValue = columnType ? ` ${columnType}` : "";
13032
+ const columnFrom = columnsFrom[0];
13033
+ return `ALTER TABLE \`${tableFrom}\` ALTER COLUMN "${columnFrom}" TO "${columnFrom}"${columnTypeValue}${columnNotNullValue}${columnsDefaultValue};`;
13034
+ }
13035
+ };
12924
13036
  MySqlDeleteForeignKeyConvertor = class extends Convertor {
12925
13037
  can(statement, dialect7) {
12926
13038
  return statement.type === "delete_reference" && dialect7 === "mysql";
@@ -13084,10 +13196,90 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13084
13196
  return `DROP INDEX \`${name2}\` ON \`${statement.tableName}\`;`;
13085
13197
  }
13086
13198
  };
13199
+ SQLiteRecreateTableConvertor = class extends Convertor {
13200
+ can(statement, dialect7, driver2) {
13201
+ return statement.type === "recreate_table" && dialect7 === "sqlite" && !driver2;
13202
+ }
13203
+ convert(statement) {
13204
+ const { tableName, columns, compositePKs, referenceData } = statement;
13205
+ const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
13206
+ const sqlStatements = [];
13207
+ sqlStatements.push(
13208
+ new SqliteRenameTableConvertor().convert({
13209
+ fromSchema: "",
13210
+ tableNameFrom: tableName,
13211
+ tableNameTo: `__old__generate_${tableName}`,
13212
+ toSchema: "",
13213
+ type: "rename_table"
13214
+ })
13215
+ );
13216
+ sqlStatements.push(
13217
+ new SQLiteCreateTableConvertor().convert({
13218
+ type: "sqlite_create_table",
13219
+ tableName,
13220
+ columns,
13221
+ referenceData,
13222
+ compositePKs
13223
+ })
13224
+ );
13225
+ sqlStatements.push(
13226
+ `INSERT INTO \`${tableName}\`(${columnNames}) SELECT ${columnNames} FROM \`__old__generate_${tableName}\`;`
13227
+ );
13228
+ sqlStatements.push(
13229
+ new SQLiteDropTableConvertor().convert({
13230
+ type: "drop_table",
13231
+ tableName: `__old__generate_${tableName}`,
13232
+ schema: ""
13233
+ })
13234
+ );
13235
+ return sqlStatements;
13236
+ }
13237
+ };
13238
+ LibSQLRecreateTableConvertor = class extends Convertor {
13239
+ can(statement, dialect7, driver2) {
13240
+ return statement.type === "recreate_table" && dialect7 === "sqlite" && driver2 === "turso";
13241
+ }
13242
+ convert(statement) {
13243
+ const { tableName, columns, compositePKs, referenceData } = statement;
13244
+ const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
13245
+ const sqlStatements = [];
13246
+ sqlStatements.push(
13247
+ new SqliteRenameTableConvertor().convert({
13248
+ fromSchema: "",
13249
+ tableNameFrom: tableName,
13250
+ tableNameTo: `__old__generate_${tableName}`,
13251
+ toSchema: "",
13252
+ type: "rename_table"
13253
+ })
13254
+ );
13255
+ sqlStatements.push(
13256
+ new SQLiteCreateTableConvertor().convert({
13257
+ type: "sqlite_create_table",
13258
+ tableName,
13259
+ columns,
13260
+ referenceData,
13261
+ compositePKs
13262
+ })
13263
+ );
13264
+ sqlStatements.push(
13265
+ `INSERT INTO \`${tableName}\`(${columnNames}) SELECT ${columnNames} FROM \`__old__generate_${tableName}\`;`
13266
+ );
13267
+ sqlStatements.push(
13268
+ new SQLiteDropTableConvertor().convert({
13269
+ type: "drop_table",
13270
+ tableName: `__old__generate_${tableName}`,
13271
+ schema: ""
13272
+ })
13273
+ );
13274
+ return sqlStatements;
13275
+ }
13276
+ };
13087
13277
  convertors = [];
13088
13278
  convertors.push(new PgCreateTableConvertor());
13089
13279
  convertors.push(new MySqlCreateTableConvertor());
13090
13280
  convertors.push(new SQLiteCreateTableConvertor());
13281
+ convertors.push(new SQLiteRecreateTableConvertor());
13282
+ convertors.push(new LibSQLRecreateTableConvertor());
13091
13283
  convertors.push(new CreateTypeEnumConvertor());
13092
13284
  convertors.push(new CreatePgSequenceConvertor());
13093
13285
  convertors.push(new DropPgSequenceConvertor());
@@ -13135,6 +13327,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13135
13327
  convertors.push(new SqliteAlterTableAlterColumnAlterGeneratedConvertor());
13136
13328
  convertors.push(new SqliteAlterTableAlterColumnSetExpressionConvertor());
13137
13329
  convertors.push(new MySqlModifyColumn());
13330
+ convertors.push(new LibSQLModifyColumn());
13138
13331
  convertors.push(new PgCreateForeignKeyConvertor());
13139
13332
  convertors.push(new MySqlCreateForeignKeyConvertor());
13140
13333
  convertors.push(new PgAlterForeignKeyConvertor());
@@ -13149,7 +13342,9 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13149
13342
  convertors.push(new SQLiteAlterTableAlterColumnSetTypeConvertor());
13150
13343
  convertors.push(new SqliteAlterForeignKeyConvertor());
13151
13344
  convertors.push(new SqliteDeleteForeignKeyConvertor());
13345
+ convertors.push(new LibSQLDeleteForeignKeyConvertor());
13152
13346
  convertors.push(new SqliteCreateForeignKeyConvertor());
13347
+ convertors.push(new LibSQLCreateForeignKeyConvertor());
13153
13348
  convertors.push(new SQLiteAlterTableAddUniqueConstraintConvertor());
13154
13349
  convertors.push(new SQLiteAlterTableDropUniqueConstraintConvertor());
13155
13350
  convertors.push(new PgAlterTableAlterColumnDropGenerated());
@@ -13172,19 +13367,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13172
13367
  convertors.push(new MySqlAlterTableCreateCompositePrimaryKeyConvertor());
13173
13368
  convertors.push(new MySqlAlterTableAddPk());
13174
13369
  convertors.push(new MySqlAlterTableAlterCompositePrimaryKeyConvertor());
13175
- fromJson = (statements, dialect7) => {
13176
- const result = statements.flatMap((statement) => {
13177
- const filtered = convertors.filter((it) => {
13178
- return it.can(statement, dialect7);
13179
- });
13180
- const convertor = filtered.length === 1 ? filtered[0] : void 0;
13181
- if (!convertor) {
13182
- return "";
13183
- }
13184
- return convertor.convert(statement);
13185
- }).filter((it) => it !== "");
13186
- return result;
13187
- };
13188
13370
  https: `
13189
13371
  create table users (
13190
13372
  id int,
@@ -13211,12 +13393,237 @@ drop type __venum;
13211
13393
  }
13212
13394
  });
13213
13395
 
13396
+ // src/cli/commands/sqlitePushUtils.ts
13397
+ var _moveDataStatements, getOldTableName, getNewTableName, logSuggestionsAndReturn;
13398
+ var init_sqlitePushUtils = __esm({
13399
+ "src/cli/commands/sqlitePushUtils.ts"() {
13400
+ "use strict";
13401
+ init_source();
13402
+ init_sqliteSchema();
13403
+ init_sqlgenerator();
13404
+ init_utils();
13405
+ _moveDataStatements = (tableName, json, dataLoss = false) => {
13406
+ const statements = [];
13407
+ statements.push(
13408
+ new SqliteRenameTableConvertor().convert({
13409
+ type: "rename_table",
13410
+ tableNameFrom: tableName,
13411
+ tableNameTo: `__old_push_${tableName}`,
13412
+ fromSchema: "",
13413
+ toSchema: ""
13414
+ })
13415
+ );
13416
+ const tableColumns = Object.values(json.tables[tableName].columns);
13417
+ const referenceData = Object.values(json.tables[tableName].foreignKeys);
13418
+ const compositePKs = Object.values(
13419
+ json.tables[tableName].compositePrimaryKeys
13420
+ ).map((it) => SQLiteSquasher.unsquashPK(it));
13421
+ const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
13422
+ statements.push(
13423
+ new SQLiteCreateTableConvertor().convert({
13424
+ type: "sqlite_create_table",
13425
+ tableName,
13426
+ columns: tableColumns,
13427
+ referenceData: fks,
13428
+ compositePKs
13429
+ })
13430
+ );
13431
+ if (!dataLoss) {
13432
+ const columns = Object.keys(json.tables[tableName].columns).map(
13433
+ (c) => `"${c}"`
13434
+ );
13435
+ statements.push(
13436
+ `INSERT INTO \`${tableName}\`(${columns.join(
13437
+ ", "
13438
+ )}) SELECT (${columns.join(", ")}) FROM \`__old_push_${tableName}\`;`
13439
+ );
13440
+ }
13441
+ statements.push(
13442
+ new SQLiteDropTableConvertor().convert({
13443
+ type: "drop_table",
13444
+ tableName: `__old_push_${tableName}`,
13445
+ schema: ""
13446
+ })
13447
+ );
13448
+ for (const idx of Object.values(json.tables[tableName].indexes)) {
13449
+ statements.push(
13450
+ new CreateSqliteIndexConvertor().convert({
13451
+ type: "create_index",
13452
+ tableName,
13453
+ schema: "",
13454
+ data: idx
13455
+ })
13456
+ );
13457
+ }
13458
+ return statements;
13459
+ };
13460
+ getOldTableName = (tableName, meta) => {
13461
+ for (const key of Object.keys(meta.tables)) {
13462
+ const value = meta.tables[key];
13463
+ if (`"${tableName}"` === value) {
13464
+ return key.substring(1, key.length - 1);
13465
+ }
13466
+ }
13467
+ return tableName;
13468
+ };
13469
+ getNewTableName = (tableName, meta) => {
13470
+ if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
13471
+ return meta.tables[`"${tableName}"`].substring(
13472
+ 1,
13473
+ meta.tables[`"${tableName}"`].length - 1
13474
+ );
13475
+ }
13476
+ return tableName;
13477
+ };
13478
+ logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
13479
+ let shouldAskForApprove = false;
13480
+ const statementsToExecute = [];
13481
+ const infoToPrint = [];
13482
+ const tablesToRemove = [];
13483
+ const columnsToRemove = [];
13484
+ const schemasToRemove = [];
13485
+ const tablesToTruncate = [];
13486
+ for (const statement of statements) {
13487
+ if (statement.type === "drop_table") {
13488
+ const res = await connection.query(
13489
+ `select count(*) as count from \`${statement.tableName}\``
13490
+ );
13491
+ const count2 = Number(res[0].count);
13492
+ if (count2 > 0) {
13493
+ infoToPrint.push(
13494
+ `\xB7 You're about to delete ${source_default.underline(
13495
+ statement.tableName
13496
+ )} table with ${count2} items`
13497
+ );
13498
+ tablesToRemove.push(statement.tableName);
13499
+ shouldAskForApprove = true;
13500
+ }
13501
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13502
+ statementsToExecute.push(
13503
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13504
+ );
13505
+ } else if (statement.type === "alter_table_drop_column") {
13506
+ const tableName = statement.tableName;
13507
+ const columnName = statement.columnName;
13508
+ const res = await connection.query(
13509
+ `select count(\`${tableName}\`.\`${columnName}\`) as count from \`${tableName}\``
13510
+ );
13511
+ const count2 = Number(res[0].count);
13512
+ if (count2 > 0) {
13513
+ infoToPrint.push(
13514
+ `\xB7 You're about to delete ${source_default.underline(
13515
+ columnName
13516
+ )} column in ${tableName} table with ${count2} items`
13517
+ );
13518
+ columnsToRemove.push(`${tableName}_${statement.columnName}`);
13519
+ shouldAskForApprove = true;
13520
+ }
13521
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13522
+ statementsToExecute.push(
13523
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13524
+ );
13525
+ } else if (statement.type === "sqlite_alter_table_add_column" && (statement.column.notNull && !statement.column.default)) {
13526
+ const tableName = statement.tableName;
13527
+ const columnName = statement.column.name;
13528
+ const res = await connection.query(
13529
+ `select count(*) as count from \`${tableName}\``
13530
+ );
13531
+ const count2 = Number(res[0].count);
13532
+ if (count2 > 0) {
13533
+ infoToPrint.push(
13534
+ `\xB7 You're about to add not-null ${source_default.underline(
13535
+ columnName
13536
+ )} column without default value, which contains ${count2} items`
13537
+ );
13538
+ tablesToTruncate.push(tableName);
13539
+ statementsToExecute.push(`delete from ${tableName};`);
13540
+ shouldAskForApprove = true;
13541
+ }
13542
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13543
+ statementsToExecute.push(
13544
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13545
+ );
13546
+ } else if (statement.type === "recreate_table") {
13547
+ const tableName = statement.tableName;
13548
+ const oldTableName = getOldTableName(tableName, meta);
13549
+ const prevColumnNames = Object.keys(json1.tables[oldTableName].columns);
13550
+ const currentColumnNames = Object.keys(json2.tables[tableName].columns);
13551
+ const { removedColumns, addedColumns } = findAddedAndRemoved(
13552
+ prevColumnNames,
13553
+ currentColumnNames
13554
+ );
13555
+ if (removedColumns.length) {
13556
+ for (const removedColumn of removedColumns) {
13557
+ const res = await connection.query(
13558
+ `select count(\`${tableName}\`.\`${removedColumn}\`) as count from \`${tableName}\``
13559
+ );
13560
+ const count2 = Number(res[0].count);
13561
+ if (count2 > 0) {
13562
+ infoToPrint.push(
13563
+ `\xB7 You're about to delete ${source_default.underline(
13564
+ removedColumn
13565
+ )} column in ${tableName} table with ${count2} items`
13566
+ );
13567
+ columnsToRemove.push(removedColumn);
13568
+ shouldAskForApprove = true;
13569
+ }
13570
+ }
13571
+ }
13572
+ if (addedColumns.length) {
13573
+ for (const addedColumn of addedColumns) {
13574
+ const [res] = await connection.query(
13575
+ `select count(*) as count from \`${tableName}\``
13576
+ );
13577
+ const columnConf = json2.tables[tableName].columns[addedColumn];
13578
+ const count2 = Number(res.count);
13579
+ if (count2 > 0 && columnConf.notNull && !columnConf.default) {
13580
+ infoToPrint.push(
13581
+ `\xB7 You're about to add not-null ${source_default.underline(
13582
+ addedColumn
13583
+ )} column without default value to table, which contains ${count2} items`
13584
+ );
13585
+ shouldAskForApprove = true;
13586
+ tablesToTruncate.push(tableName);
13587
+ }
13588
+ }
13589
+ }
13590
+ statementsToExecute.push(..._moveDataStatements(tableName, json2));
13591
+ const tablesReferencingCurrent = [];
13592
+ for (const table4 of Object.values(json2.tables)) {
13593
+ const tablesRefs = Object.values(json2.tables[table4.name].foreignKeys).filter((t) => SQLiteSquasher.unsquashPushFK(t).tableTo === tableName).map((it) => SQLiteSquasher.unsquashPushFK(it).tableFrom);
13594
+ tablesReferencingCurrent.push(...tablesRefs);
13595
+ }
13596
+ const uniqueTableRefs = [...new Set(tablesReferencingCurrent)];
13597
+ for (const table4 of uniqueTableRefs) {
13598
+ statementsToExecute.push(..._moveDataStatements(table4, json2));
13599
+ }
13600
+ } else {
13601
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13602
+ statementsToExecute.push(
13603
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13604
+ );
13605
+ }
13606
+ }
13607
+ return {
13608
+ statementsToExecute,
13609
+ shouldAskForApprove,
13610
+ infoToPrint,
13611
+ columnsToRemove: [...new Set(columnsToRemove)],
13612
+ schemasToRemove: [...new Set(schemasToRemove)],
13613
+ tablesToTruncate: [...new Set(tablesToTruncate)],
13614
+ tablesToRemove: [...new Set(tablesToRemove)]
13615
+ };
13616
+ };
13617
+ }
13618
+ });
13619
+
13214
13620
  // src/jsonStatements.ts
13215
- var preparePgCreateTableJson, prepareMySqlCreateTableJson, prepareSQLiteCreateTable, prepareDropTableJson, prepareRenameTableJson, prepareCreateEnumJson, prepareAddValuesToEnumJson, prepareDropEnumJson, prepareMoveEnumJson, prepareRenameEnumJson, prepareCreateSequenceJson, prepareAlterSequenceJson, prepareDropSequenceJson, prepareMoveSequenceJson, prepareRenameSequenceJson, prepareCreateSchemasJson, prepareRenameSchemasJson, prepareDeleteSchemasJson, prepareRenameColumns, _prepareDropColumns, _prepareAddColumns, _prepareSqliteAddColumns, prepareAlterColumnsMysql, preparePgAlterColumns, prepareSqliteAlterColumns, preparePgCreateIndexesJson, prepareCreateIndexesJson, prepareCreateReferencesJson, prepareDropReferencesJson, prepareAlterReferencesJson, prepareDropIndexesJson, prepareAddCompositePrimaryKeySqlite, prepareDeleteCompositePrimaryKeySqlite, prepareAlterCompositePrimaryKeySqlite, prepareAddCompositePrimaryKeyPg, prepareDeleteCompositePrimaryKeyPg, prepareAlterCompositePrimaryKeyPg, prepareAddUniqueConstraintPg, prepareDeleteUniqueConstraintPg, prepareAddCompositePrimaryKeyMySql, prepareDeleteCompositePrimaryKeyMySql, prepareAlterCompositePrimaryKeyMySql;
13621
+ var preparePgCreateTableJson, prepareMySqlCreateTableJson, prepareSQLiteCreateTable, prepareDropTableJson, prepareRenameTableJson, prepareCreateEnumJson, prepareAddValuesToEnumJson, prepareDropEnumJson, prepareMoveEnumJson, prepareRenameEnumJson, prepareCreateSequenceJson, prepareAlterSequenceJson, prepareDropSequenceJson, prepareMoveSequenceJson, prepareRenameSequenceJson, prepareCreateSchemasJson, prepareRenameSchemasJson, prepareDeleteSchemasJson, prepareRenameColumns, _prepareDropColumns, _prepareAddColumns, _prepareSqliteAddColumns, prepareAlterColumnsMysql, preparePgAlterColumns, prepareSqliteAlterColumns, preparePgCreateIndexesJson, prepareCreateIndexesJson, prepareCreateReferencesJson, prepareLibSQLCreateReferencesJson, prepareDropReferencesJson, prepareLibSQLDropReferencesJson, prepareAlterReferencesJson, prepareDropIndexesJson, prepareAddCompositePrimaryKeySqlite, prepareDeleteCompositePrimaryKeySqlite, prepareAlterCompositePrimaryKeySqlite, prepareAddCompositePrimaryKeyPg, prepareDeleteCompositePrimaryKeyPg, prepareAlterCompositePrimaryKeyPg, prepareAddUniqueConstraintPg, prepareDeleteUniqueConstraintPg, prepareAddCompositePrimaryKeyMySql, prepareDeleteCompositePrimaryKeyMySql, prepareAlterCompositePrimaryKeyMySql;
13216
13622
  var init_jsonStatements = __esm({
13217
13623
  "src/jsonStatements.ts"() {
13218
13624
  "use strict";
13219
13625
  init_source();
13626
+ init_sqlitePushUtils();
13220
13627
  init_views();
13221
13628
  init_mysqlSchema();
13222
13629
  init_pgSchema();
@@ -14000,6 +14407,49 @@ var init_jsonStatements = __esm({
14000
14407
  const columnPk = json2.tables[tableName].columns[columnName].primaryKey;
14001
14408
  const columnGenerated = json2.tables[tableName].columns[columnName].generated;
14002
14409
  const compositePk = json2.tables[tableName].compositePrimaryKeys[`${tableName}_${columnName}`];
14410
+ if (column4.autoincrement?.type === "added") {
14411
+ statements.push({
14412
+ type: "alter_table_alter_column_set_autoincrement",
14413
+ tableName,
14414
+ columnName,
14415
+ schema: schema4,
14416
+ newDataType: columnType,
14417
+ columnDefault,
14418
+ columnOnUpdate,
14419
+ columnNotNull,
14420
+ columnAutoIncrement,
14421
+ columnPk
14422
+ });
14423
+ }
14424
+ if (column4.autoincrement?.type === "changed") {
14425
+ const type = column4.autoincrement.new ? "alter_table_alter_column_set_autoincrement" : "alter_table_alter_column_drop_autoincrement";
14426
+ statements.push({
14427
+ type,
14428
+ tableName,
14429
+ columnName,
14430
+ schema: schema4,
14431
+ newDataType: columnType,
14432
+ columnDefault,
14433
+ columnOnUpdate,
14434
+ columnNotNull,
14435
+ columnAutoIncrement,
14436
+ columnPk
14437
+ });
14438
+ }
14439
+ if (column4.autoincrement?.type === "deleted") {
14440
+ statements.push({
14441
+ type: "alter_table_alter_column_drop_autoincrement",
14442
+ tableName,
14443
+ columnName,
14444
+ schema: schema4,
14445
+ newDataType: columnType,
14446
+ columnDefault,
14447
+ columnOnUpdate,
14448
+ columnNotNull,
14449
+ columnAutoIncrement,
14450
+ columnPk
14451
+ });
14452
+ }
14003
14453
  if (typeof column4.name !== "string") {
14004
14454
  statements.push({
14005
14455
  type: "alter_table_rename_column",
@@ -14263,33 +14713,111 @@ var init_jsonStatements = __esm({
14263
14713
  };
14264
14714
  });
14265
14715
  };
14266
- prepareDropReferencesJson = (tableName, schema4, foreignKeys) => {
14716
+ prepareLibSQLCreateReferencesJson = (tableName, schema4, foreignKeys, json2, action) => {
14267
14717
  return Object.values(foreignKeys).map((fkData) => {
14718
+ const { columnsFrom, tableFrom, columnsTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14719
+ let isMulticolumn = false;
14720
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
14721
+ isMulticolumn = true;
14722
+ return {
14723
+ type: "create_reference",
14724
+ tableName,
14725
+ data: fkData,
14726
+ schema: schema4,
14727
+ isMulticolumn
14728
+ };
14729
+ }
14730
+ const columnFrom = columnsFrom[0];
14731
+ const {
14732
+ notNull: columnNotNull,
14733
+ default: columnDefault,
14734
+ type: columnType
14735
+ } = json2.tables[tableFrom].columns[columnFrom];
14268
14736
  return {
14269
- type: "delete_reference",
14737
+ type: "create_reference",
14270
14738
  tableName,
14271
14739
  data: fkData,
14272
- schema: schema4
14740
+ schema: schema4,
14741
+ columnNotNull,
14742
+ columnDefault,
14743
+ columnType
14273
14744
  };
14274
14745
  });
14275
14746
  };
14276
- prepareAlterReferencesJson = (tableName, schema4, foreignKeys) => {
14277
- const stmts = [];
14278
- Object.values(foreignKeys).map((val) => {
14279
- stmts.push({
14747
+ prepareDropReferencesJson = (tableName, schema4, foreignKeys) => {
14748
+ return Object.values(foreignKeys).map((fkData) => {
14749
+ return {
14280
14750
  type: "delete_reference",
14281
14751
  tableName,
14282
- schema: schema4,
14283
- data: val.__old
14284
- });
14285
- stmts.push({
14286
- type: "create_reference",
14287
- tableName,
14288
- schema: schema4,
14289
- data: val.__new
14290
- });
14752
+ data: fkData,
14753
+ schema: schema4
14754
+ };
14291
14755
  });
14292
- return stmts;
14756
+ };
14757
+ prepareLibSQLDropReferencesJson = (tableName, schema4, foreignKeys, json2, meta, action) => {
14758
+ const statements = Object.values(foreignKeys).map((fkData) => {
14759
+ const { columnsFrom, tableFrom, columnsTo, name: name2, tableTo, onDelete, onUpdate } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14760
+ const keys = Object.keys(json2.tables[tableName].columns);
14761
+ const filtered = columnsFrom.filter((it) => keys.includes(it));
14762
+ const fullDrop = filtered.length === 0;
14763
+ if (fullDrop) return;
14764
+ let isMulticolumn = false;
14765
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
14766
+ isMulticolumn = true;
14767
+ return {
14768
+ type: "delete_reference",
14769
+ tableName,
14770
+ data: fkData,
14771
+ schema: schema4,
14772
+ isMulticolumn
14773
+ };
14774
+ }
14775
+ const columnFrom = columnsFrom[0];
14776
+ const newTableName = getNewTableName(tableFrom, meta);
14777
+ const {
14778
+ notNull: columnNotNull,
14779
+ default: columnDefault,
14780
+ type: columnType
14781
+ } = json2.tables[newTableName].columns[columnFrom];
14782
+ const fkToSquash = {
14783
+ columnsFrom,
14784
+ columnsTo,
14785
+ name: name2,
14786
+ tableFrom: newTableName,
14787
+ tableTo,
14788
+ onDelete,
14789
+ onUpdate
14790
+ };
14791
+ const foreignKey = action === "push" ? SQLiteSquasher.squashPushFK(fkToSquash) : SQLiteSquasher.squashFK(fkToSquash);
14792
+ return {
14793
+ type: "delete_reference",
14794
+ tableName,
14795
+ data: foreignKey,
14796
+ schema: schema4,
14797
+ columnNotNull,
14798
+ columnDefault,
14799
+ columnType
14800
+ };
14801
+ });
14802
+ return statements.filter((it) => it);
14803
+ };
14804
+ prepareAlterReferencesJson = (tableName, schema4, foreignKeys) => {
14805
+ const stmts = [];
14806
+ Object.values(foreignKeys).map((val) => {
14807
+ stmts.push({
14808
+ type: "delete_reference",
14809
+ tableName,
14810
+ schema: schema4,
14811
+ data: val.__old
14812
+ });
14813
+ stmts.push({
14814
+ type: "create_reference",
14815
+ tableName,
14816
+ schema: schema4,
14817
+ data: val.__new
14818
+ });
14819
+ });
14820
+ return stmts;
14293
14821
  };
14294
14822
  prepareDropIndexesJson = (tableName, schema4, indexes) => {
14295
14823
  return Object.values(indexes).map((indexData) => {
@@ -14426,10 +14954,298 @@ var init_jsonStatements = __esm({
14426
14954
  }
14427
14955
  });
14428
14956
 
14957
+ // src/statementCombiner.ts
14958
+ var prepareLibSQLRecreateTable, prepareSQLiteRecreateTable, libSQLCombineStatements, sqliteCombineStatements;
14959
+ var init_statementCombiner = __esm({
14960
+ "src/statementCombiner.ts"() {
14961
+ "use strict";
14962
+ init_jsonStatements();
14963
+ init_sqliteSchema();
14964
+ prepareLibSQLRecreateTable = (table4, action) => {
14965
+ const { name: name2, columns, uniqueConstraints, indexes } = table4;
14966
+ const composites = Object.values(table4.compositePrimaryKeys).map(
14967
+ (it) => SQLiteSquasher.unsquashPK(it)
14968
+ );
14969
+ const references2 = Object.values(table4.foreignKeys);
14970
+ const fks = references2.map(
14971
+ (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14972
+ );
14973
+ const statements = [
14974
+ {
14975
+ type: "recreate_table",
14976
+ tableName: name2,
14977
+ columns: Object.values(columns),
14978
+ compositePKs: composites,
14979
+ referenceData: fks,
14980
+ uniqueConstraints: Object.values(uniqueConstraints)
14981
+ }
14982
+ ];
14983
+ if (Object.keys(indexes).length) {
14984
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
14985
+ }
14986
+ return statements;
14987
+ };
14988
+ prepareSQLiteRecreateTable = (table4, action) => {
14989
+ const { name: name2, columns, uniqueConstraints, indexes } = table4;
14990
+ const composites = Object.values(table4.compositePrimaryKeys).map(
14991
+ (it) => SQLiteSquasher.unsquashPK(it)
14992
+ );
14993
+ const references2 = Object.values(table4.foreignKeys);
14994
+ const fks = references2.map(
14995
+ (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14996
+ );
14997
+ const statements = [
14998
+ {
14999
+ type: "recreate_table",
15000
+ tableName: name2,
15001
+ columns: Object.values(columns),
15002
+ compositePKs: composites,
15003
+ referenceData: fks,
15004
+ uniqueConstraints: Object.values(uniqueConstraints)
15005
+ }
15006
+ ];
15007
+ if (Object.keys(indexes).length) {
15008
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
15009
+ }
15010
+ return statements;
15011
+ };
15012
+ libSQLCombineStatements = (statements, json2, action) => {
15013
+ const newStatements = {};
15014
+ for (const statement of statements) {
15015
+ if (statement.type === "alter_table_alter_column_drop_autoincrement" || statement.type === "alter_table_alter_column_set_autoincrement" || statement.type === "alter_table_alter_column_drop_pk" || statement.type === "alter_table_alter_column_set_pk" || statement.type === "create_composite_pk" || statement.type === "alter_composite_pk" || statement.type === "delete_composite_pk") {
15016
+ const tableName2 = statement.tableName;
15017
+ const statementsForTable2 = newStatements[tableName2];
15018
+ if (!statementsForTable2) {
15019
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15020
+ continue;
15021
+ }
15022
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15023
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
15024
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15025
+ if (wasRename) {
15026
+ newStatements[tableName2].push(...preparedStatements);
15027
+ } else {
15028
+ newStatements[tableName2] = preparedStatements;
15029
+ }
15030
+ continue;
15031
+ }
15032
+ continue;
15033
+ }
15034
+ if (statement.type === "alter_table_alter_column_set_type" || statement.type === "alter_table_alter_column_drop_notnull" || statement.type === "alter_table_alter_column_set_notnull" || statement.type === "alter_table_alter_column_set_default" || statement.type === "alter_table_alter_column_drop_default") {
15035
+ const { tableName: tableName2, columnName, columnPk } = statement;
15036
+ const columnIsPartOfUniqueIndex = Object.values(
15037
+ json2.tables[tableName2].indexes
15038
+ ).some((it) => {
15039
+ const unsquashIndex = SQLiteSquasher.unsquashIdx(it);
15040
+ return unsquashIndex.columns.includes(columnName) && unsquashIndex.isUnique;
15041
+ });
15042
+ const columnIsPartOfForeignKey = Object.values(
15043
+ json2.tables[tableName2].foreignKeys
15044
+ ).some((it) => {
15045
+ const unsquashFk = action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it);
15046
+ return unsquashFk.columnsFrom.includes(columnName);
15047
+ });
15048
+ const statementsForTable2 = newStatements[tableName2];
15049
+ if (!statementsForTable2 && (columnIsPartOfUniqueIndex || columnIsPartOfForeignKey || columnPk)) {
15050
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15051
+ continue;
15052
+ }
15053
+ if (statementsForTable2 && (columnIsPartOfUniqueIndex || columnIsPartOfForeignKey || columnPk)) {
15054
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15055
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
15056
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15057
+ if (wasRename) {
15058
+ newStatements[tableName2].push(...preparedStatements);
15059
+ } else {
15060
+ newStatements[tableName2] = preparedStatements;
15061
+ }
15062
+ }
15063
+ continue;
15064
+ }
15065
+ if (statementsForTable2 && !(columnIsPartOfUniqueIndex || columnIsPartOfForeignKey || columnPk)) {
15066
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15067
+ newStatements[tableName2].push(statement);
15068
+ }
15069
+ continue;
15070
+ }
15071
+ newStatements[tableName2] = [statement];
15072
+ continue;
15073
+ }
15074
+ if (statement.type === "create_reference") {
15075
+ const tableName2 = statement.tableName;
15076
+ const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
15077
+ const statementsForTable2 = newStatements[tableName2];
15078
+ if (!statementsForTable2) {
15079
+ newStatements[tableName2] = statement.isMulticolumn ? prepareLibSQLRecreateTable(json2.tables[tableName2], action) : newStatements[tableName2] = [statement];
15080
+ continue;
15081
+ }
15082
+ if (!statement.isMulticolumn && statementsForTable2.some(
15083
+ (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
15084
+ )) {
15085
+ continue;
15086
+ }
15087
+ if (statement.isMulticolumn) {
15088
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15089
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
15090
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15091
+ if (wasRename) {
15092
+ newStatements[tableName2].push(...preparedStatements);
15093
+ } else {
15094
+ newStatements[tableName2] = preparedStatements;
15095
+ }
15096
+ continue;
15097
+ }
15098
+ continue;
15099
+ }
15100
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15101
+ newStatements[tableName2].push(statement);
15102
+ }
15103
+ continue;
15104
+ }
15105
+ if (statement.type === "delete_reference") {
15106
+ const tableName2 = statement.tableName;
15107
+ const statementsForTable2 = newStatements[tableName2];
15108
+ if (!statementsForTable2) {
15109
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15110
+ continue;
15111
+ }
15112
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15113
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
15114
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15115
+ if (wasRename) {
15116
+ newStatements[tableName2].push(...preparedStatements);
15117
+ } else {
15118
+ newStatements[tableName2] = preparedStatements;
15119
+ }
15120
+ continue;
15121
+ }
15122
+ continue;
15123
+ }
15124
+ if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
15125
+ const tableName2 = statement.tableName;
15126
+ const statementsForTable2 = newStatements[tableName2];
15127
+ if (!statementsForTable2) {
15128
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15129
+ continue;
15130
+ }
15131
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15132
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
15133
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15134
+ if (wasRename) {
15135
+ newStatements[tableName2].push(...preparedStatements);
15136
+ } else {
15137
+ newStatements[tableName2] = preparedStatements;
15138
+ }
15139
+ continue;
15140
+ }
15141
+ continue;
15142
+ }
15143
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
15144
+ const statementsForTable = newStatements[tableName];
15145
+ if (!statementsForTable) {
15146
+ newStatements[tableName] = [statement];
15147
+ continue;
15148
+ }
15149
+ if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
15150
+ newStatements[tableName].push(statement);
15151
+ }
15152
+ }
15153
+ const combinedStatements = Object.values(newStatements).flat();
15154
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
15155
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
15156
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
15157
+ return [...renamedTables, ...renamedColumns, ...rest];
15158
+ };
15159
+ sqliteCombineStatements = (statements, json2, action) => {
15160
+ const newStatements = {};
15161
+ for (const statement of statements) {
15162
+ if (statement.type === "alter_table_alter_column_set_type" || statement.type === "alter_table_alter_column_set_default" || statement.type === "alter_table_alter_column_drop_default" || statement.type === "alter_table_alter_column_set_notnull" || statement.type === "alter_table_alter_column_drop_notnull" || statement.type === "alter_table_alter_column_drop_autoincrement" || statement.type === "alter_table_alter_column_set_autoincrement" || statement.type === "alter_table_alter_column_drop_pk" || statement.type === "alter_table_alter_column_set_pk" || statement.type === "delete_reference" || statement.type === "alter_reference" || statement.type === "create_composite_pk" || statement.type === "alter_composite_pk" || statement.type === "delete_composite_pk" || statement.type === "create_unique_constraint" || statement.type === "delete_unique_constraint") {
15163
+ const tableName2 = statement.tableName;
15164
+ const statementsForTable2 = newStatements[tableName2];
15165
+ if (!statementsForTable2) {
15166
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15167
+ continue;
15168
+ }
15169
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15170
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
15171
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15172
+ if (wasRename) {
15173
+ newStatements[tableName2].push(...preparedStatements);
15174
+ } else {
15175
+ newStatements[tableName2] = preparedStatements;
15176
+ }
15177
+ continue;
15178
+ }
15179
+ continue;
15180
+ }
15181
+ if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
15182
+ const tableName2 = statement.tableName;
15183
+ const statementsForTable2 = newStatements[tableName2];
15184
+ if (!statementsForTable2) {
15185
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15186
+ continue;
15187
+ }
15188
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15189
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
15190
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15191
+ if (wasRename) {
15192
+ newStatements[tableName2].push(...preparedStatements);
15193
+ } else {
15194
+ newStatements[tableName2] = preparedStatements;
15195
+ }
15196
+ continue;
15197
+ }
15198
+ continue;
15199
+ }
15200
+ if (statement.type === "create_reference") {
15201
+ const tableName2 = statement.tableName;
15202
+ const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
15203
+ const statementsForTable2 = newStatements[tableName2];
15204
+ if (!statementsForTable2) {
15205
+ newStatements[tableName2] = prepareSQLiteRecreateTable(json2.tables[tableName2], action);
15206
+ continue;
15207
+ }
15208
+ if (data.columnsFrom.length === 1 && statementsForTable2.some(
15209
+ (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
15210
+ )) {
15211
+ continue;
15212
+ }
15213
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
15214
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
15215
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
15216
+ if (wasRename) {
15217
+ newStatements[tableName2].push(...preparedStatements);
15218
+ } else {
15219
+ newStatements[tableName2] = preparedStatements;
15220
+ }
15221
+ continue;
15222
+ }
15223
+ continue;
15224
+ }
15225
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
15226
+ const statementsForTable = newStatements[tableName];
15227
+ if (!statementsForTable) {
15228
+ newStatements[tableName] = [statement];
15229
+ continue;
15230
+ }
15231
+ if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
15232
+ newStatements[tableName].push(statement);
15233
+ }
15234
+ }
15235
+ const combinedStatements = Object.values(newStatements).flat();
15236
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
15237
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
15238
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
15239
+ return [...renamedTables, ...renamedColumns, ...rest];
15240
+ };
15241
+ }
15242
+ });
15243
+
14429
15244
  // src/snapshotsDiffer.ts
14430
15245
  var snapshotsDiffer_exports = {};
14431
15246
  __export(snapshotsDiffer_exports, {
14432
15247
  alteredTableScheme: () => alteredTableScheme,
15248
+ applyLibSQLSnapshotsDiff: () => applyLibSQLSnapshotsDiff,
14433
15249
  applyMysqlSnapshotsDiff: () => applyMysqlSnapshotsDiff,
14434
15250
  applyPgSnapshotsDiff: () => applyPgSnapshotsDiff,
14435
15251
  applySqliteSnapshotsDiff: () => applySqliteSnapshotsDiff,
@@ -14439,7 +15255,7 @@ __export(snapshotsDiffer_exports, {
14439
15255
  makePatched: () => makePatched,
14440
15256
  makeSelfOrPatched: () => makeSelfOrPatched
14441
15257
  });
14442
- var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff;
15258
+ var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff, applyLibSQLSnapshotsDiff;
14443
15259
  var init_snapshotsDiffer = __esm({
14444
15260
  "src/snapshotsDiffer.ts"() {
14445
15261
  "use strict";
@@ -14451,6 +15267,7 @@ var init_snapshotsDiffer = __esm({
14451
15267
  init_mysqlSchema();
14452
15268
  init_pgSchema();
14453
15269
  init_sqliteSchema();
15270
+ init_statementCombiner();
14454
15271
  init_utils();
14455
15272
  makeChanged = (schema4) => {
14456
15273
  return objectType({
@@ -15368,9 +16185,308 @@ var init_snapshotsDiffer = __esm({
15368
16185
  );
15369
16186
  alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
15370
16187
  it.name,
15371
- it.alteredCompositePKs,
15372
- prevFull,
15373
- curFull
16188
+ it.alteredCompositePKs,
16189
+ prevFull,
16190
+ curFull
16191
+ );
16192
+ let addedUniqueConstraints = [];
16193
+ let deletedUniqueConstraints = [];
16194
+ let alteredUniqueConstraints = [];
16195
+ addedUniqueConstraints = prepareAddUniqueConstraintPg(
16196
+ it.name,
16197
+ it.schema,
16198
+ it.addedUniqueConstraints
16199
+ );
16200
+ deletedUniqueConstraints = prepareDeleteUniqueConstraintPg(
16201
+ it.name,
16202
+ it.schema,
16203
+ it.deletedUniqueConstraints
16204
+ );
16205
+ if (it.alteredUniqueConstraints) {
16206
+ const added = {};
16207
+ const deleted = {};
16208
+ for (const k of Object.keys(it.alteredUniqueConstraints)) {
16209
+ added[k] = it.alteredUniqueConstraints[k].__new;
16210
+ deleted[k] = it.alteredUniqueConstraints[k].__old;
16211
+ }
16212
+ addedUniqueConstraints.push(
16213
+ ...prepareAddUniqueConstraintPg(it.name, it.schema, added)
16214
+ );
16215
+ deletedUniqueConstraints.push(
16216
+ ...prepareDeleteUniqueConstraintPg(it.name, it.schema, deleted)
16217
+ );
16218
+ }
16219
+ jsonAddedCompositePKs.push(...addedCompositePKs);
16220
+ jsonDeletedCompositePKs.push(...deletedCompositePKs);
16221
+ jsonAlteredCompositePKs.push(...alteredCompositePKs);
16222
+ jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
16223
+ jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
16224
+ jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
16225
+ });
16226
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
16227
+ const tableName = it.tableName;
16228
+ const schema4 = it.schema;
16229
+ return {
16230
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
16231
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
16232
+ };
16233
+ });
16234
+ const jsonTableAlternations = alteredTables.map((it) => {
16235
+ return prepareAlterColumnsMysql(
16236
+ it.name,
16237
+ it.schema,
16238
+ it.altered,
16239
+ json1,
16240
+ json2,
16241
+ action
16242
+ );
16243
+ }).flat();
16244
+ const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
16245
+ return prepareCreateIndexesJson(
16246
+ it.name,
16247
+ it.schema,
16248
+ it.addedIndexes || {},
16249
+ curFull.internal
16250
+ );
16251
+ }).flat();
16252
+ const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16253
+ return prepareDropIndexesJson(
16254
+ it.name,
16255
+ it.schema,
16256
+ it.deletedIndexes || {}
16257
+ );
16258
+ }).flat();
16259
+ alteredTables.forEach((it) => {
16260
+ const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
16261
+ (current, item) => {
16262
+ current[item] = it.alteredIndexes[item].__old;
16263
+ return current;
16264
+ },
16265
+ {}
16266
+ );
16267
+ const createdIndexes = Object.keys(it.alteredIndexes).reduce(
16268
+ (current, item) => {
16269
+ current[item] = it.alteredIndexes[item].__new;
16270
+ return current;
16271
+ },
16272
+ {}
16273
+ );
16274
+ jsonCreateIndexesForAllAlteredTables.push(
16275
+ ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16276
+ );
16277
+ jsonDropIndexesForAllAlteredTables.push(
16278
+ ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
16279
+ );
16280
+ });
16281
+ const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
16282
+ return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
16283
+ }).flat();
16284
+ const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16285
+ const forAdded = prepareCreateReferencesJson(
16286
+ it.name,
16287
+ it.schema,
16288
+ it.addedForeignKeys
16289
+ );
16290
+ const forAltered = prepareDropReferencesJson(
16291
+ it.name,
16292
+ it.schema,
16293
+ it.deletedForeignKeys
16294
+ );
16295
+ const alteredFKs = prepareAlterReferencesJson(
16296
+ it.name,
16297
+ it.schema,
16298
+ it.alteredForeignKeys
16299
+ );
16300
+ return [...forAdded, ...forAltered, ...alteredFKs];
16301
+ }).flat();
16302
+ const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16303
+ (t) => t.type === "create_reference"
16304
+ );
16305
+ const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16306
+ (t) => t.type === "delete_reference"
16307
+ );
16308
+ const jsonMySqlCreateTables = createdTables.map((it) => {
16309
+ return prepareMySqlCreateTableJson(
16310
+ it,
16311
+ curFull,
16312
+ curFull.internal
16313
+ );
16314
+ });
16315
+ jsonStatements.push(...jsonMySqlCreateTables);
16316
+ jsonStatements.push(...jsonDropTables);
16317
+ jsonStatements.push(...jsonRenameTables);
16318
+ jsonStatements.push(...jsonRenameColumnsStatements);
16319
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16320
+ jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
16321
+ jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
16322
+ jsonStatements.push(...jsonDeletedCompositePKs);
16323
+ jsonStatements.push(...jsonTableAlternations);
16324
+ jsonStatements.push(...jsonAddedCompositePKs);
16325
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16326
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16327
+ jsonStatements.push(...jsonAddColumnsStatemets);
16328
+ jsonStatements.push(...jsonCreateReferencesForCreatedTables);
16329
+ jsonStatements.push(...jsonCreateIndexesForCreatedTables);
16330
+ jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
16331
+ jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16332
+ jsonStatements.push(...jsonDropColumnsStatemets);
16333
+ jsonStatements.push(...jsonAlteredCompositePKs);
16334
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16335
+ jsonStatements.push(...jsonAlteredUniqueConstraints);
16336
+ const sqlStatements = fromJson(jsonStatements, "mysql");
16337
+ const uniqueSqlStatements = [];
16338
+ sqlStatements.forEach((ss) => {
16339
+ if (!uniqueSqlStatements.includes(ss)) {
16340
+ uniqueSqlStatements.push(ss);
16341
+ }
16342
+ });
16343
+ const rTables = renamedTables.map((it) => {
16344
+ return { from: it.from, to: it.to };
16345
+ });
16346
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
16347
+ return {
16348
+ statements: jsonStatements,
16349
+ sqlStatements: uniqueSqlStatements,
16350
+ _meta
16351
+ };
16352
+ };
16353
+ applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16354
+ const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
16355
+ const {
16356
+ created: createdTables,
16357
+ deleted: deletedTables,
16358
+ renamed: renamedTables
16359
+ } = await tablesResolver2({
16360
+ created: tablesDiff.added,
16361
+ deleted: tablesDiff.deleted
16362
+ });
16363
+ const tablesPatchedSnap1 = copy(json1);
16364
+ tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_2, it) => {
16365
+ const { name: name2 } = nameChangeFor(it, renamedTables);
16366
+ it.name = name2;
16367
+ return [name2, it];
16368
+ });
16369
+ const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
16370
+ const columnRenames = [];
16371
+ const columnCreates = [];
16372
+ const columnDeletes = [];
16373
+ for (let entry of Object.values(res)) {
16374
+ const { renamed, created, deleted } = await columnsResolver2({
16375
+ tableName: entry.name,
16376
+ schema: entry.schema,
16377
+ deleted: entry.columns.deleted,
16378
+ created: entry.columns.added
16379
+ });
16380
+ if (created.length > 0) {
16381
+ columnCreates.push({
16382
+ table: entry.name,
16383
+ columns: created
16384
+ });
16385
+ }
16386
+ if (deleted.length > 0) {
16387
+ columnDeletes.push({
16388
+ table: entry.name,
16389
+ columns: deleted
16390
+ });
16391
+ }
16392
+ if (renamed.length > 0) {
16393
+ columnRenames.push({
16394
+ table: entry.name,
16395
+ renames: renamed
16396
+ });
16397
+ }
16398
+ }
16399
+ const columnRenamesDict = columnRenames.reduce(
16400
+ (acc, it) => {
16401
+ acc[it.table] = it.renames;
16402
+ return acc;
16403
+ },
16404
+ {}
16405
+ );
16406
+ const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
16407
+ columnsPatchedSnap1.tables = mapEntries(
16408
+ columnsPatchedSnap1.tables,
16409
+ (tableKey2, tableValue) => {
16410
+ const patchedColumns = mapKeys(
16411
+ tableValue.columns,
16412
+ (columnKey, column4) => {
16413
+ const rens = columnRenamesDict[tableValue.name] || [];
16414
+ const newName = columnChangeFor(columnKey, rens);
16415
+ column4.name = newName;
16416
+ return newName;
16417
+ }
16418
+ );
16419
+ tableValue.columns = patchedColumns;
16420
+ return [tableKey2, tableValue];
16421
+ }
16422
+ );
16423
+ const diffResult = applyJsonDiff(columnsPatchedSnap1, json2);
16424
+ const typedResult = diffResultSchemeSQLite.parse(diffResult);
16425
+ const tablesMap = {};
16426
+ typedResult.alteredTablesWithColumns.forEach((obj) => {
16427
+ tablesMap[obj.name] = obj;
16428
+ });
16429
+ const jsonCreateTables = createdTables.map((it) => {
16430
+ return prepareSQLiteCreateTable(it, action);
16431
+ });
16432
+ const jsonCreateIndexesForCreatedTables = createdTables.map((it) => {
16433
+ return prepareCreateIndexesJson(
16434
+ it.name,
16435
+ it.schema,
16436
+ it.indexes,
16437
+ curFull.internal
16438
+ );
16439
+ }).flat();
16440
+ const jsonDropTables = deletedTables.map((it) => {
16441
+ return prepareDropTableJson(it);
16442
+ });
16443
+ const jsonRenameTables = renamedTables.map((it) => {
16444
+ return prepareRenameTableJson(it.from, it.to);
16445
+ });
16446
+ const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
16447
+ const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
16448
+ const jsonAddColumnsStatemets = columnCreates.map((it) => {
16449
+ return _prepareSqliteAddColumns(
16450
+ it.table,
16451
+ it.columns,
16452
+ tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
16453
+ );
16454
+ }).flat();
16455
+ const allAltered = typedResult.alteredTablesWithColumns;
16456
+ const jsonAddedCompositePKs = [];
16457
+ const jsonDeletedCompositePKs = [];
16458
+ const jsonAlteredCompositePKs = [];
16459
+ const jsonAddedUniqueConstraints = [];
16460
+ const jsonDeletedUniqueConstraints = [];
16461
+ const jsonAlteredUniqueConstraints = [];
16462
+ allAltered.forEach((it) => {
16463
+ let addedColumns = [];
16464
+ for (const addedPkName of Object.keys(it.addedCompositePKs)) {
16465
+ const addedPkColumns = it.addedCompositePKs[addedPkName];
16466
+ addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
16467
+ }
16468
+ let deletedColumns = [];
16469
+ for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
16470
+ const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
16471
+ deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
16472
+ }
16473
+ const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
16474
+ let addedCompositePKs = [];
16475
+ let deletedCompositePKs = [];
16476
+ let alteredCompositePKs = [];
16477
+ if (doPerformDeleteAndCreate) {
16478
+ addedCompositePKs = prepareAddCompositePrimaryKeySqlite(
16479
+ it.name,
16480
+ it.addedCompositePKs
16481
+ );
16482
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeySqlite(
16483
+ it.name,
16484
+ it.deletedCompositePKs
16485
+ );
16486
+ }
16487
+ alteredCompositePKs = prepareAlterCompositePrimaryKeySqlite(
16488
+ it.name,
16489
+ it.alteredCompositePKs
15374
16490
  );
15375
16491
  let addedUniqueConstraints = [];
15376
16492
  let deletedUniqueConstraints = [];
@@ -15414,17 +16530,10 @@ var init_snapshotsDiffer = __esm({
15414
16530
  to: { schema: schema4, table: tableName, column: it.newColumnName }
15415
16531
  };
15416
16532
  });
15417
- const jsonTableAlternations = alteredTables.map((it) => {
15418
- return prepareAlterColumnsMysql(
15419
- it.name,
15420
- it.schema,
15421
- it.altered,
15422
- json1,
15423
- json2,
15424
- action
15425
- );
16533
+ const jsonTableAlternations = allAltered.map((it) => {
16534
+ return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15426
16535
  }).flat();
15427
- const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
16536
+ const jsonCreateIndexesForAllAlteredTables = allAltered.map((it) => {
15428
16537
  return prepareCreateIndexesJson(
15429
16538
  it.name,
15430
16539
  it.schema,
@@ -15432,14 +16541,14 @@ var init_snapshotsDiffer = __esm({
15432
16541
  curFull.internal
15433
16542
  );
15434
16543
  }).flat();
15435
- const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16544
+ const jsonDropIndexesForAllAlteredTables = allAltered.map((it) => {
15436
16545
  return prepareDropIndexesJson(
15437
16546
  it.name,
15438
16547
  it.schema,
15439
16548
  it.deletedIndexes || {}
15440
16549
  );
15441
16550
  }).flat();
15442
- alteredTables.forEach((it) => {
16551
+ allAltered.forEach((it) => {
15443
16552
  const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
15444
16553
  (current, item) => {
15445
16554
  current[item] = it.alteredIndexes[item].__old;
@@ -15455,16 +16564,18 @@ var init_snapshotsDiffer = __esm({
15455
16564
  {}
15456
16565
  );
15457
16566
  jsonCreateIndexesForAllAlteredTables.push(
15458
- ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16567
+ ...prepareCreateIndexesJson(
16568
+ it.name,
16569
+ it.schema,
16570
+ createdIndexes || {},
16571
+ curFull.internal
16572
+ )
15459
16573
  );
15460
16574
  jsonDropIndexesForAllAlteredTables.push(
15461
16575
  ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
15462
16576
  );
15463
16577
  });
15464
- const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
15465
- return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
15466
- }).flat();
15467
- const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16578
+ const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15468
16579
  const forAdded = prepareCreateReferencesJson(
15469
16580
  it.name,
15470
16581
  it.schema,
@@ -15488,35 +16599,25 @@ var init_snapshotsDiffer = __esm({
15488
16599
  const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
15489
16600
  (t) => t.type === "delete_reference"
15490
16601
  );
15491
- const jsonMySqlCreateTables = createdTables.map((it) => {
15492
- return prepareMySqlCreateTableJson(
15493
- it,
15494
- curFull,
15495
- curFull.internal
15496
- );
15497
- });
15498
- jsonStatements.push(...jsonMySqlCreateTables);
16602
+ const jsonStatements = [];
16603
+ jsonStatements.push(...jsonCreateTables);
15499
16604
  jsonStatements.push(...jsonDropTables);
15500
16605
  jsonStatements.push(...jsonRenameTables);
15501
16606
  jsonStatements.push(...jsonRenameColumnsStatements);
15502
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15503
16607
  jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
15504
16608
  jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
15505
16609
  jsonStatements.push(...jsonDeletedCompositePKs);
15506
16610
  jsonStatements.push(...jsonTableAlternations);
15507
16611
  jsonStatements.push(...jsonAddedCompositePKs);
15508
- jsonStatements.push(...jsonAddedUniqueConstraints);
15509
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15510
16612
  jsonStatements.push(...jsonAddColumnsStatemets);
15511
- jsonStatements.push(...jsonCreateReferencesForCreatedTables);
15512
16613
  jsonStatements.push(...jsonCreateIndexesForCreatedTables);
15513
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15514
16614
  jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16615
+ jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15515
16616
  jsonStatements.push(...jsonDropColumnsStatemets);
15516
16617
  jsonStatements.push(...jsonAlteredCompositePKs);
15517
- jsonStatements.push(...jsonAddedUniqueConstraints);
15518
16618
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15519
- const sqlStatements = fromJson(jsonStatements, "mysql");
16619
+ const combinedJsonStatements = sqliteCombineStatements(jsonStatements, json2, action);
16620
+ const sqlStatements = fromJson(combinedJsonStatements, "sqlite");
15520
16621
  const uniqueSqlStatements = [];
15521
16622
  sqlStatements.forEach((ss) => {
15522
16623
  if (!uniqueSqlStatements.includes(ss)) {
@@ -15528,12 +16629,12 @@ var init_snapshotsDiffer = __esm({
15528
16629
  });
15529
16630
  const _meta = prepareMigrationMeta([], rTables, rColumns);
15530
16631
  return {
15531
- statements: jsonStatements,
16632
+ statements: combinedJsonStatements,
15532
16633
  sqlStatements: uniqueSqlStatements,
15533
16634
  _meta
15534
16635
  };
15535
16636
  };
15536
- applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16637
+ applyLibSQLSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
15537
16638
  const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
15538
16639
  const {
15539
16640
  created: createdTables,
@@ -15635,6 +16736,18 @@ var init_snapshotsDiffer = __esm({
15635
16736
  tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
15636
16737
  );
15637
16738
  }).flat();
16739
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
16740
+ const tableName = it.tableName;
16741
+ const schema4 = it.schema;
16742
+ return {
16743
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
16744
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
16745
+ };
16746
+ });
16747
+ const rTables = renamedTables.map((it) => {
16748
+ return { from: it.from, to: it.to };
16749
+ });
16750
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
15638
16751
  const allAltered = typedResult.alteredTablesWithColumns;
15639
16752
  const jsonAddedCompositePKs = [];
15640
16753
  const jsonDeletedCompositePKs = [];
@@ -15705,14 +16818,6 @@ var init_snapshotsDiffer = __esm({
15705
16818
  jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15706
16819
  jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15707
16820
  });
15708
- const rColumns = jsonRenameColumnsStatements.map((it) => {
15709
- const tableName = it.tableName;
15710
- const schema4 = it.schema;
15711
- return {
15712
- from: { schema: schema4, table: tableName, column: it.oldColumnName },
15713
- to: { schema: schema4, table: tableName, column: it.newColumnName }
15714
- };
15715
- });
15716
16821
  const jsonTableAlternations = allAltered.map((it) => {
15717
16822
  return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15718
16823
  }).flat();
@@ -15759,21 +16864,22 @@ var init_snapshotsDiffer = __esm({
15759
16864
  );
15760
16865
  });
15761
16866
  const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15762
- const forAdded = prepareCreateReferencesJson(
15763
- it.name,
15764
- it.schema,
15765
- it.addedForeignKeys
15766
- );
15767
- const forAltered = prepareDropReferencesJson(
16867
+ const forAdded = prepareLibSQLCreateReferencesJson(
15768
16868
  it.name,
15769
16869
  it.schema,
15770
- it.deletedForeignKeys
16870
+ it.addedForeignKeys,
16871
+ json2,
16872
+ action
15771
16873
  );
15772
- const alteredFKs = prepareAlterReferencesJson(
16874
+ const forAltered = prepareLibSQLDropReferencesJson(
15773
16875
  it.name,
15774
16876
  it.schema,
15775
- it.alteredForeignKeys
16877
+ it.deletedForeignKeys,
16878
+ json2,
16879
+ _meta,
16880
+ action
15776
16881
  );
16882
+ const alteredFKs = prepareAlterReferencesJson(it.name, it.schema, it.alteredForeignKeys);
15777
16883
  return [...forAdded, ...forAltered, ...alteredFKs];
15778
16884
  }).flat();
15779
16885
  const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
@@ -15799,19 +16905,22 @@ var init_snapshotsDiffer = __esm({
15799
16905
  jsonStatements.push(...jsonDropColumnsStatemets);
15800
16906
  jsonStatements.push(...jsonAlteredCompositePKs);
15801
16907
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15802
- const sqlStatements = fromJson(jsonStatements, "sqlite");
16908
+ const combinedJsonStatements = libSQLCombineStatements(jsonStatements, json2, action);
16909
+ const sqlStatements = fromJson(
16910
+ combinedJsonStatements,
16911
+ "sqlite",
16912
+ action,
16913
+ "turso",
16914
+ json2
16915
+ );
15803
16916
  const uniqueSqlStatements = [];
15804
16917
  sqlStatements.forEach((ss) => {
15805
16918
  if (!uniqueSqlStatements.includes(ss)) {
15806
16919
  uniqueSqlStatements.push(ss);
15807
16920
  }
15808
16921
  });
15809
- const rTables = renamedTables.map((it) => {
15810
- return { from: it.from, to: it.to };
15811
- });
15812
- const _meta = prepareMigrationMeta([], rTables, rColumns);
15813
16922
  return {
15814
- statements: jsonStatements,
16923
+ statements: combinedJsonStatements,
15815
16924
  sqlStatements: uniqueSqlStatements,
15816
16925
  _meta
15817
16926
  };
@@ -17482,7 +18591,7 @@ function getTableName(table4) {
17482
18591
  function getTableUniqueName(table4) {
17483
18592
  return `${table4[Schema] ?? "public"}.${table4[TableName]}`;
17484
18593
  }
17485
- var TableName, Schema, Columns, ExtraConfigColumns, OriginalName, BaseName, IsAlias, ExtraConfigBuilder, IsDrizzleTable, _a3, _b, _c, _d, _e, _f, _g, _h, _i, Table2;
18594
+ var TableName, Schema, Columns, ExtraConfigColumns, OriginalName, BaseName, IsAlias, ExtraConfigBuilder, IsDrizzleTable, _a3, _b, _c, _d, _e, _f, _g, _h, _i, _j, Table2;
17486
18595
  var init_table = __esm({
17487
18596
  "../drizzle-orm/dist/table.js"() {
17488
18597
  "use strict";
@@ -17496,32 +18605,34 @@ var init_table = __esm({
17496
18605
  IsAlias = Symbol.for("drizzle:IsAlias");
17497
18606
  ExtraConfigBuilder = Symbol.for("drizzle:ExtraConfigBuilder");
17498
18607
  IsDrizzleTable = Symbol.for("drizzle:IsDrizzleTable");
17499
- _i = entityKind, _h = TableName, _g = OriginalName, _f = Schema, _e = Columns, _d = ExtraConfigColumns, _c = BaseName, _b = IsAlias, _a3 = ExtraConfigBuilder;
18608
+ _j = entityKind, _i = TableName, _h = OriginalName, _g = Schema, _f = Columns, _e = ExtraConfigColumns, _d = BaseName, _c = IsAlias, _b = IsDrizzleTable, _a3 = ExtraConfigBuilder;
17500
18609
  Table2 = class {
17501
18610
  constructor(name2, schema4, baseName) {
17502
18611
  /**
17503
18612
  * @internal
17504
18613
  * Can be changed if the table is aliased.
17505
18614
  */
17506
- __publicField(this, _h);
18615
+ __publicField(this, _i);
17507
18616
  /**
17508
18617
  * @internal
17509
18618
  * Used to store the original name of the table, before any aliasing.
17510
18619
  */
18620
+ __publicField(this, _h);
18621
+ /** @internal */
17511
18622
  __publicField(this, _g);
17512
18623
  /** @internal */
17513
18624
  __publicField(this, _f);
17514
18625
  /** @internal */
17515
18626
  __publicField(this, _e);
17516
- /** @internal */
17517
- __publicField(this, _d);
17518
18627
  /**
17519
18628
  * @internal
17520
18629
  * Used to store the table name before the transformation via the `tableCreator` functions.
17521
18630
  */
17522
- __publicField(this, _c);
18631
+ __publicField(this, _d);
18632
+ /** @internal */
18633
+ __publicField(this, _c, false);
17523
18634
  /** @internal */
17524
- __publicField(this, _b, false);
18635
+ __publicField(this, _b, true);
17525
18636
  /** @internal */
17526
18637
  __publicField(this, _a3);
17527
18638
  this[TableName] = this[OriginalName] = name2;
@@ -17529,7 +18640,7 @@ var init_table = __esm({
17529
18640
  this[BaseName] = baseName;
17530
18641
  }
17531
18642
  };
17532
- __publicField(Table2, _i, "Table");
18643
+ __publicField(Table2, _j, "Table");
17533
18644
  /** @internal */
17534
18645
  __publicField(Table2, "Symbol", {
17535
18646
  Name: TableName,
@@ -18105,7 +19216,7 @@ var version;
18105
19216
  var init_version = __esm({
18106
19217
  "../drizzle-orm/dist/version.js"() {
18107
19218
  "use strict";
18108
- version = "0.32.2";
19219
+ version = "0.33.0";
18109
19220
  }
18110
19221
  });
18111
19222
 
@@ -18207,6 +19318,12 @@ function fillPlaceholders(params, values) {
18207
19318
  }
18208
19319
  return values[p.name];
18209
19320
  }
19321
+ if (is(p, Param) && is(p.value, Placeholder)) {
19322
+ if (!(p.value.name in values)) {
19323
+ throw new Error(`No value for placeholder "${p.value.name}" was provided`);
19324
+ }
19325
+ return p.encoder.mapToDriverValue(values[p.value.name]);
19326
+ }
18210
19327
  return p;
18211
19328
  });
18212
19329
  }
@@ -18320,6 +19437,9 @@ var init_sql = __esm({
18320
19437
  };
18321
19438
  }
18322
19439
  if (is(chunk, Param)) {
19440
+ if (is(chunk.value, Placeholder)) {
19441
+ return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk], typings: ["none"] };
19442
+ }
18323
19443
  const mappedValue = chunk.value === null ? null : chunk.encoder.mapToDriverValue(chunk.value);
18324
19444
  if (is(mappedValue, _SQL)) {
18325
19445
  return this.buildQueryFromSourceParams([mappedValue], config);
@@ -24641,6 +25761,30 @@ function minRangeForIdentityBasedOn(columnType) {
24641
25761
  function stringFromDatabaseIdentityProperty(field) {
24642
25762
  return typeof field === "string" ? field : typeof field === "undefined" ? void 0 : typeof field === "bigint" ? field.toString() : String(field);
24643
25763
  }
25764
+ function buildArrayString(array, sqlType) {
25765
+ sqlType = sqlType.split("[")[0];
25766
+ const values = array.map((value) => {
25767
+ if (typeof value === "number" || typeof value === "bigint") {
25768
+ return value.toString();
25769
+ } else if (typeof value === "boolean") {
25770
+ return value ? "true" : "false";
25771
+ } else if (Array.isArray(value)) {
25772
+ return buildArrayString(value, sqlType);
25773
+ } else if (value instanceof Date) {
25774
+ if (sqlType === "date") {
25775
+ return `"${value.toISOString().split("T")[0]}"`;
25776
+ } else if (sqlType === "timestamp") {
25777
+ return `"${value.toISOString().replace("T", " ").slice(0, 23)}"`;
25778
+ } else {
25779
+ return `"${value.toISOString()}"`;
25780
+ }
25781
+ } else if (typeof value === "object") {
25782
+ return `"${JSON.stringify(value).replaceAll('"', '\\"')}"`;
25783
+ }
25784
+ return `"${value}"`;
25785
+ }).join(",");
25786
+ return `{${values}}`;
25787
+ }
24644
25788
  var dialect4, indexName, generatePgSnapshot, trimChar, fromDatabase, columnToDefault, defaultForColumn;
24645
25789
  var init_pgSerializer = __esm({
24646
25790
  "src/serializer/pgSerializer.ts"() {
@@ -24651,6 +25795,7 @@ var init_pgSerializer = __esm({
24651
25795
  init_pg_core();
24652
25796
  init_vector3();
24653
25797
  init_outputs();
25798
+ init_utils();
24654
25799
  init_serializer();
24655
25800
  dialect4 = new PgDialect();
24656
25801
  indexName = (tableName, columns) => {
@@ -24757,6 +25902,11 @@ ${withStyle.errorWarning(`We've found duplicated unique constraint names in ${so
24757
25902
  } else {
24758
25903
  columnToSet.default = `'${column4.default.toISOString()}'`;
24759
25904
  }
25905
+ } else if (isPgArrayType(sqlTypeLowered) && Array.isArray(column4.default)) {
25906
+ columnToSet.default = `'${buildArrayString(
25907
+ column4.default,
25908
+ sqlTypeLowered
25909
+ )}'::${sqlTypeLowered}`;
24760
25910
  } else {
24761
25911
  columnToSet.default = column4.default;
24762
25912
  }
@@ -29036,7 +30186,7 @@ The unique constraint ${source_default.underline.blue(
29036
30186
  primaryKeys.forEach((it) => {
29037
30187
  if (it.columns.length > 1) {
29038
30188
  primaryKeysObject[it.getName()] = {
29039
- columns: it.columns.map((it2) => it2.name).sort(),
30189
+ columns: it.columns.map((it2) => it2.name),
29040
30190
  name: it.getName()
29041
30191
  };
29042
30192
  } else {
@@ -29161,7 +30311,6 @@ The unique constraint ${source_default.underline.blue(
29161
30311
  }
29162
30312
  for (const [key, value] of Object.entries(tableToPk)) {
29163
30313
  if (value.length > 1) {
29164
- value.sort();
29165
30314
  result[key].compositePrimaryKeys = {
29166
30315
  [`${key}_${value.join("_")}_pk`]: {
29167
30316
  columns: value,
@@ -33605,7 +34754,7 @@ We have encountered a collision between the index name on columns ${source_defau
33605
34754
  onUpdate = true;
33606
34755
  }
33607
34756
  const newColumn = {
33608
- default: columnDefault === null ? void 0 : /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault) && !columnType.startsWith("decimal") ? Number(columnDefault) : isDefaultAnExpression ? clearDefaults(columnDefault, collation) : `'${columnDefault}'`,
34757
+ default: columnDefault === null ? void 0 : /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault) && !["decimal", "char", "varchar"].some((type) => columnType.startsWith(type)) ? Number(columnDefault) : isDefaultAnExpression ? clearDefaults(columnDefault, collation) : `'${columnDefault}'`,
33609
34758
  autoincrement: isAutoincrement,
33610
34759
  name: columnName,
33611
34760
  type: changedType,
@@ -33833,7 +34982,8 @@ var init_cli = __esm({
33833
34982
  schemaFilter: unionType([stringType(), stringType().array()]).optional().default(["public"]),
33834
34983
  extensionsFilters: literalType("postgis").array().optional(),
33835
34984
  verbose: booleanType().optional(),
33836
- strict: booleanType().optional()
34985
+ strict: booleanType().optional(),
34986
+ driver: driver.optional()
33837
34987
  }).passthrough();
33838
34988
  pullParams = objectType({
33839
34989
  config: stringType().optional(),
@@ -34808,6 +35958,37 @@ var updateUpToV6 = (json) => {
34808
35958
  enums
34809
35959
  };
34810
35960
  };
35961
+ var updateUpToV7 = (json) => {
35962
+ const schema4 = pgSchemaV6.parse(json);
35963
+ const tables = Object.fromEntries(
35964
+ Object.entries(schema4.tables).map((it) => {
35965
+ const table4 = it[1];
35966
+ const mappedIndexes = Object.fromEntries(
35967
+ Object.entries(table4.indexes).map((idx) => {
35968
+ const { columns, ...rest } = idx[1];
35969
+ const mappedColumns = columns.map((it2) => {
35970
+ return {
35971
+ expression: it2,
35972
+ isExpression: false,
35973
+ asc: true,
35974
+ nulls: "last",
35975
+ opClass: void 0
35976
+ };
35977
+ });
35978
+ return [idx[0], { columns: mappedColumns, with: {}, ...rest }];
35979
+ })
35980
+ );
35981
+ return [it[0], { ...table4, indexes: mappedIndexes }];
35982
+ })
35983
+ );
35984
+ return {
35985
+ ...schema4,
35986
+ version: "7",
35987
+ dialect: "postgresql",
35988
+ sequences: {},
35989
+ tables
35990
+ };
35991
+ };
34811
35992
 
34812
35993
  // src/cli/commands/sqliteIntrospect.ts
34813
35994
  var import_hanji6 = __toESM(require_hanji());
@@ -34954,284 +36135,8 @@ var sqlitePushIntrospect = async (db, filters) => {
34954
36135
  return { schema: schema4 };
34955
36136
  };
34956
36137
 
34957
- // src/cli/commands/sqlitePushUtils.ts
34958
- init_source();
34959
- init_sqliteSchema();
34960
- init_sqlgenerator();
34961
- var _moveDataStatements = (tableName, json, dataLoss = false) => {
34962
- const statements = [];
34963
- statements.push(
34964
- new SqliteRenameTableConvertor().convert({
34965
- type: "rename_table",
34966
- tableNameFrom: tableName,
34967
- tableNameTo: `__old_push_${tableName}`,
34968
- fromSchema: "",
34969
- toSchema: ""
34970
- })
34971
- );
34972
- const tableColumns = Object.values(json.tables[tableName].columns);
34973
- const referenceData = Object.values(json.tables[tableName].foreignKeys);
34974
- const compositePKs = Object.values(
34975
- json.tables[tableName].compositePrimaryKeys
34976
- ).map((it) => SQLiteSquasher.unsquashPK(it));
34977
- const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
34978
- statements.push(
34979
- new SQLiteCreateTableConvertor().convert({
34980
- type: "sqlite_create_table",
34981
- tableName,
34982
- columns: tableColumns,
34983
- referenceData: fks,
34984
- compositePKs
34985
- })
34986
- );
34987
- if (!dataLoss) {
34988
- statements.push(
34989
- `INSERT INTO "${tableName}" SELECT * FROM "__old_push_${tableName}";`
34990
- );
34991
- }
34992
- statements.push(
34993
- new SQLiteDropTableConvertor().convert({
34994
- type: "drop_table",
34995
- tableName: `__old_push_${tableName}`,
34996
- schema: ""
34997
- })
34998
- );
34999
- for (const idx of Object.values(json.tables[tableName].indexes)) {
35000
- statements.push(
35001
- new CreateSqliteIndexConvertor().convert({
35002
- type: "create_index",
35003
- tableName,
35004
- schema: "",
35005
- data: idx
35006
- })
35007
- );
35008
- }
35009
- return statements;
35010
- };
35011
- var getOldTableName = (tableName, meta) => {
35012
- for (const key of Object.keys(meta.tables)) {
35013
- const value = meta.tables[key];
35014
- if (`"${tableName}"` === value) {
35015
- return key.substring(1, key.length - 1);
35016
- }
35017
- }
35018
- return tableName;
35019
- };
35020
- var getNewTableName = (tableName, meta) => {
35021
- if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
35022
- return meta.tables[`"${tableName}"`].substring(
35023
- 1,
35024
- meta.tables[`"${tableName}"`].length - 1
35025
- );
35026
- }
35027
- return tableName;
35028
- };
35029
- var logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
35030
- let shouldAskForApprove = false;
35031
- const statementsToExecute = [];
35032
- const infoToPrint = [];
35033
- const tablesToRemove = [];
35034
- const columnsToRemove = [];
35035
- const schemasToRemove = [];
35036
- const tablesToTruncate = [];
35037
- const tablesContext = {};
35038
- for (const statement of statements) {
35039
- if (statement.type === "drop_table") {
35040
- const res = await connection.query(
35041
- `select count(*) as count from \`${statement.tableName}\``
35042
- );
35043
- const count2 = Number(res[0].count);
35044
- if (count2 > 0) {
35045
- infoToPrint.push(
35046
- `\xB7 You're about to delete ${source_default.underline(
35047
- statement.tableName
35048
- )} table with ${count2} items`
35049
- );
35050
- tablesToRemove.push(statement.tableName);
35051
- shouldAskForApprove = true;
35052
- }
35053
- const stmnt = fromJson([statement], "sqlite")[0];
35054
- statementsToExecute.push(stmnt);
35055
- } else if (statement.type === "alter_table_drop_column") {
35056
- const newTableName = getOldTableName(statement.tableName, meta);
35057
- const columnIsPartOfPk = Object.values(
35058
- json1.tables[newTableName].compositePrimaryKeys
35059
- ).find((c) => SQLiteSquasher.unsquashPK(c).includes(statement.columnName));
35060
- const columnIsPartOfIndex = Object.values(
35061
- json1.tables[newTableName].indexes
35062
- ).find((c) => SQLiteSquasher.unsquashIdx(c).columns.includes(statement.columnName));
35063
- const columnIsPk = json1.tables[newTableName].columns[statement.columnName].primaryKey;
35064
- const columnIsPartOfFk = Object.values(
35065
- json1.tables[newTableName].foreignKeys
35066
- ).find(
35067
- (t) => SQLiteSquasher.unsquashPushFK(t).columnsFrom.includes(
35068
- statement.columnName
35069
- )
35070
- );
35071
- const res = await connection.query(
35072
- `select count(*) as count from \`${newTableName}\``
35073
- );
35074
- const count2 = Number(res[0].count);
35075
- if (count2 > 0) {
35076
- infoToPrint.push(
35077
- `\xB7 You're about to delete ${source_default.underline(
35078
- statement.columnName
35079
- )} column in ${newTableName} table with ${count2} items`
35080
- );
35081
- columnsToRemove.push(`${newTableName}_${statement.columnName}`);
35082
- shouldAskForApprove = true;
35083
- }
35084
- if (columnIsPk || columnIsPartOfPk || columnIsPartOfIndex || columnIsPartOfFk) {
35085
- tablesContext[newTableName] = [
35086
- ..._moveDataStatements(statement.tableName, json2, true)
35087
- ];
35088
- const tablesReferncingCurrent = [];
35089
- for (const table4 of Object.values(json1.tables)) {
35090
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35091
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35092
- ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35093
- tablesReferncingCurrent.push(...tablesRefs);
35094
- }
35095
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35096
- for (const table4 of uniqueTableRefs) {
35097
- if (typeof tablesContext[table4] === "undefined") {
35098
- tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35099
- }
35100
- }
35101
- } else {
35102
- if (typeof tablesContext[newTableName] === "undefined") {
35103
- const stmnt = fromJson([statement], "sqlite")[0];
35104
- statementsToExecute.push(stmnt);
35105
- }
35106
- }
35107
- } else if (statement.type === "sqlite_alter_table_add_column") {
35108
- const newTableName = getOldTableName(statement.tableName, meta);
35109
- if (statement.column.notNull && !statement.column.default) {
35110
- const res = await connection.query(
35111
- `select count(*) as count from \`${newTableName}\``
35112
- );
35113
- const count2 = Number(res[0].count);
35114
- if (count2 > 0) {
35115
- infoToPrint.push(
35116
- `\xB7 You're about to add not-null ${source_default.underline(
35117
- statement.column.name
35118
- )} column without default value, which contains ${count2} items`
35119
- );
35120
- tablesToTruncate.push(newTableName);
35121
- statementsToExecute.push(`delete from ${newTableName};`);
35122
- shouldAskForApprove = true;
35123
- }
35124
- }
35125
- if (statement.column.primaryKey) {
35126
- tablesContext[newTableName] = [
35127
- ..._moveDataStatements(statement.tableName, json2, true)
35128
- ];
35129
- const tablesReferncingCurrent = [];
35130
- for (const table4 of Object.values(json1.tables)) {
35131
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35132
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35133
- ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35134
- tablesReferncingCurrent.push(...tablesRefs);
35135
- }
35136
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35137
- for (const table4 of uniqueTableRefs) {
35138
- if (typeof tablesContext[table4] === "undefined") {
35139
- tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35140
- }
35141
- }
35142
- } else {
35143
- if (typeof tablesContext[newTableName] === "undefined") {
35144
- const stmnt = fromJson([statement], "sqlite")[0];
35145
- statementsToExecute.push(stmnt);
35146
- }
35147
- }
35148
- } else if (statement.type === "alter_table_alter_column_set_type" || statement.type === "alter_table_alter_column_set_default" || statement.type === "alter_table_alter_column_drop_default" || statement.type === "alter_table_alter_column_set_notnull" || statement.type === "alter_table_alter_column_drop_notnull" || statement.type === "alter_table_alter_column_drop_autoincrement" || statement.type === "alter_table_alter_column_set_autoincrement" || statement.type === "alter_table_alter_column_drop_pk" || statement.type === "alter_table_alter_column_set_pk") {
35149
- if (!(statement.type === "alter_table_alter_column_set_notnull" && statement.columnPk)) {
35150
- const newTableName = getOldTableName(statement.tableName, meta);
35151
- if (statement.type === "alter_table_alter_column_set_notnull" && typeof statement.columnDefault === "undefined") {
35152
- const res = await connection.query(
35153
- `select count(*) as count from \`${newTableName}\``
35154
- );
35155
- const count2 = Number(res[0].count);
35156
- if (count2 > 0) {
35157
- infoToPrint.push(
35158
- `\xB7 You're about to add not-null constraint to ${source_default.underline(
35159
- statement.columnName
35160
- )} column without default value, which contains ${count2} items`
35161
- );
35162
- tablesToTruncate.push(newTableName);
35163
- shouldAskForApprove = true;
35164
- }
35165
- tablesContext[newTableName] = _moveDataStatements(
35166
- statement.tableName,
35167
- json1,
35168
- true
35169
- );
35170
- } else {
35171
- if (typeof tablesContext[newTableName] === "undefined") {
35172
- tablesContext[newTableName] = _moveDataStatements(
35173
- statement.tableName,
35174
- json1
35175
- );
35176
- }
35177
- }
35178
- const tablesReferncingCurrent = [];
35179
- for (const table4 of Object.values(json1.tables)) {
35180
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35181
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35182
- ).map((t) => {
35183
- return getNewTableName(
35184
- SQLiteSquasher.unsquashPushFK(t).tableFrom,
35185
- meta
35186
- );
35187
- });
35188
- tablesReferncingCurrent.push(...tablesRefs);
35189
- }
35190
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35191
- for (const table4 of uniqueTableRefs) {
35192
- if (typeof tablesContext[table4] === "undefined") {
35193
- tablesContext[table4] = [..._moveDataStatements(table4, json1)];
35194
- }
35195
- }
35196
- }
35197
- } else if (statement.type === "create_reference" || statement.type === "delete_reference" || statement.type === "alter_reference") {
35198
- const fk4 = SQLiteSquasher.unsquashPushFK(statement.data);
35199
- if (typeof tablesContext[statement.tableName] === "undefined") {
35200
- tablesContext[statement.tableName] = _moveDataStatements(
35201
- statement.tableName,
35202
- json2
35203
- );
35204
- }
35205
- } else if (statement.type === "create_composite_pk" || statement.type === "alter_composite_pk" || statement.type === "delete_composite_pk" || statement.type === "create_unique_constraint" || statement.type === "delete_unique_constraint") {
35206
- const newTableName = getOldTableName(statement.tableName, meta);
35207
- if (typeof tablesContext[newTableName] === "undefined") {
35208
- tablesContext[newTableName] = _moveDataStatements(
35209
- statement.tableName,
35210
- json2
35211
- );
35212
- }
35213
- } else {
35214
- const stmnt = fromJson([statement], "sqlite");
35215
- if (typeof stmnt !== "undefined") {
35216
- statementsToExecute.push(...stmnt);
35217
- }
35218
- }
35219
- }
35220
- for (const context of Object.values(tablesContext)) {
35221
- statementsToExecute.push(...context);
35222
- }
35223
- return {
35224
- statementsToExecute,
35225
- shouldAskForApprove,
35226
- infoToPrint,
35227
- columnsToRemove: [...new Set(columnsToRemove)],
35228
- schemasToRemove: [...new Set(schemasToRemove)],
35229
- tablesToTruncate: [...new Set(tablesToTruncate)],
35230
- tablesToRemove: [...new Set(tablesToRemove)]
35231
- };
35232
- };
35233
-
35234
36138
  // src/api.ts
36139
+ init_sqlitePushUtils();
35235
36140
  init_global();
35236
36141
  init_migrationPreparator();
35237
36142
  init_mysqlSchema();
@@ -35464,7 +36369,13 @@ var pushMySQLSchema = async (imports, drizzleInstance, databaseName) => {
35464
36369
  };
35465
36370
  };
35466
36371
  var upPgSnapshot = (snapshot) => {
35467
- return updateUpToV6(snapshot);
36372
+ if (snapshot.version === "5") {
36373
+ return updateUpToV7(updateUpToV6(snapshot));
36374
+ }
36375
+ if (snapshot.version === "6") {
36376
+ return updateUpToV7(snapshot);
36377
+ }
36378
+ return snapshot;
35468
36379
  };
35469
36380
  export {
35470
36381
  generateDrizzleJson,