drizzle-kit 0.30.2-8e428d1 → 0.30.2-b776df4

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 +634 -338
  2. package/api.mjs +634 -338
  3. package/bin.cjs +29348 -44428
  4. package/package.json +1 -1
package/api.mjs CHANGED
@@ -12126,7 +12126,7 @@ function fromJson(statements, dialect6, action, json22) {
12126
12126
  }).filter((it) => it !== "");
12127
12127
  return result;
12128
12128
  }
12129
- 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;
12129
+ 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;
12130
12130
  var init_sqlgenerator = __esm({
12131
12131
  "src/sqlgenerator.ts"() {
12132
12132
  "use strict";
@@ -12472,7 +12472,7 @@ var init_sqlgenerator = __esm({
12472
12472
  if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
12473
12473
  statement += ",\n";
12474
12474
  const compositePK5 = SingleStoreSquasher.unsquashPK(compositePKs[0]);
12475
- statement += ` CONSTRAINT \`${st.compositePkName}\` PRIMARY KEY(\`${compositePK5.columns.join(`\`,\``)}\`)`;
12475
+ statement += ` CONSTRAINT \`${compositePK5.name}\` PRIMARY KEY(\`${compositePK5.columns.join(`\`,\``)}\`)`;
12476
12476
  }
12477
12477
  if (typeof uniqueConstraints !== "undefined" && uniqueConstraints.length > 0) {
12478
12478
  for (const uniqueConstraint5 of uniqueConstraints) {
@@ -13127,7 +13127,7 @@ WITH ${withCheckOption} CHECK OPTION` : "";
13127
13127
  }
13128
13128
  convert(statement) {
13129
13129
  const { tableNameFrom, tableNameTo } = statement;
13130
- return `RENAME TABLE \`${tableNameFrom}\` TO \`${tableNameTo}\`;`;
13130
+ return `ALTER TABLE \`${tableNameFrom}\` RENAME TO \`${tableNameTo}\`;`;
13131
13131
  }
13132
13132
  };
13133
13133
  PgAlterTableRenameColumnConvertor = class extends Convertor {
@@ -13155,7 +13155,7 @@ WITH ${withCheckOption} CHECK OPTION` : "";
13155
13155
  }
13156
13156
  convert(statement) {
13157
13157
  const { tableName, oldColumnName, newColumnName } = statement;
13158
- return `ALTER TABLE \`${tableName}\` RENAME COLUMN \`${oldColumnName}\` TO \`${newColumnName}\`;`;
13158
+ return `ALTER TABLE \`${tableName}\` CHANGE \`${oldColumnName}\` \`${newColumnName}\`;`;
13159
13159
  }
13160
13160
  };
13161
13161
  SQLiteAlterTableRenameColumnConvertor = class extends Convertor {
@@ -14465,10 +14465,52 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.newC
14465
14465
  return sqlStatements;
14466
14466
  }
14467
14467
  };
14468
+ SingleStoreRecreateTableConvertor = class extends Convertor {
14469
+ can(statement, dialect6) {
14470
+ return statement.type === "singlestore_recreate_table" && dialect6 === "singlestore";
14471
+ }
14472
+ convert(statement) {
14473
+ const { tableName, columns, compositePKs, uniqueConstraints } = statement;
14474
+ const columnNames = columns.map((it) => `\`${it.name}\``).join(", ");
14475
+ const newTableName = `__new_${tableName}`;
14476
+ const sqlStatements = [];
14477
+ sqlStatements.push(
14478
+ new SingleStoreCreateTableConvertor().convert({
14479
+ type: "create_table",
14480
+ tableName: newTableName,
14481
+ columns,
14482
+ compositePKs,
14483
+ uniqueConstraints,
14484
+ schema: ""
14485
+ })
14486
+ );
14487
+ sqlStatements.push(
14488
+ `INSERT INTO \`${newTableName}\`(${columnNames}) SELECT ${columnNames} FROM \`${tableName}\`;`
14489
+ );
14490
+ sqlStatements.push(
14491
+ new SingleStoreDropTableConvertor().convert({
14492
+ type: "drop_table",
14493
+ tableName,
14494
+ schema: ""
14495
+ })
14496
+ );
14497
+ sqlStatements.push(
14498
+ new SingleStoreRenameTableConvertor().convert({
14499
+ fromSchema: "",
14500
+ tableNameFrom: newTableName,
14501
+ tableNameTo: tableName,
14502
+ toSchema: "",
14503
+ type: "rename_table"
14504
+ })
14505
+ );
14506
+ return sqlStatements;
14507
+ }
14508
+ };
14468
14509
  convertors = [];
14469
14510
  convertors.push(new PgCreateTableConvertor());
14470
14511
  convertors.push(new MySqlCreateTableConvertor());
14471
14512
  convertors.push(new SingleStoreCreateTableConvertor());
14513
+ convertors.push(new SingleStoreRecreateTableConvertor());
14472
14514
  convertors.push(new SQLiteCreateTableConvertor());
14473
14515
  convertors.push(new SQLiteRecreateTableConvertor());
14474
14516
  convertors.push(new LibSQLRecreateTableConvertor());
@@ -16464,7 +16506,7 @@ var init_jsonStatements = __esm({
16464
16506
  });
16465
16507
 
16466
16508
  // src/statementCombiner.ts
