drizzle-kit 0.24.0-6386ea9 → 0.24.0-bb966f7

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 +1219 -656
  2. package/api.mjs +1219 -656
  3. package/bin.cjs +1790 -1200
  4. package/package.json +4 -4
  5. package/utils.js +9 -0
  6. package/utils.mjs +8 -0
package/api.mjs CHANGED
@@ -8346,6 +8346,13 @@ 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
+ }
8349
8356
  var copy, prepareMigrationMeta, schemaRenameKey, tableRenameKey, columnRenameKey;
8350
8357
  var init_utils = __esm({
8351
8358
  "src/utils.ts"() {
@@ -11455,7 +11462,20 @@ var init_jsonDiffer = __esm({
11455
11462
  });
11456
11463
 
11457
11464
  // src/sqlgenerator.ts
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;
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, CreatePgSequenceConvertor, DropPgSequenceConvertor, RenamePgSequenceConvertor, MovePgSequenceConvertor, AlterPgSequenceConvertor, CreateTypeEnumConvertor, AlterTypeAddValueConvertor, PgDropTableConvertor, MySQLDropTableConvertor, SQLiteDropTableConvertor, PgRenameTableConvertor, SqliteRenameTableConvertor, MySqlRenameTableConvertor, PgAlterTableRenameColumnConvertor, MySqlAlterTableRenameColumnConvertor, SQLiteAlterTableRenameColumnConvertor, PgAlterTableDropColumnConvertor, MySqlAlterTableDropColumnConvertor, SQLiteAlterTableDropColumnConvertor, PgAlterTableAddColumnConvertor, MySqlAlterTableAddColumnConvertor, SQLiteAlterTableAddColumnConvertor, PgAlterTableAlterColumnSetTypeConvertor, PgAlterTableAlterColumnSetDefaultConvertor, PgAlterTableAlterColumnDropDefaultConvertor, PgAlterTableAlterColumnDropGeneratedConvertor, PgAlterTableAlterColumnSetExpressionConvertor, PgAlterTableAlterColumnAlterrGeneratedConvertor, SqliteAlterTableAlterColumnDropGeneratedConvertor, SqliteAlterTableAlterColumnSetExpressionConvertor, SqliteAlterTableAlterColumnAlterGeneratedConvertor, MySqlAlterTableAlterColumnAlterrGeneratedConvertor, MySqlAlterTableAddPk, MySqlAlterTableDropPk, LibSQLModifyColumn, MySqlModifyColumn, PgAlterTableCreateCompositePrimaryKeyConvertor, PgAlterTableDeleteCompositePrimaryKeyConvertor, PgAlterTableAlterCompositePrimaryKeyConvertor, MySqlAlterTableCreateCompositePrimaryKeyConvertor, MySqlAlterTableDeleteCompositePrimaryKeyConvertor, MySqlAlterTableAlterCompositePrimaryKeyConvertor, PgAlterTableAlterColumnSetPrimaryKeyConvertor, PgAlterTableAlterColumnDropPrimaryKeyConvertor, PgAlterTableAlterColumnSetNotNullConvertor, PgAlterTableAlterColumnDropNotNullConvertor, PgCreateForeignKeyConvertor, LibSQLCreateForeignKeyConvertor, MySqlCreateForeignKeyConvertor, PgAlterForeignKeyConvertor, PgDeleteForeignKeyConvertor, MySqlDeleteForeignKeyConvertor, CreatePgIndexConvertor, CreateMySqlIndexConvertor, CreateSqliteIndexConvertor, PgDropIndexConvertor, PgCreateSchemaConvertor, PgRenameSchemaConvertor, PgDropSchemaConvertor, PgAlterTableSetSchemaConvertor, PgAlterTableSetNewSchemaConvertor, PgAlterTableRemoveFromSchemaConvertor, SqliteDropIndexConvertor, MySqlDropIndexConvertor, SQLiteRecreateTableConvertor, LibSQLRecreateTableConvertor, convertors;
11459
11479
  var init_sqlgenerator = __esm({
11460
11480
  "src/sqlgenerator.ts"() {
11461
11481
  "use strict";
@@ -11777,32 +11797,6 @@ var init_sqlgenerator = __esm({
11777
11797
  return `ALTER TABLE \`${statement.tableName}\` DROP INDEX \`${unsquashed.name}\`;`;
11778
11798
  }
11779
11799
  };
11780
- SQLiteAlterTableAddUniqueConstraintConvertor = class extends Convertor {
11781
- can(statement, dialect7) {
11782
- return statement.type === "create_unique_constraint" && dialect7 === "sqlite";
11783
- }
11784
- convert(statement) {
11785
- return `/*
11786
- SQLite does not support "Adding unique constraint to an existing table" out of the box, we do not generate automatic migration for that, so it has to be done manually
11787
- Please refer to: https://www.techonthenet.com/sqlite/unique.php
11788
-
11789
- Due to that we don't generate migration automatically and it has to be done manually
11790
- */`;
11791
- }
11792
- };
11793
- SQLiteAlterTableDropUniqueConstraintConvertor = class extends Convertor {
11794
- can(statement, dialect7) {
11795
- return statement.type === "delete_unique_constraint" && dialect7 === "sqlite";
11796
- }
11797
- convert(statement) {
11798
- return `/*
11799
- SQLite does not support "Dropping unique constraint from an existing table" out of the box, we do not generate automatic migration for that, so it has to be done manually
11800
- Please refer to: https://www.techonthenet.com/sqlite/unique.php
11801
-
11802
- Due to that we don't generate migration automatically and it has to be done manually
11803
- */`;
11804
- }
11805
- };
11806
11800
  CreatePgSequenceConvertor = class extends Convertor {
11807
11801
  can(statement, dialect7) {
11808
11802
  return statement.type === "create_sequence" && dialect7 === "postgresql";
@@ -11971,7 +11965,7 @@ var init_sqlgenerator = __esm({
11971
11965
  }
11972
11966
  convert(statement) {
11973
11967
  const { tableName, oldColumnName, newColumnName } = statement;
11974
- return `ALTER TABLE \`${tableName}\` RENAME COLUMN \`${oldColumnName}\` TO \`${newColumnName}\`;`;
11968
+ return `ALTER TABLE \`${tableName}\` RENAME COLUMN "${oldColumnName}" TO "${newColumnName}";`;
11975
11969
  }
11976
11970
  };
11977
11971
  PgAlterTableDropColumnConvertor = class extends Convertor {
@@ -12072,21 +12066,6 @@ var init_sqlgenerator = __esm({
12072
12066
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DATA TYPE ${newDataType};`;
12073
12067
  }
12074
12068
  };
12075
- SQLiteAlterTableAlterColumnSetTypeConvertor = class extends Convertor {
12076
- can(statement, dialect7) {
12077
- return statement.type === "alter_table_alter_column_set_type" && dialect7 === "sqlite";
12078
- }
12079
- convert(statement) {
12080
- return `/*
12081
- SQLite does not support "Changing existing column type" out of the box, we do not generate automatic migration for that, so it has to be done manually
12082
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12083
- https://www.sqlite.org/lang_altertable.html
12084
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12085
-
12086
- Due to that we don't generate migration automatically and it has to be done manually
12087
- */`;
12088
- }
12089
- };
12090
12069
  PgAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
12091
12070
  can(statement, dialect7) {
12092
12071
  return statement.type === "alter_table_alter_column_set_default" && dialect7 === "postgresql";
@@ -12097,21 +12076,6 @@ var init_sqlgenerator = __esm({
12097
12076
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DEFAULT ${statement.newDefaultValue};`;
12098
12077
  }
12099
12078
  };
12100
- SqliteAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
12101
- can(statement, dialect7) {
12102
- return statement.type === "alter_table_alter_column_set_default" && dialect7 === "sqlite";
12103
- }
12104
- convert(statement) {
12105
- return `/*
12106
- SQLite does not support "Set default to column" out of the box, we do not generate automatic migration for that, so it has to be done manually
12107
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12108
- https://www.sqlite.org/lang_altertable.html
12109
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12110
-
12111
- Due to that we don't generate migration automatically and it has to be done manually
12112
- */`;
12113
- }
12114
- };
12115
12079
  PgAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
12116
12080
  can(statement, dialect7) {
12117
12081
  return statement.type === "alter_table_alter_column_drop_default" && dialect7 === "postgresql";
@@ -12385,6 +12349,67 @@ var init_sqlgenerator = __esm({
12385
12349
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY`;
12386
12350
  }
12387
12351
  };
12352
+ LibSQLModifyColumn = class extends Convertor {
12353
+ can(statement, dialect7, driver2) {
12354
+ 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";
12355
+ }
12356
+ convert(statement, json2, action) {
12357
+ const { tableName, columnName } = statement;
12358
+ let columnType = ``;
12359
+ let columnDefault = "";
12360
+ let columnNotNull = "";
12361
+ const sqlStatements = [];
12362
+ const indexes = [];
12363
+ for (const table4 of Object.values(json2.tables)) {
12364
+ for (const index4 of Object.values(table4.indexes)) {
12365
+ const unsquashed = SQLiteSquasher.unsquashIdx(index4);
12366
+ sqlStatements.push(`DROP INDEX IF EXISTS "${unsquashed.name}";`);
12367
+ indexes.push({ ...unsquashed, tableName: table4.name });
12368
+ }
12369
+ }
12370
+ switch (statement.type) {
12371
+ case "alter_table_alter_column_set_type":
12372
+ columnType = ` ${statement.newDataType}`;
12373
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12374
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12375
+ break;
12376
+ case "alter_table_alter_column_drop_notnull":
12377
+ columnType = ` ${statement.newDataType}`;
12378
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12379
+ columnNotNull = "";
12380
+ break;
12381
+ case "alter_table_alter_column_set_notnull":
12382
+ columnType = ` ${statement.newDataType}`;
12383
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12384
+ columnNotNull = ` NOT NULL`;
12385
+ break;
12386
+ case "alter_table_alter_column_set_default":
12387
+ columnType = ` ${statement.newDataType}`;
12388
+ columnDefault = ` DEFAULT ${statement.newDefaultValue}`;
12389
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12390
+ break;
12391
+ case "alter_table_alter_column_drop_default":
12392
+ columnType = ` ${statement.newDataType}`;
12393
+ columnDefault = "";
12394
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12395
+ break;
12396
+ }
12397
+ columnDefault = columnDefault instanceof Date ? columnDefault.toISOString() : columnDefault;
12398
+ sqlStatements.push(
12399
+ `ALTER TABLE \`${tableName}\` ALTER COLUMN "${columnName}" TO "${columnName}"${columnType}${columnNotNull}${columnDefault};`
12400
+ );
12401
+ for (const index4 of indexes) {
12402
+ const indexPart = index4.isUnique ? "UNIQUE INDEX" : "INDEX";
12403
+ const whereStatement = index4.where ? ` WHERE ${index4.where}` : "";
12404
+ const uniqueString = index4.columns.map((it) => `\`${it}\``).join(",");
12405
+ const tableName2 = index4.tableName;
12406
+ sqlStatements.push(
12407
+ `CREATE ${indexPart} \`${index4.name}\` ON \`${tableName2}\` (${uniqueString})${whereStatement};`
12408
+ );
12409
+ }
12410
+ return sqlStatements;
12411
+ }
12412
+ };
12388
12413
  MySqlModifyColumn = class extends Convertor {
12389
12414
  can(statement, dialect7) {
12390
12415
  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";
@@ -12522,21 +12547,6 @@ var init_sqlgenerator = __esm({
12522
12547
  return `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${columnName}\`${columnType}${columnAutoincrement}${columnNotNull}${columnDefault}${columnOnUpdate}${columnGenerated};`;
12523
12548
  }
12524
12549
  };
12525
- SqliteAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
12526
- can(statement, dialect7) {
12527
- return statement.type === "alter_table_alter_column_drop_default" && dialect7 === "sqlite";
12528
- }
12529
- convert(statement) {
12530
- return `/*
12531
- SQLite does not support "Drop default from column" out of the box, we do not generate automatic migration for that, so it has to be done manually
12532
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12533
- https://www.sqlite.org/lang_altertable.html
12534
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12535
-
12536
- Due to that we don't generate migration automatically and it has to be done manually
12537
- */`;
12538
- }
12539
- };
12540
12550
  PgAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
12541
12551
  can(statement, dialect7) {
12542
12552
  return statement.type === "create_composite_pk" && dialect7 === "postgresql";
@@ -12601,85 +12611,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12601
12611
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY, ADD PRIMARY KEY(\`${newColumns.join("`,`")}\`);`;
12602
12612
  }
12603
12613
  };
12604
- SqliteAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
12605
- can(statement, dialect7) {
12606
- return statement.type === "create_composite_pk" && dialect7 === "sqlite";
12607
- }
12608
- convert(statement) {
12609
- let msg = "/*\n";
12610
- msg += `You're trying to add PRIMARY KEY(${statement.data}) to '${statement.tableName}' table
12611
- `;
12612
- msg += "SQLite does not support adding primary key to an already created table\n";
12613
- msg += "You can do it in 3 steps with drizzle orm:\n";
12614
- msg += " - create new mirror table with needed pk, rename current table to old_table, generate SQL\n";
12615
- msg += " - migrate old data from one table to another\n";
12616
- msg += " - delete old_table in schema, generate sql\n\n";
12617
- msg += "or create manual migration like below:\n\n";
12618
- msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12619
- msg += "CREATE TABLE table_name (\n";
12620
- msg += " column1 datatype [ NULL | NOT NULL ],\n";
12621
- msg += " column2 datatype [ NULL | NOT NULL ],\n";
12622
- msg += " ...\n";
12623
- msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12624
- msg += " );\n";
12625
- msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12626
- msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12627
- msg += "*/\n";
12628
- return msg;
12629
- }
12630
- };
12631
- SqliteAlterTableDeleteCompositePrimaryKeyConvertor = class extends Convertor {
12632
- can(statement, dialect7) {
12633
- return statement.type === "delete_composite_pk" && dialect7 === "sqlite";
12634
- }
12635
- convert(statement) {
12636
- let msg = "/*\n";
12637
- msg += `You're trying to delete PRIMARY KEY(${statement.data}) from '${statement.tableName}' table
12638
- `;
12639
- msg += "SQLite does not supportprimary key deletion from existing table\n";
12640
- msg += "You can do it in 3 steps with drizzle orm:\n";
12641
- msg += " - create new mirror table table without pk, rename current table to old_table, generate SQL\n";
12642
- msg += " - migrate old data from one table to another\n";
12643
- msg += " - delete old_table in schema, generate sql\n\n";
12644
- msg += "or create manual migration like below:\n\n";
12645
- msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12646
- msg += "CREATE TABLE table_name (\n";
12647
- msg += " column1 datatype [ NULL | NOT NULL ],\n";
12648
- msg += " column2 datatype [ NULL | NOT NULL ],\n";
12649
- msg += " ...\n";
12650
- msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12651
- msg += " );\n";
12652
- msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12653
- msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12654
- msg += "*/\n";
12655
- return msg;
12656
- }
12657
- };
12658
- SqliteAlterTableAlterCompositePrimaryKeyConvertor = class extends Convertor {
12659
- can(statement, dialect7) {
12660
- return statement.type === "alter_composite_pk" && dialect7 === "sqlite";
12661
- }
12662
- convert(statement) {
12663
- let msg = "/*\n";
12664
- msg += "SQLite does not support altering primary key\n";
12665
- msg += "You can do it in 3 steps with drizzle orm:\n";
12666
- msg += " - create new mirror table with needed pk, rename current table to old_table, generate SQL\n";
12667
- msg += " - migrate old data from one table to another\n";
12668
- msg += " - delete old_table in schema, generate sql\n\n";
12669
- msg += "or create manual migration like below:\n\n";
12670
- msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12671
- msg += "CREATE TABLE table_name (\n";
12672
- msg += " column1 datatype [ NULL | NOT NULL ],\n";
12673
- msg += " column2 datatype [ NULL | NOT NULL ],\n";
12674
- msg += " ...\n";
12675
- msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12676
- msg += " );\n";
12677
- msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12678
- msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12679
- msg += "*/\n";
12680
- return msg;
12681
- }
12682
- };
12683
12614
  PgAlterTableAlterColumnSetPrimaryKeyConvertor = class extends Convertor {
12684
12615
  can(statement, dialect7) {
12685
12616
  return statement.type === "alter_table_alter_column_set_pk" && dialect7 === "postgresql";
@@ -12724,51 +12655,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12724
12655
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET NOT NULL;`;
12725
12656
  }
12726
12657
  };
12727
- SqliteAlterTableAlterColumnSetNotNullConvertor = class extends Convertor {
12728
- can(statement, dialect7) {
12729
- return statement.type === "alter_table_alter_column_set_notnull" && dialect7 === "sqlite";
12730
- }
12731
- convert(statement) {
12732
- return `/*
12733
- SQLite does not support "Set not null to column" out of the box, we do not generate automatic migration for that, so it has to be done manually
12734
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12735
- https://www.sqlite.org/lang_altertable.html
12736
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12737
-
12738
- Due to that we don't generate migration automatically and it has to be done manually
12739
- */`;
12740
- }
12741
- };
12742
- SqliteAlterTableAlterColumnSetAutoincrementConvertor = class extends Convertor {
12743
- can(statement, dialect7) {
12744
- return statement.type === "alter_table_alter_column_set_autoincrement" && dialect7 === "sqlite";
12745
- }
12746
- convert(statement) {
12747
- return `/*
12748
- SQLite does not support "Set autoincrement to a column" out of the box, we do not generate automatic migration for that, so it has to be done manually
12749
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12750
- https://www.sqlite.org/lang_altertable.html
12751
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12752
-
12753
- Due to that we don't generate migration automatically and it has to be done manually
12754
- */`;
12755
- }
12756
- };
12757
- SqliteAlterTableAlterColumnDropAutoincrementConvertor = class extends Convertor {
12758
- can(statement, dialect7) {
12759
- return statement.type === "alter_table_alter_column_drop_autoincrement" && dialect7 === "sqlite";
12760
- }
12761
- convert(statement) {
12762
- return `/*
12763
- SQLite does not support "Drop autoincrement from a column" out of the box, we do not generate automatic migration for that, so it has to be done manually
12764
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12765
- https://www.sqlite.org/lang_altertable.html
12766
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12767
-
12768
- Due to that we don't generate migration automatically and it has to be done manually
12769
- */`;
12770
- }
12771
- };
12772
12658
  PgAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12773
12659
  can(statement, dialect7) {
12774
12660
  return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "postgresql";
@@ -12779,21 +12665,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12779
12665
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" DROP NOT NULL;`;
12780
12666
  }
12781
12667
  };
12782
- SqliteAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12783
- can(statement, dialect7) {
12784
- return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "sqlite";
12785
- }
12786
- convert(statement) {
12787
- return `/*
12788
- SQLite does not support "Drop not null from column" out of the box, we do not generate automatic migration for that, so it has to be done manually
12789
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12790
- https://www.sqlite.org/lang_altertable.html
12791
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12792
-
12793
- Due to that we don't generate migration automatically and it has to be done manually
12794
- */`;
12795
- }
12796
- };
12797
12668
  PgCreateForeignKeyConvertor = class extends Convertor {
12798
12669
  can(statement, dialect7) {
12799
12670
  return statement.type === "create_reference" && dialect7 === "postgresql";
@@ -12824,18 +12695,21 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12824
12695
  return sql2;
12825
12696
  }
12826
12697
  };
12827
- SqliteCreateForeignKeyConvertor = class extends Convertor {
12828
- can(statement, dialect7) {
12829
- return statement.type === "create_reference" && dialect7 === "sqlite";
12698
+ LibSQLCreateForeignKeyConvertor = class extends Convertor {
12699
+ can(statement, dialect7, driver2) {
12700
+ return statement.type === "create_reference" && dialect7 === "sqlite" && driver2 === "turso";
12830
12701
  }
12831
- convert(statement) {
12832
- return `/*
12833
- SQLite does not support "Creating foreign key on existing column" out of the box, we do not generate automatic migration for that, so it has to be done manually
12834
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12835
- https://www.sqlite.org/lang_altertable.html
12836
-
12837
- Due to that we don't generate migration automatically and it has to be done manually
12838
- */`;
12702
+ convert(statement, json2, action) {
12703
+ const { columnsFrom, columnsTo, tableFrom, onDelete, onUpdate, tableTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
12704
+ const { columnDefault, columnNotNull, columnType } = statement;
12705
+ const onDeleteStatement = onDelete ? ` ON DELETE ${onDelete}` : "";
12706
+ const onUpdateStatement = onUpdate ? ` ON UPDATE ${onUpdate}` : "";
12707
+ const columnsDefaultValue = columnDefault ? ` DEFAULT ${columnDefault}` : "";
12708
+ const columnNotNullValue = columnNotNull ? ` NOT NULL` : "";
12709
+ const columnTypeValue = columnType ? ` ${columnType}` : "";
12710
+ const columnFrom = columnsFrom[0];
12711
+ const columnTo = columnsTo[0];
12712
+ return `ALTER TABLE \`${tableFrom}\` ALTER COLUMN "${columnFrom}" TO "${columnFrom}"${columnTypeValue}${columnNotNullValue}${columnsDefaultValue} REFERENCES ${tableTo}(${columnTo})${onDeleteStatement}${onUpdateStatement};`;
12839
12713
  }
12840
12714
  };
12841
12715
  MySqlCreateForeignKeyConvertor = class extends Convertor {
@@ -12884,20 +12758,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12884
12758
  return sql2;
12885
12759
  }
12886
12760
  };
12887
- SqliteAlterForeignKeyConvertor = class extends Convertor {
12888
- can(statement, dialect7) {
12889
- return statement.type === "alter_reference" && dialect7 === "sqlite";
12890
- }
12891
- convert(statement) {
12892
- return `/*
12893
- SQLite does not support "Changing existing foreign key" out of the box, we do not generate automatic migration for that, so it has to be done manually
12894
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12895
- https://www.sqlite.org/lang_altertable.html
12896
-
12897
- Due to that we don't generate migration automatically and it has to be done manually
12898
- */`;
12899
- }
12900
- };
12901
12761
  PgDeleteForeignKeyConvertor = class extends Convertor {
12902
12762
  can(statement, dialect7) {
12903
12763
  return statement.type === "delete_reference" && dialect7 === "postgresql";
@@ -12910,20 +12770,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12910
12770
  `;
12911
12771
  }
12912
12772
  };
12913
- SqliteDeleteForeignKeyConvertor = class extends Convertor {
12914
- can(statement, dialect7) {
12915
- return statement.type === "delete_reference" && dialect7 === "sqlite";
12916
- }
12917
- convert(statement) {
12918
- return `/*
12919
- SQLite does not support "Dropping foreign key" out of the box, we do not generate automatic migration for that, so it has to be done manually
12920
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12921
- https://www.sqlite.org/lang_altertable.html
12922
-
12923
- Due to that we don't generate migration automatically and it has to be done manually
12924
- */`;
12925
- }
12926
- };
12927
12773
  MySqlDeleteForeignKeyConvertor = class extends Convertor {
12928
12774
  can(statement, dialect7) {
12929
12775
  return statement.type === "delete_reference" && dialect7 === "mysql";
@@ -13087,10 +12933,96 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13087
12933
  return `DROP INDEX \`${name2}\` ON \`${statement.tableName}\`;`;
13088
12934
  }
13089
12935
  };
12936
+ SQLiteRecreateTableConvertor = class extends Convertor {
12937
+ can(statement, dialect7, driver2) {
12938
+ return statement.type === "recreate_table" && dialect7 === "sqlite" && !driver2;
12939
+ }
12940
+ convert(statement) {
12941
+ const { tableName, columns, compositePKs, referenceData } = statement;
12942
+ const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
12943
+ const newTableName = `__new_${tableName}`;
12944
+ const sqlStatements = [];
12945
+ sqlStatements.push(`PRAGMA foreign_keys=OFF;`);
12946
+ sqlStatements.push(
12947
+ new SQLiteCreateTableConvertor().convert({
12948
+ type: "sqlite_create_table",
12949
+ tableName: newTableName,
12950
+ columns,
12951
+ referenceData,
12952
+ compositePKs
12953
+ })
12954
+ );
12955
+ sqlStatements.push(
12956
+ `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
12957
+ );
12958
+ sqlStatements.push(
12959
+ new SQLiteDropTableConvertor().convert({
12960
+ type: "drop_table",
12961
+ tableName,
12962
+ schema: ""
12963
+ })
12964
+ );
12965
+ sqlStatements.push(
12966
+ new SqliteRenameTableConvertor().convert({
12967
+ fromSchema: "",
12968
+ tableNameFrom: newTableName,
12969
+ tableNameTo: tableName,
12970
+ toSchema: "",
12971
+ type: "rename_table"
12972
+ })
12973
+ );
12974
+ sqlStatements.push(`PRAGMA foreign_keys=ON;`);
12975
+ return sqlStatements;
12976
+ }
12977
+ };
12978
+ LibSQLRecreateTableConvertor = class extends Convertor {
12979
+ can(statement, dialect7, driver2) {
12980
+ return statement.type === "recreate_table" && dialect7 === "sqlite" && driver2 === "turso";
12981
+ }
12982
+ convert(statement) {
12983
+ const { tableName, columns, compositePKs, referenceData } = statement;
12984
+ const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
12985
+ const newTableName = `__new_${tableName}`;
12986
+ const sqlStatements = [];
12987
+ sqlStatements.push(`PRAGMA foreign_keys=OFF;`);
12988
+ sqlStatements.push(
12989
+ new SQLiteCreateTableConvertor().convert({
12990
+ type: "sqlite_create_table",
12991
+ tableName: newTableName,
12992
+ columns,
12993
+ referenceData,
12994
+ compositePKs
12995
+ })
12996
+ );
12997
+ sqlStatements.push(
12998
+ `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
12999
+ );
13000
+ sqlStatements.push(
13001
+ new SQLiteDropTableConvertor().convert({
13002
+ type: "drop_table",
13003
+ tableName,
13004
+ schema: ""
13005
+ })
13006
+ );
13007
+ sqlStatements.push(
13008
+ new SqliteRenameTableConvertor().convert({
13009
+ fromSchema: "",
13010
+ tableNameFrom: newTableName,
13011
+ tableNameTo: tableName,
13012
+ toSchema: "",
13013
+ type: "rename_table"
13014
+ })
13015
+ );
13016
+ sqlStatements.push(`PRAGMA foreign_keys=ON;`);
13017
+ return sqlStatements;
13018
+ }
13019
+ };
13090
13020
  convertors = [];
13091
13021
  convertors.push(new PgCreateTableConvertor());
13092
13022
  convertors.push(new MySqlCreateTableConvertor());
13093
13023
  convertors.push(new SQLiteCreateTableConvertor());
13024
+ convertors.push(new SQLiteRecreateTableConvertor());
13025
+ convertors.push(new LibSQLRecreateTableConvertor());
13094
13026
  convertors.push(new CreateTypeEnumConvertor());
13095
13027
  convertors.push(new CreatePgSequenceConvertor());
13096
13028
  convertors.push(new DropPgSequenceConvertor());
@@ -13138,6 +13070,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13138
13070
  convertors.push(new SqliteAlterTableAlterColumnAlterGeneratedConvertor());
13139
13071
  convertors.push(new SqliteAlterTableAlterColumnSetExpressionConvertor());
13140
13072
  convertors.push(new MySqlModifyColumn());
13073
+ convertors.push(new LibSQLModifyColumn());
13141
13074
  convertors.push(new PgCreateForeignKeyConvertor());
13142
13075
  convertors.push(new MySqlCreateForeignKeyConvertor());
13143
13076
  convertors.push(new PgAlterForeignKeyConvertor());
@@ -13149,24 +13082,10 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13149
13082
  convertors.push(new PgAlterTableSetSchemaConvertor());
13150
13083
  convertors.push(new PgAlterTableSetNewSchemaConvertor());
13151
13084
  convertors.push(new PgAlterTableRemoveFromSchemaConvertor());
13152
- convertors.push(new SQLiteAlterTableAlterColumnSetTypeConvertor());
13153
- convertors.push(new SqliteAlterForeignKeyConvertor());
13154
- convertors.push(new SqliteDeleteForeignKeyConvertor());
13155
- convertors.push(new SqliteCreateForeignKeyConvertor());
13156
- convertors.push(new SQLiteAlterTableAddUniqueConstraintConvertor());
13157
- convertors.push(new SQLiteAlterTableDropUniqueConstraintConvertor());
13085
+ convertors.push(new LibSQLCreateForeignKeyConvertor());
13158
13086
  convertors.push(new PgAlterTableAlterColumnDropGenerated());
13159
13087
  convertors.push(new PgAlterTableAlterColumnSetGenerated());
13160
13088
  convertors.push(new PgAlterTableAlterColumnAlterGenerated());
13161
- convertors.push(new SqliteAlterTableAlterColumnSetNotNullConvertor());
13162
- convertors.push(new SqliteAlterTableAlterColumnDropNotNullConvertor());
13163
- convertors.push(new SqliteAlterTableAlterColumnSetDefaultConvertor());
13164
- convertors.push(new SqliteAlterTableAlterColumnDropDefaultConvertor());
13165
- convertors.push(new SqliteAlterTableAlterColumnSetAutoincrementConvertor());
13166
- convertors.push(new SqliteAlterTableAlterColumnDropAutoincrementConvertor());
13167
- convertors.push(new SqliteAlterTableCreateCompositePrimaryKeyConvertor());
13168
- convertors.push(new SqliteAlterTableDeleteCompositePrimaryKeyConvertor());
13169
- convertors.push(new SqliteAlterTableAlterCompositePrimaryKeyConvertor());
13170
13089
  convertors.push(new PgAlterTableCreateCompositePrimaryKeyConvertor());
13171
13090
  convertors.push(new PgAlterTableDeleteCompositePrimaryKeyConvertor());
13172
13091
  convertors.push(new PgAlterTableAlterCompositePrimaryKeyConvertor());
@@ -13175,19 +13094,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13175
13094
  convertors.push(new MySqlAlterTableCreateCompositePrimaryKeyConvertor());
13176
13095
  convertors.push(new MySqlAlterTableAddPk());
13177
13096
  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
- };
13191
13097
  https: `
13192
13098
  create table users (
13193
13099
  id int,
@@ -13214,12 +13120,248 @@ drop type __venum;
13214
13120
  }
13215
13121
  });
13216
13122
 
13123
+ // src/cli/commands/sqlitePushUtils.ts
13124
+ var _moveDataStatements, getOldTableName, getNewTableName, logSuggestionsAndReturn;
13125
+ var init_sqlitePushUtils = __esm({
13126
+ "src/cli/commands/sqlitePushUtils.ts"() {
13127
+ "use strict";
13128
+ init_source();
13129
+ init_sqliteSchema();
13130
+ init_sqlgenerator();
13131
+ init_utils();
13132
+ _moveDataStatements = (tableName, json, dataLoss = false) => {
13133
+ const statements = [];
13134
+ const newTableName = `__new_${tableName}`;
13135
+ const tableColumns = Object.values(json.tables[tableName].columns);
13136
+ const referenceData = Object.values(json.tables[tableName].foreignKeys);
13137
+ const compositePKs = Object.values(
13138
+ json.tables[tableName].compositePrimaryKeys
13139
+ ).map((it) => SQLiteSquasher.unsquashPK(it));
13140
+ const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
13141
+ statements.push(
13142
+ new SQLiteCreateTableConvertor().convert({
13143
+ type: "sqlite_create_table",
13144
+ tableName: newTableName,
13145
+ columns: tableColumns,
13146
+ referenceData: fks,
13147
+ compositePKs
13148
+ })
13149
+ );
13150
+ if (!dataLoss) {
13151
+ const columns = Object.keys(json.tables[tableName].columns).map(
13152
+ (c) => `"${c}"`
13153
+ );
13154
+ statements.push(
13155
+ `INSERT INTO \`${newTableName}\`(${columns.join(
13156
+ ", "
13157
+ )}) SELECT ${columns.join(", ")} FROM \`${tableName}\`;`
13158
+ );
13159
+ }
13160
+ statements.push(
13161
+ new SQLiteDropTableConvertor().convert({
13162
+ type: "drop_table",
13163
+ tableName,
13164
+ schema: ""
13165
+ })
13166
+ );
13167
+ statements.push(
13168
+ new SqliteRenameTableConvertor().convert({
13169
+ fromSchema: "",
13170
+ tableNameFrom: newTableName,
13171
+ tableNameTo: tableName,
13172
+ toSchema: "",
13173
+ type: "rename_table"
13174
+ })
13175
+ );
13176
+ for (const idx of Object.values(json.tables[tableName].indexes)) {
13177
+ statements.push(
13178
+ new CreateSqliteIndexConvertor().convert({
13179
+ type: "create_index",
13180
+ tableName,
13181
+ schema: "",
13182
+ data: idx
13183
+ })
13184
+ );
13185
+ }
13186
+ return statements;
13187
+ };
13188
+ getOldTableName = (tableName, meta) => {
13189
+ for (const key of Object.keys(meta.tables)) {
13190
+ const value = meta.tables[key];
13191
+ if (`"${tableName}"` === value) {
13192
+ return key.substring(1, key.length - 1);
13193
+ }
13194
+ }
13195
+ return tableName;
13196
+ };
13197
+ getNewTableName = (tableName, meta) => {
13198
+ if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
13199
+ return meta.tables[`"${tableName}"`].substring(
13200
+ 1,
13201
+ meta.tables[`"${tableName}"`].length - 1
13202
+ );
13203
+ }
13204
+ return tableName;
13205
+ };
13206
+ logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
13207
+ let shouldAskForApprove = false;
13208
+ const statementsToExecute = [];
13209
+ const infoToPrint = [];
13210
+ const tablesToRemove = [];
13211
+ const columnsToRemove = [];
13212
+ const schemasToRemove = [];
13213
+ const tablesToTruncate = [];
13214
+ for (const statement of statements) {
13215
+ if (statement.type === "drop_table") {
13216
+ const res = await connection.query(
13217
+ `select count(*) as count from \`${statement.tableName}\``
13218
+ );
13219
+ const count2 = Number(res[0].count);
13220
+ if (count2 > 0) {
13221
+ infoToPrint.push(
13222
+ `\xB7 You're about to delete ${source_default.underline(
13223
+ statement.tableName
13224
+ )} table with ${count2} items`
13225
+ );
13226
+ tablesToRemove.push(statement.tableName);
13227
+ shouldAskForApprove = true;
13228
+ }
13229
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13230
+ statementsToExecute.push(
13231
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13232
+ );
13233
+ } else if (statement.type === "alter_table_drop_column") {
13234
+ const tableName = statement.tableName;
13235
+ const columnName = statement.columnName;
13236
+ const res = await connection.query(
13237
+ `select count(\`${tableName}\`.\`${columnName}\`) as count from \`${tableName}\``
13238
+ );
13239
+ const count2 = Number(res[0].count);
13240
+ if (count2 > 0) {
13241
+ infoToPrint.push(
13242
+ `\xB7 You're about to delete ${source_default.underline(
13243
+ columnName
13244
+ )} column in ${tableName} table with ${count2} items`
13245
+ );
13246
+ columnsToRemove.push(`${tableName}_${statement.columnName}`);
13247
+ shouldAskForApprove = true;
13248
+ }
13249
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13250
+ statementsToExecute.push(
13251
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13252
+ );
13253
+ } else if (statement.type === "sqlite_alter_table_add_column" && (statement.column.notNull && !statement.column.default)) {
13254
+ const tableName = statement.tableName;
13255
+ const columnName = statement.column.name;
13256
+ const res = await connection.query(
13257
+ `select count(*) as count from \`${tableName}\``
13258
+ );
13259
+ const count2 = Number(res[0].count);
13260
+ if (count2 > 0) {
13261
+ infoToPrint.push(
13262
+ `\xB7 You're about to add not-null ${source_default.underline(
13263
+ columnName
13264
+ )} column without default value, which contains ${count2} items`
13265
+ );
13266
+ tablesToTruncate.push(tableName);
13267
+ statementsToExecute.push(`delete from ${tableName};`);
13268
+ shouldAskForApprove = true;
13269
+ }
13270
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13271
+ statementsToExecute.push(
13272
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13273
+ );
13274
+ } else if (statement.type === "recreate_table") {
13275
+ const tableName = statement.tableName;
13276
+ const oldTableName = getOldTableName(tableName, meta);
13277
+ let dataLoss = false;
13278
+ const prevColumnNames = Object.keys(json1.tables[oldTableName].columns);
13279
+ const currentColumnNames = Object.keys(json2.tables[tableName].columns);
13280
+ const { removedColumns, addedColumns } = findAddedAndRemoved(
13281
+ prevColumnNames,
13282
+ currentColumnNames
13283
+ );
13284
+ if (removedColumns.length) {
13285
+ for (const removedColumn of removedColumns) {
13286
+ const res = await connection.query(
13287
+ `select count(\`${tableName}\`.\`${removedColumn}\`) as count from \`${tableName}\``
13288
+ );
13289
+ const count2 = Number(res[0].count);
13290
+ if (count2 > 0) {
13291
+ infoToPrint.push(
13292
+ `\xB7 You're about to delete ${source_default.underline(
13293
+ removedColumn
13294
+ )} column in ${tableName} table with ${count2} items`
13295
+ );
13296
+ columnsToRemove.push(removedColumn);
13297
+ shouldAskForApprove = true;
13298
+ }
13299
+ }
13300
+ }
13301
+ if (addedColumns.length) {
13302
+ for (const addedColumn of addedColumns) {
13303
+ const [res] = await connection.query(
13304
+ `select count(*) as count from \`${tableName}\``
13305
+ );
13306
+ const columnConf = json2.tables[tableName].columns[addedColumn];
13307
+ const count2 = Number(res.count);
13308
+ if (count2 > 0 && columnConf.notNull && !columnConf.default) {
13309
+ dataLoss = true;
13310
+ infoToPrint.push(
13311
+ `\xB7 You're about to add not-null ${source_default.underline(
13312
+ addedColumn
13313
+ )} column without default value to table, which contains ${count2} items`
13314
+ );
13315
+ shouldAskForApprove = true;
13316
+ tablesToTruncate.push(tableName);
13317
+ statementsToExecute.push(`DELETE FROM \`${tableName}\`;`);
13318
+ }
13319
+ }
13320
+ }
13321
+ const tablesReferencingCurrent = [];
13322
+ for (const table4 of Object.values(json2.tables)) {
13323
+ const tablesRefs = Object.values(json2.tables[table4.name].foreignKeys).filter((t) => SQLiteSquasher.unsquashPushFK(t).tableTo === tableName).map((it) => SQLiteSquasher.unsquashPushFK(it).tableFrom);
13324
+ tablesReferencingCurrent.push(...tablesRefs);
13325
+ }
13326
+ if (!tablesReferencingCurrent.length) {
13327
+ statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
13328
+ continue;
13329
+ }
13330
+ const [{ foreign_keys: pragmaState }] = await connection.query(`PRAGMA foreign_keys;`);
13331
+ if (pragmaState) {
13332
+ statementsToExecute.push(`PRAGMA foreign_keys=OFF;`);
13333
+ }
13334
+ statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
13335
+ if (pragmaState) {
13336
+ statementsToExecute.push(`PRAGMA foreign_keys=ON;`);
13337
+ }
13338
+ } else {
13339
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13340
+ statementsToExecute.push(
13341
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13342
+ );
13343
+ }
13344
+ }
13345
+ return {
13346
+ statementsToExecute,
13347
+ shouldAskForApprove,
13348
+ infoToPrint,
13349
+ columnsToRemove: [...new Set(columnsToRemove)],
13350
+ schemasToRemove: [...new Set(schemasToRemove)],
13351
+ tablesToTruncate: [...new Set(tablesToTruncate)],
13352
+ tablesToRemove: [...new Set(tablesToRemove)]
13353
+ };
13354
+ };
13355
+ }
13356
+ });
13357
+
13217
13358
  // src/jsonStatements.ts
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;
13359
+ 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;
13219
13360
  var init_jsonStatements = __esm({
13220
13361
  "src/jsonStatements.ts"() {
13221
13362
  "use strict";
13222
13363
  init_source();
13364
+ init_sqlitePushUtils();
13223
13365
  init_views();
13224
13366
  init_mysqlSchema();
13225
13367
  init_pgSchema();
@@ -14003,21 +14145,64 @@ var init_jsonStatements = __esm({
14003
14145
  const columnPk = json2.tables[tableName].columns[columnName].primaryKey;
14004
14146
  const columnGenerated = json2.tables[tableName].columns[columnName].generated;
14005
14147
  const compositePk = json2.tables[tableName].compositePrimaryKeys[`${tableName}_${columnName}`];
14006
- if (typeof column4.name !== "string") {
14007
- statements.push({
14008
- type: "alter_table_rename_column",
14009
- tableName,
14010
- oldColumnName: column4.name.old,
14011
- newColumnName: column4.name.new,
14012
- schema: schema4
14013
- });
14014
- }
14015
- if (column4.type?.type === "changed") {
14148
+ if (column4.autoincrement?.type === "added") {
14016
14149
  statements.push({
14017
- type: "alter_table_alter_column_set_type",
14150
+ type: "alter_table_alter_column_set_autoincrement",
14018
14151
  tableName,
14019
14152
  columnName,
14020
- newDataType: column4.type.new,
14153
+ schema: schema4,
14154
+ newDataType: columnType,
14155
+ columnDefault,
14156
+ columnOnUpdate,
14157
+ columnNotNull,
14158
+ columnAutoIncrement,
14159
+ columnPk
14160
+ });
14161
+ }
14162
+ if (column4.autoincrement?.type === "changed") {
14163
+ const type = column4.autoincrement.new ? "alter_table_alter_column_set_autoincrement" : "alter_table_alter_column_drop_autoincrement";
14164
+ statements.push({
14165
+ type,
14166
+ tableName,
14167
+ columnName,
14168
+ schema: schema4,
14169
+ newDataType: columnType,
14170
+ columnDefault,
14171
+ columnOnUpdate,
14172
+ columnNotNull,
14173
+ columnAutoIncrement,
14174
+ columnPk
14175
+ });
14176
+ }
14177
+ if (column4.autoincrement?.type === "deleted") {
14178
+ statements.push({
14179
+ type: "alter_table_alter_column_drop_autoincrement",
14180
+ tableName,
14181
+ columnName,
14182
+ schema: schema4,
14183
+ newDataType: columnType,
14184
+ columnDefault,
14185
+ columnOnUpdate,
14186
+ columnNotNull,
14187
+ columnAutoIncrement,
14188
+ columnPk
14189
+ });
14190
+ }
14191
+ if (typeof column4.name !== "string") {
14192
+ statements.push({
14193
+ type: "alter_table_rename_column",
14194
+ tableName,
14195
+ oldColumnName: column4.name.old,
14196
+ newColumnName: column4.name.new,
14197
+ schema: schema4
14198
+ });
14199
+ }
14200
+ if (column4.type?.type === "changed") {
14201
+ statements.push({
14202
+ type: "alter_table_alter_column_set_type",
14203
+ tableName,
14204
+ columnName,
14205
+ newDataType: column4.type.new,
14021
14206
  oldDataType: column4.type.old,
14022
14207
  schema: schema4,
14023
14208
  columnDefault,
@@ -14266,6 +14451,37 @@ var init_jsonStatements = __esm({
14266
14451
  };
14267
14452
  });
14268
14453
  };
14454
+ prepareLibSQLCreateReferencesJson = (tableName, schema4, foreignKeys, json2, action) => {
14455
+ return Object.values(foreignKeys).map((fkData) => {
14456
+ const { columnsFrom, tableFrom, columnsTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14457
+ let isMulticolumn = false;
14458
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
14459
+ isMulticolumn = true;
14460
+ return {
14461
+ type: "create_reference",
14462
+ tableName,
14463
+ data: fkData,
14464
+ schema: schema4,
14465
+ isMulticolumn
14466
+ };
14467
+ }
14468
+ const columnFrom = columnsFrom[0];
14469
+ const {
14470
+ notNull: columnNotNull,
14471
+ default: columnDefault,
14472
+ type: columnType
14473
+ } = json2.tables[tableFrom].columns[columnFrom];
14474
+ return {
14475
+ type: "create_reference",
14476
+ tableName,
14477
+ data: fkData,
14478
+ schema: schema4,
14479
+ columnNotNull,
14480
+ columnDefault,
14481
+ columnType
14482
+ };
14483
+ });
14484
+ };
14269
14485
  prepareDropReferencesJson = (tableName, schema4, foreignKeys) => {
14270
14486
  return Object.values(foreignKeys).map((fkData) => {
14271
14487
  return {
@@ -14276,6 +14492,53 @@ var init_jsonStatements = __esm({
14276
14492
  };
14277
14493
  });
14278
14494
  };
14495
+ prepareLibSQLDropReferencesJson = (tableName, schema4, foreignKeys, json2, meta, action) => {
14496
+ const statements = Object.values(foreignKeys).map((fkData) => {
14497
+ const { columnsFrom, tableFrom, columnsTo, name: name2, tableTo, onDelete, onUpdate } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14498
+ const keys = Object.keys(json2.tables[tableName].columns);
14499
+ const filtered = columnsFrom.filter((it) => keys.includes(it));
14500
+ const fullDrop = filtered.length === 0;
14501
+ if (fullDrop) return;
14502
+ let isMulticolumn = false;
14503
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
14504
+ isMulticolumn = true;
14505
+ return {
14506
+ type: "delete_reference",
14507
+ tableName,
14508
+ data: fkData,
14509
+ schema: schema4,
14510
+ isMulticolumn
14511
+ };
14512
+ }
14513
+ const columnFrom = columnsFrom[0];
14514
+ const newTableName = getNewTableName(tableFrom, meta);
14515
+ const {
14516
+ notNull: columnNotNull,
14517
+ default: columnDefault,
14518
+ type: columnType
14519
+ } = json2.tables[newTableName].columns[columnFrom];
14520
+ const fkToSquash = {
14521
+ columnsFrom,
14522
+ columnsTo,
14523
+ name: name2,
14524
+ tableFrom: newTableName,
14525
+ tableTo,
14526
+ onDelete,
14527
+ onUpdate
14528
+ };
14529
+ const foreignKey = action === "push" ? SQLiteSquasher.squashPushFK(fkToSquash) : SQLiteSquasher.squashFK(fkToSquash);
14530
+ return {
14531
+ type: "delete_reference",
14532
+ tableName,
14533
+ data: foreignKey,
14534
+ schema: schema4,
14535
+ columnNotNull,
14536
+ columnDefault,
14537
+ columnType
14538
+ };
14539
+ });
14540
+ return statements.filter((it) => it);
14541
+ };
14279
14542
  prepareAlterReferencesJson = (tableName, schema4, foreignKeys) => {
14280
14543
  const stmts = [];
14281
14544
  Object.values(foreignKeys).map((val) => {
@@ -14429,10 +14692,292 @@ var init_jsonStatements = __esm({
14429
14692
  }
14430
14693
  });
14431
14694
 
14695
+ // src/statementCombiner.ts
14696
+ var prepareLibSQLRecreateTable, prepareSQLiteRecreateTable, libSQLCombineStatements, sqliteCombineStatements;
14697
+ var init_statementCombiner = __esm({
14698
+ "src/statementCombiner.ts"() {
14699
+ "use strict";
14700
+ init_jsonStatements();
14701
+ init_sqliteSchema();
14702
+ prepareLibSQLRecreateTable = (table4, action) => {
14703
+ const { name: name2, columns, uniqueConstraints, indexes } = table4;
14704
+ const composites = Object.values(table4.compositePrimaryKeys).map(
14705
+ (it) => SQLiteSquasher.unsquashPK(it)
14706
+ );
14707
+ const references2 = Object.values(table4.foreignKeys);
14708
+ const fks = references2.map(
14709
+ (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14710
+ );
14711
+ const statements = [
14712
+ {
14713
+ type: "recreate_table",
14714
+ tableName: name2,
14715
+ columns: Object.values(columns),
14716
+ compositePKs: composites,
14717
+ referenceData: fks,
14718
+ uniqueConstraints: Object.values(uniqueConstraints)
14719
+ }
14720
+ ];
14721
+ if (Object.keys(indexes).length) {
14722
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
14723
+ }
14724
+ return statements;
14725
+ };
14726
+ prepareSQLiteRecreateTable = (table4, action) => {
14727
+ const { name: name2, columns, uniqueConstraints, indexes } = table4;
14728
+ const composites = Object.values(table4.compositePrimaryKeys).map(
14729
+ (it) => SQLiteSquasher.unsquashPK(it)
14730
+ );
14731
+ const references2 = Object.values(table4.foreignKeys);
14732
+ const fks = references2.map(
14733
+ (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14734
+ );
14735
+ const statements = [
14736
+ {
14737
+ type: "recreate_table",
14738
+ tableName: name2,
14739
+ columns: Object.values(columns),
14740
+ compositePKs: composites,
14741
+ referenceData: fks,
14742
+ uniqueConstraints: Object.values(uniqueConstraints)
14743
+ }
14744
+ ];
14745
+ if (Object.keys(indexes).length) {
14746
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
14747
+ }
14748
+ return statements;
14749
+ };
14750
+ libSQLCombineStatements = (statements, json2, action) => {
14751
+ const newStatements = {};
14752
+ for (const statement of statements) {
14753
+ 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") {
14754
+ const tableName2 = statement.tableName;
14755
+ const statementsForTable2 = newStatements[tableName2];
14756
+ if (!statementsForTable2) {
14757
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14758
+ continue;
14759
+ }
14760
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14761
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14762
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14763
+ if (wasRename) {
14764
+ newStatements[tableName2].push(...preparedStatements);
14765
+ } else {
14766
+ newStatements[tableName2] = preparedStatements;
14767
+ }
14768
+ continue;
14769
+ }
14770
+ continue;
14771
+ }
14772
+ 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") {
14773
+ const { tableName: tableName2, columnName, columnPk } = statement;
14774
+ const columnIsPartOfForeignKey = Object.values(
14775
+ json2.tables[tableName2].foreignKeys
14776
+ ).some((it) => {
14777
+ const unsquashFk = action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it);
14778
+ return unsquashFk.columnsFrom.includes(columnName);
14779
+ });
14780
+ const statementsForTable2 = newStatements[tableName2];
14781
+ if (!statementsForTable2 && (columnIsPartOfForeignKey || columnPk)) {
14782
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14783
+ continue;
14784
+ }
14785
+ if (statementsForTable2 && (columnIsPartOfForeignKey || columnPk)) {
14786
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14787
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14788
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14789
+ if (wasRename) {
14790
+ newStatements[tableName2].push(...preparedStatements);
14791
+ } else {
14792
+ newStatements[tableName2] = preparedStatements;
14793
+ }
14794
+ }
14795
+ continue;
14796
+ }
14797
+ if (statementsForTable2 && !(columnIsPartOfForeignKey || columnPk)) {
14798
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14799
+ newStatements[tableName2].push(statement);
14800
+ }
14801
+ continue;
14802
+ }
14803
+ newStatements[tableName2] = [statement];
14804
+ continue;
14805
+ }
14806
+ if (statement.type === "create_reference") {
14807
+ const tableName2 = statement.tableName;
14808
+ const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
14809
+ const statementsForTable2 = newStatements[tableName2];
14810
+ if (!statementsForTable2) {
14811
+ newStatements[tableName2] = statement.isMulticolumn ? prepareLibSQLRecreateTable(json2.tables[tableName2], action) : [statement];
14812
+ continue;
14813
+ }
14814
+ if (!statement.isMulticolumn && statementsForTable2.some(
14815
+ (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
14816
+ )) {
14817
+ continue;
14818
+ }
14819
+ if (statement.isMulticolumn) {
14820
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14821
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14822
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14823
+ if (wasRename) {
14824
+ newStatements[tableName2].push(...preparedStatements);
14825
+ } else {
14826
+ newStatements[tableName2] = preparedStatements;
14827
+ }
14828
+ continue;
14829
+ }
14830
+ continue;
14831
+ }
14832
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14833
+ newStatements[tableName2].push(statement);
14834
+ }
14835
+ continue;
14836
+ }
14837
+ if (statement.type === "delete_reference") {
14838
+ const tableName2 = statement.tableName;
14839
+ const statementsForTable2 = newStatements[tableName2];
14840
+ if (!statementsForTable2) {
14841
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14842
+ continue;
14843
+ }
14844
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14845
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14846
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14847
+ if (wasRename) {
14848
+ newStatements[tableName2].push(...preparedStatements);
14849
+ } else {
14850
+ newStatements[tableName2] = preparedStatements;
14851
+ }
14852
+ continue;
14853
+ }
14854
+ continue;
14855
+ }
14856
+ if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
14857
+ const tableName2 = statement.tableName;
14858
+ const statementsForTable2 = newStatements[tableName2];
14859
+ if (!statementsForTable2) {
14860
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14861
+ continue;
14862
+ }
14863
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14864
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14865
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14866
+ if (wasRename) {
14867
+ newStatements[tableName2].push(...preparedStatements);
14868
+ } else {
14869
+ newStatements[tableName2] = preparedStatements;
14870
+ }
14871
+ continue;
14872
+ }
14873
+ continue;
14874
+ }
14875
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
14876
+ const statementsForTable = newStatements[tableName];
14877
+ if (!statementsForTable) {
14878
+ newStatements[tableName] = [statement];
14879
+ continue;
14880
+ }
14881
+ if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
14882
+ newStatements[tableName].push(statement);
14883
+ }
14884
+ }
14885
+ const combinedStatements = Object.values(newStatements).flat();
14886
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
14887
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
14888
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
14889
+ return [...renamedTables, ...renamedColumns, ...rest];
14890
+ };
14891
+ sqliteCombineStatements = (statements, json2, action) => {
14892
+ const newStatements = {};
14893
+ for (const statement of statements) {
14894
+ 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") {
14895
+ const tableName2 = statement.tableName;
14896
+ const statementsForTable2 = newStatements[tableName2];
14897
+ if (!statementsForTable2) {
14898
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14899
+ continue;
14900
+ }
14901
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14902
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14903
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14904
+ if (wasRename) {
14905
+ newStatements[tableName2].push(...preparedStatements);
14906
+ } else {
14907
+ newStatements[tableName2] = preparedStatements;
14908
+ }
14909
+ continue;
14910
+ }
14911
+ continue;
14912
+ }
14913
+ if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
14914
+ const tableName2 = statement.tableName;
14915
+ const statementsForTable2 = newStatements[tableName2];
14916
+ if (!statementsForTable2) {
14917
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14918
+ continue;
14919
+ }
14920
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14921
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14922
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14923
+ if (wasRename) {
14924
+ newStatements[tableName2].push(...preparedStatements);
14925
+ } else {
14926
+ newStatements[tableName2] = preparedStatements;
14927
+ }
14928
+ continue;
14929
+ }
14930
+ continue;
14931
+ }
14932
+ if (statement.type === "create_reference") {
14933
+ const tableName2 = statement.tableName;
14934
+ const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
14935
+ const statementsForTable2 = newStatements[tableName2];
14936
+ if (!statementsForTable2) {
14937
+ newStatements[tableName2] = prepareSQLiteRecreateTable(json2.tables[tableName2], action);
14938
+ continue;
14939
+ }
14940
+ if (data.columnsFrom.length === 1 && statementsForTable2.some(
14941
+ (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
14942
+ )) {
14943
+ continue;
14944
+ }
14945
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14946
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14947
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14948
+ if (wasRename) {
14949
+ newStatements[tableName2].push(...preparedStatements);
14950
+ } else {
14951
+ newStatements[tableName2] = preparedStatements;
14952
+ }
14953
+ continue;
14954
+ }
14955
+ continue;
14956
+ }
14957
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
14958
+ const statementsForTable = newStatements[tableName];
14959
+ if (!statementsForTable) {
14960
+ newStatements[tableName] = [statement];
14961
+ continue;
14962
+ }
14963
+ if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
14964
+ newStatements[tableName].push(statement);
14965
+ }
14966
+ }
14967
+ const combinedStatements = Object.values(newStatements).flat();
14968
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
14969
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
14970
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
14971
+ return [...renamedTables, ...renamedColumns, ...rest];
14972
+ };
14973
+ }
14974
+ });
14975
+
14432
14976
  // src/snapshotsDiffer.ts
