drizzle-kit 0.24.0-6386ea9 → 0.24.0-bddd952

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 +1616 -641
  4. package/package.json +1 -1
  5. package/utils.js +9 -0
  6. package/utils.mjs +8 -0
package/api.js CHANGED
@@ -8341,6 +8341,13 @@ var init_sqliteSchema = __esm({
8341
8341
  function isPgArrayType(sqlType) {
8342
8342
  return sqlType.match(/.*\[\d*\].*|.*\[\].*/g) !== null;
8343
8343
  }
8344
+ function findAddedAndRemoved(columnNames1, columnNames2) {
8345
+ const set1 = new Set(columnNames1);
8346
+ const set2 = new Set(columnNames2);
8347
+ const addedColumns = columnNames2.filter((it) => !set1.has(it));
8348
+ const removedColumns = columnNames1.filter((it) => !set2.has(it));
8349
+ return { addedColumns, removedColumns };
8350
+ }
8344
8351
  var copy, prepareMigrationMeta, schemaRenameKey, tableRenameKey, columnRenameKey;
8345
8352
  var init_utils = __esm({
8346
8353
  "src/utils.ts"() {
@@ -11450,7 +11457,20 @@ var init_jsonDiffer = __esm({
11450
11457
  });
11451
11458
 
11452
11459
  // src/sqlgenerator.ts
11453
- 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;
11460
+ function fromJson(statements, dialect7, action, driver2, json2) {
11461
+ const result = statements.flatMap((statement) => {
11462
+ const filtered = convertors.filter((it) => {
11463
+ return it.can(statement, dialect7, driver2);
11464
+ });
11465
+ const convertor = filtered.length === 1 ? filtered[0] : void 0;
11466
+ if (!convertor) {
11467
+ return "";
11468
+ }
11469
+ return convertor.convert(statement, json2, action);
11470
+ }).filter((it) => it !== "");
11471
+ return result;
11472
+ }
11473
+ 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;
11454
11474
  var init_sqlgenerator = __esm({
11455
11475
  "src/sqlgenerator.ts"() {
11456
11476
  "use strict";
@@ -11772,32 +11792,6 @@ var init_sqlgenerator = __esm({
11772
11792
  return `ALTER TABLE \`${statement.tableName}\` DROP INDEX \`${unsquashed.name}\`;`;
11773
11793
  }
11774
11794
  };
11775
- SQLiteAlterTableAddUniqueConstraintConvertor = class extends Convertor {
11776
- can(statement, dialect7) {
11777
- return statement.type === "create_unique_constraint" && dialect7 === "sqlite";
11778
- }
11779
- convert(statement) {
11780
- return `/*
11781
- 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
11782
- Please refer to: https://www.techonthenet.com/sqlite/unique.php
11783
-
11784
- Due to that we don't generate migration automatically and it has to be done manually
11785
- */`;
11786
- }
11787
- };
11788
- SQLiteAlterTableDropUniqueConstraintConvertor = class extends Convertor {
11789
- can(statement, dialect7) {
11790
- return statement.type === "delete_unique_constraint" && dialect7 === "sqlite";
11791
- }
11792
- convert(statement) {
11793
- return `/*
11794
- 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
11795
- Please refer to: https://www.techonthenet.com/sqlite/unique.php
11796
-
11797
- Due to that we don't generate migration automatically and it has to be done manually
11798
- */`;
11799
- }
11800
- };
11801
11795
  CreatePgSequenceConvertor = class extends Convertor {
11802
11796
  can(statement, dialect7) {
11803
11797
  return statement.type === "create_sequence" && dialect7 === "postgresql";
@@ -11966,7 +11960,7 @@ var init_sqlgenerator = __esm({
11966
11960
  }
11967
11961
  convert(statement) {
11968
11962
  const { tableName, oldColumnName, newColumnName } = statement;
11969
- return `ALTER TABLE \`${tableName}\` RENAME COLUMN \`${oldColumnName}\` TO \`${newColumnName}\`;`;
11963
+ return `ALTER TABLE \`${tableName}\` RENAME COLUMN "${oldColumnName}" TO "${newColumnName}";`;
11970
11964
  }
11971
11965
  };
11972
11966
  PgAlterTableDropColumnConvertor = class extends Convertor {
@@ -12067,21 +12061,6 @@ var init_sqlgenerator = __esm({
12067
12061
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DATA TYPE ${newDataType};`;
12068
12062
  }
12069
12063
  };
12070
- SQLiteAlterTableAlterColumnSetTypeConvertor = class extends Convertor {
12071
- can(statement, dialect7) {
12072
- return statement.type === "alter_table_alter_column_set_type" && dialect7 === "sqlite";
12073
- }
12074
- convert(statement) {
12075
- return `/*
12076
- 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
12077
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12078
- https://www.sqlite.org/lang_altertable.html
12079
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12080
-
12081
- Due to that we don't generate migration automatically and it has to be done manually
12082
- */`;
12083
- }
12084
- };
12085
12064
  PgAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
12086
12065
  can(statement, dialect7) {
12087
12066
  return statement.type === "alter_table_alter_column_set_default" && dialect7 === "postgresql";
@@ -12092,21 +12071,6 @@ var init_sqlgenerator = __esm({
12092
12071
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DEFAULT ${statement.newDefaultValue};`;
12093
12072
  }
12094
12073
  };
12095
- SqliteAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
12096
- can(statement, dialect7) {
12097
- return statement.type === "alter_table_alter_column_set_default" && dialect7 === "sqlite";
12098
- }
12099
- convert(statement) {
12100
- return `/*
12101
- 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
12102
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12103
- https://www.sqlite.org/lang_altertable.html
12104
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12105
-
12106
- Due to that we don't generate migration automatically and it has to be done manually
12107
- */`;
12108
- }
12109
- };
12110
12074
  PgAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
12111
12075
  can(statement, dialect7) {
12112
12076
  return statement.type === "alter_table_alter_column_drop_default" && dialect7 === "postgresql";
@@ -12380,6 +12344,67 @@ var init_sqlgenerator = __esm({
12380
12344
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY`;
12381
12345
  }
12382
12346
  };
12347
+ LibSQLModifyColumn = class extends Convertor {
12348
+ can(statement, dialect7, driver2) {
12349
+ 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";
12350
+ }
12351
+ convert(statement, json2, action) {
12352
+ const { tableName, columnName } = statement;
12353
+ let columnType = ``;
12354
+ let columnDefault = "";
12355
+ let columnNotNull = "";
12356
+ const sqlStatements = [];
12357
+ const indexes = [];
12358
+ for (const table4 of Object.values(json2.tables)) {
12359
+ for (const index4 of Object.values(table4.indexes)) {
12360
+ const unsquashed = SQLiteSquasher.unsquashIdx(index4);
12361
+ sqlStatements.push(`DROP INDEX IF EXISTS "${unsquashed.name}";`);
12362
+ indexes.push({ ...unsquashed, tableName: table4.name });
12363
+ }
12364
+ }
12365
+ switch (statement.type) {
12366
+ case "alter_table_alter_column_set_type":
12367
+ columnType = ` ${statement.newDataType}`;
12368
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12369
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12370
+ break;
12371
+ case "alter_table_alter_column_drop_notnull":
12372
+ columnType = ` ${statement.newDataType}`;
12373
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12374
+ columnNotNull = "";
12375
+ break;
12376
+ case "alter_table_alter_column_set_notnull":
12377
+ columnType = ` ${statement.newDataType}`;
12378
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12379
+ columnNotNull = ` NOT NULL`;
12380
+ break;
12381
+ case "alter_table_alter_column_set_default":
12382
+ columnType = ` ${statement.newDataType}`;
12383
+ columnDefault = ` DEFAULT ${statement.newDefaultValue}`;
12384
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12385
+ break;
12386
+ case "alter_table_alter_column_drop_default":
12387
+ columnType = ` ${statement.newDataType}`;
12388
+ columnDefault = "";
12389
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12390
+ break;
12391
+ }
12392
+ columnDefault = columnDefault instanceof Date ? columnDefault.toISOString() : columnDefault;
12393
+ sqlStatements.push(
12394
+ `ALTER TABLE \`${tableName}\` ALTER COLUMN "${columnName}" TO "${columnName}"${columnType}${columnNotNull}${columnDefault};`
12395
+ );
12396
+ for (const index4 of indexes) {
12397
+ const indexPart = index4.isUnique ? "UNIQUE INDEX" : "INDEX";
12398
+ const whereStatement = index4.where ? ` WHERE ${index4.where}` : "";
12399
+ const uniqueString = index4.columns.map((it) => `\`${it}\``).join(",");
12400
+ const tableName2 = index4.tableName;
12401
+ sqlStatements.push(
12402
+ `CREATE ${indexPart} \`${index4.name}\` ON \`${tableName2}\` (${uniqueString})${whereStatement};`
12403
+ );
12404
+ }
12405
+ return sqlStatements;
12406
+ }
12407
+ };
12383
12408
  MySqlModifyColumn = class extends Convertor {
12384
12409
  can(statement, dialect7) {
12385
12410
  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";
@@ -12517,21 +12542,6 @@ var init_sqlgenerator = __esm({
12517
12542
  return `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${columnName}\`${columnType}${columnAutoincrement}${columnNotNull}${columnDefault}${columnOnUpdate}${columnGenerated};`;
12518
12543
  }
12519
12544
  };
12520
- SqliteAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
12521
- can(statement, dialect7) {
12522
- return statement.type === "alter_table_alter_column_drop_default" && dialect7 === "sqlite";
12523
- }
12524
- convert(statement) {
12525
- return `/*
12526
- 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
12527
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12528
- https://www.sqlite.org/lang_altertable.html
12529
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12530
-
12531
- Due to that we don't generate migration automatically and it has to be done manually
12532
- */`;
12533
- }
12534
- };
12535
12545
  PgAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
12536
12546
  can(statement, dialect7) {
12537
12547
  return statement.type === "create_composite_pk" && dialect7 === "postgresql";
@@ -12596,85 +12606,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12596
12606
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY, ADD PRIMARY KEY(\`${newColumns.join("`,`")}\`);`;
12597
12607
  }
12598
12608
  };
12599
- SqliteAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
12600
- can(statement, dialect7) {
12601
- return statement.type === "create_composite_pk" && dialect7 === "sqlite";
12602
- }
12603
- convert(statement) {
12604
- let msg = "/*\n";
12605
- msg += `You're trying to add PRIMARY KEY(${statement.data}) to '${statement.tableName}' table
12606
- `;
12607
- msg += "SQLite does not support adding primary key to an already created table\n";
12608
- msg += "You can do it in 3 steps with drizzle orm:\n";
12609
- msg += " - create new mirror table with needed pk, rename current table to old_table, generate SQL\n";
12610
- msg += " - migrate old data from one table to another\n";
12611
- msg += " - delete old_table in schema, generate sql\n\n";
12612
- msg += "or create manual migration like below:\n\n";
12613
- msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12614
- msg += "CREATE TABLE table_name (\n";
12615
- msg += " column1 datatype [ NULL | NOT NULL ],\n";
12616
- msg += " column2 datatype [ NULL | NOT NULL ],\n";
12617
- msg += " ...\n";
12618
- msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12619
- msg += " );\n";
12620
- msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12621
- msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12622
- msg += "*/\n";
12623
- return msg;
12624
- }
12625
- };
12626
- SqliteAlterTableDeleteCompositePrimaryKeyConvertor = class extends Convertor {
12627
- can(statement, dialect7) {
12628
- return statement.type === "delete_composite_pk" && dialect7 === "sqlite";
12629
- }
12630
- convert(statement) {
12631
- let msg = "/*\n";
12632
- msg += `You're trying to delete PRIMARY KEY(${statement.data}) from '${statement.tableName}' table
12633
- `;
12634
- msg += "SQLite does not supportprimary key deletion from existing table\n";
12635
- msg += "You can do it in 3 steps with drizzle orm:\n";
12636
- msg += " - create new mirror table table without pk, rename current table to old_table, generate SQL\n";
12637
- msg += " - migrate old data from one table to another\n";
12638
- msg += " - delete old_table in schema, generate sql\n\n";
12639
- msg += "or create manual migration like below:\n\n";
12640
- msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12641
- msg += "CREATE TABLE table_name (\n";
12642
- msg += " column1 datatype [ NULL | NOT NULL ],\n";
12643
- msg += " column2 datatype [ NULL | NOT NULL ],\n";
12644
- msg += " ...\n";
12645
- msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12646
- msg += " );\n";
12647
- msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12648
- msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12649
- msg += "*/\n";
12650
- return msg;
12651
- }
12652
- };
12653
- SqliteAlterTableAlterCompositePrimaryKeyConvertor = class extends Convertor {
12654
- can(statement, dialect7) {
12655
- return statement.type === "alter_composite_pk" && dialect7 === "sqlite";
12656
- }
12657
- convert(statement) {
12658
- let msg = "/*\n";
12659
- msg += "SQLite does not support altering primary key\n";
12660
- msg += "You can do it in 3 steps with drizzle orm:\n";
12661
- msg += " - create new mirror table with needed pk, rename current table to old_table, generate SQL\n";
12662
- msg += " - migrate old data from one table to another\n";
12663
- msg += " - delete old_table in schema, generate sql\n\n";
12664
- msg += "or create manual migration like below:\n\n";
12665
- msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12666
- msg += "CREATE TABLE table_name (\n";
12667
- msg += " column1 datatype [ NULL | NOT NULL ],\n";
12668
- msg += " column2 datatype [ NULL | NOT NULL ],\n";
12669
- msg += " ...\n";
12670
- msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12671
- msg += " );\n";
12672
- msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12673
- msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12674
- msg += "*/\n";
12675
- return msg;
12676
- }
12677
- };
12678
12609
  PgAlterTableAlterColumnSetPrimaryKeyConvertor = class extends Convertor {
12679
12610
  can(statement, dialect7) {
12680
12611
  return statement.type === "alter_table_alter_column_set_pk" && dialect7 === "postgresql";
@@ -12719,51 +12650,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12719
12650
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET NOT NULL;`;
12720
12651
  }
12721
12652
  };
12722
- SqliteAlterTableAlterColumnSetNotNullConvertor = class extends Convertor {
12723
- can(statement, dialect7) {
12724
- return statement.type === "alter_table_alter_column_set_notnull" && dialect7 === "sqlite";
12725
- }
12726
- convert(statement) {
12727
- return `/*
12728
- 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
12729
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12730
- https://www.sqlite.org/lang_altertable.html
12731
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12732
-
12733
- Due to that we don't generate migration automatically and it has to be done manually
12734
- */`;
12735
- }
12736
- };
12737
- SqliteAlterTableAlterColumnSetAutoincrementConvertor = class extends Convertor {
12738
- can(statement, dialect7) {
12739
- return statement.type === "alter_table_alter_column_set_autoincrement" && dialect7 === "sqlite";
12740
- }
12741
- convert(statement) {
12742
- return `/*
12743
- 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
12744
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12745
- https://www.sqlite.org/lang_altertable.html
12746
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12747
-
12748
- Due to that we don't generate migration automatically and it has to be done manually
12749
- */`;
12750
- }
12751
- };
12752
- SqliteAlterTableAlterColumnDropAutoincrementConvertor = class extends Convertor {
12753
- can(statement, dialect7) {
12754
- return statement.type === "alter_table_alter_column_drop_autoincrement" && dialect7 === "sqlite";
12755
- }
12756
- convert(statement) {
12757
- return `/*
12758
- 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
12759
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12760
- https://www.sqlite.org/lang_altertable.html
12761
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12762
-
12763
- Due to that we don't generate migration automatically and it has to be done manually
12764
- */`;
12765
- }
12766
- };
12767
12653
  PgAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12768
12654
  can(statement, dialect7) {
12769
12655
  return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "postgresql";
@@ -12774,21 +12660,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12774
12660
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" DROP NOT NULL;`;
12775
12661
  }
12776
12662
  };
12777
- SqliteAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12778
- can(statement, dialect7) {
12779
- return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "sqlite";
12780
- }
12781
- convert(statement) {
12782
- return `/*
12783
- 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
12784
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12785
- https://www.sqlite.org/lang_altertable.html
12786
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12787
-
12788
- Due to that we don't generate migration automatically and it has to be done manually
12789
- */`;
12790
- }
12791
- };
12792
12663
  PgCreateForeignKeyConvertor = class extends Convertor {
12793
12664
  can(statement, dialect7) {
12794
12665
  return statement.type === "create_reference" && dialect7 === "postgresql";
@@ -12819,18 +12690,21 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12819
12690
  return sql2;
12820
12691
  }
12821
12692
  };
12822
- SqliteCreateForeignKeyConvertor = class extends Convertor {
12823
- can(statement, dialect7) {
12824
- return statement.type === "create_reference" && dialect7 === "sqlite";
12693
+ LibSQLCreateForeignKeyConvertor = class extends Convertor {
12694
+ can(statement, dialect7, driver2) {
12695
+ return statement.type === "create_reference" && dialect7 === "sqlite" && driver2 === "turso";
12825
12696
  }
12826
- convert(statement) {
12827
- return `/*
12828
- 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
12829
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12830
- https://www.sqlite.org/lang_altertable.html
12831
-
12832
- Due to that we don't generate migration automatically and it has to be done manually
12833
- */`;
12697
+ convert(statement, json2, action) {
12698
+ const { columnsFrom, columnsTo, tableFrom, onDelete, onUpdate, tableTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
12699
+ const { columnDefault, columnNotNull, columnType } = statement;
12700
+ const onDeleteStatement = onDelete ? ` ON DELETE ${onDelete}` : "";
12701
+ const onUpdateStatement = onUpdate ? ` ON UPDATE ${onUpdate}` : "";
12702
+ const columnsDefaultValue = columnDefault ? ` DEFAULT ${columnDefault}` : "";
12703
+ const columnNotNullValue = columnNotNull ? ` NOT NULL` : "";
12704
+ const columnTypeValue = columnType ? ` ${columnType}` : "";
12705
+ const columnFrom = columnsFrom[0];
12706
+ const columnTo = columnsTo[0];
12707
+ return `ALTER TABLE \`${tableFrom}\` ALTER COLUMN "${columnFrom}" TO "${columnFrom}"${columnTypeValue}${columnNotNullValue}${columnsDefaultValue} REFERENCES ${tableTo}(${columnTo})${onDeleteStatement}${onUpdateStatement};`;
12834
12708
  }
12835
12709
  };
12836
12710
  MySqlCreateForeignKeyConvertor = class extends Convertor {
@@ -12879,20 +12753,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12879
12753
  return sql2;
12880
12754
  }
12881
12755
  };
12882
- SqliteAlterForeignKeyConvertor = class extends Convertor {
12883
- can(statement, dialect7) {
12884
- return statement.type === "alter_reference" && dialect7 === "sqlite";
12885
- }
12886
- convert(statement) {
12887
- return `/*
12888
- 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
12889
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12890
- https://www.sqlite.org/lang_altertable.html
12891
-
12892
- Due to that we don't generate migration automatically and it has to be done manually
12893
- */`;
12894
- }
12895
- };
12896
12756
  PgDeleteForeignKeyConvertor = class extends Convertor {
12897
12757
  can(statement, dialect7) {
12898
12758
  return statement.type === "delete_reference" && dialect7 === "postgresql";
@@ -12905,20 +12765,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12905
12765
  `;
12906
12766
  }
12907
12767
  };
12908
- SqliteDeleteForeignKeyConvertor = class extends Convertor {
12909
- can(statement, dialect7) {
12910
- return statement.type === "delete_reference" && dialect7 === "sqlite";
12911
- }
12912
- convert(statement) {
12913
- return `/*
12914
- 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
12915
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12916
- https://www.sqlite.org/lang_altertable.html
12917
-
12918
- Due to that we don't generate migration automatically and it has to be done manually
12919
- */`;
12920
- }
12921
- };
12922
12768
  MySqlDeleteForeignKeyConvertor = class extends Convertor {
12923
12769
  can(statement, dialect7) {
12924
12770
  return statement.type === "delete_reference" && dialect7 === "mysql";
@@ -13082,10 +12928,96 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13082
12928
  return `DROP INDEX \`${name2}\` ON \`${statement.tableName}\`;`;
13083
12929
  }
13084
12930
  };
12931
+ SQLiteRecreateTableConvertor = class extends Convertor {
12932
+ can(statement, dialect7, driver2) {
12933
+ return statement.type === "recreate_table" && dialect7 === "sqlite" && !driver2;
12934
+ }
12935
+ convert(statement) {
12936
+ const { tableName, columns, compositePKs, referenceData } = statement;
12937
+ const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
12938
+ const newTableName = `__new_${tableName}`;
12939
+ const sqlStatements = [];
12940
+ sqlStatements.push(`PRAGMA foreign_keys=OFF;`);
12941
+ sqlStatements.push(
12942
+ new SQLiteCreateTableConvertor().convert({
12943
+ type: "sqlite_create_table",
12944
+ tableName: newTableName,
12945
+ columns,
12946
+ referenceData,
12947
+ compositePKs
12948
+ })
12949
+ );
12950
+ sqlStatements.push(
12951
+ `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
12952
+ );
12953
+ sqlStatements.push(
12954
+ new SQLiteDropTableConvertor().convert({
12955
+ type: "drop_table",
12956
+ tableName,
12957
+ schema: ""
12958
+ })
12959
+ );
12960
+ sqlStatements.push(
12961
+ new SqliteRenameTableConvertor().convert({
12962
+ fromSchema: "",
12963
+ tableNameFrom: newTableName,
12964
+ tableNameTo: tableName,
12965
+ toSchema: "",
12966
+ type: "rename_table"
12967
+ })
12968
+ );
12969
+ sqlStatements.push(`PRAGMA foreign_keys=ON;`);
12970
+ return sqlStatements;
12971
+ }
12972
+ };
12973
+ LibSQLRecreateTableConvertor = class extends Convertor {
12974
+ can(statement, dialect7, driver2) {
12975
+ return statement.type === "recreate_table" && dialect7 === "sqlite" && driver2 === "turso";
12976
+ }
12977
+ convert(statement) {
12978
+ const { tableName, columns, compositePKs, referenceData } = statement;
12979
+ const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
12980
+ const newTableName = `__new_${tableName}`;
12981
+ const sqlStatements = [];
12982
+ sqlStatements.push(`PRAGMA foreign_keys=OFF;`);
12983
+ sqlStatements.push(
12984
+ new SQLiteCreateTableConvertor().convert({
12985
+ type: "sqlite_create_table",
12986
+ tableName: newTableName,
12987
+ columns,
12988
+ referenceData,
12989
+ compositePKs
12990
+ })
12991
+ );
12992
+ sqlStatements.push(
12993
+ `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
12994
+ );
12995
+ sqlStatements.push(
12996
+ new SQLiteDropTableConvertor().convert({
12997
+ type: "drop_table",
12998
+ tableName,
12999
+ schema: ""
13000
+ })
13001
+ );
13002
+ sqlStatements.push(
13003
+ new SqliteRenameTableConvertor().convert({
13004
+ fromSchema: "",
13005
+ tableNameFrom: newTableName,
13006
+ tableNameTo: tableName,
13007
+ toSchema: "",
13008
+ type: "rename_table"
13009
+ })
13010
+ );
13011
+ sqlStatements.push(`PRAGMA foreign_keys=ON;`);
13012
+ return sqlStatements;
13013
+ }
13014
+ };
13085
13015
  convertors = [];
13086
13016
  convertors.push(new PgCreateTableConvertor());
13087
13017
  convertors.push(new MySqlCreateTableConvertor());
13088
13018
  convertors.push(new SQLiteCreateTableConvertor());
13019
+ convertors.push(new SQLiteRecreateTableConvertor());
13020
+ convertors.push(new LibSQLRecreateTableConvertor());
13089
13021
  convertors.push(new CreateTypeEnumConvertor());
13090
13022
  convertors.push(new CreatePgSequenceConvertor());
13091
13023
  convertors.push(new DropPgSequenceConvertor());
@@ -13133,6 +13065,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13133
13065
  convertors.push(new SqliteAlterTableAlterColumnAlterGeneratedConvertor());
13134
13066
  convertors.push(new SqliteAlterTableAlterColumnSetExpressionConvertor());
13135
13067
  convertors.push(new MySqlModifyColumn());
13068
+ convertors.push(new LibSQLModifyColumn());
13136
13069
  convertors.push(new PgCreateForeignKeyConvertor());
13137
13070
  convertors.push(new MySqlCreateForeignKeyConvertor());
13138
13071
  convertors.push(new PgAlterForeignKeyConvertor());
@@ -13144,24 +13077,10 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13144
13077
  convertors.push(new PgAlterTableSetSchemaConvertor());
13145
13078
  convertors.push(new PgAlterTableSetNewSchemaConvertor());
13146
13079
  convertors.push(new PgAlterTableRemoveFromSchemaConvertor());
13147
- convertors.push(new SQLiteAlterTableAlterColumnSetTypeConvertor());
13148
- convertors.push(new SqliteAlterForeignKeyConvertor());
13149
- convertors.push(new SqliteDeleteForeignKeyConvertor());
13150
- convertors.push(new SqliteCreateForeignKeyConvertor());
13151
- convertors.push(new SQLiteAlterTableAddUniqueConstraintConvertor());
13152
- convertors.push(new SQLiteAlterTableDropUniqueConstraintConvertor());
13080
+ convertors.push(new LibSQLCreateForeignKeyConvertor());
13153
13081
  convertors.push(new PgAlterTableAlterColumnDropGenerated());
13154
13082
  convertors.push(new PgAlterTableAlterColumnSetGenerated());
13155
13083
  convertors.push(new PgAlterTableAlterColumnAlterGenerated());
13156
- convertors.push(new SqliteAlterTableAlterColumnSetNotNullConvertor());
13157
- convertors.push(new SqliteAlterTableAlterColumnDropNotNullConvertor());
13158
- convertors.push(new SqliteAlterTableAlterColumnSetDefaultConvertor());
13159
- convertors.push(new SqliteAlterTableAlterColumnDropDefaultConvertor());
13160
- convertors.push(new SqliteAlterTableAlterColumnSetAutoincrementConvertor());
13161
- convertors.push(new SqliteAlterTableAlterColumnDropAutoincrementConvertor());
13162
- convertors.push(new SqliteAlterTableCreateCompositePrimaryKeyConvertor());
13163
- convertors.push(new SqliteAlterTableDeleteCompositePrimaryKeyConvertor());
13164
- convertors.push(new SqliteAlterTableAlterCompositePrimaryKeyConvertor());
13165
13084
  convertors.push(new PgAlterTableCreateCompositePrimaryKeyConvertor());
13166
13085
  convertors.push(new PgAlterTableDeleteCompositePrimaryKeyConvertor());
13167
13086
  convertors.push(new PgAlterTableAlterCompositePrimaryKeyConvertor());
@@ -13170,19 +13089,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13170
13089
  convertors.push(new MySqlAlterTableCreateCompositePrimaryKeyConvertor());
13171
13090
  convertors.push(new MySqlAlterTableAddPk());
13172
13091
  convertors.push(new MySqlAlterTableAlterCompositePrimaryKeyConvertor());
13173
- fromJson = (statements, dialect7) => {
13174
- const result = statements.flatMap((statement) => {
13175
- const filtered = convertors.filter((it) => {
13176
- return it.can(statement, dialect7);
13177
- });
13178
- const convertor = filtered.length === 1 ? filtered[0] : void 0;
13179
- if (!convertor) {
13180
- return "";
13181
- }
13182
- return convertor.convert(statement);
13183
- }).filter((it) => it !== "");
13184
- return result;
13185
- };
13186
13092
  https: `
13187
13093
  create table users (
13188
13094
  id int,
@@ -13209,12 +13115,248 @@ drop type __venum;
13209
13115
  }
13210
13116
  });
13211
13117
 
13118
+ // src/cli/commands/sqlitePushUtils.ts
13119
+ var _moveDataStatements, getOldTableName, getNewTableName, logSuggestionsAndReturn;
13120
+ var init_sqlitePushUtils = __esm({
13121
+ "src/cli/commands/sqlitePushUtils.ts"() {
13122
+ "use strict";
13123
+ init_source();
13124
+ init_sqliteSchema();
13125
+ init_sqlgenerator();
13126
+ init_utils();
13127
+ _moveDataStatements = (tableName, json, dataLoss = false) => {
13128
+ const statements = [];
13129
+ const newTableName = `__new_${tableName}`;
13130
+ const tableColumns = Object.values(json.tables[tableName].columns);
13131
+ const referenceData = Object.values(json.tables[tableName].foreignKeys);
13132
+ const compositePKs = Object.values(
13133
+ json.tables[tableName].compositePrimaryKeys
13134
+ ).map((it) => SQLiteSquasher.unsquashPK(it));
13135
+ const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
13136
+ statements.push(
13137
+ new SQLiteCreateTableConvertor().convert({
13138
+ type: "sqlite_create_table",
13139
+ tableName: newTableName,
13140
+ columns: tableColumns,
13141
+ referenceData: fks,
13142
+ compositePKs
13143
+ })
13144
+ );
13145
+ if (!dataLoss) {
13146
+ const columns = Object.keys(json.tables[tableName].columns).map(
13147
+ (c) => `"${c}"`
13148
+ );
13149
+ statements.push(
13150
+ `INSERT INTO \`${newTableName}\`(${columns.join(
13151
+ ", "
13152
+ )}) SELECT ${columns.join(", ")} FROM \`${tableName}\`;`
13153
+ );
13154
+ }
13155
+ statements.push(
13156
+ new SQLiteDropTableConvertor().convert({
13157
+ type: "drop_table",
13158
+ tableName,
13159
+ schema: ""
13160
+ })
13161
+ );
13162
+ statements.push(
13163
+ new SqliteRenameTableConvertor().convert({
13164
+ fromSchema: "",
13165
+ tableNameFrom: newTableName,
13166
+ tableNameTo: tableName,
13167
+ toSchema: "",
13168
+ type: "rename_table"
13169
+ })
13170
+ );
13171
+ for (const idx of Object.values(json.tables[tableName].indexes)) {
13172
+ statements.push(
13173
+ new CreateSqliteIndexConvertor().convert({
13174
+ type: "create_index",
13175
+ tableName,
13176
+ schema: "",
13177
+ data: idx
13178
+ })
13179
+ );
13180
+ }
13181
+ return statements;
13182
+ };
13183
+ getOldTableName = (tableName, meta) => {
13184
+ for (const key of Object.keys(meta.tables)) {
13185
+ const value = meta.tables[key];
13186
+ if (`"${tableName}"` === value) {
13187
+ return key.substring(1, key.length - 1);
13188
+ }
13189
+ }
13190
+ return tableName;
13191
+ };
13192
+ getNewTableName = (tableName, meta) => {
13193
+ if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
13194
+ return meta.tables[`"${tableName}"`].substring(
13195
+ 1,
13196
+ meta.tables[`"${tableName}"`].length - 1
13197
+ );
13198
+ }
13199
+ return tableName;
13200
+ };
13201
+ logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
13202
+ let shouldAskForApprove = false;
13203
+ const statementsToExecute = [];
13204
+ const infoToPrint = [];
13205
+ const tablesToRemove = [];
13206
+ const columnsToRemove = [];
13207
+ const schemasToRemove = [];
13208
+ const tablesToTruncate = [];
13209
+ for (const statement of statements) {
13210
+ if (statement.type === "drop_table") {
13211
+ const res = await connection.query(
13212
+ `select count(*) as count from \`${statement.tableName}\``
13213
+ );
13214
+ const count2 = Number(res[0].count);
13215
+ if (count2 > 0) {
13216
+ infoToPrint.push(
13217
+ `\xB7 You're about to delete ${source_default.underline(
13218
+ statement.tableName
13219
+ )} table with ${count2} items`
13220
+ );
13221
+ tablesToRemove.push(statement.tableName);
13222
+ shouldAskForApprove = true;
13223
+ }
13224
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13225
+ statementsToExecute.push(
13226
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13227
+ );
13228
+ } else if (statement.type === "alter_table_drop_column") {
13229
+ const tableName = statement.tableName;
13230
+ const columnName = statement.columnName;
13231
+ const res = await connection.query(
13232
+ `select count(\`${tableName}\`.\`${columnName}\`) as count from \`${tableName}\``
13233
+ );
13234
+ const count2 = Number(res[0].count);
13235
+ if (count2 > 0) {
13236
+ infoToPrint.push(
13237
+ `\xB7 You're about to delete ${source_default.underline(
13238
+ columnName
13239
+ )} column in ${tableName} table with ${count2} items`
13240
+ );
13241
+ columnsToRemove.push(`${tableName}_${statement.columnName}`);
13242
+ shouldAskForApprove = true;
13243
+ }
13244
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13245
+ statementsToExecute.push(
13246
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13247
+ );
13248
+ } else if (statement.type === "sqlite_alter_table_add_column" && (statement.column.notNull && !statement.column.default)) {
13249
+ const tableName = statement.tableName;
13250
+ const columnName = statement.column.name;
13251
+ const res = await connection.query(
13252
+ `select count(*) as count from \`${tableName}\``
13253
+ );
13254
+ const count2 = Number(res[0].count);
13255
+ if (count2 > 0) {
13256
+ infoToPrint.push(
13257
+ `\xB7 You're about to add not-null ${source_default.underline(
13258
+ columnName
13259
+ )} column without default value, which contains ${count2} items`
13260
+ );
13261
+ tablesToTruncate.push(tableName);
13262
+ statementsToExecute.push(`delete from ${tableName};`);
13263
+ shouldAskForApprove = true;
13264
+ }
13265
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13266
+ statementsToExecute.push(
13267
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13268
+ );
13269
+ } else if (statement.type === "recreate_table") {
13270
+ const tableName = statement.tableName;
13271
+ const oldTableName = getOldTableName(tableName, meta);
13272
+ let dataLoss = false;
13273
+ const prevColumnNames = Object.keys(json1.tables[oldTableName].columns);
13274
+ const currentColumnNames = Object.keys(json2.tables[tableName].columns);
13275
+ const { removedColumns, addedColumns } = findAddedAndRemoved(
13276
+ prevColumnNames,
13277
+ currentColumnNames
13278
+ );
13279
+ if (removedColumns.length) {
13280
+ for (const removedColumn of removedColumns) {
13281
+ const res = await connection.query(
13282
+ `select count(\`${tableName}\`.\`${removedColumn}\`) as count from \`${tableName}\``
13283
+ );
13284
+ const count2 = Number(res[0].count);
13285
+ if (count2 > 0) {
13286
+ infoToPrint.push(
13287
+ `\xB7 You're about to delete ${source_default.underline(
13288
+ removedColumn
13289
+ )} column in ${tableName} table with ${count2} items`
13290
+ );
13291
+ columnsToRemove.push(removedColumn);
13292
+ shouldAskForApprove = true;
13293
+ }
13294
+ }
13295
+ }
13296
+ if (addedColumns.length) {
13297
+ for (const addedColumn of addedColumns) {
13298
+ const [res] = await connection.query(
13299
+ `select count(*) as count from \`${tableName}\``
13300
+ );
13301
+ const columnConf = json2.tables[tableName].columns[addedColumn];
13302
+ const count2 = Number(res.count);
13303
+ if (count2 > 0 && columnConf.notNull && !columnConf.default) {
13304
+ dataLoss = true;
13305
+ infoToPrint.push(
13306
+ `\xB7 You're about to add not-null ${source_default.underline(
13307
+ addedColumn
13308
+ )} column without default value to table, which contains ${count2} items`
13309
+ );
13310
+ shouldAskForApprove = true;
13311
+ tablesToTruncate.push(tableName);
13312
+ statementsToExecute.push(`DELETE FROM \`${tableName}\`;`);
13313
+ }
13314
+ }
13315
+ }
13316
+ const tablesReferencingCurrent = [];
13317
+ for (const table4 of Object.values(json2.tables)) {
13318
+ const tablesRefs = Object.values(json2.tables[table4.name].foreignKeys).filter((t) => SQLiteSquasher.unsquashPushFK(t).tableTo === tableName).map((it) => SQLiteSquasher.unsquashPushFK(it).tableFrom);
13319
+ tablesReferencingCurrent.push(...tablesRefs);
13320
+ }
13321
+ if (!tablesReferencingCurrent.length) {
13322
+ statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
13323
+ continue;
13324
+ }
13325
+ const [{ foreign_keys: pragmaState }] = await connection.query(`PRAGMA foreign_keys;`);
13326
+ if (pragmaState) {
13327
+ statementsToExecute.push(`PRAGMA foreign_keys=OFF;`);
13328
+ }
13329
+ statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
13330
+ if (pragmaState) {
13331
+ statementsToExecute.push(`PRAGMA foreign_keys=ON;`);
13332
+ }
13333
+ } else {
13334
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13335
+ statementsToExecute.push(
13336
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13337
+ );
13338
+ }
13339
+ }
13340
+ return {
13341
+ statementsToExecute,
13342
+ shouldAskForApprove,
13343
+ infoToPrint,
13344
+ columnsToRemove: [...new Set(columnsToRemove)],
13345
+ schemasToRemove: [...new Set(schemasToRemove)],
13346
+ tablesToTruncate: [...new Set(tablesToTruncate)],
13347
+ tablesToRemove: [...new Set(tablesToRemove)]
13348
+ };
13349
+ };
13350
+ }
13351
+ });
13352
+
13212
13353
  // src/jsonStatements.ts
13213
- 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;
13354
+ 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;
13214
13355
  var init_jsonStatements = __esm({
13215
13356
  "src/jsonStatements.ts"() {
13216
13357
  "use strict";
13217
13358
  init_source();
13359
+ init_sqlitePushUtils();
13218
13360
  init_views();
13219
13361
  init_mysqlSchema();
13220
13362
  init_pgSchema();
@@ -13998,21 +14140,64 @@ var init_jsonStatements = __esm({
13998
14140
  const columnPk = json2.tables[tableName].columns[columnName].primaryKey;
13999
14141
  const columnGenerated = json2.tables[tableName].columns[columnName].generated;
14000
14142
  const compositePk = json2.tables[tableName].compositePrimaryKeys[`${tableName}_${columnName}`];
14001
- if (typeof column4.name !== "string") {
14002
- statements.push({
14003
- type: "alter_table_rename_column",
14004
- tableName,
14005
- oldColumnName: column4.name.old,
14006
- newColumnName: column4.name.new,
14007
- schema: schema4
14008
- });
14009
- }
14010
- if (column4.type?.type === "changed") {
14143
+ if (column4.autoincrement?.type === "added") {
14011
14144
  statements.push({
14012
- type: "alter_table_alter_column_set_type",
14145
+ type: "alter_table_alter_column_set_autoincrement",
14013
14146
  tableName,
14014
14147
  columnName,
14015
- newDataType: column4.type.new,
14148
+ schema: schema4,
14149
+ newDataType: columnType,
14150
+ columnDefault,
14151
+ columnOnUpdate,
14152
+ columnNotNull,
14153
+ columnAutoIncrement,
14154
+ columnPk
14155
+ });
14156
+ }
14157
+ if (column4.autoincrement?.type === "changed") {
14158
+ const type = column4.autoincrement.new ? "alter_table_alter_column_set_autoincrement" : "alter_table_alter_column_drop_autoincrement";
14159
+ statements.push({
14160
+ type,
14161
+ tableName,
14162
+ columnName,
14163
+ schema: schema4,
14164
+ newDataType: columnType,
14165
+ columnDefault,
14166
+ columnOnUpdate,
14167
+ columnNotNull,
14168
+ columnAutoIncrement,
14169
+ columnPk
14170
+ });
14171
+ }
14172
+ if (column4.autoincrement?.type === "deleted") {
14173
+ statements.push({
14174
+ type: "alter_table_alter_column_drop_autoincrement",
14175
+ tableName,
14176
+ columnName,
14177
+ schema: schema4,
14178
+ newDataType: columnType,
14179
+ columnDefault,
14180
+ columnOnUpdate,
14181
+ columnNotNull,
14182
+ columnAutoIncrement,
14183
+ columnPk
14184
+ });
14185
+ }
14186
+ if (typeof column4.name !== "string") {
14187
+ statements.push({
14188
+ type: "alter_table_rename_column",
14189
+ tableName,
14190
+ oldColumnName: column4.name.old,
14191
+ newColumnName: column4.name.new,
14192
+ schema: schema4
14193
+ });
14194
+ }
14195
+ if (column4.type?.type === "changed") {
14196
+ statements.push({
14197
+ type: "alter_table_alter_column_set_type",
14198
+ tableName,
14199
+ columnName,
14200
+ newDataType: column4.type.new,
14016
14201
  oldDataType: column4.type.old,
14017
14202
  schema: schema4,
14018
14203
  columnDefault,
@@ -14261,6 +14446,37 @@ var init_jsonStatements = __esm({
14261
14446
  };
14262
14447
  });
14263
14448
  };
14449
+ prepareLibSQLCreateReferencesJson = (tableName, schema4, foreignKeys, json2, action) => {
14450
+ return Object.values(foreignKeys).map((fkData) => {
14451
+ const { columnsFrom, tableFrom, columnsTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14452
+ let isMulticolumn = false;
14453
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
14454
+ isMulticolumn = true;
14455
+ return {
14456
+ type: "create_reference",
14457
+ tableName,
14458
+ data: fkData,
14459
+ schema: schema4,
14460
+ isMulticolumn
14461
+ };
14462
+ }
14463
+ const columnFrom = columnsFrom[0];
14464
+ const {
14465
+ notNull: columnNotNull,
14466
+ default: columnDefault,
14467
+ type: columnType
14468
+ } = json2.tables[tableFrom].columns[columnFrom];
14469
+ return {
14470
+ type: "create_reference",
14471
+ tableName,
14472
+ data: fkData,
14473
+ schema: schema4,
14474
+ columnNotNull,
14475
+ columnDefault,
14476
+ columnType
14477
+ };
14478
+ });
14479
+ };
14264
14480
  prepareDropReferencesJson = (tableName, schema4, foreignKeys) => {
14265
14481
  return Object.values(foreignKeys).map((fkData) => {
14266
14482
  return {
@@ -14271,6 +14487,53 @@ var init_jsonStatements = __esm({
14271
14487
  };
14272
14488
  });
14273
14489
  };
14490
+ prepareLibSQLDropReferencesJson = (tableName, schema4, foreignKeys, json2, meta, action) => {
14491
+ const statements = Object.values(foreignKeys).map((fkData) => {
14492
+ const { columnsFrom, tableFrom, columnsTo, name: name2, tableTo, onDelete, onUpdate } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14493
+ const keys = Object.keys(json2.tables[tableName].columns);
14494
+ const filtered = columnsFrom.filter((it) => keys.includes(it));
14495
+ const fullDrop = filtered.length === 0;
14496
+ if (fullDrop) return;
14497
+ let isMulticolumn = false;
14498
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
14499
+ isMulticolumn = true;
14500
+ return {
14501
+ type: "delete_reference",
14502
+ tableName,
14503
+ data: fkData,
14504
+ schema: schema4,
14505
+ isMulticolumn
14506
+ };
14507
+ }
14508
+ const columnFrom = columnsFrom[0];
14509
+ const newTableName = getNewTableName(tableFrom, meta);
14510
+ const {
14511
+ notNull: columnNotNull,
14512
+ default: columnDefault,
14513
+ type: columnType
14514
+ } = json2.tables[newTableName].columns[columnFrom];
14515
+ const fkToSquash = {
14516
+ columnsFrom,
14517
+ columnsTo,
14518
+ name: name2,
14519
+ tableFrom: newTableName,
14520
+ tableTo,
14521
+ onDelete,
14522
+ onUpdate
14523
+ };
14524
+ const foreignKey = action === "push" ? SQLiteSquasher.squashPushFK(fkToSquash) : SQLiteSquasher.squashFK(fkToSquash);
14525
+ return {
14526
+ type: "delete_reference",
14527
+ tableName,
14528
+ data: foreignKey,
14529
+ schema: schema4,
14530
+ columnNotNull,
14531
+ columnDefault,
14532
+ columnType
14533
+ };
14534
+ });
14535
+ return statements.filter((it) => it);
14536
+ };
14274
14537
  prepareAlterReferencesJson = (tableName, schema4, foreignKeys) => {
14275
14538
  const stmts = [];
14276
14539
  Object.values(foreignKeys).map((val) => {
@@ -14424,10 +14687,292 @@ var init_jsonStatements = __esm({
14424
14687
  }
14425
14688
  });
14426
14689
 
14690
+ // src/statementCombiner.ts
14691
+ var prepareLibSQLRecreateTable, prepareSQLiteRecreateTable, libSQLCombineStatements, sqliteCombineStatements;
14692
+ var init_statementCombiner = __esm({
14693
+ "src/statementCombiner.ts"() {
14694
+ "use strict";
14695
+ init_jsonStatements();
14696
+ init_sqliteSchema();
14697
+ prepareLibSQLRecreateTable = (table4, action) => {
14698
+ const { name: name2, columns, uniqueConstraints, indexes } = table4;
14699
+ const composites = Object.values(table4.compositePrimaryKeys).map(
14700
+ (it) => SQLiteSquasher.unsquashPK(it)
14701
+ );
14702
+ const references2 = Object.values(table4.foreignKeys);
14703
+ const fks = references2.map(
14704
+ (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14705
+ );
14706
+ const statements = [
14707
+ {
14708
+ type: "recreate_table",
14709
+ tableName: name2,
14710
+ columns: Object.values(columns),
14711
+ compositePKs: composites,
14712
+ referenceData: fks,
14713
+ uniqueConstraints: Object.values(uniqueConstraints)
14714
+ }
14715
+ ];
14716
+ if (Object.keys(indexes).length) {
14717
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
14718
+ }
14719
+ return statements;
14720
+ };
14721
+ prepareSQLiteRecreateTable = (table4, action) => {
14722
+ const { name: name2, columns, uniqueConstraints, indexes } = table4;
14723
+ const composites = Object.values(table4.compositePrimaryKeys).map(
14724
+ (it) => SQLiteSquasher.unsquashPK(it)
14725
+ );
14726
+ const references2 = Object.values(table4.foreignKeys);
14727
+ const fks = references2.map(
14728
+ (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14729
+ );
14730
+ const statements = [
14731
+ {
14732
+ type: "recreate_table",
14733
+ tableName: name2,
14734
+ columns: Object.values(columns),
14735
+ compositePKs: composites,
14736
+ referenceData: fks,
14737
+ uniqueConstraints: Object.values(uniqueConstraints)
14738
+ }
14739
+ ];
14740
+ if (Object.keys(indexes).length) {
14741
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
14742
+ }
14743
+ return statements;
14744
+ };
14745
+ libSQLCombineStatements = (statements, json2, action) => {
14746
+ const newStatements = {};
14747
+ for (const statement of statements) {
14748
+ 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") {
14749
+ const tableName2 = statement.tableName;
14750
+ const statementsForTable2 = newStatements[tableName2];
14751
+ if (!statementsForTable2) {
14752
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14753
+ continue;
14754
+ }
14755
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14756
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14757
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14758
+ if (wasRename) {
14759
+ newStatements[tableName2].push(...preparedStatements);
14760
+ } else {
14761
+ newStatements[tableName2] = preparedStatements;
14762
+ }
14763
+ continue;
14764
+ }
14765
+ continue;
14766
+ }
14767
+ 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") {
14768
+ const { tableName: tableName2, columnName, columnPk } = statement;
14769
+ const columnIsPartOfForeignKey = Object.values(
14770
+ json2.tables[tableName2].foreignKeys
14771
+ ).some((it) => {
14772
+ const unsquashFk = action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it);
14773
+ return unsquashFk.columnsFrom.includes(columnName);
14774
+ });
14775
+ const statementsForTable2 = newStatements[tableName2];
14776
+ if (!statementsForTable2 && (columnIsPartOfForeignKey || columnPk)) {
14777
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14778
+ continue;
14779
+ }
14780
+ if (statementsForTable2 && (columnIsPartOfForeignKey || columnPk)) {
14781
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14782
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14783
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14784
+ if (wasRename) {
14785
+ newStatements[tableName2].push(...preparedStatements);
14786
+ } else {
14787
+ newStatements[tableName2] = preparedStatements;
14788
+ }
14789
+ }
14790
+ continue;
14791
+ }
14792
+ if (statementsForTable2 && !(columnIsPartOfForeignKey || columnPk)) {
14793
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14794
+ newStatements[tableName2].push(statement);
14795
+ }
14796
+ continue;
14797
+ }
14798
+ newStatements[tableName2] = [statement];
14799
+ continue;
14800
+ }
14801
+ if (statement.type === "create_reference") {
14802
+ const tableName2 = statement.tableName;
14803
+ const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
14804
+ const statementsForTable2 = newStatements[tableName2];
14805
+ if (!statementsForTable2) {
14806
+ newStatements[tableName2] = statement.isMulticolumn ? prepareLibSQLRecreateTable(json2.tables[tableName2], action) : [statement];
14807
+ continue;
14808
+ }
14809
+ if (!statement.isMulticolumn && statementsForTable2.some(
14810
+ (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
14811
+ )) {
14812
+ continue;
14813
+ }
14814
+ if (statement.isMulticolumn) {
14815
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14816
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14817
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14818
+ if (wasRename) {
14819
+ newStatements[tableName2].push(...preparedStatements);
14820
+ } else {
14821
+ newStatements[tableName2] = preparedStatements;
14822
+ }
14823
+ continue;
14824
+ }
14825
+ continue;
14826
+ }
14827
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14828
+ newStatements[tableName2].push(statement);
14829
+ }
14830
+ continue;
14831
+ }
14832
+ if (statement.type === "delete_reference") {
14833
+ const tableName2 = statement.tableName;
14834
+ const statementsForTable2 = newStatements[tableName2];
14835
+ if (!statementsForTable2) {
14836
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14837
+ continue;
14838
+ }
14839
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14840
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14841
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14842
+ if (wasRename) {
14843
+ newStatements[tableName2].push(...preparedStatements);
14844
+ } else {
14845
+ newStatements[tableName2] = preparedStatements;
14846
+ }
14847
+ continue;
14848
+ }
14849
+ continue;
14850
+ }
14851
+ if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
14852
+ const tableName2 = statement.tableName;
14853
+ const statementsForTable2 = newStatements[tableName2];
14854
+ if (!statementsForTable2) {
14855
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14856
+ continue;
14857
+ }
14858
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14859
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14860
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14861
+ if (wasRename) {
14862
+ newStatements[tableName2].push(...preparedStatements);
14863
+ } else {
14864
+ newStatements[tableName2] = preparedStatements;
14865
+ }
14866
+ continue;
14867
+ }
14868
+ continue;
14869
+ }
14870
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
14871
+ const statementsForTable = newStatements[tableName];
14872
+ if (!statementsForTable) {
14873
+ newStatements[tableName] = [statement];
14874
+ continue;
14875
+ }
14876
+ if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
14877
+ newStatements[tableName].push(statement);
14878
+ }
14879
+ }
14880
+ const combinedStatements = Object.values(newStatements).flat();
14881
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
14882
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
14883
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
14884
+ return [...renamedTables, ...renamedColumns, ...rest];
14885
+ };
14886
+ sqliteCombineStatements = (statements, json2, action) => {
14887
+ const newStatements = {};
14888
+ for (const statement of statements) {
14889
+ 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") {
14890
+ const tableName2 = statement.tableName;
14891
+ const statementsForTable2 = newStatements[tableName2];
14892
+ if (!statementsForTable2) {
14893
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14894
+ continue;
14895
+ }
14896
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14897
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14898
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14899
+ if (wasRename) {
14900
+ newStatements[tableName2].push(...preparedStatements);
14901
+ } else {
14902
+ newStatements[tableName2] = preparedStatements;
14903
+ }
14904
+ continue;
14905
+ }
14906
+ continue;
14907
+ }
14908
+ if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
14909
+ const tableName2 = statement.tableName;
14910
+ const statementsForTable2 = newStatements[tableName2];
14911
+ if (!statementsForTable2) {
14912
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14913
+ continue;
14914
+ }
14915
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14916
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14917
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14918
+ if (wasRename) {
14919
+ newStatements[tableName2].push(...preparedStatements);
14920
+ } else {
14921
+ newStatements[tableName2] = preparedStatements;
14922
+ }
14923
+ continue;
14924
+ }
14925
+ continue;
14926
+ }
14927
+ if (statement.type === "create_reference") {
14928
+ const tableName2 = statement.tableName;
14929
+ const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
14930
+ const statementsForTable2 = newStatements[tableName2];
14931
+ if (!statementsForTable2) {
14932
+ newStatements[tableName2] = prepareSQLiteRecreateTable(json2.tables[tableName2], action);
14933
+ continue;
14934
+ }
14935
+ if (data.columnsFrom.length === 1 && statementsForTable2.some(
14936
+ (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
14937
+ )) {
14938
+ continue;
14939
+ }
14940
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14941
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14942
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14943
+ if (wasRename) {
14944
+ newStatements[tableName2].push(...preparedStatements);
14945
+ } else {
14946
+ newStatements[tableName2] = preparedStatements;
14947
+ }
14948
+ continue;
14949
+ }
14950
+ continue;
14951
+ }
14952
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
14953
+ const statementsForTable = newStatements[tableName];
14954
+ if (!statementsForTable) {
14955
+ newStatements[tableName] = [statement];
14956
+ continue;
14957
+ }
14958
+ if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
14959
+ newStatements[tableName].push(statement);
14960
+ }
14961
+ }
14962
+ const combinedStatements = Object.values(newStatements).flat();
14963
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
14964
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
14965
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
14966
+ return [...renamedTables, ...renamedColumns, ...rest];
14967
+ };
14968
+ }
14969
+ });
14970
+
14427
14971
  // src/snapshotsDiffer.ts