16467
- var prepareLibSQLRecreateTable, prepareSQLiteRecreateTable, libSQLCombineStatements, sqliteCombineStatements;
16509
+ var prepareLibSQLRecreateTable, prepareSQLiteRecreateTable, libSQLCombineStatements, sqliteCombineStatements, prepareSingleStoreRecreateTable, singleStoreCombineStatements;
16468
16510
  var init_statementCombiner = __esm({
16469
16511
  "src/statementCombiner.ts"() {
16470
16512
  "use strict";
@@ -16743,6 +16785,101 @@ var init_statementCombiner = __esm({
16743
16785
  const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
16744
16786
  return [...renamedTables, ...renamedColumns, ...rest];
16745
16787
  };
16788
+ prepareSingleStoreRecreateTable = (table5) => {
16789
+ const { name: name2, columns, uniqueConstraints, indexes, compositePrimaryKeys } = table5;
16790
+ const composites = Object.values(compositePrimaryKeys);
16791
+ const statements = [
16792
+ {
16793
+ type: "singlestore_recreate_table",
16794
+ tableName: name2,
16795
+ columns: Object.values(columns),
16796
+ compositePKs: composites,
16797
+ uniqueConstraints: Object.values(uniqueConstraints)
16798
+ }
16799
+ ];
16800
+ if (Object.keys(indexes).length) {
16801
+ statements.push(...prepareCreateIndexesJson(name2, "", indexes));
16802
+ }
16803
+ return statements;
16804
+ };
16805
+ singleStoreCombineStatements = (statements, json22) => {
16806
+ const newStatements = {};
16807
+ for (const statement of statements) {
16808
+ 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") {
16809
+ const tableName2 = statement.tableName;
16810
+ const statementsForTable2 = newStatements[tableName2];
16811
+ if (!statementsForTable2) {
16812
+ newStatements[tableName2] = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16813
+ continue;
16814
+ }
16815
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
16816
+ const wasRename = statementsForTable2.some(
16817
+ ({ type }) => type === "rename_table" || type === "alter_table_rename_column"
16818
+ );
16819
+ const preparedStatements = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16820
+ if (wasRename) {
16821
+ newStatements[tableName2].push(...preparedStatements);
16822
+ } else {
16823
+ newStatements[tableName2] = preparedStatements;
16824
+ }
16825
+ continue;
16826
+ }
16827
+ continue;
16828
+ }
16829
+ if ((statement.type === "alter_table_alter_column_drop_default" || statement.type === "alter_table_alter_column_set_default") && statement.columnNotNull) {
16830
+ const tableName2 = statement.tableName;
16831
+ const statementsForTable2 = newStatements[tableName2];
16832
+ if (!statementsForTable2) {
16833
+ newStatements[tableName2] = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16834
+ continue;
16835
+ }
16836
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
16837
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
16838
+ const preparedStatements = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16839
+ if (wasRename) {
16840
+ newStatements[tableName2].push(...preparedStatements);
16841
+ } else {
16842
+ newStatements[tableName2] = preparedStatements;
16843
+ }
16844
+ continue;
16845
+ }
16846
+ continue;
16847
+ }
16848
+ if (statement.type === "alter_table_add_column" && statement.column.primaryKey) {
16849
+ const tableName2 = statement.tableName;
16850
+ const statementsForTable2 = newStatements[tableName2];
16851
+ if (!statementsForTable2) {
16852
+ newStatements[tableName2] = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16853
+ continue;
16854
+ }
16855
+ if (!statementsForTable2.some(({ type }) => type === "recreate_table")) {
16856
+ const wasRename = statementsForTable2.some(({ type }) => type === "rename_table");
16857
+ const preparedStatements = prepareSingleStoreRecreateTable(json22.tables[tableName2]);
16858
+ if (wasRename) {
16859
+ newStatements[tableName2].push(...preparedStatements);
16860
+ } else {
16861
+ newStatements[tableName2] = preparedStatements;
16862
+ }
16863
+ continue;
16864
+ }
16865
+ continue;
16866
+ }
16867
+ const tableName = statement.type === "rename_table" ? statement.tableNameTo : statement.tableName;
16868
+ const statementsForTable = newStatements[tableName];
16869
+ if (!statementsForTable) {
16870
+ newStatements[tableName] = [statement];
16871
+ continue;
16872
+ }
16873
+ if (!statementsForTable.some(({ type }) => type === "singlestore_recreate_table")) {
16874
+ newStatements[tableName].push(statement);
16875
+ }
16876
+ }
16877
+ const combinedStatements = Object.values(newStatements).flat();
16878
+ const renamedTables = combinedStatements.filter((it) => it.type === "rename_table");
16879
+ const renamedColumns = combinedStatements.filter((it) => it.type === "alter_table_rename_column");
16880
+ const rest = combinedStatements.filter((it) => it.type !== "rename_table" && it.type !== "alter_table_rename_column");
16881
+ return [...renamedTables, ...renamedColumns, ...rest];
16882
+ };
16746
16883
  }
16747
16884
  });
16748
16885
 
@@ -18688,7 +18825,7 @@ var init_snapshotsDiffer = __esm({
18688
18825
  return [tableKey2, tableValue];
18689
18826
  }
18690
18827
  );
18691
- const diffResult = applyJsonDiff(tablesPatchedSnap1, json22);
18828
+ const diffResult = applyJsonDiff(columnsPatchedSnap1, json22);
18692
18829
  const typedResult = diffResultSchemeSingleStore.parse(diffResult);
18693
18830
  const jsonStatements = [];
