Repository navigation
Commit dabf761
fix(auth): carry Better Auth update hooks and defaults onto generated columns (#764)
Closes #756
## Problem
Better Auth applies `onUpdate` and `defaultValue` itself on its own
writes, but the Drizzle columns in `.void/better-auth-schema.ts` had
neither. Writes that go directly through Drizzle missed them:
`db.update()` on an auth table left `updatedAt` unchanged, and a row
inserted without `banned` stored `null` instead of `false`.
## Changes
`createColumnDefinition` now emits:
- `.$onUpdate(() => new Date())` for Better Auth's built-in `updatedAt`
fields, unless overridden through additional fields or a plugin
- `.$defaultFn(() => <literal>)` for string, number and boolean
defaults, on optional fields too
```ts
"emailVerified": integer("emailVerified", { mode: "boolean" }).notNull().$defaultFn(() => false),
"updatedAt": integer("updatedAt", { mode: "timestamp_ms" }).notNull().$onUpdate(() => new Date()),
"banned": integer("banned", { mode: "boolean" }).$defaultFn(() => false),
```
Both are runtime-only Drizzle modifiers, so existing apps need no
migration. `resolveFixedDefault` still returns only required fields'
defaults, so `migrationSchema` is unchanged.
Custom update callbacks remain handled by Better Auth and are omitted
from the generated Drizzle columns.
Function defaults such as `createdAt`'s `() => new Date()` are left out,
as are date and JSON literals, matching the existing exclusions.
Drizzle also runs `$onUpdate` on insert when the value is omitted, and
treats the column as optional in insert types. For `updatedAt` this
fills in the current time rather than failing the `NOT NULL` constraint.
## Verification
- New cases in `packages/void/test/unit/auth-drizzle.test.ts` cover
update hooks in all three dialects, required and optional literal
defaults, excluded function/date/JSON defaults, and unchanged migration
metadata.
- Ran the generated schemas through real Drizzle and drizzle-kit:
snapshots are identical with and without the modifiers on SQLite,
PostgreSQL and MySQL; the generated code typechecks in all three; on
SQLite a direct `update` sets `updatedAt` and an insert gets the literal
defaults.
- `vp run check` passes.
- `vp test run` locally: 16,453 passed, 8 failed.
- 5 are `api-webhooks` timeouts that also fail on `main` in a full run
on this machine.
- 3 are in `cf-build-output` (timeout) and `plugin-cloudflare-v2`
(inspector port `EADDRINUSE`). These did not fail on `main`, but passed
3 of 3 runs in isolation on this branch and do not reference the auth
generator.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Christoph Nakazawa <cnakazawa@cloudflare.com>1 parent 1003bc9 commit dabf761
2 files changed
Lines changed: 200 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
| 183 | + | |
184 | 184 | | |
185 | 185 | | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
191 | 189 | | |
192 | 190 | | |
193 | 191 | | |
194 | 192 | | |
195 | 193 | | |
196 | | - | |
197 | | - | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
198 | 204 | | |
199 | 205 | | |
200 | 206 | | |
| |||
301 | 307 | | |
302 | 308 | | |
303 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
304 | 325 | | |
| 326 | + | |
305 | 327 | | |
306 | 328 | | |
307 | 329 | | |
| |||
360 | 382 | | |
361 | 383 | | |
362 | 384 | | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
363 | 398 | | |
364 | 399 | | |
365 | 400 | | |
| |||
466 | 501 | | |
467 | 502 | | |
468 | 503 | | |
| 504 | + | |
469 | 505 | | |
470 | 506 | | |
471 | 507 | | |
| |||
477 | 513 | | |
478 | 514 | | |
479 | 515 | | |
| 516 | + | |
480 | 517 | | |
481 | 518 | | |
482 | 519 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
109 | 263 | | |
110 | 264 | | |
111 | 265 | | |
| |||
0 commit comments