14428
14972
  var snapshotsDiffer_exports = {};
14429
14973
  __export(snapshotsDiffer_exports, {
14430
14974
  alteredTableScheme: () => alteredTableScheme,
14975
+ applyLibSQLSnapshotsDiff: () => applyLibSQLSnapshotsDiff,
14431
14976
  applyMysqlSnapshotsDiff: () => applyMysqlSnapshotsDiff,
14432
14977
  applyPgSnapshotsDiff: () => applyPgSnapshotsDiff,
14433
14978
  applySqliteSnapshotsDiff: () => applySqliteSnapshotsDiff,
@@ -14437,7 +14982,7 @@ __export(snapshotsDiffer_exports, {
14437
14982
  makePatched: () => makePatched,
14438
14983
  makeSelfOrPatched: () => makeSelfOrPatched
14439
14984
  });
14440
- var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff;
14985
+ var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff, applyLibSQLSnapshotsDiff;
14441
14986
  var init_snapshotsDiffer = __esm({
14442
14987
  "src/snapshotsDiffer.ts"() {
14443
14988
  "use strict";
@@ -14449,6 +14994,7 @@ var init_snapshotsDiffer = __esm({
14449
14994
  init_mysqlSchema();
14450
14995
  init_pgSchema();
14451
14996
  init_sqliteSchema();
14997
+ init_statementCombiner();
14452
14998
  init_utils();
14453
14999
  makeChanged = (schema4) => {
14454
15000
  return objectType({
@@ -15322,53 +15868,352 @@ var init_snapshotsDiffer = __esm({
15322
15868
  curFull.internal
15323
15869
  );
15324
15870
  }).flat();
15325
- const jsonDropTables = deletedTables.map((it) => {
15326
- return prepareDropTableJson(it);
15327
- });
15328
- const jsonRenameTables = renamedTables.map((it) => {
15329
- return prepareRenameTableJson(it.from, it.to);
15330
- });
15331
- const alteredTables = typedResult.alteredTablesWithColumns;
15871
+ const jsonDropTables = deletedTables.map((it) => {
15872
+ return prepareDropTableJson(it);
15873
+ });
15874
+ const jsonRenameTables = renamedTables.map((it) => {
15875
+ return prepareRenameTableJson(it.from, it.to);
15876
+ });
15877
+ const alteredTables = typedResult.alteredTablesWithColumns;
15878
+ const jsonAddedCompositePKs = [];
15879
+ const jsonDeletedCompositePKs = [];
15880
+ const jsonAlteredCompositePKs = [];
15881
+ const jsonAddedUniqueConstraints = [];
15882
+ const jsonDeletedUniqueConstraints = [];
15883
+ const jsonAlteredUniqueConstraints = [];
15884
+ const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
15885
+ const jsonAddColumnsStatemets = columnCreates.map((it) => _prepareAddColumns(it.table, "", it.columns)).flat();
15886
+ const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
15887
+ alteredTables.forEach((it) => {
15888
+ let addedColumns = [];
15889
+ for (const addedPkName of Object.keys(it.addedCompositePKs)) {
15890
+ const addedPkColumns = it.addedCompositePKs[addedPkName];
15891
+ addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
15892
+ }
15893
+ let deletedColumns = [];
15894
+ for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
15895
+ const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
15896
+ deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
15897
+ }
15898
+ const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
15899
+ let addedCompositePKs = [];
15900
+ let deletedCompositePKs = [];
15901
+ let alteredCompositePKs = [];
15902
+ addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
15903
+ it.name,
15904
+ it.addedCompositePKs,
15905
+ prevFull,
15906
+ curFull
15907
+ );
15908
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15909
+ it.name,
15910
+ it.deletedCompositePKs,
15911
+ prevFull
15912
+ );
15913
+ alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
15914
+ it.name,
15915
+ it.alteredCompositePKs,
15916
+ prevFull,
15917
+ curFull
15918
+ );
15919
+ let addedUniqueConstraints = [];
15920
+ let deletedUniqueConstraints = [];
15921
+ let alteredUniqueConstraints = [];
15922
+ addedUniqueConstraints = prepareAddUniqueConstraintPg(
15923
+ it.name,
15924
+ it.schema,
15925
+ it.addedUniqueConstraints
15926
+ );
15927
+ deletedUniqueConstraints = prepareDeleteUniqueConstraintPg(
15928
+ it.name,
15929
+ it.schema,
15930
+ it.deletedUniqueConstraints
15931
+ );
15932
+ if (it.alteredUniqueConstraints) {
15933
+ const added = {};
15934
+ const deleted = {};
15935
+ for (const k of Object.keys(it.alteredUniqueConstraints)) {
15936
+ added[k] = it.alteredUniqueConstraints[k].__new;
15937
+ deleted[k] = it.alteredUniqueConstraints[k].__old;
15938
+ }
15939
+ addedUniqueConstraints.push(
15940
+ ...prepareAddUniqueConstraintPg(it.name, it.schema, added)
15941
+ );
15942
+ deletedUniqueConstraints.push(
15943
+ ...prepareDeleteUniqueConstraintPg(it.name, it.schema, deleted)
15944
+ );
15945
+ }
15946
+ jsonAddedCompositePKs.push(...addedCompositePKs);
15947
+ jsonDeletedCompositePKs.push(...deletedCompositePKs);
15948
+ jsonAlteredCompositePKs.push(...alteredCompositePKs);
15949
+ jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
15950
+ jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15951
+ jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15952
+ });
15953
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
15954
+ const tableName = it.tableName;
15955
+ const schema4 = it.schema;
15956
+ return {
15957
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
15958
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
15959
+ };
15960
+ });
15961
+ const jsonTableAlternations = alteredTables.map((it) => {
15962
+ return prepareAlterColumnsMysql(
15963
+ it.name,
15964
+ it.schema,
15965
+ it.altered,
15966
+ json1,
15967
+ json2,
15968
+ action
15969
+ );
15970
+ }).flat();
15971
+ const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
15972
+ return prepareCreateIndexesJson(
15973
+ it.name,
15974
+ it.schema,
15975
+ it.addedIndexes || {},
15976
+ curFull.internal
15977
+ );
15978
+ }).flat();
15979
+ const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
15980
+ return prepareDropIndexesJson(
15981
+ it.name,
15982
+ it.schema,
15983
+ it.deletedIndexes || {}
15984
+ );
15985
+ }).flat();
15986
+ alteredTables.forEach((it) => {
15987
+ const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
15988
+ (current, item) => {
15989
+ current[item] = it.alteredIndexes[item].__old;
15990
+ return current;
15991
+ },
15992
+ {}
15993
+ );
15994
+ const createdIndexes = Object.keys(it.alteredIndexes).reduce(
15995
+ (current, item) => {
15996
+ current[item] = it.alteredIndexes[item].__new;
15997
+ return current;
15998
+ },
15999
+ {}
16000
+ );
16001
+ jsonCreateIndexesForAllAlteredTables.push(
16002
+ ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16003
+ );
16004
+ jsonDropIndexesForAllAlteredTables.push(
16005
+ ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
16006
+ );
16007
+ });
16008
+ const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
16009
+ return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
16010
+ }).flat();
16011
+ const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16012
+ const forAdded = prepareCreateReferencesJson(
16013
+ it.name,
16014
+ it.schema,
16015
+ it.addedForeignKeys
16016
+ );
16017
+ const forAltered = prepareDropReferencesJson(
16018
+ it.name,
16019
+ it.schema,
16020
+ it.deletedForeignKeys
16021
+ );
16022
+ const alteredFKs = prepareAlterReferencesJson(
16023
+ it.name,
16024
+ it.schema,
16025
+ it.alteredForeignKeys
16026
+ );
16027
+ return [...forAdded, ...forAltered, ...alteredFKs];
16028
+ }).flat();
16029
+ const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16030
+ (t) => t.type === "create_reference"
16031
+ );
16032
+ const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16033
+ (t) => t.type === "delete_reference"
16034
+ );
16035
+ const jsonMySqlCreateTables = createdTables.map((it) => {
16036
+ return prepareMySqlCreateTableJson(
16037
+ it,
16038
+ curFull,
16039
+ curFull.internal
16040
+ );
16041
+ });
16042
+ jsonStatements.push(...jsonMySqlCreateTables);
16043
+ jsonStatements.push(...jsonDropTables);
16044
+ jsonStatements.push(...jsonRenameTables);
16045
+ jsonStatements.push(...jsonRenameColumnsStatements);
16046
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16047
+ jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
16048
+ jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
16049
+ jsonStatements.push(...jsonDeletedCompositePKs);
16050
+ jsonStatements.push(...jsonTableAlternations);
16051
+ jsonStatements.push(...jsonAddedCompositePKs);
16052
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16053
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16054
+ jsonStatements.push(...jsonAddColumnsStatemets);
16055
+ jsonStatements.push(...jsonCreateReferencesForCreatedTables);
16056
+ jsonStatements.push(...jsonCreateIndexesForCreatedTables);
16057
+ jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
16058
+ jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16059
+ jsonStatements.push(...jsonDropColumnsStatemets);
16060
+ jsonStatements.push(...jsonAlteredCompositePKs);
16061
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16062
+ jsonStatements.push(...jsonAlteredUniqueConstraints);
16063
+ const sqlStatements = fromJson(jsonStatements, "mysql");
16064
+ const uniqueSqlStatements = [];
16065
+ sqlStatements.forEach((ss) => {
16066
+ if (!uniqueSqlStatements.includes(ss)) {
16067
+ uniqueSqlStatements.push(ss);
16068
+ }
16069
+ });
16070
+ const rTables = renamedTables.map((it) => {
16071
+ return { from: it.from, to: it.to };
16072
+ });
16073
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
16074
+ return {
16075
+ statements: jsonStatements,
16076
+ sqlStatements: uniqueSqlStatements,
16077
+ _meta
16078
+ };
16079
+ };
16080
+ applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16081
+ const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
16082
+ const {
16083
+ created: createdTables,
16084
+ deleted: deletedTables,
16085
+ renamed: renamedTables
16086
+ } = await tablesResolver2({
16087
+ created: tablesDiff.added,
16088
+ deleted: tablesDiff.deleted
16089
+ });
16090
+ const tablesPatchedSnap1 = copy(json1);
16091
+ tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_2, it) => {
16092
+ const { name: name2 } = nameChangeFor(it, renamedTables);
16093
+ it.name = name2;
16094
+ return [name2, it];
16095
+ });
16096
+ const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
16097
+ const columnRenames = [];
16098
+ const columnCreates = [];
16099
+ const columnDeletes = [];
16100
+ for (let entry of Object.values(res)) {
16101
+ const { renamed, created, deleted } = await columnsResolver2({
16102
+ tableName: entry.name,
16103
+ schema: entry.schema,
16104
+ deleted: entry.columns.deleted,
16105
+ created: entry.columns.added
16106
+ });
16107
+ if (created.length > 0) {
16108
+ columnCreates.push({
16109
+ table: entry.name,
16110
+ columns: created
16111
+ });
16112
+ }
16113
+ if (deleted.length > 0) {
16114
+ columnDeletes.push({
16115
+ table: entry.name,
16116
+ columns: deleted
16117
+ });
16118
+ }
16119
+ if (renamed.length > 0) {
16120
+ columnRenames.push({
16121
+ table: entry.name,
16122
+ renames: renamed
16123
+ });
16124
+ }
16125
+ }
16126
+ const columnRenamesDict = columnRenames.reduce(
16127
+ (acc, it) => {
16128
+ acc[it.table] = it.renames;
16129
+ return acc;
16130
+ },
16131
+ {}
16132
+ );
16133
+ const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
16134
+ columnsPatchedSnap1.tables = mapEntries(
16135
+ columnsPatchedSnap1.tables,
16136
+ (tableKey2, tableValue) => {
16137
+ const patchedColumns = mapKeys(
16138
+ tableValue.columns,
16139
+ (columnKey, column4) => {
16140
+ const rens = columnRenamesDict[tableValue.name] || [];
16141
+ const newName = columnChangeFor(columnKey, rens);
16142
+ column4.name = newName;
16143
+ return newName;
16144
+ }
16145
+ );
16146
+ tableValue.columns = patchedColumns;
16147
+ return [tableKey2, tableValue];
16148
+ }
16149
+ );
16150
+ const diffResult = applyJsonDiff(columnsPatchedSnap1, json2);
16151
+ const typedResult = diffResultSchemeSQLite.parse(diffResult);
16152
+ const tablesMap = {};
16153
+ typedResult.alteredTablesWithColumns.forEach((obj) => {
16154
+ tablesMap[obj.name] = obj;
16155
+ });
16156
+ const jsonCreateTables = createdTables.map((it) => {
16157
+ return prepareSQLiteCreateTable(it, action);
16158
+ });
16159
+ const jsonCreateIndexesForCreatedTables = createdTables.map((it) => {
16160
+ return prepareCreateIndexesJson(
16161
+ it.name,
16162
+ it.schema,
16163
+ it.indexes,
16164
+ curFull.internal
16165
+ );
16166
+ }).flat();
16167
+ const jsonDropTables = deletedTables.map((it) => {
16168
+ return prepareDropTableJson(it);
16169
+ });
16170
+ const jsonRenameTables = renamedTables.map((it) => {
16171
+ return prepareRenameTableJson(it.from, it.to);
16172
+ });
16173
+ const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
16174
+ const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
16175
+ const jsonAddColumnsStatemets = columnCreates.map((it) => {
16176
+ return _prepareSqliteAddColumns(
16177
+ it.table,
16178
+ it.columns,
16179
+ tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
16180
+ );
16181
+ }).flat();
16182
+ const allAltered = typedResult.alteredTablesWithColumns;
15332
16183
  const jsonAddedCompositePKs = [];
