drizzle-kit 0.29.1 → 0.30.0-21dab20

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 +93 -60
  2. package/api.mjs +93 -60
  3. package/bin.cjs +280 -106
  4. package/package.json +1 -1
package/bin.cjs CHANGED
@@ -28892,7 +28892,8 @@ var init_common = __esm({
28892
28892
  schemaFilter: unionType([stringType(), stringType().array()]).default(["public"]),
28893
28893
  migrations: configMigrations,
28894
28894
  dbCredentials: anyType().optional(),
28895
- casing: casingType.optional()
28895
+ casing: casingType.optional(),
28896
+ sql: booleanType().default(true)
28896
28897
  }).passthrough();
28897
28898
  casing = unionType([literalType("camel"), literalType("preserve")]).default(
28898
28899
  "camel"
@@ -34995,7 +34996,7 @@ var require_node2 = __commonJS({
34995
34996
  });
34996
34997
 
34997
34998
  // src/cli/commands/utils.ts
34998
- var import_fs3, import_hanji2, import_path3, assertES5, safeRegister, prepareCheckParams, prepareDropParams, prepareGenerateConfig, flattenDatabaseCredentials, flattenPull, preparePushConfig, preparePullConfig, prepareStudioConfig, migrateConfig, prepareMigrateConfig, drizzleConfigFromFile;
34999
+ var import_fs3, import_hanji2, import_path3, assertES5, safeRegister, prepareCheckParams, prepareDropParams, prepareGenerateConfig, prepareExportConfig, flattenDatabaseCredentials, flattenPull, preparePushConfig, preparePullConfig, prepareStudioConfig, migrateConfig, prepareMigrateConfig, drizzleConfigFromFile;
34999
35000
  var init_utils3 = __esm({
35000
35001
  "src/cli/commands/utils.ts"() {
35001
35002
  "use strict";
@@ -35104,6 +35105,26 @@ var init_utils3 = __esm({
35104
35105
  driver: driver2
35105
35106
  };
35106
35107
  };
35108
+ prepareExportConfig = async (options, from) => {
35109
+ const config = from === "config" ? await drizzleConfigFromFile(options.config, true) : options;
35110
+ const { schema: schema6, dialect: dialect6, sql } = config;
35111
+ if (!schema6 || !dialect6) {
35112
+ console.log(error("Please provide required params:"));
35113
+ console.log(wrapParam("schema", schema6));
35114
+ console.log(wrapParam("dialect", dialect6));
35115
+ process.exit(1);
35116
+ }
35117
+ const fileNames = prepareFilenames(schema6);
35118
+ if (fileNames.length === 0) {
35119
+ (0, import_hanji2.render)(`[${source_default.blue("i")}] No schema file in ${schema6} was found`);
35120
+ process.exit(0);
35121
+ }
35122
+ return {
35123
+ dialect: dialect6,
35124
+ schema: schema6,
35125
+ sql
35126
+ };
35127
+ };
35107
35128
  flattenDatabaseCredentials = (config) => {
35108
35129
  if ("dbCredentials" in config) {
35109
35130
  const { dbCredentials, ...rest } = config;
@@ -35544,7 +35565,7 @@ var init_utils3 = __esm({
35544
35565
  }
35545
35566
  assertUnreachable(dialect6);
35546
35567
  };
35547
- drizzleConfigFromFile = async (configPath) => {
35568
+ drizzleConfigFromFile = async (configPath, isExport) => {
35548
35569
  const prefix2 = process.env.TEST_CONFIG_PATH_PREFIX || "";
35549
35570
  const defaultTsConfigExists = (0, import_fs3.existsSync)((0, import_path3.resolve)((0, import_path3.join)(prefix2, "drizzle.config.ts")));
35550
35571
  const defaultJsConfigExists = (0, import_fs3.existsSync)((0, import_path3.resolve)((0, import_path3.join)(prefix2, "drizzle.config.js")));
@@ -35552,7 +35573,7 @@ var init_utils3 = __esm({
35552
35573
  (0, import_path3.join)((0, import_path3.resolve)("drizzle.config.json"))
35553
35574
  );
35554
35575
  const defaultConfigPath = defaultTsConfigExists ? "drizzle.config.ts" : defaultJsConfigExists ? "drizzle.config.js" : "drizzle.config.json";
35555
- if (!configPath) {
35576
+ if (!configPath && !isExport) {
35556
35577
  console.log(
35557
35578
  source_default.gray(
35558
35579
  `No config path provided, using default '${defaultConfigPath}'`
@@ -35564,7 +35585,8 @@ var init_utils3 = __esm({
35564
35585
  console.log(`${path5} file does not exist`);
35565
35586
  process.exit(1);
35566
35587
  }
35567
- console.log(source_default.grey(`Reading config file '${path5}'`));
35588
+ if (!isExport)
35589
+ console.log(source_default.grey(`Reading config file '${path5}'`));
35568
35590
  const { unregister } = await safeRegister();
35569
35591
  const required = require(`${path5}`);
35570
35592
  const content = required.default ?? required;
@@ -42787,7 +42809,7 @@ var init_sqlgenerator = __esm({
42787
42809
  const { tableName, schema: schema6, columns, compositePKs, uniqueConstraints, checkConstraints, policies, isRLSEnabled } = st;
42788
42810
  let statement = "";
42789
42811
  const name = schema6 ? `"${schema6}"."${tableName}"` : `"${tableName}"`;
42790
- statement += `CREATE TABLE IF NOT EXISTS ${name} (
42812
+ statement += `CREATE TABLE ${name} (
42791
42813
  `;
42792
42814
  for (let i2 = 0; i2 < columns.length; i2++) {
42793
42815
  const column9 = columns[i2];
@@ -43633,7 +43655,7 @@ WITH ${withCheckOption} CHECK OPTION` : "";
43633
43655
  convert(statement) {
43634
43656
  const { tableName, columnName, schema: schema6 } = statement;
43635
43657
  const tableNameWithSchema = schema6 ? `"${schema6}"."${tableName}"` : `"${tableName}"`;
43636
- return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN IF EXISTS "${columnName}";`;
43658
+ return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${columnName}";`;
43637
43659
  }
43638
43660
  };
43639
43661
  MySqlAlterTableDropColumnConvertor = class extends Convertor {
@@ -44054,7 +44076,7 @@ WITH ${withCheckOption} CHECK OPTION` : "";
44054
44076
  for (const table5 of Object.values(json2.tables)) {
44055
44077
  for (const index5 of Object.values(table5.indexes)) {
44056
44078
  const unsquashed = SQLiteSquasher.unsquashIdx(index5);
44057
- sqlStatements.push(`DROP INDEX IF EXISTS "${unsquashed.name}";`);
44079
+ sqlStatements.push(`DROP INDEX "${unsquashed.name}";`);
44058
44080
  indexes.push({ ...unsquashed, tableName: table5.name });
44059
44081
  }
44060
44082
  }
@@ -44570,13 +44592,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.newC
44570
44592
  const toColumnsString = columnsTo.map((it) => `"${it}"`).join(",");
44571
44593
  const tableNameWithSchema = statement.schema ? `"${statement.schema}"."${tableFrom}"` : `"${tableFrom}"`;
44572
44594
  const tableToNameWithSchema = schemaTo ? `"${schemaTo}"."${tableTo}"` : `"${tableTo}"`;
44573
- const alterStatement = `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${name}" FOREIGN KEY (${fromColumnsString}) REFERENCES ${tableToNameWithSchema}(${toColumnsString})${onDeleteStatement}${onUpdateStatement}`;
44574
- let sql = "DO $$ BEGIN\n";
44575
- sql += " " + alterStatement + ";\n";
44576
- sql += "EXCEPTION\n";
44577
- sql += " WHEN duplicate_object THEN null;\n";
44578
- sql += "END $$;\n";
44579
- return sql;
44595
+ const alterStatement = `ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${name}" FOREIGN KEY (${fromColumnsString}) REFERENCES ${tableToNameWithSchema}(${toColumnsString})${onDeleteStatement}${onUpdateStatement};`;
44596
+ return alterStatement;
44580
44597
  }
44581
44598
  };
44582
44599
  LibSQLCreateForeignKeyConvertor = class extends Convertor {
@@ -44633,12 +44650,8 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.newC
44633
44650
  const toColumnsString = newFk.columnsTo.map((it) => `"${it}"`).join(",");
44634
44651
  const tableFromNameWithSchema = oldFk.schemaTo ? `"${oldFk.schemaTo}"."${oldFk.tableFrom}"` : `"${oldFk.tableFrom}"`;
44635
44652
  const tableToNameWithSchema = newFk.schemaTo ? `"${newFk.schemaTo}"."${newFk.tableFrom}"` : `"${newFk.tableFrom}"`;
44636
- const alterStatement = `ALTER TABLE ${tableFromNameWithSchema} ADD CONSTRAINT "${newFk.name}" FOREIGN KEY (${fromColumnsString}) REFERENCES ${tableToNameWithSchema}(${toColumnsString})${onDeleteStatement}${onUpdateStatement}`;
44637
- sql += "DO $$ BEGIN\n";
44638
- sql += " " + alterStatement + ";\n";
44639
- sql += "EXCEPTION\n";
44640
- sql += " WHEN duplicate_object THEN null;\n";
44641
- sql += "END $$;\n";
44653
+ const alterStatement = `ALTER TABLE ${tableFromNameWithSchema} ADD CONSTRAINT "${newFk.name}" FOREIGN KEY (${fromColumnsString}) REFERENCES ${tableToNameWithSchema}(${toColumnsString})${onDeleteStatement}${onUpdateStatement};`;
44654
+ sql += alterStatement;
44642
44655
  return sql;
44643
44656
  }
44644
44657
  };
@@ -44694,7 +44707,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.newC
44694
44707
  reversedString = reversedString.slice(0, -1);
44695
44708
  return reversedString;
44696
44709
  }
44697
- return `CREATE ${indexPart}${concurrently ? " CONCURRENTLY" : ""} IF NOT EXISTS "${name}" ON ${tableNameWithSchema} USING ${method} (${value})${Object.keys(withMap).length !== 0 ? ` WITH (${reverseLogic(withMap)})` : ""}${where ? ` WHERE ${where}` : ""};`;
44710
+ return `CREATE ${indexPart}${concurrently ? " CONCURRENTLY" : ""} "${name}" ON ${tableNameWithSchema} USING ${method} (${value})${Object.keys(withMap).length !== 0 ? ` WITH (${reverseLogic(withMap)})` : ""}${where ? ` WHERE ${where}` : ""};`;
44698
44711
  }
44699
44712
  };
44700
44713
  CreateMySqlIndexConvertor = class extends Convertor {
@@ -44752,7 +44765,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.newC
44752
44765
  }
44753
44766
  convert(statement) {
44754
44767
  const { name } = PgSquasher.unsquashIdx(statement.data);
44755
- return `DROP INDEX IF EXISTS "${name}";`;
44768
+ return `DROP INDEX "${name}";`;
44756
44769
  }
44757
44770
  };
44758
44771
  PgCreateSchemaConvertor = class extends Convertor {
@@ -44823,7 +44836,7 @@ ${BREAKPOINT}ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${statement.newC
44823
44836
  }
44824
44837
  convert(statement) {
44825
44838
  const { name } = PgSquasher.unsquashIdx(statement.data);
44826
- return `DROP INDEX IF EXISTS \`${name}\`;`;
44839
+ return `DROP INDEX \`${name}\`;`;
44827
44840
  }
44828
44841
  };
44829
44842
  MySqlDropIndexConvertor = class extends Convertor {
@@ -51427,6 +51440,11 @@ __export(migrate_exports, {
51427
51440
  indPolicyResolver: () => indPolicyResolver,
51428
51441
  mySqlViewsResolver: () => mySqlViewsResolver,
51429
51442
  policyResolver: () => policyResolver,
51443
+ prepareAndExportLibSQL: () => prepareAndExportLibSQL,
51444
+ prepareAndExportMysql: () => prepareAndExportMysql,
51445
+ prepareAndExportPg: () => prepareAndExportPg,
51446
+ prepareAndExportSinglestore: () => prepareAndExportSinglestore,
51447
+ prepareAndExportSqlite: () => prepareAndExportSqlite,
51430
51448
  prepareAndMigrateLibSQL: () => prepareAndMigrateLibSQL,
51431
51449
  prepareAndMigrateMysql: () => prepareAndMigrateMysql,
51432
51450
  prepareAndMigratePg: () => prepareAndMigratePg,
@@ -51450,7 +51468,7 @@ __export(migrate_exports, {
51450
51468
  viewsResolver: () => viewsResolver,
51451
51469
  writeResult: () => writeResult
51452
51470
  });
51453
- var import_fs6, import_hanji3, import_path5, schemasResolver, tablesResolver, viewsResolver, mySqlViewsResolver, sqliteViewsResolver, sequencesResolver, roleResolver, policyResolver, indPolicyResolver, enumsResolver, columnsResolver, prepareAndMigratePg, preparePgPush, prepareMySQLPush, prepareAndMigrateMysql, prepareSingleStorePush, prepareAndMigrateSingleStore, prepareAndMigrateSqlite, prepareAndMigrateLibSQL, prepareSQLitePush, prepareLibSQLPush, promptColumnsConflicts, promptNamedConflict, promptNamedWithSchemasConflict, promptSchemasConflict, BREAKPOINT, writeResult, embeddedMigrations, prepareSnapshotFolderName, two;
51471
+ var import_fs6, import_hanji3, import_path5, schemasResolver, tablesResolver, viewsResolver, mySqlViewsResolver, sqliteViewsResolver, sequencesResolver, roleResolver, policyResolver, indPolicyResolver, enumsResolver, columnsResolver, prepareAndMigratePg, prepareAndExportPg, preparePgPush, prepareMySQLPush, prepareAndMigrateMysql, prepareSingleStorePush, prepareAndMigrateSingleStore, prepareAndExportSinglestore, prepareAndExportMysql, prepareAndMigrateSqlite, prepareAndExportSqlite, prepareAndMigrateLibSQL, prepareAndExportLibSQL, prepareSQLitePush, prepareLibSQLPush, promptColumnsConflicts, promptNamedConflict, promptNamedWithSchemasConflict, promptSchemasConflict, BREAKPOINT, writeResult, embeddedMigrations, prepareSnapshotFolderName, two;
51454
51472
  var init_migrate = __esm({
51455
51473
  "src/cli/commands/migrate.ts"() {
51456
51474
  "use strict";
@@ -51700,6 +51718,39 @@ var init_migrate = __esm({
51700
51718
  console.error(e2);
51701
51719
  }
51702
51720
  };
51721
+ prepareAndExportPg = async (config) => {
51722
+ const schemaPath = config.schema;
51723
+ try {
51724
+ const { prev, cur } = await preparePgMigrationSnapshot(
51725
+ [],
51726
+ // no snapshots before
51727
+ schemaPath,
51728
+ void 0
51729
+ );
51730
+ const validatedPrev = pgSchema.parse(prev);
51731
+ const validatedCur = pgSchema.parse(cur);
51732
+ const squashedPrev = squashPgScheme(validatedPrev);
51733
+ const squashedCur = squashPgScheme(validatedCur);
51734
+ const { sqlStatements } = await applyPgSnapshotsDiff(
51735
+ squashedPrev,
51736
+ squashedCur,
51737
+ schemasResolver,
51738
+ enumsResolver,
51739
+ sequencesResolver,
51740
+ policyResolver,
51741
+ indPolicyResolver,
51742
+ roleResolver,
51743
+ tablesResolver,
51744
+ columnsResolver,
51745
+ viewsResolver,
51746
+ validatedPrev,
51747
+ validatedCur
51748
+ );
51749
+ console.log(sqlStatements.join("\n"));
51750
+ } catch (e2) {
51751
+ console.error(e2);
51752
+ }
51753
+ };
51703
51754
  preparePgPush = async (cur, prev) => {
51704
51755
  const validatedPrev = pgSchema.parse(prev);
51705
51756
  const validatedCur = pgSchema.parse(cur);
@@ -51881,6 +51932,58 @@ var init_migrate = __esm({
51881
51932
  console.error(e2);
51882
51933
  }
51883
51934
  };
51935
+ prepareAndExportSinglestore = async (config) => {
51936
+ const schemaPath = config.schema;
51937
+ try {
51938
+ const { prev, cur } = await prepareSingleStoreMigrationSnapshot(
51939
+ [],
51940
+ schemaPath,
51941
+ void 0
51942
+ );
51943
+ const validatedPrev = singlestoreSchema.parse(prev);
51944
+ const validatedCur = singlestoreSchema.parse(cur);
51945
+ const squashedPrev = squashSingleStoreScheme(validatedPrev);
51946
+ const squashedCur = squashSingleStoreScheme(validatedCur);
51947
+ const { sqlStatements, _meta } = await applySingleStoreSnapshotsDiff(
51948
+ squashedPrev,
51949
+ squashedCur,
51950
+ tablesResolver,
51951
+ columnsResolver,
51952
+ /* singleStoreViewsResolver, */
51953
+ validatedPrev,
51954
+ validatedCur
51955
+ );
51956
+ console.log(sqlStatements.join("\n"));
51957
+ } catch (e2) {
51958
+ console.error(e2);
51959
+ }
51960
+ };
51961
+ prepareAndExportMysql = async (config) => {
51962
+ const schemaPath = config.schema;
51963
+ try {
51964
+ const { prev, cur, custom: custom2 } = await prepareMySqlMigrationSnapshot(
51965
+ [],
51966
+ schemaPath,
51967
+ void 0
51968
+ );
51969
+ const validatedPrev = mysqlSchema.parse(prev);
51970
+ const validatedCur = mysqlSchema.parse(cur);
51971
+ const squashedPrev = squashMysqlScheme(validatedPrev);
51972
+ const squashedCur = squashMysqlScheme(validatedCur);
51973
+ const { sqlStatements, statements, _meta } = await applyMysqlSnapshotsDiff(
51974
+ squashedPrev,
51975
+ squashedCur,
51976
+ tablesResolver,
51977
+ columnsResolver,
51978
+ mySqlViewsResolver,
51979
+ validatedPrev,
51980
+ validatedCur
51981
+ );
51982
+ console.log(sqlStatements.join("\n"));
51983
+ } catch (e2) {
51984
+ console.error(e2);
51985
+ }
51986
+ };
51884
51987
  prepareAndMigrateSqlite = async (config) => {
51885
51988
  const outFolder = config.out;
51886
51989
  const schemaPath = config.schema;
@@ -51936,6 +52039,32 @@ var init_migrate = __esm({
51936
52039
  console.error(e2);
51937
52040
  }
51938
52041
  };
52042
+ prepareAndExportSqlite = async (config) => {
52043
+ const schemaPath = config.schema;
52044
+ try {
52045
+ const { prev, cur } = await prepareSqliteMigrationSnapshot(
52046
+ [],
52047
+ schemaPath,
52048
+ void 0
52049
+ );
52050
+ const validatedPrev = sqliteSchema.parse(prev);
52051
+ const validatedCur = sqliteSchema.parse(cur);
52052
+ const squashedPrev = squashSqliteScheme(validatedPrev);
52053
+ const squashedCur = squashSqliteScheme(validatedCur);
52054
+ const { sqlStatements, _meta } = await applySqliteSnapshotsDiff(
52055
+ squashedPrev,
52056
+ squashedCur,
52057
+ tablesResolver,
52058
+ columnsResolver,
52059
+ sqliteViewsResolver,
52060
+ validatedPrev,
52061
+ validatedCur
52062
+ );
52063
+ console.log(sqlStatements.join("\n"));
52064
+ } catch (e2) {
52065
+ console.error(e2);
52066
+ }
52067
+ };
51939
52068
  prepareAndMigrateLibSQL = async (config) => {
51940
52069
  const outFolder = config.out;
51941
52070
  const schemaPath = config.schema;
@@ -51990,6 +52119,32 @@ var init_migrate = __esm({
51990
52119
  console.error(e2);
51991
52120
  }
51992
52121
  };
52122
+ prepareAndExportLibSQL = async (config) => {
52123
+ const schemaPath = config.schema;
52124
+ try {
52125
+ const { prev, cur, custom: custom2 } = await prepareSqliteMigrationSnapshot(
52126
+ [],
52127
+ schemaPath,
52128
+ void 0
52129
+ );
52130
+ const validatedPrev = sqliteSchema.parse(prev);
52131
+ const validatedCur = sqliteSchema.parse(cur);
52132
+ const squashedPrev = squashSqliteScheme(validatedPrev);
52133
+ const squashedCur = squashSqliteScheme(validatedCur);
52134
+ const { sqlStatements, _meta } = await applyLibSQLSnapshotsDiff(
52135
+ squashedPrev,
52136
+ squashedCur,
52137
+ tablesResolver,
52138
+ columnsResolver,
52139
+ sqliteViewsResolver,
52140
+ validatedPrev,
52141
+ validatedCur
52142
+ );
52143
+ console.log(sqlStatements.join("\n"));
52144
+ } catch (e2) {
52145
+ console.error(e2);
52146
+ }
52147
+ };
51993
52148
  prepareSQLitePush = async (schemaPath, snapshot, casing2) => {
51994
52149
  const { prev, cur } = await prepareSQLiteDbPushSnapshot(snapshot, schemaPath, casing2);
51995
52150
  const validatedPrev = sqliteSchema.parse(prev);
@@ -81454,8 +81609,7 @@ var init_introspect_sqlite = __esm({
81454
81609
  });
81455
81610
  if (Object.keys(table5.indexes).length > 0 || filteredFKs.length > 0 || Object.keys(table5.compositePrimaryKeys).length > 0 || Object.keys(table5.uniqueConstraints).length > 0 || Object.keys(table5.checkConstraints).length > 0) {
81456
81611
  statement += ",\n";
81457
- statement += "(table) => {\n";
81458
- statement += " return {\n";
81612
+ statement += "(table) => [";
81459
81613
  statement += createTableIndexes(
81460
81614
  table5.name,
81461
81615
  Object.values(table5.indexes),
@@ -81474,8 +81628,7 @@ var init_introspect_sqlite = __esm({
81474
81628
  Object.values(table5.checkConstraints),
81475
81629
  casing2
81476
81630
  );
81477
- statement += " }\n";
81478
- statement += "}";
81631
+ statement += "\n]";
81479
81632
  }
81480
81633
  statement += ");";
81481
81634
  return statement;
@@ -81632,12 +81785,11 @@ var init_introspect_sqlite = __esm({
81632
81785
  idxKey = withCasing(idxKey, casing2);
81633
81786
  const indexGeneratedName = indexName4(tableName, it.columns);
81634
81787
  const escapedIndexName = indexGeneratedName === it.name ? "" : `"${it.name}"`;
81635
- statement += ` ${idxKey}: `;
81788
+ statement += `
81789
+ `;
81636
81790
  statement += it.isUnique ? "uniqueIndex(" : "index(";
81637
81791
  statement += `${escapedIndexName})`;
81638
81792
  statement += `.on(${it.columns.map((it2) => `table.${withCasing(it2, casing2)}`).join(", ")}),`;
81639
- statement += `
81640
- `;
81641
81793
  });
81642
81794
  return statement;
81643
81795
  };
@@ -81645,39 +81797,36 @@ var init_introspect_sqlite = __esm({
81645
81797
  let statement = "";
81646
81798
  unqs.forEach((it) => {
81647
81799
  const idxKey = withCasing(it.name, casing2);
81648
- statement += ` ${idxKey}: `;
81800
+ statement += `
81801
+ `;
81649
81802
  statement += "unique(";
81650
81803
  statement += `"${it.name}")`;
81651
81804
  statement += `.on(${it.columns.map((it2) => `table.${withCasing(it2, casing2)}`).join(", ")}),`;
81652
- statement += `
81653
- `;
81654
81805
  });
81655
81806
  return statement;
81656
81807
  };
81657
81808
  createTableChecks = (checks, casing2) => {
81658
81809
  let statement = "";
81659
81810
  checks.forEach((it) => {
81660
- const checkKey = withCasing(it.name, casing2);
81661
- statement += ` ${checkKey}: `;
81811
+ statement += `
81812
+ `;
81662
81813
  statement += "check(";
81663
81814
  statement += `"${it.name}", `;
81664
81815
  statement += `sql\`${it.value}\`)`;
81665
- statement += `,
81666
- `;
81816
+ statement += `,`;
81667
81817
  });
81668
81818
  return statement;
81669
81819
  };
81670
81820
  createTablePKs = (pks, casing2) => {
81671
81821
  let statement = "";
81672
81822
  pks.forEach((it, i2) => {
81673
- statement += ` pk${i2}: `;
81823
+ statement += `
81824
+ `;
81674
81825
  statement += "primaryKey({ columns: [";
81675
81826
  statement += `${it.columns.map((c) => {
81676
81827
  return `table.${withCasing(c, casing2)}`;
81677
81828
  }).join(", ")}]${it.name ? `, name: "${it.name}"` : ""}}`;
81678
81829
  statement += ")";
81679
- statement += `
81680
- `;
81681
81830
  });
81682
81831
  return statement;
81683
81832
  };
@@ -81686,7 +81835,9 @@ var init_introspect_sqlite = __esm({
81686
81835
  fks.forEach((it) => {
81687
81836
  const isSelf4 = it.tableTo === it.tableFrom;
81688
81837
  const tableTo = isSelf4 ? "table" : `${withCasing(it.tableTo, casing2)}`;
81689
- statement += ` ${withCasing(it.name, casing2)}: foreignKey(() => ({
81838
+ statement += `
81839
+ `;
81840
+ statement += `foreignKey(() => ({
81690
81841
  `;
81691
81842
  statement += ` columns: [${it.columnsFrom.map((i2) => `table.${withCasing(i2, casing2)}`).join(", ")}],
81692
81843
  `;
@@ -81697,8 +81848,7 @@ var init_introspect_sqlite = __esm({
81697
81848
  statement += ` }))`;
81698
81849
  statement += it.onUpdate && it.onUpdate !== "no action" ? `.onUpdate("${it.onUpdate}")` : "";
81699
81850
  statement += it.onDelete && it.onDelete !== "no action" ? `.onDelete("${it.onDelete}")` : "";
81700
- statement += `,
81701
- `;
81851
+ statement += `,`;
81702
81852
  });
81703
81853
  return statement;
81704
81854
  };
@@ -82827,8 +82977,7 @@ var init_introspect_mysql = __esm({
82827
82977
  });
82828
82978
  if (Object.keys(table5.indexes).length > 0 || filteredFKs.length > 0 || Object.keys(table5.compositePrimaryKeys).length > 0 || Object.keys(table5.uniqueConstraints).length > 0 || Object.keys(table5.checkConstraint).length > 0) {
82829
82979
  statement += ",\n";
82830
- statement += "(table) => {\n";
82831
- statement += " return {\n";
82980
+ statement += "(table) => [";
82832
82981
  statement += createTableIndexes2(
82833
82982
  table5.name,
82834
82983
  Object.values(table5.indexes),
@@ -82847,8 +82996,7 @@ var init_introspect_mysql = __esm({
82847
82996
  Object.values(table5.checkConstraint),
82848
82997
  withCasing4
82849
82998
  );
82850
- statement += " }\n";
82851
- statement += "}";
82999
+ statement += "\n]";
82852
83000
  }
82853
83001
  statement += ");";
82854
83002
  return statement;
@@ -83218,12 +83366,11 @@ import { sql } from "drizzle-orm"
83218
83366
  idxKey = casing2(idxKey);
83219
83367
  const indexGeneratedName = indexName(tableName, it.columns);
83220
83368
  const escapedIndexName = indexGeneratedName === it.name ? "" : `"${it.name}"`;
83221
- statement += ` ${idxKey}: `;
83369
+ statement += `
83370
+ `;
83222
83371
  statement += it.isUnique ? "uniqueIndex(" : "index(";
83223
83372
  statement += `${escapedIndexName})`;
83224
83373
  statement += `.on(${it.columns.map((it2) => `table.${casing2(it2)}`).join(", ")}),`;
83225
- statement += `
83226
- `;
83227
83374
  });
83228
83375
  return statement;
83229
83376
  };
@@ -83231,25 +83378,23 @@ import { sql } from "drizzle-orm"
83231
83378
  let statement = "";
83232
83379
  unqs.forEach((it) => {
83233
83380
  const idxKey = casing2(it.name);
83234
- statement += ` ${idxKey}: `;
83381
+ statement += `
83382
+ `;
83235
83383
  statement += "unique(";
83236
83384
  statement += `"${it.name}")`;
83237
83385
  statement += `.on(${it.columns.map((it2) => `table.${casing2(it2)}`).join(", ")}),`;
83238
- statement += `
83239
- `;
83240
83386
  });
83241
83387
  return statement;
83242
83388
  };
83243
83389
  createTableChecks2 = (checks, casing2) => {
83244
83390
  let statement = "";
83245
83391
  checks.forEach((it) => {
83246
- const checkKey = casing2(it.name);
83247
- statement += ` ${checkKey}: `;
83392
+ statement += `
83393
+ `;
83248
83394
  statement += "check(";
83249
83395
  statement += `"${it.name}", `;
83250
83396
  statement += `sql\`${it.value.replace(/`/g, "\\`")}\`)`;
83251
- statement += `,
83252
- `;
83397
+ statement += `,`;
83253
83398
  });
83254
83399
  return statement;
83255
83400
  };
@@ -83257,14 +83402,13 @@ import { sql } from "drizzle-orm"
83257
83402
  let statement = "";
83258
83403
  pks.forEach((it) => {
83259
83404
  let idxKey = casing2(it.name);
83260
- statement += ` ${idxKey}: `;
83405
+ statement += `
83406
+ `;
83261
83407
  statement += "primaryKey({ columns: [";
83262
83408
  statement += `${it.columns.map((c) => {
83263
83409
  return `table.${casing2(c)}`;
83264
83410
  }).join(", ")}]${it.name ? `, name: "${it.name}"` : ""}}`;
83265
83411
  statement += "),";
83266
- statement += `
83267
- `;
83268
83412
  });
83269
83413
  return statement;
83270
83414
  };
@@ -83273,7 +83417,9 @@ import { sql } from "drizzle-orm"
83273
83417
  fks.forEach((it) => {
83274
83418
  const isSelf4 = it.tableTo === it.tableFrom;
83275
83419
  const tableTo = isSelf4 ? "table" : `${casing2(it.tableTo)}`;
83276
- statement += ` ${casing2(it.name)}: foreignKey({
83420
+ statement += `
83421
+ `;
83422
+ statement += `foreignKey({
83277
83423
  `;
83278
83424
  statement += ` columns: [${it.columnsFrom.map((i2) => `table.${casing2(i2)}`).join(", ")}],
83279
83425
  `;
@@ -83284,8 +83430,7 @@ import { sql } from "drizzle-orm"
83284
83430
  statement += ` })`;
83285
83431
  statement += it.onUpdate && it.onUpdate !== "no action" ? `.onUpdate("${it.onUpdate}")` : "";
83286
83432
  statement += it.onDelete && it.onDelete !== "no action" ? `.onDelete("${it.onDelete}")` : "";
83287
- statement += `,
83288
- `;
83433
+ statement += `,`;
83289
83434
  });
83290
83435
  return statement;
83291
83436
  };
@@ -83630,8 +83775,7 @@ var init_introspect_pg = __esm({
83630
83775
  statement += "}";
83631
83776
  if (Object.keys(table5.indexes).length > 0 || Object.values(table5.foreignKeys).length > 0 || Object.values(table5.policies).length > 0 || Object.keys(table5.compositePrimaryKeys).length > 0 || Object.keys(table5.uniqueConstraints).length > 0 || Object.keys(table5.checkConstraints).length > 0) {
83632
83777
  statement += ", ";
83633
- statement += "(table) => {\n";
83634
- statement += " return {\n";
83778
+ statement += "(table) => [";
83635
83779
  statement += createTableIndexes3(table5.name, Object.values(table5.indexes), casing2);
83636
83780
  statement += createTableFKs3(Object.values(table5.foreignKeys), schemas, casing2);
83637
83781
  statement += createTablePKs3(
@@ -83651,8 +83795,7 @@ var init_introspect_pg = __esm({
83651
83795
  Object.values(table5.checkConstraints),
83652
83796
  casing2
83653
83797
  );
83654
- statement += " }\n";
83655
- statement += "}";
83798
+ statement += "\n]";
83656
83799
  }
83657
83800
  statement += ");";
83658
83801
  return statement;
@@ -84047,7 +84190,8 @@ import { sql } from "drizzle-orm"
84047
84190
  it.columns.map((it2) => it2.expression)
84048
84191
  );
84049
84192
  const escapedIndexName = indexGeneratedName === it.name ? "" : `"${it.name}"`;
84050
- statement += ` ${idxKey}: `;
84193
+ statement += `
84194
+ `;
84051
84195
  statement += it.isUnique ? "uniqueIndex(" : "index(";
84052
84196
  statement += `${escapedIndexName})`;
84053
84197
  statement += `${it.concurrently ? `.concurrently()` : ""}`;
@@ -84070,23 +84214,21 @@ import { sql } from "drizzle-orm"
84070
84214
  return `${reversedString}}`;
84071
84215
  }
84072
84216
  statement += it.with && Object.keys(it.with).length > 0 ? `.with(${reverseLogic(it.with)})` : "";
84073
- statement += `,
84074
- `;
84217
+ statement += `,`;
84075
84218
  });
84076
84219
  return statement;
84077
84220
  };
84078
84221
  createTablePKs3 = (pks, casing2) => {
84079
84222
  let statement = "";
84080
84223
  pks.forEach((it) => {
84081
- let idxKey = withCasing2(it.name, casing2);
84082
- statement += ` ${idxKey}: `;
84224
+ statement += `
84225
+ `;
84083
84226
  statement += "primaryKey({ columns: [";
84084
84227
  statement += `${it.columns.map((c) => {
84085
84228
  return `table.${withCasing2(c, casing2)}`;
84086
84229
  }).join(", ")}]${it.name ? `, name: "${it.name}"` : ""}}`;
84087
84230
  statement += ")";
84088
- statement += `,
84089
- `;
84231
+ statement += `,`;
84090
84232
  });
84091
84233
  return statement;
84092
84234
  };
@@ -84098,39 +84240,37 @@ import { sql } from "drizzle-orm"
84098
84240
  const mappedItTo = (_a = it.to) == null ? void 0 : _a.map((v) => {
84099
84241
  return rolesNameToTsKey[v] ? withCasing2(rolesNameToTsKey[v], casing2) : `"${v}"`;
84100
84242
  });
84101
- statement += ` ${idxKey}: `;
84243
+ statement += `
84244
+ `;
84102
84245
  statement += "pgPolicy(";
84103
84246
  statement += `"${it.name}", { `;
84104
84247
  statement += `as: "${(_b = it.as) == null ? void 0 : _b.toLowerCase()}", for: "${(_c = it.for) == null ? void 0 : _c.toLowerCase()}", to: [${mappedItTo == null ? void 0 : mappedItTo.join(", ")}]${it.using ? `, using: sql\`${it.using}\`` : ""}${it.withCheck ? `, withCheck: sql\`${it.withCheck}\` ` : ""}`;
84105
- statement += ` }),
84106
- `;
84248
+ statement += ` }),`;
84107
84249
  });
84108
84250
  return statement;
84109
84251
  };
84110
84252
  createTableUniques3 = (unqs, casing2) => {
84111
84253
  let statement = "";
84112
84254
  unqs.forEach((it) => {
84113
- const idxKey = withCasing2(it.name, casing2);
84114
- statement += ` ${idxKey}: `;
84255
+ statement += `
84256
+ `;
84115
84257
  statement += "unique(";
84116
84258
  statement += `"${it.name}")`;
84117
84259
  statement += `.on(${it.columns.map((it2) => `table.${withCasing2(it2, casing2)}`).join(", ")})`;
84118
84260
  statement += it.nullsNotDistinct ? `.nullsNotDistinct()` : "";
84119
- statement += `,
84120
- `;
84261
+ statement += `,`;
84121
84262
  });
84122
84263
  return statement;
84123
84264
  };
84124
84265
  createTableChecks3 = (checkConstraints, casing2) => {
84125
84266
  let statement = "";
84126
84267
  checkConstraints.forEach((it) => {
84127
- const checkKey = withCasing2(it.name, casing2);
84128
- statement += ` ${checkKey}: `;
84268
+ statement += `
84269
+ `;
84129
84270
  statement += "check(";
84130
84271
  statement += `"${it.name}", `;
84131
84272
  statement += `sql\`${it.value}\`)`;
84132
- statement += `,
84133
- `;
84273
+ statement += `,`;
84134
84274
  });
84135
84275
  return statement;
84136
84276
  };
@@ -84141,7 +84281,9 @@ import { sql } from "drizzle-orm"
84141
84281
  const paramName = paramNameFor(it.tableTo, tableSchema);
84142
84282
  const isSelf4 = it.tableTo === it.tableFrom;
84143
84283
  const tableTo = isSelf4 ? "table" : `${withCasing2(paramName, casing2)}`;
84144
- statement += ` ${withCasing2(it.name, casing2)}: foreignKey({
84284
+ statement += `
84285
+ `;
84286
+ statement += `foreignKey({
84145
84287
  `;
84146
84288
  statement += ` columns: [${it.columnsFrom.map((i2) => `table.${withCasing2(i2, casing2)}`).join(", ")}],
84147
84289
  `;
@@ -84152,8 +84294,7 @@ import { sql } from "drizzle-orm"
84152
84294
  statement += ` })`;
84153
84295
  statement += it.onUpdate && it.onUpdate !== "no action" ? `.onUpdate("${it.onUpdate}")` : "";
84154
84296
  statement += it.onDelete && it.onDelete !== "no action" ? `.onDelete("${it.onDelete}")` : "";
84155
- statement += `,
84156
- `;
84297
+ statement += `,`;
84157
84298
  });
84158
84299
  return statement;
84159
84300
  };
@@ -84319,8 +84460,7 @@ var init_introspect_singlestore = __esm({
84319
84460
  statement += "}";
84320
84461
  if (Object.keys(table5.indexes).length > 0 || Object.keys(table5.compositePrimaryKeys).length > 0 || Object.keys(table5.uniqueConstraints).length > 0) {
84321
84462
  statement += ",\n";
84322
- statement += "(table) => {\n";
84323
- statement += " return {\n";
84463
+ statement += "(table) => [";
84324
84464
  statement += createTableIndexes4(
84325
84465
  table5.name,
84326
84466
  Object.values(table5.indexes),
@@ -84334,8 +84474,7 @@ var init_introspect_singlestore = __esm({
84334
84474
  Object.values(table5.uniqueConstraints),
84335
84475
  withCasing4
84336
84476
  );
84337
- statement += " }\n";
84338
- statement += "}";
84477
+ statement += "\n]";
84339
84478
  }
84340
84479
  statement += ");";
84341
84480
  return statement;
@@ -84638,25 +84777,22 @@ import { sql } from "drizzle-orm"
84638
84777
  idxKey = casing2(idxKey);
84639
84778
  const indexGeneratedName = indexName3(tableName, it.columns);
84640
84779
  const escapedIndexName = indexGeneratedName === it.name ? "" : `"${it.name}"`;
84641
- statement += ` ${idxKey}: `;
84780
+ statement += `
84781
+ `;
84642
84782
  statement += it.isUnique ? "uniqueIndex(" : "index(";
84643
84783
  statement += `${escapedIndexName})`;
84644
84784
  statement += `.on(${it.columns.map((it2) => `table.${casing2(it2)}`).join(", ")}),`;
84645
- statement += `
84646
- `;
84647
84785
  });
84648
84786
  return statement;
84649
84787
  };
84650
84788
  createTableUniques4 = (unqs, casing2) => {
84651
84789
  let statement = "";
84652
84790
  unqs.forEach((it) => {
84653
- const idxKey = casing2(it.name);
84654
- statement += ` ${idxKey}: `;
84791
+ statement += `
84792
+ `;
84655
84793
  statement += "unique(";
84656
84794
  statement += `"${it.name}")`;
84657
84795
  statement += `.on(${it.columns.map((it2) => `table.${casing2(it2)}`).join(", ")}),`;
84658
- statement += `
84659
- `;
84660
84796
  });
84661
84797
  return statement;
84662
84798
  };
@@ -84664,14 +84800,13 @@ import { sql } from "drizzle-orm"
84664
84800
  let statement = "";
84665
84801
  pks.forEach((it) => {
84666
84802
  let idxKey = casing2(it.name);
84667
- statement += ` ${idxKey}: `;
84803
+ statement += `
84804
+ `;
84668
84805
  statement += "primaryKey({ columns: [";
84669
84806
  statement += `${it.columns.map((c) => {
84670
84807
  return `table.${casing2(c)}`;
84671
84808
  }).join(", ")}]${it.name ? `, name: "${it.name}"` : ""}}`;
84672
84809
  statement += "),";
84673
- statement += `
84674
- `;
84675
84810
  });
84676
84811
  return statement;
84677
84812
  };
@@ -92429,13 +92564,52 @@ Drizzle Studio is up and running on ${source_default.blue(
92429
92564
  }
92430
92565
  }
92431
92566
  });
92567
+ var exportRaw = command2({
92568
+ name: "export",
92569
+ desc: "Generate diff between current state and empty state in specified formats: sql",
92570
+ options: {
92571
+ sql: boolean("sql").default(true).desc("Generate as sql"),
92572
+ config: optionConfig,
92573
+ dialect: optionDialect,
92574
+ schema: string().desc("Path to a schema file or folder")
92575
+ },
92576
+ transform: async (opts) => {
92577
+ const from = assertCollisions("export", opts, ["sql"], ["dialect", "schema"]);
92578
+ return prepareExportConfig(opts, from);
92579
+ },
92580
+ handler: async (opts) => {
92581
+ await assertOrmCoreVersion();
92582
+ await assertPackages("drizzle-orm");
92583
+ const {
92584
+ prepareAndExportPg: prepareAndExportPg2,
92585
+ prepareAndExportMysql: prepareAndExportMysql2,
92586
+ prepareAndExportSqlite: prepareAndExportSqlite2,
92587
+ prepareAndExportLibSQL: prepareAndExportLibSQL2,
92588
+ prepareAndExportSinglestore: prepareAndExportSinglestore2
92589
+ } = await Promise.resolve().then(() => (init_migrate(), migrate_exports));
92590
+ const dialect6 = opts.dialect;
92591
+ if (dialect6 === "postgresql") {
92592
+ await prepareAndExportPg2(opts);
92593
+ } else if (dialect6 === "mysql") {
92594
+ await prepareAndExportMysql2(opts);
92595
+ } else if (dialect6 === "sqlite") {
92596
+ await prepareAndExportSqlite2(opts);
92597
+ } else if (dialect6 === "turso") {
92598
+ await prepareAndExportLibSQL2(opts);
92599
+ } else if (dialect6 === "singlestore") {
92600
+ await prepareAndExportSinglestore2(opts);
92601
+ } else {
92602
+ assertUnreachable(dialect6);
92603
+ }
92604
+ }
92605
+ });
92432
92606
 
92433
92607
  // src/cli/index.ts
92434
92608
  init_utils5();
92435
92609
  var version2 = async () => {
92436
92610
  const { npmVersion } = await ormCoreVersions();
92437
92611
  const ormVersion = npmVersion ? `drizzle-orm: v${npmVersion}` : "";
92438
- const envVersion = "0.29.1";
92612
+ const envVersion = "0.30.0-21dab20";
92439
92613
  const kitVersion = envVersion ? `v${envVersion}` : "--";
92440
92614
  const versions = `drizzle-kit: ${kitVersion}
92441
92615
  ${ormVersion}`;
@@ -92469,7 +92643,7 @@ var legacy = [
92469
92643
  legacyCommand("check:mysql", "check"),
92470
92644
  legacyCommand("check:sqlite", "check")
92471
92645
  ];
92472
- run([generate, migrate, pull, push, studio, up, check, drop, ...legacy], {
92646
+ run([generate, migrate, pull, push, studio, up, check, drop, exportRaw, ...legacy], {
92473
92647
  name: "drizzle-kit",
92474
92648
  version: version2
92475
92649
  });