Skip to content

Commit 39bfb1e

Browse files
committed
Migration updates
1 parent 2140f75 commit 39bfb1e

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ COPY --from=builder /app/dist ./dist
3131
COPY --from=builder /app/package.json ./package.json
3232
COPY --from=builder /app/docker-entrypoint.sh ./docker-entrypoint.sh
3333
COPY --from=builder /app/scripts ./scripts
34+
COPY --from=builder /app/drizzle ./drizzle
3435

3536
ENV NODE_ENV=production
3637
ENV HOST=0.0.0.0

scripts/migrate-better-auth.ts

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env bun
22
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";
65

76
console.log("🔄 Starting Better Auth migration...");
87

@@ -15,31 +14,51 @@ async function migrateToBetterAuth() {
1514
return;
1615
}
1716

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+
);
2022

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");
2347
return;
2448
}
2549

26-
console.log(`📊 Found ${allUsers.length} users to migrate`);
50+
console.log(`📊 Found ${allUsersWithPasswords.length} users to migrate`);
2751

2852
// Migrate each user
29-
for (const user of allUsers) {
53+
for (const user of allUsersWithPasswords) {
3054
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-
3755
// Create Better Auth account entry
3856
await db.insert(accounts).values({
3957
id: crypto.randomUUID(),
4058
userId: user.id,
4159
accountId: user.email, // Use email as account ID
4260
providerId: "credential", // Better Auth credential provider
61+
providerUserId: null,
4362
accessToken: null,
4463
refreshToken: null,
4564
expiresAt: null,
@@ -48,18 +67,23 @@ async function migrateToBetterAuth() {
4867
updatedAt: new Date()
4968
});
5069

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-
5670
console.log(`✓ Migrated user: ${user.email}`);
5771
} catch (error) {
5872
console.error(`❌ Failed to migrate user ${user.email}:`, error);
5973
// Continue with other users even if one fails
6074
}
6175
}
6276

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+
6387
console.log("✅ Better Auth migration completed successfully");
6488

6589
// Verify migration

0 commit comments

Comments
 (0)