15333
16184
  const jsonDeletedCompositePKs = [];
15334
16185
  const jsonAlteredCompositePKs = [];
15335
16186
  const jsonAddedUniqueConstraints = [];
15336
16187
  const jsonDeletedUniqueConstraints = [];
15337
16188
  const jsonAlteredUniqueConstraints = [];
15338
- const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
15339
- const jsonAddColumnsStatemets = columnCreates.map((it) => _prepareAddColumns(it.table, "", it.columns)).flat();
15340
- const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
15341
- alteredTables.forEach((it) => {
16189
+ allAltered.forEach((it) => {
15342
16190
  let addedColumns = [];
15343
16191
  for (const addedPkName of Object.keys(it.addedCompositePKs)) {
15344
16192
  const addedPkColumns = it.addedCompositePKs[addedPkName];
15345
- addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
16193
+ addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
15346
16194
  }
15347
16195
  let deletedColumns = [];
15348
16196
  for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
15349
16197
  const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
15350
- deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
16198
+ deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
15351
16199
  }
15352
16200
  const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
15353
16201
  let addedCompositePKs = [];
15354
16202
  let deletedCompositePKs = [];
15355
16203
  let alteredCompositePKs = [];
15356
- addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
15357
- it.name,
15358
- it.addedCompositePKs,
15359
- prevFull,
15360
- curFull
15361
- );
15362
- deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15363
- it.name,
15364
- it.deletedCompositePKs,
15365
- prevFull
15366
- );
15367
- alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
16204
+ if (doPerformDeleteAndCreate) {
16205
+ addedCompositePKs = prepareAddCompositePrimaryKeySqlite(
16206
+ it.name,
16207
+ it.addedCompositePKs
16208
+ );
16209
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeySqlite(
16210
+ it.name,
16211
+ it.deletedCompositePKs
16212
+ );
16213
+ }
16214
+ alteredCompositePKs = prepareAlterCompositePrimaryKeySqlite(
15368
16215
  it.name,
15369
- it.alteredCompositePKs,
15370
- prevFull,
15371
- curFull
16216
+ it.alteredCompositePKs
15372
16217
  );
15373
16218
  let addedUniqueConstraints = [];
15374
16219
  let deletedUniqueConstraints = [];
@@ -15412,17 +16257,10 @@ var init_snapshotsDiffer = __esm({
15412
16257
  to: { schema: schema4, table: tableName, column: it.newColumnName }
15413
16258
  };
15414
16259
  });
