drizzle-kit 0.24.1 → 0.24.2-1321cf9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. package/api.js +1250 -665
  2. package/api.mjs +1250 -665
  3. package/bin.cjs +50406 -47005
  4. package/index.d.mts +12 -7
  5. package/index.d.ts +12 -7
  6. package/package.json +2 -2
  7. package/utils.js +19 -0
  8. package/utils.mjs +17 -0
package/api.mjs CHANGED
@@ -8366,6 +8366,13 @@ 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
+ }
8369
8376
  var copy, prepareMigrationMeta, schemaRenameKey, tableRenameKey, columnRenameKey;
8370
8377
  var init_utils = __esm({
8371
8378
  "src/utils.ts"() {
@@ -11475,7 +11482,20 @@ var init_jsonDiffer = __esm({
11475
11482
  });
11476
11483
 
11477
11484
  // src/sqlgenerator.ts
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;
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;
11479
11499
  var init_sqlgenerator = __esm({
11480
11500
  "src/sqlgenerator.ts"() {
11481
11501
  "use strict";
@@ -11550,11 +11570,11 @@ var init_sqlgenerator = __esm({
11550
11570
  const schemaPrefix = column4.typeSchema && column4.typeSchema !== "public" ? `"${column4.typeSchema}".` : "";
11551
11571
  const type = isPgNativeType(column4.type) ? column4.type : `${schemaPrefix}"${column4.type}"`;
11552
11572
  const generated = column4.generated;
11553
- const generatedStatement = ` GENERATED ALWAYS AS (${generated?.as}) STORED`;
11573
+ const generatedStatement = generated ? ` GENERATED ALWAYS AS (${generated?.as}) STORED` : "";
11554
11574
  const unsquashedIdentity = column4.identity ? PgSquasher.unsquashIdentity(column4.identity) : void 0;
11555
11575
  const identityWithSchema = schema4 ? `"${schema4}"."${unsquashedIdentity?.name}"` : `"${unsquashedIdentity?.name}"`;
11556
11576
  const identity = unsquashedIdentity ? ` GENERATED ${unsquashedIdentity.type === "always" ? "ALWAYS" : "BY DEFAULT"} AS IDENTITY (sequence name ${identityWithSchema}${unsquashedIdentity.increment ? ` INCREMENT BY ${unsquashedIdentity.increment}` : ""}${unsquashedIdentity.minValue ? ` MINVALUE ${unsquashedIdentity.minValue}` : ""}${unsquashedIdentity.maxValue ? ` MAXVALUE ${unsquashedIdentity.maxValue}` : ""}${unsquashedIdentity.startWith ? ` START WITH ${unsquashedIdentity.startWith}` : ""}${unsquashedIdentity.cache ? ` CACHE ${unsquashedIdentity.cache}` : ""}${unsquashedIdentity.cycle ? ` CYCLE` : ""})` : "";
11557
- statement += ` "${column4.name}" ${type}${primaryKeyStatement}${defaultStatement}${notNullStatement}${uniqueConstraint4}${generated ? generatedStatement : ""}${identity}`;
11577
+ statement += ` "${column4.name}" ${type}${primaryKeyStatement}${defaultStatement}${generatedStatement}${notNullStatement}${uniqueConstraint4}${identity}`;
11558
11578
  statement += i === columns.length - 1 ? "" : ",\n";
11559
11579
  }
11560
11580
  if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
@@ -11600,7 +11620,7 @@ var init_sqlgenerator = __esm({
11600
11620
  const onUpdateStatement = column4.onUpdate ? ` ON UPDATE CURRENT_TIMESTAMP` : "";
11601
11621
  const autoincrementStatement = column4.autoincrement ? " AUTO_INCREMENT" : "";
11602
11622
  const generatedStatement = column4.generated ? ` GENERATED ALWAYS AS (${column4.generated?.as}) ${column4.generated?.type.toUpperCase()}` : "";
11603
- statement += ` \`${column4.name}\` ${column4.type}${autoincrementStatement}${primaryKeyStatement}${notNullStatement}${defaultStatement}${onUpdateStatement}${generatedStatement}`;
11623
+ statement += ` \`${column4.name}\` ${column4.type}${autoincrementStatement}${primaryKeyStatement}${generatedStatement}${notNullStatement}${defaultStatement}${onUpdateStatement}`;
11604
11624
  statement += i === columns.length - 1 ? "" : ",\n";
11605
11625
  }
11606
11626
  if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
@@ -11627,7 +11647,7 @@ var init_sqlgenerator = __esm({
11627
11647
  };
11628
11648
  SQLiteCreateTableConvertor = class extends Convertor {
11629
11649
  can(statement, dialect7) {
11630
- return statement.type === "sqlite_create_table" && dialect7 === "sqlite";
11650
+ return statement.type === "sqlite_create_table" && (dialect7 === "sqlite" || dialect7 === "turso");
11631
11651
  }
11632
11652
  convert(st) {
11633
11653
  const {
@@ -11648,7 +11668,7 @@ var init_sqlgenerator = __esm({
11648
11668
  const autoincrementStatement = column4.autoincrement ? " AUTOINCREMENT" : "";
11649
11669
  const generatedStatement = column4.generated ? ` GENERATED ALWAYS AS ${column4.generated.as} ${column4.generated.type.toUpperCase()}` : "";
11650
11670
  statement += " ";
11651
- statement += `\`${column4.name}\` ${column4.type}${primaryKeyStatement}${autoincrementStatement}${defaultStatement}${notNullStatement}${generatedStatement}`;
11671
+ statement += `\`${column4.name}\` ${column4.type}${primaryKeyStatement}${autoincrementStatement}${defaultStatement}${generatedStatement}${notNullStatement}`;
11652
11672
  statement += i === columns.length - 1 ? "" : ",\n";
11653
11673
  }
11654
11674
  compositePKs.forEach((it) => {
@@ -11797,32 +11817,6 @@ var init_sqlgenerator = __esm({
11797
11817
  return `ALTER TABLE \`${statement.tableName}\` DROP INDEX \`${unsquashed.name}\`;`;
11798
11818
  }
11799
11819
  };
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
- };
11826
11820
  CreatePgSequenceConvertor = class extends Convertor {
11827
11821
  can(statement, dialect7) {
11828
11822
  return statement.type === "create_sequence" && dialect7 === "postgresql";
@@ -11930,7 +11924,7 @@ var init_sqlgenerator = __esm({
11930
11924
  };
11931
11925
  SQLiteDropTableConvertor = class extends Convertor {
11932
11926
  can(statement, dialect7) {
11933
- return statement.type === "drop_table" && dialect7 === "sqlite";
11927
+ return statement.type === "drop_table" && (dialect7 === "sqlite" || dialect7 === "turso");
11934
11928
  }
11935
11929
  convert(statement) {
11936
11930
  const { tableName } = statement;
@@ -11950,7 +11944,7 @@ var init_sqlgenerator = __esm({
11950
11944
  };
11951
11945
  SqliteRenameTableConvertor = class extends Convertor {
11952
11946
  can(statement, dialect7) {
11953
- return statement.type === "rename_table" && dialect7 === "sqlite";
11947
+ return statement.type === "rename_table" && (dialect7 === "sqlite" || dialect7 === "turso");
11954
11948
  }
11955
11949
  convert(statement) {
11956
11950
  const { tableNameFrom, tableNameTo } = statement;
@@ -11987,11 +11981,11 @@ var init_sqlgenerator = __esm({
11987
11981
  };
11988
11982
  SQLiteAlterTableRenameColumnConvertor = class extends Convertor {
11989
11983
  can(statement, dialect7) {
11990
- return statement.type === "alter_table_rename_column" && dialect7 === "sqlite";
11984
+ return statement.type === "alter_table_rename_column" && (dialect7 === "sqlite" || dialect7 === "turso");
11991
11985
  }
11992
11986
  convert(statement) {
11993
11987
  const { tableName, oldColumnName, newColumnName } = statement;
11994
- return `ALTER TABLE \`${tableName}\` RENAME COLUMN \`${oldColumnName}\` TO \`${newColumnName}\`;`;
11988
+ return `ALTER TABLE \`${tableName}\` RENAME COLUMN "${oldColumnName}" TO "${newColumnName}";`;
11995
11989
  }
11996
11990
  };
11997
11991
  PgAlterTableDropColumnConvertor = class extends Convertor {
@@ -12015,7 +12009,7 @@ var init_sqlgenerator = __esm({
12015
12009
  };
12016
12010
  SQLiteAlterTableDropColumnConvertor = class extends Convertor {
12017
12011
  can(statement, dialect7) {
12018
- return statement.type === "alter_table_drop_column" && dialect7 === "sqlite";
12012
+ return statement.type === "alter_table_drop_column" && (dialect7 === "sqlite" || dialect7 === "turso");
12019
12013
  }
12020
12014
  convert(statement) {
12021
12015
  const { tableName, columnName } = statement;
@@ -12038,8 +12032,8 @@ var init_sqlgenerator = __esm({
12038
12032
  const unsquashedIdentity = identity ? PgSquasher.unsquashIdentity(identity) : void 0;
12039
12033
  const identityWithSchema = schema4 ? `"${schema4}"."${unsquashedIdentity?.name}"` : `"${unsquashedIdentity?.name}"`;
12040
12034
  const identityStatement = unsquashedIdentity ? ` GENERATED ${unsquashedIdentity.type === "always" ? "ALWAYS" : "BY DEFAULT"} AS IDENTITY (sequence name ${identityWithSchema}${unsquashedIdentity.increment ? ` INCREMENT BY ${unsquashedIdentity.increment}` : ""}${unsquashedIdentity.minValue ? ` MINVALUE ${unsquashedIdentity.minValue}` : ""}${unsquashedIdentity.maxValue ? ` MAXVALUE ${unsquashedIdentity.maxValue}` : ""}${unsquashedIdentity.startWith ? ` START WITH ${unsquashedIdentity.startWith}` : ""}${unsquashedIdentity.cache ? ` CACHE ${unsquashedIdentity.cache}` : ""}${unsquashedIdentity.cycle ? ` CYCLE` : ""})` : "";
12041
- const generatedStatement = ` GENERATED ALWAYS AS (${generated?.as}) STORED`;
12042
- return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN "${name2}" ${fixedType}${primaryKeyStatement}${defaultStatement}${notNullStatement}${generated ? generatedStatement : ""}${identityStatement};`;
12035
+ const generatedStatement = generated ? ` GENERATED ALWAYS AS (${generated?.as}) STORED` : "";
12036
+ return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN "${name2}" ${fixedType}${primaryKeyStatement}${defaultStatement}${generatedStatement}${notNullStatement}${identityStatement};`;
12043
12037
  }
12044
12038
  };
12045
12039
  MySqlAlterTableAddColumnConvertor = class extends Convertor {
@@ -12063,12 +12057,12 @@ var init_sqlgenerator = __esm({
12063
12057
  const autoincrementStatement = `${autoincrement ? " AUTO_INCREMENT" : ""}`;
12064
12058
  const onUpdateStatement = `${onUpdate ? " ON UPDATE CURRENT_TIMESTAMP" : ""}`;
12065
12059
  const generatedStatement = generated ? ` GENERATED ALWAYS AS (${generated?.as}) ${generated?.type.toUpperCase()}` : "";
12066
- return `ALTER TABLE \`${tableName}\` ADD \`${name2}\` ${type}${primaryKeyStatement}${autoincrementStatement}${defaultStatement}${notNullStatement}${onUpdateStatement}${generatedStatement};`;
12060
+ return `ALTER TABLE \`${tableName}\` ADD \`${name2}\` ${type}${primaryKeyStatement}${autoincrementStatement}${defaultStatement}${generatedStatement}${notNullStatement}${onUpdateStatement};`;
12067
12061
  }
12068
12062
  };
12069
12063
  SQLiteAlterTableAddColumnConvertor = class extends Convertor {
12070
12064
  can(statement, dialect7) {
12071
- return statement.type === "sqlite_alter_table_add_column" && dialect7 === "sqlite";
12065
+ return statement.type === "sqlite_alter_table_add_column" && (dialect7 === "sqlite" || dialect7 === "turso");
12072
12066
  }
12073
12067
  convert(statement) {
12074
12068
  const { tableName, column: column4, referenceData } = statement;
@@ -12079,7 +12073,7 @@ var init_sqlgenerator = __esm({
12079
12073
  const referenceAsObject = referenceData ? SQLiteSquasher.unsquashFK(referenceData) : void 0;
12080
12074
  const referenceStatement = `${referenceAsObject ? ` REFERENCES ${referenceAsObject.tableTo}(${referenceAsObject.columnsTo})` : ""}`;
12081
12075
  const generatedStatement = generated ? ` GENERATED ALWAYS AS ${generated.as} ${generated.type.toUpperCase()}` : "";
12082
- return `ALTER TABLE \`${tableName}\` ADD \`${name2}\` ${type}${primaryKeyStatement}${defaultStatement}${notNullStatement}${generatedStatement}${referenceStatement};`;
12076
+ return `ALTER TABLE \`${tableName}\` ADD \`${name2}\` ${type}${primaryKeyStatement}${defaultStatement}${generatedStatement}${notNullStatement}${referenceStatement};`;
12083
12077
  }
12084
12078
  };
12085
12079
  PgAlterTableAlterColumnSetTypeConvertor = class extends Convertor {
@@ -12092,21 +12086,6 @@ var init_sqlgenerator = __esm({
12092
12086
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DATA TYPE ${newDataType};`;
12093
12087
  }
12094
12088
  };
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
- };
12110
12089
  PgAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
12111
12090
  can(statement, dialect7) {
12112
12091
  return statement.type === "alter_table_alter_column_set_default" && dialect7 === "postgresql";
@@ -12117,21 +12096,6 @@ var init_sqlgenerator = __esm({
12117
12096
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET DEFAULT ${statement.newDefaultValue};`;
12118
12097
  }
12119
12098
  };
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
- };
12135
12099
  PgAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
12136
12100
  can(statement, dialect7) {
12137
12101
  return statement.type === "alter_table_alter_column_drop_default" && dialect7 === "postgresql";
@@ -12230,7 +12194,7 @@ var init_sqlgenerator = __esm({
12230
12194
  };
12231
12195
  SqliteAlterTableAlterColumnDropGeneratedConvertor = class extends Convertor {
12232
12196
  can(statement, dialect7) {
12233
- return statement.type === "alter_table_alter_column_drop_generated" && dialect7 === "sqlite";
12197
+ return statement.type === "alter_table_alter_column_drop_generated" && (dialect7 === "sqlite" || dialect7 === "turso");
12234
12198
  }
12235
12199
  convert(statement) {
12236
12200
  const {
@@ -12271,7 +12235,7 @@ var init_sqlgenerator = __esm({
12271
12235
  };
12272
12236
  SqliteAlterTableAlterColumnSetExpressionConvertor = class extends Convertor {
12273
12237
  can(statement, dialect7) {
12274
- return statement.type === "alter_table_alter_column_set_generated" && dialect7 === "sqlite";
12238
+ return statement.type === "alter_table_alter_column_set_generated" && (dialect7 === "sqlite" || dialect7 === "turso");
12275
12239
  }
12276
12240
  convert(statement) {
12277
12241
  const {
@@ -12312,7 +12276,7 @@ var init_sqlgenerator = __esm({
12312
12276
  };
12313
12277
  SqliteAlterTableAlterColumnAlterGeneratedConvertor = class extends Convertor {
12314
12278
  can(statement, dialect7) {
12315
- return statement.type === "alter_table_alter_column_alter_generated" && dialect7 === "sqlite";
12279
+ return statement.type === "alter_table_alter_column_alter_generated" && (dialect7 === "sqlite" || dialect7 === "turso");
12316
12280
  }
12317
12281
  convert(statement) {
12318
12282
  const {
@@ -12405,6 +12369,67 @@ var init_sqlgenerator = __esm({
12405
12369
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY`;
12406
12370
  }
12407
12371
  };
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
+ };
12408
12433
  MySqlModifyColumn = class extends Convertor {
12409
12434
  can(statement, dialect7) {
12410
12435
  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";
@@ -12539,22 +12564,7 @@ var init_sqlgenerator = __esm({
12539
12564
  columnGenerated = statement.columnGenerated ? ` GENERATED ALWAYS AS (${statement.columnGenerated?.as}) ${statement.columnGenerated?.type.toUpperCase()}` : "";
12540
12565
  }
12541
12566
  columnDefault = columnDefault instanceof Date ? columnDefault.toISOString() : columnDefault;
12542
- return `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${columnName}\`${columnType}${columnAutoincrement}${columnNotNull}${columnDefault}${columnOnUpdate}${columnGenerated};`;
12543
- }
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
- */`;
12567
+ return `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${columnName}\`${columnType}${columnAutoincrement}${columnGenerated}${columnNotNull}${columnDefault}${columnOnUpdate};`;
12558
12568
  }
12559
12569
  };
12560
12570
  PgAlterTableCreateCompositePrimaryKeyConvertor = class extends Convertor {
@@ -12621,85 +12631,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12621
12631
  return `ALTER TABLE \`${statement.tableName}\` DROP PRIMARY KEY, ADD PRIMARY KEY(\`${newColumns.join("`,`")}\`);`;
12622
12632
  }
12623
12633
  };
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
- };
12703
12634
  PgAlterTableAlterColumnSetPrimaryKeyConvertor = class extends Convertor {
12704
12635
  can(statement, dialect7) {
12705
12636
  return statement.type === "alter_table_alter_column_set_pk" && dialect7 === "postgresql";
@@ -12744,51 +12675,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12744
12675
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" SET NOT NULL;`;
12745
12676
  }
12746
12677
  };
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
- };
12792
12678
  PgAlterTableAlterColumnDropNotNullConvertor = class extends Convertor {
12793
12679
  can(statement, dialect7) {
12794
12680
  return statement.type === "alter_table_alter_column_drop_notnull" && dialect7 === "postgresql";
@@ -12799,21 +12685,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12799
12685
  return `ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${columnName}" DROP NOT NULL;`;
12800
12686
  }
12801
12687
  };
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
- };
12817
12688
  PgCreateForeignKeyConvertor = class extends Convertor {
12818
12689
  can(statement, dialect7) {
12819
12690
  return statement.type === "create_reference" && dialect7 === "postgresql";
@@ -12844,18 +12715,21 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12844
12715
  return sql2;
12845
12716
  }
12846
12717
  };
12847
- SqliteCreateForeignKeyConvertor = class extends Convertor {
12718
+ LibSQLCreateForeignKeyConvertor = class extends Convertor {
12848
12719
  can(statement, dialect7) {
12849
- return statement.type === "create_reference" && dialect7 === "sqlite";
12720
+ return statement.type === "create_reference" && dialect7 === "turso";
12850
12721
  }
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
- */`;
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};`;
12859
12733
  }
12860
12734
  };
12861
12735
  MySqlCreateForeignKeyConvertor = class extends Convertor {
@@ -12904,20 +12778,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12904
12778
  return sql2;
12905
12779
  }
12906
12780
  };
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
- };
12921
12781
  PgDeleteForeignKeyConvertor = class extends Convertor {
12922
12782
  can(statement, dialect7) {
12923
12783
  return statement.type === "delete_reference" && dialect7 === "postgresql";
@@ -12930,20 +12790,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
12930
12790
  `;
12931
12791
  }
12932
12792
  };
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
- };
12947
12793
  MySqlDeleteForeignKeyConvertor = class extends Convertor {
12948
12794
  can(statement, dialect7) {
12949
12795
  return statement.type === "delete_reference" && dialect7 === "mysql";
@@ -13004,7 +12850,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13004
12850
  };
13005
12851
  CreateSqliteIndexConvertor = class extends Convertor {
13006
12852
  can(statement, dialect7) {
13007
- return statement.type === "create_index" && dialect7 === "sqlite";
12853
+ return statement.type === "create_index" && (dialect7 === "sqlite" || dialect7 === "turso");
13008
12854
  }
13009
12855
  convert(statement) {
13010
12856
  const { name: name2, columns, isUnique, where } = SQLiteSquasher.unsquashIdx(
@@ -13091,7 +12937,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13091
12937
  };
13092
12938
  SqliteDropIndexConvertor = class extends Convertor {
13093
12939
  can(statement, dialect7) {
13094
- return statement.type === "drop_index" && dialect7 === "sqlite";
12940
+ return statement.type === "drop_index" && (dialect7 === "sqlite" || dialect7 === "turso");
13095
12941
  }
13096
12942
  convert(statement) {
13097
12943
  const { name: name2 } = PgSquasher.unsquashIdx(statement.data);
@@ -13107,10 +12953,96 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13107
12953
  return `DROP INDEX \`${name2}\` ON \`${statement.tableName}\`;`;
13108
12954
  }
13109
12955
  };
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
+ };
13110
13040
  convertors = [];
13111
13041
  convertors.push(new PgCreateTableConvertor());
13112
13042
  convertors.push(new MySqlCreateTableConvertor());
13113
13043
  convertors.push(new SQLiteCreateTableConvertor());
13044
+ convertors.push(new SQLiteRecreateTableConvertor());
13045
+ convertors.push(new LibSQLRecreateTableConvertor());
13114
13046
  convertors.push(new CreateTypeEnumConvertor());
13115
13047
  convertors.push(new CreatePgSequenceConvertor());
13116
13048
  convertors.push(new DropPgSequenceConvertor());
@@ -13158,6 +13090,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13158
13090
  convertors.push(new SqliteAlterTableAlterColumnAlterGeneratedConvertor());
13159
13091
  convertors.push(new SqliteAlterTableAlterColumnSetExpressionConvertor());
13160
13092
  convertors.push(new MySqlModifyColumn());
13093
+ convertors.push(new LibSQLModifyColumn());
13161
13094
  convertors.push(new PgCreateForeignKeyConvertor());
13162
13095
  convertors.push(new MySqlCreateForeignKeyConvertor());
13163
13096
  convertors.push(new PgAlterForeignKeyConvertor());
@@ -13169,24 +13102,10 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13169
13102
  convertors.push(new PgAlterTableSetSchemaConvertor());
13170
13103
  convertors.push(new PgAlterTableSetNewSchemaConvertor());
13171
13104
  convertors.push(new PgAlterTableRemoveFromSchemaConvertor());
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());
13105
+ convertors.push(new LibSQLCreateForeignKeyConvertor());
13178
13106
  convertors.push(new PgAlterTableAlterColumnDropGenerated());
13179
13107
  convertors.push(new PgAlterTableAlterColumnSetGenerated());
13180
13108
  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());
13190
13109
  convertors.push(new PgAlterTableCreateCompositePrimaryKeyConvertor());
13191
13110
  convertors.push(new PgAlterTableDeleteCompositePrimaryKeyConvertor());
13192
13111
  convertors.push(new PgAlterTableAlterCompositePrimaryKeyConvertor());
@@ -13195,19 +13114,6 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT ${statement.newCo
13195
13114
  convertors.push(new MySqlAlterTableCreateCompositePrimaryKeyConvertor());
13196
13115
  convertors.push(new MySqlAlterTableAddPk());
13197
13116
  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
- };
13211
13117
  https: `
13212
13118
  create table users (
13213
13119
  id int,
@@ -13234,12 +13140,248 @@ drop type __venum;
13234
13140
  }
13235
13141
  });
13236
13142
 
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
+
13237
13378
  // src/jsonStatements.ts
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;
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;
13239
13380
  var init_jsonStatements = __esm({
13240
13381
  "src/jsonStatements.ts"() {
13241
13382
  "use strict";
13242
13383
  init_source();
13384
+ init_sqlitePushUtils();
13243
13385
  init_views();
13244
13386
  init_mysqlSchema();
13245
13387
  init_pgSchema();
@@ -14023,18 +14165,61 @@ var init_jsonStatements = __esm({
14023
14165
  const columnPk = json2.tables[tableName].columns[columnName].primaryKey;
14024
14166
  const columnGenerated = json2.tables[tableName].columns[columnName].generated;
14025
14167
  const compositePk = json2.tables[tableName].compositePrimaryKeys[`${tableName}_${columnName}`];
14026
- if (typeof column4.name !== "string") {
14168
+ if (column4.autoincrement?.type === "added") {
14027
14169
  statements.push({
14028
- type: "alter_table_rename_column",
14170
+ type: "alter_table_alter_column_set_autoincrement",
14029
14171
  tableName,
14030
- oldColumnName: column4.name.old,
14031
- newColumnName: column4.name.new,
14032
- schema: schema4
14033
- });
14034
- }
14035
- if (column4.type?.type === "changed") {
14036
- statements.push({
14037
- type: "alter_table_alter_column_set_type",
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",
14214
+ tableName,
14215
+ oldColumnName: column4.name.old,
14216
+ newColumnName: column4.name.new,
14217
+ schema: schema4
14218
+ });
14219
+ }
14220
+ if (column4.type?.type === "changed") {
14221
+ statements.push({
14222
+ type: "alter_table_alter_column_set_type",
14038
14223
  tableName,
14039
14224
  columnName,
14040
14225
  newDataType: column4.type.new,
@@ -14286,6 +14471,37 @@ var init_jsonStatements = __esm({
14286
14471
  };
14287
14472
  });
14288
14473
  };
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
+ };
14289
14505
  prepareDropReferencesJson = (tableName, schema4, foreignKeys) => {
14290
14506
  return Object.values(foreignKeys).map((fkData) => {
14291
14507
  return {
@@ -14296,6 +14512,53 @@ var init_jsonStatements = __esm({
14296
14512
  };
14297
14513
  });
14298
14514
  };
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
+ };
14299
14562
  prepareAlterReferencesJson = (tableName, schema4, foreignKeys) => {
14300
14563
  const stmts = [];
14301
14564
  Object.values(foreignKeys).map((val) => {
@@ -14449,10 +14712,292 @@ var init_jsonStatements = __esm({
14449
14712
  }
14450
14713
  });
14451
14714
 
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
+
14452
14996
  // src/snapshotsDiffer.ts
14453
14997
  var snapshotsDiffer_exports = {};
14454
14998
  __export(snapshotsDiffer_exports, {
14455
14999
  alteredTableScheme: () => alteredTableScheme,
15000
+ applyLibSQLSnapshotsDiff: () => applyLibSQLSnapshotsDiff,
14456
15001
  applyMysqlSnapshotsDiff: () => applyMysqlSnapshotsDiff,
14457
15002
  applyPgSnapshotsDiff: () => applyPgSnapshotsDiff,
14458
15003
  applySqliteSnapshotsDiff: () => applySqliteSnapshotsDiff,
@@ -14462,7 +15007,7 @@ __export(snapshotsDiffer_exports, {
14462
15007
  makePatched: () => makePatched,
14463
15008
  makeSelfOrPatched: () => makeSelfOrPatched
14464
15009
  });
14465
- var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff;
15010
+ var makeChanged, makeSelfOrChanged, makePatched, makeSelfOrPatched, columnSchema, alteredColumnSchema, enumSchema2, changedEnumSchema, tableScheme, alteredTableScheme, diffResultScheme, diffResultSchemeMysql, diffResultSchemeSQLite, schemaChangeFor, nameChangeFor, nameSchemaChangeFor, columnChangeFor, applyPgSnapshotsDiff, applyMysqlSnapshotsDiff, applySqliteSnapshotsDiff, applyLibSQLSnapshotsDiff;
14466
15011
  var init_snapshotsDiffer = __esm({
14467
15012
  "src/snapshotsDiffer.ts"() {
14468
15013
  "use strict";
@@ -14474,6 +15019,7 @@ var init_snapshotsDiffer = __esm({
14474
15019
  init_mysqlSchema();
14475
15020
  init_pgSchema();
14476
15021
  init_sqliteSchema();
15022
+ init_statementCombiner();
14477
15023
  init_utils();
14478
15024
  makeChanged = (schema4) => {
14479
15025
  return objectType({
@@ -15367,33 +15913,332 @@ var init_snapshotsDiffer = __esm({
15367
15913
  let addedColumns = [];
15368
15914
  for (const addedPkName of Object.keys(it.addedCompositePKs)) {
15369
15915
  const addedPkColumns = it.addedCompositePKs[addedPkName];
15370
- addedColumns = MySqlSquasher.unsquashPK(addedPkColumns).columns;
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) => {
16215
+ let addedColumns = [];
16216
+ for (const addedPkName of Object.keys(it.addedCompositePKs)) {
16217
+ const addedPkColumns = it.addedCompositePKs[addedPkName];
16218
+ addedColumns = SQLiteSquasher.unsquashPK(addedPkColumns);
15371
16219
  }
15372
16220
  let deletedColumns = [];
15373
16221
  for (const deletedPkName of Object.keys(it.deletedCompositePKs)) {
15374
16222
  const deletedPkColumns = it.deletedCompositePKs[deletedPkName];
15375
- deletedColumns = MySqlSquasher.unsquashPK(deletedPkColumns).columns;
16223
+ deletedColumns = SQLiteSquasher.unsquashPK(deletedPkColumns);
15376
16224
  }
15377
16225
  const doPerformDeleteAndCreate = JSON.stringify(addedColumns) !== JSON.stringify(deletedColumns);
15378
16226
  let addedCompositePKs = [];
15379
16227
  let deletedCompositePKs = [];
15380
16228
  let alteredCompositePKs = [];
15381
- addedCompositePKs = prepareAddCompositePrimaryKeyMySql(
15382
- it.name,
15383
- it.addedCompositePKs,
15384
- prevFull,
15385
- curFull
15386
- );
15387
- deletedCompositePKs = prepareDeleteCompositePrimaryKeyMySql(
15388
- it.name,
15389
- it.deletedCompositePKs,
15390
- prevFull
15391
- );
15392
- alteredCompositePKs = prepareAlterCompositePrimaryKeyMySql(
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(
15393
16240
  it.name,
15394
- it.alteredCompositePKs,
15395
- prevFull,
15396
- curFull
16241
+ it.alteredCompositePKs
15397
16242
  );
15398
16243
  let addedUniqueConstraints = [];
15399
16244
  let deletedUniqueConstraints = [];
@@ -15437,17 +16282,10 @@ var init_snapshotsDiffer = __esm({
15437
16282
  to: { schema: schema4, table: tableName, column: it.newColumnName }
15438
16283
  };
15439
16284
  });
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
- );
16285
+ const jsonTableAlternations = allAltered.map((it) => {
16286
+ return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15449
16287
  }).flat();
15450
- const jsonCreateIndexesForAllAlteredTables = alteredTables.map((it) => {
16288
+ const jsonCreateIndexesForAllAlteredTables = allAltered.map((it) => {
15451
16289
  return prepareCreateIndexesJson(
15452
16290
  it.name,
15453
16291
  it.schema,
@@ -15455,14 +16293,14 @@ var init_snapshotsDiffer = __esm({
15455
16293
  curFull.internal
15456
16294
  );
15457
16295
  }).flat();
15458
- const jsonDropIndexesForAllAlteredTables = alteredTables.map((it) => {
16296
+ const jsonDropIndexesForAllAlteredTables = allAltered.map((it) => {
15459
16297
  return prepareDropIndexesJson(
15460
16298
  it.name,
15461
16299
  it.schema,
15462
16300
  it.deletedIndexes || {}
15463
16301
  );
15464
16302
  }).flat();
15465
- alteredTables.forEach((it) => {
16303
+ allAltered.forEach((it) => {
15466
16304
  const droppedIndexes = Object.keys(it.alteredIndexes).reduce(
15467
16305
  (current, item) => {
15468
16306
  current[item] = it.alteredIndexes[item].__old;
@@ -15478,16 +16316,18 @@ var init_snapshotsDiffer = __esm({
15478
16316
  {}
15479
16317
  );
15480
16318
  jsonCreateIndexesForAllAlteredTables.push(
15481
- ...prepareCreateIndexesJson(it.name, it.schema, createdIndexes || {})
16319
+ ...prepareCreateIndexesJson(
16320
+ it.name,
16321
+ it.schema,
16322
+ createdIndexes || {},
16323
+ curFull.internal
16324
+ )
15482
16325
  );
15483
16326
  jsonDropIndexesForAllAlteredTables.push(
15484
16327
  ...prepareDropIndexesJson(it.name, it.schema, droppedIndexes || {})
15485
16328
  );
15486
16329
  });
15487
- const jsonCreateReferencesForCreatedTables = createdTables.map((it) => {
15488
- return prepareCreateReferencesJson(it.name, it.schema, it.foreignKeys);
15489
- }).flat();
15490
- const jsonReferencesForAllAlteredTables = alteredTables.map((it) => {
16330
+ const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15491
16331
  const forAdded = prepareCreateReferencesJson(
15492
16332
  it.name,
15493
16333
  it.schema,
@@ -15511,35 +16351,25 @@ var init_snapshotsDiffer = __esm({
15511
16351
  const jsonDroppedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
15512
16352
  (t) => t.type === "delete_reference"
15513
16353
  );
15514
- const jsonMySqlCreateTables = createdTables.map((it) => {
15515
- return prepareMySqlCreateTableJson(
15516
- it,
15517
- curFull,
15518
- curFull.internal
15519
- );
15520
- });
15521
- jsonStatements.push(...jsonMySqlCreateTables);
16354
+ const jsonStatements = [];
16355
+ jsonStatements.push(...jsonCreateTables);
15522
16356
  jsonStatements.push(...jsonDropTables);
15523
16357
  jsonStatements.push(...jsonRenameTables);
15524
16358
  jsonStatements.push(...jsonRenameColumnsStatements);
15525
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15526
16359
  jsonStatements.push(...jsonDroppedReferencesForAlteredTables);
15527
16360
  jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
15528
16361
  jsonStatements.push(...jsonDeletedCompositePKs);
15529
16362
  jsonStatements.push(...jsonTableAlternations);
15530
16363
  jsonStatements.push(...jsonAddedCompositePKs);
15531
- jsonStatements.push(...jsonAddedUniqueConstraints);
15532
- jsonStatements.push(...jsonDeletedUniqueConstraints);
15533
16364
  jsonStatements.push(...jsonAddColumnsStatemets);
15534
- jsonStatements.push(...jsonCreateReferencesForCreatedTables);
15535
16365
  jsonStatements.push(...jsonCreateIndexesForCreatedTables);
15536
- jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15537
16366
  jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
16367
+ jsonStatements.push(...jsonCreatedReferencesForAlteredTables);
15538
16368
  jsonStatements.push(...jsonDropColumnsStatemets);
15539
16369
  jsonStatements.push(...jsonAlteredCompositePKs);
15540
- jsonStatements.push(...jsonAddedUniqueConstraints);
15541
16370
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15542
- const sqlStatements = fromJson(jsonStatements, "mysql");
16371
+ const combinedJsonStatements = sqliteCombineStatements(jsonStatements, json2, action);
16372
+ const sqlStatements = fromJson(combinedJsonStatements, "sqlite");
15543
16373
  const uniqueSqlStatements = [];
15544
16374
  sqlStatements.forEach((ss) => {
15545
16375
  if (!uniqueSqlStatements.includes(ss)) {
@@ -15551,12 +16381,12 @@ var init_snapshotsDiffer = __esm({
15551
16381
  });
15552
16382
  const _meta = prepareMigrationMeta([], rTables, rColumns);
15553
16383
  return {
15554
- statements: jsonStatements,
16384
+ statements: combinedJsonStatements,
15555
16385
  sqlStatements: uniqueSqlStatements,
15556
16386
  _meta
15557
16387
  };
15558
16388
  };
15559
- applySqliteSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
16389
+ applyLibSQLSnapshotsDiff = async (json1, json2, tablesResolver2, columnsResolver2, prevFull, curFull, action) => {
15560
16390
  const tablesDiff = diffSchemasOrTables(json1.tables, json2.tables);
15561
16391
  const {
15562
16392
  created: createdTables,
@@ -15658,6 +16488,18 @@ var init_snapshotsDiffer = __esm({
15658
16488
  tablesMap[it.table] && tablesMap[it.table].addedForeignKeys ? Object.values(tablesMap[it.table].addedForeignKeys) : []
15659
16489
  );
15660
16490
  }).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);
15661
16503
  const allAltered = typedResult.alteredTablesWithColumns;
15662
16504
  const jsonAddedCompositePKs = [];
15663
16505
  const jsonDeletedCompositePKs = [];
@@ -15728,14 +16570,6 @@ var init_snapshotsDiffer = __esm({
15728
16570
  jsonDeletedUniqueConstraints.push(...deletedUniqueConstraints);
15729
16571
  jsonAlteredUniqueConstraints.push(...alteredUniqueConstraints);
15730
16572
  });
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
- });
15739
16573
  const jsonTableAlternations = allAltered.map((it) => {
15740
16574
  return prepareSqliteAlterColumns(it.name, it.schema, it.altered, json2);
15741
16575
  }).flat();
@@ -15782,21 +16616,22 @@ var init_snapshotsDiffer = __esm({
15782
16616
  );
15783
16617
  });
15784
16618
  const jsonReferencesForAllAlteredTables = allAltered.map((it) => {
15785
- const forAdded = prepareCreateReferencesJson(
15786
- it.name,
15787
- it.schema,
15788
- it.addedForeignKeys
15789
- );
15790
- const forAltered = prepareDropReferencesJson(
16619
+ const forAdded = prepareLibSQLCreateReferencesJson(
15791
16620
  it.name,
15792
16621
  it.schema,
15793
- it.deletedForeignKeys
16622
+ it.addedForeignKeys,
16623
+ json2,
16624
+ action
15794
16625
  );
15795
- const alteredFKs = prepareAlterReferencesJson(
16626
+ const forAltered = prepareLibSQLDropReferencesJson(
15796
16627
  it.name,
15797
16628
  it.schema,
15798
- it.alteredForeignKeys
16629
+ it.deletedForeignKeys,
16630
+ json2,
16631
+ _meta,
16632
+ action
15799
16633
  );
16634
+ const alteredFKs = prepareAlterReferencesJson(it.name, it.schema, it.alteredForeignKeys);
15800
16635
  return [...forAdded, ...forAltered, ...alteredFKs];
15801
16636
  }).flat();
15802
16637
  const jsonCreatedReferencesForAlteredTables = jsonReferencesForAllAlteredTables.filter(
@@ -15822,19 +16657,21 @@ var init_snapshotsDiffer = __esm({
15822
16657
  jsonStatements.push(...jsonDropColumnsStatemets);
15823
16658
  jsonStatements.push(...jsonAlteredCompositePKs);
15824
16659
  jsonStatements.push(...jsonAlteredUniqueConstraints);
15825
- const sqlStatements = fromJson(jsonStatements, "sqlite");
16660
+ const combinedJsonStatements = libSQLCombineStatements(jsonStatements, json2, action);
16661
+ const sqlStatements = fromJson(
16662
+ combinedJsonStatements,
16663
+ "turso",
16664
+ action,
16665
+ json2
16666
+ );
15826
16667
  const uniqueSqlStatements = [];
15827
16668
  sqlStatements.forEach((ss) => {
15828
16669
  if (!uniqueSqlStatements.includes(ss)) {
15829
16670
  uniqueSqlStatements.push(ss);
15830
16671
  }
15831
16672
  });
15832
- const rTables = renamedTables.map((it) => {
15833
- return { from: it.from, to: it.to };
15834
- });
15835
- const _meta = prepareMigrationMeta([], rTables, rColumns);
15836
16673
  return {
15837
- statements: jsonStatements,
16674
+ statements: combinedJsonStatements,
15838
16675
  sqlStatements: uniqueSqlStatements,
15839
16676
  _meta
15840
16677
  };
@@ -15858,7 +16695,7 @@ var init_schemaValidator = __esm({
15858
16695
  init_mysqlSchema();
15859
16696
  init_pgSchema();
15860
16697
  init_sqliteSchema();
15861
- dialects = ["postgresql", "mysql", "sqlite"];
16698
+ dialects = ["postgresql", "mysql", "sqlite", "turso"];
15862
16699
  dialect3 = enumType(dialects);
15863
16700
  commonSquashedSchema = unionType([
15864
16701
  pgSchemaSquashed,
@@ -15870,7 +16707,7 @@ var init_schemaValidator = __esm({
15870
16707
  });
15871
16708
 
15872
16709
  // src/cli/validations/common.ts
15873
- var sqliteDriversLiterals, prefixes, prefix, sqliteDriver, postgresDriver, driver, configMigrations, configCommonSchema, casing, introspectParams, configIntrospectCliSchema, configGenerateSchema, configPushSchema;
16710
+ var sqliteDriversLiterals, postgresqlDriversLiterals, prefixes, prefix, sqliteDriver, postgresDriver, driver, configMigrations, configCommonSchema, casing, introspectParams, configIntrospectCliSchema, configGenerateSchema, configPushSchema;
15874
16711
  var init_common = __esm({
15875
16712
  "src/cli/validations/common.ts"() {
15876
16713
  "use strict";
@@ -15878,10 +16715,13 @@ var init_common = __esm({
15878
16715
  init_schemaValidator();
15879
16716
  init_outputs();
15880
16717
  sqliteDriversLiterals = [
15881
- literalType("turso"),
15882
16718
  literalType("d1-http"),
15883
16719
  literalType("expo")
15884
16720
  ];
16721
+ postgresqlDriversLiterals = [
16722
+ literalType("aws-data-api"),
16723
+ literalType("pglite")
16724
+ ];
15885
16725
  prefixes = [
15886
16726
  "index",
15887
16727
  "timestamp",
@@ -15894,7 +16734,7 @@ var init_common = __esm({
15894
16734
  const _2 = "";
15895
16735
  }
15896
16736
  sqliteDriver = unionType(sqliteDriversLiterals);
15897
- postgresDriver = literalType("aws-data-api");
16737
+ postgresDriver = unionType(postgresqlDriversLiterals);
15898
16738
  driver = unionType([sqliteDriver, postgresDriver]);
15899
16739
  configMigrations = objectType({
15900
16740
  table: stringType().optional(),
@@ -33956,6 +34796,21 @@ var init_cli = __esm({
33956
34796
  }
33957
34797
  });
33958
34798
 
34799
+ // src/cli/validations/libsql.ts
34800
+ var libSQLCredentials;
34801
+ var init_libsql = __esm({
34802
+ "src/cli/validations/libsql.ts"() {
34803
+ "use strict";
34804
+ init_lib();
34805
+ init_views();
34806
+ init_common();
34807
+ libSQLCredentials = objectType({
34808
+ url: stringType().min(1),
34809
+ authToken: stringType().min(1).optional()
34810
+ });
34811
+ }
34812
+ });
34813
+
33959
34814
  // src/cli/validations/mysql.ts
33960
34815
  var mysqlCredentials;
33961
34816
  var init_mysql = __esm({
@@ -34033,6 +34888,10 @@ var init_postgres = __esm({
34033
34888
  database: stringType().min(1),
34034
34889
  secretArn: stringType().min(1),
34035
34890
  resourceArn: stringType().min(1)
34891
+ }),
34892
+ objectType({
34893
+ driver: literalType("pglite"),
34894
+ url: stringType().min(1)
34036
34895
  })
34037
34896
  ]);
34038
34897
  }
@@ -34123,6 +34982,8 @@ var init_utils8 = __esm({
34123
34982
  init_serializer();
34124
34983
  init_cli();
34125
34984
  init_common();
34985
+ init_libsql();
34986
+ init_libsql();
34126
34987
  init_mysql();
34127
34988
  init_outputs();
34128
34989
  init_postgres();
@@ -35080,284 +35941,8 @@ var sqlitePushIntrospect = async (db, filters) => {
35080
35941
  return { schema: schema4 };
35081
35942
  };
35082
35943
 
35083
- // src/cli/commands/sqlitePushUtils.ts
35084
- init_source();
35085
- init_sqliteSchema();
35086
- init_sqlgenerator();
35087
- var _moveDataStatements = (tableName, json, dataLoss = false) => {
35088
- const statements = [];
35089
- statements.push(
35090
- new SqliteRenameTableConvertor().convert({
35091
- type: "rename_table",
35092
- tableNameFrom: tableName,
35093
- tableNameTo: `__old_push_${tableName}`,
35094
- fromSchema: "",
35095
- toSchema: ""
35096
- })
35097
- );
35098
- const tableColumns = Object.values(json.tables[tableName].columns);
35099
- const referenceData = Object.values(json.tables[tableName].foreignKeys);
35100
- const compositePKs = Object.values(
35101
- json.tables[tableName].compositePrimaryKeys
35102
- ).map((it) => SQLiteSquasher.unsquashPK(it));
35103
- const fks = referenceData.map((it) => SQLiteSquasher.unsquashPushFK(it));
35104
- statements.push(
35105
- new SQLiteCreateTableConvertor().convert({
35106
- type: "sqlite_create_table",
35107
- tableName,
35108
- columns: tableColumns,
35109
- referenceData: fks,
35110
- compositePKs
35111
- })
35112
- );
35113
- if (!dataLoss) {
35114
- statements.push(
35115
- `INSERT INTO "${tableName}" SELECT * FROM "__old_push_${tableName}";`
35116
- );
35117
- }
35118
- statements.push(
35119
- new SQLiteDropTableConvertor().convert({
35120
- type: "drop_table",
35121
- tableName: `__old_push_${tableName}`,
35122
- schema: ""
35123
- })
35124
- );
35125
- for (const idx of Object.values(json.tables[tableName].indexes)) {
35126
- statements.push(
35127
- new CreateSqliteIndexConvertor().convert({
35128
- type: "create_index",
35129
- tableName,
35130
- schema: "",
35131
- data: idx
35132
- })
35133
- );
35134
- }
35135
- return statements;
35136
- };
35137
- var getOldTableName = (tableName, meta) => {
35138
- for (const key of Object.keys(meta.tables)) {
35139
- const value = meta.tables[key];
35140
- if (`"${tableName}"` === value) {
35141
- return key.substring(1, key.length - 1);
35142
- }
35143
- }
35144
- return tableName;
35145
- };
35146
- var getNewTableName = (tableName, meta) => {
35147
- if (typeof meta.tables[`"${tableName}"`] !== "undefined") {
35148
- return meta.tables[`"${tableName}"`].substring(
35149
- 1,
35150
- meta.tables[`"${tableName}"`].length - 1
35151
- );
35152
- }
35153
- return tableName;
35154
- };
35155
- var logSuggestionsAndReturn = async (connection, statements, json1, json2, meta) => {
35156
- let shouldAskForApprove = false;
35157
- const statementsToExecute = [];
35158
- const infoToPrint = [];
35159
- const tablesToRemove = [];
35160
- const columnsToRemove = [];
35161
- const schemasToRemove = [];
35162
- const tablesToTruncate = [];
35163
- const tablesContext = {};
35164
- for (const statement of statements) {
35165
- if (statement.type === "drop_table") {
35166
- const res = await connection.query(
35167
- `select count(*) as count from \`${statement.tableName}\``
35168
- );
35169
- const count2 = Number(res[0].count);
35170
- if (count2 > 0) {
35171
- infoToPrint.push(
35172
- `\xB7 You're about to delete ${source_default.underline(
35173
- statement.tableName
35174
- )} table with ${count2} items`
35175
- );
35176
- tablesToRemove.push(statement.tableName);
35177
- shouldAskForApprove = true;
35178
- }
35179
- const stmnt = fromJson([statement], "sqlite")[0];
35180
- statementsToExecute.push(stmnt);
35181
- } else if (statement.type === "alter_table_drop_column") {
35182
- const newTableName = getOldTableName(statement.tableName, meta);
35183
- const columnIsPartOfPk = Object.values(
35184
- json1.tables[newTableName].compositePrimaryKeys
35185
- ).find((c) => SQLiteSquasher.unsquashPK(c).includes(statement.columnName));
35186
- const columnIsPartOfIndex = Object.values(
35187
- json1.tables[newTableName].indexes
35188
- ).find((c) => SQLiteSquasher.unsquashIdx(c).columns.includes(statement.columnName));
35189
- const columnIsPk = json1.tables[newTableName].columns[statement.columnName].primaryKey;
35190
- const columnIsPartOfFk = Object.values(
35191
- json1.tables[newTableName].foreignKeys
35192
- ).find(
35193
- (t) => SQLiteSquasher.unsquashPushFK(t).columnsFrom.includes(
35194
- statement.columnName
35195
- )
35196
- );
35197
- const res = await connection.query(
35198
- `select count(*) as count from \`${newTableName}\``
35199
- );
35200
- const count2 = Number(res[0].count);
35201
- if (count2 > 0) {
35202
- infoToPrint.push(
35203
- `\xB7 You're about to delete ${source_default.underline(
35204
- statement.columnName
35205
- )} column in ${newTableName} table with ${count2} items`
35206
- );
35207
- columnsToRemove.push(`${newTableName}_${statement.columnName}`);
35208
- shouldAskForApprove = true;
35209
- }
35210
- if (columnIsPk || columnIsPartOfPk || columnIsPartOfIndex || columnIsPartOfFk) {
35211
- tablesContext[newTableName] = [
35212
- ..._moveDataStatements(statement.tableName, json2, true)
35213
- ];
35214
- const tablesReferncingCurrent = [];
35215
- for (const table4 of Object.values(json1.tables)) {
35216
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35217
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35218
- ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35219
- tablesReferncingCurrent.push(...tablesRefs);
35220
- }
35221
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35222
- for (const table4 of uniqueTableRefs) {
35223
- if (typeof tablesContext[table4] === "undefined") {
35224
- tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35225
- }
35226
- }
35227
- } else {
35228
- if (typeof tablesContext[newTableName] === "undefined") {
35229
- const stmnt = fromJson([statement], "sqlite")[0];
35230
- statementsToExecute.push(stmnt);
35231
- }
35232
- }
35233
- } else if (statement.type === "sqlite_alter_table_add_column") {
35234
- const newTableName = getOldTableName(statement.tableName, meta);
35235
- if (statement.column.notNull && !statement.column.default) {
35236
- const res = await connection.query(
35237
- `select count(*) as count from \`${newTableName}\``
35238
- );
35239
- const count2 = Number(res[0].count);
35240
- if (count2 > 0) {
35241
- infoToPrint.push(
35242
- `\xB7 You're about to add not-null ${source_default.underline(
35243
- statement.column.name
35244
- )} column without default value, which contains ${count2} items`
35245
- );
35246
- tablesToTruncate.push(newTableName);
35247
- statementsToExecute.push(`delete from ${newTableName};`);
35248
- shouldAskForApprove = true;
35249
- }
35250
- }
35251
- if (statement.column.primaryKey) {
35252
- tablesContext[newTableName] = [
35253
- ..._moveDataStatements(statement.tableName, json2, true)
35254
- ];
35255
- const tablesReferncingCurrent = [];
35256
- for (const table4 of Object.values(json1.tables)) {
35257
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35258
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35259
- ).map((t) => SQLiteSquasher.unsquashPushFK(t).tableFrom);
35260
- tablesReferncingCurrent.push(...tablesRefs);
35261
- }
35262
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35263
- for (const table4 of uniqueTableRefs) {
35264
- if (typeof tablesContext[table4] === "undefined") {
35265
- tablesContext[table4] = [..._moveDataStatements(table4, json2)];
35266
- }
35267
- }
35268
- } else {
35269
- if (typeof tablesContext[newTableName] === "undefined") {
35270
- const stmnt = fromJson([statement], "sqlite")[0];
35271
- statementsToExecute.push(stmnt);
35272
- }
35273
- }
35274
- } 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") {
35275
- if (!(statement.type === "alter_table_alter_column_set_notnull" && statement.columnPk)) {
35276
- const newTableName = getOldTableName(statement.tableName, meta);
35277
- if (statement.type === "alter_table_alter_column_set_notnull" && typeof statement.columnDefault === "undefined") {
35278
- const res = await connection.query(
35279
- `select count(*) as count from \`${newTableName}\``
35280
- );
35281
- const count2 = Number(res[0].count);
35282
- if (count2 > 0) {
35283
- infoToPrint.push(
35284
- `\xB7 You're about to add not-null constraint to ${source_default.underline(
35285
- statement.columnName
35286
- )} column without default value, which contains ${count2} items`
35287
- );
35288
- tablesToTruncate.push(newTableName);
35289
- shouldAskForApprove = true;
35290
- }
35291
- tablesContext[newTableName] = _moveDataStatements(
35292
- statement.tableName,
35293
- json1,
35294
- true
35295
- );
35296
- } else {
35297
- if (typeof tablesContext[newTableName] === "undefined") {
35298
- tablesContext[newTableName] = _moveDataStatements(
35299
- statement.tableName,
35300
- json1
35301
- );
35302
- }
35303
- }
35304
- const tablesReferncingCurrent = [];
35305
- for (const table4 of Object.values(json1.tables)) {
35306
- const tablesRefs = Object.values(json1.tables[table4.name].foreignKeys).filter(
35307
- (t) => SQLiteSquasher.unsquashPushFK(t).tableTo === newTableName
35308
- ).map((t) => {
35309
- return getNewTableName(
35310
- SQLiteSquasher.unsquashPushFK(t).tableFrom,
35311
- meta
35312
- );
35313
- });
35314
- tablesReferncingCurrent.push(...tablesRefs);
35315
- }
35316
- const uniqueTableRefs = [...new Set(tablesReferncingCurrent)];
35317
- for (const table4 of uniqueTableRefs) {
35318
- if (typeof tablesContext[table4] === "undefined") {
35319
- tablesContext[table4] = [..._moveDataStatements(table4, json1)];
35320
- }
35321
- }
35322
- }
35323
- } else if (statement.type === "create_reference" || statement.type === "delete_reference" || statement.type === "alter_reference") {
35324
- const fk4 = SQLiteSquasher.unsquashPushFK(statement.data);
35325
- if (typeof tablesContext[statement.tableName] === "undefined") {
35326
- tablesContext[statement.tableName] = _moveDataStatements(
35327
- statement.tableName,
35328
- json2
35329
- );
35330
- }
35331
- } 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") {
35332
- const newTableName = getOldTableName(statement.tableName, meta);
35333
- if (typeof tablesContext[newTableName] === "undefined") {
35334
- tablesContext[newTableName] = _moveDataStatements(
35335
- statement.tableName,
35336
- json2
35337
- );
35338
- }
35339
- } else {
35340
- const stmnt = fromJson([statement], "sqlite");
35341
- if (typeof stmnt !== "undefined") {
35342
- statementsToExecute.push(...stmnt);
35343
- }
35344
- }
35345
- }
35346
- for (const context of Object.values(tablesContext)) {
35347
- statementsToExecute.push(...context);
35348
- }
35349
- return {
35350
- statementsToExecute,
35351
- shouldAskForApprove,
35352
- infoToPrint,
35353
- columnsToRemove: [...new Set(columnsToRemove)],
35354
- schemasToRemove: [...new Set(schemasToRemove)],
35355
- tablesToTruncate: [...new Set(tablesToTruncate)],
35356
- tablesToRemove: [...new Set(tablesToRemove)]
35357
- };
35358
- };
35359
-
35360
35944
  // src/api.ts
35945
+ init_sqlitePushUtils();
35361
35946
  init_global();
35362
35947
  init_migrationPreparator();
35363
35948
  init_mysqlSchema();