drizzle-kit 0.16.1 → 0.16.3

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 (3) hide show
  1. package/index.js +76 -40
  2. package/package.json +1 -1
  3. package/readme.md +43 -22
package/index.js CHANGED
@@ -11819,31 +11819,31 @@ var require_node2 = __commonJS({
11819
11819
  var configAbsolutePath = walkForTsConfig(cwd);
11820
11820
  return configAbsolutePath ? path5.resolve(configAbsolutePath) : void 0;
11821
11821
  }
11822
- function walkForTsConfig(directory, existsSync2) {
11823
- if (existsSync2 === void 0) {
11824
- existsSync2 = fs32.existsSync;
11822
+ function walkForTsConfig(directory, existsSync3) {
11823
+ if (existsSync3 === void 0) {
11824
+ existsSync3 = fs32.existsSync;
11825
11825
  }
11826
11826
  var configPath = path5.join(directory, "./tsconfig.json");
11827
- if (existsSync2(configPath)) {
11827
+ if (existsSync3(configPath)) {
11828
11828
  return configPath;
11829
11829
  }
11830
11830
  var parentDirectory = path5.join(directory, "../");
11831
11831
  if (directory === parentDirectory) {
11832
11832
  return void 0;
11833
11833
  }
11834
- return walkForTsConfig(parentDirectory, existsSync2);
11834
+ return walkForTsConfig(parentDirectory, existsSync3);
11835
11835
  }
11836
11836
  exports2.walkForTsConfig = walkForTsConfig;
11837
- function loadTsconfig(configFilePath, existsSync2, readFileSync2) {
11838
- if (existsSync2 === void 0) {
11839
- existsSync2 = fs32.existsSync;
11837
+ function loadTsconfig(configFilePath, existsSync3, readFileSync2) {
11838
+ if (existsSync3 === void 0) {
11839
+ existsSync3 = fs32.existsSync;
11840
11840
  }
11841
11841
  if (readFileSync2 === void 0) {
11842
11842
  readFileSync2 = function(filename) {
11843
11843
  return fs32.readFileSync(filename, "utf8");
11844
11844
  };
11845
11845
  }
11846
- if (!existsSync2(configFilePath)) {
11846
+ if (!existsSync3(configFilePath)) {
11847
11847
  return void 0;
11848
11848
  }
11849
11849
  var configString = readFileSync2(configFilePath);
@@ -11856,10 +11856,10 @@ var require_node2 = __commonJS({
11856
11856
  }
11857
11857
  var currentDir = path5.dirname(configFilePath);
11858
11858
  var extendedConfigPath = path5.join(currentDir, extendedConfig);
11859
- if (extendedConfig.indexOf("/") !== -1 && extendedConfig.indexOf(".") !== -1 && !existsSync2(extendedConfigPath)) {
11859
+ if (extendedConfig.indexOf("/") !== -1 && extendedConfig.indexOf(".") !== -1 && !existsSync3(extendedConfigPath)) {
11860
11860
  extendedConfigPath = path5.join(currentDir, "node_modules", extendedConfig);
11861
11861
  }
11862
- var base = loadTsconfig(extendedConfigPath, existsSync2, readFileSync2) || {};
11862
+ var base = loadTsconfig(extendedConfigPath, existsSync3, readFileSync2) || {};
11863
11863
  if (base.compilerOptions && base.compilerOptions.baseUrl) {
11864
11864
  var extendsDir = path5.dirname(extendedConfig);
11865
11865
  base.compilerOptions.baseUrl = path5.join(extendsDir, base.compilerOptions.baseUrl);
@@ -13863,10 +13863,10 @@ var init_sqlgenerator = __esm({
13863
13863
  convert(statement) {
13864
13864
  const newFk = PgSquasher.unsquashFK(statement.data);
13865
13865
  const oldFk = PgSquasher.unsquashFK(statement.oldFkey);
13866
- let sql = `ALTER TABLE ${oldFk.tableFrom} DROP FOREIGN KEY ${oldFk.name};
13866
+ let sql = `ALTER TABLE ${oldFk.tableFrom} DROP CONSTRAINT ${oldFk.name};
13867
13867
  `;
13868
- const onDeleteStatement = newFk.onDelete || "";
13869
- const onUpdateStatement = newFk.onUpdate || "";
13868
+ const onDeleteStatement = newFk.onDelete ? `ON DELETE ${newFk.onDelete}` : "";
13869
+ const onUpdateStatement = newFk.onUpdate ? `ON UPDATE ${newFk.onDelete}` : "";
13870
13870
  const fromColumnsString = newFk.columnsFrom.map((it) => `"${it}"`).join(",");
13871
13871
  const toColumnsString = newFk.columnsTo.map((it) => `"${it}"`).join(",");
13872
13872
  const alterStatement = `ALTER TABLE ${newFk.tableFrom} ADD CONSTRAINT ${newFk.name} FOREIGN KEY (${fromColumnsString}) REFERENCES ${newFk.tableTo}(${toColumnsString}) ${onDeleteStatement} ${onUpdateStatement}`.replace(/ +/g, " ").trim();
@@ -20585,7 +20585,7 @@ var init_jsonStatements = __esm({
20585
20585
  });
20586
20586
  };
20587
20587
  prepareAlterTableColumnsJson = (tableName, deleted, added, altered, addedFk, dialect6) => {
20588
- const statements = [];
20588
+ const addColumns = [];
20589
20589
  const dropColumns = _prepareDropColumns(tableName, deleted);
20590
20590
  const alterColumns = _prepareAlterColumns(tableName, altered);
20591
20591
  if (dialect6 === "sqlite") {
@@ -20595,14 +20595,12 @@ var init_jsonStatements = __esm({
20595
20595
  added,
20596
20596
  jsonCreateFKStatements
20597
20597
  );
20598
- statements.push(...sqliteAddColumns);
20598
+ addColumns.push(...sqliteAddColumns);
20599
20599
  } else {
20600
- const addColumns = _prepareAddColumns(tableName, added);
20601
- statements.push(...addColumns);
20600
+ const addColumns2 = _prepareAddColumns(tableName, added);
20601
+ addColumns2.push(...addColumns2);
20602
20602
  }
20603
- statements.push(...dropColumns);
20604
- statements.push(...alterColumns);
20605
- return statements;
20603
+ return { addColumns, dropColumns, alterColumns };
20606
20604
  };
20607
20605
  _prepareDropColumns = (taleName, columns) => {
20608
20606
  return columns.map((it) => {
@@ -21020,7 +21018,7 @@ var init_snapshotsDiffer = __esm({
21020
21018
  jsonDeletedCompositePKs.push(...deletedCompositePKs);
21021
21019
  jsonAlteredCompositePKs.push(...alteredCompositePKs);
21022
21020
  });
21023
- const jsonAlterTables = allAlteredResolved.map((it) => {
21021
+ const jsonTableAlternations = allAlteredResolved.map((it) => {
21024
21022
  return prepareAlterTableColumnsJson(
21025
21023
  it.name,
21026
21024
  it.deleted,
@@ -21029,7 +21027,12 @@ var init_snapshotsDiffer = __esm({
21029
21027
  it.addedForeignKeys,
21030
21028
  dialect6
21031
21029
  );
21032
- }).flat();
21030
+ }).flat().reduce((res, it) => {
21031
+ res.createColumns.push(...it.addColumns);
21032
+ res.dropColumns.push(...it.dropColumns);
21033
+ res.alterColumns.push(...it.alterColumns);
21034
+ return res;
21035
+ }, { createColumns: [], dropColumns: [], alterColumns: [] });
21033
21036
  const jsonCreateIndexesForAllAlteredTables = allAltered.map((it) => {
21034
21037
  return prepareCreateIndexesJson(it.name, it.addedIndexes || {});
21035
21038
  }).flat();
@@ -21091,15 +21094,17 @@ var init_snapshotsDiffer = __esm({
21091
21094
  jsonStatements.push(...jsonDropTables);
21092
21095
  jsonStatements.push(...jsonRenameTables);
21093
21096
  jsonStatements.push(...jsonRenameColumnsStatements);
21094
- jsonStatements.push(...jsonAlterTables);
21097
+ jsonStatements.push(...jsonTableAlternations.alterColumns);
21098
+ jsonStatements.push(...jsonAlterReferencesForAlteredTables);
21099
+ jsonStatements.push(...jsonTableAlternations.dropColumns);
21100
+ jsonStatements.push(...jsonTableAlternations.createColumns);
21095
21101
  if (dialect6 !== "sqlite")
21096
21102
  jsonStatements.push(...jsonCreateReferences);
21097
- jsonStatements.push(...jsonAlterReferencesForAlteredTables);
21103
+ jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
21098
21104
  jsonStatements.push(...jsonCreateIndexesForCreatedTables);
21099
21105
  jsonStatements.push(...jsonCreateIndexesForAllAlteredTables);
21100
- jsonStatements.push(...jsonDropIndexesForAllAlteredTables);
21101
- jsonStatements.push(...jsonAddedCompositePKs);
21102
21106
  jsonStatements.push(...jsonDeletedCompositePKs);
21107
+ jsonStatements.push(...jsonAddedCompositePKs);
21103
21108
  jsonStatements.push(...jsonAlteredCompositePKs);
21104
21109
  jsonStatements.push(...jsonSetTableSchemas);
21105
21110
  jsonStatements.push(...filteredJsonSetNewTableSchemas);
@@ -26377,7 +26382,7 @@ init_source();
26377
26382
  // package.json
26378
26383
  var package_default = {
26379
26384
  name: "drizzle-kit",
26380
- version: "0.16.1",
26385
+ version: "0.16.3",
26381
26386
  repository: "https://github.com/drizzle-team/drizzle-kit-mirror",
26382
26387
  author: "Alex Blokh <aleksandrblokh@gmail.com>",
26383
26388
  license: "MIT",
@@ -26739,6 +26744,19 @@ var printVersions = () => {
26739
26744
  console.log(`${source_default.gray(versions())}
26740
26745
  `);
26741
26746
  };
26747
+ var assertEitherConfigOrOut = (config, out) => {
26748
+ if (out)
26749
+ return out;
26750
+ if (!(0, import_fs8.existsSync)(import_path6.default.join(import_path6.default.resolve(config)))) {
26751
+ console.log(`${config} file does not exist`);
26752
+ process.exit(1);
26753
+ }
26754
+ console.log(`Reading ${config}`);
26755
+ const drizzleConfig = JSON.parse(
26756
+ import_fs8.default.readFileSync(import_path6.default.join(import_path6.default.resolve(config))).toString()
26757
+ );
26758
+ return drizzleConfig.out;
26759
+ };
26742
26760
  var versions = () => {
26743
26761
  const { npmVersion } = pgVersions();
26744
26762
  const ormVersion = npmVersion ? `
@@ -26815,11 +26833,13 @@ var prepareGenerateConfig = (options) => {
26815
26833
  return configSchema.parse({ schema: schema4, out });
26816
26834
  };
26817
26835
  var checkSchema = objectType({
26818
- out: stringType().default("drizzle")
26836
+ out: stringType().optional(),
26837
+ config: stringType().default("drizzle.config.json")
26819
26838
  }).strict();
26820
- var checkPgCommand = new Command("check:pg").option("--out <out>", `Output folder, 'drizzle' by default`).action((options) => {
26839
+ var checkPgCommand = new Command("check:pg").option("--out <out>", `Output folder`).option("--config <config>", `Config path [default=drizzle.config.json]`).action((options) => {
26821
26840
  printVersions();
26822
- const { out } = checkSchema.parse(options);
26841
+ const params = checkSchema.parse(options);
26842
+ const out = assertEitherConfigOrOut(params.config, params.out);
26823
26843
  if (!out) {
26824
26844
  console.log(error(`'out' folder param must be set`));
26825
26845
  process.exit(0);
@@ -26827,9 +26847,10 @@ var checkPgCommand = new Command("check:pg").option("--out <out>", `Output folde
26827
26847
  checkHandler(out, "pg");
26828
26848
  console.log("Everything's fine \u{1F436}\u{1F525}");
26829
26849
  });
26830
- var checkSqliteCommand = new Command("check:sqlite").option("--out <out>", `Output folder, 'drizzle' by default`).action((options) => {
26850
+ var checkSqliteCommand = new Command("check:sqlite").option("--out <out>", `Output folder`).option("--config <config>", `Config path [default=drizzle.config.json]`).action((options) => {
26831
26851
  printVersions();
26832
- const { out } = checkSchema.parse(options);
26852
+ const params = checkSchema.parse(options);
26853
+ const out = assertEitherConfigOrOut(params.config, params.out);
26833
26854
  if (!out) {
26834
26855
  console.log(error(`'out' folder param must be set`));
26835
26856
  process.exit(0);
@@ -26837,28 +26858,43 @@ var checkSqliteCommand = new Command("check:sqlite").option("--out <out>", `Outp
26837
26858
  checkHandler(out, "sqlite");
26838
26859
  console.log("Everything's fine \u{1F436}\u{1F525}");
26839
26860
  });
26840
- var upPgCommand = new Command("up:pg").option("--out <out>", `Output folder, 'drizzle' by default`).action((options) => {
26861
+ var upPgCommand = new Command("up:pg").option("--out <out>", `Output folder`).option("--config <config>", `Config path [default=drizzle.config.json]`).action((options) => {
26841
26862
  printVersions();
26842
- const { out } = checkSchema.parse(options);
26863
+ const params = checkSchema.parse(options);
26864
+ const out = assertEitherConfigOrOut(params.config, params.out);
26843
26865
  if (!out) {
26844
- console.log(error(`'out' folder param must be set`));
26866
+ console.log(
26867
+ error(
26868
+ `'out' folder param must be set through '--out' param or in config file`
26869
+ )
26870
+ );
26845
26871
  process.exit(0);
26846
26872
  }
26847
26873
  assertPackages("drizzle-orm", "drizzle-orm-pg");
26848
26874
  upPgHandler(out);
26849
26875
  });
26850
- var upMysqlCommand = new Command("up:mysql").option("--out <out>", `Output folder, 'drizzle' by default`).action((options) => {
26876
+ var upMysqlCommand = new Command("up:mysql").option("--out <out>", `Output folder`).option("--config <config>", `Config path [default=drizzle.config.json]`).action((options) => {
26851
26877
  printVersions();
26852
- const { out } = checkSchema.parse(options);
26878
+ const params = checkSchema.parse(options);
26879
+ const out = assertEitherConfigOrOut(params.config, params.out);
26880
+ if (!out) {
26881
+ console.log(
26882
+ error(
26883
+ `'out' folder param must be set through '--out' param or in config file`
26884
+ )
26885
+ );
26886
+ process.exit(0);
26887
+ }
26853
26888
  if (!out) {
26854
26889
  console.log(error(`'out' folder param must be set`));
26855
26890
  process.exit(0);
26856
26891
  }
26857
26892
  upMysqlHandler(out);
26858
26893
  });
26859
- var upSqliteCommand = new Command("up:sqlite").option("--out <out>", `Output folder, 'drizzle' by default`).action((options) => {
26894
+ var upSqliteCommand = new Command("up:sqlite").option("--out <out>", `Output folder`).option("--config <config>", `Config path [default=drizzle.config.json]`).action((options) => {
26860
26895
  printVersions();
26861
- const { out } = checkSchema.parse(options);
26896
+ const params = checkSchema.parse(options);
26897
+ const out = assertEitherConfigOrOut(params.config, params.out);
26862
26898
  if (!out) {
26863
26899
  console.log(error(`'out' folder param must be set`));
26864
26900
  process.exit(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-kit",
3
- "version": "0.16.1",
3
+ "version": "0.16.3",
4
4
  "repository": "https://github.com/drizzle-team/drizzle-kit-mirror",
5
5
  "author": "Alex Blokh <aleksandrblokh@gmail.com>",
6
6
  "license": "MIT",
package/readme.md CHANGED
@@ -61,49 +61,70 @@ $ npm exec drizzle-kit generate --out migrations-folder --dialect pg --schema sr
61
61
  Or put your file to `drizzle.config.json` configuration file:
62
62
  ```json
63
63
  {
64
- "dialect": "pg",
65
64
  "out": "./migrations-folder",
66
65
  "schema": "./src/db"
67
66
  }
68
67
  ```
69
68
  ---
70
- ### List of commands
71
- **`$ drizzle-kit generate`** - generates SQL migrations based on .ts schema\
72
- `--config` [optional defalut=drizzle.config.json] path to an optional config file\
73
- `--dialect` [optional default=pg] database dialect, one of -> pg, mysql, sqlite\
74
- `--schema` path to a schema file or folder with multiple schema files\
75
- `--out` [optional default=drizzle/] place where to store migration files\
69
+ ## List of commands
70
+
71
+ ### Generate SQL migrations based on current .ts schema\
72
+ **`$ drizzle-kit generate:pg`** \
73
+ **`$ drizzle-kit generate:mysql`** \
74
+ **`$ drizzle-kit generate:sqlite`** \
75
+
76
+ `--config` [optional defalut=drizzle.config.json] config file path\
77
+ `--schema` path to typescript schema file or folder with multiple schema files\
78
+ `--out` [optional default=drizzle/] migrations folder
76
79
  ```shell
77
- $ drizzle-kit generate
80
+ $ drizzle-kit generate:pg
78
81
  ## runs generate command with drizzle.config.json
79
82
 
80
- $ drizzle-kit generate --config custom.config.json
83
+ $ drizzle-kit generate:pg --config=./custom.config.json
81
84
  ## runs generate command with custom.config.json
82
85
 
83
- $ drizzle-kit generate --dialect pg --schema src/schema.ts
86
+ $ drizzle-kit generate:pg --schema=./src/schema.ts
84
87
  ## runs generate command and outputs results to ./drizzle
85
88
 
86
- $ drizzle-kit generate --dialect .. --schema .. --out ./migration-folder
87
- ## runs generate command and outputs results to ./migration-folder
89
+ $ drizzle-kit generate:pg --schema=./src/schema.ts --out=./migrations/
90
+ ## runs generate command and outputs results to ./migration
88
91
  ```
89
92
 
90
-
91
- **`$ drizzle-kit introspect:pg`** - generate `schema.ts` file from existing PG database within seconds
93
+ ### Introspect existing database and generate typescript schema
94
+ **`$ drizzle-kit introspect:pg`**
92
95
  ```shell
93
96
  drizzle-kit introspect:pg --out=migrations/ --connectionString=postgresql://user:pass@host:port/db_name
94
97
 
95
98
  drizzle-kit introspect:pg --out=migrations/ --host=0.0.0.0 --port=5432 --user=postgres --password=pass --database=db_name --ssl
96
99
  ```
97
100
 
98
- **`$ drizzle-kit up`** - updates stale snapshots
99
- `--config` [optional defalut=drizzle.config.json] path to an optional config file\
100
- `--dialect` [optional default=pg] database dialect, one of -> pg, mysql, sqlite\
101
- `--schema` path to a schema file or folder with multiple schema files\
101
+ ### Update stale snapshots
102
+ **`$ drizzle-kit up:pg`** \
103
+ **`$ drizzle-kit up:mysql`**\
104
+ **`$ drizzle-kit up:sqlite`**
102
105
 
103
- **`$ drizzle-kit check`** - checks for collisions
104
- `--config` [optional defalut=drizzle.config.json] path to an optional config file\
105
- `--dialect` [optional default=pg] database dialect, one of -> pg, mysql, sqlite\
106
- `--schema` path to a schema file or folder with multiple schema files\
106
+ `--out` [optional] migrations folder\
107
+ `--config` [optional defalut=drizzle.config.json] config file path
108
+ ```shell
109
+ ## migrations folder is taken from drizzle.config.json
110
+ drizzle-kit up:pg
111
+
112
+ drizzle-kit up:pg --out=migrations/
113
+ ```
114
+
115
+ ### Migrations collisions check
116
+ **`$ drizzle-kit check:pg`**\
117
+ **`$ drizzle-kit check:mysql`**\
118
+ **`$ drizzle-kit check:sqlite`**
119
+
120
+ `--out` [optional] migration folder\
121
+ `--config` [optional defalut=drizzle.config.json] config file path
122
+ ```shell
123
+ ## migrations folder is taken from drizzle.config.json
124
+ drizzle-kit check:pg
125
+
126
+ drizzle-kit check:pg --out=migrations/
127
+ ```
107
128
 
108
129
 
109
130