drizzle-kit 0.24.2-5be80aa → 0.24.2-6fbcf46

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 +651 -1229
  2. package/api.mjs +651 -1229
  3. package/bin.cjs +47555 -50804
  4. package/index.d.mts +4 -3
  5. package/index.d.ts +4 -3
  6. package/package.json +2 -2
  7. package/utils.js +0 -11
  8. package/utils.mjs +0 -10
package/api.mjs CHANGED
@@ -8366,13 +8366,6 @@ var init_sqliteSchema = __esm({
8366
8366
  function isPgArrayType(sqlType) {
8367
8367
  return sqlType.match(/.*\[\d*\].*|.*\[\].*/g) !== null;
8368
8368
  }
8369
- function findAddedAndRemoved(columnNames1, columnNames2) {
8370
- const set1 = new Set(columnNames1);
8371
- const set2 = new Set(columnNames2);
8372
- const addedColumns = columnNames2.filter((it) => !set1.has(it));
8373
- const removedColumns = columnNames1.filter((it) => !set2.has(it));
8374
- return { addedColumns, removedColumns };
8375
- }
8376
8369
  var copy, prepareMigrationMeta, schemaRenameKey, tableRenameKey, columnRenameKey;
8377
8370
  var init_utils = __esm({
8378
8371
  "src/utils.ts"() {
@@ -11482,20 +11475,7 @@ var init_jsonDiffer = __esm({
11482
11475
  });
11483
11476
 
11484
11477
  // src/sqlgenerator.ts
11485
- function fromJson(statements, dialect7, action, json2) {
11486
- const result = statements.flatMap((statement) => {
11487
- const filtered = convertors.filter((it) => {
11488
- return it.can(statement, dialect7);
11489
- });
11490
- const convertor = filtered.length === 1 ? filtered[0] : void 0;
11491
- if (!convertor) {
11492
- return "";
11493
- }
11494
- return convertor.convert(statement, json2, action);
11495
- }).filter((it) => it !== "");
11496
- return result;
11497
- }
11498
- 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;
11478
+ var pgNativeTypes, isPgNativeType, Convertor, PgCreateTableConvertor, MySqlCreateTableConvertor, SQLiteCreateTableConvertor, PgAlterTableAlterColumnSetGenerated, PgAlterTableAlterColumnDropGenerated, PgAlterTableAlterColumnAlterGenerated, PgAlterTableAddUniqueConstraintConvertor, PgAlterTableDropUniqueConstraintConvertor, MySQLAlterTableAddUniqueConstraintConvertor, MySQLAlterTableDropUniqueConstraintConvertor, SQLiteAlterTableAddUniqueConstraintConvertor, SQLiteAlterTableDropUniqueConstraintConvertor, CreatePgSequenceConvertor, DropPgSequenceConvertor, RenamePgSequenceConvertor, MovePgSequenceConvertor, AlterPgSequenceConvertor, CreateTypeEnumConvertor, AlterTypeAddValueConvertor, PgDropTableConvertor, MySQLDropTableConvertor, SQLiteDropTableConvertor, PgRenameTableConvertor, SqliteRenameTableConvertor, MySqlRenameTableConvertor, PgAlterTableRenameColumnConvertor, MySqlAlterTableRenameColumnConvertor, SQLiteAlterTableRenameColumnConvertor, PgAlterTableDropColumnConvertor, MySqlAlterTableDropColumnConvertor, SQLiteAlterTableDropColumnConvertor, PgAlterTableAddColumnConvertor, MySqlAlterTableAddColumnConvertor, SQLiteAlterTableAddColumnConvertor, PgAlterTableAlterColumnSetTypeConvertor, SQLiteAlterTableAlterColumnSetTypeConvertor, PgAlterTableAlterColumnSetDefaultConvertor, SqliteAlterTableAlterColumnSetDefaultConvertor, PgAlterTableAlterColumnDropDefaultConvertor, PgAlterTableAlterColumnDropGeneratedConvertor, PgAlterTableAlterColumnSetExpressionConvertor, PgAlterTableAlterColumnAlterrGeneratedConvertor, SqliteAlterTableAlterColumnDropGeneratedConvertor, SqliteAlterTableAlterColumnSetExpressionConvertor, SqliteAlterTableAlterColumnAlterGeneratedConvertor, MySqlAlterTableAlterColumnAlterrGeneratedConvertor, MySqlAlterTableAddPk, MySqlAlterTableDropPk, 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;
11499
11479
  var init_sqlgenerator = __esm({
11500
11480
  "src/sqlgenerator.ts"() {
11501
11481
  "use strict";
@@ -11647,7 +11627,7 @@ var init_sqlgenerator = __esm({
11647
11627
  };
11648
11628
  SQLiteCreateTableConvertor = class extends Convertor {
11649
11629
  can(statement, dialect7) {
11650
- return statement.type === "sqlite_create_table" && (dialect7 === "sqlite" || dialect7 === "turso");
11630
+ return statement.type === "sqlite_create_table" && dialect7 === "sqlite";
11651
11631
  }
11652
11632
  convert(st) {
11653
11633
  const {
@@ -11817,6 +11797,32 @@ var init_sqlgenerator = __esm({
11817
11797
  return `ALTER TABLE \`${statement.tableName}\` DROP INDEX \`${unsquashed.name}\`;`;
11818
11798
  }
11819
11799
  };
11800
+ SQLiteAlterTableAddUniqueConstraintConvertor = class extends Convertor {
11801
+ can(statement, dialect7) {
11802
+ return statement.type === "create_unique_constraint" && dialect7 === "sqlite";
11803
+ }
11804
+ convert(statement) {
11805
+ return `/*
11806
+ 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
11807
+ Please refer to: https://www.techonthenet.com/sqlite/unique.php
11808
+
11809
+ Due to that we don't generate migration automatically and it has to be done manually
11810
+ */`;
11811
+ }
11812
+ };
11813
+ SQLiteAlterTableDropUniqueConstraintConvertor = class extends Convertor {
11814
+ can(statement, dialect7) {
11815
+ return statement.type === "delete_unique_constraint" && dialect7 === "sqlite";
11816
+ }
11817
+ convert(statement) {
11818
+ return `/*
11819
+ 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
11820
+ Please refer to: https://www.techonthenet.com/sqlite/unique.php
11821
+
11822
+ Due to that we don't generate migration automatically and it has to be done manually
11823
+ */`;
11824
+ }
11825
+ };
11820
11826
  CreatePgSequenceConvertor = class extends Convertor {
11821
11827
  can(statement, dialect7) {
11822
11828
  return statement.type === "create_sequence" && dialect7 === "postgresql";
@@ -11924,7 +11930,7 @@ var init_sqlgenerator = __esm({
11924
11930
  };
11925
11931
  SQLiteDropTableConvertor = class extends Convertor {
11926
11932
  can(statement, dialect7) {
11927
- return statement.type === "drop_table" && (dialect7 === "sqlite" || dialect7 === "turso");
11933
+ return statement.type === "drop_table" && dialect7 === "sqlite";
11928
11934
  }
11929
11935
  convert(statement) {
11930
11936
  const { tableName } = statement;
@@ -11944,7 +11950,7 @@ var init_sqlgenerator = __esm({
11944
11950
  };
11945
11951
  SqliteRenameTableConvertor = class extends Convertor {
11946
11952
  can(statement, dialect7) {
11947
- return statement.type === "rename_table" && (dialect7 === "sqlite" || dialect7 === "turso");
11953
+ return statement.type === "rename_table" && dialect7 === "sqlite";
11948
11954
  }
11949
11955
  convert(statement) {
11950
11956
  const { tableNameFrom, tableNameTo } = statement;
@@ -11981,11 +11987,11 @@ var init_sqlgenerator = __esm({
11981
11987
  };
11982
11988
  SQLiteAlterTableRenameColumnConvertor = class extends Convertor {
11983
11989
  can(statement, dialect7) {
11984
- return statement.type === "alter_table_rename_column" && (dialect7 === "sqlite" || dialect7 === "turso");
11990
+ return statement.type === "alter_table_rename_column" && dialect7 === "sqlite";
11985
11991
  }
11986
11992
  convert(statement) {
11987
11993
  const { tableName, oldColumnName, newColumnName } = statement;
11988
- return `ALTER TABLE \`${tableName}\` RENAME COLUMN "${oldColumnName}" TO "${newColumnName}";`;
11994
+ return `ALTER TABLE \`${tableName}\` RENAME COLUMN \`${oldColumnName}\` TO \`${newColumnName}\`;`;
11989
11995
  }
11990
11996
  };
11991
11997
  PgAlterTableDropColumnConvertor = class extends Convertor {
@@ -12009,7 +12015,7 @@ var init_sqlgenerator = __esm({
12009
12015
  };
12010
12016
  SQLiteAlterTableDropColumnConvertor = class extends Convertor {
12011
12017
  can(statement, dialect7) {
12012
- return statement.type === "alter_table_drop_column" && (dialect7 === "sqlite" || dialect7 === "turso");
12018
+ return statement.type === "alter_table_drop_column" && dialect7 === "sqlite";
12013
12019
  }
12014
12020
  convert(statement) {
12015
12021
  const { tableName, columnName } = statement;
@@ -12062,7 +12068,7 @@ var init_sqlgenerator = __esm({
12062
12068
  };
12063
12069
  SQLiteAlterTableAddColumnConvertor = class extends Convertor {
12064
12070
  can(statement, dialect7) {
12065
- return statement.type === "sqlite_alter_table_add_column" && (dialect7 === "sqlite" || dialect7 === "turso");
12071
+ return statement.type === "sqlite_alter_table_add_column" && dialect7 === "sqlite";
12066
12072
  }
12067
12073
  convert(statement) {
12068
12074
  const { tableName, column: column4, referenceData } = statement;
@@ -12086,6 +12092,21 @@ var init_sqlgenerator = __esm({
12086
12092
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DATA TYPE ${newDataType};`;
12087
12093
  }
12088
12094
  };
12095
+ SQLiteAlterTableAlterColumnSetTypeConvertor = class extends Convertor {
12096
+ can(statement, dialect7) {
12097
+ return statement.type === "alter_table_alter_column_set_type" && dialect7 === "sqlite";
12098
+ }
12099
+ convert(statement) {
12100
+ return `/*
12101
+ 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
12102
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12103
+ https://www.sqlite.org/lang_altertable.html
12104
+ https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12105
+
12106
+ Due to that we don't generate migration automatically and it has to be done manually
12107
+ */`;
12108
+ }
12109
+ };
12089
12110
  PgAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
12090
12111
  can(statement, dialect7) {
12091
12112
  return statement.type === "alter_table_alter_column_set_default" && dialect7 === "postgresql";
@@ -12096,6 +12117,21 @@ var init_sqlgenerator = __esm({
12096
12117
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DEFAULT ${statement.newDefaultValue};`;
12097
12118
  }
12098
12119
  };
12120
+ SqliteAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
12121
+ can(statement, dialect7) {
12122
+ return statement.type === "alter_table_alter_column_set_default" && dialect7 === "sqlite";
12123
+ }
12124
+ convert(statement) {
12125
+ return `/*
12126
+ 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
12127
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12128
+ https://www.sqlite.org/lang_altertable.html
12129
+ https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12130
+
12131
+ Due to that we don't generate migration automatically and it has to be done manually
12132
+ */`;
12133
+ }
12134
+ };
12099
12135
  PgAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
12100
12136
  can(statement, dialect7) {
12101
12137
  return statement.type === "alter_table_alter_column_drop_default" && dialect7 === "postgresql";
@@ -12194,7 +12230,7 @@ var init_sqlgenerator = __esm({
12194
12230
  };
12195
12231
  SqliteAlterTableAlterColumnDropGeneratedConvertor = class extends Convertor {
12196
12232
  can(statement, dialect7) {
12197
- return statement.type === "alter_table_alter_column_drop_generated" && (dialect7 === "sqlite" || dialect7 === "turso");
12233
+ return statement.type === "alter_table_alter_column_drop_generated" && dialect7 === "sqlite";
12198
12234
  }
12199
12235
  convert(statement) {
12200
12236
  const {
@@ -12235,7 +12271,7 @@ var init_sqlgenerator = __esm({
12235
12271
  };
12236
12272
  SqliteAlterTableAlterColumnSetExpressionConvertor = class extends Convertor {
12237
12273
  can(statement, dialect7) {
12238
- return statement.type === "alter_table_alter_column_set_generated" && (dialect7 === "sqlite" || dialect7 === "turso");
12274
+ return statement.type === "alter_table_alter_column_set_generated" && dialect7 === "sqlite";
12239
12275
  }
12240
12276
  convert(statement) {
12241
12277
  const {
@@ -12276,7 +12312,7 @@ var init_sqlgenerator = __esm({
12276
12312
  };
12277
12313
  SqliteAlterTableAlterColumnAlterGeneratedConvertor = class extends Convertor {
12278
12314
  can(statement, dialect7) {
12279
- return statement.type === "alter_table_alter_column_alter_generated" && (dialect7 === "sqlite" || dialect7 === "turso");
12315
+ return statement.type === "alter_table_alter_column_alter_generated" && dialect7 === "sqlite";
12280
12316
  }
12281
12317
  convert(statement) {
12282
12318
  const {
@@ -12369,67 +12405,6 @@ var init_sqlgenerator = __esm({
12369
12405
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY`;
12370
12406
  }
12371
12407
  };
12372
- LibSQLModifyColumn = class extends Convertor {
12373
- can(statement, dialect7) {
12374
- 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";
12375
- }
12376
- convert(statement, json2) {
12377
- const { tableName, columnName } = statement;
12378
- let columnType = ``;
12379
- let columnDefault = "";
12380
- let columnNotNull = "";
12381
- const sqlStatements = [];
12382
- const indexes = [];
12383
- for (const table4 of Object.values(json2.tables)) {
12384
- for (const index4 of Object.values(table4.indexes)) {
12385
- const unsquashed = SQLiteSquasher.unsquashIdx(index4);
12386
- sqlStatements.push(`DROP INDEX IF EXISTS "${unsquashed.name}";`);
12387
- indexes.push({ ...unsquashed, tableName: table4.name });
12388
- }
12389
- }
12390
- switch (statement.type) {
12391
- case "alter_table_alter_column_set_type":
12392
- columnType = ` ${statement.newDataType}`;
12393
- columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12394
- columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12395
- break;
12396
- case "alter_table_alter_column_drop_notnull":
12397
- columnType = ` ${statement.newDataType}`;
12398
- columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12399
- columnNotNull = "";
12400
- break;
12401
- case "alter_table_alter_column_set_notnull":
12402
- columnType = ` ${statement.newDataType}`;
12403
- columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
12404
- columnNotNull = ` NOT NULL`;
12405
- break;
12406
- case "alter_table_alter_column_set_default":
12407
- columnType = ` ${statement.newDataType}`;
12408
- columnDefault = ` DEFAULT ${statement.newDefaultValue}`;
12409
- columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12410
- break;
12411
- case "alter_table_alter_column_drop_default":
12412
- columnType = ` ${statement.newDataType}`;
12413
- columnDefault = "";
12414
- columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
12415
- break;
12416
- }
12417
- columnDefault = columnDefault instanceof Date ? columnDefault.toISOString() : columnDefault;
12418
- sqlStatements.push(
12419
- `ALTER TABLE \`${tableName}\` ALTER COLUMN "${columnName}" TO "${columnName}"${columnType}${columnNotNull}${columnDefault};`
12420
- );
12421
- for (const index4 of indexes) {
12422
- const indexPart = index4.isUnique ? "UNIQUE INDEX" : "INDEX";
12423
- const whereStatement = index4.where ? ` WHERE ${index4.where}` : "";
12424
- const uniqueString = index4.columns.map((it) => `\`${it}\``).join(",");
12425
- const tableName2 = index4.tableName;
12426
- sqlStatements.push(
12427
- `CREATE ${indexPart} \`${index4.name}\` ON \`${tableName2}\` (${uniqueString})${whereStatement};`
12428
- );
12429
- }
12430
- return sqlStatements;
12431
- }
12432
- };
12433
12408
  MySqlModifyColumn = class extends Convertor {
12434
12409
  can(statement, dialect7) {
12435
12410
  return (statement.type === "alter_table_alter_column_set_type" || statement.type === "alter_table_alter_column_set_notnull" || statement.type === "alter_table_alter_column_drop_notnull" || statement.type === "alter_table_alter_column_drop_on_update" || statement.type === "alter_table_alter_column_set_on_update" || statement.type === "alter_table_alter_column_set_autoincrement" || statement.type === "alter_table_alter_column_drop_autoincrement" || statement.type === "alter_table_alter_column_set_default" || statement.type === "alter_table_alter_column_drop_default" || statement.type === "alter_table_alter_column_set_generated" || statement.type === "alter_table_alter_column_drop_generated") && dialect7 === "mysql";
@@ -12567,6 +12542,21 @@ var init_sqlgenerator = __esm({
12567
12542
  return `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${columnName}\`${columnType}${columnAutoincrement}${columnGenerated}${columnNotNull}${columnDefault}${columnOnUpdate};`;
12568
12543
  }
12569
12544
  };
12545
+ SqliteAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
12546
+ can(statement, dialect7) {
12547
+ return statement.type === "alter_table_alter_column_drop_default" && dialect7 === "sqlite";
12548
+ }
12549
+ convert(statement) {
12550
+ return `/*
12551
+ 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
12552
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12553
+ https://www.sqlite.org/lang_altertable.html
12554
+ https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12555
+
12556
+ Due to that we don't generate migration automatically and it has to be done manually
12557
+ */`;
12558
+ }
12559
+ };
12570
12560
  PgAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
12571
12561
  can(statement, dialect7) {
12572
12562
  return statement.type === "create_composite_pk" && dialect7 === "postgresql";
@@ -12631,6 +12621,85 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12631
12621
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY, ADD PRIMARY KEY(\`${newColumns.join("`,`")}\`);`;
12632
12622
  }
12633
12623
  };
12624
+ SqliteAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
12625
+ can(statement, dialect7) {
12626
+ return statement.type === "create_composite_pk" && dialect7 === "sqlite";
12627
+ }
12628
+ convert(statement) {
12629
+ let msg = "/*\n";
12630
+ msg += `You're trying to add PRIMARY KEY(${statement.data}) to '${statement.tableName}' table
12631
+ `;
12632
+ msg += "SQLite does not support adding primary key to an already created table\n";
12633
+ msg += "You can do it in 3 steps with drizzle orm:\n";
12634
+ msg += " - create new mirror table with needed pk, rename current table to old_table, generate SQL\n";
12635
+ msg += " - migrate old data from one table to another\n";
12636
+ msg += " - delete old_table in schema, generate sql\n\n";
12637
+ msg += "or create manual migration like below:\n\n";
12638
+ msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12639
+ msg += "CREATE TABLE table_name (\n";
12640
+ msg += " column1 datatype [ NULL | NOT NULL ],\n";
12641
+ msg += " column2 datatype [ NULL | NOT NULL ],\n";
12642
+ msg += " ...\n";
12643
+ msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12644
+ msg += " );\n";
12645
+ msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12646
+ msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12647
+ msg += "*/\n";
12648
+ return msg;
12649
+ }
12650
+ };
12651
+ SqliteAlterTableDeleteCompositePrimaryKeyConvertor = class extends Convertor {
12652
+ can(statement, dialect7) {
12653
+ return statement.type === "delete_composite_pk" && dialect7 === "sqlite";
12654
+ }
12655
+ convert(statement) {
12656
+ let msg = "/*\n";
12657
+ msg += `You're trying to delete PRIMARY KEY(${statement.data}) from '${statement.tableName}' table
12658
+ `;
12659
+ msg += "SQLite does not supportprimary key deletion from existing table\n";
12660
+ msg += "You can do it in 3 steps with drizzle orm:\n";
12661
+ msg += " - create new mirror table table without pk, rename current table to old_table, generate SQL\n";
12662
+ msg += " - migrate old data from one table to another\n";
12663
+ msg += " - delete old_table in schema, generate sql\n\n";
12664
+ msg += "or create manual migration like below:\n\n";
12665
+ msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12666
+ msg += "CREATE TABLE table_name (\n";
12667
+ msg += " column1 datatype [ NULL | NOT NULL ],\n";
12668
+ msg += " column2 datatype [ NULL | NOT NULL ],\n";
12669
+ msg += " ...\n";
12670
+ msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12671
+ msg += " );\n";
12672
+ msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12673
+ msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12674
+ msg += "*/\n";
12675
+ return msg;
12676
+ }
12677
+ };
12678
+ SqliteAlterTableAlterCompositePrimaryKeyConvertor = class extends Convertor {
12679
+ can(statement, dialect7) {
12680
+ return statement.type === "alter_composite_pk" && dialect7 === "sqlite";
12681
+ }
12682
+ convert(statement) {
12683
+ let msg = "/*\n";
12684
+ msg += "SQLite does not support altering primary key\n";
12685
+ msg += "You can do it in 3 steps with drizzle orm:\n";
12686
+ msg += " - create new mirror table with needed pk, rename current table to old_table, generate SQL\n";
12687
+ msg += " - migrate old data from one table to another\n";
12688
+ msg += " - delete old_table in schema, generate sql\n\n";
12689
+ msg += "or create manual migration like below:\n\n";
12690
+ msg += "ALTER TABLE table_name RENAME TO old_table;\n";
12691
+ msg += "CREATE TABLE table_name (\n";
12692
+ msg += " column1 datatype [ NULL | NOT NULL ],\n";
12693
+ msg += " column2 datatype [ NULL | NOT NULL ],\n";
12694
+ msg += " ...\n";
12695
+ msg += " PRIMARY KEY (pk_col1, pk_col2, ... pk_col_n)\n";
12696
+ msg += " );\n";
12697
+ msg += "INSERT INTO table_name SELECT * FROM old_table;\n\n";
12698
+ msg += "Due to that we don't generate migration automatically and it has to be done manually\n";
12699
+ msg += "*/\n";
12700
+ return msg;
12701
+ }
12702
+ };
12634
12703
  PgAlterTableAlterColumnSetPrimaryKeyConvertor = class extends Convertor {
12635
12704
  can(statement, dialect7) {
12636
12705
  return statement.type === "alter_table_alter_column_set_pk" && dialect7 === "postgresql";
@@ -12675,6 +12744,51 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12675
12744
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET NOT NULL;`;
12676
12745
  }
12677
12746
  };
12747
+ SqliteAlterTableAlterColumnSetNotNullConvertor = class extends Convertor {
12748
+ can(statement, dialect7) {
12749
+ return statement.type === "alter_table_alter_column_set_notnull" && dialect7 === "sqlite";
12750
+ }
12751
+ convert(statement) {
12752
+ return `/*
12753
+ 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
12754
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12755
+ https://www.sqlite.org/lang_altertable.html
12756
+ https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12757
+
12758
+ Due to that we don't generate migration automatically and it has to be done manually
12759
+ */`;
12760
+ }
12761
+ };
12762
+ SqliteAlterTableAlterColumnSetAutoincrementConvertor = class extends Convertor {
12763
+ can(statement, dialect7) {
12764
+ return statement.type === "alter_table_alter_column_set_autoincrement" && dialect7 === "sqlite";
12765
+ }
12766
+ convert(statement) {
12767
+ return `/*
12768
+ 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
12769
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12770
+ https://www.sqlite.org/lang_altertable.html
12771
+ https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12772
+
12773
+ Due to that we don't generate migration automatically and it has to be done manually
12774
+ */`;
12775
+ }
12776
+ };
12777
+ SqliteAlterTableAlterColumnDropAutoincrementConvertor = class extends Convertor {
12778
+ can(statement, dialect7) {
12779
+ return statement.type === "alter_table_alter_column_drop_autoincrement" && dialect7 === "sqlite";
12780
+ }
12781
+ convert(statement) {
12782
+ return `/*
12783
+ 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
12784
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12785
+ https://www.sqlite.org/lang_altertable.html
12786
+ https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12787
+
12788
+ Due to that we don't generate migration automatically and it has to be done manually
12789
+ */`;
12790
+ }
12791
+ };
12678
12792
  PgAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12679
12793
  can(statement, dialect7) {
12680
12794
  return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "postgresql";
@@ -12685,6 +12799,21 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12685
12799
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" DROP NOT NULL;`;
12686
12800
  }
12687
12801
  };
12802
+ SqliteAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12803
+ can(statement, dialect7) {
12804
+ return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "sqlite";
12805
+ }
12806
+ convert(statement) {
12807
+ return `/*
12808
+ 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
12809
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12810
+ https://www.sqlite.org/lang_altertable.html
12811
+ https://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3
12812
+
12813
+ Due to that we don't generate migration automatically and it has to be done manually
12814
+ */`;
12815
+ }
12816
+ };
12688
12817
  PgCreateForeignKeyConvertor = class extends Convertor {
12689
12818
  can(statement, dialect7) {
12690
12819
  return statement.type === "create_reference" && dialect7 === "postgresql";
@@ -12715,21 +12844,18 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12715
12844
  return sql2;
12716
12845
  }
12717
12846
  };
12718
- LibSQLCreateForeignKeyConvertor = class extends Convertor {
12847
+ SqliteCreateForeignKeyConvertor = class extends Convertor {
12719
12848
  can(statement, dialect7) {
12720
- return statement.type === "create_reference" && dialect7 === "turso";
12849
+ return statement.type === "create_reference" && dialect7 === "sqlite";
12721
12850
  }
12722
- convert(statement, json2, action) {
12723
- const { columnsFrom, columnsTo, tableFrom, onDelete, onUpdate, tableTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
12724
- const { columnDefault, columnNotNull, columnType } = statement;
12725
- const onDeleteStatement = onDelete ? ` ON DELETE ${onDelete}` : "";
12726
- const onUpdateStatement = onUpdate ? ` ON UPDATE ${onUpdate}` : "";
12727
- const columnsDefaultValue = columnDefault ? ` DEFAULT ${columnDefault}` : "";
12728
- const columnNotNullValue = columnNotNull ? ` NOT NULL` : "";
12729
- const columnTypeValue = columnType ? ` ${columnType}` : "";
12730
- const columnFrom = columnsFrom[0];
12731
- const columnTo = columnsTo[0];
12732
- return `ALTER TABLE \`${tableFrom}\` ALTER COLUMN "${columnFrom}" TO "${columnFrom}"${columnTypeValue}${columnNotNullValue}${columnsDefaultValue} REFERENCES ${tableTo}(${columnTo})${onDeleteStatement}${onUpdateStatement};`;
12851
+ convert(statement) {
12852
+ return `/*
12853
+ 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
12854
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12855
+ https://www.sqlite.org/lang_altertable.html
12856
+
12857
+ Due to that we don't generate migration automatically and it has to be done manually
12858
+ */`;
12733
12859
  }
12734
12860
  };
12735
12861
  MySqlCreateForeignKeyConvertor = class extends Convertor {
@@ -12778,6 +12904,20 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12778
12904
  return sql2;
12779
12905
  }
12780
12906
  };
12907
+ SqliteAlterForeignKeyConvertor = class extends Convertor {
12908
+ can(statement, dialect7) {
12909
+ return statement.type === "alter_reference" && dialect7 === "sqlite";
12910
+ }
12911
+ convert(statement) {
12912
+ return `/*
12913
+ 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
12914
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12915
+ https://www.sqlite.org/lang_altertable.html
12916
+
12917
+ Due to that we don't generate migration automatically and it has to be done manually
12918
+ */`;
12919
+ }
12920
+ };
12781
12921
  PgDeleteForeignKeyConvertor = class extends Convertor {
12782
12922
  can(statement, dialect7) {
12783
12923
  return statement.type === "delete_reference" && dialect7 === "postgresql";
@@ -12790,6 +12930,20 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12790
12930
  `;
12791
12931
  }
12792
12932
  };
12933
+ SqliteDeleteForeignKeyConvertor = class extends Convertor {
12934
+ can(statement, dialect7) {
12935
+ return statement.type === "delete_reference" && dialect7 === "sqlite";
12936
+ }
12937
+ convert(statement) {
12938
+ return `/*
12939
+ 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
12940
+ Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
12941
+ https://www.sqlite.org/lang_altertable.html
12942
+
12943
+ Due to that we don't generate migration automatically and it has to be done manually
12944
+ */`;
12945
+ }
12946
+ };
12793
12947
  MySqlDeleteForeignKeyConvertor = class extends Convertor {
12794
12948
  can(statement, dialect7) {
12795
12949
  return statement.type === "delete_reference" && dialect7 === "mysql";
@@ -12850,7 +13004,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12850
13004
  };
12851
13005
  CreateSqliteIndexConvertor = class extends Convertor {
12852
13006
  can(statement, dialect7) {
12853
- return statement.type === "create_index" && (dialect7 === "sqlite" || dialect7 === "turso");
13007
+ return statement.type === "create_index" && dialect7 === "sqlite";
12854
13008
  }
12855
13009
  convert(statement) {
12856
13010
  const { name: name2, columns, isUnique, where } = SQLiteSquasher.unsquashIdx(
@@ -12937,7 +13091,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12937
13091
  };
12938
13092
  SqliteDropIndexConvertor = class extends Convertor {
12939
13093
  can(statement, dialect7) {
12940
- return statement.type === "drop_index" && (dialect7 === "sqlite" || dialect7 === "turso");
13094
+ return statement.type === "drop_index" && dialect7 === "sqlite";
12941
13095
  }
12942
13096
  convert(statement) {
12943
13097
  const { name: name2 } = PgSquasher.unsquashIdx(statement.data);
@@ -12953,96 +13107,10 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12953
13107
  return `DROP INDEX \`${name2}\` ON \`${statement.tableName}\`;`;
12954
13108
  }
12955
13109
  };
12956
- SQLiteRecreateTableConvertor = class extends Convertor {
12957
- can(statement, dialect7) {
12958
- return statement.type === "recreate_table" && dialect7 === "sqlite";
12959
- }
12960
- convert(statement) {
12961
- const { tableName, columns, compositePKs, referenceData } = statement;
12962
- const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
12963
- const newTableName = `__new_${tableName}`;
12964
- const sqlStatements = [];
12965
- sqlStatements.push(`PRAGMA foreign_keys=OFF;`);
12966
- sqlStatements.push(
12967
- new SQLiteCreateTableConvertor().convert({
12968
- type: "sqlite_create_table",
12969
- tableName: newTableName,
12970
- columns,
12971
- referenceData,
12972
- compositePKs
12973
- })
12974
- );
12975
- sqlStatements.push(
12976
- `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
12977
- );
12978
- sqlStatements.push(
12979
- new SQLiteDropTableConvertor().convert({
12980
- type: "drop_table",
12981
- tableName,
12982
- schema: ""
12983
- })
12984
- );
12985
- sqlStatements.push(
12986
- new SqliteRenameTableConvertor().convert({
12987
- fromSchema: "",
12988
- tableNameFrom: newTableName,
12989
- tableNameTo: tableName,
12990
- toSchema: "",
12991
- type: "rename_table"
12992
- })
12993
- );
12994
- sqlStatements.push(`PRAGMA foreign_keys=ON;`);
12995
- return sqlStatements;
12996
- }
12997
- };
12998
- LibSQLRecreateTableConvertor = class extends Convertor {
12999
- can(statement, dialect7) {
13000
- return statement.type === "recreate_table" && dialect7 === "turso";
13001
- }
13002
- convert(statement) {
13003
- const { tableName, columns, compositePKs, referenceData } = statement;
13004
- const columnNames = columns.map((it) => `"${it.name}"`).join(", ");
13005
- const newTableName = `__new_${tableName}`;
13006
- const sqlStatements = [];
13007
- sqlStatements.push(`PRAGMA foreign_keys=OFF;`);
13008
- sqlStatements.push(
13009
- new SQLiteCreateTableConvertor().convert({
13010
- type: "sqlite_create_table",
13011
- tableName: newTableName,
13012
- columns,
13013
- referenceData,
13014
- compositePKs
13015
- })
13016
- );
13017
- sqlStatements.push(
13018
- `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
13019
- );
13020
- sqlStatements.push(
13021
- new SQLiteDropTableConvertor().convert({
13022
- type: "drop_table",
13023
- tableName,
13024
- schema: ""
13025
- })
13026
- );
13027
- sqlStatements.push(
13028
- new SqliteRenameTableConvertor().convert({
13029
- fromSchema: "",
13030
- tableNameFrom: newTableName,
13031
- tableNameTo: tableName,
13032
- toSchema: "",
13033
- type: "rename_table"
13034
- })
13035
- );
13036
- sqlStatements.push(`PRAGMA foreign_keys=ON;`);
13037
- return sqlStatements;
13038
- }
13039
- };
13040
13110
  convertors = [];
13041
13111
  convertors.push(new PgCreateTableConvertor());
13042
13112
  convertors.push(new MySqlCreateTableConvertor());
13043
13113
  convertors.push(new SQLiteCreateTableConvertor());
13044
- convertors.push(new SQLiteRecreateTableConvertor());
13045
- convertors.push(new LibSQLRecreateTableConvertor());
13046
13114
  convertors.push(new CreateTypeEnumConvertor());
13047
13115
  convertors.push(new CreatePgSequenceConvertor());
13048
13116
  convertors.push(new DropPgSequenceConvertor());
@@ -13090,7 +13158,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13090
13158
  convertors.push(new SqliteAlterTableAlterColumnAlterGeneratedConvertor());
13091
13159
  convertors.push(new SqliteAlterTableAlterColumnSetExpressionConvertor());
13092
13160
  convertors.push(new MySqlModifyColumn());
13093
- convertors.push(new LibSQLModifyColumn());
13094
13161
  convertors.push(new PgCreateForeignKeyConvertor());
13095
13162
  convertors.push(new MySqlCreateForeignKeyConvertor());
13096
13163
  convertors.push(new PgAlterForeignKeyConvertor());
@@ -13102,10 +13169,24 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13102
13169
  convertors.push(new PgAlterTableSetSchemaConvertor());
13103
13170
  convertors.push(new PgAlterTableSetNewSchemaConvertor());
13104
13171
  convertors.push(new PgAlterTableRemoveFromSchemaConvertor());
13105
- convertors.push(new LibSQLCreateForeignKeyConvertor());
13172
+ convertors.push(new SQLiteAlterTableAlterColumnSetTypeConvertor());
13173
+ convertors.push(new SqliteAlterForeignKeyConvertor());
13174
+ convertors.push(new SqliteDeleteForeignKeyConvertor());
13175
+ convertors.push(new SqliteCreateForeignKeyConvertor());
13176
+ convertors.push(new SQLiteAlterTableAddUniqueConstraintConvertor());
13177
+ convertors.push(new SQLiteAlterTableDropUniqueConstraintConvertor());
13106
13178
  convertors.push(new PgAlterTableAlterColumnDropGenerated());
13107
13179
  convertors.push(new PgAlterTableAlterColumnSetGenerated());
13108
13180
  convertors.push(new PgAlterTableAlterColumnAlterGenerated());
13181
+ convertors.push(new SqliteAlterTableAlterColumnSetNotNullConvertor());
13182
+ convertors.push(new SqliteAlterTableAlterColumnDropNotNullConvertor());
13183
+ convertors.push(new SqliteAlterTableAlterColumnSetDefaultConvertor());
13184
+ convertors.push(new SqliteAlterTableAlterColumnDropDefaultConvertor());
13185
+ convertors.push(new SqliteAlterTableAlterColumnSetAutoincrementConvertor());
13186
+ convertors.push(new SqliteAlterTableAlterColumnDropAutoincrementConvertor());
13187
+ convertors.push(new SqliteAlterTableCreateCompositePrimaryKeyConvertor());
13188
+ convertors.push(new SqliteAlterTableDeleteCompositePrimaryKeyConvertor());
13189
+ convertors.push(new SqliteAlterTableAlterCompositePrimaryKeyConvertor());
13109
13190
  convertors.push(new PgAlterTableCreateCompositePrimaryKeyConvertor());
13110
13191
  convertors.push(new PgAlterTableDeleteCompositePrimaryKeyConvertor());
13111
13192
  convertors.push(new PgAlterTableAlterCompositePrimaryKeyConvertor());
@@ -13114,6 +13195,19 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13114
13195
  convertors.push(new MySqlAlterTableCreateCompositePrimaryKeyConvertor());
13115
13196
  convertors.push(new MySqlAlterTableAddPk());
13116
13197
  convertors.push(new MySqlAlterTableAlterCompositePrimaryKeyConvertor());
13198
+ fromJson = (statements, dialect7) => {
13199
+ const result = statements.flatMap((statement) => {
13200
+ const filtered = convertors.filter((it) => {
13201
+ return it.can(statement, dialect7);
13202
+ });
13203
+ const convertor = filtered.length === 1 ? filtered[0] : void 0;
13204
+ if (!convertor) {
13205
+ return "";
13206
+ }
13207
+ return convertor.convert(statement);
13208
+ }).filter((it) => it !== "");
13209
+ return result;
13210
+ };
13117
13211
  https: `
13118
13212
  create table users (
13119
13213
  id int,
@@ -13140,248 +13234,12 @@ drop type __venum;
13140
13234
  }
13141
13235
  });
13142
13236
 
13143
- // src/cli/commands/sqlitePushUtils.ts
13144
- var _moveDataStatements, getOldTableName, getNewTableName, logSuggestionsAndReturn;
13145
- var init_sqlitePushUtils = __esm({
13146
- "src/cli/commands/sqlitePushUtils.ts"() {
13147
- "use strict";
13148
- init_source();
13149
- init_sqliteSchema();
13150
- init_sqlgenerator();
13151
- init_utils();
13152
- _moveDataStatements = (tableName, json, dataLoss = false) => {
13153
- const statements = [];
13154
- const newTableName = `__new_${tableName}`;
13155
- const tableColumns = Object.values(json.tables[tableName].columns);
13156
- const referenceData = Object.values(json.tables[tableName].foreignKeys);
13157
- const compositePKs = Object.values(
13158
- json.tables[tableName].compositePrimaryKeys
13159
- ).map((it) => SQLiteSquasher.unsquashPK(it));
13160
- const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
13161
- statements.push(
13162
- new SQLiteCreateTableConvertor().convert({
13163
- type: "sqlite_create_table",
13164
- tableName: newTableName,
13165
- columns: tableColumns,
13166
- referenceData: fks,
13167
- compositePKs
13168
- })
13169
- );
13170
- if (!dataLoss) {
13171
- const columns = Object.keys(json.tables[tableName].columns).map(
13172
- (c) => `"${c}"`
13173
- );
13174
- statements.push(
13175
- `INSERT INTO \`${newTableName}\`(${columns.join(
13176
- ", "
13177
- )}) SELECT ${columns.join(", ")} FROM \`${tableName}\`;`
13178
- );
13179
- }
13180
- statements.push(
13181
- new SQLiteDropTableConvertor().convert({
13182
- type: "drop_table",
13183
- tableName,
13184
- schema: ""
13185
- })
13186
- );
13187
- statements.push(
13188
- new SqliteRenameTableConvertor().convert({
13189
- fromSchema: "",
13190
- tableNameFrom: newTableName,
13191
- tableNameTo: tableName,
13192
- toSchema: "",
13193
- type: "rename_table"
13194
- })
13195
- );
13196
- for (const idx of Object.values(json.tables[tableName].indexes)) {
13197
- statements.push(
13198
- new CreateSqliteIndexConvertor().convert({
13199
- type: "create_index",
13200
- tableName,
13201
- schema: "",
13202
- data: idx
13203
- })
13204
- );
13205
- }
13206
- return statements;
13207
- };
13208
- getOldTableName = (tableName, meta) => {
13209
- for (const key of Object.keys(meta.tables)) {
13210
- const value = meta.tables[key];
13211
- if (`"${tableName}"` === value) {
13212
- return key.substring(1, key.length - 1);
13213
- }
13214
- }
13215
- return tableName;
13216
- };
13217
- getNewTableName = (tableName, meta) => {
13218
- if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
13219
- return meta.tables[`"${tableName}"`].substring(
13220
- 1,
13221
- meta.tables[`"${tableName}"`].length - 1
13222
- );
13223
- }
13224
- return tableName;
13225
- };
13226
- logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
13227
- let shouldAskForApprove = false;
13228
- const statementsToExecute = [];
13229
- const infoToPrint = [];
13230
- const tablesToRemove = [];
13231
- const columnsToRemove = [];
13232
- const schemasToRemove = [];
13233
- const tablesToTruncate = [];
13234
- for (const statement of statements) {
13235
- if (statement.type === "drop_table") {
13236
- const res = await connection.query(
13237
- `select count(*) as count from \`${statement.tableName}\``
13238
- );
13239
- const count2 = Number(res[0].count);
13240
- if (count2 > 0) {
13241
- infoToPrint.push(
13242
- `\xB7 You're about to delete ${source_default.underline(
13243
- statement.tableName
13244
- )} table with ${count2} items`
13245
- );
13246
- tablesToRemove.push(statement.tableName);
13247
- shouldAskForApprove = true;
13248
- }
13249
- const fromJsonStatement = fromJson([statement], "sqlite", "push");
13250
- statementsToExecute.push(
13251
- ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13252
- );
13253
- } else if (statement.type === "alter_table_drop_column") {
13254
- const tableName = statement.tableName;
13255
- const columnName = statement.columnName;
13256
- const res = await connection.query(
13257
- `select count(\`${tableName}\`.\`${columnName}\`) as count from \`${tableName}\``
13258
- );
13259
- const count2 = Number(res[0].count);
13260
- if (count2 > 0) {
13261
- infoToPrint.push(
13262
- `\xB7 You're about to delete ${source_default.underline(
13263
- columnName
13264
- )} column in ${tableName} table with ${count2} items`
13265
- );
13266
- columnsToRemove.push(`${tableName}_${statement.columnName}`);
13267
- shouldAskForApprove = true;
13268
- }
13269
- const fromJsonStatement = fromJson([statement], "sqlite", "push");
13270
- statementsToExecute.push(
13271
- ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13272
- );
13273
- } else if (statement.type === "sqlite_alter_table_add_column" && (statement.column.notNull && !statement.column.default)) {
13274
- const tableName = statement.tableName;
13275
- const columnName = statement.column.name;
13276
- const res = await connection.query(
13277
- `select count(*) as count from \`${tableName}\``
13278
- );
13279
- const count2 = Number(res[0].count);
13280
- if (count2 > 0) {
13281
- infoToPrint.push(
13282
- `\xB7 You're about to add not-null ${source_default.underline(
13283
- columnName
13284
- )} column without default value, which contains ${count2} items`
13285
- );
13286
- tablesToTruncate.push(tableName);
13287
- statementsToExecute.push(`delete from ${tableName};`);
13288
- shouldAskForApprove = true;
13289
- }
13290
- const fromJsonStatement = fromJson([statement], "sqlite", "push");
13291
- statementsToExecute.push(
13292
- ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13293
- );
13294
- } else if (statement.type === "recreate_table") {
13295
- const tableName = statement.tableName;
13296
- const oldTableName = getOldTableName(tableName, meta);
13297
- let dataLoss = false;
13298
- const prevColumnNames = Object.keys(json1.tables[oldTableName].columns);
13299
- const currentColumnNames = Object.keys(json2.tables[tableName].columns);
13300
- const { removedColumns, addedColumns } = findAddedAndRemoved(
13301
- prevColumnNames,
13302
- currentColumnNames
13303
- );
13304
- if (removedColumns.length) {
13305
- for (const removedColumn of removedColumns) {
13306
- const res = await connection.query(
13307
- `select count(\`${tableName}\`.\`${removedColumn}\`) as count from \`${tableName}\``
13308
- );
13309
- const count2 = Number(res[0].count);
13310
- if (count2 > 0) {
13311
- infoToPrint.push(
13312
- `\xB7 You're about to delete ${source_default.underline(
13313
- removedColumn
13314
- )} column in ${tableName} table with ${count2} items`
13315
- );
13316
- columnsToRemove.push(removedColumn);
13317
- shouldAskForApprove = true;
13318
- }
13319
- }
13320
- }
13321
- if (addedColumns.length) {
13322
- for (const addedColumn of addedColumns) {
13323
- const [res] = await connection.query(
13324
- `select count(*) as count from \`${tableName}\``
13325
- );
13326
- const columnConf = json2.tables[tableName].columns[addedColumn];
13327
- const count2 = Number(res.count);
13328
- if (count2 > 0 && columnConf.notNull && !columnConf.default) {
13329
- dataLoss = true;
13330
- infoToPrint.push(
13331
- `\xB7 You're about to add not-null ${source_default.underline(
13332
- addedColumn
13333
- )} column without default value to table, which contains ${count2} items`
13334
- );
13335
- shouldAskForApprove = true;
13336
- tablesToTruncate.push(tableName);
13337
- statementsToExecute.push(`DELETE FROM \`${tableName}\`;`);
13338
- }
13339
- }
13340
- }
13341
- const tablesReferencingCurrent = [];
13342
- for (const table4 of Object.values(json2.tables)) {
13343
- const tablesRefs = Object.values(json2.tables[table4.name].foreignKeys).filter((t) => SQLiteSquasher.unsquashPushFK(t).tableTo === tableName).map((it) => SQLiteSquasher.unsquashPushFK(it).tableFrom);
13344
- tablesReferencingCurrent.push(...tablesRefs);
13345
- }
13346
- if (!tablesReferencingCurrent.length) {
13347
- statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
13348
- continue;
13349
- }
13350
- const [{ foreign_keys: pragmaState }] = await connection.query(`PRAGMA foreign_keys;`);
13351
- if (pragmaState) {
13352
- statementsToExecute.push(`PRAGMA foreign_keys=OFF;`);
13353
- }
13354
- statementsToExecute.push(..._moveDataStatements(tableName, json2, dataLoss));
13355
- if (pragmaState) {
13356
- statementsToExecute.push(`PRAGMA foreign_keys=ON;`);
13357
- }
13358
- } else {
13359
- const fromJsonStatement = fromJson([statement], "sqlite", "push");
13360
- statementsToExecute.push(
13361
- ...Array.isArray(fromJsonStatement) ? fromJsonStatement : [fromJsonStatement]
13362
- );
13363
- }
13364
- }
13365
- return {
13366
- statementsToExecute,
13367
- shouldAskForApprove,
13368
- infoToPrint,
13369
- columnsToRemove: [...new Set(columnsToRemove)],
13370
- schemasToRemove: [...new Set(schemasToRemove)],
13371
- tablesToTruncate: [...new Set(tablesToTruncate)],
13372
- tablesToRemove: [...new Set(tablesToRemove)]
13373
- };
13374
- };
13375
- }
13376
- });
13377
-
13378
13237
  // src/jsonStatements.ts
13379
- 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;
13238
+ 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;
13380
13239
  var init_jsonStatements = __esm({
13381
13240
  "src/jsonStatements.ts"() {
13382
13241
  "use strict";
13383
13242
  init_source();
13384
- init_sqlitePushUtils();
13385
13243
  init_views();
13386
13244
  init_mysqlSchema();
13387
13245
  init_pgSchema();
@@ -14165,52 +14023,9 @@ var init_jsonStatements = __esm({
14165
14023
  const columnPk = json2.tables[tableName].columns[columnName].primaryKey;
14166
14024
  const columnGenerated = json2.tables[tableName].columns[columnName].generated;
14167
14025
  const compositePk = json2.tables[tableName].compositePrimaryKeys[`${tableName}_${columnName}`];
14168
- if (column4.autoincrement?.type === "added") {
14026
+ if (typeof column4.name !== "string") {
14169
14027
  statements.push({
14170
- type: "alter_table_alter_column_set_autoincrement",
14171
- tableName,
14172
- columnName,
14173
- schema: schema4,
14174
- newDataType: columnType,
14175
- columnDefault,
14176
- columnOnUpdate,
14177
- columnNotNull,
14178
- columnAutoIncrement,
14179
- columnPk
14180
- });
14181
- }
14182
- if (column4.autoincrement?.type === "changed") {
14183
- const type = column4.autoincrement.new ? "alter_table_alter_column_set_autoincrement" : "alter_table_alter_column_drop_autoincrement";
14184
- statements.push({
14185
- type,
14186
- tableName,
14187
- columnName,
14188
- schema: schema4,
14189
- newDataType: columnType,
14190
- columnDefault,
14191
- columnOnUpdate,
14192
- columnNotNull,
14193
- columnAutoIncrement,
14194
- columnPk
14195
- });
14196
- }
14197
- if (column4.autoincrement?.type === "deleted") {
14198
- statements.push({
14199
- type: "alter_table_alter_column_drop_autoincrement",
14200
- tableName,
14201
- columnName,
14202
- schema: schema4,
14203
- newDataType: columnType,
14204
- columnDefault,
14205
- columnOnUpdate,
14206
- columnNotNull,
14207
- columnAutoIncrement,
14208
- columnPk
14209
- });
14210
- }
14211
- if (typeof column4.name !== "string") {
14212
- statements.push({
14213
- type: "alter_table_rename_column",
14028
+ type: "alter_table_rename_column",
14214
14029
  tableName,
14215
14030
  oldColumnName: column4.name.old,
14216
14031
  newColumnName: column4.name.new,
@@ -14471,37 +14286,6 @@ var init_jsonStatements = __esm({
14471
14286
  };
14472
14287
  });
14473
14288
  };
14474
- prepareLibSQLCreateReferencesJson = (tableName, schema4, foreignKeys, json2, action) => {
14475
- return Object.values(foreignKeys).map((fkData) => {
14476
- const { columnsFrom, tableFrom, columnsTo } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14477
- let isMulticolumn = false;
14478
- if (columnsFrom.length > 1 || columnsTo.length > 1) {
14479
- isMulticolumn = true;
14480
- return {
14481
- type: "create_reference",
14482
- tableName,
14483
- data: fkData,
14484
- schema: schema4,
14485
- isMulticolumn
14486
- };
14487
- }
14488
- const columnFrom = columnsFrom[0];
14489
- const {
14490
- notNull: columnNotNull,
14491
- default: columnDefault,
14492
- type: columnType
14493
- } = json2.tables[tableFrom].columns[columnFrom];
14494
- return {
14495
- type: "create_reference",
14496
- tableName,
14497
- data: fkData,
14498
- schema: schema4,
14499
- columnNotNull,
14500
- columnDefault,
14501
- columnType
14502
- };
14503
- });
14504
- };
14505
14289
  prepareDropReferencesJson = (tableName, schema4, foreignKeys) => {
14506
14290
  return Object.values(foreignKeys).map((fkData) => {
14507
14291
  return {
@@ -14512,53 +14296,6 @@ var init_jsonStatements = __esm({
14512
14296
  };
14513
14297
  });
14514
14298
  };
14515
- prepareLibSQLDropReferencesJson = (tableName, schema4, foreignKeys, json2, meta, action) => {
14516
- const statements = Object.values(foreignKeys).map((fkData) => {
14517
- const { columnsFrom, tableFrom, columnsTo, name: name2, tableTo, onDelete, onUpdate } = action === "push" ? SQLiteSquasher.unsquashPushFK(fkData) : SQLiteSquasher.unsquashFK(fkData);
14518
- const keys = Object.keys(json2.tables[tableName].columns);
14519
- const filtered = columnsFrom.filter((it) => keys.includes(it));
14520
- const fullDrop = filtered.length === 0;
14521
- if (fullDrop) return;
14522
- let isMulticolumn = false;
14523
- if (columnsFrom.length > 1 || columnsTo.length > 1) {
14524
- isMulticolumn = true;
14525
- return {
14526
- type: "delete_reference",
14527
- tableName,
14528
- data: fkData,
14529
- schema: schema4,
14530
- isMulticolumn
14531
- };
14532
- }
14533
- const columnFrom = columnsFrom[0];
14534
- const newTableName = getNewTableName(tableFrom, meta);
14535
- const {
14536
- notNull: columnNotNull,
14537
- default: columnDefault,
14538
- type: columnType
14539
- } = json2.tables[newTableName].columns[columnFrom];
14540
- const fkToSquash = {
14541
- columnsFrom,
14542
- columnsTo,
14543
- name: name2,
14544
- tableFrom: newTableName,
14545
- tableTo,
14546
- onDelete,
14547
- onUpdate
14548
- };
14549
- const foreignKey = action === "push" ? SQLiteSquasher.squashPushFK(fkToSquash) : SQLiteSquasher.squashFK(fkToSquash);
14550
- return {
14551
- type: "delete_reference",
14552
- tableName,
14553
- data: foreignKey,
14554
- schema: schema4,
14555
- columnNotNull,
14556
- columnDefault,
14557
- columnType
14558
- };
14559
- });
14560
- return statements.filter((it) => it);
14561
- };
14562
14299
  prepareAlterReferencesJson = (tableName, schema4, foreignKeys) => {
14563
14300
  const stmts = [];
14564
14301
  Object.values(foreignKeys).map((val) => {
@@ -14712,292 +14449,10 @@ var init_jsonStatements = __esm({
14712
14449
  }
14713
14450
  });
14714
14451
 
14715
- // src/statementCombiner.ts
14716
- var prepareLibSQLRecreateTable, prepareSQLiteRecreateTable, libSQLCombineStatements, sqliteCombineStatements;
14717
- var init_statementCombiner = __esm({
14718
- "src/statementCombiner.ts"() {
14719
- "use strict";
14720
- init_jsonStatements();
14721
- init_sqliteSchema();
14722
- prepareLibSQLRecreateTable = (table4, action) => {
14723
- const { name: name2, columns, uniqueConstraints, indexes } = table4;
14724
- const composites = Object.values(table4.compositePrimaryKeys).map(
14725
- (it) => SQLiteSquasher.unsquashPK(it)
14726
- );
14727
- const references2 = Object.values(table4.foreignKeys);
14728
- const fks = references2.map(
14729
- (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14730
- );
14731
- const statements = [
14732
- {
14733
- type: "recreate_table",
14734
- tableName: name2,
14735
- columns: Object.values(columns),
14736
- compositePKs: composites,
14737
- referenceData: fks,
14738
- uniqueConstraints: Object.values(uniqueConstraints)
14739
- }
14740
- ];
14741
- if (Object.keys(indexes).length) {
14742
- statements.push(...prepareCreateIndexesJson(name2, "", indexes));
14743
- }
14744
- return statements;
14745
- };
14746
- prepareSQLiteRecreateTable = (table4, action) => {
14747
- const { name: name2, columns, uniqueConstraints, indexes } = table4;
14748
- const composites = Object.values(table4.compositePrimaryKeys).map(
14749
- (it) => SQLiteSquasher.unsquashPK(it)
14750
- );
14751
- const references2 = Object.values(table4.foreignKeys);
14752
- const fks = references2.map(
14753
- (it) => action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it)
14754
- );
14755
- const statements = [
14756
- {
14757
- type: "recreate_table",
14758
- tableName: name2,
14759
- columns: Object.values(columns),
14760
- compositePKs: composites,
14761
- referenceData: fks,
14762
- uniqueConstraints: Object.values(uniqueConstraints)
14763
- }
14764
- ];
14765
- if (Object.keys(indexes).length) {
14766
- statements.push(...prepareCreateIndexesJson(name2, "", indexes));
14767
- }
14768
- return statements;
14769
- };
14770
- libSQLCombineStatements = (statements, json2, action) => {
14771
- const newStatements = {};
14772
- for (const statement of statements) {
14773
- 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") {
14774
- const tableName2 = statement.tableName;
14775
- const statementsForTable2 = newStatements[tableName2];
14776
- if (!statementsForTable2) {
14777
- newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14778
- continue;
14779
- }
14780
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14781
- const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14782
- const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14783
- if (wasRename) {
14784
- newStatements[tableName2].push(...preparedStatements);
14785
- } else {
14786
- newStatements[tableName2] = preparedStatements;
14787
- }
14788
- continue;
14789
- }
14790
- continue;
14791
- }
14792
- 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") {
14793
- const { tableName: tableName2, columnName, columnPk } = statement;
14794
- const columnIsPartOfForeignKey = Object.values(
14795
- json2.tables[tableName2].foreignKeys
14796
- ).some((it) => {
14797
- const unsquashFk = action === "push" ? SQLiteSquasher.unsquashPushFK(it) : SQLiteSquasher.unsquashFK(it);
14798
- return unsquashFk.columnsFrom.includes(columnName);
14799
- });
14800
- const statementsForTable2 = newStatements[tableName2];
14801
- if (!statementsForTable2 && (columnIsPartOfForeignKey || columnPk)) {
14802
- newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14803
- continue;
14804
- }
14805
- if (statementsForTable2 && (columnIsPartOfForeignKey || columnPk)) {
14806
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14807
- const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14808
- const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14809
- if (wasRename) {
14810
- newStatements[tableName2].push(...preparedStatements);
14811
- } else {
14812
- newStatements[tableName2] = preparedStatements;
14813
- }
14814
- }
14815
- continue;
14816
- }
14817
- if (statementsForTable2 && !(columnIsPartOfForeignKey || columnPk)) {
14818
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14819
- newStatements[tableName2].push(statement);
14820
- }
14821
- continue;
14822
- }
14823
- newStatements[tableName2] = [statement];
14824
- continue;
14825
- }
14826
- if (statement.type === "create_reference") {
14827
- const tableName2 = statement.tableName;
14828
- const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
14829
- const statementsForTable2 = newStatements[tableName2];
14830
- if (!statementsForTable2) {
14831
- newStatements[tableName2] = statement.isMulticolumn ? prepareLibSQLRecreateTable(json2.tables[tableName2], action) : [statement];
14832
- continue;
14833
- }
14834
- if (!statement.isMulticolumn && statementsForTable2.some(
14835
- (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
14836
- )) {
14837
- continue;
14838
- }
14839
- if (statement.isMulticolumn) {
14840
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14841
- const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14842
- const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14843
- if (wasRename) {
14844
- newStatements[tableName2].push(...preparedStatements);
14845
- } else {
14846
- newStatements[tableName2] = preparedStatements;
14847
- }
14848
- continue;
14849
- }
14850
- continue;
14851
- }
14852
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14853
- newStatements[tableName2].push(statement);
14854
- }
14855
- continue;
14856
- }
14857
- if (statement.type === "delete_reference") {
14858
- const tableName2 = statement.tableName;
14859
- const statementsForTable2 = newStatements[tableName2];
14860
- if (!statementsForTable2) {
14861
- newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14862
- continue;
14863
- }
14864
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14865
- const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14866
- const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14867
- if (wasRename) {
14868
- newStatements[tableName2].push(...preparedStatements);
14869
- } else {
14870
- newStatements[tableName2] = preparedStatements;
14871
- }
14872
- continue;
14873
- }
14874
- continue;
14875
- }
14876
- if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
14877
- const tableName2 = statement.tableName;
14878
- const statementsForTable2 = newStatements[tableName2];
14879
- if (!statementsForTable2) {
14880
- newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14881
- continue;
14882
- }
14883
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14884
- const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14885
- const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14886
- if (wasRename) {
14887
- newStatements[tableName2].push(...preparedStatements);
14888
- } else {
14889
- newStatements[tableName2] = preparedStatements;
14890
- }
14891
- continue;
14892
- }
14893
- continue;
14894
- }
14895
- const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
14896
- const statementsForTable = newStatements[tableName];
14897
- if (!statementsForTable) {
14898
- newStatements[tableName] = [statement];
14899
- continue;
14900
- }
14901
- if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
14902
- newStatements[tableName].push(statement);
14903
- }
14904
- }
14905
- const combinedStatements = Object.values(newStatements).flat();
14906
- const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
14907
- const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
14908
- const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
14909
- return [...renamedTables, ...renamedColumns, ...rest];
14910
- };
14911
- sqliteCombineStatements = (statements, json2, action) => {
14912
- const newStatements = {};
14913
- for (const statement of statements) {
14914
- 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") {
14915
- const tableName2 = statement.tableName;
14916
- const statementsForTable2 = newStatements[tableName2];
14917
- if (!statementsForTable2) {
14918
- newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14919
- continue;
14920
- }
14921
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14922
- const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14923
- const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14924
- if (wasRename) {
14925
- newStatements[tableName2].push(...preparedStatements);
14926
- } else {
14927
- newStatements[tableName2] = preparedStatements;
14928
- }
14929
- continue;
14930
- }
14931
- continue;
14932
- }
14933
- if (statement.type === "sqlite_alter_table_add_column" && statement.column.primaryKey) {
14934
- const tableName2 = statement.tableName;
14935
- const statementsForTable2 = newStatements[tableName2];
14936
- if (!statementsForTable2) {
14937
- newStatements[tableName2] = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14938
- continue;
14939
- }
14940
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14941
- const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14942
- const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14943
- if (wasRename) {
14944
- newStatements[tableName2].push(...preparedStatements);
14945
- } else {
14946
- newStatements[tableName2] = preparedStatements;
14947
- }
14948
- continue;
14949
- }
14950
- continue;
14951
- }
14952
- if (statement.type === "create_reference") {
14953
- const tableName2 = statement.tableName;
14954
- const data = action === "push" ? SQLiteSquasher.unsquashPushFK(statement.data) : SQLiteSquasher.unsquashFK(statement.data);
14955
- const statementsForTable2 = newStatements[tableName2];
14956
- if (!statementsForTable2) {
14957
- newStatements[tableName2] = prepareSQLiteRecreateTable(json2.tables[tableName2], action);
14958
- continue;
14959
- }
14960
- if (data.columnsFrom.length === 1 && statementsForTable2.some(
14961
- (st) => st.type === "sqlite_alter_table_add_column" && st.column.name === data.columnsFrom[0]
14962
- )) {
14963
- continue;
14964
- }
14965
- if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
14966
- const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
14967
- const preparedStatements = prepareLibSQLRecreateTable(json2.tables[tableName2], action);
14968
- if (wasRename) {
14969
- newStatements[tableName2].push(...preparedStatements);
14970
- } else {
14971
- newStatements[tableName2] = preparedStatements;
14972
- }
14973
- continue;
14974
- }
14975
- continue;
14976
- }
14977
- const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
14978
- const statementsForTable = newStatements[tableName];
14979
- if (!statementsForTable) {
14980
- newStatements[tableName] = [statement];
14981
- continue;
14982
- }
14983
- if (!statementsForTable.some(({ type }) => type === "recreate_table")) {
14984
- newStatements[tableName].push(statement);
14985
- }
14986
- }
14987
- const combinedStatements = Object.values(newStatements).flat();
14988
- const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
14989
- const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
14990
- const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
14991
- return [...renamedTables, ...renamedColumns, ...rest];
14992
- };
14993
- }
14994
- });
14995
-
14996
14452
  // src/snapshotsDiffer.ts
14997
14453
  var snapshotsDiffer_exports = {};
14998
14454
  __export(snapshotsDiffer_exports, {
14999
14455
  alteredTableScheme: () => alteredTableScheme,
15000
- applyLibSQLSnapshotsDiff: () => applyLibSQLSnapshotsDiff,
15001
14456
  applyMysqlSnapshotsDiff: () => applyMysqlSnapshotsDiff,
15002
14457
  applyPgSnapshotsDiff: () => applyPgSnapshotsDiff,
15003
14458
  applySqliteSnapshotsDiff: () => applySqliteSnapshotsDiff,
@@ -15007,7 +14462,7 @@ __export(snapshotsDiffer_exports, {
15007
14462
  makePatched: () => makePatched,
15008
14463
  makeSelfOrPatched: () => makeSelfOrPatched
15009
14464
  });
15010
- var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff, applyLibSQLSnapshotsDiff;
14465
+ var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff;
15011
14466
  var init_snapshotsDiffer = __esm({
15012
14467
  "src/snapshotsDiffer.ts"() {
15013
14468
  "use strict";
@@ -15019,7 +14474,6 @@ var init_snapshotsDiffer = __esm({
15019
14474
  init_mysqlSchema();
15020
14475
  init_pgSchema();
15021
14476
  init_sqliteSchema();
15022
- init_statementCombiner();
15023
14477
  init_utils();
15024
14478
  makeChanged = (schema4) => {
15025
14479
  return objectType({
@@ -15906,339 +15360,40 @@ var init_snapshotsDiffer = __esm({
15906
15360
  const jsonAddedUniqueConstraints = [];
15907
15361
  const jsonDeletedUniqueConstraints = [];
15908
15362
  const jsonAlteredUniqueConstraints = [];
15909
- const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
15910
- const jsonAddColumnsStatemets = columnCreates.map((it) => _prepareAddColumns(it.table, "", it.columns)).flat();
15911
- const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
15912
- alteredTables.forEach((it) => {
15913
- let addedColumns = [];
15914
- for (const addedPkName of Object.keys(it.addedCompositePKs)) {
15915
- const addedPkColumns = it.addedCompositePKs[addedPkName];
15916
- addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
15917
- }
15918
- let deletedColumns = [];
15919
- for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
15920
- const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
15921
- deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
15922
- }
15923
- const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
15924
- let addedCompositePKs = [];
15925
- let deletedCompositePKs = [];
15926
- let alteredCompositePKs = [];
15927
- addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
15928
- it.name,
15929
- it.addedCompositePKs,
15930
- prevFull,
15931
- curFull
15932
- );
15933
- deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15934
- it.name,
15935
- it.deletedCompositePKs,
15936
- prevFull
15937
- );
15938
- alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
15939
- it.name,
15940
- it.alteredCompositePKs,
15941
- prevFull,
15942
- curFull
15943
- );
15944
- let addedUniqueConstraints = [];
15945
- let deletedUniqueConstraints = [];
15946
- let alteredUniqueConstraints = [];
15947
- addedUniqueConstraints = prepareAddUniqueConstraintPg(
15948
- it.name,
15949
- it.schema,
15950
- it.addedUniqueConstraints
15951
- );
15952
- deletedUniqueConstraints = prepareDeleteUniqueConstraintPg(
15953
- it.name,
15954
- it.schema,
15955
- it.deletedUniqueConstraints
15956
- );
15957
- if (it.alteredUniqueConstraints) {
15958
- const added = {};
15959
- const deleted = {};
15960
- for (const k of Object.keys(it.alteredUniqueConstraints)) {
15961
- added[k] = it.alteredUniqueConstraints[k].__new;
15962
- deleted[k] = it.alteredUniqueConstraints[k].__old;
15963
- }
15964
- addedUniqueConstraints.push(
15965
- ...prepareAddUniqueConstraintPg(it.name, it.schema, added)
15966
- );
15967
- deletedUniqueConstraints.push(
15968
- ...prepareDeleteUniqueConstraintPg(it.name, it.schema, deleted)
15969
- );
15970
- }
15971
- jsonAddedCompositePKs.push(...addedCompositePKs);
15972
- jsonDeletedCompositePKs.push(...deletedCompositePKs);
15973
- jsonAlteredCompositePKs.push(...alteredCompositePKs);
15974
- jsonAddedUniqueConstraints.push(...addedUniqueConstraints);
15975
- jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15976
- jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15977
- });
15978
- const rColumns = jsonRenameColumnsStatements.map((it) => {
15979
- const tableName = it.tableName;
15980
- const schema4 = it.schema;
15981
- return {
15982
- from: { schema: schema4, table: tableName, column: it.oldColumnName },
15983
- to: { schema: schema4, table: tableName, column: it.newColumnName }
15984
- };
15985
- });
15986
- const jsonTableAlternations = alteredTables.map((it) => {
15987
- return prepareAlterColumnsMysql(
15988
- it.name,
15989
- it.schema,
15990
- it.altered,
15991
- json1,
15992
- json2,
15993
- action
15994
- );
15995
- }).flat();
15996
- const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
15997
- return prepareCreateIndexesJson(
15998
- it.name,
15999
- it.schema,
16000
- it.addedIndexes || {},
16001
- curFull.internal
16002
- );
16003
- }).flat();
16004
- const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16005
- return prepareDropIndexesJson(
16006
- it.name,
16007
- it.schema,
16008
- it.deletedIndexes || {}
16009
- );
16010
- }).flat();
16011
- alteredTables.forEach((it) => {
16012
- const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
16013
- (current, item) => {
16014
- current[item] = it.alteredIndexes[item].__old;
16015
- return current;
16016
- },
16017
- {}
16018
- );
16019
- const createdIndexes = Object.keys(it.alteredIndexes).reduce(
16020
- (current, item) => {
16021
- current[item] = it.alteredIndexes[item].__new;
16022
- return current;
16023
- },
16024
- {}
16025
- );
16026
- jsonCreateIndexesForAllAlteredTables.push(
16027
- ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16028
- );
16029
- jsonDropIndexesForAllAlteredTables.push(
16030
- ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
16031
- );
16032
- });
16033
- const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
16034
- return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
16035
- }).flat();
16036
- const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16037
- const forAdded = prepareCreateReferencesJson(
16038
- it.name,
16039
- it.schema,
16040
- it.addedForeignKeys
16041
- );
16042
- const forAltered = prepareDropReferencesJson(
16043
- it.name,
16044
- it.schema,
16045
- it.deletedForeignKeys
16046
- );
16047
- const alteredFKs = prepareAlterReferencesJson(
16048
- it.name,
16049
- it.schema,
16050
- it.alteredForeignKeys
16051
- );
16052
- return [...forAdded, ...forAltered, ...alteredFKs];
16053
- }).flat();
16054
- const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16055
- (t) => t.type === "create_reference"
16056
- );
16057
- const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16058
- (t) => t.type === "delete_reference"
16059
- );
16060
- const jsonMySqlCreateTables = createdTables.map((it) => {
16061
- return prepareMySqlCreateTableJson(
16062
- it,
16063
- curFull,
16064
- curFull.internal
16065
- );
16066
- });
16067
- jsonStatements.push(...jsonMySqlCreateTables);
16068
- jsonStatements.push(...jsonDropTables);
16069
- jsonStatements.push(...jsonRenameTables);
16070
- jsonStatements.push(...jsonRenameColumnsStatements);
16071
- jsonStatements.push(...jsonDeletedUniqueConstraints);
16072
- jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
16073
- jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
16074
- jsonStatements.push(...jsonDeletedCompositePKs);
16075
- jsonStatements.push(...jsonTableAlternations);
16076
- jsonStatements.push(...jsonAddedCompositePKs);
16077
- jsonStatements.push(...jsonAddedUniqueConstraints);
16078
- jsonStatements.push(...jsonDeletedUniqueConstraints);
16079
- jsonStatements.push(...jsonAddColumnsStatemets);
16080
- jsonStatements.push(...jsonCreateReferencesForCreatedTables);
16081
- jsonStatements.push(...jsonCreateIndexesForCreatedTables);
16082
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
16083
- jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16084
- jsonStatements.push(...jsonDropColumnsStatemets);
16085
- jsonStatements.push(...jsonAlteredCompositePKs);
16086
- jsonStatements.push(...jsonAddedUniqueConstraints);
16087
- jsonStatements.push(...jsonAlteredUniqueConstraints);
16088
- const sqlStatements = fromJson(jsonStatements, "mysql");
16089
- const uniqueSqlStatements = [];
16090
- sqlStatements.forEach((ss) => {
16091
- if (!uniqueSqlStatements.includes(ss)) {
16092
- uniqueSqlStatements.push(ss);
16093
- }
16094
- });
16095
- const rTables = renamedTables.map((it) => {
16096
- return { from: it.from, to: it.to };
16097
- });
16098
- const _meta = prepareMigrationMeta([], rTables, rColumns);
16099
- return {
16100
- statements: jsonStatements,
16101
- sqlStatements: uniqueSqlStatements,
16102
- _meta
16103
- };
16104
- };
16105
- applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16106
- const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
16107
- const {
16108
- created: createdTables,
16109
- deleted: deletedTables,
16110
- renamed: renamedTables
16111
- } = await tablesResolver2({
16112
- created: tablesDiff.added,
16113
- deleted: tablesDiff.deleted
16114
- });
16115
- const tablesPatchedSnap1 = copy(json1);
16116
- tablesPatchedSnap1.tables = mapEntries(tablesPatchedSnap1.tables, (_2, it) => {
16117
- const { name: name2 } = nameChangeFor(it, renamedTables);
16118
- it.name = name2;
16119
- return [name2, it];
16120
- });
16121
- const res = diffColumns(tablesPatchedSnap1.tables, json2.tables);
16122
- const columnRenames = [];
16123
- const columnCreates = [];
16124
- const columnDeletes = [];
16125
- for (let entry of Object.values(res)) {
16126
- const { renamed, created, deleted } = await columnsResolver2({
16127
- tableName: entry.name,
16128
- schema: entry.schema,
16129
- deleted: entry.columns.deleted,
16130
- created: entry.columns.added
16131
- });
16132
- if (created.length > 0) {
16133
- columnCreates.push({
16134
- table: entry.name,
16135
- columns: created
16136
- });
16137
- }
16138
- if (deleted.length > 0) {
16139
- columnDeletes.push({
16140
- table: entry.name,
16141
- columns: deleted
16142
- });
16143
- }
16144
- if (renamed.length > 0) {
16145
- columnRenames.push({
16146
- table: entry.name,
16147
- renames: renamed
16148
- });
16149
- }
16150
- }
16151
- const columnRenamesDict = columnRenames.reduce(
16152
- (acc, it) => {
16153
- acc[it.table] = it.renames;
16154
- return acc;
16155
- },
16156
- {}
16157
- );
16158
- const columnsPatchedSnap1 = copy(tablesPatchedSnap1);
16159
- columnsPatchedSnap1.tables = mapEntries(
16160
- columnsPatchedSnap1.tables,
16161
- (tableKey2, tableValue) => {
16162
- const patchedColumns = mapKeys(
16163
- tableValue.columns,
16164
- (columnKey, column4) => {
16165
- const rens = columnRenamesDict[tableValue.name] || [];
16166
- const newName = columnChangeFor(columnKey, rens);
16167
- column4.name = newName;
16168
- return newName;
16169
- }
16170
- );
16171
- tableValue.columns = patchedColumns;
16172
- return [tableKey2, tableValue];
16173
- }
16174
- );
16175
- const diffResult = applyJsonDiff(columnsPatchedSnap1, json2);
16176
- const typedResult = diffResultSchemeSQLite.parse(diffResult);
16177
- const tablesMap = {};
16178
- typedResult.alteredTablesWithColumns.forEach((obj) => {
16179
- tablesMap[obj.name] = obj;
16180
- });
16181
- const jsonCreateTables = createdTables.map((it) => {
16182
- return prepareSQLiteCreateTable(it, action);
16183
- });
16184
- const jsonCreateIndexesForCreatedTables = createdTables.map((it) => {
16185
- return prepareCreateIndexesJson(
16186
- it.name,
16187
- it.schema,
16188
- it.indexes,
16189
- curFull.internal
16190
- );
16191
- }).flat();
16192
- const jsonDropTables = deletedTables.map((it) => {
16193
- return prepareDropTableJson(it);
16194
- });
16195
- const jsonRenameTables = renamedTables.map((it) => {
16196
- return prepareRenameTableJson(it.from, it.to);
16197
- });
16198
- const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
16199
- const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
16200
- const jsonAddColumnsStatemets = columnCreates.map((it) => {
16201
- return _prepareSqliteAddColumns(
16202
- it.table,
16203
- it.columns,
16204
- tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
16205
- );
16206
- }).flat();
16207
- const allAltered = typedResult.alteredTablesWithColumns;
16208
- const jsonAddedCompositePKs = [];
16209
- const jsonDeletedCompositePKs = [];
16210
- const jsonAlteredCompositePKs = [];
16211
- const jsonAddedUniqueConstraints = [];
16212
- const jsonDeletedUniqueConstraints = [];
16213
- const jsonAlteredUniqueConstraints = [];
16214
- allAltered.forEach((it) => {
15363
+ const jsonRenameColumnsStatements = columnRenames.map((it) => prepareRenameColumns(it.table, "", it.renames)).flat();
15364
+ const jsonAddColumnsStatemets = columnCreates.map((it) => _prepareAddColumns(it.table, "", it.columns)).flat();
15365
+ const jsonDropColumnsStatemets = columnDeletes.map((it) => _prepareDropColumns(it.table, "", it.columns)).flat();
15366
+ alteredTables.forEach((it) => {
16215
15367
  let addedColumns = [];
16216
15368
  for (const addedPkName of Object.keys(it.addedCompositePKs)) {
16217
15369
  const addedPkColumns = it.addedCompositePKs[addedPkName];
16218
- addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
15370
+ addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
16219
15371
  }
16220
15372
  let deletedColumns = [];
16221
15373
  for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
16222
15374
  const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
16223
- deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
15375
+ deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
16224
15376
  }
16225
15377
  const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
16226
15378
  let addedCompositePKs = [];
16227
15379
  let deletedCompositePKs = [];
16228
15380
  let alteredCompositePKs = [];
16229
- if (doPerformDeleteAndCreate) {
16230
- addedCompositePKs = prepareAddCompositePrimaryKeySqlite(
16231
- it.name,
16232
- it.addedCompositePKs
16233
- );
16234
- deletedCompositePKs = prepareDeleteCompositePrimaryKeySqlite(
16235
- it.name,
16236
- it.deletedCompositePKs
16237
- );
16238
- }
16239
- alteredCompositePKs = prepareAlterCompositePrimaryKeySqlite(
15381
+ addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
16240
15382
  it.name,
16241
- it.alteredCompositePKs
15383
+ it.addedCompositePKs,
15384
+ prevFull,
15385
+ curFull
15386
+ );
15387
+ deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15388
+ it.name,
15389
+ it.deletedCompositePKs,
15390
+ prevFull
15391
+ );
15392
+ alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
15393
+ it.name,
15394
+ it.alteredCompositePKs,
15395
+ prevFull,
15396
+ curFull
16242
15397
  );
16243
15398
  let addedUniqueConstraints = [];
16244
15399
  let deletedUniqueConstraints = [];
@@ -16282,10 +15437,17 @@ var init_snapshotsDiffer = __esm({
16282
15437
  to: { schema: schema4, table: tableName, column: it.newColumnName }
16283
15438
  };
16284
15439
  });
16285
- const jsonTableAlternations = allAltered.map((it) => {
16286
- return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15440
+ const jsonTableAlternations = alteredTables.map((it) => {
15441
+ return prepareAlterColumnsMysql(
15442
+ it.name,
15443
+ it.schema,
15444
+ it.altered,
15445
+ json1,
15446
+ json2,
15447
+ action
15448
+ );
16287
15449
  }).flat();
16288
- const jsonCreateIndexesForAllAlteredTables = allAltered.map((it) => {
15450
+ const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
16289
15451
  return prepareCreateIndexesJson(
16290
15452
  it.name,
16291
15453
  it.schema,
@@ -16293,14 +15455,14 @@ var init_snapshotsDiffer = __esm({
16293
15455
  curFull.internal
16294
15456
  );
16295
15457
  }).flat();
16296
- const jsonDropIndexesForAllAlteredTables = allAltered.map((it) => {
15458
+ const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16297
15459
  return prepareDropIndexesJson(
16298
15460
  it.name,
16299
15461
  it.schema,
16300
15462
  it.deletedIndexes || {}
16301
15463
  );
16302
15464
  }).flat();
16303
- allAltered.forEach((it) => {
15465
+ alteredTables.forEach((it) => {
16304
15466
  const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
16305
15467
  (current, item) => {
16306
15468
  current[item] = it.alteredIndexes[item].__old;
@@ -16316,18 +15478,16 @@ var init_snapshotsDiffer = __esm({
16316
15478
  {}
16317
15479
  );
16318
15480
  jsonCreateIndexesForAllAlteredTables.push(
16319
- ...prepareCreateIndexesJson(
16320
- it.name,
16321
- it.schema,
16322
- createdIndexes || {},
16323
- curFull.internal
16324
- )
15481
+ ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16325
15482
  );
16326
15483
  jsonDropIndexesForAllAlteredTables.push(
16327
15484
  ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
16328
15485
  );
16329
15486
  });
16330
- const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15487
+ const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
15488
+ return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
15489
+ }).flat();
15490
+ const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16331
15491
  const forAdded = prepareCreateReferencesJson(
16332
15492
  it.name,
16333
15493
  it.schema,
@@ -16351,25 +15511,35 @@ var init_snapshotsDiffer = __esm({
16351
15511
  const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
16352
15512
  (t) => t.type === "delete_reference"
16353
15513
  );
16354
- const jsonStatements = [];
16355
- jsonStatements.push(...jsonCreateTables);
15514
+ const jsonMySqlCreateTables = createdTables.map((it) => {
15515
+ return prepareMySqlCreateTableJson(
15516
+ it,
15517
+ curFull,
15518
+ curFull.internal
15519
+ );
15520
+ });
15521
+ jsonStatements.push(...jsonMySqlCreateTables);
16356
15522
  jsonStatements.push(...jsonDropTables);
16357
15523
  jsonStatements.push(...jsonRenameTables);
16358
15524
  jsonStatements.push(...jsonRenameColumnsStatements);
15525
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16359
15526
  jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
16360
15527
  jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
16361
15528
  jsonStatements.push(...jsonDeletedCompositePKs);
16362
15529
  jsonStatements.push(...jsonTableAlternations);
16363
15530
  jsonStatements.push(...jsonAddedCompositePKs);
15531
+ jsonStatements.push(...jsonAddedUniqueConstraints);
15532
+ jsonStatements.push(...jsonDeletedUniqueConstraints);
16364
15533
  jsonStatements.push(...jsonAddColumnsStatemets);
15534
+ jsonStatements.push(...jsonCreateReferencesForCreatedTables);
16365
15535
  jsonStatements.push(...jsonCreateIndexesForCreatedTables);
16366
- jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16367
15536
  jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15537
+ jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16368
15538
  jsonStatements.push(...jsonDropColumnsStatemets);
16369
15539
  jsonStatements.push(...jsonAlteredCompositePKs);
15540
+ jsonStatements.push(...jsonAddedUniqueConstraints);
16370
15541
  jsonStatements.push(...jsonAlteredUniqueConstraints);
16371
- const combinedJsonStatements = sqliteCombineStatements(jsonStatements, json2, action);
16372
- const sqlStatements = fromJson(combinedJsonStatements, "sqlite");
15542
+ const sqlStatements = fromJson(jsonStatements, "mysql");
16373
15543
  const uniqueSqlStatements = [];
16374
15544
  sqlStatements.forEach((ss) => {
16375
15545
  if (!uniqueSqlStatements.includes(ss)) {
@@ -16381,12 +15551,12 @@ var init_snapshotsDiffer = __esm({
16381
15551
  });
16382
15552
  const _meta = prepareMigrationMeta([], rTables, rColumns);
16383
15553
  return {
16384
- statements: combinedJsonStatements,
15554
+ statements: jsonStatements,
16385
15555
  sqlStatements: uniqueSqlStatements,
16386
15556
  _meta
16387
15557
  };
16388
15558
  };
16389
- applyLibSQLSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
15559
+ applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16390
15560
  const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
16391
15561
  const {
16392
15562
  created: createdTables,
@@ -16488,18 +15658,6 @@ var init_snapshotsDiffer = __esm({
16488
15658
  tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
16489
15659
  );
16490
15660
  }).flat();
16491
- const rColumns = jsonRenameColumnsStatements.map((it) => {
16492
- const tableName = it.tableName;
16493
- const schema4 = it.schema;
16494
- return {
16495
- from: { schema: schema4, table: tableName, column: it.oldColumnName },
16496
- to: { schema: schema4, table: tableName, column: it.newColumnName }
16497
- };
16498
- });
16499
- const rTables = renamedTables.map((it) => {
16500
- return { from: it.from, to: it.to };
16501
- });
16502
- const _meta = prepareMigrationMeta([], rTables, rColumns);
16503
15661
  const allAltered = typedResult.alteredTablesWithColumns;
16504
15662
  const jsonAddedCompositePKs = [];
16505
15663
  const jsonDeletedCompositePKs = [];
@@ -16570,6 +15728,14 @@ var init_snapshotsDiffer = __esm({
16570
15728
  jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
16571
15729
  jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
16572
15730
  });
15731
+ const rColumns = jsonRenameColumnsStatements.map((it) => {
15732
+ const tableName = it.tableName;
15733
+ const schema4 = it.schema;
15734
+ return {
15735
+ from: { schema: schema4, table: tableName, column: it.oldColumnName },
15736
+ to: { schema: schema4, table: tableName, column: it.newColumnName }
15737
+ };
15738
+ });
16573
15739
  const jsonTableAlternations = allAltered.map((it) => {
16574
15740
  return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
16575
15741
  }).flat();
@@ -16616,22 +15782,21 @@ var init_snapshotsDiffer = __esm({
16616
15782
  );
16617
15783
  });
16618
15784
  const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
16619
- const forAdded = prepareLibSQLCreateReferencesJson(
15785
+ const forAdded = prepareCreateReferencesJson(
16620
15786
  it.name,
16621
15787
  it.schema,
16622
- it.addedForeignKeys,
16623
- json2,
16624
- action
15788
+ it.addedForeignKeys
16625
15789
  );
16626
- const forAltered = prepareLibSQLDropReferencesJson(
15790
+ const forAltered = prepareDropReferencesJson(
16627
15791
  it.name,
16628
15792
  it.schema,
16629
- it.deletedForeignKeys,
16630
- json2,
16631
- _meta,
16632
- action
15793
+ it.deletedForeignKeys
15794
+ );
15795
+ const alteredFKs = prepareAlterReferencesJson(
15796
+ it.name,
15797
+ it.schema,
15798
+ it.alteredForeignKeys
16633
15799
  );
16634
- const alteredFKs = prepareAlterReferencesJson(it.name, it.schema, it.alteredForeignKeys);
16635
15800
  return [...forAdded, ...forAltered, ...alteredFKs];
16636
15801
  }).flat();
16637
15802
  const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
@@ -16657,21 +15822,19 @@ var init_snapshotsDiffer = __esm({
16657
15822
  jsonStatements.push(...jsonDropColumnsStatemets);
16658
15823
  jsonStatements.push(...jsonAlteredCompositePKs);
16659
15824
  jsonStatements.push(...jsonAlteredUniqueConstraints);
16660
- const combinedJsonStatements = libSQLCombineStatements(jsonStatements, json2, action);
16661
- const sqlStatements = fromJson(
16662
- combinedJsonStatements,
16663
- "turso",
16664
- action,
16665
- json2
16666
- );
15825
+ const sqlStatements = fromJson(jsonStatements, "sqlite");
16667
15826
  const uniqueSqlStatements = [];
16668
15827
  sqlStatements.forEach((ss) => {
16669
15828
  if (!uniqueSqlStatements.includes(ss)) {
16670
15829
  uniqueSqlStatements.push(ss);
16671
15830
  }
16672
15831
  });
15832
+ const rTables = renamedTables.map((it) => {
15833
+ return { from: it.from, to: it.to };
15834
+ });
15835
+ const _meta = prepareMigrationMeta([], rTables, rColumns);
16673
15836
  return {
16674
- statements: combinedJsonStatements,
15837
+ statements: jsonStatements,
16675
15838
  sqlStatements: uniqueSqlStatements,
16676
15839
  _meta
16677
15840
  };
@@ -16695,7 +15858,7 @@ var init_schemaValidator = __esm({
16695
15858
  init_mysqlSchema();
16696
15859
  init_pgSchema();
16697
15860
  init_sqliteSchema();
16698
- dialects = ["postgresql", "mysql", "sqlite", "turso"];
15861
+ dialects = ["postgresql", "mysql", "sqlite"];
16699
15862
  dialect3 = enumType(dialects);
16700
15863
  commonSquashedSchema = unionType([
16701
15864
  pgSchemaSquashed,
@@ -16715,6 +15878,7 @@ var init_common = __esm({
16715
15878
  init_schemaValidator();
16716
15879
  init_outputs();
16717
15880
  sqliteDriversLiterals = [
15881
+ literalType("turso"),
16718
15882
  literalType("d1-http"),
16719
15883
  literalType("expo")
16720
15884
  ];
@@ -34768,8 +33932,7 @@ var init_cli = __esm({
34768
33932
  schemaFilter: unionType([stringType(), stringType().array()]).optional().default(["public"]),
34769
33933
  extensionsFilters: literalType("postgis").array().optional(),
34770
33934
  verbose: booleanType().optional(),
34771
- strict: booleanType().optional(),
34772
- driver: driver.optional()
33935
+ strict: booleanType().optional()
34773
33936
  }).passthrough();
34774
33937
  pullParams = objectType({
34775
33938
  config: stringType().optional(),
@@ -34797,21 +33960,6 @@ var init_cli = __esm({
34797
33960
  }
34798
33961
  });
34799
33962
 
34800
- // src/cli/validations/libsql.ts
34801
- var libSQLCredentials;
34802
- var init_libsql = __esm({
34803
- "src/cli/validations/libsql.ts"() {
34804
- "use strict";
34805
- init_lib();
34806
- init_views();
34807
- init_common();
34808
- libSQLCredentials = objectType({
34809
- url: stringType().min(1),
34810
- authToken: stringType().min(1).optional()
34811
- });
34812
- }
34813
- });
34814
-
34815
33963
  // src/cli/validations/mysql.ts
34816
33964
  var mysqlCredentials;
34817
33965
  var init_mysql = __esm({
@@ -34983,8 +34131,6 @@ var init_utils8 = __esm({
34983
34131
  init_serializer();
34984
34132
  init_cli();
34985
34133
  init_common();
34986
- init_libsql();
34987
- init_libsql();
34988
34134
  init_mysql();
34989
34135
  init_outputs();
34990
34136
  init_postgres();
@@ -35942,8 +35088,284 @@ var sqlitePushIntrospect = async (db, filters) => {
35942
35088
  return { schema: schema4 };
35943
35089
  };
35944
35090
 
35091
+ // src/cli/commands/sqlitePushUtils.ts
35092
+ init_source();
35093
+ init_sqliteSchema();
35094
+ init_sqlgenerator();
35095
+ var _moveDataStatements = (tableName, json, dataLoss = false) => {
35096
+ const statements = [];
35097
+ statements.push(
35098
+ new SqliteRenameTableConvertor().convert({
35099
+ type: "rename_table",
35100
+ tableNameFrom: tableName,
35101
+ tableNameTo: `__old_push_${tableName}`,
35102
+ fromSchema: "",
35103
+ toSchema: ""
35104
+ })
35105
+ );
35106
+ const tableColumns = Object.values(json.tables[tableName].columns);
35107
+ const referenceData = Object.values(json.tables[tableName].foreignKeys);
35108
+ const compositePKs = Object.values(
35109
+ json.tables[tableName].compositePrimaryKeys
35110
+ ).map((it) => SQLiteSquasher.unsquashPK(it));
35111
+ const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
35112
+ statements.push(
35113
+ new SQLiteCreateTableConvertor().convert({
35114
+ type: "sqlite_create_table",
35115
+ tableName,
35116
+ columns: tableColumns,
35117
+ referenceData: fks,
35118
+ compositePKs
35119
+ })
35120
+ );
35121
+ if (!dataLoss) {
35122
+ statements.push(
35123
+ `INSERT INTO "${tableName}" SELECT * FROM "__old_push_${tableName}";`
35124
+ );
35125
+ }
35126
+ statements.push(
35127
+ new SQLiteDropTableConvertor().convert({
35128
+ type: "drop_table",
35129
+ tableName: `__old_push_${tableName}`,
35130
+ schema: ""
35131
+ })
35132
+ );
35133
+ for (const idx of Object.values(json.tables[tableName].indexes)) {
35134
+ statements.push(
35135
+ new CreateSqliteIndexConvertor().convert({
35136
+ type: "create_index",
35137
+ tableName,
35138
+ schema: "",
35139
+ data: idx
35140
+ })
35141
+ );
35142
+ }
35143
+ return statements;
35144
+ };
35145
+ var getOldTableName = (tableName, meta) => {
35146
+ for (const key of Object.keys(meta.tables)) {
35147
+ const value = meta.tables[key];
35148
+ if (`"${tableName}"` === value) {
35149
+ return key.substring(1, key.length - 1);
35150
+ }
35151
+ }
35152
+ return tableName;
35153
+ };
35154
+ var getNewTableName = (tableName, meta) => {
35155
+ if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
35156
+ return meta.tables[`"${tableName}"`].substring(
35157
+ 1,
35158
+ meta.tables[`"${tableName}"`].length - 1
35159
+ );
35160
+ }
35161
+ return tableName;
35162
+ };
35163
+ var logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
35164
+ let shouldAskForApprove = false;
35165
+ const statementsToExecute = [];
35166
+ const infoToPrint = [];
35167
+ const tablesToRemove = [];
35168
+ const columnsToRemove = [];
35169
+ const schemasToRemove = [];
35170
+ const tablesToTruncate = [];
35171
+ const tablesContext = {};
35172
+ for (const statement of statements) {
35173
+ if (statement.type === "drop_table") {
35174
+ const res = await connection.query(
35175
+ `select count(*) as count from \`${statement.tableName}\``
35176
+ );
35177
+ const count2 = Number(res[0].count);
35178
+ if (count2 > 0) {
35179
+ infoToPrint.push(
35180
+ `\xB7 You're about to delete ${source_default.underline(
35181
+ statement.tableName
35182
+ )} table with ${count2} items`
35183
+ );
35184
+ tablesToRemove.push(statement.tableName);
35185
+ shouldAskForApprove = true;
35186
+ }
35187
+ const stmnt = fromJson([statement], "sqlite")[0];
35188
+ statementsToExecute.push(stmnt);
35189
+ } else if (statement.type === "alter_table_drop_column") {
35190
+ const newTableName = getOldTableName(statement.tableName, meta);
35191
+ const columnIsPartOfPk = Object.values(
35192
+ json1.tables[newTableName].compositePrimaryKeys
35193
+ ).find((c) => SQLiteSquasher.unsquashPK(c).includes(statement.columnName));
35194
+ const columnIsPartOfIndex = Object.values(
35195
+ json1.tables[newTableName].indexes
35196
+ ).find((c) => SQLiteSquasher.unsquashIdx(c).columns.includes(statement.columnName));
35197
+ const columnIsPk = json1.tables[newTableName].columns[statement.columnName].primaryKey;
35198
+ const columnIsPartOfFk = Object.values(
35199
+ json1.tables[newTableName].foreignKeys
35200
+ ).find(
35201
+ (t) => SQLiteSquasher.unsquashPushFK(t).columnsFrom.includes(
35202
+ statement.columnName
35203
+ )
35204
+ );
35205
+ const res = await connection.query(
35206
+ `select count(*) as count from \`${newTableName}\``
35207
+ );
35208
+ const count2 = Number(res[0].count);
35209
+ if (count2 > 0) {
35210
+ infoToPrint.push(
35211
+ `\xB7 You're about to delete ${source_default.underline(
35212
+ statement.columnName
35213
+ )} column in ${newTableName} table with ${count2} items`
35214
+ );
35215
+ columnsToRemove.push(`${newTableName}_${statement.columnName}`);
35216
+ shouldAskForApprove = true;
35217
+ }
35218
+ if (columnIsPk || columnIsPartOfPk || columnIsPartOfIndex || columnIsPartOfFk) {
35219
+ tablesContext[newTableName] = [
35220
+ ..._moveDataStatements(statement.tableName, json2, true)
35221
+ ];
35222
+ const tablesReferncingCurrent = [];
35223
+ for (const table4 of Object.values(json1.tables)) {
35224
+ const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35225
+ (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35226
+ ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35227
+ tablesReferncingCurrent.push(...tablesRefs);
35228
+ }
35229
+ const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35230
+ for (const table4 of uniqueTableRefs) {
35231
+ if (typeof tablesContext[table4] === "undefined") {
35232
+ tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35233
+ }
35234
+ }
35235
+ } else {
35236
+ if (typeof tablesContext[newTableName] === "undefined") {
35237
+ const stmnt = fromJson([statement], "sqlite")[0];
35238
+ statementsToExecute.push(stmnt);
35239
+ }
35240
+ }
35241
+ } else if (statement.type === "sqlite_alter_table_add_column") {
35242
+ const newTableName = getOldTableName(statement.tableName, meta);
35243
+ if (statement.column.notNull && !statement.column.default) {
35244
+ const res = await connection.query(
35245
+ `select count(*) as count from \`${newTableName}\``
35246
+ );
35247
+ const count2 = Number(res[0].count);
35248
+ if (count2 > 0) {
35249
+ infoToPrint.push(
35250
+ `\xB7 You're about to add not-null ${source_default.underline(
35251
+ statement.column.name
35252
+ )} column without default value, which contains ${count2} items`
35253
+ );
35254
+ tablesToTruncate.push(newTableName);
35255
+ statementsToExecute.push(`delete from ${newTableName};`);
35256
+ shouldAskForApprove = true;
35257
+ }
35258
+ }
35259
+ if (statement.column.primaryKey) {
35260
+ tablesContext[newTableName] = [
35261
+ ..._moveDataStatements(statement.tableName, json2, true)
35262
+ ];
35263
+ const tablesReferncingCurrent = [];
35264
+ for (const table4 of Object.values(json1.tables)) {
35265
+ const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35266
+ (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35267
+ ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35268
+ tablesReferncingCurrent.push(...tablesRefs);
35269
+ }
35270
+ const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35271
+ for (const table4 of uniqueTableRefs) {
35272
+ if (typeof tablesContext[table4] === "undefined") {
35273
+ tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35274
+ }
35275
+ }
35276
+ } else {
35277
+ if (typeof tablesContext[newTableName] === "undefined") {
35278
+ const stmnt = fromJson([statement], "sqlite")[0];
35279
+ statementsToExecute.push(stmnt);
35280
+ }
35281
+ }
35282
+ } 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") {
35283
+ if (!(statement.type === "alter_table_alter_column_set_notnull" && statement.columnPk)) {
35284
+ const newTableName = getOldTableName(statement.tableName, meta);
35285
+ if (statement.type === "alter_table_alter_column_set_notnull" && typeof statement.columnDefault === "undefined") {
35286
+ const res = await connection.query(
35287
+ `select count(*) as count from \`${newTableName}\``
35288
+ );
35289
+ const count2 = Number(res[0].count);
35290
+ if (count2 > 0) {
35291
+ infoToPrint.push(
35292
+ `\xB7 You're about to add not-null constraint to ${source_default.underline(
35293
+ statement.columnName
35294
+ )} column without default value, which contains ${count2} items`
35295
+ );
35296
+ tablesToTruncate.push(newTableName);
35297
+ shouldAskForApprove = true;
35298
+ }
35299
+ tablesContext[newTableName] = _moveDataStatements(
35300
+ statement.tableName,
35301
+ json1,
35302
+ true
35303
+ );
35304
+ } else {
35305
+ if (typeof tablesContext[newTableName] === "undefined") {
35306
+ tablesContext[newTableName] = _moveDataStatements(
35307
+ statement.tableName,
35308
+ json1
35309
+ );
35310
+ }
35311
+ }
35312
+ const tablesReferncingCurrent = [];
35313
+ for (const table4 of Object.values(json1.tables)) {
35314
+ const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35315
+ (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35316
+ ).map((t) => {
35317
+ return getNewTableName(
35318
+ SQLiteSquasher.unsquashPushFK(t).tableFrom,
35319
+ meta
35320
+ );
35321
+ });
35322
+ tablesReferncingCurrent.push(...tablesRefs);
35323
+ }
35324
+ const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35325
+ for (const table4 of uniqueTableRefs) {
35326
+ if (typeof tablesContext[table4] === "undefined") {
35327
+ tablesContext[table4] = [..._moveDataStatements(table4, json1)];
35328
+ }
35329
+ }
35330
+ }
35331
+ } else if (statement.type === "create_reference" || statement.type === "delete_reference" || statement.type === "alter_reference") {
35332
+ const fk4 = SQLiteSquasher.unsquashPushFK(statement.data);
35333
+ if (typeof tablesContext[statement.tableName] === "undefined") {
35334
+ tablesContext[statement.tableName] = _moveDataStatements(
35335
+ statement.tableName,
35336
+ json2
35337
+ );
35338
+ }
35339
+ } 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") {
35340
+ const newTableName = getOldTableName(statement.tableName, meta);
35341
+ if (typeof tablesContext[newTableName] === "undefined") {
35342
+ tablesContext[newTableName] = _moveDataStatements(
35343
+ statement.tableName,
35344
+ json2
35345
+ );
35346
+ }
35347
+ } else {
35348
+ const stmnt = fromJson([statement], "sqlite");
35349
+ if (typeof stmnt !== "undefined") {
35350
+ statementsToExecute.push(...stmnt);
35351
+ }
35352
+ }
35353
+ }
35354
+ for (const context of Object.values(tablesContext)) {
35355
+ statementsToExecute.push(...context);
35356
+ }
35357
+ return {
35358
+ statementsToExecute,
35359
+ shouldAskForApprove,
35360
+ infoToPrint,
35361
+ columnsToRemove: [...new Set(columnsToRemove)],
35362
+ schemasToRemove: [...new Set(schemasToRemove)],
35363
+ tablesToTruncate: [...new Set(tablesToTruncate)],
35364
+ tablesToRemove: [...new Set(tablesToRemove)]
35365
+ };
35366
+ };
35367
+
35945
35368
  // src/api.ts
35946
- init_sqlitePushUtils();
35947
35369
  init_global();
35948
35370
  init_migrationPreparator();
35949
35371
  init_mysqlSchema();