From 39bfb1e2d1ee2e678331edfe1996c522aabde989 Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Thu, 17 Jul 2025 12:29:53 +0530 Subject: [PATCH] Migration updates --- Dockerfile | 1 + scripts/migrate-better-auth.ts | 64 +++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index d3cefe2..c661c2b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,6 +31,7 @@ COPY --from=builder /app/dist ./dist COPY --from=builder /app/package.json ./package.json COPY --from=builder /app/docker-entrypoint.sh ./docker-entrypoint.sh COPY --from=builder /app/scripts ./scripts +COPY --from=builder /app/drizzle ./drizzle ENV NODE_ENV=production ENV HOST=0.0.0.0 diff --git a/scripts/migrate-better-auth.ts b/scripts/migrate-better-auth.ts index bd15a58..52c99ac 100644 --- a/scripts/migrate-better-auth.ts +++ b/scripts/migrate-better-auth.ts @@ -1,8 +1,7 @@ #!/usr/bin/env bun import { db } from "../src/lib/db"; -import { users, accounts } from "../src/lib/db/schema"; -import { eq } from "drizzle-orm"; -import bcrypt from "bcryptjs"; +import { accounts } from "../src/lib/db/schema"; +import { sql } from "drizzle-orm"; console.log("🔄 Starting Better Auth migration..."); @@ -15,31 +14,51 @@ async function migrateToBetterAuth() { return; } - // Get all users with password hashes - const allUsers = await db.select().from(users); + // Check if we have old users table with passwords + // This query checks if password column exists in users table + const hasPasswordColumn = await db.get<{ count: number }>( + sql`SELECT COUNT(*) as count FROM pragma_table_info('users') WHERE name = 'password'` + ); - if (allUsers.length === 0) { - console.log("â„šī¸ No users to migrate"); + if (!hasPasswordColumn || hasPasswordColumn.count === 0) { + console.log("â„šī¸ Users table doesn't have password column - migration may have already been done"); + + // Check if we have any users without accounts + const usersWithoutAccounts = await db.all<{ id: string; email: string }>( + sql`SELECT u.id, u.email FROM users u LEFT JOIN accounts a ON u.id = a.user_id WHERE a.id IS NULL` + ); + + if (usersWithoutAccounts.length === 0) { + console.log("✓ All users have accounts - migration complete"); + return; + } + + console.log(`âš ī¸ Found ${usersWithoutAccounts.length} users without accounts - they may need to reset passwords`); return; } - console.log(`📊 Found ${allUsers.length} users to migrate`); + // Get all users with password hashes using raw SQL since the schema doesn't have password + const allUsersWithPasswords = await db.all<{ id: string; email: string; username: string; password: string }>( + sql`SELECT id, email, username, password FROM users WHERE password IS NOT NULL` + ); + + if (allUsersWithPasswords.length === 0) { + console.log("â„šī¸ No users with passwords to migrate"); + return; + } + + console.log(`📊 Found ${allUsersWithPasswords.length} users to migrate`); // Migrate each user - for (const user of allUsers) { + for (const user of allUsersWithPasswords) { try { - // Skip users without passwords (shouldn't happen but be safe) - if (!user.password) { - console.log(`âš ī¸ Skipping user ${user.email} - no password hash found`); - continue; - } - // Create Better Auth account entry await db.insert(accounts).values({ id: crypto.randomUUID(), userId: user.id, accountId: user.email, // Use email as account ID providerId: "credential", // Better Auth credential provider + providerUserId: null, accessToken: null, refreshToken: null, expiresAt: null, @@ -48,11 +67,6 @@ async function migrateToBetterAuth() { updatedAt: new Date() }); - // Remove password from users table (Better Auth manages it now) - await db.update(users) - .set({ password: null }) - .where(eq(users.id, user.id)); - console.log(`✓ Migrated user: ${user.email}`); } catch (error) { console.error(`❌ Failed to migrate user ${user.email}:`, error); @@ -60,6 +74,16 @@ async function migrateToBetterAuth() { } } + // Remove password column from users table if it exists + console.log("🔄 Cleaning up old password column..."); + try { + // SQLite doesn't support DROP COLUMN directly, so we need to recreate the table + // For now, we'll just leave it as is since it's not harmful + console.log("â„šī¸ Password column left in users table for compatibility"); + } catch (error) { + console.error("âš ī¸ Could not remove password column:", error); + } + console.log("✅ Better Auth migration completed successfully"); // Verify migration