14433
14977
  var snapshotsDiffer_exports = {};
14434
14978
  __export(snapshotsDiffer_exports, {
14435
14979
  alteredTableScheme: () => alteredTableScheme,
14980
+ applyLibSQLSnapshotsDiff: () => applyLibSQLSnapshotsDiff,
14436
14981
  applyMysqlSnapshotsDiff: () => applyMysqlSnapshotsDiff,
14437
14982
  applyPgSnapshotsDiff: () => applyPgSnapshotsDiff,
14438
14983
  applySqliteSnapshotsDiff: () => applySqliteSnapshotsDiff,
@@ -14442,7 +14987,7 @@ __export(snapshotsDiffer_exports, {
14442
14987
  makePatched: () => makePatched,
14443
14988
  makeSelfOrPatched: () => makeSelfOrPatched
14444
14989
  });
14445
- var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff;
14990
+ var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff, applyLibSQLSnapshotsDiff;
14446
14991
  var init_snapshotsDiffer = __esm({
14447
14992
  "src/snapshotsDiffer.ts"() {
14448
14993
  "use strict";
@@ -14454,6 +14999,7 @@ var init_snapshotsDiffer = __esm({
14454
14999
  init_mysqlSchema();
14455
15000
  init_pgSchema();
14456
15001
  init_sqliteSchema();
15002
+ init_statementCombiner();
14457
15003
  init_utils();
14458
15004
  makeChanged = (schema4) => {
14459
15005
  return objectType({
@@ -15327,53 +15873,352 @@ var init_snapshotsDiffer = __esm({
15327
15873
  curFull.internal
15328
15874
  );
15329
15875
  }).flat();
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;
15876
+ const jsonDropTables = deletedTables.map((it) => {
15877
+ return prepareDropTableJson(it);
15878
+ });
15879
+ const jsonRenameTables = renamedTables.map((it) => {
15880
+ return prepareRenameTableJson(it.from, it.to);
15881
+ });
15882
+ const alteredTables = typedResult.alteredTablesWithColumns;
15883
+ const jsonAddedCompositePKs = [];
15884
+ const jsonDeletedCompositePKs = [];
15885
+ const jsonAlteredCompositePKs = [];
15886
+ const jsonAddedUniqueConstraints = [];
15887
+ const jsonDeletedUniqueConstraints = [];
15888
+ const jsonAlteredUniqueConstraints = [];
15889
+ const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
15890
+ const jsonAddColumnsStatemets = columnCreates.map((it) => _prepareAddColumns(it.table, "", it.columns)).flat();
15891
+ const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
15892
+ alteredTables.forEach((it) => {
15893
+ let addedColumns = [];
15894
+ for (const addedPkName of Object.keys(it.addedCompositePKs)) {
15895
+ const addedPkColumns = it.addedCompositePKs[addedPkName];
15896
+ addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
15897
+ }
15898
+ let deletedColumns = [];
15899
+ for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
15900
+ const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
15901
+ deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
15902
+ }
15903
+ const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
15904
+ let addedCompositePKs = [];
15905
+ let deletedCompositePKs = [];
15906
+ let alteredCompositePKs = [];
15907
+ addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
15908
+ it.name,
15909
+ it.addedCompositePKs,
15910
+ prevFull,
15911
+ curFull
15912
+ );
15913
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15914
+ it.name,
15915
+ it.deletedCompositePKs,
15916
+ prevFull
15917
+ );
15918
+ alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
15919
+ it.name,
15920
+ it.alteredCompositePKs,
15921
+ prevFull,
15922
+ curFull
15923
+ );
15924
+ let addedUniqueConstraints = [];
15925
+ let deletedUniqueConstraints = [];
15926
+ let alteredUniqueConstraints = [];
15927
+ addedUniqueConstraints = prepareAddUniqueConstraintPg(
15928
+ it.name,
15929
+ it.schema,
15930
+ it.addedUniqueConstraints
15931
+ );
15932
+ deletedUniqueConstraints = prepareDeleteUniqueConstraintPg(
15933
+ it.name,
15934
+ it.schema,
15935
+ it.deletedUniqueConstraints
15936
+ );
15937
+ if (it.alteredUniqueConstraints) {
15938
+ const added = {};
15939
+ const deleted = {};
15940
+ for (const k of Object.keys(it.alteredUniqueConstraints)) {
15941
+ added[k] = it.alteredUniqueConstraints[k].__new;
15942
+ deleted[k] = it.alteredUniqueConstraints[k].__old;
15943
+ }
15944
+ addedUniqueConstraints.push(
15945
+ ...prepareAddUniqueConstraintPg(it.name, it.schema, added)
15946
+ );
15947
+ deletedUniqueConstraints.push(
15948
+ ...prepareDeleteUniqueConstraintPg(it.name, it.schema, deleted)
15949
+ );
15950
+ }
15951
+ jsonAddedCompositePKs.push(...addedCompositePKs);
15952
+ jsonDeletedCompositePKs.push(...deletedCompositePKs);
15953
+ jsonAlteredCompositePKs.push(...alteredCompositePKs);
15954
+ jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
15955
+ jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15956
+ jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15957
+ });
15958
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
15959
+ const tableName = it.tableName;
15960
+ const schema4 = it.schema;
15961
+ return {
15962
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
15963
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
15964
+ };
15965
+ });
15966
+ const jsonTableAlternations = alteredTables.map((it) => {
15967
+ return prepareAlterColumnsMysql(
15968
+ it.name,
15969
+ it.schema,
15970
+ it.altered,
15971
+ json1,
15972
+ json2,
15973
+ action
15974
+ );
15975
+ }).flat();
15976
+ const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
15977
+ return prepareCreateIndexesJson(
15978
+ it.name,
15979
+ it.schema,
15980
+ it.addedIndexes || {},
15981
+ curFull.internal
15982
+ );
15983
+ }).flat();
15984
+ const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
15985
+ return prepareDropIndexesJson(
15986
+ it.name,
15987
+ it.schema,
15988
+ it.deletedIndexes || {}
15989
+ );
15990
+ }).flat();
15991
+ alteredTables.forEach((it) => {
15992
+ const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
15993
+ (current, item) => {
15994
+ current[item] = it.alteredIndexes[item].__old;
15995
+ return current;
15996
+ },
15997
+ {}
15998
+ );
15999
+ const createdIndexes = Object.keys(it.alteredIndexes).reduce(
16000
+ (current, item) => {
16001
+ current[item] = it.alteredIndexes[item].__new;
16002
+ return current;
16003
+ },
16004
+ {}
16005
+ );
16006
+ jsonCreateIndexesForAllAlteredTables.push(
16007
+ ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16008
+ );
16009
+ jsonDropIndexesForAllAlteredTables.push(
16010
+ ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
16011
+ );
16012
+ });
16013
+ const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
16014
+ return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
16015
+ }).flat();
16016
+ const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16017
+ const forAdded = prepareCreateReferencesJson(
16018
+ it.name,
16019
+ it.schema,
16020
+ it.addedForeignKeys
16021
+ );
16022
+ const forAltered = prepareDropReferencesJson(
16023
+ it.name,
16024
+ it.schema,
16025
+ it.deletedForeignKeys
16026
+ );
16027
+ const alteredFKs = prepareAlterReferencesJson(
16028
+ it.name,
16029
+ it.schema,
16030
+ it.alteredForeignKeys
16031
+ );
16032
+ return [...forAdded, ...forAltered, ...alteredFKs];
16033
+ }).flat();
16034
+ const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16035
+ (t) => t.type === "create_reference"
16036
+ );
16037
+ const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16038
+ (t) => t.type === "delete_reference"
16039
+ );
16040
+ const jsonMySqlCreateTables = createdTables.map((it) => {
16041
+ return prepareMySqlCreateTableJson(
16042
+ it,
16043
+ curFull,
16044
+ curFull.internal
16045
+ );
16046
+ });
16047
+ jsonStatements.push(...jsonMySqlCreateTables);
16048
+ jsonStatements.push(...jsonDropTables);
16049
+ jsonStatements.push(...jsonRenameTables);
16050
+ jsonStatements.push(...jsonRenameColumnsStatements);
16051
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16052
+ jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
16053
+ jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
16054
+ jsonStatements.push(...jsonDeletedCompositePKs);
16055
+ jsonStatements.push(...jsonTableAlternations);
16056
+ jsonStatements.push(...jsonAddedCompositePKs);
16057
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16058
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16059
+ jsonStatements.push(...jsonAddColumnsStatemets);
16060
+ jsonStatements.push(...jsonCreateReferencesForCreatedTables);
16061
+ jsonStatements.push(...jsonCreateIndexesForCreatedTables);
16062
+ jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
16063
+ jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16064
+ jsonStatements.push(...jsonDropColumnsStatemets);
16065
+ jsonStatements.push(...jsonAlteredCompositePKs);
16066
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16067
+ jsonStatements.push(...jsonAlteredUniqueConstraints);
16068
+ const sqlStatements = fromJson(jsonStatements, "mysql");
16069
+ const uniqueSqlStatements = [];
16070
+ sqlStatements.forEach((ss) => {
16071
+ if (!uniqueSqlStatements.includes(ss)) {
16072
+ uniqueSqlStatements.push(ss);
16073
+ }
16074
+ });
16075
+ const rTables = renamedTables.map((it) => {
16076
+ return { from: it.from, to: it.to };
16077
+ });
16078
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
16079
+ return {
16080
+ statements: jsonStatements,
16081
+ sqlStatements: uniqueSqlStatements,
16082
+ _meta
16083
+ };
16084
+ };
16085
+ applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16086
+ const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
16087
+ const {
16088
+ created: createdTables,
16089
+ deleted: deletedTables,
16090
+ renamed: renamedTables
16091
+ } = await tablesResolver2({
16092
+ created: tablesDiff.added,
16093
+ deleted: tablesDiff.deleted
16094
+ });
16095
+ const tablesPatchedSnap1 = copy(json1);
16096
+ tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_2, it) => {
16097
+ const { name: name2 } = nameChangeFor(it, renamedTables);
16098
+ it.name = name2;
16099
+ return [name2, it];
16100
+ });
16101
+ const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
16102
+ const columnRenames = [];
16103
+ const columnCreates = [];
16104
+ const columnDeletes = [];
16105
+ for (let entry of Object.values(res)) {
16106
+ const { renamed, created, deleted } = await columnsResolver2({
16107
+ tableName: entry.name,
16108
+ schema: entry.schema,
16109
+ deleted: entry.columns.deleted,
16110
+ created: entry.columns.added
16111
+ });
16112
+ if (created.length > 0) {
16113
+ columnCreates.push({
16114
+ table: entry.name,
16115
+ columns: created
16116
+ });
16117
+ }
16118
+ if (deleted.length > 0) {
16119
+ columnDeletes.push({
16120
+ table: entry.name,
16121
+ columns: deleted
16122
+ });
16123
+ }
16124
+ if (renamed.length > 0) {
16125
+ columnRenames.push({
16126
+ table: entry.name,
16127
+ renames: renamed
16128
+ });
16129
+ }
16130
+ }
16131
+ const columnRenamesDict = columnRenames.reduce(
16132
+ (acc, it) => {
16133
+ acc[it.table] = it.renames;
16134
+ return acc;
16135
+ },
16136
+ {}
16137
+ );
16138
+ const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
16139
+ columnsPatchedSnap1.tables = mapEntries(
16140
+ columnsPatchedSnap1.tables,
16141
+ (tableKey2, tableValue) => {
16142
+ const patchedColumns = mapKeys(
16143
+ tableValue.columns,
16144
+ (columnKey, column4) => {
16145
+ const rens = columnRenamesDict[tableValue.name] || [];
16146
+ const newName = columnChangeFor(columnKey, rens);
16147
+ column4.name = newName;
16148
+ return newName;
16149
+ }
16150
+ );
16151
+ tableValue.columns = patchedColumns;
16152
+ return [tableKey2, tableValue];
16153
+ }
16154
+ );
16155
+ const diffResult = applyJsonDiff(columnsPatchedSnap1, json2);
16156
+ const typedResult = diffResultSchemeSQLite.parse(diffResult);
16157
+ const tablesMap = {};
16158
+ typedResult.alteredTablesWithColumns.forEach((obj) => {
16159
+ tablesMap[obj.name] = obj;
16160
+ });
16161
+ const jsonCreateTables = createdTables.map((it) => {
16162
+ return prepareSQLiteCreateTable(it, action);
16163
+ });
16164
+ const jsonCreateIndexesForCreatedTables = createdTables.map((it) => {
16165
+ return prepareCreateIndexesJson(
16166
+ it.name,
16167
+ it.schema,
16168
+ it.indexes,
16169
+ curFull.internal
16170
+ );
16171
+ }).flat();
16172
+ const jsonDropTables = deletedTables.map((it) => {
16173
+ return prepareDropTableJson(it);
16174
+ });
16175
+ const jsonRenameTables = renamedTables.map((it) => {
16176
+ return prepareRenameTableJson(it.from, it.to);
16177
+ });
16178
+ const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
16179
+ const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
16180
+ const jsonAddColumnsStatemets = columnCreates.map((it) => {
16181
+ return _prepareSqliteAddColumns(
16182
+ it.table,
16183
+ it.columns,
16184
+ tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
16185
+ );
16186
+ }).flat();
16187
+ const allAltered = typedResult.alteredTablesWithColumns;
15337
16188
  const jsonAddedCompositePKs = [];
