drizzle-kit 0.17.0-f32940d → 0.17.0

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 +18 -8
  2. package/package.json +1 -1
  3. package/readme.md +88 -14
package/index.js CHANGED
@@ -17553,6 +17553,7 @@ var init_upFolders = __esm({
17553
17553
  date.setUTCHours(Number(it.substring(8, 10)));
17554
17554
  date.setUTCMinutes(Number(it.substring(10, 12)));
17555
17555
  date.setUTCSeconds(Number(it.substring(12, 14)));
17556
+ date.setUTCMilliseconds(0);
17556
17557
  const path2 = (0, import_path.join)(out, it);
17557
17558
  const pathJson = (0, import_path.join)(out, it, "snapshot.json");
17558
17559
  const pathSQL = (0, import_path.join)(out, it, "migration.sql");
@@ -25922,7 +25923,7 @@ var init_mysqlSerializer = __esm({
25922
25923
  const dataType = column6["DATA_TYPE"];
25923
25924
  const columnType = column6["COLUMN_TYPE"];
25924
25925
  const isPrimary = column6["COLUMN_KEY"] === "PRI";
25925
- const isAutoincrement = column6["EXTRA"] === "auto_increment" && isPrimary;
25926
+ const isAutoincrement = column6["EXTRA"] === "auto_increment";
25926
25927
  const isDefaultAnExpression = column6["EXTRA"] === "DEFAULT_GENERATED";
25927
25928
  const columnDefault = column6["COLUMN_DEFAULT"];
25928
25929
  const collation = column6["CHARACTER_SET_NAME"];
@@ -27180,7 +27181,7 @@ var init_migrate = __esm({
27180
27181
  result.deleted.push(...leftMissing);
27181
27182
  return result;
27182
27183
  };
27183
- writeResult = (cur, sqlStatements, journal, _meta, outFolder, breakpoints) => {
27184
+ writeResult = (cur, sqlStatements, journal, _meta, outFolder, breakpoints, isIntrospect = false) => {
27184
27185
  console.log(schema(cur));
27185
27186
  if (sqlStatements.length === 0) {
27186
27187
  console.log("No schema changes, nothing to migrate \u{1F634}");
@@ -27198,7 +27199,14 @@ var init_migrate = __esm({
27198
27199
  JSON.stringify(toSave, null, 2)
27199
27200
  );
27200
27201
  const sqlDelimiter = breakpoints ? "--> statement-breakpoint\n" : "\n";
27201
- const sql = sqlStatements.join(sqlDelimiter);
27202
+ let sql = sqlStatements.join(sqlDelimiter);
27203
+ if (isIntrospect) {
27204
+ sql = `-- Current sql file was generated after introspecting the database
27205
+ -- If you want to run this migration please uncomment this code before executing migraitons
27206
+ /*
27207
+ ${sql}
27208
+ */`;
27209
+ }
27202
27210
  journal.entries.push({
27203
27211
  idx,
27204
27212
  version: cur.version,
@@ -48827,13 +48835,13 @@ import { sql } from "drizzle-orm/sql"
48827
48835
  if (lowered.startsWith("int")) {
48828
48836
  let out = `${name.camelCase()}: int("${name}")`;
48829
48837
  out += autoincrement ? `.autoincrement()` : "";
48830
- out += defaultValue ? `.default(${mapColumnDefault(defaultValue)})` : "";
48838
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault(defaultValue)})` : "";
48831
48839
  return out;
48832
48840
  }
48833
48841
  if (lowered.startsWith("tinyint")) {
48834
48842
  let out = `${name.camelCase()}: tinyint("${name}")`;
48835
48843
  out += autoincrement ? `.autoincrement()` : "";
48836
- out += defaultValue ? `.default(${mapColumnDefault(defaultValue)})` : "";
48844
+ out += typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault(defaultValue)})` : "";
48837
48845
  return out;
48838
48846
  }
48839
48847
  if (lowered.startsWith("smallint")) {
@@ -48971,7 +48979,7 @@ import { sql } from "drizzle-orm/sql"
48971
48979
  params = { precision, scale };
48972
48980
  }
48973
48981
  let out = params ? `${name.camelCase()}: decimal("${name}", ${timeConfig2(params)})` : `${name.camelCase()}: decimal("${name}")`;
48974
- defaultValue = defaultValue ? `.default(${mapColumnDefault(defaultValue)})` : "";
48982
+ defaultValue = typeof defaultValue !== "undefined" ? `.default(${mapColumnDefault(defaultValue)})` : "";
48975
48983
  out += defaultValue;
48976
48984
  return out;
48977
48985
  }
@@ -49648,7 +49656,8 @@ var introspectPgCommand = new Command("introspect:pg").option("--out <out>", `Mi
49648
49656
  journal,
49649
49657
  _meta,
49650
49658
  res.data.out,
49651
- res.data.breakpoints
49659
+ res.data.breakpoints,
49660
+ true
49652
49661
  );
49653
49662
  } else {
49654
49663
  (0, import_hanji6.render)(
@@ -49699,7 +49708,8 @@ var introspectMySqlCommand = new Command("introspect:mysql").option("--out <out>
49699
49708
  journal,
49700
49709
  _meta,
49701
49710
  res.data.out,
49702
- res.data.breakpoints
49711
+ res.data.breakpoints,
49712
+ true
49703
49713
  );
49704
49714
  } else {
49705
49715
  (0, import_hanji6.render)(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-kit",
3
- "version": "0.17.0-f32940d",
3
+ "version": "0.17.0",
4
4
  "repository": "https://github.com/drizzle-team/drizzle-kit-mirror",
5
5
  "author": "Drizzle Team",
6
6
  "license": "MIT",
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
-