drizzle-kit 0.30.2-471d797 → 0.30.2-88fe396

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/api.js +633 -337
  2. package/api.mjs +633 -337
  3. package/bin.cjs +238 -37
  4. package/package.json +1 -1
package/api.js CHANGED
@@ -12121,7 +12121,7 @@ function fromJson(statements, dialect6, action, json22) {
12121
12121
  }).filter((it) => it !== "");
12122
12122
  return result;
12123
12123
  }
12124
- var parseType, Convertor, PgCreateRoleConvertor, PgDropRoleConvertor, PgRenameRoleConvertor, PgAlterRoleConvertor, PgCreatePolicyConvertor, PgDropPolicyConvertor, PgRenamePolicyConvertor, PgAlterPolicyConvertor, PgCreateIndPolicyConvertor, PgDropIndPolicyConvertor, PgRenameIndPolicyConvertor, PgAlterIndPolicyConvertor, PgEnableRlsConvertor, PgDisableRlsConvertor, PgCreateTableConvertor, MySqlCreateTableConvertor, SingleStoreCreateTableConvertor, SQLiteCreateTableConvertor, PgCreateViewConvertor, MySqlCreateViewConvertor, SqliteCreateViewConvertor, PgDropViewConvertor, MySqlDropViewConvertor, SqliteDropViewConvertor, MySqlAlterViewConvertor, PgRenameViewConvertor, MySqlRenameViewConvertor, PgAlterViewSchemaConvertor, PgAlterViewAddWithOptionConvertor, PgAlterViewDropWithOptionConvertor, PgAlterViewAlterTablespaceConvertor, PgAlterViewAlterUsingConvertor, PgAlterTableAlterColumnSetGenerated, PgAlterTableAlterColumnDropGenerated, PgAlterTableAlterColumnAlterGenerated, PgAlterTableAddUniqueConstraintConvertor, PgAlterTableDropUniqueConstraintConvertor, PgAlterTableAddCheckConstraintConvertor, PgAlterTableDeleteCheckConstraintConvertor, MySQLAlterTableAddUniqueConstraintConvertor, MySQLAlterTableDropUniqueConstraintConvertor, MySqlAlterTableAddCheckConstraintConvertor, SingleStoreAlterTableAddUniqueConstraintConvertor, SingleStoreAlterTableDropUniqueConstraintConvertor, MySqlAlterTableDeleteCheckConstraintConvertor, CreatePgSequenceConvertor, DropPgSequenceConvertor, RenamePgSequenceConvertor, MovePgSequenceConvertor, AlterPgSequenceConvertor, CreateTypeEnumConvertor, DropTypeEnumConvertor, AlterTypeAddValueConvertor, AlterTypeSetSchemaConvertor, AlterRenameTypeConvertor, AlterTypeDropValueConvertor, PgDropTableConvertor, MySQLDropTableConvertor, SingleStoreDropTableConvertor, SQLiteDropTableConvertor, PgRenameTableConvertor, SqliteRenameTableConvertor, MySqlRenameTableConvertor, SingleStoreRenameTableConvertor, PgAlterTableRenameColumnConvertor, MySqlAlterTableRenameColumnConvertor, SingleStoreAlterTableRenameColumnConvertor, SQLiteAlterTableRenameColumnConvertor, PgAlterTableDropColumnConvertor, MySqlAlterTableDropColumnConvertor, SingleStoreAlterTableDropColumnConvertor, SQLiteAlterTableDropColumnConvertor, PgAlterTableAddColumnConvertor, MySqlAlterTableAddColumnConvertor, SingleStoreAlterTableAddColumnConvertor, SQLiteAlterTableAddColumnConvertor, PgAlterTableAlterColumnSetTypeConvertor, PgAlterTableAlterColumnSetDefaultConvertor, PgAlterTableAlterColumnDropDefaultConvertor, PgAlterTableAlterColumnDropGeneratedConvertor, PgAlterTableAlterColumnSetExpressionConvertor, PgAlterTableAlterColumnAlterrGeneratedConvertor, SqliteAlterTableAlterColumnDropGeneratedConvertor, SqliteAlterTableAlterColumnSetExpressionConvertor, SqliteAlterTableAlterColumnAlterGeneratedConvertor, MySqlAlterTableAlterColumnAlterrGeneratedConvertor, MySqlAlterTableAddPk, MySqlAlterTableDropPk, LibSQLModifyColumn, MySqlModifyColumn, SingleStoreAlterTableAlterColumnAlterrGeneratedConvertor, SingleStoreAlterTableAddPk, SingleStoreAlterTableDropPk, SingleStoreModifyColumn, PgAlterTableCreateCompositePrimaryKeyConvertor, PgAlterTableDeleteCompositePrimaryKeyConvertor, PgAlterTableAlterCompositePrimaryKeyConvertor, MySqlAlterTableCreateCompositePrimaryKeyConvertor, MySqlAlterTableDeleteCompositePrimaryKeyConvertor, MySqlAlterTableAlterCompositePrimaryKeyConvertor, PgAlterTableAlterColumnSetPrimaryKeyConvertor, PgAlterTableAlterColumnDropPrimaryKeyConvertor, PgAlterTableAlterColumnSetNotNullConvertor, PgAlterTableAlterColumnDropNotNullConvertor, PgCreateForeignKeyConvertor, LibSQLCreateForeignKeyConvertor, MySqlCreateForeignKeyConvertor, PgAlterForeignKeyConvertor, PgDeleteForeignKeyConvertor, MySqlDeleteForeignKeyConvertor, CreatePgIndexConvertor, CreateMySqlIndexConvertor, CreateSingleStoreIndexConvertor, CreateSqliteIndexConvertor, PgDropIndexConvertor, PgCreateSchemaConvertor, PgRenameSchemaConvertor, PgDropSchemaConvertor, PgAlterTableSetSchemaConvertor, PgAlterTableSetNewSchemaConvertor, PgAlterTableRemoveFromSchemaConvertor, SqliteDropIndexConvertor, MySqlDropIndexConvertor, SingleStoreDropIndexConvertor, SQLiteRecreateTableConvertor, LibSQLRecreateTableConvertor, convertors;
12124
+ var parseType, Convertor, PgCreateRoleConvertor, PgDropRoleConvertor, PgRenameRoleConvertor, PgAlterRoleConvertor, PgCreatePolicyConvertor, PgDropPolicyConvertor, PgRenamePolicyConvertor, PgAlterPolicyConvertor, PgCreateIndPolicyConvertor, PgDropIndPolicyConvertor, PgRenameIndPolicyConvertor, PgAlterIndPolicyConvertor, PgEnableRlsConvertor, PgDisableRlsConvertor, PgCreateTableConvertor, MySqlCreateTableConvertor, SingleStoreCreateTableConvertor, SQLiteCreateTableConvertor, PgCreateViewConvertor, MySqlCreateViewConvertor, SqliteCreateViewConvertor, PgDropViewConvertor, MySqlDropViewConvertor, SqliteDropViewConvertor, MySqlAlterViewConvertor, PgRenameViewConvertor, MySqlRenameViewConvertor, PgAlterViewSchemaConvertor, PgAlterViewAddWithOptionConvertor, PgAlterViewDropWithOptionConvertor, PgAlterViewAlterTablespaceConvertor, PgAlterViewAlterUsingConvertor, PgAlterTableAlterColumnSetGenerated, PgAlterTableAlterColumnDropGenerated, PgAlterTableAlterColumnAlterGenerated, PgAlterTableAddUniqueConstraintConvertor, PgAlterTableDropUniqueConstraintConvertor, PgAlterTableAddCheckConstraintConvertor, PgAlterTableDeleteCheckConstraintConvertor, MySQLAlterTableAddUniqueConstraintConvertor, MySQLAlterTableDropUniqueConstraintConvertor, MySqlAlterTableAddCheckConstraintConvertor, SingleStoreAlterTableAddUniqueConstraintConvertor, SingleStoreAlterTableDropUniqueConstraintConvertor, MySqlAlterTableDeleteCheckConstraintConvertor, CreatePgSequenceConvertor, DropPgSequenceConvertor, RenamePgSequenceConvertor, MovePgSequenceConvertor, AlterPgSequenceConvertor, CreateTypeEnumConvertor, DropTypeEnumConvertor, AlterTypeAddValueConvertor, AlterTypeSetSchemaConvertor, AlterRenameTypeConvertor, AlterTypeDropValueConvertor, PgDropTableConvertor, MySQLDropTableConvertor, SingleStoreDropTableConvertor, SQLiteDropTableConvertor, PgRenameTableConvertor, SqliteRenameTableConvertor, MySqlRenameTableConvertor, SingleStoreRenameTableConvertor, PgAlterTableRenameColumnConvertor, MySqlAlterTableRenameColumnConvertor, SingleStoreAlterTableRenameColumnConvertor, SQLiteAlterTableRenameColumnConvertor, PgAlterTableDropColumnConvertor, MySqlAlterTableDropColumnConvertor, SingleStoreAlterTableDropColumnConvertor, SQLiteAlterTableDropColumnConvertor, PgAlterTableAddColumnConvertor, MySqlAlterTableAddColumnConvertor, SingleStoreAlterTableAddColumnConvertor, SQLiteAlterTableAddColumnConvertor, PgAlterTableAlterColumnSetTypeConvertor, PgAlterTableAlterColumnSetDefaultConvertor, PgAlterTableAlterColumnDropDefaultConvertor, PgAlterTableAlterColumnDropGeneratedConvertor, PgAlterTableAlterColumnSetExpressionConvertor, PgAlterTableAlterColumnAlterrGeneratedConvertor, SqliteAlterTableAlterColumnDropGeneratedConvertor, SqliteAlterTableAlterColumnSetExpressionConvertor, SqliteAlterTableAlterColumnAlterGeneratedConvertor, MySqlAlterTableAlterColumnAlterrGeneratedConvertor, MySqlAlterTableAddPk, MySqlAlterTableDropPk, LibSQLModifyColumn, MySqlModifyColumn, SingleStoreAlterTableAlterColumnAlterrGeneratedConvertor, SingleStoreAlterTableAddPk, SingleStoreAlterTableDropPk, SingleStoreModifyColumn, PgAlterTableCreateCompositePrimaryKeyConvertor, PgAlterTableDeleteCompositePrimaryKeyConvertor, PgAlterTableAlterCompositePrimaryKeyConvertor, MySqlAlterTableCreateCompositePrimaryKeyConvertor, MySqlAlterTableDeleteCompositePrimaryKeyConvertor, MySqlAlterTableAlterCompositePrimaryKeyConvertor, PgAlterTableAlterColumnSetPrimaryKeyConvertor, PgAlterTableAlterColumnDropPrimaryKeyConvertor, PgAlterTableAlterColumnSetNotNullConvertor, PgAlterTableAlterColumnDropNotNullConvertor, PgCreateForeignKeyConvertor, LibSQLCreateForeignKeyConvertor, MySqlCreateForeignKeyConvertor, PgAlterForeignKeyConvertor, PgDeleteForeignKeyConvertor, MySqlDeleteForeignKeyConvertor, CreatePgIndexConvertor, CreateMySqlIndexConvertor, CreateSingleStoreIndexConvertor, CreateSqliteIndexConvertor, PgDropIndexConvertor, PgCreateSchemaConvertor, PgRenameSchemaConvertor, PgDropSchemaConvertor, PgAlterTableSetSchemaConvertor, PgAlterTableSetNewSchemaConvertor, PgAlterTableRemoveFromSchemaConvertor, SqliteDropIndexConvertor, MySqlDropIndexConvertor, SingleStoreDropIndexConvertor, SQLiteRecreateTableConvertor, LibSQLRecreateTableConvertor, SingleStoreRecreateTableConvertor, convertors;
12125
12125
  var init_sqlgenerator = __esm({
12126
12126
  "src/sqlgenerator.ts"() {
12127
12127
  "use strict";
@@ -12467,7 +12467,7 @@ var init_sqlgenerator = __esm({
12467
12467
  if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
12468
12468
  statement += ",\n";
12469
12469
  const compositePK5 = SingleStoreSquasher.unsquashPK(compositePKs[0]);
12470
- statement += ` CONSTRAINT \`${st.compositePkName}\` PRIMARY KEY(\`${compositePK5.columns.join(`\`,\``)}\`)`;
12470
+ statement += ` CONSTRAINT \`${compositePK5.name}\` PRIMARY KEY(\`${compositePK5.columns.join(`\`,\``)}\`)`;
12471
12471
  }
