mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2025-12-06 19:46:44 +03:00
Migration updates
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 ${allUsers.length} users to migrate`);
|
||||
|
||||
// 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;
|
||||
console.log(`⚠️ Found ${usersWithoutAccounts.length} users without accounts - they may need to reset passwords`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 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
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user