NAS deployment fixed
This commit is contained in:
80
server/reset_prod_db.js
Normal file
80
server/reset_prod_db.js
Normal file
@@ -0,0 +1,80 @@
|
||||
const { execSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
const dotenv = require('dotenv');
|
||||
|
||||
// Load .env from root
|
||||
const envPath = path.resolve(process.cwd(), '../.env');
|
||||
if (fs.existsSync(envPath)) {
|
||||
dotenv.config({ path: envPath });
|
||||
} else {
|
||||
// Try current dir just in case, but preference is root
|
||||
dotenv.config({ path: path.resolve(process.cwd(), '.env') });
|
||||
}
|
||||
|
||||
async function resetDb() {
|
||||
const adminEmail = process.env.ADMIN_EMAIL_PROD || 'admin-prod@gymflow.ai';
|
||||
const adminPassword = process.env.ADMIN_PASSWORD_PROD || 'secure-prod-password-change-me';
|
||||
|
||||
// 1. Determine DB path (relative to server dir where app runs)
|
||||
const dbPath = path.resolve(process.cwd(), 'prod.db');
|
||||
const prismaSchemaPath = path.resolve(process.cwd(), 'prisma/schema.prisma');
|
||||
|
||||
console.log(`--- Database Reset ---`);
|
||||
console.log(`Admin Email: ${adminEmail}`);
|
||||
console.log(`DB Path: ${dbPath}`);
|
||||
|
||||
// 2. Delete existing DB
|
||||
if (fs.existsSync(dbPath)) {
|
||||
console.log(`Deleting existing database...`);
|
||||
fs.unlinkSync(dbPath);
|
||||
}
|
||||
|
||||
// 3. Initialize fresh DB schema using Prisma
|
||||
console.log(`Initializing schema via Prisma...`);
|
||||
try {
|
||||
// Set DATABASE_URL for prisma CLI (used by prisma.config.ts)
|
||||
const absoluteDbPath = `file:${dbPath}`;
|
||||
console.log(`Setting DATABASE_URL=${absoluteDbPath}`);
|
||||
|
||||
execSync(`npx prisma db push`, {
|
||||
stdio: 'inherit',
|
||||
env: {
|
||||
...process.env,
|
||||
DATABASE_URL: absoluteDbPath
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Failed to initialize schema:`, error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 4. Create the Admin user
|
||||
console.log(`Creating fresh admin user...`);
|
||||
|
||||
// In Prisma 7, we must use the adapter for better-sqlite3
|
||||
const { PrismaBetterSqlite3 } = require('@prisma/adapter-better-sqlite3');
|
||||
const adapter = new PrismaBetterSqlite3({ url: dbPath });
|
||||
const prisma = new PrismaClient({ adapter });
|
||||
|
||||
try {
|
||||
const hashedPassword = await bcrypt.hash(adminPassword, 10);
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
email: adminEmail,
|
||||
password: hashedPassword,
|
||||
role: 'ADMIN',
|
||||
profile: { create: { weight: 70 } }
|
||||
}
|
||||
});
|
||||
console.log(`✅ Success! Admin user created.`);
|
||||
} catch (error) {
|
||||
console.error(`Failed to create admin:`, error.message);
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
resetDb();
|
||||
Reference in New Issue
Block a user