12472
12472
  if (typeof uniqueConstraints !== "undefined" && uniqueConstraints.length > 0) {
12473
12473
  for (const uniqueConstraint5 of uniqueConstraints) {
@@ -13122,7 +13122,7 @@ WITH ${withCheckOption} CHECK OPTION` : "";
13122
13122
  }
13123
13123
  convert(statement) {
13124
13124
  const { tableNameFrom, tableNameTo } = statement;
13125
- return `RENAME TABLE \`${tableNameFrom}\` TO \`${tableNameTo}\`;`;
13125
+ return `ALTER TABLE \`${tableNameFrom}\` RENAME TO \`${tableNameTo}\`;`;
13126
13126
  }
13127
13127
  };
13128
13128
  PgAlterTableRenameColumnConvertor = class extends Convertor {
@@ -13150,7 +13150,7 @@ WITH ${withCheckOption} CHECK OPTION` : "";
13150
13150
  }
13151
13151
  convert(statement) {
13152
13152
  const { tableName, oldColumnName, newColumnName } = statement;
13153
- return `ALTER TABLE \`${tableName}\` RENAME COLUMN \`${oldColumnName}\` TO \`${newColumnName}\`;`;
13153
+ return `ALTER TABLE \`${tableName}\` CHANGE \`${oldColumnName}\` \`${newColumnName}\`;`;
13154
13154
  }
13155
13155
  };
13156
13156
  SQLiteAlterTableRenameColumnConvertor = class extends Convertor {
@@ -14460,10 +14460,52 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.newC
14460
14460
  return sqlStatements;
14461
14461
  }
14462
14462
  };
