Migration updates

This commit is contained in:
Arunavo Ray
2025-07-17 12:29:53 +05:30
parent 2140f75436
commit 39bfb1e2d1
2 changed files with 45 additions and 20 deletions

View File

@@ -31,6 +31,7 @@ COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./package.json COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/docker-entrypoint.sh ./docker-entrypoint.sh COPY --from=builder /app/docker-entrypoint.sh ./docker-entrypoint.sh
COPY --from=builder /app/scripts ./scripts COPY --from=builder /app/scripts ./scripts
COPY --from=builder /app/drizzle ./drizzle
ENV NODE_ENV=production ENV NODE_ENV=production
ENV HOST=0.0.0.0 ENV HOST=0.0.0.0

View File

@@ -1,8 +1,7 @@
#!/usr/bin/env bun #!/usr/bin/env bun
import { db } from "../src/lib/db"; import { db } from "../src/lib/db";
import { users, accounts } from "../src/lib/db/schema"; import { accounts } from "../src/lib/db/schema";
import { eq } from "drizzle-orm"; import { sql } from "drizzle-orm";
import bcrypt from "bcryptjs";
console.log("🔄 Starting Better Auth migration..."); console.log("🔄 Starting Better Auth migration...");
@@ -15,31 +14,51 @@ async function migrateToBetterAuth() {
return; return;
} }
// Get all users with password hashes // Check if we have old users table with passwords
const allUsers = await db.select().from(users); // 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) { if (!hasPasswordColumn || hasPasswordColumn.count === 0) {
console.log(" No users to migrate"); 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; return;
} }
console.log(`📊 Found ${allUsers.length} users to migrate`); console.log(`⚠️ Found ${usersWithoutAccounts.length} users without accounts - they may need to reset passwords`);
return;
// Migrate each user
for (const user of allUsers) {
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;
} }
// 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 allUsersWithPasswords) {
try {
// Create Better Auth account entry // Create Better Auth account entry
await db.insert(accounts).values({ await db.insert(accounts).values({
id: crypto.randomUUID(), id: crypto.randomUUID(),
userId: user.id, userId: user.id,
accountId: user.email, // Use email as account ID accountId: user.email, // Use email as account ID
providerId: "credential", // Better Auth credential provider providerId: "credential", // Better Auth credential provider
providerUserId: null,
accessToken: null, accessToken: null,
refreshToken: null, refreshToken: null,
expiresAt: null, expiresAt: null,
@@ -48,11 +67,6 @@ async function migrateToBetterAuth() {
updatedAt: new Date() 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}`); console.log(`✓ Migrated user: ${user.email}`);
} catch (error) { } catch (error) {
console.error(`❌ Failed to migrate user ${user.email}:`, 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"); console.log("✅ Better Auth migration completed successfully");
// Verify migration // Verify migration