jedmund-svelte/scripts/init-db.ts
Justin Edmund c6ce13a530 Simplify posts
We had a lot of unnecessary complexity here due to post types that never ended up getting used.

We also made the post slug field reactive and bound to the title field.

We also fixed filters on the Universe admin page so we can filter by unpublished posts too (WIP)

We also fixed the hover state of BackButton
2025-06-11 00:53:54 -07:00

57 lines
1.6 KiB
TypeScript

import { PrismaClient } from '@prisma/client'
import { execSync } from 'child_process'
const prisma = new PrismaClient()
async function isDatabaseInitialized(): Promise<boolean> {
try {
// Check if we have any completed migrations
const migrationCount = await prisma.$queryRaw<[{ count: bigint }]>`
SELECT COUNT(*) as count
FROM _prisma_migrations
WHERE finished_at IS NOT NULL
`
return migrationCount[0].count > 0n
} catch (error: any) {
// If the table doesn't exist, database is not initialized
console.log('📊 Migration table check failed (expected on first deploy):', error.message)
return false
}
}
async function initializeDatabase() {
console.log('🔍 Checking database initialization status...')
// Give the database a moment to be ready
await new Promise((resolve) => setTimeout(resolve, 2000))
try {
const isInitialized = await isDatabaseInitialized()
if (!isInitialized) {
console.log('📦 First time setup detected. Initializing database...')
// Run migrations
console.log('🔄 Running database migrations...')
execSync('npx prisma migrate deploy', { stdio: 'inherit' })
// Run seeds
console.log('🌱 Seeding database...')
execSync('npx prisma db seed', { stdio: 'inherit' })
console.log('✅ Database initialization complete!')
} else {
console.log('✅ Database already initialized. Running migrations only...')
execSync('npx prisma migrate deploy', { stdio: 'inherit' })
}
} catch (error) {
console.error('❌ Database initialization failed:', error)
process.exit(1)
} finally {
await prisma.$disconnect()
}
}
// Run the initialization
initializeDatabase()