14463
+ SingleStoreRecreateTableConvertor = class extends Convertor {
14464
+ can(statement, dialect6) {
14465
+ return statement.type === "singlestore_recreate_table" && dialect6 === "singlestore";
14466
+ }
14467
+ convert(statement) {
14468
+ const { tableName, columns, compositePKs, uniqueConstraints } = statement;
14469
+ const columnNames = columns.map((it) => `\`${it.name}\``).join(", ");
14470
+ const newTableName = `__new_${tableName}`;
14471
+ const sqlStatements = [];
14472
+ sqlStatements.push(
14473
+ new SingleStoreCreateTableConvertor().convert({
14474
+ type: "create_table",
14475
+ tableName: newTableName,
14476
+ columns,
14477
+ compositePKs,
14478
+ uniqueConstraints,
14479
+ schema: ""
14480
+ })
14481
+ );
14482
+ sqlStatements.push(
14483
+ `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
14484
+ );
14485
+ sqlStatements.push(
14486
+ new SingleStoreDropTableConvertor().convert({
14487
+ type: "drop_table",
14488
+ tableName,
14489
+ schema: ""
14490
+ })
14491
+ );
14492
+ sqlStatements.push(
14493
+ new SingleStoreRenameTableConvertor().convert({
14494
+ fromSchema: "",
14495
+ tableNameFrom: newTableName,
14496
+ tableNameTo: tableName,
14497
+ toSchema: "",
14498
+ type: "rename_table"
14499
+ })
14500
+ );
14501
+ return sqlStatements;
14502
+ }
14503
+ };
14463
14504
  convertors = [];
14464
14505
  convertors.push(new PgCreateTableConvertor());
14465
14506
  convertors.push(new MySqlCreateTableConvertor());
14466
14507
  convertors.push(new SingleStoreCreateTableConvertor());
14508
+ convertors.push(new SingleStoreRecreateTableConvertor());
14467
14509
  convertors.push(new SQLiteCreateTableConvertor());
14468
14510
  convertors.push(new SQLiteRecreateTableConvertor());
14469
14511
  convertors.push(new LibSQLRecreateTableConvertor());
@@ -16459,7 +16501,7 @@ var init_jsonStatements = __esm({
16459
16501
  });
16460
16502
 
16461
16503
  // src/statementCombiner.ts
16462
- var prepareLibSQLRecreateTable, prepareSQLiteRecreateTable, libSQLCombineStatements, sqliteCombineStatements;
16504
+ var prepareLibSQLRecreateTable, prepareSQLiteRecreateTable, libSQLCombineStatements, sqliteCombineStatements, prepareSingleStoreRecreateTable, singleStoreCombineStatements;
16463
16505
  var init_statementCombiner = __esm({
16464
16506
  "src/statementCombiner.ts"() {
16465
16507
  "use strict";
@@ -16738,6 +16780,101 @@ var init_statementCombiner = __esm({
16738
16780
  const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
16739
16781
  return [...renamedTables, ...renamedColumns, ...rest];
16740
16782
  };
16783
+ prepareSingleStoreRecreateTable = (table5) => {
16784
+ const { name: name2, columns, uniqueConstraints, indexes, compositePrimaryKeys } = table5;
16785
+ const composites = Object.values(compositePrimaryKeys);
16786
+ const statements = [
16787
+ {
16788
+ type: "singlestore_recreate_table",
16789
+ tableName: name2,
16790
+ columns: Object.values(columns),
16791
+ compositePKs: composites,
16792
+ uniqueConstraints: Object.values(uniqueConstraints)
16793
+ }
16794
+ ];
16795
+ if (Object.keys(indexes).length) {
16796
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
16797
+ }
16798
+ return statements;
16799
+ };
16800
+ singleStoreCombineStatements = (statements, json22) => {
16801
+ const newStatements = {};
16802
+ for (const statement of statements) {
16803
+ if (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_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") {
16804
+ const tableName2 = statement.tableName;
16805
+ const statementsForTable2 = newStatements[tableName2];
16806
+ if (!statementsForTable2) {
16807
+ newStatements[tableName2] = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16808
+ continue;
16809
+ }
16810
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
16811
+ const wasRename = statementsForTable2.some(
16812
+ ({ type }) => type === "rename_table" || type === "alter_table_rename_column"
16813
+ );
16814
+ const preparedStatements = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16815
+ if (wasRename) {
16816
+ newStatements[tableName2].push(...preparedStatements);
16817
+ } else {
16818
+ newStatements[tableName2] = preparedStatements;
16819
+ }
16820
+ continue;
16821
+ }
16822
+ continue;
16823
+ }
16824
+ if ((statement.type === "alter_table_alter_column_drop_default" || statement.type === "alter_table_alter_column_set_default") && statement.columnNotNull) {
16825
+ const tableName2 = statement.tableName;
16826
+ const statementsForTable2 = newStatements[tableName2];
16827
+ if (!statementsForTable2) {
16828
+ newStatements[tableName2] = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16829
+ continue;
16830
+ }
16831
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
16832
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
16833
+ const preparedStatements = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16834
+ if (wasRename) {
16835
+ newStatements[tableName2].push(...preparedStatements);
16836
+ } else {
16837
+ newStatements[tableName2] = preparedStatements;
16838
+ }
16839
+ continue;
16840
+ }
16841
+ continue;
16842
+ }
16843
+ if (statement.type === "alter_table_add_column" && statement.column.primaryKey) {
16844
+ const tableName2 = statement.tableName;
16845
+ const statementsForTable2 = newStatements[tableName2];
16846
+ if (!statementsForTable2) {
16847
+ newStatements[tableName2] = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16848
+ continue;
16849
+ }
16850
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
16851
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
16852
+ const preparedStatements = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16853
+ if (wasRename) {
16854
+ newStatements[tableName2].push(...preparedStatements);
16855
+ } else {
16856
+ newStatements[tableName2] = preparedStatements;
16857
+ }
16858
+ continue;
16859
+ }
16860
+ continue;
16861
+ }
16862
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
16863
+ const statementsForTable = newStatements[tableName];
16864
+ if (!statementsForTable) {
16865
+ newStatements[tableName] = [statement];
16866
+ continue;
16867
+ }
16868
+ if (!statementsForTable.some(({ type }) => type === "singlestore_recreate_table")) {
16869
+ newStatements[tableName].push(statement);
16870
+ }
16871
+ }
16872
+ const combinedStatements = Object.values(newStatements).flat();
16873
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
16874
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
16875
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
16876
+ return [...renamedTables, ...renamedColumns, ...rest];
16877
+ };
16741
16878
  }
16742
16879
  });
16743
16880
 
@@ -18683,7 +18820,7 @@ var init_snapshotsDiffer = __esm({
18683
18820
  return [tableKey2, tableValue];
18684
18821
  }
18685
18822
  );
18686
- const diffResult = applyJsonDiff(tablesPatchedSnap1, json22);
18823
+ const diffResult = applyJsonDiff(columnsPatchedSnap1, json22);
18687
18824
  const typedResult = diffResultSchemeSingleStore.parse(diffResult);
18688
18825
  const jsonStatements = [];
18689
18826
  const jsonCreateIndexesForCreatedTables = createdTables.map((it) => {
@@ -18847,7 +18984,8 @@ var init_snapshotsDiffer = __esm({
18847
18984
  jsonStatements.push(...jsonDropColumnsStatemets);
18848
18985
  jsonStatements.push(...jsonAddedCompositePKs);
18849
18986
  jsonStatements.push(...jsonAlteredUniqueConstraints);
18850
- const sqlStatements = fromJson(jsonStatements, "singlestore");
18987
+ const combinedJsonStatements = singleStoreCombineStatements(jsonStatements, json22);
18988
+ const sqlStatements = fromJson(combinedJsonStatements, "singlestore");
18851
18989
  const uniqueSqlStatements = [];
18852
18990
  sqlStatements.forEach((ss) => {
18853
18991
  if (!uniqueSqlStatements.includes(ss)) {
@@ -18859,7 +18997,7 @@ var init_snapshotsDiffer = __esm({
18859
18997
  });
18860
18998
  const _meta = prepareMigrationMeta([], rTables, rColumns);
18861
18999
  return {
18862
- statements: jsonStatements,
19000
+ statements: combinedJsonStatements,
18863
19001
  sqlStatements: uniqueSqlStatements,
18864
19002
  _meta
18865
19003
  };
@@ -22165,6 +22303,9 @@ function fillPlaceholders(params, values) {
22165
22303
  function isView(view4) {
22166
22304
  return typeof view4 === "object" && view4 !== null && IsDrizzleView in view4;
22167
22305
  }
22306
+ function getViewName(view4) {
22307
+ return view4[ViewBaseConfig].name;
22308
+ }
22168
22309
  var _a19, FakePrimitiveParam, _a20, StringChunk, _a21, _SQL, SQL, _a22, Name, noopDecoder, noopEncoder, noopMapper, _a23, Param, _a24, Placeholder, IsDrizzleView, _a25, _b10, _c2, View3;
22169
22310
  var init_sql = __esm({
22170
22311
  "../drizzle-orm/dist/sql/sql.js"() {
@@ -25291,6 +25432,7 @@ __export(dist_exports, {
25291
25432
  getTableLikeName: () => getTableLikeName,
25292
25433
  getTableName: () => getTableName,
25293
25434
  getTableUniqueName: () => getTableUniqueName,
25435
+ getViewName: () => getViewName,
25294
25436
  getViewSelectedFields: () => getViewSelectedFields,
25295
25437
  gt: () => gt,
25296
25438
  gte: () => gte,
@@ -25445,17 +25587,100 @@ var init_columns = __esm({
25445
25587
  }
25446
25588
  });
25447
25589
 
25590
+ // ../drizzle-orm/dist/selection-proxy.js
25591
+ var _a124, _SelectionProxyHandler, SelectionProxyHandler;
25592
+ var init_selection_proxy = __esm({
25593
+ "../drizzle-orm/dist/selection-proxy.js"() {
25594
+ "use strict";
25595
+ init_alias();
25596
+ init_column();
25597
+ init_entity();
25598
+ init_sql();
25599
+ init_subquery();
25600
+ init_view_common();
25601
+ _a124 = entityKind;
25602
+ _SelectionProxyHandler = class _SelectionProxyHandler {
25603
+ constructor(config) {
25604
+ __publicField(this, "config");
25605
+ this.config = { ...config };
25606
+ }
25607
+ get(subquery, prop) {
25608
+ if (prop === "_") {
25609
+ return {
25610
+ ...subquery["_"],
25611
+ selectedFields: new Proxy(
25612
+ subquery._.selectedFields,
25613
+ this
25614
+ )
25615
+ };
25616
+ }
25617
+ if (prop === ViewBaseConfig) {
25618
+ return {
25619
+ ...subquery[ViewBaseConfig],
25620
+ selectedFields: new Proxy(
25621
+ subquery[ViewBaseConfig].selectedFields,
25622
+ this
25623
+ )
25624
+ };
25625
+ }
25626
+ if (typeof prop === "symbol") {
25627
+ return subquery[prop];
25628
+ }
25629
+ const columns = is(subquery, Subquery) ? subquery._.selectedFields : is(subquery, View3) ? subquery[ViewBaseConfig].selectedFields : subquery;
25630
+ const value = columns[prop];
25631
+ if (is(value, SQL.Aliased)) {
25632
+ if (this.config.sqlAliasedBehavior === "sql" && !value.isSelectionField) {
25633
+ return value.sql;
25634
+ }
25635
+ const newValue = value.clone();
25636
+ newValue.isSelectionField = true;
25637
+ return newValue;
25638
+ }
25639
+ if (is(value, SQL)) {
25640
+ if (this.config.sqlBehavior === "sql") {
25641
+ return value;
25642
+ }
25643
+ throw new Error(
25644
+ `You tried to reference "${prop}" field from a subquery, which is a raw SQL field, but it doesn't have an alias declared. Please add an alias to the field using ".as('alias')" method.`
25645
+ );
25646
+ }
25647
+ if (is(value, Column2)) {
25648
+ if (this.config.alias) {
25649
+ return new Proxy(
25650
+ value,
25651
+ new ColumnAliasProxyHandler(
25652
+ new Proxy(
25653
+ value.table,
25654
+ new TableAliasProxyHandler(this.config.alias, this.config.replaceOriginalName ?? false)
25655
+ )
25656
+ )
25657
+ );
25658
+ }
25659
+ return value;
25660
+ }
25661
+ if (typeof value !== "object" || value === null) {
25662
+ return value;
25663
+ }
25664
+ return new Proxy(value, new _SelectionProxyHandler(this.config));
25665
+ }
25666
+ };
25667
+ __publicField(_SelectionProxyHandler, _a124, "SelectionProxyHandler");
25668
+ SelectionProxyHandler = _SelectionProxyHandler;
25669
+ }
25670
+ });
25671
+
25448
25672
  // ../drizzle-orm/dist/pg-core/query-builders/delete.js
