54 lines
2.0 KiB
JavaScript
54 lines
2.0 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const { execSync } = require('child_process');
|
|
|
|
const target = process.argv[2]?.toLowerCase() || 'sqlite';
|
|
|
|
const rootDir = path.resolve(__dirname, '..');
|
|
const prismaDir = path.join(rootDir, 'prisma');
|
|
const envPath = path.join(rootDir, '.env');
|
|
|
|
if (target === 'sqlite') {
|
|
console.log('🔄 Mengalihkan database ke SQLite (Zero-dependency local database)...');
|
|
fs.copyFileSync(
|
|
path.join(prismaDir, 'schema.sqlite.prisma'),
|
|
path.join(prismaDir, 'schema.prisma')
|
|
);
|
|
|
|
let envContent = fs.readFileSync(envPath, 'utf8');
|
|
envContent = envContent.replace(
|
|
/DATABASE_URL=.*/,
|
|
'DATABASE_URL="file:./dev.db"'
|
|
);
|
|
fs.writeFileSync(envPath, envContent);
|
|
|
|
console.log('📦 Menghasilkan Prisma Client & migrasi SQLite dev.db...');
|
|
execSync('npx prisma generate', { stdio: 'inherit', cwd: rootDir });
|
|
execSync('npx prisma db push --skip-generate', { stdio: 'inherit', cwd: rootDir });
|
|
|
|
console.log('🌱 Melakukan seeding data awal...');
|
|
execSync('node prisma/seed.js', { stdio: 'inherit', cwd: rootDir });
|
|
|
|
console.log('✅ Berhasil beralih ke SQLite! Database lokal dev.db siap dipakai.');
|
|
} else if (target === 'postgres') {
|
|
console.log('🔄 Mengalihkan database ke PostgreSQL...');
|
|
fs.copyFileSync(
|
|
path.join(prismaDir, 'schema.postgres.prisma'),
|
|
path.join(prismaDir, 'schema.prisma')
|
|
);
|
|
|
|
let envContent = fs.readFileSync(envPath, 'utf8');
|
|
envContent = envContent.replace(
|
|
/DATABASE_URL=.*/,
|
|
'DATABASE_URL="postgresql://erp:erppassword@localhost:5432/erp_db?schema=public"'
|
|
);
|
|
fs.writeFileSync(envPath, envContent);
|
|
|
|
console.log('📦 Menghasilkan Prisma Client untuk PostgreSQL...');
|
|
execSync('npx prisma generate', { stdio: 'inherit', cwd: rootDir });
|
|
|
|
console.log('✅ Skema siap untuk PostgreSQL. Pastikan container PostgreSQL aktif (docker compose up -d db).');
|
|
} else {
|
|
console.log('Pilihan tidak dikenal. Gunakan: node scripts/switch-db.js [sqlite|postgres]');
|
|
}
|