@authhero/drizzle 1.4.2 → 1.5.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.
@@ -0,0 +1,16 @@
1
+ -- Reparent refresh tokens onto sessions (stage 2).
2
+ --
3
+ -- `session_id` is Auth0's field of the same name: the authenticated session a
4
+ -- token was issued under. Deliberately NOT a foreign key — a refresh token is
5
+ -- expected to outlive its session, so cleanup removes the session row first
6
+ -- and this pointer is left to dangle rather than cascading.
7
+ --
8
+ -- The rest are auth-event facts denormalised from the login session at mint
9
+ -- time, so the refresh grant stops resolving them through a short-lived row
10
+ -- that may already have been cleaned up.
11
+ ALTER TABLE `refresh_tokens` ADD `session_id` text(26);--> statement-breakpoint
12
+ ALTER TABLE `refresh_tokens` ADD `organization` text(191);--> statement-breakpoint
13
+ ALTER TABLE `refresh_tokens` ADD `auth_connection` text(255);--> statement-breakpoint
14
+ ALTER TABLE `refresh_tokens` ADD `auth_strategy_strategy` text(64);--> statement-breakpoint
15
+ ALTER TABLE `refresh_tokens` ADD `auth_strategy_strategy_type` text(64);--> statement-breakpoint
16
+ CREATE INDEX `idx_refresh_tokens_session_id` ON `refresh_tokens` (`tenant_id`,`session_id`);
@@ -0,0 +1,52 @@
1
+ -- Backfill the auth-event columns added by 0006, for refresh tokens minted
2
+ -- before that release started populating them at write time.
3
+ --
4
+ -- Values come from the token's parent `login_sessions` row. Tokens whose
5
+ -- parent has already been cleaned up are left null, which is correct: they are
6
+ -- indistinguishable from a token that never had a session, and Auth0
7
+ -- represents the same state with a null `session_id`.
8
+ --
9
+ -- IMPORTANT: all four facts are written together, or none are. The refresh
10
+ -- grant uses `session_id` as the marker for "this row carries its own facts"
11
+ -- and skips the login-session read when it is set — so setting `session_id`
12
+ -- alone would make the grant stop looking up the organization and connection
13
+ -- it still needs. The `ls.session_id IS NOT NULL` predicate keeps the marker
14
+ -- from ever running ahead of the data.
15
+ --
16
+ -- Single statement rather than batched: these are per-tenant D1 databases, so
17
+ -- the row counts are small. The PlanetScale equivalent batches, in
18
+ -- packages/kysely/migrate/migrations/2026-08-21T12:00:00_*_backfill.ts.
19
+ UPDATE `refresh_tokens`
20
+ SET
21
+ `session_id` = (
22
+ SELECT `ls`.`session_id` FROM `login_sessions` `ls`
23
+ WHERE `ls`.`id` = `refresh_tokens`.`login_id`
24
+ AND `ls`.`tenant_id` = `refresh_tokens`.`tenant_id`
25
+ ),
26
+ `organization` = (
27
+ SELECT json_extract(`ls`.`auth_params`, '$.organization') FROM `login_sessions` `ls`
28
+ WHERE `ls`.`id` = `refresh_tokens`.`login_id`
29
+ AND `ls`.`tenant_id` = `refresh_tokens`.`tenant_id`
30
+ ),
31
+ `auth_connection` = (
32
+ SELECT `ls`.`auth_connection` FROM `login_sessions` `ls`
33
+ WHERE `ls`.`id` = `refresh_tokens`.`login_id`
34
+ AND `ls`.`tenant_id` = `refresh_tokens`.`tenant_id`
35
+ ),
36
+ `auth_strategy_strategy` = (
37
+ SELECT `ls`.`auth_strategy_strategy` FROM `login_sessions` `ls`
38
+ WHERE `ls`.`id` = `refresh_tokens`.`login_id`
39
+ AND `ls`.`tenant_id` = `refresh_tokens`.`tenant_id`
40
+ ),
41
+ `auth_strategy_strategy_type` = (
42
+ SELECT `ls`.`auth_strategy_strategy_type` FROM `login_sessions` `ls`
43
+ WHERE `ls`.`id` = `refresh_tokens`.`login_id`
44
+ AND `ls`.`tenant_id` = `refresh_tokens`.`tenant_id`
45
+ )
46
+ WHERE `refresh_tokens`.`session_id` IS NULL
47
+ AND EXISTS (
48
+ SELECT 1 FROM `login_sessions` `ls`
49
+ WHERE `ls`.`id` = `refresh_tokens`.`login_id`
50
+ AND `ls`.`tenant_id` = `refresh_tokens`.`tenant_id`
51
+ AND `ls`.`session_id` IS NOT NULL
52
+ );