25449
- var _a124, _b97, PgDeleteBase;
25673
+ var _a125, _b97, PgDeleteBase;
25450
25674
  var init_delete = __esm({
25451
25675
  "../drizzle-orm/dist/pg-core/query-builders/delete.js"() {
25452
25676
  "use strict";
25453
25677
  init_entity();
25454
25678
  init_query_promise();
25679
+ init_selection_proxy();
25455
25680
  init_table();
25456
25681
  init_tracing();
25457
25682
  init_utils2();
25458
- PgDeleteBase = class extends (_b97 = QueryPromise, _a124 = entityKind, _b97) {
25683
+ PgDeleteBase = class extends (_b97 = QueryPromise, _a125 = entityKind, _b97) {
25459
25684
  constructor(table5, session, dialect6, withList) {
25460
25685
  super();
25461
25686
  __publicField(this, "config");
@@ -25503,6 +25728,7 @@ var init_delete = __esm({
25503
25728
  return this;
25504
25729
  }
25505
25730
  returning(fields = this.config.table[Table2.Symbol.Columns]) {
25731
+ this.config.returningFields = fields;
25506
25732
  this.config.returning = orderSelectedFields(fields);
25507
25733
  return this;
25508
25734
  }
@@ -25528,11 +25754,22 @@ var init_delete = __esm({
25528
25754
  this.authToken = token;
25529
25755
  return this;
25530
25756
  }
25757
+ /** @internal */
25758
+ getSelectedFields() {
25759
+ return this.config.returningFields ? new Proxy(
25760
+ this.config.returningFields,
25761
+ new SelectionProxyHandler({
25762
+ alias: getTableName(this.config.table),
25763
+ sqlAliasedBehavior: "alias",
25764
+ sqlBehavior: "error"
25765
+ })
25766
+ ) : void 0;
25767
+ }
25531
25768
  $dynamic() {
25532
25769
  return this;
25533
25770
  }
25534
25771
  };
25535
- __publicField(PgDeleteBase, _a124, "PgDelete");
25772
+ __publicField(PgDeleteBase, _a125, "PgDelete");
25536
25773
  }
25537
25774
  });
25538
25775
 
@@ -25551,13 +25788,13 @@ function toCamelCase(input) {
25551
25788
  function noopCase(input) {
25552
25789
  return input;
25553
25790
  }
25554
- var _a125, CasingCache;
25791
+ var _a126, CasingCache;
25555
25792
  var init_casing = __esm({
25556
25793
  "../drizzle-orm/dist/casing.js"() {
25557
25794
  "use strict";
25558
25795
  init_entity();
25559
25796
  init_table();
25560
- _a125 = entityKind;
25797
+ _a126 = entityKind;
25561
25798
  CasingCache = class {
25562
25799
  constructor(casing2) {
25563
25800
  /** @internal */
@@ -25594,25 +25831,25 @@ var init_casing = __esm({
25594
25831
  this.cachedTables = {};
25595
25832
  }
25596
25833
  };
25597
- __publicField(CasingCache, _a125, "CasingCache");
25834
+ __publicField(CasingCache, _a126, "CasingCache");
25598
25835
  }
25599
25836
  });
25600
25837
 
25601
25838
  // ../drizzle-orm/dist/pg-core/view-base.js
25602
- var _a126, _b98, PgViewBase;
25839
+ var _a127, _b98, PgViewBase;
25603
25840
  var init_view_base = __esm({
25604
25841
  "../drizzle-orm/dist/pg-core/view-base.js"() {
25605
25842
  "use strict";
25606
25843
  init_entity();
25607
25844
  init_sql();
25608
- PgViewBase = class extends (_b98 = View3, _a126 = entityKind, _b98) {
25845
+ PgViewBase = class extends (_b98 = View3, _a127 = entityKind, _b98) {
25609
25846
  };
25610
- __publicField(PgViewBase, _a126, "PgViewBase");
25847
+ __publicField(PgViewBase, _a127, "PgViewBase");
25611
25848
  }
25612
25849
  });
25613
25850
 
25614
25851
  // ../drizzle-orm/dist/pg-core/dialect.js
25615
- var _a127, PgDialect;
25852
+ var _a128, PgDialect;
25616
25853
  var init_dialect = __esm({
25617
25854
  "../drizzle-orm/dist/pg-core/dialect.js"() {
25618
25855
  "use strict";
@@ -25631,7 +25868,7 @@ var init_dialect = __esm({
25631
25868
  init_utils2();
25632
25869
  init_view_common();
25633
25870
  init_view_base();
25634
- _a127 = entityKind;
25871
+ _a128 = entityKind;
25635
25872
  PgDialect = class {
25636
25873
  constructor(config) {
25637
25874
  /** @internal */
@@ -26723,89 +26960,7 @@ var init_dialect = __esm({
26723
26960
  };
26724
26961
  }
26725
26962
  };
26726
- __publicField(PgDialect, _a127, "PgDialect");
26727
- }
26728
- });
26729
-
26730
- // ../drizzle-orm/dist/selection-proxy.js
26731
- var _a128, _SelectionProxyHandler, SelectionProxyHandler;
26732
- var init_selection_proxy = __esm({
26733
- "../drizzle-orm/dist/selection-proxy.js"() {
26734
- "use strict";
26735
- init_alias();
26736
- init_column();
26737
- init_entity();
26738
- init_sql();
26739
- init_subquery();
26740
- init_view_common();
26741
- _a128 = entityKind;
26742
- _SelectionProxyHandler = class _SelectionProxyHandler {
26743
- constructor(config) {
26744
- __publicField(this, "config");
26745
- this.config = { ...config };
26746
- }
26747
- get(subquery, prop) {
26748
- if (prop === "_") {
26749
- return {
26750
- ...subquery["_"],
26751
- selectedFields: new Proxy(
26752
- subquery._.selectedFields,
26753
- this
26754
- )
26755
- };
26756
- }
26757
- if (prop === ViewBaseConfig) {
26758
- return {
26759
- ...subquery[ViewBaseConfig],
26760
- selectedFields: new Proxy(
26761
- subquery[ViewBaseConfig].selectedFields,
26762
- this
26763
- )
26764
- };
26765
- }
26766
- if (typeof prop === "symbol") {
26767
- return subquery[prop];
26768
- }
26769
- const columns = is(subquery, Subquery) ? subquery._.selectedFields : is(subquery, View3) ? subquery[ViewBaseConfig].selectedFields : subquery;
26770
- const value = columns[prop];
26771
- if (is(value, SQL.Aliased)) {
26772
- if (this.config.sqlAliasedBehavior === "sql" && !value.isSelectionField) {
26773
- return value.sql;
26774
- }
26775
- const newValue = value.clone();
26776
- newValue.isSelectionField = true;
26777
- return newValue;
26778
- }
26779
- if (is(value, SQL)) {
26780
- if (this.config.sqlBehavior === "sql") {
26781
- return value;
26782
- }
26783
- throw new Error(
26784
- `You tried to reference "${prop}" field from a subquery, which is a raw SQL field, but it doesn't have an alias declared. Please add an alias to the field using ".as('alias')" method.`
26785
- );
26786
- }
26787
- if (is(value, Column2)) {
26788
- if (this.config.alias) {
26789
- return new Proxy(
26790
- value,
26791
- new ColumnAliasProxyHandler(
26792
- new Proxy(
26793
- value.table,
26794
- new TableAliasProxyHandler(this.config.alias, this.config.replaceOriginalName ?? false)
26795
- )
26796
- )
26797
- );
26798
- }
26799
- return value;
26800
- }
26801
- if (typeof value !== "object" || value === null) {
26802
- return value;
26803
- }
26804
- return new Proxy(value, new _SelectionProxyHandler(this.config));
26805
- }
26806
- };
26807
- __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26808
- SelectionProxyHandler = _SelectionProxyHandler;
26963
+ __publicField(PgDialect, _a128, "PgDialect");
26809
26964
  }
26810
26965
  });
26811
26966
 
@@ -26890,22 +27045,23 @@ var init_select2 = __esm({
26890
27045
  */
26891
27046
  from(source) {
26892
27047
  const isPartialSelect = !!this.fields;
27048
+ const src = source;
26893
27049
  let fields;
26894
27050
  if (this.fields) {
26895
27051
  fields = this.fields;
26896
- } else if (is(source, Subquery)) {
27052
+ } else if (is(src, Subquery)) {
26897
27053
  fields = Object.fromEntries(
26898
- Object.keys(source._.selectedFields).map((key) => [key, source[key]])
27054
+ Object.keys(src._.selectedFields).map((key) => [key, src[key]])
26899
27055
  );
26900
- } else if (is(source, PgViewBase)) {
26901
- fields = source[ViewBaseConfig].selectedFields;
26902
- } else if (is(source, SQL)) {
27056
+ } else if (is(src, PgViewBase)) {
27057
+ fields = src[ViewBaseConfig].selectedFields;
27058
+ } else if (is(src, SQL)) {
26903
27059
  fields = {};
26904
27060
  } else {
26905
- fields = getTableColumns(source);
27061
+ fields = getTableColumns(src);
26906
27062
  }
26907
27063
  return new PgSelectBase({
26908
- table: source,
27064
+ table: src,
26909
27065
  fields,
26910
27066
  isPartialSelect,
26911
27067
  session: this.session,
@@ -27590,22 +27746,26 @@ var init_query_builder2 = __esm({
27590
27746
  constructor(dialect6) {
27591
27747
  __publicField(this, "dialect");
27592
27748
  __publicField(this, "dialectConfig");
27593
- this.dialect = is(dialect6, PgDialect) ? dialect6 : void 0;
27594
- this.dialectConfig = is(dialect6, PgDialect) ? void 0 : dialect6;
27595
- }
27596
- $with(alias) {
27597
- const queryBuilder = this;
27598
- return {
27599
- as(qb) {
27749
+ __publicField(this, "$with", (alias, selection) => {
27750
+ const queryBuilder = this;
27751
+ const as = (qb) => {
27600
27752
  if (typeof qb === "function") {
27601
27753
  qb = qb(queryBuilder);
27602
27754
  }
27603
27755
  return new Proxy(
27604
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
27756
+ new WithSubquery(
27757
+ qb.getSQL(),
27758
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
27759
+ alias,
27760
+ true
27761
+ ),
27605
27762
  new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
27606
27763
  );
27607
- }
27608
- };
27764
+ };
27765
+ return { as };
27766
+ });
27767
+ this.dialect = is(dialect6, PgDialect) ? dialect6 : void 0;
27768
+ this.dialectConfig = is(dialect6, PgDialect) ? void 0 : dialect6;
27609
27769
  }
27610
27770
  with(...queries) {
27611
27771
  const self2 = this;
@@ -27677,6 +27837,7 @@ var init_insert = __esm({
27677
27837
  "use strict";
27678
27838
  init_entity();
27679
27839
  init_query_promise();
27840
+ init_selection_proxy();
27680
27841
  init_sql();
27681
27842
  init_table();
27682
27843
  init_tracing();
@@ -27751,6 +27912,7 @@ var init_insert = __esm({
27751
27912
  this.config = { table: table5, values, withList, select, overridingSystemValue_ };
27752
27913
  }
27753
27914
  returning(fields = this.config.table[Table2.Symbol.Columns]) {
27915
+ this.config.returningFields = fields;
27754
27916
  this.config.returning = orderSelectedFields(fields);
27755
27917
  return this;
27756
27918
  }
@@ -27853,6 +28015,17 @@ var init_insert = __esm({
27853
28015
  this.authToken = token;
27854
28016
  return this;
27855
28017
  }
28018
+ /** @internal */
28019
+ getSelectedFields() {
28020
+ return this.config.returningFields ? new Proxy(
28021
+ this.config.returningFields,
28022
+ new SelectionProxyHandler({
28023
+ alias: getTableName(this.config.table),
28024
+ sqlAliasedBehavior: "alias",
28025
+ sqlBehavior: "error"
28026
+ })
28027
+ ) : void 0;
28028
+ }
27856
28029
  $dynamic() {
27857
28030
  return this;
27858
28031
  }
@@ -27990,11 +28163,12 @@ var init_update = __esm({
27990
28163
  this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27991
28164
  }
27992
28165
  from(source) {
27993
- const tableName = getTableLikeName(source);
28166
+ const src = source;
28167
+ const tableName = getTableLikeName(src);
27994
28168
  if (typeof tableName === "string") {
27995
28169
  this.joinsNotNullableMap[tableName] = true;
27996
28170
  }
27997
- this.config.from = source;
28171
+ this.config.from = src;
27998
28172
  return this;
27999
28173
  }
28000
28174
  getTableLikeFields(table5) {
@@ -28109,6 +28283,7 @@ var init_update = __esm({
28109
28283
  }
28110
28284
  }
28111
28285
  }
28286
+ this.config.returningFields = fields;
28112
28287
  this.config.returning = orderSelectedFields(fields);
28113
28288
  return this;
28114
28289
  }
@@ -28134,6 +28309,17 @@ var init_update = __esm({
28134
28309
  this.authToken = token;
28135
28310
  return this;
28136
28311
  }
28312
+ /** @internal */
28313
+ getSelectedFields() {
28314
+ return this.config.returningFields ? new Proxy(
28315
+ this.config.returningFields,
28316
+ new SelectionProxyHandler({
28317
+ alias: getTableName(this.config.table),
28318
+ sqlAliasedBehavior: "alias",
28319
+ sqlBehavior: "error"
28320
+ })
28321
+ ) : void 0;
28322
+ }
28137
28323
  $dynamic() {
28138
28324
  return this;
28139
28325
  }
@@ -28395,6 +28581,56 @@ var init_db = __esm({
28395
28581
  PgDatabase = class {
28396
28582
  constructor(dialect6, session, schema5) {
28397
28583
  __publicField(this, "query");
28584
+ /**
28585
+ * Creates a subquery that defines a temporary named result set as a CTE.
28586
+ *
28587
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
28588
+ *
28589
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
28590
+ *
28591
+ * @param alias The alias for the subquery.
28592
+ *
28593
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
28594
+ *
28595
+ * @example
28596
+ *
28597
+ * ```ts
28598
+ * // Create a subquery with alias 'sq' and use it in the select query
28599
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
28600
+ *
28601
+ * const result = await db.with(sq).select().from(sq);
28602
+ * ```
28603
+ *
28604
+ * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
28605
+ *
28606
+ * ```ts
28607
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
28608
+ * const sq = db.$with('sq').as(db.select({
28609
+ * name: sql<string>`upper(${users.name})`.as('name'),
28610
+ * })
28611
+ * .from(users));
28612
+ *
28613
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
28614
+ * ```
28615
+ */
28616
+ __publicField(this, "$with", (alias, selection) => {
28617
+ const self2 = this;
28618
+ const as = (qb) => {
28619
+ if (typeof qb === "function") {
28620
+ qb = qb(new QueryBuilder(self2.dialect));
28621
+ }
28622
+ return new Proxy(
28623
+ new WithSubquery(
28624
+ qb.getSQL(),
28625
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
28626
+ alias,
28627
+ true
28628
+ ),
28629
+ new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
28630
+ );
28631
+ };
28632
+ return { as };
28633
+ });
28398
28634
  __publicField(this, "authToken");
28399
28635
  this.dialect = dialect6;
28400
28636
  this.session = session;
@@ -28424,52 +28660,6 @@ var init_db = __esm({
28424
28660
  }
28425
28661
  }
28426
28662
  }
28427
- /**
28428
- * Creates a subquery that defines a temporary named result set as a CTE.
28429
- *
28430
- * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
28431
- *
28432
- * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
28433
- *
28434
- * @param alias The alias for the subquery.
28435
- *
28436
- * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
28437
- *
28438
- * @example
28439
- *
28440
- * ```ts
28441
- * // Create a subquery with alias 'sq' and use it in the select query
28442
- * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
28443
- *
28444
- * const result = await db.with(sq).select().from(sq);
28445
- * ```
28446
- *
28447
- * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
28448
- *
28449
- * ```ts
28450
- * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
28451
- * const sq = db.$with('sq').as(db.select({
28452
- * name: sql<string>`upper(${users.name})`.as('name'),
28453
- * })
28454
- * .from(users));
28455
- *
28456
- * const result = await db.with(sq).select({ name: sq.name }).from(sq);
28457
- * ```
28458
- */
28459
- $with(alias) {
28460
- const self2 = this;
28461
- return {
28462
- as(qb) {
28463
- if (typeof qb === "function") {
28464
- qb = qb(new QueryBuilder(self2.dialect));
28465
- }
28466
- return new Proxy(
28467
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
28468
- new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
28469
- );
28470
- }
28471
- };
28472
- }
28473
28663
  $count(source, filters) {
28474
28664
  return new PgCountBuilder({ source, filters, session: this.session });
28475
28665
  }
@@ -33220,22 +33410,26 @@ var init_query_builder3 = __esm({
33220
33410
  constructor(dialect6) {
33221
33411
  __publicField(this, "dialect");
33222
33412
  __publicField(this, "dialectConfig");
33223
- this.dialect = is(dialect6, SQLiteDialect) ? dialect6 : void 0;
33224
- this.dialectConfig = is(dialect6, SQLiteDialect) ? void 0 : dialect6;
33225
- }
33226
- $with(alias) {
33227
- const queryBuilder = this;
33228
- return {
33229
- as(qb) {
33413
+ __publicField(this, "$with", (alias, selection) => {
33414
+ const queryBuilder = this;
33415
+ const as = (qb) => {
33230
33416
  if (typeof qb === "function") {
33231
33417
  qb = qb(queryBuilder);
33232
33418
  }
33233
33419
  return new Proxy(
33234
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
33420
+ new WithSubquery(
33421
+ qb.getSQL(),
33422
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
33423
+ alias,
33424
+ true
33425
+ ),
33235
33426
  new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
33236
33427
  );
33237
- }
33238
- };
33428
+ };
33429
+ return { as };
33430
+ });
33431
+ this.dialect = is(dialect6, SQLiteDialect) ? dialect6 : void 0;
33432
+ this.dialectConfig = is(dialect6, SQLiteDialect) ? void 0 : dialect6;
33239
33433
  }
33240
33434
  with(...queries) {
33241
33435
  const self2 = this;
@@ -33916,6 +34110,56 @@ var init_db2 = __esm({
33916
34110
  BaseSQLiteDatabase = class {
33917
34111
  constructor(resultKind, dialect6, session, schema5) {
33918
34112
  __publicField(this, "query");
34113
+ /**
34114
+ * Creates a subquery that defines a temporary named result set as a CTE.
34115
+ *
34116
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
34117
+ *
34118
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
34119
+ *
34120
+ * @param alias The alias for the subquery.
34121
+ *
34122
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
34123
+ *
34124
+ * @example
34125
+ *
34126
+ * ```ts
34127
+ * // Create a subquery with alias 'sq' and use it in the select query
34128
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
34129
+ *
34130
+ * const result = await db.with(sq).select().from(sq);
34131
+ * ```
34132
+ *
34133
+ * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
34134
+ *
34135
+ * ```ts
34136
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
34137
+ * const sq = db.$with('sq').as(db.select({
34138
+ * name: sql<string>`upper(${users.name})`.as('name'),
34139
+ * })
34140
+ * .from(users));
34141
+ *
34142
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
34143
+ * ```
34144
+ */
34145
+ __publicField(this, "$with", (alias, selection) => {
34146
+ const self2 = this;
34147
+ const as = (qb) => {
34148
+ if (typeof qb === "function") {
34149
+ qb = qb(new QueryBuilder2(self2.dialect));
34150
+ }
34151
+ return new Proxy(
34152
+ new WithSubquery(
34153
+ qb.getSQL(),
34154
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
34155
+ alias,
34156
+ true
34157
+ ),
34158
+ new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
34159
+ );
34160
+ };
34161
+ return { as };
34162
+ });
33919
34163
  this.resultKind = resultKind;
33920
34164
  this.dialect = dialect6;
33921
34165
  this.session = session;
@@ -33945,52 +34189,6 @@ var init_db2 = __esm({
33945
34189
  }
33946
34190
  }
33947
34191
  }
33948
- /**
33949
- * Creates a subquery that defines a temporary named result set as a CTE.
33950
- *
33951
- * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
33952
- *
33953
- * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
33954
- *
33955
- * @param alias The alias for the subquery.
33956
- *
33957
- * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
33958
- *
33959
- * @example
33960
- *
33961
- * ```ts
33962
- * // Create a subquery with alias 'sq' and use it in the select query
33963
- * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
33964
- *
33965
- * const result = await db.with(sq).select().from(sq);
33966
- * ```
33967
- *
33968
- * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
33969
- *
33970
- * ```ts
33971
- * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
33972
- * const sq = db.$with('sq').as(db.select({
33973
- * name: sql<string>`upper(${users.name})`.as('name'),
33974
- * })
33975
- * .from(users));
33976
- *
33977
- * const result = await db.with(sq).select({ name: sq.name }).from(sq);
33978
- * ```
33979
- */
33980
- $with(alias) {
33981
- const self2 = this;
33982
- return {
33983
- as(qb) {
33984
- if (typeof qb === "function") {
33985
- qb = qb(new QueryBuilder2(self2.dialect));
33986
- }
33987
- return new Proxy(
33988
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
33989
- new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
33990
- );
33991
- }
33992
- };
33993
- }
33994
34192
  $count(source, filters) {
33995
34193
  return new SQLiteCountBuilder({ source, filters, session: this.session });
33996
34194
  }
@@ -38813,22 +39011,26 @@ var init_query_builder4 = __esm({
38813
39011
  constructor(dialect6) {
38814
39012
  __publicField(this, "dialect");
38815
39013
  __publicField(this, "dialectConfig");
38816
- this.dialect = is(dialect6, MySqlDialect) ? dialect6 : void 0;
38817
- this.dialectConfig = is(dialect6, MySqlDialect) ? void 0 : dialect6;
38818
- }
38819
- $with(alias) {
38820
- const queryBuilder = this;
38821
- return {
38822
- as(qb) {
39014
+ __publicField(this, "$with", (alias, selection) => {
39015
+ const queryBuilder = this;
39016
+ const as = (qb) => {
38823
39017
  if (typeof qb === "function") {
38824
39018
  qb = qb(queryBuilder);
38825
39019
  }
38826
39020
  return new Proxy(
38827
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
39021
+ new WithSubquery(
39022
+ qb.getSQL(),
39023
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
39024
+ alias,
39025
+ true
39026
+ ),
38828
39027
  new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
38829
39028
  );
38830
- }
38831
- };
39029
+ };
39030
+ return { as };
39031
+ });
39032
+ this.dialect = is(dialect6, MySqlDialect) ? dialect6 : void 0;
39033
+ this.dialectConfig = is(dialect6, MySqlDialect) ? void 0 : dialect6;
38832
39034
  }
38833
39035
  with(...queries) {
38834
39036
  const self2 = this;
@@ -39283,6 +39485,56 @@ var init_db3 = __esm({
39283
39485
  MySqlDatabase = class {
39284
39486
  constructor(dialect6, session, schema5, mode) {
39285
39487
  __publicField(this, "query");
39488
+ /**
39489
+ * Creates a subquery that defines a temporary named result set as a CTE.
39490
+ *
39491
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
39492
+ *
39493
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
39494
+ *
39495
+ * @param alias The alias for the subquery.
39496
+ *
39497
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
39498
+ *
39499
+ * @example
39500
+ *
39501
+ * ```ts
39502
+ * // Create a subquery with alias 'sq' and use it in the select query
39503
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
39504
+ *
39505
+ * const result = await db.with(sq).select().from(sq);
39506
+ * ```
39507
+ *
39508
+ * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
39509
+ *
39510
+ * ```ts
39511
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
39512
+ * const sq = db.$with('sq').as(db.select({
39513
+ * name: sql<string>`upper(${users.name})`.as('name'),
39514
+ * })
39515
+ * .from(users));
39516
+ *
39517
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
39518
+ * ```
39519
+ */
39520
+ __publicField(this, "$with", (alias, selection) => {
39521
+ const self2 = this;
39522
+ const as = (qb) => {
39523
+ if (typeof qb === "function") {
39524
+ qb = qb(new QueryBuilder3(self2.dialect));
39525
+ }
39526
+ return new Proxy(
39527
+ new WithSubquery(
39528
+ qb.getSQL(),
39529
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
39530
+ alias,
39531
+ true
39532
+ ),
39533
+ new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
39534
+ );
39535
+ };
39536
+ return { as };
39537
+ });
39286
39538
  this.dialect = dialect6;
39287
39539
  this.session = session;
39288
39540
  this.mode = mode;
@@ -39311,52 +39563,6 @@ var init_db3 = __esm({
39311
39563
  }
39312
39564
  }
39313
39565
  }
39314
- /**
39315
- * Creates a subquery that defines a temporary named result set as a CTE.
39316
- *
39317
- * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
39318
- *
39319
- * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
39320
- *
39321
- * @param alias The alias for the subquery.
39322
- *
39323
- * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
39324
- *
39325
- * @example
39326
- *
39327
- * ```ts
39328
- * // Create a subquery with alias 'sq' and use it in the select query
39329
- * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
39330
- *
39331
- * const result = await db.with(sq).select().from(sq);
39332
- * ```
39333
- *
39334
- * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
39335
- *
39336
- * ```ts
39337
- * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
39338
- * const sq = db.$with('sq').as(db.select({
39339
- * name: sql<string>`upper(${users.name})`.as('name'),
39340
- * })
39341
- * .from(users));
39342
- *
39343
- * const result = await db.with(sq).select({ name: sq.name }).from(sq);
39344
- * ```
39345
- */
39346
- $with(alias) {
39347
- const self2 = this;
39348
- return {
39349
- as(qb) {
39350
- if (typeof qb === "function") {
39351
- qb = qb(new QueryBuilder3(self2.dialect));
39352
- }
39353
- return new Proxy(
39354
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
39355
- new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
39356
- );
39357
- }
39358
- };
39359
- }
39360
39566
  $count(source, filters) {
39361
39567
  return new MySqlCountBuilder({ source, filters, session: this.session });
39362
39568
  }
@@ -43912,22 +44118,26 @@ var init_query_builder5 = __esm({
43912
44118
  constructor(dialect6) {
43913
44119
  __publicField(this, "dialect");
43914
44120
  __publicField(this, "dialectConfig");
43915
- this.dialect = is(dialect6, SingleStoreDialect) ? dialect6 : void 0;
43916
- this.dialectConfig = is(dialect6, SingleStoreDialect) ? void 0 : dialect6;
43917
- }
43918
- $with(alias) {
43919
- const queryBuilder = this;
43920
- return {
43921
- as(qb) {
44121
+ __publicField(this, "$with", (alias, selection) => {
44122
+ const queryBuilder = this;
44123
+ const as = (qb) => {
43922
44124
  if (typeof qb === "function") {
43923
44125
  qb = qb(queryBuilder);
43924
44126
  }
43925
44127
  return new Proxy(
43926
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
44128
+ new WithSubquery(
44129
+ qb.getSQL(),
44130
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
44131
+ alias,
44132
+ true
44133
+ ),
43927
44134
  new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
43928
44135
  );
43929
- }
43930
- };
44136
+ };
44137
+ return { as };
44138
+ });
44139
+ this.dialect = is(dialect6, SingleStoreDialect) ? dialect6 : void 0;
44140
+ this.dialectConfig = is(dialect6, SingleStoreDialect) ? void 0 : dialect6;
43931
44141
  }
43932
44142
  with(...queries) {
43933
44143
  const self2 = this;
@@ -44140,6 +44350,56 @@ var init_db4 = __esm({
44140
44350
  // We are waiting for SingleStore support for `json_array` function
44141
44351
  /**@inrernal */
44142
44352
  __publicField(this, "query");
44353
+ /**
44354
+ * Creates a subquery that defines a temporary named result set as a CTE.
44355
+ *
44356
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
44357
+ *
44358
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
44359
+ *
44360
+ * @param alias The alias for the subquery.
44361
+ *
44362
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
44363
+ *
44364
+ * @example
44365
+ *
44366
+ * ```ts
44367
+ * // Create a subquery with alias 'sq' and use it in the select query
44368
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
44369
+ *
44370
+ * const result = await db.with(sq).select().from(sq);
44371
+ * ```
44372
+ *
44373
+ * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
44374
+ *
44375
+ * ```ts
44376
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
44377
+ * const sq = db.$with('sq').as(db.select({
44378
+ * name: sql<string>`upper(${users.name})`.as('name'),
44379
+ * })
44380
+ * .from(users));
44381
+ *
44382
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
44383
+ * ```
44384
+ */
44385
+ __publicField(this, "$with", (alias, selection) => {
44386
+ const self2 = this;
44387
+ const as = (qb) => {
44388
+ if (typeof qb === "function") {
44389
+ qb = qb(new QueryBuilder4(self2.dialect));
44390
+ }
44391
+ return new Proxy(
44392
+ new WithSubquery(
44393
+ qb.getSQL(),
44394
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
44395
+ alias,
44396
+ true
44397
+ ),
44398
+ new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
44399
+ );
44400
+ };
44401
+ return { as };
44402
+ });
44143
44403
  this.dialect = dialect6;
44144
44404
  this.session = session;
44145
44405
  this._ = schema5 ? {
@@ -44153,52 +44413,6 @@ var init_db4 = __esm({
44153
44413
  };
44154
44414
  this.query = {};
44155
44415
  }
44156
- /**
44157
- * Creates a subquery that defines a temporary named result set as a CTE.
44158
- *
44159
- * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
44160
- *
44161
- * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
44162
- *
44163
- * @param alias The alias for the subquery.
44164
- *
44165
- * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
44166
- *
44167
- * @example
44168
- *
44169
- * ```ts
44170
- * // Create a subquery with alias 'sq' and use it in the select query
44171
- * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
44172
- *
44173
- * const result = await db.with(sq).select().from(sq);
44174
- * ```
44175
- *
44176
- * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
44177
- *
44178
- * ```ts
44179
- * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
44180
- * const sq = db.$with('sq').as(db.select({
44181
- * name: sql<string>`upper(${users.name})`.as('name'),
44182
- * })
44183
- * .from(users));
44184
- *
44185
- * const result = await db.with(sq).select({ name: sq.name }).from(sq);
44186
- * ```
44187
- */
44188
- $with(alias) {
44189
- const self2 = this;
44190
- return {
44191
- as(qb) {
44192
- if (typeof qb === "function") {
44193
- qb = qb(new QueryBuilder4(self2.dialect));
44194
- }
44195
- return new Proxy(
44196
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
44197
- new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
44198
- );
44199
- }
44200
- };
44201
- }
44202
44416
  $count(source, filters) {
44203
44417
  return new SingleStoreCountBuilder({ source, filters, session: this.session });
44204
44418
  }
@@ -45469,15 +45683,31 @@ var init_singlestoreImports = __esm({
45469
45683
  var singlestorePushUtils_exports = {};
45470
45684
  __export(singlestorePushUtils_exports, {
45471
45685
  filterStatements: () => filterStatements2,
45686
+ findColumnTypeAlternations: () => findColumnTypeAlternations,
45472
45687
  logSuggestionsAndReturn: () => logSuggestionsAndReturn3
45473
45688
  });
45689
+ function findColumnTypeAlternations(columns1, columns2) {
45690
+ const changes = [];
45691
+ for (const key in columns1) {
45692
+ if (columns1.hasOwnProperty(key) && columns2.hasOwnProperty(key)) {
45693
+ const col1 = columns1[key];
45694
+ const col2 = columns2[key];
45695
+ if (col1.type !== col2.type) {
45696
+ changes.push(col2.name);
45697
+ }
45698
+ }
45699
+ }
45700
+ return changes;
45701
+ }
45474
45702
  var import_hanji10, filterStatements2, logSuggestionsAndReturn3;
45475
45703
  var init_singlestorePushUtils = __esm({
45476
45704
  "src/cli/commands/singlestorePushUtils.ts"() {
45477
45705
  "use strict";
45478
45706
  init_source();
45479
45707
  import_hanji10 = __toESM(require_hanji());
45708
+ init_sqlgenerator();
45480
45709
  init_singlestoreSchema();
45710
+ init_utils();
45481
45711
  init_selector_ui();
45482
45712
  init_outputs();
45483
45713
  filterStatements2 = (statements, currentSchema, prevSchema) => {
@@ -45521,7 +45751,7 @@ var init_singlestorePushUtils = __esm({
45521
45751
  return true;
45522
45752
  });
45523
45753
  };
45524
- logSuggestionsAndReturn3 = async (db, statements, json22) => {
45754
+ logSuggestionsAndReturn3 = async (db, statements, json22, json1) => {
45525
45755
  let shouldAskForApprove = false;
45526
45756
  const statementsToExecute = [];
45527
45757
  const infoToPrint = [];
@@ -45702,6 +45932,71 @@ var init_singlestorePushUtils = __esm({
45702
45932
  shouldAskForApprove = true;
45703
45933
  }
45704
45934
  }
45935
+ } else if (statement.type === "singlestore_recreate_table") {
45936
+ const tableName = statement.tableName;
45937
+ const prevColumns = json1.tables[tableName].columns;
45938
+ const currentColumns = json22.tables[tableName].columns;
45939
+ const { removedColumns, addedColumns } = findAddedAndRemoved(
45940
+ Object.keys(prevColumns),
45941
+ Object.keys(currentColumns)
45942
+ );
45943
+ if (removedColumns.length) {
45944
+ for (const removedColumn of removedColumns) {
45945
+ const res = await db.query(
45946
+ `select count(\`${tableName}\`.\`${removedColumn}\`) as count from \`${tableName}\``
45947
+ );
45948
+ const count2 = Number(res[0].count);
45949
+ if (count2 > 0) {
45950
+ infoToPrint.push(
45951
+ `\xB7 You're about to delete ${source_default.underline(
45952
+ removedColumn
45953
+ )} column in ${tableName} table with ${count2} items`
45954
+ );
45955
+ columnsToRemove.push(removedColumn);
45956
+ shouldAskForApprove = true;
45957
+ }
45958
+ }
45959
+ }
45960
+ if (addedColumns.length) {
45961
+ for (const addedColumn of addedColumns) {
45962
+ const [res] = await db.query(
45963
+ `select count(*) as count from \`${tableName}\``
45964
+ );
45965
+ const columnConf = json22.tables[tableName].columns[addedColumn];
45966
+ const count2 = Number(res.count);
45967
+ if (count2 > 0 && columnConf.notNull && !columnConf.default) {
45968
+ infoToPrint.push(
45969
+ `\xB7 You're about to add not-null ${source_default.underline(
45970
+ addedColumn
45971
+ )} column without default value to table, which contains ${count2} items`
45972
+ );
45973
+ shouldAskForApprove = true;
45974
+ tablesToTruncate.push(tableName);
45975
+ statementsToExecute.push(`TRUNCATE TABLE \`${tableName}\`;`);
45976
+ }
45977
+ }
45978
+ }
45979
+ const columnWithChangedType = findColumnTypeAlternations(prevColumns, currentColumns);
45980
+ for (const column5 of columnWithChangedType) {
45981
+ const [res] = await db.query(
45982
+ `select count(*) as count from \`${tableName}\` WHERE \`${tableName}\`.\`${column5}\` IS NOT NULL;`
45983
+ );
45984
+ const count2 = Number(res.count);
45985
+ if (count2 > 0) {
45986
+ infoToPrint.push(
45987
+ `\xB7 You're about recreate ${source_default.underline(tableName)} table with data type changing for ${source_default.underline(
45988
+ column5
45989
+ )} column, which contains ${count2} items`
45990
+ );
45991
+ shouldAskForApprove = true;
45992
+ tablesToTruncate.push(tableName);
45993
+ statementsToExecute.push(`TRUNCATE TABLE \`${tableName}\`;`);
45994
+ }
45995
+ }
45996
+ }
45997
+ const stmnt = fromJson([statement], "singlestore", "push");
45998
+ if (typeof stmnt !== "undefined") {
45999
+ statementsToExecute.push(...stmnt);
45705
46000
  }
45706
46001
  }
45707
46002
  return {
@@ -46547,7 +46842,8 @@ var pushSingleStoreSchema = async (imports, drizzleInstance, databaseName) => {
46547
46842
  const { shouldAskForApprove, statementsToExecute, infoToPrint } = await logSuggestionsAndReturn4(
46548
46843
  db,
46549
46844
  statements,
46550
- validatedCur
46845
+ validatedCur,
46846
+ validatedPrev
46551
46847
  );
46552
46848
  return {
46553
46849
  hasDataLoss: shouldAskForApprove,