18694
18831
  const jsonCreateIndexesForCreatedTables = createdTables.map((it) => {
@@ -18852,7 +18989,8 @@ var init_snapshotsDiffer = __esm({
18852
18989
  jsonStatements.push(...jsonDropColumnsStatemets);
18853
18990
  jsonStatements.push(...jsonAddedCompositePKs);
18854
18991
  jsonStatements.push(...jsonAlteredUniqueConstraints);
18855
- const sqlStatements = fromJson(jsonStatements, "singlestore");
18992
+ const combinedJsonStatements = singleStoreCombineStatements(jsonStatements, json22);
18993
+ const sqlStatements = fromJson(combinedJsonStatements, "singlestore");
18856
18994
  const uniqueSqlStatements = [];
18857
18995
  sqlStatements.forEach((ss) => {
18858
18996
  if (!uniqueSqlStatements.includes(ss)) {
@@ -18864,7 +19002,7 @@ var init_snapshotsDiffer = __esm({
18864
19002
  });
18865
19003
  const _meta = prepareMigrationMeta([], rTables, rColumns);
18866
19004
  return {
18867
- statements: jsonStatements,
19005
+ statements: combinedJsonStatements,
18868
19006
  sqlStatements: uniqueSqlStatements,
18869
19007
  _meta
18870
19008
  };
@@ -21982,7 +22120,7 @@ var version;
21982
22120
  var init_version = __esm({
21983
22121
  "../drizzle-orm/dist/version.js"() {
21984
22122
  "use strict";
21985
- version = "0.38.3";
22123
+ version = "0.38.4";
21986
22124
  }
21987
22125
  });
21988
22126
 
@@ -22170,6 +22308,9 @@ function fillPlaceholders(params, values) {
22170
22308
  function isView(view4) {
22171
22309
  return typeof view4 === "object" && view4 !== null && IsDrizzleView in view4;
22172
22310
  }
22311
+ function getViewName(view4) {
22312
+ return view4[ViewBaseConfig].name;
22313
+ }
22173
22314
  var _a19, FakePrimitiveParam, _a20, StringChunk, _a21, _SQL, SQL, _a22, Name, noopDecoder, noopEncoder, noopMapper, _a23, Param, _a24, Placeholder, IsDrizzleView, _a25, _b10, _c2, View3;
22174
22315
  var init_sql = __esm({
22175
22316
  "../drizzle-orm/dist/sql/sql.js"() {
@@ -25296,6 +25437,7 @@ __export(dist_exports, {
25296
25437
  getTableLikeName: () => getTableLikeName,
25297
25438
  getTableName: () => getTableName,
25298
25439
  getTableUniqueName: () => getTableUniqueName,
25440
+ getViewName: () => getViewName,
25299
25441
  getViewSelectedFields: () => getViewSelectedFields,
25300
25442
  gt: () => gt,
25301
25443
  gte: () => gte,
@@ -25450,17 +25592,100 @@ var init_columns = __esm({
25450
25592
  }
25451
25593
  });
25452
25594
 
25595
+ // ../drizzle-orm/dist/selection-proxy.js
25596
+ var _a124, _SelectionProxyHandler, SelectionProxyHandler;
25597
+ var init_selection_proxy = __esm({
25598
+ "../drizzle-orm/dist/selection-proxy.js"() {
25599
+ "use strict";
25600
+ init_alias();
25601
+ init_column();
25602
+ init_entity();
25603
+ init_sql();
25604
+ init_subquery();
25605
+ init_view_common();
25606
+ _a124 = entityKind;
25607
+ _SelectionProxyHandler = class _SelectionProxyHandler {
25608
+ constructor(config) {
25609
+ __publicField(this, "config");
25610
+ this.config = { ...config };
25611
+ }
25612
+ get(subquery, prop) {
25613
+ if (prop === "_") {
25614
+ return {
25615
+ ...subquery["_"],
25616
+ selectedFields: new Proxy(
25617
+ subquery._.selectedFields,
25618
+ this
25619
+ )
25620
+ };
25621
+ }
25622
+ if (prop === ViewBaseConfig) {
25623
+ return {
25624
+ ...subquery[ViewBaseConfig],
25625
+ selectedFields: new Proxy(
25626
+ subquery[ViewBaseConfig].selectedFields,
25627
+ this
25628
+ )
25629
+ };
25630
+ }
25631
+ if (typeof prop === "symbol") {
25632
+ return subquery[prop];
25633
+ }
25634
+ const columns = is(subquery, Subquery) ? subquery._.selectedFields : is(subquery, View3) ? subquery[ViewBaseConfig].selectedFields : subquery;
25635
+ const value = columns[prop];
25636
+ if (is(value, SQL.Aliased)) {
25637
+ if (this.config.sqlAliasedBehavior === "sql" && !value.isSelectionField) {
25638
+ return value.sql;
25639
+ }
25640
+ const newValue = value.clone();
25641
+ newValue.isSelectionField = true;
25642
+ return newValue;
25643
+ }
25644
+ if (is(value, SQL)) {
25645
+ if (this.config.sqlBehavior === "sql") {
25646
+ return value;
25647
+ }
25648
+ throw new Error(
25649
+ `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.`
25650
+ );
25651
+ }
25652
+ if (is(value, Column2)) {
25653
+ if (this.config.alias) {
25654
+ return new Proxy(
25655
+ value,
25656
+ new ColumnAliasProxyHandler(
25657
+ new Proxy(
25658
+ value.table,
25659
+ new TableAliasProxyHandler(this.config.alias, this.config.replaceOriginalName ?? false)
25660
+ )
25661
+ )
25662
+ );
25663
+ }
25664
+ return value;
25665
+ }
25666
+ if (typeof value !== "object" || value === null) {
25667
+ return value;
25668
+ }
25669
+ return new Proxy(value, new _SelectionProxyHandler(this.config));
25670
+ }
25671
+ };
25672
+ __publicField(_SelectionProxyHandler, _a124, "SelectionProxyHandler");
25673
+ SelectionProxyHandler = _SelectionProxyHandler;
25674
+ }
25675
+ });
25676
+
25453
25677
  // ../drizzle-orm/dist/pg-core/query-builders/delete.js
25454
- var _a124, _b97, PgDeleteBase;
25678
+ var _a125, _b97, PgDeleteBase;
25455
25679
  var init_delete = __esm({
25456
25680
  "../drizzle-orm/dist/pg-core/query-builders/delete.js"() {
25457
25681
  "use strict";
25458
25682
  init_entity();
25459
25683
  init_query_promise();
25684
+ init_selection_proxy();
25460
25685
  init_table();
25461
25686
  init_tracing();
25462
25687
  init_utils2();
25463
- PgDeleteBase = class extends (_b97 = QueryPromise, _a124 = entityKind, _b97) {
25688
+ PgDeleteBase = class extends (_b97 = QueryPromise, _a125 = entityKind, _b97) {
25464
25689
  constructor(table5, session, dialect6, withList) {
25465
25690
  super();
25466
25691
  __publicField(this, "config");
@@ -25508,6 +25733,7 @@ var init_delete = __esm({
25508
25733
  return this;
25509
25734
  }
25510
25735
  returning(fields = this.config.table[Table2.Symbol.Columns]) {
25736
+ this.config.returningFields = fields;
25511
25737
  this.config.returning = orderSelectedFields(fields);
25512
25738
  return this;
25513
25739
  }
@@ -25533,11 +25759,22 @@ var init_delete = __esm({
25533
25759
  this.authToken = token;
25534
25760
  return this;
25535
25761
  }
25762
+ /** @internal */
25763
+ getSelectedFields() {
25764
+ return this.config.returningFields ? new Proxy(
25765
+ this.config.returningFields,
25766
+ new SelectionProxyHandler({
25767
+ alias: getTableName(this.config.table),
25768
+ sqlAliasedBehavior: "alias",
25769
+ sqlBehavior: "error"
25770
+ })
25771
+ ) : void 0;
25772
+ }
25536
25773
  $dynamic() {
25537
25774
  return this;
25538
25775
  }
25539
25776
  };
25540
- __publicField(PgDeleteBase, _a124, "PgDelete");
25777
+ __publicField(PgDeleteBase, _a125, "PgDelete");
25541
25778
  }
25542
25779
  });
25543
25780
 
@@ -25556,13 +25793,13 @@ function toCamelCase(input) {
25556
25793
  function noopCase(input) {
25557
25794
  return input;
25558
25795
  }
25559
- var _a125, CasingCache;
25796
+ var _a126, CasingCache;
25560
25797
  var init_casing = __esm({
25561
25798
  "../drizzle-orm/dist/casing.js"() {
25562
25799
  "use strict";
25563
25800
  init_entity();
25564
25801
  init_table();
25565
- _a125 = entityKind;
25802
+ _a126 = entityKind;
25566
25803
  CasingCache = class {
25567
25804
  constructor(casing2) {
25568
25805
  /** @internal */
@@ -25599,25 +25836,25 @@ var init_casing = __esm({
25599
25836
  this.cachedTables = {};
25600
25837
  }
25601
25838
  };
25602
- __publicField(CasingCache, _a125, "CasingCache");
25839
+ __publicField(CasingCache, _a126, "CasingCache");
25603
25840
  }
25604
25841
  });
25605
25842
 
25606
25843
  // ../drizzle-orm/dist/pg-core/view-base.js
25607
- var _a126, _b98, PgViewBase;
25844
+ var _a127, _b98, PgViewBase;
25608
25845
  var init_view_base = __esm({
25609
25846
  "../drizzle-orm/dist/pg-core/view-base.js"() {
25610
25847
  "use strict";
25611
25848
  init_entity();
25612
25849
  init_sql();
25613
- PgViewBase = class extends (_b98 = View3, _a126 = entityKind, _b98) {
25850
+ PgViewBase = class extends (_b98 = View3, _a127 = entityKind, _b98) {
25614
25851
  };
25615
- __publicField(PgViewBase, _a126, "PgViewBase");
25852
+ __publicField(PgViewBase, _a127, "PgViewBase");
25616
25853
  }
25617
25854
  });
25618
25855
 
25619
25856
  // ../drizzle-orm/dist/pg-core/dialect.js
25620
- var _a127, PgDialect;
25857
+ var _a128, PgDialect;
25621
25858
  var init_dialect = __esm({
25622
25859
  "../drizzle-orm/dist/pg-core/dialect.js"() {
25623
25860
  "use strict";
@@ -25636,7 +25873,7 @@ var init_dialect = __esm({
25636
25873
  init_utils2();
25637
25874
  init_view_common();
25638
25875
  init_view_base();
25639
- _a127 = entityKind;
25876
+ _a128 = entityKind;
25640
25877
  PgDialect = class {
25641
25878
  constructor(config) {
25642
25879
  /** @internal */
@@ -26728,89 +26965,7 @@ var init_dialect = __esm({
26728
26965
  };
26729
26966
  }
26730
26967
  };
26731
- __publicField(PgDialect, _a127, "PgDialect");
26732
- }
26733
- });
26734
-
26735
- // ../drizzle-orm/dist/selection-proxy.js
26736
- var _a128, _SelectionProxyHandler, SelectionProxyHandler;
26737
- var init_selection_proxy = __esm({
26738
- "../drizzle-orm/dist/selection-proxy.js"() {
26739
- "use strict";
26740
- init_alias();
26741
- init_column();
26742
- init_entity();
26743
- init_sql();
26744
- init_subquery();
26745
- init_view_common();
26746
- _a128 = entityKind;
26747
- _SelectionProxyHandler = class _SelectionProxyHandler {
26748
- constructor(config) {
26749
- __publicField(this, "config");
26750
- this.config = { ...config };
26751
- }
26752
- get(subquery, prop) {
26753
- if (prop === "_") {
26754
- return {
26755
- ...subquery["_"],
26756
- selectedFields: new Proxy(
26757
- subquery._.selectedFields,
26758
- this
26759
- )
26760
- };
26761
- }
26762
- if (prop === ViewBaseConfig) {
26763
- return {
26764
- ...subquery[ViewBaseConfig],
26765
- selectedFields: new Proxy(
26766
- subquery[ViewBaseConfig].selectedFields,
26767
- this
26768
- )
26769
- };
26770
- }
26771
- if (typeof prop === "symbol") {
26772
- return subquery[prop];
26773
- }
26774
- const columns = is(subquery, Subquery) ? subquery._.selectedFields : is(subquery, View3) ? subquery[ViewBaseConfig].selectedFields : subquery;
26775
- const value = columns[prop];
26776
- if (is(value, SQL.Aliased)) {
26777
- if (this.config.sqlAliasedBehavior === "sql" && !value.isSelectionField) {
26778
- return value.sql;
26779
- }
26780
- const newValue = value.clone();
26781
- newValue.isSelectionField = true;
26782
- return newValue;
26783
- }
26784
- if (is(value, SQL)) {
26785
- if (this.config.sqlBehavior === "sql") {
26786
- return value;
26787
- }
26788
- throw new Error(
26789
- `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.`
26790
- );
26791
- }
26792
- if (is(value, Column2)) {
26793
- if (this.config.alias) {
26794
- return new Proxy(
26795
- value,
26796
- new ColumnAliasProxyHandler(
26797
- new Proxy(
26798
- value.table,
26799
- new TableAliasProxyHandler(this.config.alias, this.config.replaceOriginalName ?? false)
26800
- )
26801
- )
26802
- );
26803
- }
26804
- return value;
26805
- }
26806
- if (typeof value !== "object" || value === null) {
26807
- return value;
26808
- }
26809
- return new Proxy(value, new _SelectionProxyHandler(this.config));
26810
- }
26811
- };
26812
- __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26813
- SelectionProxyHandler = _SelectionProxyHandler;
26968
+ __publicField(PgDialect, _a128, "PgDialect");
26814
26969
  }
26815
26970
  });
26816
26971
 
@@ -26895,22 +27050,23 @@ var init_select2 = __esm({
26895
27050
  */
26896
27051
  from(source) {
26897
27052
  const isPartialSelect = !!this.fields;
27053
+ const src = source;
26898
27054
  let fields;
26899
27055
  if (this.fields) {
26900
27056
  fields = this.fields;
26901
- } else if (is(source, Subquery)) {
27057
+ } else if (is(src, Subquery)) {
26902
27058
  fields = Object.fromEntries(
26903
- Object.keys(source._.selectedFields).map((key) => [key, source[key]])
27059
+ Object.keys(src._.selectedFields).map((key) => [key, src[key]])
26904
27060
  );
26905
- } else if (is(source, PgViewBase)) {
26906
- fields = source[ViewBaseConfig].selectedFields;
26907
- } else if (is(source, SQL)) {
27061
+ } else if (is(src, PgViewBase)) {
27062
+ fields = src[ViewBaseConfig].selectedFields;
27063
+ } else if (is(src, SQL)) {
26908
27064
  fields = {};
26909
27065
  } else {
26910
- fields = getTableColumns(source);
27066
+ fields = getTableColumns(src);
26911
27067
  }
26912
27068
  return new PgSelectBase({
26913
- table: source,
27069
+ table: src,
26914
27070
  fields,
26915
27071
  isPartialSelect,
26916
27072
  session: this.session,
@@ -27595,22 +27751,26 @@ var init_query_builder2 = __esm({
27595
27751
  constructor(dialect6) {
27596
27752
  __publicField(this, "dialect");
27597
27753
  __publicField(this, "dialectConfig");
27598
- this.dialect = is(dialect6, PgDialect) ? dialect6 : void 0;
27599
- this.dialectConfig = is(dialect6, PgDialect) ? void 0 : dialect6;
27600
- }
27601
- $with(alias) {
27602
- const queryBuilder = this;
27603
- return {
27604
- as(qb) {
27754
+ __publicField(this, "$with", (alias, selection) => {
27755
+ const queryBuilder = this;
27756
+ const as = (qb) => {
27605
27757
  if (typeof qb === "function") {
27606
27758
  qb = qb(queryBuilder);
27607
27759
  }
27608
27760
  return new Proxy(
27609
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
27761
+ new WithSubquery(
27762
+ qb.getSQL(),
27763
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
27764
+ alias,
27765
+ true
27766
+ ),
27610
27767
  new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
27611
27768
  );
27612
- }
27613
- };
27769
+ };
27770
+ return { as };
27771
+ });
27772
+ this.dialect = is(dialect6, PgDialect) ? dialect6 : void 0;
27773
+ this.dialectConfig = is(dialect6, PgDialect) ? void 0 : dialect6;
27614
27774
  }
27615
27775
  with(...queries) {
27616
27776
  const self2 = this;
@@ -27682,6 +27842,7 @@ var init_insert = __esm({
27682
27842
  "use strict";
27683
27843
  init_entity();
27684
27844
  init_query_promise();
27845
+ init_selection_proxy();
27685
27846
  init_sql();
27686
27847
  init_table();
27687
27848
  init_tracing();
@@ -27756,6 +27917,7 @@ var init_insert = __esm({
27756
27917
  this.config = { table: table5, values, withList, select, overridingSystemValue_ };
27757
27918
  }
27758
27919
  returning(fields = this.config.table[Table2.Symbol.Columns]) {
27920
+ this.config.returningFields = fields;
27759
27921
  this.config.returning = orderSelectedFields(fields);
27760
27922
  return this;
27761
27923
  }
@@ -27858,6 +28020,17 @@ var init_insert = __esm({
27858
28020
  this.authToken = token;
27859
28021
  return this;
27860
28022
  }
28023
+ /** @internal */
28024
+ getSelectedFields() {
28025
+ return this.config.returningFields ? new Proxy(
28026
+ this.config.returningFields,
28027
+ new SelectionProxyHandler({
28028
+ alias: getTableName(this.config.table),
28029
+ sqlAliasedBehavior: "alias",
28030
+ sqlBehavior: "error"
28031
+ })
28032
+ ) : void 0;
28033
+ }
27861
28034
  $dynamic() {
27862
28035
  return this;
27863
28036
  }
@@ -27995,11 +28168,12 @@ var init_update = __esm({
27995
28168
  this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27996
28169
  }
27997
28170
  from(source) {
27998
- const tableName = getTableLikeName(source);
28171
+ const src = source;
28172
+ const tableName = getTableLikeName(src);
27999
28173
  if (typeof tableName === "string") {
28000
28174
  this.joinsNotNullableMap[tableName] = true;
28001
28175
  }
28002
- this.config.from = source;
28176
+ this.config.from = src;
28003
28177
  return this;
28004
28178
  }
28005
28179
  getTableLikeFields(table5) {
@@ -28114,6 +28288,7 @@ var init_update = __esm({
28114
28288
  }
28115
28289
  }
28116
28290
  }
28291
+ this.config.returningFields = fields;
28117
28292
  this.config.returning = orderSelectedFields(fields);
28118
28293
  return this;
28119
28294
  }
@@ -28139,6 +28314,17 @@ var init_update = __esm({
28139
28314
  this.authToken = token;
28140
28315
  return this;
28141
28316
  }
28317
+ /** @internal */
28318
+ getSelectedFields() {
28319
+ return this.config.returningFields ? new Proxy(
28320
+ this.config.returningFields,
28321
+ new SelectionProxyHandler({
28322
+ alias: getTableName(this.config.table),
28323
+ sqlAliasedBehavior: "alias",
28324
+ sqlBehavior: "error"
28325
+ })
28326
+ ) : void 0;
28327
+ }
28142
28328
  $dynamic() {
28143
28329
  return this;
28144
28330
  }
@@ -28400,6 +28586,56 @@ var init_db = __esm({
28400
28586
  PgDatabase = class {
28401
28587
  constructor(dialect6, session, schema5) {
28402
28588
  __publicField(this, "query");
28589
+ /**
28590
+ * Creates a subquery that defines a temporary named result set as a CTE.
28591
+ *
28592
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
28593
+ *
28594
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
28595
+ *
28596
+ * @param alias The alias for the subquery.
28597
+ *
28598
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
28599
+ *
28600
+ * @example
28601
+ *
28602
+ * ```ts
28603
+ * // Create a subquery with alias 'sq' and use it in the select query
28604
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
28605
+ *
28606
+ * const result = await db.with(sq).select().from(sq);
28607
+ * ```
28608
+ *
28609
+ * 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:
28610
+ *
28611
+ * ```ts
28612
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
28613
+ * const sq = db.$with('sq').as(db.select({
28614
+ * name: sql<string>`upper(${users.name})`.as('name'),
28615
+ * })
28616
+ * .from(users));
28617
+ *
28618
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
28619
+ * ```
28620
+ */
28621
+ __publicField(this, "$with", (alias, selection) => {
28622
+ const self2 = this;
28623
+ const as = (qb) => {
28624
+ if (typeof qb === "function") {
28625
+ qb = qb(new QueryBuilder(self2.dialect));
28626
+ }
28627
+ return new Proxy(
28628
+ new WithSubquery(
28629
+ qb.getSQL(),
28630
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
28631
+ alias,
28632
+ true
28633
+ ),
28634
+ new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
28635
+ );
28636
+ };
28637
+ return { as };
28638
+ });
28403
28639
  __publicField(this, "authToken");
28404
28640
  this.dialect = dialect6;
28405
28641
  this.session = session;
@@ -28429,52 +28665,6 @@ var init_db = __esm({
28429
28665
  }
28430
28666
  }
28431
28667
  }
28432
- /**
28433
- * Creates a subquery that defines a temporary named result set as a CTE.
28434
- *
28435
- * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
28436
- *
28437
- * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
28438
- *
28439
- * @param alias The alias for the subquery.
28440
- *
28441
- * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
28442
- *
28443
- * @example
28444
- *
28445
- * ```ts
28446
- * // Create a subquery with alias 'sq' and use it in the select query
28447
- * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
28448
- *
28449
- * const result = await db.with(sq).select().from(sq);
28450
- * ```
28451
- *
28452
- * 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:
28453
- *
28454
- * ```ts
28455
- * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
28456
- * const sq = db.$with('sq').as(db.select({
28457
- * name: sql<string>`upper(${users.name})`.as('name'),
28458
- * })
28459
- * .from(users));
28460
- *
28461
- * const result = await db.with(sq).select({ name: sq.name }).from(sq);
28462
- * ```
28463
- */
28464
- $with(alias) {
28465
- const self2 = this;
28466
- return {
28467
- as(qb) {
28468
- if (typeof qb === "function") {
28469
- qb = qb(new QueryBuilder(self2.dialect));
28470
- }
28471
- return new Proxy(
28472
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
28473
- new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
28474
- );
28475
- }
28476
- };
28477
- }
28478
28668
  $count(source, filters) {
28479
28669
  return new PgCountBuilder({ source, filters, session: this.session });
28480
28670
  }
@@ -33225,22 +33415,26 @@ var init_query_builder3 = __esm({
33225
33415
  constructor(dialect6) {
33226
33416
  __publicField(this, "dialect");
33227
33417
  __publicField(this, "dialectConfig");
33228
- this.dialect = is(dialect6, SQLiteDialect) ? dialect6 : void 0;
33229
- this.dialectConfig = is(dialect6, SQLiteDialect) ? void 0 : dialect6;
33230
- }
33231
- $with(alias) {
33232
- const queryBuilder = this;
33233
- return {
33234
- as(qb) {
33418
+ __publicField(this, "$with", (alias, selection) => {
33419
+ const queryBuilder = this;
33420
+ const as = (qb) => {
33235
33421
  if (typeof qb === "function") {
33236
33422
  qb = qb(queryBuilder);
33237
33423
  }
33238
33424
  return new Proxy(
33239
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
33425
+ new WithSubquery(
33426
+ qb.getSQL(),
33427
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
33428
+ alias,
33429
+ true
33430
+ ),
33240
33431
  new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
33241
33432
  );
33242
- }
33243
- };
33433
+ };
33434
+ return { as };
33435
+ });
33436
+ this.dialect = is(dialect6, SQLiteDialect) ? dialect6 : void 0;
33437
+ this.dialectConfig = is(dialect6, SQLiteDialect) ? void 0 : dialect6;
33244
33438
  }
33245
33439
  with(...queries) {
33246
33440
  const self2 = this;
@@ -33921,6 +34115,56 @@ var init_db2 = __esm({
33921
34115
  BaseSQLiteDatabase = class {
33922
34116
  constructor(resultKind, dialect6, session, schema5) {
33923
34117
  __publicField(this, "query");
34118
+ /**
34119
+ * Creates a subquery that defines a temporary named result set as a CTE.
34120
+ *
34121
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
34122
+ *
34123
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
34124
+ *
34125
+ * @param alias The alias for the subquery.
34126
+ *
34127
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
34128
+ *
34129
+ * @example
34130
+ *
34131
+ * ```ts
34132
+ * // Create a subquery with alias 'sq' and use it in the select query
34133
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
34134
+ *
34135
+ * const result = await db.with(sq).select().from(sq);
34136
+ * ```
34137
+ *
34138
+ * 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:
34139
+ *
34140
+ * ```ts
34141
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
34142
+ * const sq = db.$with('sq').as(db.select({
34143
+ * name: sql<string>`upper(${users.name})`.as('name'),
34144
+ * })
34145
+ * .from(users));
34146
+ *
34147
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
34148
+ * ```
34149
+ */
34150
+ __publicField(this, "$with", (alias, selection) => {
34151
+ const self2 = this;
34152
+ const as = (qb) => {
34153
+ if (typeof qb === "function") {
34154
+ qb = qb(new QueryBuilder2(self2.dialect));
34155
+ }
34156
+ return new Proxy(
34157
+ new WithSubquery(
34158
+ qb.getSQL(),
34159
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
34160
+ alias,
34161
+ true
34162
+ ),
34163
+ new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
34164
+ );
34165
+ };
34166
+ return { as };
34167
+ });
33924
34168
  this.resultKind = resultKind;
33925
34169
  this.dialect = dialect6;
33926
34170
  this.session = session;
@@ -33950,52 +34194,6 @@ var init_db2 = __esm({
33950
34194
  }
33951
34195
  }
33952
34196
  }
33953
- /**
33954
- * Creates a subquery that defines a temporary named result set as a CTE.
33955
- *
33956
- * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
33957
- *
33958
- * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
33959
- *
33960
- * @param alias The alias for the subquery.
33961
- *
33962
- * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
33963
- *
33964
- * @example
33965
- *
33966
- * ```ts
33967
- * // Create a subquery with alias 'sq' and use it in the select query
33968
- * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
33969
- *
33970
- * const result = await db.with(sq).select().from(sq);
33971
- * ```
33972
- *
33973
- * 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:
33974
- *
33975
- * ```ts
33976
- * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
33977
- * const sq = db.$with('sq').as(db.select({
33978
- * name: sql<string>`upper(${users.name})`.as('name'),
33979
- * })
33980
- * .from(users));
33981
- *
33982
- * const result = await db.with(sq).select({ name: sq.name }).from(sq);
33983
- * ```
33984
- */
33985
- $with(alias) {
33986
- const self2 = this;
33987
- return {
33988
- as(qb) {
33989
- if (typeof qb === "function") {
33990
- qb = qb(new QueryBuilder2(self2.dialect));
33991
- }
33992
- return new Proxy(
33993
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
33994
- new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
33995
- );
33996
- }
33997
- };
33998
- }
33999
34197
  $count(source, filters) {
34000
34198
  return new SQLiteCountBuilder({ source, filters, session: this.session });
34001
34199
  }
@@ -38818,22 +39016,26 @@ var init_query_builder4 = __esm({
38818
39016
  constructor(dialect6) {
38819
39017
  __publicField(this, "dialect");
38820
39018
  __publicField(this, "dialectConfig");
38821
- this.dialect = is(dialect6, MySqlDialect) ? dialect6 : void 0;
38822
- this.dialectConfig = is(dialect6, MySqlDialect) ? void 0 : dialect6;
38823
- }
38824
- $with(alias) {
38825
- const queryBuilder = this;
38826
- return {
38827
- as(qb) {
39019
+ __publicField(this, "$with", (alias, selection) => {
39020
+ const queryBuilder = this;
39021
+ const as = (qb) => {
38828
39022
  if (typeof qb === "function") {
38829
39023
  qb = qb(queryBuilder);
38830
39024
  }
38831
39025
  return new Proxy(
38832
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
39026
+ new WithSubquery(
39027
+ qb.getSQL(),
39028
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
39029
+ alias,
39030
+ true
39031
+ ),
38833
39032
  new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
38834
39033
  );
38835
- }
38836
- };
39034
+ };
39035
+ return { as };
39036
+ });
39037
+ this.dialect = is(dialect6, MySqlDialect) ? dialect6 : void 0;
39038
+ this.dialectConfig = is(dialect6, MySqlDialect) ? void 0 : dialect6;
38837
39039
  }
38838
39040
  with(...queries) {
38839
39041
  const self2 = this;
@@ -39288,6 +39490,56 @@ var init_db3 = __esm({
39288
39490
  MySqlDatabase = class {
39289
39491
  constructor(dialect6, session, schema5, mode) {
39290
39492
  __publicField(this, "query");
39493
+ /**
39494
+ * Creates a subquery that defines a temporary named result set as a CTE.
39495
+ *
39496
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
39497
+ *
39498
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
39499
+ *
39500
+ * @param alias The alias for the subquery.
39501
+ *
39502
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
39503
+ *
39504
+ * @example
39505
+ *
39506
+ * ```ts
39507
+ * // Create a subquery with alias 'sq' and use it in the select query
39508
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
39509
+ *
39510
+ * const result = await db.with(sq).select().from(sq);
39511
+ * ```
39512
+ *
39513
+ * 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:
39514
+ *
39515
+ * ```ts
39516
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
39517
+ * const sq = db.$with('sq').as(db.select({
39518
+ * name: sql<string>`upper(${users.name})`.as('name'),
39519
+ * })
39520
+ * .from(users));
39521
+ *
39522
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
39523
+ * ```
39524
+ */
39525
+ __publicField(this, "$with", (alias, selection) => {
39526
+ const self2 = this;
39527
+ const as = (qb) => {
39528
+ if (typeof qb === "function") {
39529
+ qb = qb(new QueryBuilder3(self2.dialect));
39530
+ }
39531
+ return new Proxy(
39532
+ new WithSubquery(
39533
+ qb.getSQL(),
39534
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
39535
+ alias,
39536
+ true
39537
+ ),
39538
+ new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
39539
+ );
39540
+ };
39541
+ return { as };
39542
+ });
39291
39543
  this.dialect = dialect6;
39292
39544
  this.session = session;
39293
39545
  this.mode = mode;
@@ -39316,52 +39568,6 @@ var init_db3 = __esm({
39316
39568
  }
39317
39569
  }
39318
39570
  }
39319
- /**
39320
- * Creates a subquery that defines a temporary named result set as a CTE.
39321
- *
39322
- * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
39323
- *
39324
- * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
39325
- *
39326
- * @param alias The alias for the subquery.
39327
- *
39328
- * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
39329
- *
39330
- * @example
39331
- *
39332
- * ```ts
39333
- * // Create a subquery with alias 'sq' and use it in the select query
39334
- * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
39335
- *
39336
- * const result = await db.with(sq).select().from(sq);
39337
- * ```
39338
- *
39339
- * 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:
39340
- *
39341
- * ```ts
39342
- * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
39343
- * const sq = db.$with('sq').as(db.select({
39344
- * name: sql<string>`upper(${users.name})`.as('name'),
39345
- * })
39346
- * .from(users));
39347
- *
39348
- * const result = await db.with(sq).select({ name: sq.name }).from(sq);
39349
- * ```
39350
- */
39351
- $with(alias) {
39352
- const self2 = this;
39353
- return {
39354
- as(qb) {
39355
- if (typeof qb === "function") {
39356
- qb = qb(new QueryBuilder3(self2.dialect));
39357
- }
39358
- return new Proxy(
39359
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
39360
- new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
39361
- );
39362
- }
39363
- };
39364
- }
39365
39571
  $count(source, filters) {
39366
39572
  return new MySqlCountBuilder({ source, filters, session: this.session });
39367
39573
  }
@@ -43917,22 +44123,26 @@ var init_query_builder5 = __esm({
43917
44123
  constructor(dialect6) {
43918
44124
  __publicField(this, "dialect");
43919
44125
  __publicField(this, "dialectConfig");
43920
- this.dialect = is(dialect6, SingleStoreDialect) ? dialect6 : void 0;
43921
- this.dialectConfig = is(dialect6, SingleStoreDialect) ? void 0 : dialect6;
43922
- }
43923
- $with(alias) {
43924
- const queryBuilder = this;
43925
- return {
43926
- as(qb) {
44126
+ __publicField(this, "$with", (alias, selection) => {
44127
+ const queryBuilder = this;
44128
+ const as = (qb) => {
43927
44129
  if (typeof qb === "function") {
43928
44130
  qb = qb(queryBuilder);
43929
44131
  }
43930
44132
  return new Proxy(
43931
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
44133
+ new WithSubquery(
44134
+ qb.getSQL(),
44135
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
44136
+ alias,
44137
+ true
44138
+ ),
43932
44139
  new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
43933
44140
  );
43934
- }
43935
- };
44141
+ };
44142
+ return { as };
44143
+ });
44144
+ this.dialect = is(dialect6, SingleStoreDialect) ? dialect6 : void 0;
44145
+ this.dialectConfig = is(dialect6, SingleStoreDialect) ? void 0 : dialect6;
43936
44146
  }
43937
44147
  with(...queries) {
43938
44148
  const self2 = this;
@@ -44145,6 +44355,56 @@ var init_db4 = __esm({
44145
44355
  // We are waiting for SingleStore support for `json_array` function
44146
44356
  /**@inrernal */
44147
44357
  __publicField(this, "query");
44358
+ /**
44359
+ * Creates a subquery that defines a temporary named result set as a CTE.
44360
+ *
44361
+ * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
44362
+ *
44363
+ * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
44364
+ *
44365
+ * @param alias The alias for the subquery.
44366
+ *
44367
+ * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
44368
+ *
44369
+ * @example
44370
+ *
44371
+ * ```ts
44372
+ * // Create a subquery with alias 'sq' and use it in the select query
44373
+ * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
44374
+ *
44375
+ * const result = await db.with(sq).select().from(sq);
44376
+ * ```
44377
+ *
44378
+ * 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:
44379
+ *
44380
+ * ```ts
44381
+ * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
44382
+ * const sq = db.$with('sq').as(db.select({
44383
+ * name: sql<string>`upper(${users.name})`.as('name'),
44384
+ * })
44385
+ * .from(users));
44386
+ *
44387
+ * const result = await db.with(sq).select({ name: sq.name }).from(sq);
44388
+ * ```
44389
+ */
44390
+ __publicField(this, "$with", (alias, selection) => {
44391
+ const self2 = this;
44392
+ const as = (qb) => {
44393
+ if (typeof qb === "function") {
44394
+ qb = qb(new QueryBuilder4(self2.dialect));
44395
+ }
44396
+ return new Proxy(
44397
+ new WithSubquery(
44398
+ qb.getSQL(),
44399
+ selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
44400
+ alias,
44401
+ true
44402
+ ),
44403
+ new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
44404
+ );
44405
+ };
44406
+ return { as };
44407
+ });
44148
44408
  this.dialect = dialect6;
44149
44409
  this.session = session;
44150
44410
  this._ = schema5 ? {
@@ -44158,52 +44418,6 @@ var init_db4 = __esm({
44158
44418
  };
44159
44419
  this.query = {};
44160
44420
  }
44161
- /**
44162
- * Creates a subquery that defines a temporary named result set as a CTE.
44163
- *
44164
- * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
44165
- *
44166
- * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
44167
- *
44168
- * @param alias The alias for the subquery.
44169
- *
44170
- * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
44171
- *
44172
- * @example
44173
- *
44174
- * ```ts
44175
- * // Create a subquery with alias 'sq' and use it in the select query
44176
- * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
44177
- *
44178
- * const result = await db.with(sq).select().from(sq);
44179
- * ```
44180
- *
44181
- * 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:
44182
- *
44183
- * ```ts
44184
- * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
44185
- * const sq = db.$with('sq').as(db.select({
44186
- * name: sql<string>`upper(${users.name})`.as('name'),
44187
- * })
44188
- * .from(users));
44189
- *
44190
- * const result = await db.with(sq).select({ name: sq.name }).from(sq);
44191
- * ```
44192
- */
44193
- $with(alias) {
44194
- const self2 = this;
44195
- return {
44196
- as(qb) {
44197
- if (typeof qb === "function") {
44198
- qb = qb(new QueryBuilder4(self2.dialect));
44199
- }
44200
- return new Proxy(
44201
- new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true),
44202
- new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
44203
- );
44204
- }
44205
- };
44206
- }
44207
44421
  $count(source, filters) {
44208
44422
  return new SingleStoreCountBuilder({ source, filters, session: this.session });
44209
44423
  }
@@ -45474,15 +45688,31 @@ var init_singlestoreImports = __esm({
45474
45688
  var singlestorePushUtils_exports = {};
45475
45689
  __export(singlestorePushUtils_exports, {
45476
45690
  filterStatements: () => filterStatements2,
45691
+ findColumnTypeAlternations: () => findColumnTypeAlternations,
45477
45692
  logSuggestionsAndReturn: () => logSuggestionsAndReturn3
45478
45693
  });
45694
+ function findColumnTypeAlternations(columns1, columns2) {
45695
+ const changes = [];
45696
+ for (const key in columns1) {
45697
+ if (columns1.hasOwnProperty(key) && columns2.hasOwnProperty(key)) {
45698
+ const col1 = columns1[key];
45699
+ const col2 = columns2[key];
45700
+ if (col1.type !== col2.type) {
45701
+ changes.push(col2.name);
45702
+ }
45703
+ }
45704
+ }
45705
+ return changes;
45706
+ }
45479
45707
  var import_hanji10, filterStatements2, logSuggestionsAndReturn3;
45480
45708
  var init_singlestorePushUtils = __esm({
45481
45709
  "src/cli/commands/singlestorePushUtils.ts"() {
45482
45710
  "use strict";
45483
45711
  init_source();
45484
45712
  import_hanji10 = __toESM(require_hanji());
45713
+ init_sqlgenerator();
45485
45714
  init_singlestoreSchema();
45715
+ init_utils();
45486
45716
  init_selector_ui();
45487
45717
  init_outputs();
45488
45718
  filterStatements2 = (statements, currentSchema, prevSchema) => {
@@ -45526,7 +45756,7 @@ var init_singlestorePushUtils = __esm({
45526
45756
  return true;
45527
45757
  });
45528
45758
  };
45529
- logSuggestionsAndReturn3 = async (db, statements, json22) => {
45759
+ logSuggestionsAndReturn3 = async (db, statements, json22, json1) => {
45530
45760
  let shouldAskForApprove = false;
45531
45761
  const statementsToExecute = [];
45532
45762
  const infoToPrint = [];
@@ -45707,6 +45937,71 @@ var init_singlestorePushUtils = __esm({
45707
45937
  shouldAskForApprove = true;
45708
45938
  }
45709
45939
  }
45940
+ } else if (statement.type === "singlestore_recreate_table") {
45941
+ const tableName = statement.tableName;
45942
+ const prevColumns = json1.tables[tableName].columns;
45943
+ const currentColumns = json22.tables[tableName].columns;
45944
+ const { removedColumns, addedColumns } = findAddedAndRemoved(
45945
+ Object.keys(prevColumns),
45946
+ Object.keys(currentColumns)
45947
+ );
45948
+ if (removedColumns.length) {
45949
+ for (const removedColumn of removedColumns) {
45950
+ const res = await db.query(
45951
+ `select count(\`${tableName}\`.\`${removedColumn}\`) as count from \`${tableName}\``
45952
+ );
45953
+ const count2 = Number(res[0].count);
45954
+ if (count2 > 0) {
45955
+ infoToPrint.push(
45956
+ `\xB7 You're about to delete ${source_default.underline(
45957
+ removedColumn
45958
+ )} column in ${tableName} table with ${count2} items`
45959
+ );
45960
+ columnsToRemove.push(removedColumn);
45961
+ shouldAskForApprove = true;
45962
+ }
45963
+ }
45964
+ }
45965
+ if (addedColumns.length) {
45966
+ for (const addedColumn of addedColumns) {
45967
+ const [res] = await db.query(
45968
+ `select count(*) as count from \`${tableName}\``
45969
+ );
45970
+ const columnConf = json22.tables[tableName].columns[addedColumn];
45971
+ const count2 = Number(res.count);
45972
+ if (count2 > 0 && columnConf.notNull && !columnConf.default) {
45973
+ infoToPrint.push(
45974
+ `\xB7 You're about to add not-null ${source_default.underline(
45975
+ addedColumn
45976
+ )} column without default value to table, which contains ${count2} items`
45977
+ );
45978
+ shouldAskForApprove = true;
45979
+ tablesToTruncate.push(tableName);
45980
+ statementsToExecute.push(`TRUNCATE TABLE \`${tableName}\`;`);
45981
+ }
45982
+ }
45983
+ }
45984
+ const columnWithChangedType = findColumnTypeAlternations(prevColumns, currentColumns);
45985
+ for (const column5 of columnWithChangedType) {
45986
+ const [res] = await db.query(
45987
+ `select count(*) as count from \`${tableName}\` WHERE \`${tableName}\`.\`${column5}\` IS NOT NULL;`
45988
+ );
45989
+ const count2 = Number(res.count);
45990
+ if (count2 > 0) {
45991
+ infoToPrint.push(
45992
+ `\xB7 You're about recreate ${source_default.underline(tableName)} table with data type changing for ${source_default.underline(
45993
+ column5
45994
+ )} column, which contains ${count2} items`
45995
+ );
45996
+ shouldAskForApprove = true;
45997
+ tablesToTruncate.push(tableName);
45998
+ statementsToExecute.push(`TRUNCATE TABLE \`${tableName}\`;`);
45999
+ }
46000
+ }
46001
+ }
46002
+ const stmnt = fromJson([statement], "singlestore", "push");
46003
+ if (typeof stmnt !== "undefined") {
46004
+ statementsToExecute.push(...stmnt);
45710
46005
  }
45711
46006
  }
45712
46007
  return {
@@ -46535,7 +46830,8 @@ var pushSingleStoreSchema = async (imports, drizzleInstance, databaseName) => {
46535
46830
  const { shouldAskForApprove, statementsToExecute, infoToPrint } = await logSuggestionsAndReturn4(
46536
46831
  db,
46537
46832
  statements,
46538
- validatedCur
46833
+ validatedCur,
46834
+ validatedPrev
46539
46835
  );
46540
46836
  return {
46541
46837
  hasDataLoss: shouldAskForApprove,