1
1
#!/usr/bin/env bun
2
2
import { db } from "../src/lib/db" ;
3
- import { users , accounts } from "../src/lib/db/schema" ;
4
- import { eq } from "drizzle-orm" ;
5
- import bcrypt from "bcryptjs" ;
3
+ import { accounts } from "../src/lib/db/schema" ;
4
+ import { sql } from "drizzle-orm" ;
6
5
7
6
console . log ( "🔄 Starting Better Auth migration..." ) ;
8
7
@@ -15,31 +14,51 @@ async function migrateToBetterAuth() {
15
14
return ;
16
15
}
17
16
18
- // Get all users with password hashes
19
- const allUsers = await db . select ( ) . from ( users ) ;
17
+ // Check if we have old users table with passwords
18
+ // This query checks if password column exists in users table
19
+ const hasPasswordColumn = await db . get < { count : number } > (
20
+ sql `SELECT COUNT(*) as count FROM pragma_table_info('users') WHERE name = 'password'`
21
+ ) ;
20
22
21
- if ( allUsers . length === 0 ) {
22
- console . log ( "ℹ️ No users to migrate" ) ;
23
+ if ( ! hasPasswordColumn || hasPasswordColumn . count === 0 ) {
24
+ console . log ( "ℹ️ Users table doesn't have password column - migration may have already been done" ) ;
25
+
26
+ // Check if we have any users without accounts
27
+ const usersWithoutAccounts = await db . all < { id : string ; email : string } > (
28
+ sql `SELECT u.id, u.email FROM users u LEFT JOIN accounts a ON u.id = a.user_id WHERE a.id IS NULL`
29
+ ) ;
30
+
31
+ if ( usersWithoutAccounts . length === 0 ) {
32
+ console . log ( "✓ All users have accounts - migration complete" ) ;
33
+ return ;
34
+ }
35
+
36
+ console . log ( `⚠️ Found ${ usersWithoutAccounts . length } users without accounts - they may need to reset passwords` ) ;
37
+ return ;
38
+ }
39
+
40
+ // Get all users with password hashes using raw SQL since the schema doesn't have password
41
+ const allUsersWithPasswords = await db . all < { id : string ; email : string ; username : string ; password : string } > (
42
+ sql `SELECT id, email, username, password FROM users WHERE password IS NOT NULL`
43
+ ) ;
44
+
45
+ if ( allUsersWithPasswords . length === 0 ) {
46
+ console . log ( "ℹ️ No users with passwords to migrate" ) ;
23
47
return ;
24
48
}
25
49
26
- console . log ( `📊 Found ${ allUsers . length } users to migrate` ) ;
50
+ console . log ( `📊 Found ${ allUsersWithPasswords . length } users to migrate` ) ;
27
51
28
52
// Migrate each user
29
- for ( const user of allUsers ) {
53
+ for ( const user of allUsersWithPasswords ) {
30
54
try {
31
- // Skip users without passwords (shouldn't happen but be safe)
32
- if ( ! user . password ) {
33
- console . log ( `⚠️ Skipping user ${ user . email } - no password hash found` ) ;
34
- continue ;
35
- }
36
-
37
55
// Create Better Auth account entry
38
56
await db . insert ( accounts ) . values ( {
39
57
id : crypto . randomUUID ( ) ,
40
58
userId : user . id ,
41
59
accountId : user . email , // Use email as account ID
42
60
providerId : "credential" , // Better Auth credential provider
61
+ providerUserId : null ,
43
62
accessToken : null ,
44
63
refreshToken : null ,
45
64
expiresAt : null ,
@@ -48,18 +67,23 @@ async function migrateToBetterAuth() {
48
67
updatedAt : new Date ( )
49
68
} ) ;
50
69
51
- // Remove password from users table (Better Auth manages it now)
52
- await db . update ( users )
53
- . set ( { password : null } )
54
- . where ( eq ( users . id , user . id ) ) ;
55
-
56
70
console . log ( `✓ Migrated user: ${ user . email } ` ) ;
57
71
} catch ( error ) {
58
72
console . error ( `❌ Failed to migrate user ${ user . email } :` , error ) ;
59
73
// Continue with other users even if one fails
60
74
}
61
75
}
62
76
77
+ // Remove password column from users table if it exists
78
+ console . log ( "🔄 Cleaning up old password column..." ) ;
79
+ try {
80
+ // SQLite doesn't support DROP COLUMN directly, so we need to recreate the table
81
+ // For now, we'll just leave it as is since it's not harmful
82
+ console . log ( "ℹ️ Password column left in users table for compatibility" ) ;
83
+ } catch ( error ) {
84
+ console . error ( "⚠️ Could not remove password column:" , error ) ;
85
+ }
86
+
63
87
console . log ( "✅ Better Auth migration completed successfully" ) ;
64
88
65
89
// Verify migration
0 commit comments