15415
- const jsonTableAlternations = alteredTables.map((it) => {
15416
- return prepareAlterColumnsMysql(
15417
- it.name,
15418
- it.schema,
15419
- it.altered,
15420
- json1,
15421
- json2,
15422
- action
15423
- );
16260
+ const jsonTableAlternations = allAltered.map((it) => {
16261
+ return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15424
16262
  }).flat();
15425
- const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
16263
+ const jsonCreateIndexesForAllAlteredTables = allAltered.map((it) => {
15426
16264
  return prepareCreateIndexesJson(
15427
16265
  it.name,
15428
16266
  it.schema,
@@ -15430,14 +16268,14 @@ var init_snapshotsDiffer = __esm({
15430
16268
  curFull.internal
15431
16269
  );
15432
16270
  }).flat();
15433
- const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16271
+ const jsonDropIndexesForAllAlteredTables = allAltered.map((it) => {
15434
16272
  return prepareDropIndexesJson(
15435
16273
  it.name,
15436
16274
  it.schema,
15437
16275
  it.deletedIndexes || {}
15438
16276
  );
15439
16277
  }).flat();
15440
- alteredTables.forEach((it) => {
16278
+ allAltered.forEach((it) => {
15441
16279
  const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
15442
16280
  (current, item) => {
15443
16281
  current[item] = it.alteredIndexes[item].__old;
@@ -15453,16 +16291,18 @@ var init_snapshotsDiffer = __esm({
15453
16291
  {}
15454
16292
  );
15455
16293
  jsonCreateIndexesForAllAlteredTables.push(
15456
- ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16294
+ ...prepareCreateIndexesJson(
16295
+ it.name,
16296
+ it.schema,
16297
+ createdIndexes || {},
16298
+ curFull.internal
16299
+ )
15457
16300
  );
15458
16301
  jsonDropIndexesForAllAlteredTables.push(
15459
16302
  ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
15460
16303
  );
15461
16304
  });
15462
- const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
15463
- return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
15464
- }).flat();
15465
- const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16305
+ const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15466
16306
  const forAdded = prepareCreateReferencesJson(
15467
16307
  it.name,
15468
16308
  it.schema,
@@ -15486,35 +16326,25 @@ var init_snapshotsDiffer = __esm({
15486
16326
  const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
15487
16327
  (t) => t.type === "delete_reference"
15488
16328
  );
15489
- const jsonMySqlCreateTables = createdTables.map((it) => {
15490
- return prepareMySqlCreateTableJson(
15491
- it,
15492
- curFull,
15493
- curFull.internal
15494
- );
15495
- });
15496
- jsonStatements.push(...jsonMySqlCreateTables);
16329
+ const jsonStatements = [];
16330
+ jsonStatements.push(...jsonCreateTables);
15497
16331
  jsonStatements.push(...jsonDropTables);
15498
16332
  jsonStatements.push(...jsonRenameTables);
15499
16333
  jsonStatements.push(...jsonRenameColumnsStatements);
15500
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15501
16334
  jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
15502
16335
  jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
15503
16336
  jsonStatements.push(...jsonDeletedCompositePKs);
15504
16337
  jsonStatements.push(...jsonTableAlternations);
15505
16338
  jsonStatements.push(...jsonAddedCompositePKs);
15506
- jsonStatements.push(...jsonAddedUniqueConstraints);
15507
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15508
16339
  jsonStatements.push(...jsonAddColumnsStatemets);
15509
- jsonStatements.push(...jsonCreateReferencesForCreatedTables);
15510
16340
  jsonStatements.push(...jsonCreateIndexesForCreatedTables);
15511
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15512
16341
  jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16342
+ jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15513
16343
  jsonStatements.push(...jsonDropColumnsStatemets);
15514
16344
  jsonStatements.push(...jsonAlteredCompositePKs);
15515
- jsonStatements.push(...jsonAddedUniqueConstraints);
15516
16345
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15517
- const sqlStatements = fromJson(jsonStatements, "mysql");
16346
+ const combinedJsonStatements = sqliteCombineStatements(jsonStatements, json2, action);
16347
+ const sqlStatements = fromJson(combinedJsonStatements, "sqlite");
15518
16348
  const uniqueSqlStatements = [];
15519
16349
  sqlStatements.forEach((ss) => {
15520
16350
  if (!uniqueSqlStatements.includes(ss)) {
@@ -15526,12 +16356,12 @@ var init_snapshotsDiffer = __esm({
15526
16356
  });
15527
16357
  const _meta = prepareMigrationMeta([], rTables, rColumns);
15528
16358
  return {
15529
- statements: jsonStatements,
16359
+ statements: combinedJsonStatements,
15530
16360
  sqlStatements: uniqueSqlStatements,
15531
16361
  _meta
15532
16362
  };
15533
16363
  };
15534
- applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16364
+ applyLibSQLSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
15535
16365
  const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
15536
16366
  const {
15537
16367
  created: createdTables,
@@ -15633,6 +16463,18 @@ var init_snapshotsDiffer = __esm({
15633
16463
  tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
15634
16464
  );
15635
16465
  }).flat();
16466
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
16467
+ const tableName = it.tableName;
16468
+ const schema4 = it.schema;
16469
+ return {
16470
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
16471
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
16472
+ };
16473
+ });
16474
+ const rTables = renamedTables.map((it) => {
16475
+ return { from: it.from, to: it.to };
16476
+ });
16477
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
15636
16478
  const allAltered = typedResult.alteredTablesWithColumns;
15637
16479
  const jsonAddedCompositePKs = [];
15638
16480
  const jsonDeletedCompositePKs = [];
@@ -15703,14 +16545,6 @@ var init_snapshotsDiffer = __esm({
15703
16545
  jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15704
16546
  jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15705
16547
  });
15706
- const rColumns = jsonRenameColumnsStatements.map((it) => {
15707
- const tableName = it.tableName;
15708
- const schema4 = it.schema;
15709
- return {
15710
- from: { schema: schema4, table: tableName, column: it.oldColumnName },
15711
- to: { schema: schema4, table: tableName, column: it.newColumnName }
15712
- };
15713
- });
15714
16548
  const jsonTableAlternations = allAltered.map((it) => {
15715
16549
  return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15716
16550
  }).flat();
@@ -15757,21 +16591,22 @@ var init_snapshotsDiffer = __esm({
15757
16591
  );
15758
16592
  });
15759
16593
  const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15760
- const forAdded = prepareCreateReferencesJson(
15761
- it.name,
15762
- it.schema,
15763
- it.addedForeignKeys
15764
- );
15765
- const forAltered = prepareDropReferencesJson(
16594
+ const forAdded = prepareLibSQLCreateReferencesJson(
15766
16595
  it.name,
15767
16596
  it.schema,
15768
- it.deletedForeignKeys
16597
+ it.addedForeignKeys,
16598
+ json2,
16599
+ action
15769
16600
  );
15770
- const alteredFKs = prepareAlterReferencesJson(
16601
+ const forAltered = prepareLibSQLDropReferencesJson(
15771
16602
  it.name,
15772
16603
  it.schema,
15773
- it.alteredForeignKeys
16604
+ it.deletedForeignKeys,
16605
+ json2,
16606
+ _meta,
16607
+ action
15774
16608
  );
16609
+ const alteredFKs = prepareAlterReferencesJson(it.name, it.schema, it.alteredForeignKeys);
15775
16610
  return [...forAdded, ...forAltered, ...alteredFKs];
15776
16611
  }).flat();
15777
16612
  const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
@@ -15797,19 +16632,22 @@ var init_snapshotsDiffer = __esm({
15797
16632
  jsonStatements.push(...jsonDropColumnsStatemets);
15798
16633
  jsonStatements.push(...jsonAlteredCompositePKs);
15799
16634
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15800
- const sqlStatements = fromJson(jsonStatements, "sqlite");
16635
+ const combinedJsonStatements = libSQLCombineStatements(jsonStatements, json2, action);
16636
+ const sqlStatements = fromJson(
16637
+ combinedJsonStatements,
16638
+ "sqlite",
16639
+ action,
16640
+ "turso",
16641
+ json2
16642
+ );
15801
16643
  const uniqueSqlStatements = [];
15802
16644
  sqlStatements.forEach((ss) => {
15803
16645
  if (!uniqueSqlStatements.includes(ss)) {
15804
16646
  uniqueSqlStatements.push(ss);
15805
16647
  }
15806
16648
  });
15807
- const rTables = renamedTables.map((it) => {
15808
- return { from: it.from, to: it.to };
15809
- });
15810
- const _meta = prepareMigrationMeta([], rTables, rColumns);
15811
16649
  return {
15812
- statements: jsonStatements,
16650
+ statements: combinedJsonStatements,
15813
16651
  sqlStatements: uniqueSqlStatements,
15814
16652
  _meta
15815
16653
  };
@@ -33871,7 +34709,8 @@ var init_cli = __esm({
33871
34709
  schemaFilter: unionType([stringType(), stringType().array()]).optional().default(["public"]),
33872
34710
  extensionsFilters: literalType("postgis").array().optional(),
33873
34711
  verbose: booleanType().optional(),
33874
- strict: booleanType().optional()
34712
+ strict: booleanType().optional(),
34713
+ driver: driver.optional()
33875
34714
  }).passthrough();
33876
34715
  pullParams = objectType({
33877
34716
  config: stringType().optional(),
@@ -35037,284 +35876,8 @@ var sqlitePushIntrospect = async (db, filters) => {
35037
35876
  return { schema: schema4 };
35038
35877
  };
35039
35878
 
35040
- // src/cli/commands/sqlitePushUtils.ts
35041
- init_source();
35042
- init_sqliteSchema();
35043
- init_sqlgenerator();
35044
- var _moveDataStatements = (tableName, json, dataLoss = false) => {
35045
- const statements = [];
35046
- statements.push(
35047
- new SqliteRenameTableConvertor().convert({
35048
- type: "rename_table",
35049
- tableNameFrom: tableName,
35050
- tableNameTo: `__old_push_${tableName}`,
35051
- fromSchema: "",
35052
- toSchema: ""
35053
- })
35054
- );
35055
- const tableColumns = Object.values(json.tables[tableName].columns);
35056
- const referenceData = Object.values(json.tables[tableName].foreignKeys);
35057
- const compositePKs = Object.values(
35058
- json.tables[tableName].compositePrimaryKeys
35059
- ).map((it) => SQLiteSquasher.unsquashPK(it));
35060
- const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
35061
- statements.push(
35062
- new SQLiteCreateTableConvertor().convert({
35063
- type: "sqlite_create_table",
35064
- tableName,
35065
- columns: tableColumns,
35066
- referenceData: fks,
35067
- compositePKs
35068
- })
35069
- );
35070
- if (!dataLoss) {
35071
- statements.push(
35072
- `INSERT INTO "${tableName}" SELECT * FROM "__old_push_${tableName}";`
35073
- );
35074
- }
35075
- statements.push(
35076
- new SQLiteDropTableConvertor().convert({
35077
- type: "drop_table",
35078
- tableName: `__old_push_${tableName}`,
35079
- schema: ""
35080
- })
35081
- );
35082
- for (const idx of Object.values(json.tables[tableName].indexes)) {
35083
- statements.push(
35084
- new CreateSqliteIndexConvertor().convert({
35085
- type: "create_index",
35086
- tableName,
35087
- schema: "",
35088
- data: idx
35089
- })
35090
- );
35091
- }
35092
- return statements;
35093
- };
35094
- var getOldTableName = (tableName, meta) => {
35095
- for (const key of Object.keys(meta.tables)) {
35096
- const value = meta.tables[key];
35097
- if (`"${tableName}"` === value) {
35098
- return key.substring(1, key.length - 1);
35099
- }
35100
- }
35101
- return tableName;
35102
- };
35103
- var getNewTableName = (tableName, meta) => {
35104
- if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
35105
- return meta.tables[`"${tableName}"`].substring(
35106
- 1,
35107
- meta.tables[`"${tableName}"`].length - 1
35108
- );
35109
- }
35110
- return tableName;
35111
- };
35112
- var logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
35113
- let shouldAskForApprove = false;
35114
- const statementsToExecute = [];
35115
- const infoToPrint = [];
35116
- const tablesToRemove = [];
35117
- const columnsToRemove = [];
35118
- const schemasToRemove = [];
35119
- const tablesToTruncate = [];
35120
- const tablesContext = {};
35121
- for (const statement of statements) {
35122
- if (statement.type === "drop_table") {
35123
- const res = await connection.query(
35124
- `select count(*) as count from \`${statement.tableName}\``
35125
- );
35126
- const count2 = Number(res[0].count);
35127
- if (count2 > 0) {
35128
- infoToPrint.push(
35129
- `\xB7 You're about to delete ${source_default.underline(
35130
- statement.tableName
35131
- )} table with ${count2} items`
35132
- );
35133
- tablesToRemove.push(statement.tableName);
35134
- shouldAskForApprove = true;
35135
- }
35136
- const stmnt = fromJson([statement], "sqlite")[0];
35137
- statementsToExecute.push(stmnt);
35138
- } else if (statement.type === "alter_table_drop_column") {
35139
- const newTableName = getOldTableName(statement.tableName, meta);
35140
- const columnIsPartOfPk = Object.values(
35141
- json1.tables[newTableName].compositePrimaryKeys
35142
- ).find((c) => SQLiteSquasher.unsquashPK(c).includes(statement.columnName));
35143
- const columnIsPartOfIndex = Object.values(
35144
- json1.tables[newTableName].indexes
35145
- ).find((c) => SQLiteSquasher.unsquashIdx(c).columns.includes(statement.columnName));
35146
- const columnIsPk = json1.tables[newTableName].columns[statement.columnName].primaryKey;
35147
- const columnIsPartOfFk = Object.values(
35148
- json1.tables[newTableName].foreignKeys
35149
- ).find(
35150
- (t) => SQLiteSquasher.unsquashPushFK(t).columnsFrom.includes(
35151
- statement.columnName
35152
- )
35153
- );
35154
- const res = await connection.query(
35155
- `select count(*) as count from \`${newTableName}\``
35156
- );
35157
- const count2 = Number(res[0].count);
35158
- if (count2 > 0) {
35159
- infoToPrint.push(
35160
- `\xB7 You're about to delete ${source_default.underline(
35161
- statement.columnName
35162
- )} column in ${newTableName} table with ${count2} items`
35163
- );
35164
- columnsToRemove.push(`${newTableName}_${statement.columnName}`);
35165
- shouldAskForApprove = true;
35166
- }
35167
- if (columnIsPk || columnIsPartOfPk || columnIsPartOfIndex || columnIsPartOfFk) {
35168
- tablesContext[newTableName] = [
35169
- ..._moveDataStatements(statement.tableName, json2, true)
35170
- ];
35171
- const tablesReferncingCurrent = [];
35172
- for (const table4 of Object.values(json1.tables)) {
35173
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35174
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35175
- ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35176
- tablesReferncingCurrent.push(...tablesRefs);
35177
- }
35178
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35179
- for (const table4 of uniqueTableRefs) {
35180
- if (typeof tablesContext[table4] === "undefined") {
35181
- tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35182
- }
35183
- }
35184
- } else {
35185
- if (typeof tablesContext[newTableName] === "undefined") {
35186
- const stmnt = fromJson([statement], "sqlite")[0];
35187
- statementsToExecute.push(stmnt);
35188
- }
35189
- }
35190
- } else if (statement.type === "sqlite_alter_table_add_column") {
35191
- const newTableName = getOldTableName(statement.tableName, meta);
35192
- if (statement.column.notNull && !statement.column.default) {
35193
- const res = await connection.query(
35194
- `select count(*) as count from \`${newTableName}\``
35195
- );
35196
- const count2 = Number(res[0].count);
35197
- if (count2 > 0) {
35198
- infoToPrint.push(
35199
- `\xB7 You're about to add not-null ${source_default.underline(
35200
- statement.column.name
35201
- )} column without default value, which contains ${count2} items`
35202
- );
35203
- tablesToTruncate.push(newTableName);
35204
- statementsToExecute.push(`delete from ${newTableName};`);
35205
- shouldAskForApprove = true;
35206
- }
35207
- }
35208
- if (statement.column.primaryKey) {
35209
- tablesContext[newTableName] = [
35210
- ..._moveDataStatements(statement.tableName, json2, true)
35211
- ];
35212
- const tablesReferncingCurrent = [];
35213
- for (const table4 of Object.values(json1.tables)) {
35214
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35215
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35216
- ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35217
- tablesReferncingCurrent.push(...tablesRefs);
35218
- }
35219
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35220
- for (const table4 of uniqueTableRefs) {
35221
- if (typeof tablesContext[table4] === "undefined") {
35222
- tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35223
- }
35224
- }
35225
- } else {
35226
- if (typeof tablesContext[newTableName] === "undefined") {
35227
- const stmnt = fromJson([statement], "sqlite")[0];
35228
- statementsToExecute.push(stmnt);
35229
- }
35230
- }
35231
- } 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") {
35232
- if (!(statement.type === "alter_table_alter_column_set_notnull" && statement.columnPk)) {
35233
- const newTableName = getOldTableName(statement.tableName, meta);
35234
- if (statement.type === "alter_table_alter_column_set_notnull" && typeof statement.columnDefault === "undefined") {
35235
- const res = await connection.query(
35236
- `select count(*) as count from \`${newTableName}\``
35237
- );
35238
- const count2 = Number(res[0].count);
35239
- if (count2 > 0) {
35240
- infoToPrint.push(
35241
- `\xB7 You're about to add not-null constraint to ${source_default.underline(
35242
- statement.columnName
35243
- )} column without default value, which contains ${count2} items`
35244
- );
35245
- tablesToTruncate.push(newTableName);
35246
- shouldAskForApprove = true;
35247
- }
35248
- tablesContext[newTableName] = _moveDataStatements(
35249
- statement.tableName,
35250
- json1,
35251
- true
35252
- );
35253
- } else {
35254
- if (typeof tablesContext[newTableName] === "undefined") {
35255
- tablesContext[newTableName] = _moveDataStatements(
35256
- statement.tableName,
35257
- json1
35258
- );
35259
- }
35260
- }
35261
- const tablesReferncingCurrent = [];
35262
- for (const table4 of Object.values(json1.tables)) {
35263
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35264
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35265
- ).map((t) => {
35266
- return getNewTableName(
35267
- SQLiteSquasher.unsquashPushFK(t).tableFrom,
35268
- meta
35269
- );
35270
- });
35271
- tablesReferncingCurrent.push(...tablesRefs);
35272
- }
35273
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35274
- for (const table4 of uniqueTableRefs) {
35275
- if (typeof tablesContext[table4] === "undefined") {
35276
- tablesContext[table4] = [..._moveDataStatements(table4, json1)];
35277
- }
35278
- }
35279
- }
35280
- } else if (statement.type === "create_reference" || statement.type === "delete_reference" || statement.type === "alter_reference") {
35281
- const fk4 = SQLiteSquasher.unsquashPushFK(statement.data);
35282
- if (typeof tablesContext[statement.tableName] === "undefined") {
35283
- tablesContext[statement.tableName] = _moveDataStatements(
35284
- statement.tableName,
35285
- json2
35286
- );
35287
- }
35288
- } 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") {
35289
- const newTableName = getOldTableName(statement.tableName, meta);
35290
- if (typeof tablesContext[newTableName] === "undefined") {
35291
- tablesContext[newTableName] = _moveDataStatements(
35292
- statement.tableName,
35293
- json2
35294
- );
35295
- }
35296
- } else {
35297
- const stmnt = fromJson([statement], "sqlite");
35298
- if (typeof stmnt !== "undefined") {
35299
- statementsToExecute.push(...stmnt);
35300
- }
35301
- }
35302
- }
35303
- for (const context of Object.values(tablesContext)) {
35304
- statementsToExecute.push(...context);
35305
- }
35306
- return {
35307
- statementsToExecute,
35308
- shouldAskForApprove,
35309
- infoToPrint,
35310
- columnsToRemove: [...new Set(columnsToRemove)],
35311
- schemasToRemove: [...new Set(schemasToRemove)],
35312
- tablesToTruncate: [...new Set(tablesToTruncate)],
35313
- tablesToRemove: [...new Set(tablesToRemove)]
35314
- };
35315
- };
35316
-
35317
35879
  // src/api.ts
35880
+ init_sqlitePushUtils();
35318
35881
  init_global();
35319
35882
  init_migrationPreparator();
35320
35883
  init_mysqlSchema();