drizzle-kit 0.24.0-38d6dab → 0.24.0-6386ea9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. package/api.js +411 -1242
  2. package/api.mjs +411 -1242
  3. package/bin.cjs +950 -1796
  4. package/package.json +3 -3
  5. package/utils.js +0 -9
  6. package/utils.mjs +0 -8
package/api.mjs CHANGED
@@ -8346,13 +8346,6 @@ var init_sqliteSchema = __esm({
8346
8346
  function isPgArrayType(sqlType) {
8347
8347
  return sqlType.match(/.*\[\d*\].*|.*\[\].*/g) !== null;
8348
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
- }
8356
8349
  var copy, prepareMigrationMeta, schemaRenameKey, tableRenameKey, columnRenameKey;
8357
8350
  var init_utils = __esm({
8358
8351
  "src/utils.ts"() {
@@ -11462,20 +11455,7 @@ var init_jsonDiffer = __esm({
11462
11455
  });
11463
11456
 
11464
11457
  // src/sqlgenerator.ts
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;
11458
+ 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;
11479
11459
  var init_sqlgenerator = __esm({
11480
11460
  "src/sqlgenerator.ts"() {
11481
11461
  "use strict";
@@ -11991,7 +11971,7 @@ var init_sqlgenerator = __esm({
11991
11971
  }
11992
11972
  convert(statement) {
11993
11973
  const { tableName, oldColumnName, newColumnName } = statement;
11994
- return `ALTER TABLE \`${tableName}\` RENAME COLUMN "${oldColumnName}" TO "${newColumnName}";`;
11974
+ return `ALTER TABLE \`${tableName}\` RENAME COLUMN \`${oldColumnName}\` TO \`${newColumnName}\`;`;
11995
11975
  }
11996
11976
  };
11997
11977
  PgAlterTableDropColumnConvertor = class extends Convertor {
@@ -12093,8 +12073,8 @@ var init_sqlgenerator = __esm({
12093
12073
  }
12094
12074
  };
12095
12075
  SQLiteAlterTableAlterColumnSetTypeConvertor = class extends Convertor {
12096
- can(statement, dialect7, driver2) {
12097
- return statement.type === "alter_table_alter_column_set_type" && dialect7 === "sqlite" && !driver2;
12076
+ can(statement, dialect7) {
12077
+ return statement.type === "alter_table_alter_column_set_type" && dialect7 === "sqlite";
12098
12078
  }
12099
12079
  convert(statement) {
12100
12080
  return `/*
@@ -12405,46 +12385,6 @@ var init_sqlgenerator = __esm({
12405
12385
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY`;
12406
12386
  }
12407
12387
  };
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
- };
12448
12388
  MySqlModifyColumn = class extends Convertor {
12449
12389
  can(statement, dialect7) {
12450
12390
  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";
@@ -12785,8 +12725,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12785
12725
  }
12786
12726
  };
12787
12727
  SqliteAlterTableAlterColumnSetNotNullConvertor = class extends Convertor {
12788
- can(statement, dialect7, driver2) {
12789
- return statement.type === "alter_table_alter_column_set_notnull" && dialect7 === "sqlite" && !driver2;
12728
+ can(statement, dialect7) {
12729
+ return statement.type === "alter_table_alter_column_set_notnull" && dialect7 === "sqlite";
12790
12730
  }
12791
12731
  convert(statement) {
12792
12732
  return `/*
@@ -12800,8 +12740,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12800
12740
  }
12801
12741
  };
12802
12742
  SqliteAlterTableAlterColumnSetAutoincrementConvertor = class extends Convertor {
12803
- can(statement, dialect7, driver2) {
12804
- return statement.type === "alter_table_alter_column_set_autoincrement" && dialect7 === "sqlite" && !driver2;
12743
+ can(statement, dialect7) {
12744
+ return statement.type === "alter_table_alter_column_set_autoincrement" && dialect7 === "sqlite";
12805
12745
  }
12806
12746
  convert(statement) {
12807
12747
  return `/*
@@ -12815,8 +12755,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12815
12755
  }
12816
12756
  };
12817
12757
  SqliteAlterTableAlterColumnDropAutoincrementConvertor = class extends Convertor {
12818
- can(statement, dialect7, driver2) {
12819
- return statement.type === "alter_table_alter_column_drop_autoincrement" && dialect7 === "sqlite" && !driver2;
12758
+ can(statement, dialect7) {
12759
+ return statement.type === "alter_table_alter_column_drop_autoincrement" && dialect7 === "sqlite";
12820
12760
  }
12821
12761
  convert(statement) {
12822
12762
  return `/*
@@ -12840,8 +12780,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12840
12780
  }
12841
12781
  };
12842
12782
  SqliteAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12843
- can(statement, dialect7, driver2) {
12844
- return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "sqlite" && !driver2;
12783
+ can(statement, dialect7) {
12784
+ return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "sqlite";
12845
12785
  }
12846
12786
  convert(statement) {
12847
12787
  return `/*
@@ -12885,8 +12825,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12885
12825
  }
12886
12826
  };
12887
12827
  SqliteCreateForeignKeyConvertor = class extends Convertor {
12888
- can(statement, dialect7, driver2) {
12889
- return statement.type === "create_reference" && dialect7 === "sqlite" && !driver2;
12828
+ can(statement, dialect7) {
12829
+ return statement.type === "create_reference" && dialect7 === "sqlite";
12890
12830
  }
12891
12831
  convert(statement) {
12892
12832
  return `/*
@@ -12898,32 +12838,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12898
12838
  */`;
12899
12839
  }
12900
12840
  };
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
- };
12927
12841
  MySqlCreateForeignKeyConvertor = class extends Convertor {
12928
12842
  can(statement, dialect7) {
12929
12843
  return statement.type === "create_reference" && dialect7 === "mysql";
@@ -12997,8 +12911,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12997
12911
  }
12998
12912
  };
12999
12913
  SqliteDeleteForeignKeyConvertor = class extends Convertor {
13000
- can(statement, dialect7, driver2) {
13001
- return statement.type === "delete_reference" && dialect7 === "sqlite" && !driver2;
12914
+ can(statement, dialect7) {
12915
+ return statement.type === "delete_reference" && dialect7 === "sqlite";
13002
12916
  }
13003
12917
  convert(statement) {
13004
12918
  return `/*
@@ -13010,29 +12924,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13010
12924
  */`;
13011
12925
  }
13012
12926
  };
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
- };
13036
12927
  MySqlDeleteForeignKeyConvertor = class extends Convertor {
13037
12928
  can(statement, dialect7) {
13038
12929
  return statement.type === "delete_reference" && dialect7 === "mysql";
@@ -13196,90 +13087,10 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13196
13087
  return `DROP INDEX \`${name2}\` ON \`${statement.tableName}\`;`;
13197
13088
  }
13198
13089
  };
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
- };
13277
13090
  convertors = [];
13278
13091
  convertors.push(new PgCreateTableConvertor());
13279
13092
  convertors.push(new MySqlCreateTableConvertor());
13280
13093
  convertors.push(new SQLiteCreateTableConvertor());
13281
- convertors.push(new SQLiteRecreateTableConvertor());
13282
- convertors.push(new LibSQLRecreateTableConvertor());
13283
13094
  convertors.push(new CreateTypeEnumConvertor());
13284
13095
  convertors.push(new CreatePgSequenceConvertor());
13285
13096
  convertors.push(new DropPgSequenceConvertor());
@@ -13327,7 +13138,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13327
13138
  convertors.push(new SqliteAlterTableAlterColumnAlterGeneratedConvertor());
13328
13139
  convertors.push(new SqliteAlterTableAlterColumnSetExpressionConvertor());
13329
13140
  convertors.push(new MySqlModifyColumn());
13330
- convertors.push(new LibSQLModifyColumn());
13331
13141
  convertors.push(new PgCreateForeignKeyConvertor());
13332
13142
  convertors.push(new MySqlCreateForeignKeyConvertor());
13333
13143
  convertors.push(new PgAlterForeignKeyConvertor());
@@ -13342,9 +13152,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13342
13152
  convertors.push(new SQLiteAlterTableAlterColumnSetTypeConvertor());
13343
13153
  convertors.push(new SqliteAlterForeignKeyConvertor());
13344
13154
  convertors.push(new SqliteDeleteForeignKeyConvertor());
13345
- convertors.push(new LibSQLDeleteForeignKeyConvertor());
13346
13155
  convertors.push(new SqliteCreateForeignKeyConvertor());
13347
- convertors.push(new LibSQLCreateForeignKeyConvertor());
13348
13156
  convertors.push(new SQLiteAlterTableAddUniqueConstraintConvertor());
13349
13157
  convertors.push(new SQLiteAlterTableDropUniqueConstraintConvertor());
13350
13158
  convertors.push(new PgAlterTableAlterColumnDropGenerated());
@@ -13367,6 +13175,19 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13367
13175
  convertors.push(new MySqlAlterTableCreateCompositePrimaryKeyConvertor());
13368
13176
  convertors.push(new MySqlAlterTableAddPk());
13369
13177
  convertors.push(new MySqlAlterTableAlterCompositePrimaryKeyConvertor());
13178
+ fromJson = (statements, dialect7) => {
13179
+ const result = statements.flatMap((statement) => {
13180
+ const filtered = convertors.filter((it) => {
13181
+ return it.can(statement, dialect7);
13182
+ });
13183
+ const convertor = filtered.length === 1 ? filtered[0] : void 0;
13184
+ if (!convertor) {
13185
+ return "";
13186
+ }
13187
+ return convertor.convert(statement);
13188
+ }).filter((it) => it !== "");
13189
+ return result;
13190
+ };
13370
13191
  https: `
13371
13192
  create table users (
13372
13193
  id int,
@@ -13393,237 +13214,12 @@ drop type __venum;
13393
13214
  }
13394
13215
  });
13395
13216
 
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
-
13620
13217
  // src/jsonStatements.ts
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;
13218
+ 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;
13622
13219
  var init_jsonStatements = __esm({
13623
13220
  "src/jsonStatements.ts"() {
13624
13221
  "use strict";
13625
13222
  init_source();
13626
- init_sqlitePushUtils();
13627
13223
  init_views();
13628
13224
  init_mysqlSchema();
13629
13225
  init_pgSchema();
@@ -14407,49 +14003,6 @@ var init_jsonStatements = __esm({
14407
14003
  const columnPk = json2.tables[tableName].columns[columnName].primaryKey;
14408
14004
  const columnGenerated = json2.tables[tableName].columns[columnName].generated;
14409
14005
  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
- }
14453
14006
  if (typeof column4.name !== "string") {
14454
14007
  statements.push({
14455
14008
  type: "alter_table_rename_column",
@@ -14713,111 +14266,33 @@ var init_jsonStatements = __esm({
14713
14266
  };
14714
14267
  });
14715
14268
  };
14716
- prepareLibSQLCreateReferencesJson = (tableName, schema4, foreignKeys, json2, action) => {
14269
+ prepareDropReferencesJson = (tableName, schema4, foreignKeys) => {
14717
14270
  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];
14736
14271
  return {
14737
- type: "create_reference",
14272
+ type: "delete_reference",
14738
14273
  tableName,
14739
14274
  data: fkData,
14740
- schema: schema4,
14741
- columnNotNull,
14742
- columnDefault,
14743
- columnType
14275
+ schema: schema4
14744
14276
  };
14745
14277
  });
14746
14278
  };
14747
- prepareDropReferencesJson = (tableName, schema4, foreignKeys) => {
14748
- return Object.values(foreignKeys).map((fkData) => {
14749
- return {
14279
+ prepareAlterReferencesJson = (tableName, schema4, foreignKeys) => {
14280
+ const stmts = [];
14281
+ Object.values(foreignKeys).map((val) => {
14282
+ stmts.push({
14750
14283
  type: "delete_reference",
14751
14284
  tableName,
14752
- data: fkData,
14753
- schema: schema4
14754
- };
14285
+ schema: schema4,
14286
+ data: val.__old
14287
+ });
14288
+ stmts.push({
14289
+ type: "create_reference",
14290
+ tableName,
14291
+ schema: schema4,
14292
+ data: val.__new
14293
+ });
14755
14294
  });
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;
14295
+ return stmts;
14821
14296
  };
14822
14297
  prepareDropIndexesJson = (tableName, schema4, indexes) => {
14823
14298
  return Object.values(indexes).map((indexData) => {
@@ -14954,298 +14429,10 @@ var init_jsonStatements = __esm({
14954
14429
  }
14955
14430
  });
14956
14431
 
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
-
15244
14432
  // src/snapshotsDiffer.ts
15245
14433
  var snapshotsDiffer_exports = {};
15246
14434
  __export(snapshotsDiffer_exports, {
15247
14435
  alteredTableScheme: () => alteredTableScheme,
15248
- applyLibSQLSnapshotsDiff: () => applyLibSQLSnapshotsDiff,
15249
14436
  applyMysqlSnapshotsDiff: () => applyMysqlSnapshotsDiff,
15250
14437
  applyPgSnapshotsDiff: () => applyPgSnapshotsDiff,
15251
14438
  applySqliteSnapshotsDiff: () => applySqliteSnapshotsDiff,
@@ -15255,7 +14442,7 @@ __export(snapshotsDiffer_exports, {
15255
14442
  makePatched: () => makePatched,
15256
14443
  makeSelfOrPatched: () => makeSelfOrPatched
15257
14444
  });
15258
- var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff, applyLibSQLSnapshotsDiff;
14445
+ var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff;
15259
14446
  var init_snapshotsDiffer = __esm({
15260
14447
  "src/snapshotsDiffer.ts"() {
15261
14448
  "use strict";
@@ -15267,7 +14454,6 @@ var init_snapshotsDiffer = __esm({
15267
14454
  init_mysqlSchema();
15268
14455
  init_pgSchema();
15269
14456
  init_sqliteSchema();
15270
- init_statementCombiner();
15271
14457
  init_utils();
15272
14458
  makeChanged = (schema4) => {
15273
14459
  return objectType({
@@ -16141,352 +15327,53 @@ var init_snapshotsDiffer = __esm({
16141
15327
  curFull.internal
16142
15328
  );
16143
15329
  }).flat();
16144
- const jsonDropTables = deletedTables.map((it) => {
16145
- return prepareDropTableJson(it);
16146
- });
16147
- const jsonRenameTables = renamedTables.map((it) => {
16148
- return prepareRenameTableJson(it.from, it.to);
16149
- });
16150
- const alteredTables = typedResult.alteredTablesWithColumns;
16151
- const jsonAddedCompositePKs = [];
16152
- const jsonDeletedCompositePKs = [];
16153
- const jsonAlteredCompositePKs = [];
16154
- const jsonAddedUniqueConstraints = [];
16155
- const jsonDeletedUniqueConstraints = [];
16156
- const jsonAlteredUniqueConstraints = [];
16157
- const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
16158
- const jsonAddColumnsStatemets = columnCreates.map((it) => _prepareAddColumns(it.table, "", it.columns)).flat();
16159
- const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
16160
- alteredTables.forEach((it) => {
16161
- let addedColumns = [];
16162
- for (const addedPkName of Object.keys(it.addedCompositePKs)) {
16163
- const addedPkColumns = it.addedCompositePKs[addedPkName];
16164
- addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
16165
- }
16166
- let deletedColumns = [];
16167
- for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
16168
- const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
16169
- deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
16170
- }
16171
- const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
16172
- let addedCompositePKs = [];
16173
- let deletedCompositePKs = [];
16174
- let alteredCompositePKs = [];
16175
- addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
16176
- it.name,
16177
- it.addedCompositePKs,
16178
- prevFull,
16179
- curFull
16180
- );
16181
- deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
16182
- it.name,
16183
- it.deletedCompositePKs,
16184
- prevFull
16185
- );
16186
- alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
16187
- it.name,
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;
15330
+ const jsonDropTables = deletedTables.map((it) => {
15331
+ return prepareDropTableJson(it);
15332
+ });
15333
+ const jsonRenameTables = renamedTables.map((it) => {
15334
+ return prepareRenameTableJson(it.from, it.to);
15335
+ });
15336
+ const alteredTables = typedResult.alteredTablesWithColumns;
16456
15337
  const jsonAddedCompositePKs = [];
16457
15338
  const jsonDeletedCompositePKs = [];
16458
15339
  const jsonAlteredCompositePKs = [];
16459
15340
  const jsonAddedUniqueConstraints = [];
16460
15341
  const jsonDeletedUniqueConstraints = [];
16461
15342
  const jsonAlteredUniqueConstraints = [];
16462
- allAltered.forEach((it) => {
15343
+ const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
15344
+ const jsonAddColumnsStatemets = columnCreates.map((it) => _prepareAddColumns(it.table, "", it.columns)).flat();
15345
+ const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
15346
+ alteredTables.forEach((it) => {
16463
15347
  let addedColumns = [];
16464
15348
  for (const addedPkName of Object.keys(it.addedCompositePKs)) {
16465
15349
  const addedPkColumns = it.addedCompositePKs[addedPkName];
16466
- addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
15350
+ addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
16467
15351
  }
16468
15352
  let deletedColumns = [];
16469
15353
  for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
16470
15354
  const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
16471
- deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
15355
+ deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
16472
15356
  }
16473
15357
  const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
16474
15358
  let addedCompositePKs = [];
16475
15359
  let deletedCompositePKs = [];
16476
15360
  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(
15361
+ addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
16488
15362
  it.name,
16489
- it.alteredCompositePKs
15363
+ it.addedCompositePKs,
15364
+ prevFull,
15365
+ curFull
15366
+ );
15367
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15368
+ it.name,
15369
+ it.deletedCompositePKs,
15370
+ prevFull
15371
+ );
15372
+ alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
15373
+ it.name,
15374
+ it.alteredCompositePKs,
15375
+ prevFull,
15376
+ curFull
16490
15377
  );
16491
15378
  let addedUniqueConstraints = [];
16492
15379
  let deletedUniqueConstraints = [];
@@ -16530,10 +15417,17 @@ var init_snapshotsDiffer = __esm({
16530
15417
  to: { schema: schema4, table: tableName, column: it.newColumnName }
16531
15418
  };
16532
15419
  });
16533
- const jsonTableAlternations = allAltered.map((it) => {
16534
- return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15420
+ const jsonTableAlternations = alteredTables.map((it) => {
15421
+ return prepareAlterColumnsMysql(
15422
+ it.name,
15423
+ it.schema,
15424
+ it.altered,
15425
+ json1,
15426
+ json2,
15427
+ action
15428
+ );
16535
15429
  }).flat();
16536
- const jsonCreateIndexesForAllAlteredTables = allAltered.map((it) => {
15430
+ const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
16537
15431
  return prepareCreateIndexesJson(
16538
15432
  it.name,
16539
15433
  it.schema,
@@ -16541,14 +15435,14 @@ var init_snapshotsDiffer = __esm({
16541
15435
  curFull.internal
16542
15436
  );
16543
15437
  }).flat();
16544
- const jsonDropIndexesForAllAlteredTables = allAltered.map((it) => {
15438
+ const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16545
15439
  return prepareDropIndexesJson(
16546
15440
  it.name,
16547
15441
  it.schema,
16548
15442
  it.deletedIndexes || {}
16549
15443
  );
16550
15444
  }).flat();
16551
- allAltered.forEach((it) => {
15445
+ alteredTables.forEach((it) => {
16552
15446
  const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
16553
15447
  (current, item) => {
16554
15448
  current[item] = it.alteredIndexes[item].__old;
@@ -16564,18 +15458,16 @@ var init_snapshotsDiffer = __esm({
16564
15458
  {}
16565
15459
  );
16566
15460
  jsonCreateIndexesForAllAlteredTables.push(
16567
- ...prepareCreateIndexesJson(
16568
- it.name,
16569
- it.schema,
16570
- createdIndexes || {},
16571
- curFull.internal
16572
- )
15461
+ ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16573
15462
  );
16574
15463
  jsonDropIndexesForAllAlteredTables.push(
16575
15464
  ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
16576
15465
  );
16577
15466
  });
16578
- const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15467
+ const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
15468
+ return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
15469
+ }).flat();
15470
+ const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16579
15471
  const forAdded = prepareCreateReferencesJson(
16580
15472
  it.name,
16581
15473
  it.schema,
@@ -16599,25 +15491,35 @@ var init_snapshotsDiffer = __esm({
16599
15491
  const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16600
15492
  (t) => t.type === "delete_reference"
16601
15493
  );
16602
- const jsonStatements = [];
16603
- jsonStatements.push(...jsonCreateTables);
15494
+ const jsonMySqlCreateTables = createdTables.map((it) => {
15495
+ return prepareMySqlCreateTableJson(
15496
+ it,
15497
+ curFull,
15498
+ curFull.internal
15499
+ );
15500
+ });
15501
+ jsonStatements.push(...jsonMySqlCreateTables);
16604
15502
  jsonStatements.push(...jsonDropTables);
16605
15503
  jsonStatements.push(...jsonRenameTables);
16606
15504
  jsonStatements.push(...jsonRenameColumnsStatements);
15505
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16607
15506
  jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
16608
15507
  jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
16609
15508
  jsonStatements.push(...jsonDeletedCompositePKs);
16610
15509
  jsonStatements.push(...jsonTableAlternations);
16611
15510
  jsonStatements.push(...jsonAddedCompositePKs);
15511
+ jsonStatements.push(...jsonAddedUniqueConstraints);
15512
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16612
15513
  jsonStatements.push(...jsonAddColumnsStatemets);
15514
+ jsonStatements.push(...jsonCreateReferencesForCreatedTables);
16613
15515
  jsonStatements.push(...jsonCreateIndexesForCreatedTables);
16614
- jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16615
15516
  jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15517
+ jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16616
15518
  jsonStatements.push(...jsonDropColumnsStatemets);
16617
15519
  jsonStatements.push(...jsonAlteredCompositePKs);
15520
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16618
15521
  jsonStatements.push(...jsonAlteredUniqueConstraints);
16619
- const combinedJsonStatements = sqliteCombineStatements(jsonStatements, json2, action);
16620
- const sqlStatements = fromJson(combinedJsonStatements, "sqlite");
15522
+ const sqlStatements = fromJson(jsonStatements, "mysql");
16621
15523
  const uniqueSqlStatements = [];
16622
15524
  sqlStatements.forEach((ss) => {
16623
15525
  if (!uniqueSqlStatements.includes(ss)) {
@@ -16629,12 +15531,12 @@ var init_snapshotsDiffer = __esm({
16629
15531
  });
16630
15532
  const _meta = prepareMigrationMeta([], rTables, rColumns);
16631
15533
  return {
16632
- statements: combinedJsonStatements,
15534
+ statements: jsonStatements,
16633
15535
  sqlStatements: uniqueSqlStatements,
16634
15536
  _meta
16635
15537
  };
16636
15538
  };
16637
- applyLibSQLSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
15539
+ applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16638
15540
  const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
16639
15541
  const {
16640
15542
  created: createdTables,
@@ -16736,18 +15638,6 @@ var init_snapshotsDiffer = __esm({
16736
15638
  tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
16737
15639
  );
16738
15640
  }).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);
16751
15641
  const allAltered = typedResult.alteredTablesWithColumns;
16752
15642
  const jsonAddedCompositePKs = [];
16753
15643
  const jsonDeletedCompositePKs = [];
@@ -16818,6 +15708,14 @@ var init_snapshotsDiffer = __esm({
16818
15708
  jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
16819
15709
  jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
16820
15710
  });
15711
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
15712
+ const tableName = it.tableName;
15713
+ const schema4 = it.schema;
15714
+ return {
15715
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
15716
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
15717
+ };
15718
+ });
16821
15719
  const jsonTableAlternations = allAltered.map((it) => {
16822
15720
  return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
16823
15721
  }).flat();
@@ -16864,22 +15762,21 @@ var init_snapshotsDiffer = __esm({
16864
15762
  );
16865
15763
  });
16866
15764
  const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
16867
- const forAdded = prepareLibSQLCreateReferencesJson(
15765
+ const forAdded = prepareCreateReferencesJson(
16868
15766
  it.name,
16869
15767
  it.schema,
16870
- it.addedForeignKeys,
16871
- json2,
16872
- action
15768
+ it.addedForeignKeys
16873
15769
  );
16874
- const forAltered = prepareLibSQLDropReferencesJson(
15770
+ const forAltered = prepareDropReferencesJson(
16875
15771
  it.name,
16876
15772
  it.schema,
16877
- it.deletedForeignKeys,
16878
- json2,
16879
- _meta,
16880
- action
15773
+ it.deletedForeignKeys
15774
+ );
15775
+ const alteredFKs = prepareAlterReferencesJson(
15776
+ it.name,
15777
+ it.schema,
15778
+ it.alteredForeignKeys
16881
15779
  );
16882
- const alteredFKs = prepareAlterReferencesJson(it.name, it.schema, it.alteredForeignKeys);
16883
15780
  return [...forAdded, ...forAltered, ...alteredFKs];
16884
15781
  }).flat();
16885
15782
  const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
@@ -16905,22 +15802,19 @@ var init_snapshotsDiffer = __esm({
16905
15802
  jsonStatements.push(...jsonDropColumnsStatemets);
16906
15803
  jsonStatements.push(...jsonAlteredCompositePKs);
16907
15804
  jsonStatements.push(...jsonAlteredUniqueConstraints);
16908
- const combinedJsonStatements = libSQLCombineStatements(jsonStatements, json2, action);
16909
- const sqlStatements = fromJson(
16910
- combinedJsonStatements,
16911
- "sqlite",
16912
- action,
16913
- "turso",
16914
- json2
16915
- );
15805
+ const sqlStatements = fromJson(jsonStatements, "sqlite");
16916
15806
  const uniqueSqlStatements = [];
16917
15807
  sqlStatements.forEach((ss) => {
16918
15808
  if (!uniqueSqlStatements.includes(ss)) {
16919
15809
  uniqueSqlStatements.push(ss);
16920
15810
  }
16921
15811
  });
15812
+ const rTables = renamedTables.map((it) => {
15813
+ return { from: it.from, to: it.to };
15814
+ });
15815
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
16922
15816
  return {
16923
- statements: combinedJsonStatements,
15817
+ statements: jsonStatements,
16924
15818
  sqlStatements: uniqueSqlStatements,
16925
15819
  _meta
16926
15820
  };
@@ -34982,8 +33876,7 @@ var init_cli = __esm({
34982
33876
  schemaFilter: unionType([stringType(), stringType().array()]).optional().default(["public"]),
34983
33877
  extensionsFilters: literalType("postgis").array().optional(),
34984
33878
  verbose: booleanType().optional(),
34985
- strict: booleanType().optional(),
34986
- driver: driver.optional()
33879
+ strict: booleanType().optional()
34987
33880
  }).passthrough();
34988
33881
  pullParams = objectType({
34989
33882
  config: stringType().optional(),
@@ -36135,8 +35028,284 @@ var sqlitePushIntrospect = async (db, filters) => {
36135
35028
  return { schema: schema4 };
36136
35029
  };
36137
35030
 
35031
+ // src/cli/commands/sqlitePushUtils.ts
35032
+ init_source();
35033
+ init_sqliteSchema();
35034
+ init_sqlgenerator();
35035
+ var _moveDataStatements = (tableName, json, dataLoss = false) => {
35036
+ const statements = [];
35037
+ statements.push(
35038
+ new SqliteRenameTableConvertor().convert({
35039
+ type: "rename_table",
35040
+ tableNameFrom: tableName,
35041
+ tableNameTo: `__old_push_${tableName}`,
35042
+ fromSchema: "",
35043
+ toSchema: ""
35044
+ })
35045
+ );
35046
+ const tableColumns = Object.values(json.tables[tableName].columns);
35047
+ const referenceData = Object.values(json.tables[tableName].foreignKeys);
35048
+ const compositePKs = Object.values(
35049
+ json.tables[tableName].compositePrimaryKeys
35050
+ ).map((it) => SQLiteSquasher.unsquashPK(it));
35051
+ const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
35052
+ statements.push(
35053
+ new SQLiteCreateTableConvertor().convert({
35054
+ type: "sqlite_create_table",
35055
+ tableName,
35056
+ columns: tableColumns,
35057
+ referenceData: fks,
35058
+ compositePKs
35059
+ })
35060
+ );
35061
+ if (!dataLoss) {
35062
+ statements.push(
35063
+ `INSERT INTO "${tableName}" SELECT * FROM "__old_push_${tableName}";`
35064
+ );
35065
+ }
35066
+ statements.push(
35067
+ new SQLiteDropTableConvertor().convert({
35068
+ type: "drop_table",
35069
+ tableName: `__old_push_${tableName}`,
35070
+ schema: ""
35071
+ })
35072
+ );
35073
+ for (const idx of Object.values(json.tables[tableName].indexes)) {
35074
+ statements.push(
35075
+ new CreateSqliteIndexConvertor().convert({
35076
+ type: "create_index",
35077
+ tableName,
35078
+ schema: "",
35079
+ data: idx
35080
+ })
35081
+ );
35082
+ }
35083
+ return statements;
35084
+ };
35085
+ var getOldTableName = (tableName, meta) => {
35086
+ for (const key of Object.keys(meta.tables)) {
35087
+ const value = meta.tables[key];
35088
+ if (`"${tableName}"` === value) {
35089
+ return key.substring(1, key.length - 1);
35090
+ }
35091
+ }
35092
+ return tableName;
35093
+ };
35094
+ var getNewTableName = (tableName, meta) => {
35095
+ if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
35096
+ return meta.tables[`"${tableName}"`].substring(
35097
+ 1,
35098
+ meta.tables[`"${tableName}"`].length - 1
35099
+ );
35100
+ }
35101
+ return tableName;
35102
+ };
35103
+ var logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
35104
+ let shouldAskForApprove = false;
35105
+ const statementsToExecute = [];
35106
+ const infoToPrint = [];
35107
+ const tablesToRemove = [];
35108
+ const columnsToRemove = [];
35109
+ const schemasToRemove = [];
35110
+ const tablesToTruncate = [];
35111
+ const tablesContext = {};
35112
+ for (const statement of statements) {
35113
+ if (statement.type === "drop_table") {
35114
+ const res = await connection.query(
35115
+ `select count(*) as count from \`${statement.tableName}\``
35116
+ );
35117
+ const count2 = Number(res[0].count);
35118
+ if (count2 > 0) {
35119
+ infoToPrint.push(
35120
+ `\xB7 You're about to delete ${source_default.underline(
35121
+ statement.tableName
35122
+ )} table with ${count2} items`
35123
+ );
35124
+ tablesToRemove.push(statement.tableName);
35125
+ shouldAskForApprove = true;
35126
+ }
35127
+ const stmnt = fromJson([statement], "sqlite")[0];
35128
+ statementsToExecute.push(stmnt);
35129
+ } else if (statement.type === "alter_table_drop_column") {
35130
+ const newTableName = getOldTableName(statement.tableName, meta);
35131
+ const columnIsPartOfPk = Object.values(
35132
+ json1.tables[newTableName].compositePrimaryKeys
35133
+ ).find((c) => SQLiteSquasher.unsquashPK(c).includes(statement.columnName));
35134
+ const columnIsPartOfIndex = Object.values(
35135
+ json1.tables[newTableName].indexes
35136
+ ).find((c) => SQLiteSquasher.unsquashIdx(c).columns.includes(statement.columnName));
35137
+ const columnIsPk = json1.tables[newTableName].columns[statement.columnName].primaryKey;
35138
+ const columnIsPartOfFk = Object.values(
35139
+ json1.tables[newTableName].foreignKeys
35140
+ ).find(
35141
+ (t) => SQLiteSquasher.unsquashPushFK(t).columnsFrom.includes(
35142
+ statement.columnName
35143
+ )
35144
+ );
35145
+ const res = await connection.query(
35146
+ `select count(*) as count from \`${newTableName}\``
35147
+ );
35148
+ const count2 = Number(res[0].count);
35149
+ if (count2 > 0) {
35150
+ infoToPrint.push(
35151
+ `\xB7 You're about to delete ${source_default.underline(
35152
+ statement.columnName
35153
+ )} column in ${newTableName} table with ${count2} items`
35154
+ );
35155
+ columnsToRemove.push(`${newTableName}_${statement.columnName}`);
35156
+ shouldAskForApprove = true;
35157
+ }
35158
+ if (columnIsPk || columnIsPartOfPk || columnIsPartOfIndex || columnIsPartOfFk) {
35159
+ tablesContext[newTableName] = [
35160
+ ..._moveDataStatements(statement.tableName, json2, true)
35161
+ ];
35162
+ const tablesReferncingCurrent = [];
35163
+ for (const table4 of Object.values(json1.tables)) {
35164
+ const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35165
+ (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35166
+ ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35167
+ tablesReferncingCurrent.push(...tablesRefs);
35168
+ }
35169
+ const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35170
+ for (const table4 of uniqueTableRefs) {
35171
+ if (typeof tablesContext[table4] === "undefined") {
35172
+ tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35173
+ }
35174
+ }
35175
+ } else {
35176
+ if (typeof tablesContext[newTableName] === "undefined") {
35177
+ const stmnt = fromJson([statement], "sqlite")[0];
35178
+ statementsToExecute.push(stmnt);
35179
+ }
35180
+ }
35181
+ } else if (statement.type === "sqlite_alter_table_add_column") {
35182
+ const newTableName = getOldTableName(statement.tableName, meta);
35183
+ if (statement.column.notNull && !statement.column.default) {
35184
+ const res = await connection.query(
35185
+ `select count(*) as count from \`${newTableName}\``
35186
+ );
35187
+ const count2 = Number(res[0].count);
35188
+ if (count2 > 0) {
35189
+ infoToPrint.push(
35190
+ `\xB7 You're about to add not-null ${source_default.underline(
35191
+ statement.column.name
35192
+ )} column without default value, which contains ${count2} items`
35193
+ );
35194
+ tablesToTruncate.push(newTableName);
35195
+ statementsToExecute.push(`delete from ${newTableName};`);
35196
+ shouldAskForApprove = true;
35197
+ }
35198
+ }
35199
+ if (statement.column.primaryKey) {
35200
+ tablesContext[newTableName] = [
35201
+ ..._moveDataStatements(statement.tableName, json2, true)
35202
+ ];
35203
+ const tablesReferncingCurrent = [];
35204
+ for (const table4 of Object.values(json1.tables)) {
35205
+ const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35206
+ (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35207
+ ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35208
+ tablesReferncingCurrent.push(...tablesRefs);
35209
+ }
35210
+ const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35211
+ for (const table4 of uniqueTableRefs) {
35212
+ if (typeof tablesContext[table4] === "undefined") {
35213
+ tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35214
+ }
35215
+ }
35216
+ } else {
35217
+ if (typeof tablesContext[newTableName] === "undefined") {
35218
+ const stmnt = fromJson([statement], "sqlite")[0];
35219
+ statementsToExecute.push(stmnt);
35220
+ }
35221
+ }
35222
+ } 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") {
35223
+ if (!(statement.type === "alter_table_alter_column_set_notnull" && statement.columnPk)) {
35224
+ const newTableName = getOldTableName(statement.tableName, meta);
35225
+ if (statement.type === "alter_table_alter_column_set_notnull" && typeof statement.columnDefault === "undefined") {
35226
+ const res = await connection.query(
35227
+ `select count(*) as count from \`${newTableName}\``
35228
+ );
35229
+ const count2 = Number(res[0].count);
35230
+ if (count2 > 0) {
35231
+ infoToPrint.push(
35232
+ `\xB7 You're about to add not-null constraint to ${source_default.underline(
35233
+ statement.columnName
35234
+ )} column without default value, which contains ${count2} items`
35235
+ );
35236
+ tablesToTruncate.push(newTableName);
35237
+ shouldAskForApprove = true;
35238
+ }
35239
+ tablesContext[newTableName] = _moveDataStatements(
35240
+ statement.tableName,
35241
+ json1,
35242
+ true
35243
+ );
35244
+ } else {
35245
+ if (typeof tablesContext[newTableName] === "undefined") {
35246
+ tablesContext[newTableName] = _moveDataStatements(
35247
+ statement.tableName,
35248
+ json1
35249
+ );
35250
+ }
35251
+ }
35252
+ const tablesReferncingCurrent = [];
35253
+ for (const table4 of Object.values(json1.tables)) {
35254
+ const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35255
+ (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35256
+ ).map((t) => {
35257
+ return getNewTableName(
35258
+ SQLiteSquasher.unsquashPushFK(t).tableFrom,
35259
+ meta
35260
+ );
35261
+ });
35262
+ tablesReferncingCurrent.push(...tablesRefs);
35263
+ }
35264
+ const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35265
+ for (const table4 of uniqueTableRefs) {
35266
+ if (typeof tablesContext[table4] === "undefined") {
35267
+ tablesContext[table4] = [..._moveDataStatements(table4, json1)];
35268
+ }
35269
+ }
35270
+ }
35271
+ } else if (statement.type === "create_reference" || statement.type === "delete_reference" || statement.type === "alter_reference") {
35272
+ const fk4 = SQLiteSquasher.unsquashPushFK(statement.data);
35273
+ if (typeof tablesContext[statement.tableName] === "undefined") {
35274
+ tablesContext[statement.tableName] = _moveDataStatements(
35275
+ statement.tableName,
35276
+ json2
35277
+ );
35278
+ }
35279
+ } 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") {
35280
+ const newTableName = getOldTableName(statement.tableName, meta);
35281
+ if (typeof tablesContext[newTableName] === "undefined") {
35282
+ tablesContext[newTableName] = _moveDataStatements(
35283
+ statement.tableName,
35284
+ json2
35285
+ );
35286
+ }
35287
+ } else {
35288
+ const stmnt = fromJson([statement], "sqlite");
35289
+ if (typeof stmnt !== "undefined") {
35290
+ statementsToExecute.push(...stmnt);
35291
+ }
35292
+ }
35293
+ }
35294
+ for (const context of Object.values(tablesContext)) {
35295
+ statementsToExecute.push(...context);
35296
+ }
35297
+ return {
35298
+ statementsToExecute,
35299
+ shouldAskForApprove,
35300
+ infoToPrint,
35301
+ columnsToRemove: [...new Set(columnsToRemove)],
35302
+ schemasToRemove: [...new Set(schemasToRemove)],
35303
+ tablesToTruncate: [...new Set(tablesToTruncate)],
35304
+ tablesToRemove: [...new Set(tablesToRemove)]
35305
+ };
35306
+ };
35307
+
36138
35308
  // src/api.ts
36139
- init_sqlitePushUtils();
36140
35309
  init_global();
36141
35310
  init_migrationPreparator();
36142
35311
  init_mysqlSchema();