39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import { PrismaPg } from '@prisma/adapter-pg';
|
|
import pg from 'pg';
|
|
import { config } from 'dotenv';
|
|
|
|
config();
|
|
config({ path: '.env.local', override: true });
|
|
|
|
const connectionString = process.env.DATABASE_URL!;
|
|
const pool = new pg.Pool({ connectionString });
|
|
const adapter = new PrismaPg(pool);
|
|
const prisma = new PrismaClient({ adapter });
|
|
|
|
async function migrate() {
|
|
console.log('Starting migration...');
|
|
const categories = await prisma.businessCategory.findMany();
|
|
const categoryMap = new Map(categories.map(c => [c.name, c.id]));
|
|
|
|
const businesses = await prisma.business.findMany();
|
|
let updatedCount = 0;
|
|
|
|
for (const biz of businesses) {
|
|
const catId = categoryMap.get(biz.category);
|
|
if (catId) {
|
|
await prisma.business.update({
|
|
where: { id: biz.id },
|
|
data: { categoryId: catId }
|
|
});
|
|
updatedCount++;
|
|
}
|
|
}
|
|
|
|
console.log(`Migration finished. Updated ${updatedCount} businesses.`);
|
|
}
|
|
|
|
migrate()
|
|
.catch(console.error)
|
|
.finally(() => prisma.$disconnect());
|