15338
16189
  const jsonDeletedCompositePKs = [];
15339
16190
  const jsonAlteredCompositePKs = [];
15340
16191
  const jsonAddedUniqueConstraints = [];
15341
16192
  const jsonDeletedUniqueConstraints = [];
15342
16193
  const jsonAlteredUniqueConstraints = [];
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) => {
16194
+ allAltered.forEach((it) => {
15347
16195
  let addedColumns = [];
15348
16196
  for (const addedPkName of Object.keys(it.addedCompositePKs)) {
15349
16197
  const addedPkColumns = it.addedCompositePKs[addedPkName];
15350
- addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
16198
+ addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
15351
16199
  }
15352
16200
  let deletedColumns = [];
15353
16201
  for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
15354
16202
  const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
15355
- deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
16203
+ deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
15356
16204
  }
15357
16205
  const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
15358
16206
  let addedCompositePKs = [];
15359
16207
  let deletedCompositePKs = [];
15360
16208
  let alteredCompositePKs = [];
15361
- addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
15362
- it.name,
15363
- it.addedCompositePKs,
15364
- prevFull,
15365
- curFull
15366
- );
15367
- deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15368
- it.name,
15369
- it.deletedCompositePKs,
15370
- prevFull
15371
- );
15372
- alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
16209
+ if (doPerformDeleteAndCreate) {
16210
+ addedCompositePKs = prepareAddCompositePrimaryKeySqlite(
16211
+ it.name,
16212
+ it.addedCompositePKs
16213
+ );
16214
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeySqlite(
16215
+ it.name,
16216
+ it.deletedCompositePKs
16217
+ );
16218
+ }
16219
+ alteredCompositePKs = prepareAlterCompositePrimaryKeySqlite(
15373
16220
  it.name,
15374
- it.alteredCompositePKs,
15375
- prevFull,
15376
- curFull
16221
+ it.alteredCompositePKs
15377
16222
  );
