refactor: 改用 Nitro 插件实现启动时数据库迁移
This commit is contained in:
@@ -11,4 +11,3 @@ out/
|
|||||||
*.bun-build
|
*.bun-build
|
||||||
|
|
||||||
.vscode/
|
.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)
|
- **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)
|
- **Runtime**: Bun-compiled standalone binary (glibc-linked, requires `cc` distroless variant)
|
||||||
- **Compose**: `app` (port 3000) + `postgres:17-alpine` (port 5432) with health check
|
- **Compose**: `app` + `db` with health check dependency
|
||||||
- **Migrations**: Run separately — `bun run db:push` (dev) or `bun run db:migrate` (prod) against the compose DB
|
- **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
|
- **Env**: `DATABASE_URL` set in `compose.yaml` pointing to the `db` service
|
||||||
|
|
||||||
## Git Workflow
|
## Git Workflow
|
||||||
|
|||||||
+4
-2
@@ -12,9 +12,11 @@ RUN bun run build \
|
|||||||
|
|
||||||
FROM gcr.io/distroless/cc-debian13:nonroot
|
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
|
ENV HOST=0.0.0.0
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
CMD ["/app/server"]
|
CMD ["./server"]
|
||||||
|
|||||||
+9
-7
@@ -1,22 +1,24 @@
|
|||||||
services:
|
services:
|
||||||
app:
|
app:
|
||||||
build: .
|
build: .
|
||||||
depends_on:
|
|
||||||
db:
|
|
||||||
condition: service_healthy
|
|
||||||
ports:
|
ports:
|
||||||
- '3000:3000'
|
- '3000:3000'
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_URL=postgres://postgres:postgres@db:5432/postgres
|
DATABASE_URL: postgres://postgres:postgres@db:5432/postgres
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: postgres:18
|
image: postgres:17-alpine
|
||||||
volumes:
|
# ports:
|
||||||
- pgdata:/var/lib/postgresql/data
|
# - '5432:5432'
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: postgres
|
POSTGRES_USER: postgres
|
||||||
POSTGRES_PASSWORD: postgres
|
POSTGRES_PASSWORD: postgres
|
||||||
POSTGRES_DB: postgres
|
POSTGRES_DB: postgres
|
||||||
|
volumes:
|
||||||
|
- pgdata:/var/lib/postgresql/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ['CMD', 'pg_isready', '-U', 'postgres']
|
test: ['CMD', 'pg_isready', '-U', 'postgres']
|
||||||
interval: 5s
|
interval: 5s
|
||||||
|
|||||||
@@ -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({
|
nitro({
|
||||||
preset: 'bun',
|
preset: 'bun',
|
||||||
serveStatic: 'inline',
|
serveStatic: 'inline',
|
||||||
|
plugins: ['./src/server/plugins/migrate.ts'],
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
resolve: {
|
resolve: {
|
||||||
|
|||||||
Reference in New Issue
Block a user