drizzle-kit 0.17.0-c637bb8 → 0.17.0-c6501de

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/index.js +18488 -18368
  2. package/package.json +4 -2
  3. package/readme.md +88 -14
  4. package/utils.js +33 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-kit",
3
- "version": "0.17.0-c637bb8",
3
+ "version": "0.17.0-c6501de",
4
4
  "repository": "https://github.com/drizzle-team/drizzle-kit-mirror",
5
5
  "author": "Drizzle Team",
6
6
  "license": "MIT",
@@ -9,11 +9,13 @@
9
9
  },
10
10
  "scripts": {
11
11
  "migrate:old": "drizzle-kit generate:pg --out ./dev/migrations-pg --schema ./dev/migrations-pg/schema.ts",
12
+ "push": "node -r esbuild-register ./src/cli/index.ts push:mysql",
12
13
  "migrate:old:mysql": "drizzle-kit generate:mysql --out ./dev/migrations-mysql --schema ./dev/migrations-mysql/schema.ts",
13
14
  "start:pg": "node -r esbuild-register ./src/cli/index.ts generate:pg --out ./dev/migrations-pg --schema ./dev/migrations-pg/schema.ts",
15
+ "start:sqlite": "node -r esbuild-register ./src/cli/index.ts generate:sqlite --out ./dev/migrations-sqlite --schema ./dev/migrations-sqlite/schema.ts",
14
16
  "start:mysql": "node -r esbuild-register ./src/cli/index.ts generate:mysql --out ./dev/migrations-mysql --schema ./dev/migrations-mysql/schema.ts",
15
17
  "check:pg": "node -r esbuild-register ./src/cli/index.ts check --out ./dev/migrations --dialect pg",
16
- "introspect:mysql": "node -r esbuild-register ./src/cli/index.ts introspect:mysql --out ./dev/introspect-mysql --connectionString",
18
+ "introspect:mysql": "node -r esbuild-register ./src/cli/index.ts introspect:mysql",
17
19
  "introspect:pg": "node -r esbuild-register ./src/cli/index.ts introspect:pg --out ./dev/introspect-pg --connectionString=postgresql://postgres@localhost:5432/introspect",
18
20
  "up:pg": "node -r esbuild-register ./src/cli/index.ts up:pg --out ./dev/migrations-pg",
19
21
  "up:mysql": "node -r esbuild-register ./src/cli/index.ts up:mysql --out ./dev/migrations-mysql",
package/readme.md CHANGED
@@ -1,12 +1,15 @@
1
1
  ## Drizzle Kit
2
+
2
3
  DrizzleKit - is a CLI migrator tool for DrizzleORM. It is probably one and only tool that lets you completely automatically generate SQL migrations and covers ~95% of the common cases like delitions and renames by prompting user input.\
3
- https://github.com/drizzle-team/drizzle-kit-mirror - is a mirror repository for issues.
4
+ <https://github.com/drizzle-team/drizzle-kit-mirror> - is a mirror repository for issues.
4
5
 
5
6
  ### How it works
7
+
6
8
  `drizzle-kit` will traverse `schema folder` or `schema file`, generate schema snapshot and compare it to the previous version(if there's one).\
7
9
  Based on the difference it will generate all needed SQL migrations and if there're any `automatically unresolvable` cases like `renames` it will prompt user for input.
8
10
 
9
11
  For schema file:
12
+
10
13
  ```typescript
11
14
  // ./src/db/schema.ts
12
15
 
@@ -26,17 +29,19 @@ export const authOtp = pgTable("auth_otp", {
26
29
  userId: integer("user_id").references(() => users.id),
27
30
  });
28
31
  ```
32
+
29
33
  It will generate:
34
+
30
35
  ```SQL
31
36
  CREATE TABLE IF NOT EXISTS auth_otp (
32
- "id" SERIAL PRIMARY KEY,
33
- "phone" character varying(256),
34
- "user_id" INT
37
+ "id" SERIAL PRIMARY KEY,
38
+ "phone" character varying(256),
39
+ "user_id" INT
35
40
  );
36
41
 
37
42
  CREATE TABLE IF NOT EXISTS users (
38
- "id" SERIAL PRIMARY KEY,
39
- "full_name" character varying(256)
43
+ "id" SERIAL PRIMARY KEY,
44
+ "full_name" character varying(256)
40
45
  );
41
46
 
42
47
  DO $$ BEGIN
@@ -49,26 +54,69 @@ CREATE INDEX IF NOT EXISTS users_full_name_index ON users (full_name);
49
54
  ```
50
55
 
51
56
  ### Installation & configuration
57
+
52
58
  ```shell
53
- $ npm install -D drizzle-kit
59
+ npm install -D drizzle-kit
54
60
  ```
55
61
 
56
62
  Running with CLI options
63
+
57
64
  ```shell
58
- $ npm exec drizzle-kit generate --out migrations-folder --dialect pg --schema src/db/schema.ts
65
+ npm exec drizzle-kit generate --out migrations-folder --dialect pg --schema src/db/schema.ts
59
66
  ```
60
67
 
61
68
  Or put your file to `drizzle.config.json` configuration file:
69
+
62
70
  ```json
63
71
  {
64
72
  "out": "./migrations-folder",
65
73
  "schema": "./src/db"
66
74
  }
67
75
  ```
76
+
68
77
  ---
78
+
79
+ ## Upgrading to 0.17.0
80
+
81
+ Before running any new migrations `drizzle-kit` will ask you to upgrade in a first place
82
+
83
+ Migration file structure < 0.17.0
84
+
85
+ ```plaintext
86
+ 📦 <project root>
87
+ └ 📂 migrations
88
+ └ 📂 20221207174503
89
+ ├ 📜 migration.sql
90
+ ├ 📜 snapshot.json
91
+ └ 📂 20230101104503
92
+ ├ 📜 migration.sql
93
+ ├ 📜 snapshot.json
94
+ ```
95
+
96
+ Migration file structure >= 0.17.0
97
+
98
+ ```plaintext
99
+ 📦 <project root>
100
+ └ 📂 migrations
101
+ └ 📂 meta
102
+ ├ 📜 _journal.json
103
+ ├ 📜 0000_snapshot.json
104
+ ├ 📜 0001_snapshot.json
105
+ └ 📜 0000_icy_stranger.sql
106
+ └ 📜 0001_strange_avengers.sql
107
+ ```
108
+
109
+ To easily migrate from previous folder structure to new you need to run `up` command in drizzle kit. It's a great helper to upgrade your migrations to new format on each drizzle kit major update
110
+
111
+ ![](media/up_mysql.gif)
112
+ ---
113
+
69
114
  ## List of commands
70
115
 
71
116
  ### Generate SQL migrations based on current .ts schema\
117
+
118
+ ---
119
+
72
120
  **`$ drizzle-kit generate:pg`** \
73
121
  **`$ drizzle-kit generate:mysql`** \
74
122
  **`$ drizzle-kit generate:sqlite`** \
@@ -76,6 +124,7 @@ Or put your file to `drizzle.config.json` configuration file:
76
124
  `--config` [optional defalut=drizzle.config.json] config file path\
77
125
  `--schema` path to typescript schema file or folder with multiple schema files\
78
126
  `--out` [optional default=drizzle/] migrations folder
127
+
79
128
  ```shell
80
129
  $ drizzle-kit generate:pg
81
130
  ## runs generate command with drizzle.config.json
@@ -91,40 +140,65 @@ $ drizzle-kit generate:pg --schema=./src/schema.ts --out=./migrations/
91
140
  ```
92
141
 
93
142
  ### Introspect existing database and generate typescript schema
94
- **`$ drizzle-kit introspect:pg`**
143
+
144
+ ---
145
+
146
+ **`$ drizzle-kit introspect:pg`** \
147
+ **`$ drizzle-kit introspect:mysql`**
148
+
95
149
  ```shell
96
150
  drizzle-kit introspect:pg --out=migrations/ --connectionString=postgresql://user:pass@host:port/db_name
97
151
 
98
152
  drizzle-kit introspect:pg --out=migrations/ --host=0.0.0.0 --port=5432 --user=postgres --password=pass --database=db_name --ssl
99
153
  ```
100
154
 
155
+ ![](media/introspect_mysql.gif)
156
+
101
157
  ### Update stale snapshots
158
+
159
+ ---
160
+
102
161
  **`$ drizzle-kit up:pg`** \
103
162
  **`$ drizzle-kit up:mysql`**\
104
163
  **`$ drizzle-kit up:sqlite`**
105
164
 
106
165
  `--out` [optional] migrations folder\
107
166
  `--config` [optional defalut=drizzle.config.json] config file path
167
+
108
168
  ```shell
109
169
  ## migrations folder is taken from drizzle.config.json
110
- drizzle-kit up:pg
170
+ drizzle-kit up:mysql
111
171
 
112
- drizzle-kit up:pg --out=migrations/
172
+ drizzle-kit up:mysql --out=migrations/
113
173
  ```
114
174
 
175
+ ![](media/up_mysql.gif)
176
+
177
+ ### Drop migration
178
+
179
+ ---
180
+
181
+ **`$ drizzle-kit drop`** \
182
+
183
+ `--out` [optional] migrations folder\
184
+ `--config` [optional defalut=drizzle.config.json] config file path
185
+
186
+ ![](media/drop.gif)
187
+
115
188
  ### Migrations collisions check
189
+
190
+ ---
191
+
116
192
  **`$ drizzle-kit check:pg`**\
117
193
  **`$ drizzle-kit check:mysql`**\
118
194
  **`$ drizzle-kit check:sqlite`**
119
195
 
120
196
  `--out` [optional] migration folder\
121
197
  `--config` [optional defalut=drizzle.config.json] config file path
198
+
122
199
  ```shell
123
200
  ## migrations folder is taken from drizzle.config.json
124
201
  drizzle-kit check:pg
125
202
 
126
203
  drizzle-kit check:pg --out=migrations/
127
204
  ```
128
-
129
-
130
-
package/utils.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -11232,7 +11231,9 @@ var MySqlCreateTableConvertor = class extends Convertor {
11232
11231
  `;
11233
11232
  if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
11234
11233
  const compositePK4 = MySqlSquasher.unsquashPK(compositePKs[0]);
11235
- statement += `ALTER TABLE ${tName} ADD PRIMARY KEY(\`${compositePK4.columns.join("`,`")}\`);`;
11234
+ statement += `ALTER TABLE ${tName} ADD PRIMARY KEY(\`${compositePK4.columns.join(
11235
+ "`,`"
11236
+ )}\`);`;
11236
11237
  statement += `
11237
11238
  `;
11238
11239
  }
@@ -11326,7 +11327,7 @@ var DropTableConvertor = class extends Convertor {
11326
11327
  }
11327
11328
  convert(statement) {
11328
11329
  const { tableName } = statement;
11329
- return `DROP TABLE ${tableName};`;
11330
+ return `DROP TABLE \`${tableName}\`;`;
11330
11331
  }
11331
11332
  };
11332
11333
  var PgRenameTableConvertor = class extends Convertor {
@@ -11514,27 +11515,9 @@ var PgAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
11514
11515
  return `ALTER TABLE ${tableName} ALTER COLUMN "${columnName}" DROP DEFAULT;`;
11515
11516
  }
11516
11517
  };
11517
- var MySqlAlterTableAlterColumnSetDefaultConvertor = class extends Convertor {
11518
- can(statement, dialect3) {
11519
- return statement.type === "alter_table_alter_column_set_default" && dialect3 === "mysql";
11520
- }
11521
- convert(statement) {
11522
- const { tableName, columnName } = statement;
11523
- return `ALTER TABLE ${tableName} ALTER COLUMN \`${columnName}\` SET DEFAULT ${statement.newDefaultValue};`;
11524
- }
11525
- };
11526
- var MySqlAlterTableAlterColumnDropDefaultConvertor = class extends Convertor {
11527
- can(statement, dialect3) {
11528
- return statement.type === "alter_table_alter_column_drop_default" && dialect3 === "mysql";
11529
- }
11530
- convert(statement) {
11531
- const { tableName, columnName } = statement;
11532
- return `ALTER TABLE ${tableName} ALTER COLUMN \`${columnName}\` DROP DEFAULT;`;
11533
- }
11534
- };
11535
11518
  var MySqlModifyColumn = class extends Convertor {
11536
11519
  can(statement, dialect3) {
11537
- return (statement.type === "alter_table_alter_column_set_type" || statement.type === "alter_table_alter_column_set_notnull" || statement.type === "alter_table_alter_column_drop_notnull" || statement.type === "alter_table_alter_column_drop_on_update" || statement.type === "alter_table_alter_column_set_on_update" || statement.type === "alter_table_alter_column_set_autoincrement" || statement.type === "alter_table_alter_column_drop_autoincrement") && dialect3 === "mysql";
11520
+ return (statement.type === "alter_table_alter_column_set_type" || statement.type === "alter_table_alter_column_set_notnull" || statement.type === "alter_table_alter_column_drop_notnull" || statement.type === "alter_table_alter_column_drop_on_update" || statement.type === "alter_table_alter_column_set_on_update" || statement.type === "alter_table_alter_column_set_autoincrement" || statement.type === "alter_table_alter_column_drop_autoincrement" || statement.type === "alter_table_alter_column_set_default" || statement.type === "alter_table_alter_column_drop_default") && dialect3 === "mysql";
11538
11521
  }
11539
11522
  convert(statement) {
11540
11523
  const { tableName, columnName } = statement;
@@ -11579,6 +11562,18 @@ var MySqlModifyColumn = class extends Convertor {
11579
11562
  columnType = ` ${statement.newDataType}`;
11580
11563
  columnDefault = statement.columnDefault ? ` DEFAULT ${statement.columnDefault}` : "";
11581
11564
  columnAutoincrement = "";
11565
+ } else if (statement.type === "alter_table_alter_column_set_default") {
11566
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
11567
+ columnOnUpdate = columnOnUpdate = statement.columnOnUpdate ? ` ON UPDATE CURRENT_TIMESTAMP` : "";
11568
+ columnType = ` ${statement.newDataType}`;
11569
+ columnDefault = ` DEFAULT ${statement.newDefaultValue}`;
11570
+ columnAutoincrement = statement.columnAutoIncrement ? " AUTO_INCREMENT" : "";
11571
+ } else if (statement.type === "alter_table_alter_column_drop_default") {
11572
+ columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
11573
+ columnOnUpdate = columnOnUpdate = statement.columnOnUpdate ? ` ON UPDATE CURRENT_TIMESTAMP` : "";
11574
+ columnType = ` ${statement.newDataType}`;
11575
+ columnDefault = "";
11576
+ columnAutoincrement = statement.columnAutoIncrement ? " AUTO_INCREMENT" : "";
11582
11577
  } else {
11583
11578
  columnType = ` ${statement.newDataType}`;
11584
11579
  columnNotNull = statement.columnNotNull ? ` NOT NULL` : "";
@@ -12143,8 +12138,6 @@ convertors.push(new PgAlterTableAlterColumnDropNotNullConvertor());
12143
12138
  convertors.push(new PgAlterTableAlterColumnSetDefaultConvertor());
12144
12139
  convertors.push(new PgAlterTableAlterColumnDropDefaultConvertor());
12145
12140
  convertors.push(new MySqlModifyColumn());
12146
- convertors.push(new MySqlAlterTableAlterColumnSetDefaultConvertor());
12147
- convertors.push(new MySqlAlterTableAlterColumnDropDefaultConvertor());
12148
12141
  convertors.push(new PgCreateForeignKeyConvertor());
12149
12142
  convertors.push(new MySqlCreateForeignKeyConvertor());
12150
12143
  convertors.push(new PgAlterForeignKeyConvertor());
@@ -12417,7 +12410,11 @@ var _prepareAlterColumns = (tableName, schema3, columns, json2) => {
12417
12410
  tableName,
12418
12411
  columnName,
12419
12412
  newDefaultValue: column4.default.value,
12420
- schema: schema3
12413
+ schema: schema3,
12414
+ columnOnUpdate,
12415
+ columnNotNull,
12416
+ columnAutoIncrement,
12417
+ newDataType: columnType
12421
12418
  });
12422
12419
  }
12423
12420
  if (((_c = column4.default) == null ? void 0 : _c.type) === "changed") {
@@ -12426,7 +12423,11 @@ var _prepareAlterColumns = (tableName, schema3, columns, json2) => {
12426
12423
  tableName,
12427
12424
  columnName,
12428
12425
  newDefaultValue: column4.default.new,
12429
- schema: schema3
12426
+ schema: schema3,
12427
+ columnOnUpdate,
12428
+ columnNotNull,
12429
+ columnAutoIncrement,
12430
+ newDataType: columnType
12430
12431
  });
12431
12432
  }
12432
12433
  if (((_d = column4.default) == null ? void 0 : _d.type) === "deleted") {
@@ -12434,7 +12435,12 @@ var _prepareAlterColumns = (tableName, schema3, columns, json2) => {
12434
12435
  type: "alter_table_alter_column_drop_default",
12435
12436
  tableName,
12436
12437
  columnName,
12437
- schema: schema3
12438
+ schema: schema3,
12439
+ columnDefault,
12440
+ columnOnUpdate,
12441
+ columnNotNull,
12442
+ columnAutoIncrement,
12443
+ newDataType: columnType
12438
12444
  });
12439
12445
  }
12440
12446
  if (((_e = column4.notNull) == null ? void 0 : _e.type) === "added") {