22import "dotenv-flow/config" ;
33
44import { qstash } from "@/lib/cron" ;
5+ import { prisma } from "@/lib/prisma" ;
56import { APP_DOMAIN_WITH_NGROK } from "@dub/utils" ;
67
78async function main ( ) {
8- await migrateUsers ( ) ;
9+ // await migrateUsers();
10+ // await migrateAccounts();
11+ // await migrateCredentials();
12+ await verifyMigration ( ) ;
913}
1014
1115async function migrateUsers ( ) {
1216 const qstashResponse = await qstash . publishJSON ( {
1317 method : "POST" ,
1418 url : `${ APP_DOMAIN_WITH_NGROK } /api/cron/better-auth/migrate-users` ,
15- delay : "10s" ,
1619 retries : 0 ,
1720 flowControl : {
1821 key : "better-auth-migrate-users" ,
@@ -23,4 +26,84 @@ async function migrateUsers() {
2326 console . log ( `migrateUsers executed: ${ qstashResponse . messageId } ` ) ;
2427}
2528
29+ async function migrateAccounts ( ) {
30+ const qstashResponse = await qstash . publishJSON ( {
31+ method : "POST" ,
32+ url : `${ APP_DOMAIN_WITH_NGROK } /api/cron/better-auth/migrate-accounts` ,
33+ retries : 0 ,
34+ flowControl : {
35+ key : "better-auth-migrate-accounts" ,
36+ parallelism : 1 ,
37+ } ,
38+ } ) ;
39+
40+ console . log ( `migrateAccounts executed: ${ qstashResponse . messageId } ` ) ;
41+ }
42+
43+ async function migrateCredentials ( ) {
44+ const qstashResponse = await qstash . publishJSON ( {
45+ method : "POST" ,
46+ url : `${ APP_DOMAIN_WITH_NGROK } /api/cron/better-auth/migrate-credentials` ,
47+ retries : 0 ,
48+ flowControl : {
49+ key : "better-auth-migrate-credentials" ,
50+ parallelism : 1 ,
51+ } ,
52+ } ) ;
53+
54+ console . log ( `migrateCredentials executed: ${ qstashResponse . messageId } ` ) ;
55+ }
56+
57+ async function verifyMigration ( ) {
58+ const [ users , accounts , credentials ] = await Promise . all ( [
59+ prisma . user . count ( {
60+ where : {
61+ emailVerified : {
62+ not : null ,
63+ } ,
64+ emailVerifiedBa : false ,
65+ } ,
66+ } ) ,
67+
68+ prisma . account . count ( {
69+ where : {
70+ accountId : null ,
71+ providerAccountId : {
72+ not : null ,
73+ } ,
74+ } ,
75+ } ) ,
76+
77+ prisma . user . count ( {
78+ where : {
79+ passwordHash : {
80+ not : null ,
81+ } ,
82+ accounts : {
83+ none : {
84+ providerId : "credential" ,
85+ } ,
86+ } ,
87+ } ,
88+ } ) ,
89+ ] ) ;
90+
91+ const ok = users === 0 && accounts === 0 && credentials === 0 ;
92+
93+ console . log (
94+ ok
95+ ? "Better Auth migration complete."
96+ : "Better Auth migration incomplete." ,
97+ {
98+ users,
99+ accounts,
100+ credentials,
101+ } ,
102+ ) ;
103+
104+ if ( ! ok ) {
105+ process . exit ( 1 ) ;
106+ }
107+ }
108+
26109main ( ) ;
0 commit comments