15378
16223
  let addedUniqueConstraints = [];
15379
16224
  let deletedUniqueConstraints = [];
@@ -15417,17 +16262,10 @@ var init_snapshotsDiffer = __esm({
15417
16262
  to: { schema: schema4, table: tableName, column: it.newColumnName }
15418
16263
  };
15419
16264
  });
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
- );
16265
+ const jsonTableAlternations = allAltered.map((it) => {
16266
+ return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15429
16267
  }).flat();
15430
- const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
16268
+ const jsonCreateIndexesForAllAlteredTables = allAltered.map((it) => {
15431
16269
  return prepareCreateIndexesJson(
15432
16270
  it.name,
15433
16271
  it.schema,
@@ -15435,14 +16273,14 @@ var init_snapshotsDiffer = __esm({
15435
16273
  curFull.internal
15436
16274
  );
15437
16275
  }).flat();
15438
- const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16276
+ const jsonDropIndexesForAllAlteredTables = allAltered.map((it) => {
15439
16277
  return prepareDropIndexesJson(
15440
16278
  it.name,
15441
16279
  it.schema,
15442
16280
  it.deletedIndexes || {}
15443
16281
  );
15444
16282
  }).flat();
15445
- alteredTables.forEach((it) => {
16283
+ allAltered.forEach((it) => {
15446
16284
  const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
15447
16285
  (current, item) => {
15448
16286
  current[item] = it.alteredIndexes[item].__old;
@@ -15458,16 +16296,18 @@ var init_snapshotsDiffer = __esm({
15458
16296
  {}
15459
16297
  );
15460
16298
  jsonCreateIndexesForAllAlteredTables.push(
15461
- ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16299
+ ...prepareCreateIndexesJson(
16300
+ it.name,
16301
+ it.schema,
16302
+ createdIndexes || {},
16303
+ curFull.internal
16304
+ )
15462
16305
  );
15463
16306
  jsonDropIndexesForAllAlteredTables.push(
15464
16307
  ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
15465
16308
  );
15466
16309
  });
15467
- const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
15468
- return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
15469
- }).flat();
15470
- const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16310
+ const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15471
16311
  const forAdded = prepareCreateReferencesJson(
15472
16312
  it.name,
15473
16313
  it.schema,
@@ -15491,35 +16331,25 @@ var init_snapshotsDiffer = __esm({
15491
16331
  const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
15492
16332
  (t) => t.type === "delete_reference"
15493
16333
  );
15494
- const jsonMySqlCreateTables = createdTables.map((it) => {
15495
- return prepareMySqlCreateTableJson(
15496
- it,
15497
- curFull,
15498
- curFull.internal
15499
- );
15500
- });
15501
- jsonStatements.push(...jsonMySqlCreateTables);
16334
+ const jsonStatements = [];
16335
+ jsonStatements.push(...jsonCreateTables);
15502
16336
  jsonStatements.push(...jsonDropTables);
15503
16337
  jsonStatements.push(...jsonRenameTables);
15504
16338
  jsonStatements.push(...jsonRenameColumnsStatements);
15505
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15506
16339
  jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
15507
16340
  jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
15508
16341
  jsonStatements.push(...jsonDeletedCompositePKs);
15509
16342
  jsonStatements.push(...jsonTableAlternations);
15510
16343
  jsonStatements.push(...jsonAddedCompositePKs);
15511
- jsonStatements.push(...jsonAddedUniqueConstraints);
15512
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15513
16344
  jsonStatements.push(...jsonAddColumnsStatemets);
15514
- jsonStatements.push(...jsonCreateReferencesForCreatedTables);
15515
16345
  jsonStatements.push(...jsonCreateIndexesForCreatedTables);
15516
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15517
16346
  jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16347
+ jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15518
16348
  jsonStatements.push(...jsonDropColumnsStatemets);
15519
16349
  jsonStatements.push(...jsonAlteredCompositePKs);
15520
- jsonStatements.push(...jsonAddedUniqueConstraints);
15521
16350
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15522
- const sqlStatements = fromJson(jsonStatements, "mysql");
16351
+ const combinedJsonStatements = sqliteCombineStatements(jsonStatements, json2, action);
16352
+ const sqlStatements = fromJson(combinedJsonStatements, "sqlite");
15523
16353
  const uniqueSqlStatements = [];
15524
16354
  sqlStatements.forEach((ss) => {
15525
16355
  if (!uniqueSqlStatements.includes(ss)) {
@@ -15531,12 +16361,12 @@ var init_snapshotsDiffer = __esm({
15531
16361
  });
15532
16362
  const _meta = prepareMigrationMeta([], rTables, rColumns);
15533
16363
  return {
15534
- statements: jsonStatements,
16364
+ statements: combinedJsonStatements,
15535
16365
  sqlStatements: uniqueSqlStatements,
15536
16366
  _meta
15537
16367
  };
15538
16368
  };
15539
- applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16369
+ applyLibSQLSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
15540
16370
  const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
15541
16371
  const {
15542
16372
  created: createdTables,
@@ -15638,6 +16468,18 @@ var init_snapshotsDiffer = __esm({
15638
16468
  tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
15639
16469
  );
15640
16470
  }).flat();
16471
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
16472
+ const tableName = it.tableName;
16473
+ const schema4 = it.schema;
16474
+ return {
16475
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
16476
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
16477
+ };
16478
+ });
16479
+ const rTables = renamedTables.map((it) => {
16480
+ return { from: it.from, to: it.to };
16481
+ });
16482
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
15641
16483
  const allAltered = typedResult.alteredTablesWithColumns;
15642
16484
  const jsonAddedCompositePKs = [];
15643
16485
  const jsonDeletedCompositePKs = [];
@@ -15708,14 +16550,6 @@ var init_snapshotsDiffer = __esm({
15708
16550
  jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15709
16551
  jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15710
16552
  });
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
- });
15719
16553
  const jsonTableAlternations = allAltered.map((it) => {
15720
16554
  return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15721
16555
  }).flat();
@@ -15762,21 +16596,22 @@ var init_snapshotsDiffer = __esm({
15762
16596
  );
15763
16597
  });
15764
16598
  const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15765
- const forAdded = prepareCreateReferencesJson(
15766
- it.name,
15767
- it.schema,
15768
- it.addedForeignKeys
15769
- );
15770
- const forAltered = prepareDropReferencesJson(
16599
+ const forAdded = prepareLibSQLCreateReferencesJson(
15771
16600
  it.name,
15772
16601
  it.schema,
15773
- it.deletedForeignKeys
16602
+ it.addedForeignKeys,
16603
+ json2,
16604
+ action
15774
16605
  );
15775
- const alteredFKs = prepareAlterReferencesJson(
16606
+ const forAltered = prepareLibSQLDropReferencesJson(
15776
16607
  it.name,
15777
16608
  it.schema,
15778
- it.alteredForeignKeys
16609
+ it.deletedForeignKeys,
16610
+ json2,
16611
+ _meta,
16612
+ action
15779
16613
  );
16614
+ const alteredFKs = prepareAlterReferencesJson(it.name, it.schema, it.alteredForeignKeys);
15780
16615
  return [...forAdded, ...forAltered, ...alteredFKs];
15781
16616
  }).flat();
15782
16617
  const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
@@ -15802,19 +16637,22 @@ var init_snapshotsDiffer = __esm({
15802
16637
  jsonStatements.push(...jsonDropColumnsStatemets);
15803
16638
  jsonStatements.push(...jsonAlteredCompositePKs);
15804
16639
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15805
- const sqlStatements = fromJson(jsonStatements, "sqlite");
16640
+ const combinedJsonStatements = libSQLCombineStatements(jsonStatements, json2, action);
16641
+ const sqlStatements = fromJson(
16642
+ combinedJsonStatements,
16643
+ "sqlite",
16644
+ action,
16645
+ "turso",
16646
+ json2
16647
+ );
15806
16648
  const uniqueSqlStatements = [];
15807
16649
  sqlStatements.forEach((ss) => {
15808
16650
  if (!uniqueSqlStatements.includes(ss)) {
15809
16651
  uniqueSqlStatements.push(ss);
15810
16652
  }
15811
16653
  });
15812
- const rTables = renamedTables.map((it) => {
15813
- return { from: it.from, to: it.to };
15814
- });
15815
- const _meta = prepareMigrationMeta([], rTables, rColumns);
15816
16654
  return {
15817
- statements: jsonStatements,
16655
+ statements: combinedJsonStatements,
15818
16656
  sqlStatements: uniqueSqlStatements,
15819
16657
  _meta
15820
16658
  };
@@ -33876,7 +34714,8 @@ var init_cli = __esm({
33876
34714
  schemaFilter: unionType([stringType(), stringType().array()]).optional().default(["public"]),
33877
34715
  extensionsFilters: literalType("postgis").array().optional(),
33878
34716
  verbose: booleanType().optional(),
33879
- strict: booleanType().optional()
34717
+ strict: booleanType().optional(),
34718
+ driver: driver.optional()
33880
34719
  }).passthrough();
33881
34720
  pullParams = objectType({
33882
34721
  config: stringType().optional(),
@@ -35028,284 +35867,8 @@ var sqlitePushIntrospect = async (db, filters) => {
35028
35867
  return { schema: schema4 };
35029
35868
  };
35030
35869
 
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
-
35308
35870
  // src/api.ts
35871
+ init_sqlitePushUtils();
35309
35872
  init_global();
35310
35873
  init_migrationPreparator();
35311
35874
  init_mysqlSchema();