refactor: 改用 Nitro 插件实现启动时数据库迁移
This commit is contained in:
@@ -11,4 +11,3 @@ out/
|
||||
*.bun-build
|
||||
|
||||
.vscode/
|
||||
drizzle/
|
||||
|
||||
@@ -125,8 +125,8 @@ const handler = new RPCHandler(router, {
|
||||
|
||||
- **Dockerfile**: Multi-stage — `oven/bun:1` (build + compile) → `gcr.io/distroless/cc-debian13:nonroot` (runtime)
|
||||
- **Runtime**: Bun-compiled standalone binary (glibc-linked, requires `cc` distroless variant)
|
||||
- **Compose**: `app` (port 3000) + `postgres:17-alpine` (port 5432) with health check
|
||||
- **Migrations**: Run separately — `bun run db:push` (dev) or `bun run db:migrate` (prod) against the compose DB
|
||||
- **Compose**: `app` + `db` with health check dependency
|
||||
- **Migrations**: Nitro plugin runs `migrate()` at server startup (see `src/server/plugins/migrate.ts`)
|
||||
- **Env**: `DATABASE_URL` set in `compose.yaml` pointing to the `db` service
|
||||
|
||||
## Git Workflow
|
||||
|
||||
+4
-2
@@ -12,9 +12,11 @@ RUN bun run build \
|
||||
|
||||
FROM gcr.io/distroless/cc-debian13:nonroot
|
||||
|
||||
COPY --from=build --chown=nonroot:nonroot /app/out/server /app/server
|
||||
WORKDIR /app
|
||||
COPY --from=build --chown=nonroot:nonroot /app/out/server ./server
|
||||
COPY --from=build --chown=nonroot:nonroot /app/drizzle ./drizzle
|
||||
|
||||
ENV HOST=0.0.0.0
|
||||
EXPOSE 3000
|
||||
|
||||
CMD ["/app/server"]
|
||||
CMD ["./server"]
|
||||
|
||||
+10
-8
@@ -1,24 +1,26 @@
|
||||
services:
|
||||
app:
|
||||
build: .
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
ports:
|
||||
- '3000:3000'
|
||||
environment:
|
||||
- DATABASE_URL=postgres://postgres:postgres@db:5432/postgres
|
||||
DATABASE_URL: postgres://postgres:postgres@db:5432/postgres
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
|
||||
db:
|
||||
image: postgres:18
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
image: postgres:17-alpine
|
||||
# ports:
|
||||
# - '5432:5432'
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: postgres
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: [ 'CMD', 'pg_isready', '-U', 'postgres' ]
|
||||
test: ['CMD', 'pg_isready', '-U', 'postgres']
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { drizzle } from 'drizzle-orm/postgres-js'
|
||||
import { migrate } from 'drizzle-orm/postgres-js/migrator'
|
||||
import { env } from '@/env'
|
||||
|
||||
export default async () => {
|
||||
if (import.meta.dev) return
|
||||
|
||||
const db = drizzle({ connection: { url: env.DATABASE_URL, max: 1 } })
|
||||
|
||||
try {
|
||||
console.log('Applying migrations...')
|
||||
await migrate(db, { migrationsFolder: './drizzle' })
|
||||
console.log('Migrations applied successfully.')
|
||||
} finally {
|
||||
await db.$client.end()
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ export default defineConfig({
|
||||
nitro({
|
||||
preset: 'bun',
|
||||
serveStatic: 'inline',
|
||||
plugins: ['./src/server/plugins/migrate.ts'],
|
||||
}),
|
||||
],
|
||||
resolve: {
|
||||
|
||||
Reference in New Issue
Block a user