drizzle-kit 0.24.2-12ef143 → 0.24.2-1321cf9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. package/api.js +1235 -658
  2. package/api.mjs +1235 -658
  3. package/bin.cjs +50506 -47233
  4. package/index.d.mts +3 -4
  5. package/index.d.ts +3 -4
  6. package/package.json +2 -2
  7. package/utils.js +11 -0
  8. package/utils.mjs +10 -0
package/api.js CHANGED
@@ -8361,6 +8361,13 @@ var init_sqliteSchema = __esm({
8361
8361
  function isPgArrayType(sqlType) {
8362
8362
  return sqlType.match(/.*\[\d*\].*|.*\[\].*/g) !== null;
8363
8363
  }
8364
+ function findAddedAndRemoved(columnNames1, columnNames2) {
8365
+ const set1 = new Set(columnNames1);
8366
+ const set2 = new Set(columnNames2);
8367
+ const addedColumns = columnNames2.filter((it) => !set1.has(it));
8368
+ const removedColumns = columnNames1.filter((it) => !set2.has(it));
8369
+ return { addedColumns, removedColumns };
8370
+ }
8364
8371
  var copy, prepareMigrationMeta, schemaRenameKey, tableRenameKey, columnRenameKey;
8365
8372
  var init_utils = __esm({
8366
8373
  "src/utils.ts"() {
@@ -11470,7 +11477,20 @@ var init_jsonDiffer = __esm({
11470
11477
  });
11471
11478
 
11472
11479
  // src/sqlgenerator.ts
11473
- 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;
11480
+ function fromJson(statements, dialect7, action, json2) {
11481
+ const result = statements.flatMap((statement) => {
11482
+ const filtered = convertors.filter((it) => {
11483
+ return it.can(statement, dialect7);
11484
+ });
11485
+ const convertor = filtered.length === 1 ? filtered[0] : void 0;
11486
+ if (!convertor) {
11487
+ return "";
11488
+ }
11489
+ return convertor.convert(statement, json2, action);
11490
+ }).filter((it) => it !== "");
11491
+ return result;
11492
+ }
11493
+ 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;
11474
11494
  var init_sqlgenerator = __esm({
11475
11495
  "src/sqlgenerator.ts"() {
11476
11496
  "use strict";
@@ -11622,7 +11642,7 @@ var init_sqlgenerator = __esm({
11622
11642
  };
11623
11643
  SQLiteCreateTableConvertor = class extends Convertor {
11624
11644
  can(statement, dialect7) {
11625
- return statement.type === "sqlite_create_table" && dialect7 === "sqlite";
11645
+ return statement.type === "sqlite_create_table" && (dialect7 === "sqlite" || dialect7 === "turso");
11626
11646
  }
11627
11647
  convert(st) {
11628
11648
  const {
@@ -11792,32 +11812,6 @@ var init_sqlgenerator = __esm({
11792
11812
  return `ALTER TABLE \`${statement.tableName}\` DROP INDEX \`${unsquashed.name}\`;`;
11793
11813
  }
11794
11814
  };
11795
- SQLiteAlterTableAddUniqueConstraintConvertor = class extends Convertor {
11796
- can(statement, dialect7) {
11797
- return statement.type === "create_unique_constraint" && dialect7 === "sqlite";
11798
- }
11799
- convert(statement) {
11800
- return `/*
11801
- 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
11802
- Please refer to: https://www.techonthenet.com/sqlite/unique.php
11803
-
11804
- Due to that we don't generate migration automatically and it has to be done manually
11805
- */`;
11806
- }
11807
- };
11808
- SQLiteAlterTableDropUniqueConstraintConvertor = class extends Convertor {
11809
- can(statement, dialect7) {
11810
- return statement.type === "delete_unique_constraint" && dialect7 === "sqlite";
11811
- }
11812
- convert(statement) {
11813
- return `/*
11814
- 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
11815
- Please refer to: https://www.techonthenet.com/sqlite/unique.php
11816
-
11817
- Due to that we don't generate migration automatically and it has to be done manually
11818
- */`;
11819
- }
11820
- };
11821
11815
  CreatePgSequenceConvertor = class extends Convertor {
11822
11816
  can(statement, dialect7) {
11823
11817
  return statement.type === "create_sequence" && dialect7 === "postgresql";
@@ -11925,7 +11919,7 @@ var init_sqlgenerator = __esm({
11925
11919
  };
11926
11920
  SQLiteDropTableConvertor = class extends Convertor {
11927
11921
  can(statement, dialect7) {
11928
- return statement.type === "drop_table" && dialect7 === "sqlite";
11922
+ return statement.type === "drop_table" && (dialect7 === "sqlite" || dialect7 === "turso");
11929
11923
  }
11930
11924
  convert(statement) {
11931
11925
  const { tableName } = statement;
@@ -11945,7 +11939,7 @@ var init_sqlgenerator = __esm({
11945
11939
  };
11946
11940
  SqliteRenameTableConvertor = class extends Convertor {
11947
11941
  can(statement, dialect7) {
11948
- return statement.type === "rename_table" && dialect7 === "sqlite";
11942
+ return statement.type === "rename_table" && (dialect7 === "sqlite" || dialect7 === "turso");
11949
11943
  }
11950
11944
  convert(statement) {
11951
11945
  const { tableNameFrom, tableNameTo } = statement;
@@ -11982,11 +11976,11 @@ var init_sqlgenerator = __esm({
11982
11976
  };
11983
11977
  SQLiteAlterTableRenameColumnConvertor = class extends Convertor {
11984
11978
  can(statement, dialect7) {
11985
- return statement.type === "alter_table_rename_column" && dialect7 === "sqlite";
11979
+ return statement.type === "alter_table_rename_column" && (dialect7 === "sqlite" || dialect7 === "turso");
11986
11980
  }
11987
11981
  convert(statement) {
11988
11982
  const { tableName, oldColumnName, newColumnName } = statement;
11989
- return `ALTER TABLE \`${tableName}\` RENAME COLUMN \`${oldColumnName}\` TO \`${newColumnName}\`;`;
11983
+ return `ALTER TABLE \`${tableName}\` RENAME COLUMN "${oldColumnName}" TO "${newColumnName}";`;
11990
11984
  }
11991
11985
  };
11992
11986
  PgAlterTableDropColumnConvertor = class extends Convertor {
@@ -12010,7 +12004,7 @@ var init_sqlgenerator = __esm({
12010
12004
  };
12011
12005
  SQLiteAlterTableDropColumnConvertor = class extends Convertor {
12012
12006
  can(statement, dialect7) {
12013
- return statement.type === "alter_table_drop_column" && dialect7 === "sqlite";
12007
+ return statement.type === "alter_table_drop_column" && (dialect7 === "sqlite" || dialect7 === "turso");
12014
12008
  }
12015
12009
  convert(statement) {
12016
12010
  const { tableName, columnName } = statement;
@@ -12063,7 +12057,7 @@ var init_sqlgenerator = __esm({
12063
12057
  };
12064
12058
  SQLiteAlterTableAddColumnConvertor = class extends Convertor {
12065
12059
  can(statement, dialect7) {
12066
- return statement.type === "sqlite_alter_table_add_column" && dialect7 === "sqlite";
12060
+ return statement.type === "sqlite_alter_table_add_column" && (dialect7 === "sqlite" || dialect7 === "turso");
12067
12061
  }
12068
12062
  convert(statement) {
12069
12063
  const { tableName, column: column4, referenceData } = statement;
@@ -12087,21 +12081,6 @@ var init_sqlgenerator = __esm({
12087
12081
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DATA TYPE ${newDataType};`;
12088
12082
  }
12089
12083
  };
12090
- SQLiteAlterTableAlterColumnSetTypeConvertor = class extends Convertor {
12091
- can(statement, dialect7) {
12092
- return statement.type === "alter_table_alter_column_set_type" && dialect7 === "sqlite";
12093
- }
12094
- convert(statement) {
12095
- return `/*
12096
- 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
12097
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12098
- https://www.sqlite.org/lang_altertable.html
12099
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12100
-
12101
- Due to that we don't generate migration automatically and it has to be done manually
12102
- */`;
12103
- }
12104
- };
12105
12084
  PgAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
12106
12085
  can(statement, dialect7) {
12107
12086
  return statement.type === "alter_table_alter_column_set_default" && dialect7 === "postgresql";
@@ -12112,21 +12091,6 @@ var init_sqlgenerator = __esm({
12112
12091
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DEFAULT ${statement.newDefaultValue};`;
12113
12092
  }
12114
12093
  };
12115
- SqliteAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
12116
- can(statement, dialect7) {
12117
- return statement.type === "alter_table_alter_column_set_default" && dialect7 === "sqlite";
12118
- }
12119
- convert(statement) {
12120
- return `/*
12121
- 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
12122
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12123
- https://www.sqlite.org/lang_altertable.html
12124
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12125
-
12126
- Due to that we don't generate migration automatically and it has to be done manually
12127
- */`;
12128
- }
12129
- };
12130
12094
  PgAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
12131
12095
  can(statement, dialect7) {
12132
12096
  return statement.type === "alter_table_alter_column_drop_default" && dialect7 === "postgresql";
@@ -12225,7 +12189,7 @@ var init_sqlgenerator = __esm({
12225
12189
  };
12226
12190
  SqliteAlterTableAlterColumnDropGeneratedConvertor = class extends Convertor {
12227
12191
  can(statement, dialect7) {
12228
- return statement.type === "alter_table_alter_column_drop_generated" && dialect7 === "sqlite";
12192
+ return statement.type === "alter_table_alter_column_drop_generated" && (dialect7 === "sqlite" || dialect7 === "turso");
12229
12193
  }
12230
12194
  convert(statement) {
12231
12195
  const {
@@ -12266,7 +12230,7 @@ var init_sqlgenerator = __esm({
12266
12230
  };
12267
12231
  SqliteAlterTableAlterColumnSetExpressionConvertor = class extends Convertor {
12268
12232
  can(statement, dialect7) {
12269
- return statement.type === "alter_table_alter_column_set_generated" && dialect7 === "sqlite";
12233
+ return statement.type === "alter_table_alter_column_set_generated" && (dialect7 === "sqlite" || dialect7 === "turso");
12270
12234
  }
12271
12235
  convert(statement) {
12272
12236
  const {
@@ -12307,7 +12271,7 @@ var init_sqlgenerator = __esm({
12307
12271
  };
12308
12272
  SqliteAlterTableAlterColumnAlterGeneratedConvertor = class extends Convertor {
12309
12273
  can(statement, dialect7) {
12310
- return statement.type === "alter_table_alter_column_alter_generated" && dialect7 === "sqlite";
12274
+ return statement.type === "alter_table_alter_column_alter_generated" && (dialect7 === "sqlite" || dialect7 === "turso");
12311
12275
  }
12312
12276
  convert(statement) {
12313
12277
  const {
@@ -12400,6 +12364,67 @@ var init_sqlgenerator = __esm({
12400
12364
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY`;
12401
12365
  }
12402
12366
  };
12367
+ LibSQLModifyColumn = class extends Convertor {
12368
+ can(statement, dialect7) {
12369
+ 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 === "turso";
12370
+ }
12371
+ convert(statement, json2) {
12372
+ const { tableName, columnName } = statement;
12373
+ let columnType = ``;
12374
+ let columnDefault = "";
12375
+ let columnNotNull = "";
12376
+ const sqlStatements = [];
12377
+ const indexes = [];
12378
+ for (const table4 of Object.values(json2.tables)) {
12379
+ for (const index4 of Object.values(table4.indexes)) {
12380
+ const unsquashed = SQLiteSquasher.unsquashIdx(index4);
12381
+ sqlStatements.push(`DROP INDEX IF EXISTS "${unsquashed.name}";`);
12382
+ indexes.push({ ...unsquashed, tableName: table4.name });
12383
+ }
12384
+ }
12385
+ switch (statement.type) {
12386
+ case "alter_table_alter_column_set_type":
12387
+ columnType = ` ${statement.newDataType}`;
12388
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12389
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12390
+ break;
12391
+ case "alter_table_alter_column_drop_notnull":
12392
+ columnType = ` ${statement.newDataType}`;
12393
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12394
+ columnNotNull = "";
12395
+ break;
12396
+ case "alter_table_alter_column_set_notnull":
12397
+ columnType = ` ${statement.newDataType}`;
12398
+ columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12399
+ columnNotNull = ` NOT NULL`;
12400
+ break;
12401
+ case "alter_table_alter_column_set_default":
12402
+ columnType = ` ${statement.newDataType}`;
12403
+ columnDefault = ` DEFAULT ${statement.newDefaultValue}`;
12404
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12405
+ break;
12406
+ case "alter_table_alter_column_drop_default":
12407
+ columnType = ` ${statement.newDataType}`;
12408
+ columnDefault = "";
12409
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12410
+ break;
12411
+ }
12412
+ columnDefault = columnDefault instanceof Date ? columnDefault.toISOString() : columnDefault;
12413
+ sqlStatements.push(
12414
+ `ALTER TABLE \`${tableName}\` ALTER COLUMN "${columnName}" TO "${columnName}"${columnType}${columnNotNull}${columnDefault};`
12415
+ );
12416
+ for (const index4 of indexes) {
12417
+ const indexPart = index4.isUnique ? "UNIQUE INDEX" : "INDEX";
12418
+ const whereStatement = index4.where ? ` WHERE ${index4.where}` : "";
12419
+ const uniqueString = index4.columns.map((it) => `\`${it}\``).join(",");
12420
+ const tableName2 = index4.tableName;
12421
+ sqlStatements.push(
12422
+ `CREATE ${indexPart} \`${index4.name}\` ON \`${tableName2}\` (${uniqueString})${whereStatement};`
12423
+ );
12424
+ }
12425
+ return sqlStatements;
12426
+ }
12427
+ };
12403
12428
  MySqlModifyColumn = class extends Convertor {
12404
12429
  can(statement, dialect7) {
12405
12430
  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";
@@ -12537,21 +12562,6 @@ var init_sqlgenerator = __esm({
12537
12562
  return `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${columnName}\`${columnType}${columnAutoincrement}${columnGenerated}${columnNotNull}${columnDefault}${columnOnUpdate};`;
12538
12563
  }
12539
12564
  };
12540
- SqliteAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
12541
- can(statement, dialect7) {
12542
- return statement.type === "alter_table_alter_column_drop_default" && dialect7 === "sqlite";
12543
- }
12544
- convert(statement) {
12545
- return `/*
12546
- 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
12547
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12548
- https://www.sqlite.org/lang_altertable.html
12549
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12550
-
12551
- Due to that we don't generate migration automatically and it has to be done manually
12552
- */`;
12553
- }
12554
- };
12555
12565
  PgAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
12556
12566
  can(statement, dialect7) {
12557
12567
  return statement.type === "create_composite_pk" && dialect7 === "postgresql";
@@ -12616,85 +12626,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12616
12626
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY, ADD PRIMARY KEY(\`${newColumns.join("`,`")}\`);`;
12617
12627
  }
12618
12628
  };
12619
- SqliteAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
12620
- can(statement, dialect7) {
12621
- return statement.type === "create_composite_pk" && dialect7 === "sqlite";
12622
- }
12623
- convert(statement) {
12624
- let msg = "/*\n";
12625
- msg += `You're trying to add PRIMARY KEY(${statement.data}) to '${statement.tableName}' table
12626
- `;
12627
- msg += "SQLite does not support adding primary key to an already created table\n";
12628
- msg += "You can do it in 3 steps with drizzle orm:\n";
12629
- msg += " - create new mirror table with needed pk, rename current table to old_table, generate SQL\n";
12630
- msg += " - migrate old data from one table to another\n";
12631
- msg += " - delete old_table in schema, generate sql\n\n";
12632
- msg += "or create manual migration like below:\n\n";
12633
- msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12634
- msg += "CREATE TABLE table_name (\n";
12635
- msg += " column1 datatype [ NULL | NOT NULL ],\n";
12636
- msg += " column2 datatype [ NULL | NOT NULL ],\n";
12637
- msg += " ...\n";
12638
- msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12639
- msg += " );\n";
12640
- msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12641
- msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12642
- msg += "*/\n";
12643
- return msg;
12644
- }
12645
- };
12646
- SqliteAlterTableDeleteCompositePrimaryKeyConvertor = class extends Convertor {
12647
- can(statement, dialect7) {
12648
- return statement.type === "delete_composite_pk" && dialect7 === "sqlite";
12649
- }
12650
- convert(statement) {
12651
- let msg = "/*\n";
12652
- msg += `You're trying to delete PRIMARY KEY(${statement.data}) from '${statement.tableName}' table
12653
- `;
12654
- msg += "SQLite does not supportprimary key deletion from existing table\n";
12655
- msg += "You can do it in 3 steps with drizzle orm:\n";
12656
- msg += " - create new mirror table table without pk, rename current table to old_table, generate SQL\n";
12657
- msg += " - migrate old data from one table to another\n";
12658
- msg += " - delete old_table in schema, generate sql\n\n";
12659
- msg += "or create manual migration like below:\n\n";
12660
- msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12661
- msg += "CREATE TABLE table_name (\n";
12662
- msg += " column1 datatype [ NULL | NOT NULL ],\n";
12663
- msg += " column2 datatype [ NULL | NOT NULL ],\n";
12664
- msg += " ...\n";
12665
- msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12666
- msg += " );\n";
12667
- msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12668
- msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12669
- msg += "*/\n";
12670
- return msg;
12671
- }
12672
- };
12673
- SqliteAlterTableAlterCompositePrimaryKeyConvertor = class extends Convertor {
12674
- can(statement, dialect7) {
12675
- return statement.type === "alter_composite_pk" && dialect7 === "sqlite";
12676
- }
12677
- convert(statement) {
12678
- let msg = "/*\n";
12679
- msg += "SQLite does not support altering primary key\n";
12680
- msg += "You can do it in 3 steps with drizzle orm:\n";
12681
- msg += " - create new mirror table with needed pk, rename current table to old_table, generate SQL\n";
12682
- msg += " - migrate old data from one table to another\n";
12683
- msg += " - delete old_table in schema, generate sql\n\n";
12684
- msg += "or create manual migration like below:\n\n";
12685
- msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12686
- msg += "CREATE TABLE table_name (\n";
12687
- msg += " column1 datatype [ NULL | NOT NULL ],\n";
12688
- msg += " column2 datatype [ NULL | NOT NULL ],\n";
12689
- msg += " ...\n";
12690
- msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12691
- msg += " );\n";
12692
- msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12693
- msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12694
- msg += "*/\n";
12695
- return msg;
12696
- }
12697
- };
12698
12629
  PgAlterTableAlterColumnSetPrimaryKeyConvertor = class extends Convertor {
12699
12630
  can(statement, dialect7) {
12700
12631
  return statement.type === "alter_table_alter_column_set_pk" && dialect7 === "postgresql";
@@ -12739,51 +12670,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12739
12670
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET NOT NULL;`;
12740
12671
  }
12741
12672
  };
12742
- SqliteAlterTableAlterColumnSetNotNullConvertor = class extends Convertor {
12743
- can(statement, dialect7) {
12744
- return statement.type === "alter_table_alter_column_set_notnull" && dialect7 === "sqlite";
12745
- }
12746
- convert(statement) {
12747
- return `/*
12748
- 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
12749
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12750
- https://www.sqlite.org/lang_altertable.html
12751
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12752
-
12753
- Due to that we don't generate migration automatically and it has to be done manually
12754
- */`;
12755
- }
12756
- };
12757
- SqliteAlterTableAlterColumnSetAutoincrementConvertor = class extends Convertor {
12758
- can(statement, dialect7) {
12759
- return statement.type === "alter_table_alter_column_set_autoincrement" && dialect7 === "sqlite";
12760
- }
12761
- convert(statement) {
12762
- return `/*
12763
- 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
12764
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12765
- https://www.sqlite.org/lang_altertable.html
12766
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12767
-
12768
- Due to that we don't generate migration automatically and it has to be done manually
12769
- */`;
12770
- }
12771
- };
12772
- SqliteAlterTableAlterColumnDropAutoincrementConvertor = class extends Convertor {
12773
- can(statement, dialect7) {
12774
- return statement.type === "alter_table_alter_column_drop_autoincrement" && dialect7 === "sqlite";
12775
- }
12776
- convert(statement) {
12777
- return `/*
12778
- 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
12779
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12780
- https://www.sqlite.org/lang_altertable.html
12781
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12782
-
12783
- Due to that we don't generate migration automatically and it has to be done manually
12784
- */`;
12785
- }
12786
- };
12787
12673
  PgAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12788
12674
  can(statement, dialect7) {
12789
12675
  return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "postgresql";
@@ -12794,21 +12680,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12794
12680
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" DROP NOT NULL;`;
12795
12681
  }
12796
12682
  };
12797
- SqliteAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12798
- can(statement, dialect7) {
12799
- return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "sqlite";
12800
- }
12801
- convert(statement) {
12802
- return `/*
12803
- 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
12804
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12805
- https://www.sqlite.org/lang_altertable.html
12806
- https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12807
-
12808
- Due to that we don't generate migration automatically and it has to be done manually
12809
- */`;
12810
- }
12811
- };
12812
12683
  PgCreateForeignKeyConvertor = class extends Convertor {
12813
12684
  can(statement, dialect7) {
12814
12685
  return statement.type === "create_reference" && dialect7 === "postgresql";
@@ -12839,18 +12710,21 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12839
12710
  return sql2;
12840
12711
  }
12841
12712
  };
12842
- SqliteCreateForeignKeyConvertor = class extends Convertor {
12713
+ LibSQLCreateForeignKeyConvertor = class extends Convertor {
12843
12714
  can(statement, dialect7) {
12844
- return statement.type === "create_reference" && dialect7 === "sqlite";
12715
+ return statement.type === "create_reference" && dialect7 === "turso";
12845
12716
  }
12846
- convert(statement) {
12847
- return `/*
12848
- 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
12849
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12850
- https://www.sqlite.org/lang_altertable.html
12851
-
12852
- Due to that we don't generate migration automatically and it has to be done manually
12853
- */`;
12717
+ convert(statement, json2, action) {
12718
+ const { columnsFrom, columnsTo, tableFrom, onDelete, onUpdate, tableTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
12719
+ const { columnDefault, columnNotNull, columnType } = statement;
12720
+ const onDeleteStatement = onDelete ? ` ON DELETE ${onDelete}` : "";
12721
+ const onUpdateStatement = onUpdate ? ` ON UPDATE ${onUpdate}` : "";
12722
+ const columnsDefaultValue = columnDefault ? ` DEFAULT ${columnDefault}` : "";
12723
+ const columnNotNullValue = columnNotNull ? ` NOT NULL` : "";
12724
+ const columnTypeValue = columnType ? ` ${columnType}` : "";
12725
+ const columnFrom = columnsFrom[0];
12726
+ const columnTo = columnsTo[0];
12727
+ return `ALTER TABLE \`${tableFrom}\` ALTER COLUMN "${columnFrom}" TO "${columnFrom}"${columnTypeValue}${columnNotNullValue}${columnsDefaultValue} REFERENCES ${tableTo}(${columnTo})${onDeleteStatement}${onUpdateStatement};`;
12854
12728
  }
12855
12729
  };
12856
12730
  MySqlCreateForeignKeyConvertor = class extends Convertor {
@@ -12899,20 +12773,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12899
12773
  return sql2;
12900
12774
  }
12901
12775
  };
12902
- SqliteAlterForeignKeyConvertor = class extends Convertor {
12903
- can(statement, dialect7) {
12904
- return statement.type === "alter_reference" && dialect7 === "sqlite";
12905
- }
12906
- convert(statement) {
12907
- return `/*
12908
- 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
12909
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12910
- https://www.sqlite.org/lang_altertable.html
12911
-
12912
- Due to that we don't generate migration automatically and it has to be done manually
12913
- */`;
12914
- }
12915
- };
12916
12776
  PgDeleteForeignKeyConvertor = class extends Convertor {
12917
12777
  can(statement, dialect7) {
12918
12778
  return statement.type === "delete_reference" && dialect7 === "postgresql";
@@ -12925,20 +12785,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12925
12785
  `;
12926
12786
  }
12927
12787
  };
12928
- SqliteDeleteForeignKeyConvertor = class extends Convertor {
12929
- can(statement, dialect7) {
12930
- return statement.type === "delete_reference" && dialect7 === "sqlite";
12931
- }
12932
- convert(statement) {
12933
- return `/*
12934
- 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
12935
- Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12936
- https://www.sqlite.org/lang_altertable.html
12937
-
12938
- Due to that we don't generate migration automatically and it has to be done manually
12939
- */`;
12940
- }
12941
- };
12942
12788
  MySqlDeleteForeignKeyConvertor = class extends Convertor {
12943
12789
  can(statement, dialect7) {
12944
12790
  return statement.type === "delete_reference" && dialect7 === "mysql";
@@ -12999,7 +12845,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12999
12845
  };
13000
12846
  CreateSqliteIndexConvertor = class extends Convertor {
13001
12847
  can(statement, dialect7) {
13002
- return statement.type === "create_index" && dialect7 === "sqlite";
12848
+ return statement.type === "create_index" && (dialect7 === "sqlite" || dialect7 === "turso");
13003
12849
  }
13004
12850
  convert(statement) {
13005
12851
  const { name: name2, columns, isUnique, where } = SQLiteSquasher.unsquashIdx(
@@ -13086,7 +12932,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13086
12932
  };
13087
12933
  SqliteDropIndexConvertor = class extends Convertor {
13088
12934
  can(statement, dialect7) {
13089
- return statement.type === "drop_index" && dialect7 === "sqlite";
12935
+ return statement.type === "drop_index" && (dialect7 === "sqlite" || dialect7 === "turso");
13090
12936
  }
13091
12937
  convert(statement) {
13092
12938
  const { name: name2 } = PgSquasher.unsquashIdx(statement.data);
@@ -13102,10 +12948,96 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13102
12948
  return `DROP INDEX \`${name2}\` ON \`${statement.tableName}\`;`;
13103
12949
  }
13104
12950
  };
12951
+ SQLiteRecreateTableConvertor = class extends Convertor {
12952
+ can(statement, dialect7) {
12953
+ return statement.type === "recreate_table" && dialect7 === "sqlite";
12954
+ }
12955
+ convert(statement) {
12956
+ const { tableName, columns, compositePKs, referenceData } = statement;
12957
+ const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
12958
+ const newTableName = `__new_${tableName}`;
12959
+ const sqlStatements = [];
12960
+ sqlStatements.push(`PRAGMA foreign_keys=OFF;`);
12961
+ sqlStatements.push(
12962
+ new SQLiteCreateTableConvertor().convert({
12963
+ type: "sqlite_create_table",
12964
+ tableName: newTableName,
12965
+ columns,
12966
+ referenceData,
12967
+ compositePKs
12968
+ })
12969
+ );
12970
+ sqlStatements.push(
12971
+ `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
12972
+ );
12973
+ sqlStatements.push(
12974
+ new SQLiteDropTableConvertor().convert({
12975
+ type: "drop_table",
12976
+ tableName,
12977
+ schema: ""
12978
+ })
12979
+ );
12980
+ sqlStatements.push(
12981
+ new SqliteRenameTableConvertor().convert({
12982
+ fromSchema: "",
12983
+ tableNameFrom: newTableName,
12984
+ tableNameTo: tableName,
12985
+ toSchema: "",
12986
+ type: "rename_table"
12987
+ })
12988
+ );
12989
+ sqlStatements.push(`PRAGMA foreign_keys=ON;`);
12990
+ return sqlStatements;
12991
+ }
12992
+ };
12993
+ LibSQLRecreateTableConvertor = class extends Convertor {
12994
+ can(statement, dialect7) {
12995
+ return statement.type === "recreate_table" && dialect7 === "turso";
12996
+ }
12997
+ convert(statement) {
12998
+ const { tableName, columns, compositePKs, referenceData } = statement;
12999
+ const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
13000
+ const newTableName = `__new_${tableName}`;
13001
+ const sqlStatements = [];
13002
+ sqlStatements.push(`PRAGMA foreign_keys=OFF;`);
13003
+ sqlStatements.push(
13004
+ new SQLiteCreateTableConvertor().convert({
13005
+ type: "sqlite_create_table",
13006
+ tableName: newTableName,
13007
+ columns,
13008
+ referenceData,
13009
+ compositePKs
13010
+ })
13011
+ );
13012
+ sqlStatements.push(
13013
+ `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
13014
+ );
13015
+ sqlStatements.push(
13016
+ new SQLiteDropTableConvertor().convert({
13017
+ type: "drop_table",
13018
+ tableName,
13019
+ schema: ""
13020
+ })
13021
+ );
13022
+ sqlStatements.push(
13023
+ new SqliteRenameTableConvertor().convert({
13024
+ fromSchema: "",
13025
+ tableNameFrom: newTableName,
13026
+ tableNameTo: tableName,
13027
+ toSchema: "",
13028
+ type: "rename_table"
13029
+ })
13030
+ );
13031
+ sqlStatements.push(`PRAGMA foreign_keys=ON;`);
13032
+ return sqlStatements;
13033
+ }
13034
+ };
13105
13035
  convertors = [];
13106
13036
  convertors.push(new PgCreateTableConvertor());
13107
13037
  convertors.push(new MySqlCreateTableConvertor());
13108
13038
  convertors.push(new SQLiteCreateTableConvertor());
13039
+ convertors.push(new SQLiteRecreateTableConvertor());
13040
+ convertors.push(new LibSQLRecreateTableConvertor());
13109
13041
  convertors.push(new CreateTypeEnumConvertor());
13110
13042
  convertors.push(new CreatePgSequenceConvertor());
13111
13043
  convertors.push(new DropPgSequenceConvertor());
@@ -13153,6 +13085,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13153
13085
  convertors.push(new SqliteAlterTableAlterColumnAlterGeneratedConvertor());
13154
13086
  convertors.push(new SqliteAlterTableAlterColumnSetExpressionConvertor());
13155
13087
  convertors.push(new MySqlModifyColumn());
13088
+ convertors.push(new LibSQLModifyColumn());
13156
13089
  convertors.push(new PgCreateForeignKeyConvertor());
13157
13090
  convertors.push(new MySqlCreateForeignKeyConvertor());
13158
13091
  convertors.push(new PgAlterForeignKeyConvertor());
@@ -13164,24 +13097,10 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13164
13097
  convertors.push(new PgAlterTableSetSchemaConvertor());
13165
13098
  convertors.push(new PgAlterTableSetNewSchemaConvertor());
13166
13099
  convertors.push(new PgAlterTableRemoveFromSchemaConvertor());
13167
- convertors.push(new SQLiteAlterTableAlterColumnSetTypeConvertor());
13168
- convertors.push(new SqliteAlterForeignKeyConvertor());
13169
- convertors.push(new SqliteDeleteForeignKeyConvertor());
13170
- convertors.push(new SqliteCreateForeignKeyConvertor());
13171
- convertors.push(new SQLiteAlterTableAddUniqueConstraintConvertor());
13172
- convertors.push(new SQLiteAlterTableDropUniqueConstraintConvertor());
13100
+ convertors.push(new LibSQLCreateForeignKeyConvertor());
13173
13101
  convertors.push(new PgAlterTableAlterColumnDropGenerated());
13174
13102
  convertors.push(new PgAlterTableAlterColumnSetGenerated());
13175
13103
  convertors.push(new PgAlterTableAlterColumnAlterGenerated());
13176
- convertors.push(new SqliteAlterTableAlterColumnSetNotNullConvertor());
13177
- convertors.push(new SqliteAlterTableAlterColumnDropNotNullConvertor());
13178
- convertors.push(new SqliteAlterTableAlterColumnSetDefaultConvertor());
13179
- convertors.push(new SqliteAlterTableAlterColumnDropDefaultConvertor());
13180
- convertors.push(new SqliteAlterTableAlterColumnSetAutoincrementConvertor());
13181
- convertors.push(new SqliteAlterTableAlterColumnDropAutoincrementConvertor());
13182
- convertors.push(new SqliteAlterTableCreateCompositePrimaryKeyConvertor());
13183
- convertors.push(new SqliteAlterTableDeleteCompositePrimaryKeyConvertor());
13184
- convertors.push(new SqliteAlterTableAlterCompositePrimaryKeyConvertor());
13185
13104
  convertors.push(new PgAlterTableCreateCompositePrimaryKeyConvertor());
13186
13105
  convertors.push(new PgAlterTableDeleteCompositePrimaryKeyConvertor());
13187
13106
  convertors.push(new PgAlterTableAlterCompositePrimaryKeyConvertor());
@@ -13190,19 +13109,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13190
13109
  convertors.push(new MySqlAlterTableCreateCompositePrimaryKeyConvertor());
13191
13110
  convertors.push(new MySqlAlterTableAddPk());
13192
13111
  convertors.push(new MySqlAlterTableAlterCompositePrimaryKeyConvertor());
13193
- fromJson = (statements, dialect7) => {
13194
- const result = statements.flatMap((statement) => {
13195
- const filtered = convertors.filter((it) => {
13196
- return it.can(statement, dialect7);
13197
- });
13198
- const convertor = filtered.length === 1 ? filtered[0] : void 0;
13199
- if (!convertor) {
13200
- return "";
13201
- }
13202
- return convertor.convert(statement);
13203
- }).filter((it) => it !== "");
13204
- return result;
13205
- };
13206
13112
  https: `
13207
13113
  create table users (
13208
13114
  id int,
@@ -13229,12 +13135,248 @@ drop type __venum;
13229
13135
  }
13230
13136
  });
13231
13137
 
13138
+ // src/cli/commands/sqlitePushUtils.ts
13139
+ var _moveDataStatements, getOldTableName, getNewTableName, logSuggestionsAndReturn;
13140
+ var init_sqlitePushUtils = __esm({
13141
+ "src/cli/commands/sqlitePushUtils.ts"() {
13142
+ "use strict";
13143
+ init_source();
13144
+ init_sqliteSchema();
13145
+ init_sqlgenerator();
13146
+ init_utils();
13147
+ _moveDataStatements = (tableName, json, dataLoss = false) => {
13148
+ const statements = [];
13149
+ const newTableName = `__new_${tableName}`;
13150
+ const tableColumns = Object.values(json.tables[tableName].columns);
13151
+ const referenceData = Object.values(json.tables[tableName].foreignKeys);
13152
+ const compositePKs = Object.values(
13153
+ json.tables[tableName].compositePrimaryKeys
13154
+ ).map((it) => SQLiteSquasher.unsquashPK(it));
13155
+ const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
13156
+ statements.push(
13157
+ new SQLiteCreateTableConvertor().convert({
13158
+ type: "sqlite_create_table",
13159
+ tableName: newTableName,
13160
+ columns: tableColumns,
13161
+ referenceData: fks,
13162
+ compositePKs
13163
+ })
13164
+ );
13165
+ if (!dataLoss) {
13166
+ const columns = Object.keys(json.tables[tableName].columns).map(
13167
+ (c) => `"${c}"`
13168
+ );
13169
+ statements.push(
13170
+ `INSERT INTO \`${newTableName}\`(${columns.join(
13171
+ ", "
13172
+ )}) SELECT ${columns.join(", ")} FROM \`${tableName}\`;`
13173
+ );
13174
+ }
13175
+ statements.push(
13176
+ new SQLiteDropTableConvertor().convert({
13177
+ type: "drop_table",
13178
+ tableName,
13179
+ schema: ""
13180
+ })
13181
+ );
13182
+ statements.push(
13183
+ new SqliteRenameTableConvertor().convert({
13184
+ fromSchema: "",
13185
+ tableNameFrom: newTableName,
13186
+ tableNameTo: tableName,
13187
+ toSchema: "",
13188
+ type: "rename_table"
13189
+ })
13190
+ );
13191
+ for (const idx of Object.values(json.tables[tableName].indexes)) {
13192
+ statements.push(
13193
+ new CreateSqliteIndexConvertor().convert({
13194
+ type: "create_index",
13195
+ tableName,
13196
+ schema: "",
13197
+ data: idx
13198
+ })
13199
+ );
13200
+ }
13201
+ return statements;
13202
+ };
13203
+ getOldTableName = (tableName, meta) => {
13204
+ for (const key of Object.keys(meta.tables)) {
13205
+ const value = meta.tables[key];
13206
+ if (`"${tableName}"` === value) {
13207
+ return key.substring(1, key.length - 1);
13208
+ }
13209
+ }
13210
+ return tableName;
13211
+ };
13212
+ getNewTableName = (tableName, meta) => {
13213
+ if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
13214
+ return meta.tables[`"${tableName}"`].substring(
13215
+ 1,
13216
+ meta.tables[`"${tableName}"`].length - 1
13217
+ );
13218
+ }
13219
+ return tableName;
13220
+ };
13221
+ logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
13222
+ let shouldAskForApprove = false;
13223
+ const statementsToExecute = [];
13224
+ const infoToPrint = [];
13225
+ const tablesToRemove = [];
13226
+ const columnsToRemove = [];
13227
+ const schemasToRemove = [];
13228
+ const tablesToTruncate = [];
13229
+ for (const statement of statements) {
13230
+ if (statement.type === "drop_table") {
13231
+ const res = await connection.query(
13232
+ `select count(*) as count from \`${statement.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
+ statement.tableName
13239
+ )} table with ${count2} items`
13240
+ );
13241
+ tablesToRemove.push(statement.tableName);
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 === "alter_table_drop_column") {
13249
+ const tableName = statement.tableName;
13250
+ const columnName = statement.columnName;
13251
+ const res = await connection.query(
13252
+ `select count(\`${tableName}\`.\`${columnName}\`) 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 delete ${source_default.underline(
13258
+ columnName
13259
+ )} column in ${tableName} table with ${count2} items`
13260
+ );
13261
+ columnsToRemove.push(`${tableName}_${statement.columnName}`);
13262
+ shouldAskForApprove = true;
13263
+ }
13264
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13265
+ statementsToExecute.push(
13266
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13267
+ );
13268
+ } else if (statement.type === "sqlite_alter_table_add_column" && (statement.column.notNull && !statement.column.default)) {
13269
+ const tableName = statement.tableName;
13270
+ const columnName = statement.column.name;
13271
+ const res = await connection.query(
13272
+ `select count(*) as count from \`${tableName}\``
13273
+ );
13274
+ const count2 = Number(res[0].count);
13275
+ if (count2 > 0) {
13276
+ infoToPrint.push(
13277
+ `\xB7 You're about to add not-null ${source_default.underline(
13278
+ columnName
13279
+ )} column without default value, which contains ${count2} items`
13280
+ );
13281
+ tablesToTruncate.push(tableName);
13282
+ statementsToExecute.push(`delete from ${tableName};`);
13283
+ shouldAskForApprove = true;
13284
+ }
13285
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13286
+ statementsToExecute.push(
13287
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13288
+ );
13289
+ } else if (statement.type === "recreate_table") {
13290
+ const tableName = statement.tableName;
13291
+ const oldTableName = getOldTableName(tableName, meta);
13292
+ let dataLoss = false;
13293
+ const prevColumnNames = Object.keys(json1.tables[oldTableName].columns);
13294
+ const currentColumnNames = Object.keys(json2.tables[tableName].columns);
13295
+ const { removedColumns, addedColumns } = findAddedAndRemoved(
13296
+ prevColumnNames,
13297
+ currentColumnNames
13298
+ );
13299
+ if (removedColumns.length) {
13300
+ for (const removedColumn of removedColumns) {
13301
+ const res = await connection.query(
13302
+ `select count(\`${tableName}\`.\`${removedColumn}\`) as count from \`${tableName}\``
13303
+ );
13304
+ const count2 = Number(res[0].count);
13305
+ if (count2 > 0) {
13306
+ infoToPrint.push(
13307
+ `\xB7 You're about to delete ${source_default.underline(
13308
+ removedColumn
13309
+ )} column in ${tableName} table with ${count2} items`
13310
+ );
13311
+ columnsToRemove.push(removedColumn);
13312
+ shouldAskForApprove = true;
13313
+ }
13314
+ }
13315
+ }
13316
+ if (addedColumns.length) {
13317
+ for (const addedColumn of addedColumns) {
13318
+ const [res] = await connection.query(
13319
+ `select count(*) as count from \`${tableName}\``
13320
+ );
13321
+ const columnConf = json2.tables[tableName].columns[addedColumn];
13322
+ const count2 = Number(res.count);
13323
+ if (count2 > 0 && columnConf.notNull && !columnConf.default) {
13324
+ dataLoss = true;
13325
+ infoToPrint.push(
13326
+ `\xB7 You're about to add not-null ${source_default.underline(
13327
+ addedColumn
13328
+ )} column without default value to table, which contains ${count2} items`
13329
+ );
13330
+ shouldAskForApprove = true;
13331
+ tablesToTruncate.push(tableName);
13332
+ statementsToExecute.push(`DELETE FROM \`${tableName}\`;`);
13333
+ }
13334
+ }
13335
+ }
13336
+ const tablesReferencingCurrent = [];
13337
+ for (const table4 of Object.values(json2.tables)) {
13338
+ const tablesRefs = Object.values(json2.tables[table4.name].foreignKeys).filter((t) => SQLiteSquasher.unsquashPushFK(t).tableTo === tableName).map((it) => SQLiteSquasher.unsquashPushFK(it).tableFrom);
13339
+ tablesReferencingCurrent.push(...tablesRefs);
13340
+ }
13341
+ if (!tablesReferencingCurrent.length) {
13342
+ statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
13343
+ continue;
13344
+ }
13345
+ const [{ foreign_keys: pragmaState }] = await connection.query(`PRAGMA foreign_keys;`);
13346
+ if (pragmaState) {
13347
+ statementsToExecute.push(`PRAGMA foreign_keys=OFF;`);
13348
+ }
13349
+ statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
13350
+ if (pragmaState) {
13351
+ statementsToExecute.push(`PRAGMA foreign_keys=ON;`);
13352
+ }
13353
+ } else {
13354
+ const fromJsonStatement = fromJson([statement], "sqlite", "push");
13355
+ statementsToExecute.push(
13356
+ ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13357
+ );
13358
+ }
13359
+ }
13360
+ return {
13361
+ statementsToExecute,
13362
+ shouldAskForApprove,
13363
+ infoToPrint,
13364
+ columnsToRemove: [...new Set(columnsToRemove)],
13365
+ schemasToRemove: [...new Set(schemasToRemove)],
13366
+ tablesToTruncate: [...new Set(tablesToTruncate)],
13367
+ tablesToRemove: [...new Set(tablesToRemove)]
13368
+ };
13369
+ };
13370
+ }
13371
+ });
13372
+
13232
13373
  // src/jsonStatements.ts
13233
- 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;
13374
+ 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;
13234
13375
  var init_jsonStatements = __esm({
13235
13376
  "src/jsonStatements.ts"() {
13236
13377
  "use strict";
13237
13378
  init_source();
13379
+ init_sqlitePushUtils();
13238
13380
  init_views();
13239
13381
  init_mysqlSchema();
13240
13382
  init_pgSchema();
@@ -14018,18 +14160,61 @@ var init_jsonStatements = __esm({
14018
14160
  const columnPk = json2.tables[tableName].columns[columnName].primaryKey;
14019
14161
  const columnGenerated = json2.tables[tableName].columns[columnName].generated;
14020
14162
  const compositePk = json2.tables[tableName].compositePrimaryKeys[`${tableName}_${columnName}`];
14021
- if (typeof column4.name !== "string") {
14163
+ if (column4.autoincrement?.type === "added") {
14022
14164
  statements.push({
14023
- type: "alter_table_rename_column",
14165
+ type: "alter_table_alter_column_set_autoincrement",
14024
14166
  tableName,
14025
- oldColumnName: column4.name.old,
14026
- newColumnName: column4.name.new,
14027
- schema: schema4
14028
- });
14029
- }
14030
- if (column4.type?.type === "changed") {
14031
- statements.push({
14032
- type: "alter_table_alter_column_set_type",
14167
+ columnName,
14168
+ schema: schema4,
14169
+ newDataType: columnType,
14170
+ columnDefault,
14171
+ columnOnUpdate,
14172
+ columnNotNull,
14173
+ columnAutoIncrement,
14174
+ columnPk
14175
+ });
14176
+ }
14177
+ if (column4.autoincrement?.type === "changed") {
14178
+ const type = column4.autoincrement.new ? "alter_table_alter_column_set_autoincrement" : "alter_table_alter_column_drop_autoincrement";
14179
+ statements.push({
14180
+ type,
14181
+ tableName,
14182
+ columnName,
14183
+ schema: schema4,
14184
+ newDataType: columnType,
14185
+ columnDefault,
14186
+ columnOnUpdate,
14187
+ columnNotNull,
14188
+ columnAutoIncrement,
14189
+ columnPk
14190
+ });
14191
+ }
14192
+ if (column4.autoincrement?.type === "deleted") {
14193
+ statements.push({
14194
+ type: "alter_table_alter_column_drop_autoincrement",
14195
+ tableName,
14196
+ columnName,
14197
+ schema: schema4,
14198
+ newDataType: columnType,
14199
+ columnDefault,
14200
+ columnOnUpdate,
14201
+ columnNotNull,
14202
+ columnAutoIncrement,
14203
+ columnPk
14204
+ });
14205
+ }
14206
+ if (typeof column4.name !== "string") {
14207
+ statements.push({
14208
+ type: "alter_table_rename_column",
14209
+ tableName,
14210
+ oldColumnName: column4.name.old,
14211
+ newColumnName: column4.name.new,
14212
+ schema: schema4
14213
+ });
14214
+ }
14215
+ if (column4.type?.type === "changed") {
14216
+ statements.push({
14217
+ type: "alter_table_alter_column_set_type",
14033
14218
  tableName,
14034
14219
  columnName,
14035
14220
  newDataType: column4.type.new,
@@ -14281,6 +14466,37 @@ var init_jsonStatements = __esm({
14281
14466
  };
14282
14467
  });
14283
14468
  };
14469
+ prepareLibSQLCreateReferencesJson = (tableName, schema4, foreignKeys, json2, action) => {
14470
+ return Object.values(foreignKeys).map((fkData) => {
14471
+ const { columnsFrom, tableFrom, columnsTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14472
+ let isMulticolumn = false;
14473
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
14474
+ isMulticolumn = true;
14475
+ return {
14476
+ type: "create_reference",
14477
+ tableName,
14478
+ data: fkData,
14479
+ schema: schema4,
14480
+ isMulticolumn
14481
+ };
14482
+ }
14483
+ const columnFrom = columnsFrom[0];
14484
+ const {
14485
+ notNull: columnNotNull,
14486
+ default: columnDefault,
14487
+ type: columnType
14488
+ } = json2.tables[tableFrom].columns[columnFrom];
14489
+ return {
14490
+ type: "create_reference",
14491
+ tableName,
14492
+ data: fkData,
14493
+ schema: schema4,
14494
+ columnNotNull,
14495
+ columnDefault,
14496
+ columnType
14497
+ };
14498
+ });
14499
+ };
14284
14500
  prepareDropReferencesJson = (tableName, schema4, foreignKeys) => {
14285
14501
  return Object.values(foreignKeys).map((fkData) => {
14286
14502
  return {
@@ -14291,6 +14507,53 @@ var init_jsonStatements = __esm({
14291
14507
  };
14292
14508
  });
14293
14509
  };
14510
+ prepareLibSQLDropReferencesJson = (tableName, schema4, foreignKeys, json2, meta, action) => {
14511
+ const statements = Object.values(foreignKeys).map((fkData) => {
14512
+ const { columnsFrom, tableFrom, columnsTo, name: name2, tableTo, onDelete, onUpdate } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14513
+ const keys = Object.keys(json2.tables[tableName].columns);
14514
+ const filtered = columnsFrom.filter((it) => keys.includes(it));
14515
+ const fullDrop = filtered.length === 0;
14516
+ if (fullDrop) return;
14517
+ let isMulticolumn = false;
14518
+ if (columnsFrom.length > 1 || columnsTo.length > 1) {
14519
+ isMulticolumn = true;
14520
+ return {
14521
+ type: "delete_reference",
14522
+ tableName,
14523
+ data: fkData,
14524
+ schema: schema4,
14525
+ isMulticolumn
14526
+ };
14527
+ }
14528
+ const columnFrom = columnsFrom[0];
14529
+ const newTableName = getNewTableName(tableFrom, meta);
14530
+ const {
14531
+ notNull: columnNotNull,
14532
+ default: columnDefault,
14533
+ type: columnType
14534
+ } = json2.tables[newTableName].columns[columnFrom];
14535
+ const fkToSquash = {
14536
+ columnsFrom,
14537
+ columnsTo,
14538
+ name: name2,
14539
+ tableFrom: newTableName,
14540
+ tableTo,
14541
+ onDelete,
14542
+ onUpdate
14543
+ };
14544
+ const foreignKey = action === "push" ? SQLiteSquasher.squashPushFK(fkToSquash) : SQLiteSquasher.squashFK(fkToSquash);
14545
+ return {
14546
+ type: "delete_reference",
14547
+ tableName,
14548
+ data: foreignKey,
14549
+ schema: schema4,
14550
+ columnNotNull,
14551
+ columnDefault,
14552
+ columnType
14553
+ };
14554
+ });
14555
+ return statements.filter((it) => it);
14556
+ };
14294
14557
  prepareAlterReferencesJson = (tableName, schema4, foreignKeys) => {
14295
14558
  const stmts = [];
14296
14559
  Object.values(foreignKeys).map((val) => {
@@ -14444,10 +14707,292 @@ var init_jsonStatements = __esm({
14444
14707
  }
14445
14708
  });
14446
14709
 
14710
+ // src/statementCombiner.ts
14711
+ var prepareLibSQLRecreateTable, prepareSQLiteRecreateTable, libSQLCombineStatements, sqliteCombineStatements;
14712
+ var init_statementCombiner = __esm({
14713
+ "src/statementCombiner.ts"() {
14714
+ "use strict";
14715
+ init_jsonStatements();
14716
+ init_sqliteSchema();
14717
+ prepareLibSQLRecreateTable = (table4, action) => {
14718
+ const { name: name2, columns, uniqueConstraints, indexes } = table4;
14719
+ const composites = Object.values(table4.compositePrimaryKeys).map(
14720
+ (it) => SQLiteSquasher.unsquashPK(it)
14721
+ );
14722
+ const references2 = Object.values(table4.foreignKeys);
14723
+ const fks = references2.map(
14724
+ (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14725
+ );
14726
+ const statements = [
14727
+ {
14728
+ type: "recreate_table",
14729
+ tableName: name2,
14730
+ columns: Object.values(columns),
14731
+ compositePKs: composites,
14732
+ referenceData: fks,
14733
+ uniqueConstraints: Object.values(uniqueConstraints)
14734
+ }
14735
+ ];
14736
+ if (Object.keys(indexes).length) {
14737
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
14738
+ }
14739
+ return statements;
14740
+ };
14741
+ prepareSQLiteRecreateTable = (table4, action) => {
14742
+ const { name: name2, columns, uniqueConstraints, indexes } = table4;
14743
+ const composites = Object.values(table4.compositePrimaryKeys).map(
14744
+ (it) => SQLiteSquasher.unsquashPK(it)
14745
+ );
14746
+ const references2 = Object.values(table4.foreignKeys);
14747
+ const fks = references2.map(
14748
+ (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14749
+ );
14750
+ const statements = [
14751
+ {
14752
+ type: "recreate_table",
14753
+ tableName: name2,
14754
+ columns: Object.values(columns),
14755
+ compositePKs: composites,
14756
+ referenceData: fks,
14757
+ uniqueConstraints: Object.values(uniqueConstraints)
14758
+ }
14759
+ ];
14760
+ if (Object.keys(indexes).length) {
14761
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
14762
+ }
14763
+ return statements;
14764
+ };
14765
+ libSQLCombineStatements = (statements, json2, action) => {
14766
+ const newStatements = {};
14767
+ for (const statement of statements) {
14768
+ 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") {
14769
+ const tableName2 = statement.tableName;
14770
+ const statementsForTable2 = newStatements[tableName2];
14771
+ if (!statementsForTable2) {
14772
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14773
+ continue;
14774
+ }
14775
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14776
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14777
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14778
+ if (wasRename) {
14779
+ newStatements[tableName2].push(...preparedStatements);
14780
+ } else {
14781
+ newStatements[tableName2] = preparedStatements;
14782
+ }
14783
+ continue;
14784
+ }
14785
+ continue;
14786
+ }
14787
+ 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") {
14788
+ const { tableName: tableName2, columnName, columnPk } = statement;
14789
+ const columnIsPartOfForeignKey = Object.values(
14790
+ json2.tables[tableName2].foreignKeys
14791
+ ).some((it) => {
14792
+ const unsquashFk = action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it);
14793
+ return unsquashFk.columnsFrom.includes(columnName);
14794
+ });
14795
+ const statementsForTable2 = newStatements[tableName2];
14796
+ if (!statementsForTable2 && (columnIsPartOfForeignKey || columnPk)) {
14797
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14798
+ continue;
14799
+ }
14800
+ if (statementsForTable2 && (columnIsPartOfForeignKey || columnPk)) {
14801
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14802
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14803
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14804
+ if (wasRename) {
14805
+ newStatements[tableName2].push(...preparedStatements);
14806
+ } else {
14807
+ newStatements[tableName2] = preparedStatements;
14808
+ }
14809
+ }
14810
+ continue;
14811
+ }
14812
+ if (statementsForTable2 && !(columnIsPartOfForeignKey || columnPk)) {
14813
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14814
+ newStatements[tableName2].push(statement);
14815
+ }
14816
+ continue;
14817
+ }
14818
+ newStatements[tableName2] = [statement];
14819
+ continue;
14820
+ }
14821
+ if (statement.type === "create_reference") {
14822
+ const tableName2 = statement.tableName;
14823
+ const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
14824
+ const statementsForTable2 = newStatements[tableName2];
14825
+ if (!statementsForTable2) {
14826
+ newStatements[tableName2] = statement.isMulticolumn ? prepareLibSQLRecreateTable(json2.tables[tableName2], action) : [statement];
14827
+ continue;
14828
+ }
14829
+ if (!statement.isMulticolumn && statementsForTable2.some(
14830
+ (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
14831
+ )) {
14832
+ continue;
14833
+ }
14834
+ if (statement.isMulticolumn) {
14835
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14836
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14837
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14838
+ if (wasRename) {
14839
+ newStatements[tableName2].push(...preparedStatements);
14840
+ } else {
14841
+ newStatements[tableName2] = preparedStatements;
14842
+ }
14843
+ continue;
14844
+ }
14845
+ continue;
14846
+ }
14847
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14848
+ newStatements[tableName2].push(statement);
14849
+ }
14850
+ continue;
14851
+ }
14852
+ if (statement.type === "delete_reference") {
14853
+ const tableName2 = statement.tableName;
14854
+ const statementsForTable2 = newStatements[tableName2];
14855
+ if (!statementsForTable2) {
14856
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14857
+ continue;
14858
+ }
14859
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14860
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14861
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14862
+ if (wasRename) {
14863
+ newStatements[tableName2].push(...preparedStatements);
14864
+ } else {
14865
+ newStatements[tableName2] = preparedStatements;
14866
+ }
14867
+ continue;
14868
+ }
14869
+ continue;
14870
+ }
14871
+ if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
14872
+ const tableName2 = statement.tableName;
14873
+ const statementsForTable2 = newStatements[tableName2];
14874
+ if (!statementsForTable2) {
14875
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14876
+ continue;
14877
+ }
14878
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14879
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14880
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14881
+ if (wasRename) {
14882
+ newStatements[tableName2].push(...preparedStatements);
14883
+ } else {
14884
+ newStatements[tableName2] = preparedStatements;
14885
+ }
14886
+ continue;
14887
+ }
14888
+ continue;
14889
+ }
14890
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
14891
+ const statementsForTable = newStatements[tableName];
14892
+ if (!statementsForTable) {
14893
+ newStatements[tableName] = [statement];
14894
+ continue;
14895
+ }
14896
+ if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
14897
+ newStatements[tableName].push(statement);
14898
+ }
14899
+ }
14900
+ const combinedStatements = Object.values(newStatements).flat();
14901
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
14902
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
14903
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
14904
+ return [...renamedTables, ...renamedColumns, ...rest];
14905
+ };
14906
+ sqliteCombineStatements = (statements, json2, action) => {
14907
+ const newStatements = {};
14908
+ for (const statement of statements) {
14909
+ 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") {
14910
+ const tableName2 = statement.tableName;
14911
+ const statementsForTable2 = newStatements[tableName2];
14912
+ if (!statementsForTable2) {
14913
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14914
+ continue;
14915
+ }
14916
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14917
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14918
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14919
+ if (wasRename) {
14920
+ newStatements[tableName2].push(...preparedStatements);
14921
+ } else {
14922
+ newStatements[tableName2] = preparedStatements;
14923
+ }
14924
+ continue;
14925
+ }
14926
+ continue;
14927
+ }
14928
+ if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
14929
+ const tableName2 = statement.tableName;
14930
+ const statementsForTable2 = newStatements[tableName2];
14931
+ if (!statementsForTable2) {
14932
+ newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14933
+ continue;
14934
+ }
14935
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14936
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14937
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14938
+ if (wasRename) {
14939
+ newStatements[tableName2].push(...preparedStatements);
14940
+ } else {
14941
+ newStatements[tableName2] = preparedStatements;
14942
+ }
14943
+ continue;
14944
+ }
14945
+ continue;
14946
+ }
14947
+ if (statement.type === "create_reference") {
14948
+ const tableName2 = statement.tableName;
14949
+ const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
14950
+ const statementsForTable2 = newStatements[tableName2];
14951
+ if (!statementsForTable2) {
14952
+ newStatements[tableName2] = prepareSQLiteRecreateTable(json2.tables[tableName2], action);
14953
+ continue;
14954
+ }
14955
+ if (data.columnsFrom.length === 1 && statementsForTable2.some(
14956
+ (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
14957
+ )) {
14958
+ continue;
14959
+ }
14960
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14961
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14962
+ const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14963
+ if (wasRename) {
14964
+ newStatements[tableName2].push(...preparedStatements);
14965
+ } else {
14966
+ newStatements[tableName2] = preparedStatements;
14967
+ }
14968
+ continue;
14969
+ }
14970
+ continue;
14971
+ }
14972
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
14973
+ const statementsForTable = newStatements[tableName];
14974
+ if (!statementsForTable) {
14975
+ newStatements[tableName] = [statement];
14976
+ continue;
14977
+ }
14978
+ if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
14979
+ newStatements[tableName].push(statement);
14980
+ }
14981
+ }
14982
+ const combinedStatements = Object.values(newStatements).flat();
14983
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
14984
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
14985
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
14986
+ return [...renamedTables, ...renamedColumns, ...rest];
14987
+ };
14988
+ }
14989
+ });
14990
+
14447
14991
  // src/snapshotsDiffer.ts
14448
14992
  var snapshotsDiffer_exports = {};
14449
14993
  __export(snapshotsDiffer_exports, {
14450
14994
  alteredTableScheme: () => alteredTableScheme,
14995
+ applyLibSQLSnapshotsDiff: () => applyLibSQLSnapshotsDiff,
14451
14996
  applyMysqlSnapshotsDiff: () => applyMysqlSnapshotsDiff,
14452
14997
  applyPgSnapshotsDiff: () => applyPgSnapshotsDiff,
14453
14998
  applySqliteSnapshotsDiff: () => applySqliteSnapshotsDiff,
@@ -14457,7 +15002,7 @@ __export(snapshotsDiffer_exports, {
14457
15002
  makePatched: () => makePatched,
14458
15003
  makeSelfOrPatched: () => makeSelfOrPatched
14459
15004
  });
14460
- var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff;
15005
+ var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff, applyLibSQLSnapshotsDiff;
14461
15006
  var init_snapshotsDiffer = __esm({
14462
15007
  "src/snapshotsDiffer.ts"() {
14463
15008
  "use strict";
@@ -14469,6 +15014,7 @@ var init_snapshotsDiffer = __esm({
14469
15014
  init_mysqlSchema();
14470
15015
  init_pgSchema();
14471
15016
  init_sqliteSchema();
15017
+ init_statementCombiner();
14472
15018
  init_utils();
14473
15019
  makeChanged = (schema4) => {
14474
15020
  return objectType({
@@ -15355,40 +15901,339 @@ var init_snapshotsDiffer = __esm({
15355
15901
  const jsonAddedUniqueConstraints = [];
15356
15902
  const jsonDeletedUniqueConstraints = [];
15357
15903
  const jsonAlteredUniqueConstraints = [];
15358
- const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
15359
- const jsonAddColumnsStatemets = columnCreates.map((it) => _prepareAddColumns(it.table, "", it.columns)).flat();
15360
- const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
15361
- alteredTables.forEach((it) => {
15904
+ const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
15905
+ const jsonAddColumnsStatemets = columnCreates.map((it) => _prepareAddColumns(it.table, "", it.columns)).flat();
15906
+ const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
15907
+ alteredTables.forEach((it) => {
15908
+ let addedColumns = [];
15909
+ for (const addedPkName of Object.keys(it.addedCompositePKs)) {
15910
+ const addedPkColumns = it.addedCompositePKs[addedPkName];
15911
+ addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
15912
+ }
15913
+ let deletedColumns = [];
15914
+ for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
15915
+ const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
15916
+ deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
15917
+ }
15918
+ const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
15919
+ let addedCompositePKs = [];
15920
+ let deletedCompositePKs = [];
15921
+ let alteredCompositePKs = [];
15922
+ addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
15923
+ it.name,
15924
+ it.addedCompositePKs,
15925
+ prevFull,
15926
+ curFull
15927
+ );
15928
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15929
+ it.name,
15930
+ it.deletedCompositePKs,
15931
+ prevFull
15932
+ );
15933
+ alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
15934
+ it.name,
15935
+ it.alteredCompositePKs,
15936
+ prevFull,
15937
+ curFull
15938
+ );
15939
+ let addedUniqueConstraints = [];
15940
+ let deletedUniqueConstraints = [];
15941
+ let alteredUniqueConstraints = [];
15942
+ addedUniqueConstraints = prepareAddUniqueConstraintPg(
15943
+ it.name,
15944
+ it.schema,
15945
+ it.addedUniqueConstraints
15946
+ );
15947
+ deletedUniqueConstraints = prepareDeleteUniqueConstraintPg(
15948
+ it.name,
15949
+ it.schema,
15950
+ it.deletedUniqueConstraints
15951
+ );
15952
+ if (it.alteredUniqueConstraints) {
15953
+ const added = {};
15954
+ const deleted = {};
15955
+ for (const k of Object.keys(it.alteredUniqueConstraints)) {
15956
+ added[k] = it.alteredUniqueConstraints[k].__new;
15957
+ deleted[k] = it.alteredUniqueConstraints[k].__old;
15958
+ }
15959
+ addedUniqueConstraints.push(
15960
+ ...prepareAddUniqueConstraintPg(it.name, it.schema, added)
15961
+ );
15962
+ deletedUniqueConstraints.push(
15963
+ ...prepareDeleteUniqueConstraintPg(it.name, it.schema, deleted)
15964
+ );
15965
+ }
15966
+ jsonAddedCompositePKs.push(...addedCompositePKs);
15967
+ jsonDeletedCompositePKs.push(...deletedCompositePKs);
15968
+ jsonAlteredCompositePKs.push(...alteredCompositePKs);
15969
+ jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
15970
+ jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15971
+ jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15972
+ });
15973
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
15974
+ const tableName = it.tableName;
15975
+ const schema4 = it.schema;
15976
+ return {
15977
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
15978
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
15979
+ };
15980
+ });
15981
+ const jsonTableAlternations = alteredTables.map((it) => {
15982
+ return prepareAlterColumnsMysql(
15983
+ it.name,
15984
+ it.schema,
15985
+ it.altered,
15986
+ json1,
15987
+ json2,
15988
+ action
15989
+ );
15990
+ }).flat();
15991
+ const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
15992
+ return prepareCreateIndexesJson(
15993
+ it.name,
15994
+ it.schema,
15995
+ it.addedIndexes || {},
15996
+ curFull.internal
15997
+ );
15998
+ }).flat();
15999
+ const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16000
+ return prepareDropIndexesJson(
16001
+ it.name,
16002
+ it.schema,
16003
+ it.deletedIndexes || {}
16004
+ );
16005
+ }).flat();
16006
+ alteredTables.forEach((it) => {
16007
+ const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
16008
+ (current, item) => {
16009
+ current[item] = it.alteredIndexes[item].__old;
16010
+ return current;
16011
+ },
16012
+ {}
16013
+ );
16014
+ const createdIndexes = Object.keys(it.alteredIndexes).reduce(
16015
+ (current, item) => {
16016
+ current[item] = it.alteredIndexes[item].__new;
16017
+ return current;
16018
+ },
16019
+ {}
16020
+ );
16021
+ jsonCreateIndexesForAllAlteredTables.push(
16022
+ ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16023
+ );
16024
+ jsonDropIndexesForAllAlteredTables.push(
16025
+ ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
16026
+ );
16027
+ });
16028
+ const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
16029
+ return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
16030
+ }).flat();
16031
+ const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16032
+ const forAdded = prepareCreateReferencesJson(
16033
+ it.name,
16034
+ it.schema,
16035
+ it.addedForeignKeys
16036
+ );
16037
+ const forAltered = prepareDropReferencesJson(
16038
+ it.name,
16039
+ it.schema,
16040
+ it.deletedForeignKeys
16041
+ );
16042
+ const alteredFKs = prepareAlterReferencesJson(
16043
+ it.name,
16044
+ it.schema,
16045
+ it.alteredForeignKeys
16046
+ );
16047
+ return [...forAdded, ...forAltered, ...alteredFKs];
16048
+ }).flat();
16049
+ const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16050
+ (t) => t.type === "create_reference"
16051
+ );
16052
+ const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16053
+ (t) => t.type === "delete_reference"
16054
+ );
16055
+ const jsonMySqlCreateTables = createdTables.map((it) => {
16056
+ return prepareMySqlCreateTableJson(
16057
+ it,
16058
+ curFull,
16059
+ curFull.internal
16060
+ );
16061
+ });
16062
+ jsonStatements.push(...jsonMySqlCreateTables);
16063
+ jsonStatements.push(...jsonDropTables);
16064
+ jsonStatements.push(...jsonRenameTables);
16065
+ jsonStatements.push(...jsonRenameColumnsStatements);
16066
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16067
+ jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
16068
+ jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
16069
+ jsonStatements.push(...jsonDeletedCompositePKs);
16070
+ jsonStatements.push(...jsonTableAlternations);
16071
+ jsonStatements.push(...jsonAddedCompositePKs);
16072
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16073
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16074
+ jsonStatements.push(...jsonAddColumnsStatemets);
16075
+ jsonStatements.push(...jsonCreateReferencesForCreatedTables);
16076
+ jsonStatements.push(...jsonCreateIndexesForCreatedTables);
16077
+ jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
16078
+ jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16079
+ jsonStatements.push(...jsonDropColumnsStatemets);
16080
+ jsonStatements.push(...jsonAlteredCompositePKs);
16081
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16082
+ jsonStatements.push(...jsonAlteredUniqueConstraints);
16083
+ const sqlStatements = fromJson(jsonStatements, "mysql");
16084
+ const uniqueSqlStatements = [];
16085
+ sqlStatements.forEach((ss) => {
16086
+ if (!uniqueSqlStatements.includes(ss)) {
16087
+ uniqueSqlStatements.push(ss);
16088
+ }
16089
+ });
16090
+ const rTables = renamedTables.map((it) => {
16091
+ return { from: it.from, to: it.to };
16092
+ });
16093
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
16094
+ return {
16095
+ statements: jsonStatements,
16096
+ sqlStatements: uniqueSqlStatements,
16097
+ _meta
16098
+ };
16099
+ };
16100
+ applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16101
+ const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
16102
+ const {
16103
+ created: createdTables,
16104
+ deleted: deletedTables,
16105
+ renamed: renamedTables
16106
+ } = await tablesResolver2({
16107
+ created: tablesDiff.added,
16108
+ deleted: tablesDiff.deleted
16109
+ });
16110
+ const tablesPatchedSnap1 = copy(json1);
16111
+ tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_2, it) => {
16112
+ const { name: name2 } = nameChangeFor(it, renamedTables);
16113
+ it.name = name2;
16114
+ return [name2, it];
16115
+ });
16116
+ const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
16117
+ const columnRenames = [];
16118
+ const columnCreates = [];
16119
+ const columnDeletes = [];
16120
+ for (let entry of Object.values(res)) {
16121
+ const { renamed, created, deleted } = await columnsResolver2({
16122
+ tableName: entry.name,
16123
+ schema: entry.schema,
16124
+ deleted: entry.columns.deleted,
16125
+ created: entry.columns.added
16126
+ });
16127
+ if (created.length > 0) {
16128
+ columnCreates.push({
16129
+ table: entry.name,
16130
+ columns: created
16131
+ });
16132
+ }
16133
+ if (deleted.length > 0) {
16134
+ columnDeletes.push({
16135
+ table: entry.name,
16136
+ columns: deleted
16137
+ });
16138
+ }
16139
+ if (renamed.length > 0) {
16140
+ columnRenames.push({
16141
+ table: entry.name,
16142
+ renames: renamed
16143
+ });
16144
+ }
16145
+ }
16146
+ const columnRenamesDict = columnRenames.reduce(
16147
+ (acc, it) => {
16148
+ acc[it.table] = it.renames;
16149
+ return acc;
16150
+ },
16151
+ {}
16152
+ );
16153
+ const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
16154
+ columnsPatchedSnap1.tables = mapEntries(
16155
+ columnsPatchedSnap1.tables,
16156
+ (tableKey2, tableValue) => {
16157
+ const patchedColumns = mapKeys(
16158
+ tableValue.columns,
16159
+ (columnKey, column4) => {
16160
+ const rens = columnRenamesDict[tableValue.name] || [];
16161
+ const newName = columnChangeFor(columnKey, rens);
16162
+ column4.name = newName;
16163
+ return newName;
16164
+ }
16165
+ );
16166
+ tableValue.columns = patchedColumns;
16167
+ return [tableKey2, tableValue];
16168
+ }
16169
+ );
16170
+ const diffResult = applyJsonDiff(columnsPatchedSnap1, json2);
16171
+ const typedResult = diffResultSchemeSQLite.parse(diffResult);
16172
+ const tablesMap = {};
16173
+ typedResult.alteredTablesWithColumns.forEach((obj) => {
16174
+ tablesMap[obj.name] = obj;
16175
+ });
16176
+ const jsonCreateTables = createdTables.map((it) => {
16177
+ return prepareSQLiteCreateTable(it, action);
16178
+ });
16179
+ const jsonCreateIndexesForCreatedTables = createdTables.map((it) => {
16180
+ return prepareCreateIndexesJson(
16181
+ it.name,
16182
+ it.schema,
16183
+ it.indexes,
16184
+ curFull.internal
16185
+ );
16186
+ }).flat();
16187
+ const jsonDropTables = deletedTables.map((it) => {
16188
+ return prepareDropTableJson(it);
16189
+ });
16190
+ const jsonRenameTables = renamedTables.map((it) => {
16191
+ return prepareRenameTableJson(it.from, it.to);
16192
+ });
16193
+ const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
16194
+ const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
16195
+ const jsonAddColumnsStatemets = columnCreates.map((it) => {
16196
+ return _prepareSqliteAddColumns(
16197
+ it.table,
16198
+ it.columns,
16199
+ tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
16200
+ );
16201
+ }).flat();
16202
+ const allAltered = typedResult.alteredTablesWithColumns;
16203
+ const jsonAddedCompositePKs = [];
16204
+ const jsonDeletedCompositePKs = [];
16205
+ const jsonAlteredCompositePKs = [];
16206
+ const jsonAddedUniqueConstraints = [];
16207
+ const jsonDeletedUniqueConstraints = [];
16208
+ const jsonAlteredUniqueConstraints = [];
16209
+ allAltered.forEach((it) => {
15362
16210
  let addedColumns = [];
15363
16211
  for (const addedPkName of Object.keys(it.addedCompositePKs)) {
15364
16212
  const addedPkColumns = it.addedCompositePKs[addedPkName];
15365
- addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
16213
+ addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
15366
16214
  }
15367
16215
  let deletedColumns = [];
15368
16216
  for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
15369
16217
  const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
15370
- deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
16218
+ deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
15371
16219
  }
15372
16220
  const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
15373
16221
  let addedCompositePKs = [];
15374
16222
  let deletedCompositePKs = [];
15375
16223
  let alteredCompositePKs = [];
15376
- addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
15377
- it.name,
15378
- it.addedCompositePKs,
15379
- prevFull,
15380
- curFull
15381
- );
15382
- deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15383
- it.name,
15384
- it.deletedCompositePKs,
15385
- prevFull
15386
- );
15387
- alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
16224
+ if (doPerformDeleteAndCreate) {
16225
+ addedCompositePKs = prepareAddCompositePrimaryKeySqlite(
16226
+ it.name,
16227
+ it.addedCompositePKs
16228
+ );
16229
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeySqlite(
16230
+ it.name,
16231
+ it.deletedCompositePKs
16232
+ );
16233
+ }
16234
+ alteredCompositePKs = prepareAlterCompositePrimaryKeySqlite(
15388
16235
  it.name,
15389
- it.alteredCompositePKs,
15390
- prevFull,
15391
- curFull
16236
+ it.alteredCompositePKs
15392
16237
  );
15393
16238
  let addedUniqueConstraints = [];
15394
16239
  let deletedUniqueConstraints = [];
@@ -15432,17 +16277,10 @@ var init_snapshotsDiffer = __esm({
15432
16277
  to: { schema: schema4, table: tableName, column: it.newColumnName }
15433
16278
  };
15434
16279
  });
15435
- const jsonTableAlternations = alteredTables.map((it) => {
15436
- return prepareAlterColumnsMysql(
15437
- it.name,
15438
- it.schema,
15439
- it.altered,
15440
- json1,
15441
- json2,
15442
- action
15443
- );
16280
+ const jsonTableAlternations = allAltered.map((it) => {
16281
+ return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15444
16282
  }).flat();
15445
- const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
16283
+ const jsonCreateIndexesForAllAlteredTables = allAltered.map((it) => {
15446
16284
  return prepareCreateIndexesJson(
15447
16285
  it.name,
15448
16286
  it.schema,
@@ -15450,14 +16288,14 @@ var init_snapshotsDiffer = __esm({
15450
16288
  curFull.internal
15451
16289
  );
15452
16290
  }).flat();
15453
- const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16291
+ const jsonDropIndexesForAllAlteredTables = allAltered.map((it) => {
15454
16292
  return prepareDropIndexesJson(
15455
16293
  it.name,
15456
16294
  it.schema,
15457
16295
  it.deletedIndexes || {}
15458
16296
  );
15459
16297
  }).flat();
15460
- alteredTables.forEach((it) => {
16298
+ allAltered.forEach((it) => {
15461
16299
  const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
15462
16300
  (current, item) => {
15463
16301
  current[item] = it.alteredIndexes[item].__old;
@@ -15473,16 +16311,18 @@ var init_snapshotsDiffer = __esm({
15473
16311
  {}
15474
16312
  );
15475
16313
  jsonCreateIndexesForAllAlteredTables.push(
15476
- ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16314
+ ...prepareCreateIndexesJson(
16315
+ it.name,
16316
+ it.schema,
16317
+ createdIndexes || {},
16318
+ curFull.internal
16319
+ )
15477
16320
  );
15478
16321
  jsonDropIndexesForAllAlteredTables.push(
15479
16322
  ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
15480
16323
  );
15481
16324
  });
15482
- const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
15483
- return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
15484
- }).flat();
15485
- const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16325
+ const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15486
16326
  const forAdded = prepareCreateReferencesJson(
15487
16327
  it.name,
15488
16328
  it.schema,
@@ -15506,35 +16346,25 @@ var init_snapshotsDiffer = __esm({
15506
16346
  const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
15507
16347
  (t) => t.type === "delete_reference"
15508
16348
  );
15509
- const jsonMySqlCreateTables = createdTables.map((it) => {
15510
- return prepareMySqlCreateTableJson(
15511
- it,
15512
- curFull,
15513
- curFull.internal
15514
- );
15515
- });
15516
- jsonStatements.push(...jsonMySqlCreateTables);
16349
+ const jsonStatements = [];
16350
+ jsonStatements.push(...jsonCreateTables);
15517
16351
  jsonStatements.push(...jsonDropTables);
15518
16352
  jsonStatements.push(...jsonRenameTables);
15519
16353
  jsonStatements.push(...jsonRenameColumnsStatements);
15520
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15521
16354
  jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
15522
16355
  jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
15523
16356
  jsonStatements.push(...jsonDeletedCompositePKs);
15524
16357
  jsonStatements.push(...jsonTableAlternations);
15525
16358
  jsonStatements.push(...jsonAddedCompositePKs);
15526
- jsonStatements.push(...jsonAddedUniqueConstraints);
15527
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15528
16359
  jsonStatements.push(...jsonAddColumnsStatemets);
15529
- jsonStatements.push(...jsonCreateReferencesForCreatedTables);
15530
16360
  jsonStatements.push(...jsonCreateIndexesForCreatedTables);
15531
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15532
16361
  jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16362
+ jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15533
16363
  jsonStatements.push(...jsonDropColumnsStatemets);
15534
16364
  jsonStatements.push(...jsonAlteredCompositePKs);
15535
- jsonStatements.push(...jsonAddedUniqueConstraints);
15536
16365
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15537
- const sqlStatements = fromJson(jsonStatements, "mysql");
16366
+ const combinedJsonStatements = sqliteCombineStatements(jsonStatements, json2, action);
16367
+ const sqlStatements = fromJson(combinedJsonStatements, "sqlite");
15538
16368
  const uniqueSqlStatements = [];
15539
16369
  sqlStatements.forEach((ss) => {
15540
16370
  if (!uniqueSqlStatements.includes(ss)) {
@@ -15546,12 +16376,12 @@ var init_snapshotsDiffer = __esm({
15546
16376
  });
15547
16377
  const _meta = prepareMigrationMeta([], rTables, rColumns);
15548
16378
  return {
15549
- statements: jsonStatements,
16379
+ statements: combinedJsonStatements,
15550
16380
  sqlStatements: uniqueSqlStatements,
15551
16381
  _meta
15552
16382
  };
15553
16383
  };
15554
- applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16384
+ applyLibSQLSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
15555
16385
  const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
15556
16386
  const {
15557
16387
  created: createdTables,
@@ -15653,6 +16483,18 @@ var init_snapshotsDiffer = __esm({
15653
16483
  tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
15654
16484
  );
15655
16485
  }).flat();
16486
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
16487
+ const tableName = it.tableName;
16488
+ const schema4 = it.schema;
16489
+ return {
16490
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
16491
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
16492
+ };
16493
+ });
16494
+ const rTables = renamedTables.map((it) => {
16495
+ return { from: it.from, to: it.to };
16496
+ });
16497
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
15656
16498
  const allAltered = typedResult.alteredTablesWithColumns;
15657
16499
  const jsonAddedCompositePKs = [];
15658
16500
  const jsonDeletedCompositePKs = [];
@@ -15723,14 +16565,6 @@ var init_snapshotsDiffer = __esm({
15723
16565
  jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15724
16566
  jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15725
16567
  });
15726
- const rColumns = jsonRenameColumnsStatements.map((it) => {
15727
- const tableName = it.tableName;
15728
- const schema4 = it.schema;
15729
- return {
15730
- from: { schema: schema4, table: tableName, column: it.oldColumnName },
15731
- to: { schema: schema4, table: tableName, column: it.newColumnName }
15732
- };
15733
- });
15734
16568
  const jsonTableAlternations = allAltered.map((it) => {
15735
16569
  return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15736
16570
  }).flat();
@@ -15777,21 +16611,22 @@ var init_snapshotsDiffer = __esm({
15777
16611
  );
15778
16612
  });
15779
16613
  const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15780
- const forAdded = prepareCreateReferencesJson(
15781
- it.name,
15782
- it.schema,
15783
- it.addedForeignKeys
15784
- );
15785
- const forAltered = prepareDropReferencesJson(
16614
+ const forAdded = prepareLibSQLCreateReferencesJson(
15786
16615
  it.name,
15787
16616
  it.schema,
15788
- it.deletedForeignKeys
16617
+ it.addedForeignKeys,
16618
+ json2,
16619
+ action
15789
16620
  );
15790
- const alteredFKs = prepareAlterReferencesJson(
16621
+ const forAltered = prepareLibSQLDropReferencesJson(
15791
16622
  it.name,
15792
16623
  it.schema,
15793
- it.alteredForeignKeys
16624
+ it.deletedForeignKeys,
16625
+ json2,
16626
+ _meta,
16627
+ action
15794
16628
  );
16629
+ const alteredFKs = prepareAlterReferencesJson(it.name, it.schema, it.alteredForeignKeys);
15795
16630
  return [...forAdded, ...forAltered, ...alteredFKs];
15796
16631
  }).flat();
15797
16632
  const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
@@ -15817,19 +16652,21 @@ var init_snapshotsDiffer = __esm({
15817
16652
  jsonStatements.push(...jsonDropColumnsStatemets);
15818
16653
  jsonStatements.push(...jsonAlteredCompositePKs);
15819
16654
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15820
- const sqlStatements = fromJson(jsonStatements, "sqlite");
16655
+ const combinedJsonStatements = libSQLCombineStatements(jsonStatements, json2, action);
16656
+ const sqlStatements = fromJson(
16657
+ combinedJsonStatements,
16658
+ "turso",
16659
+ action,
16660
+ json2
16661
+ );
15821
16662
  const uniqueSqlStatements = [];
15822
16663
  sqlStatements.forEach((ss) => {
15823
16664
  if (!uniqueSqlStatements.includes(ss)) {
15824
16665
  uniqueSqlStatements.push(ss);
15825
16666
  }
15826
16667
  });
15827
- const rTables = renamedTables.map((it) => {
15828
- return { from: it.from, to: it.to };
15829
- });
15830
- const _meta = prepareMigrationMeta([], rTables, rColumns);
15831
16668
  return {
15832
- statements: jsonStatements,
16669
+ statements: combinedJsonStatements,
15833
16670
  sqlStatements: uniqueSqlStatements,
15834
16671
  _meta
15835
16672
  };
@@ -15853,7 +16690,7 @@ var init_schemaValidator = __esm({
15853
16690
  init_mysqlSchema();
15854
16691
  init_pgSchema();
15855
16692
  init_sqliteSchema();
15856
- dialects = ["postgresql", "mysql", "sqlite"];
16693
+ dialects = ["postgresql", "mysql", "sqlite", "turso"];
15857
16694
  dialect3 = enumType(dialects);
15858
16695
  commonSquashedSchema = unionType([
15859
16696
  pgSchemaSquashed,
@@ -15873,7 +16710,6 @@ var init_common = __esm({
15873
16710
  init_schemaValidator();
15874
16711
  init_outputs();
15875
16712
  sqliteDriversLiterals = [
15876
- literalType("turso"),
15877
16713
  literalType("d1-http"),
15878
16714
  literalType("expo")
15879
16715
  ];
@@ -33955,6 +34791,21 @@ var init_cli = __esm({
33955
34791
  }
33956
34792
  });
33957
34793
 
34794
+ // src/cli/validations/libsql.ts
34795
+ var libSQLCredentials;
34796
+ var init_libsql = __esm({
34797
+ "src/cli/validations/libsql.ts"() {
34798
+ "use strict";
34799
+ init_lib();
34800
+ init_views();
34801
+ init_common();
34802
+ libSQLCredentials = objectType({
34803
+ url: stringType().min(1),
34804
+ authToken: stringType().min(1).optional()
34805
+ });
34806
+ }
34807
+ });
34808
+
33958
34809
  // src/cli/validations/mysql.ts
33959
34810
  var mysqlCredentials;
33960
34811
  var init_mysql = __esm({
@@ -34126,6 +34977,8 @@ var init_utils8 = __esm({
34126
34977
  init_serializer();
34127
34978
  init_cli();
34128
34979
  init_common();
34980
+ init_libsql();
34981
+ init_libsql();
34129
34982
  init_mysql();
34130
34983
  init_outputs();
34131
34984
  init_postgres();
@@ -35097,284 +35950,8 @@ var sqlitePushIntrospect = async (db, filters) => {
35097
35950
  return { schema: schema4 };
35098
35951
  };
35099
35952
 
35100
- // src/cli/commands/sqlitePushUtils.ts
35101
- init_source();
35102
- init_sqliteSchema();
35103
- init_sqlgenerator();
35104
- var _moveDataStatements = (tableName, json, dataLoss = false) => {
35105
- const statements = [];
35106
- statements.push(
35107
- new SqliteRenameTableConvertor().convert({
35108
- type: "rename_table",
35109
- tableNameFrom: tableName,
35110
- tableNameTo: `__old_push_${tableName}`,
35111
- fromSchema: "",
35112
- toSchema: ""
35113
- })
35114
- );
35115
- const tableColumns = Object.values(json.tables[tableName].columns);
35116
- const referenceData = Object.values(json.tables[tableName].foreignKeys);
35117
- const compositePKs = Object.values(
35118
- json.tables[tableName].compositePrimaryKeys
35119
- ).map((it) => SQLiteSquasher.unsquashPK(it));
35120
- const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
35121
- statements.push(
35122
- new SQLiteCreateTableConvertor().convert({
35123
- type: "sqlite_create_table",
35124
- tableName,
35125
- columns: tableColumns,
35126
- referenceData: fks,
35127
- compositePKs
35128
- })
35129
- );
35130
- if (!dataLoss) {
35131
- statements.push(
35132
- `INSERT INTO "${tableName}" SELECT * FROM "__old_push_${tableName}";`
35133
- );
35134
- }
35135
- statements.push(
35136
- new SQLiteDropTableConvertor().convert({
35137
- type: "drop_table",
35138
- tableName: `__old_push_${tableName}`,
35139
- schema: ""
35140
- })
35141
- );
35142
- for (const idx of Object.values(json.tables[tableName].indexes)) {
35143
- statements.push(
35144
- new CreateSqliteIndexConvertor().convert({
35145
- type: "create_index",
35146
- tableName,
35147
- schema: "",
35148
- data: idx
35149
- })
35150
- );
35151
- }
35152
- return statements;
35153
- };
35154
- var getOldTableName = (tableName, meta) => {
35155
- for (const key of Object.keys(meta.tables)) {
35156
- const value = meta.tables[key];
35157
- if (`"${tableName}"` === value) {
35158
- return key.substring(1, key.length - 1);
35159
- }
35160
- }
35161
- return tableName;
35162
- };
35163
- var getNewTableName = (tableName, meta) => {
35164
- if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
35165
- return meta.tables[`"${tableName}"`].substring(
35166
- 1,
35167
- meta.tables[`"${tableName}"`].length - 1
35168
- );
35169
- }
35170
- return tableName;
35171
- };
35172
- var logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
35173
- let shouldAskForApprove = false;
35174
- const statementsToExecute = [];
35175
- const infoToPrint = [];
35176
- const tablesToRemove = [];
35177
- const columnsToRemove = [];
35178
- const schemasToRemove = [];
35179
- const tablesToTruncate = [];
35180
- const tablesContext = {};
35181
- for (const statement of statements) {
35182
- if (statement.type === "drop_table") {
35183
- const res = await connection.query(
35184
- `select count(*) as count from \`${statement.tableName}\``
35185
- );
35186
- const count2 = Number(res[0].count);
35187
- if (count2 > 0) {
35188
- infoToPrint.push(
35189
- `\xB7 You're about to delete ${source_default.underline(
35190
- statement.tableName
35191
- )} table with ${count2} items`
35192
- );
35193
- tablesToRemove.push(statement.tableName);
35194
- shouldAskForApprove = true;
35195
- }
35196
- const stmnt = fromJson([statement], "sqlite")[0];
35197
- statementsToExecute.push(stmnt);
35198
- } else if (statement.type === "alter_table_drop_column") {
35199
- const newTableName = getOldTableName(statement.tableName, meta);
35200
- const columnIsPartOfPk = Object.values(
35201
- json1.tables[newTableName].compositePrimaryKeys
35202
- ).find((c) => SQLiteSquasher.unsquashPK(c).includes(statement.columnName));
35203
- const columnIsPartOfIndex = Object.values(
35204
- json1.tables[newTableName].indexes
35205
- ).find((c) => SQLiteSquasher.unsquashIdx(c).columns.includes(statement.columnName));
35206
- const columnIsPk = json1.tables[newTableName].columns[statement.columnName].primaryKey;
35207
- const columnIsPartOfFk = Object.values(
35208
- json1.tables[newTableName].foreignKeys
35209
- ).find(
35210
- (t) => SQLiteSquasher.unsquashPushFK(t).columnsFrom.includes(
35211
- statement.columnName
35212
- )
35213
- );
35214
- const res = await connection.query(
35215
- `select count(*) as count from \`${newTableName}\``
35216
- );
35217
- const count2 = Number(res[0].count);
35218
- if (count2 > 0) {
35219
- infoToPrint.push(
35220
- `\xB7 You're about to delete ${source_default.underline(
35221
- statement.columnName
35222
- )} column in ${newTableName} table with ${count2} items`
35223
- );
35224
- columnsToRemove.push(`${newTableName}_${statement.columnName}`);
35225
- shouldAskForApprove = true;
35226
- }
35227
- if (columnIsPk || columnIsPartOfPk || columnIsPartOfIndex || columnIsPartOfFk) {
35228
- tablesContext[newTableName] = [
35229
- ..._moveDataStatements(statement.tableName, json2, true)
35230
- ];
35231
- const tablesReferncingCurrent = [];
35232
- for (const table4 of Object.values(json1.tables)) {
35233
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35234
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35235
- ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35236
- tablesReferncingCurrent.push(...tablesRefs);
35237
- }
35238
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35239
- for (const table4 of uniqueTableRefs) {
35240
- if (typeof tablesContext[table4] === "undefined") {
35241
- tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35242
- }
35243
- }
35244
- } else {
35245
- if (typeof tablesContext[newTableName] === "undefined") {
35246
- const stmnt = fromJson([statement], "sqlite")[0];
35247
- statementsToExecute.push(stmnt);
35248
- }
35249
- }
35250
- } else if (statement.type === "sqlite_alter_table_add_column") {
35251
- const newTableName = getOldTableName(statement.tableName, meta);
35252
- if (statement.column.notNull && !statement.column.default) {
35253
- const res = await connection.query(
35254
- `select count(*) as count from \`${newTableName}\``
35255
- );
35256
- const count2 = Number(res[0].count);
35257
- if (count2 > 0) {
35258
- infoToPrint.push(
35259
- `\xB7 You're about to add not-null ${source_default.underline(
35260
- statement.column.name
35261
- )} column without default value, which contains ${count2} items`
35262
- );
35263
- tablesToTruncate.push(newTableName);
35264
- statementsToExecute.push(`delete from ${newTableName};`);
35265
- shouldAskForApprove = true;
35266
- }
35267
- }
35268
- if (statement.column.primaryKey) {
35269
- tablesContext[newTableName] = [
35270
- ..._moveDataStatements(statement.tableName, json2, true)
35271
- ];
35272
- const tablesReferncingCurrent = [];
35273
- for (const table4 of Object.values(json1.tables)) {
35274
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35275
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35276
- ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35277
- tablesReferncingCurrent.push(...tablesRefs);
35278
- }
35279
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35280
- for (const table4 of uniqueTableRefs) {
35281
- if (typeof tablesContext[table4] === "undefined") {
35282
- tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35283
- }
35284
- }
35285
- } else {
35286
- if (typeof tablesContext[newTableName] === "undefined") {
35287
- const stmnt = fromJson([statement], "sqlite")[0];
35288
- statementsToExecute.push(stmnt);
35289
- }
35290
- }
35291
- } 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") {
35292
- if (!(statement.type === "alter_table_alter_column_set_notnull" && statement.columnPk)) {
35293
- const newTableName = getOldTableName(statement.tableName, meta);
35294
- if (statement.type === "alter_table_alter_column_set_notnull" && typeof statement.columnDefault === "undefined") {
35295
- const res = await connection.query(
35296
- `select count(*) as count from \`${newTableName}\``
35297
- );
35298
- const count2 = Number(res[0].count);
35299
- if (count2 > 0) {
35300
- infoToPrint.push(
35301
- `\xB7 You're about to add not-null constraint to ${source_default.underline(
35302
- statement.columnName
35303
- )} column without default value, which contains ${count2} items`
35304
- );
35305
- tablesToTruncate.push(newTableName);
35306
- shouldAskForApprove = true;
35307
- }
35308
- tablesContext[newTableName] = _moveDataStatements(
35309
- statement.tableName,
35310
- json1,
35311
- true
35312
- );
35313
- } else {
35314
- if (typeof tablesContext[newTableName] === "undefined") {
35315
- tablesContext[newTableName] = _moveDataStatements(
35316
- statement.tableName,
35317
- json1
35318
- );
35319
- }
35320
- }
35321
- const tablesReferncingCurrent = [];
35322
- for (const table4 of Object.values(json1.tables)) {
35323
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35324
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35325
- ).map((t) => {
35326
- return getNewTableName(
35327
- SQLiteSquasher.unsquashPushFK(t).tableFrom,
35328
- meta
35329
- );
35330
- });
35331
- tablesReferncingCurrent.push(...tablesRefs);
35332
- }
35333
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35334
- for (const table4 of uniqueTableRefs) {
35335
- if (typeof tablesContext[table4] === "undefined") {
35336
- tablesContext[table4] = [..._moveDataStatements(table4, json1)];
35337
- }
35338
- }
35339
- }
35340
- } else if (statement.type === "create_reference" || statement.type === "delete_reference" || statement.type === "alter_reference") {
35341
- const fk4 = SQLiteSquasher.unsquashPushFK(statement.data);
35342
- if (typeof tablesContext[statement.tableName] === "undefined") {
35343
- tablesContext[statement.tableName] = _moveDataStatements(
35344
- statement.tableName,
35345
- json2
35346
- );
35347
- }
35348
- } 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") {
35349
- const newTableName = getOldTableName(statement.tableName, meta);
35350
- if (typeof tablesContext[newTableName] === "undefined") {
35351
- tablesContext[newTableName] = _moveDataStatements(
35352
- statement.tableName,
35353
- json2
35354
- );
35355
- }
35356
- } else {
35357
- const stmnt = fromJson([statement], "sqlite");
35358
- if (typeof stmnt !== "undefined") {
35359
- statementsToExecute.push(...stmnt);
35360
- }
35361
- }
35362
- }
35363
- for (const context of Object.values(tablesContext)) {
35364
- statementsToExecute.push(...context);
35365
- }
35366
- return {
35367
- statementsToExecute,
35368
- shouldAskForApprove,
35369
- infoToPrint,
35370
- columnsToRemove: [...new Set(columnsToRemove)],
35371
- schemasToRemove: [...new Set(schemasToRemove)],
35372
- tablesToTruncate: [...new Set(tablesToTruncate)],
35373
- tablesToRemove: [...new Set(tablesToRemove)]
35374
- };
35375
- };
35376
-
35377
35953
  // src/api.ts
35954
+ init_sqlitePushUtils();
35378
35955
  init_global();
35379
35956
  init_migrationPreparator();
35380
35957
  init_mysqlSchema();