fix(mpc-service): 重构数据库迁移机制,从根本解决表不存在问题
问题分析: - 旧迁移文件只有 party_shares 表,缺少 session_states 和 share_backups 表 - Prisma 的 _prisma_migrations 表记录迁移已完成,导致新表无法创建 - 迁移状态与实际数据库不一致 解决方案: 1. 删除旧迁移目录,创建全新的 0001_init 迁移 2. 新迁移包含所有三个表: party_shares, session_states, share_backups 3. 添加 docker-entrypoint.sh 启动脚本,容器启动时自动运行迁移 4. 修改 Dockerfile 使用 entrypoint 脚本 deploy.sh 新增命令: - migrate-reset: 重置数据库并重新运行迁移 - migrate-push: 强制同步 schema (创建缺失的表) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
122bf84c24
commit
8850ea6ab0
|
|
@ -351,6 +351,48 @@ migrate() {
|
||||||
log_info "Migrations complete"
|
log_info "Migrations complete"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
migrate_reset() {
|
||||||
|
local service="$1"
|
||||||
|
|
||||||
|
if [ -z "$service" ]; then
|
||||||
|
log_error "Please specify a service name: ./deploy.sh migrate-reset <service-name>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
log_warn "This will reset the database for $service and re-run migrations!"
|
||||||
|
read -p "Are you sure? (y/N): " confirm
|
||||||
|
|
||||||
|
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
|
||||||
|
log_info "Migration reset cancelled"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
log_step "Resetting database and migrations for $service..."
|
||||||
|
|
||||||
|
# Use prisma db push to force sync schema without migration history
|
||||||
|
docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" exec "$service" \
|
||||||
|
npx prisma db push --force-reset --accept-data-loss
|
||||||
|
|
||||||
|
log_info "Database reset complete for $service"
|
||||||
|
}
|
||||||
|
|
||||||
|
migrate_push() {
|
||||||
|
local service="$1"
|
||||||
|
|
||||||
|
if [ -z "$service" ]; then
|
||||||
|
log_error "Please specify a service name: ./deploy.sh migrate-push <service-name>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
log_step "Force syncing schema for $service (using prisma db push)..."
|
||||||
|
|
||||||
|
# Use prisma db push to sync schema (creates missing tables)
|
||||||
|
docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" exec "$service" \
|
||||||
|
npx prisma db push --accept-data-loss
|
||||||
|
|
||||||
|
log_info "Schema sync complete for $service"
|
||||||
|
}
|
||||||
|
|
||||||
# ===========================================================================
|
# ===========================================================================
|
||||||
# Cleanup
|
# Cleanup
|
||||||
# ===========================================================================
|
# ===========================================================================
|
||||||
|
|
@ -441,6 +483,12 @@ case "${1:-}" in
|
||||||
migrate)
|
migrate)
|
||||||
migrate
|
migrate
|
||||||
;;
|
;;
|
||||||
|
migrate-reset)
|
||||||
|
migrate_reset "$2"
|
||||||
|
;;
|
||||||
|
migrate-push)
|
||||||
|
migrate_push "$2"
|
||||||
|
;;
|
||||||
clean)
|
clean)
|
||||||
clean
|
clean
|
||||||
;;
|
;;
|
||||||
|
|
@ -468,7 +516,9 @@ case "${1:-}" in
|
||||||
echo " status/ps - Show service status"
|
echo " status/ps - Show service status"
|
||||||
echo " health - Check health of all services"
|
echo " health - Check health of all services"
|
||||||
echo " logs [svc] - View logs (optionally for specific service)"
|
echo " logs [svc] - View logs (optionally for specific service)"
|
||||||
echo " migrate - Run database migrations"
|
echo " migrate - Run database migrations for all services"
|
||||||
|
echo " migrate-reset <svc> - Reset database and re-run migrations for a service"
|
||||||
|
echo " migrate-push <svc> - Force sync schema (creates missing tables)"
|
||||||
echo " clean - Remove all containers, volumes, and images"
|
echo " clean - Remove all containers, volumes, and images"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Single Service Commands:"
|
echo "Single Service Commands:"
|
||||||
|
|
|
||||||
|
|
@ -43,13 +43,17 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm ci --only=production
|
RUN npm ci --only=production
|
||||||
|
|
||||||
# Copy Prisma schema and generate client
|
# Copy Prisma schema, migrations and generate client
|
||||||
COPY prisma ./prisma/
|
COPY prisma ./prisma/
|
||||||
RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
|
|
||||||
# Copy built files
|
# Copy built files
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Copy entrypoint script
|
||||||
|
COPY docker-entrypoint.sh ./
|
||||||
|
RUN chmod +x docker-entrypoint.sh
|
||||||
|
|
||||||
# Create non-root user
|
# Create non-root user
|
||||||
RUN groupadd -g 1001 nodejs && \
|
RUN groupadd -g 1001 nodejs && \
|
||||||
useradd -u 1001 -g nodejs nestjs
|
useradd -u 1001 -g nodejs nestjs
|
||||||
|
|
@ -57,15 +61,18 @@ RUN groupadd -g 1001 nodejs && \
|
||||||
# Create temp directory for TSS
|
# Create temp directory for TSS
|
||||||
RUN mkdir -p /tmp/tss && chown -R nestjs:nodejs /tmp/tss
|
RUN mkdir -p /tmp/tss && chown -R nestjs:nodejs /tmp/tss
|
||||||
|
|
||||||
|
# Change ownership of app directory
|
||||||
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
# Expose port
|
# Expose port
|
||||||
EXPOSE 3006
|
EXPOSE 3006
|
||||||
|
|
||||||
# Health check
|
# Health check (extended start-period for migrations)
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=5 \
|
||||||
CMD curl -f http://localhost:3006/api/v1/health || exit 1
|
CMD curl -f http://localhost:3006/api/v1/health || exit 1
|
||||||
|
|
||||||
# Start service
|
# Start service with entrypoint script (runs migrations first)
|
||||||
CMD ["node", "dist/main.js"]
|
CMD ["./docker-entrypoint.sh"]
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "=== MPC Service Starting ==="
|
||||||
|
echo "Running database migrations..."
|
||||||
|
|
||||||
|
# Run Prisma migrations
|
||||||
|
npx prisma migrate deploy
|
||||||
|
|
||||||
|
echo "Migrations completed successfully"
|
||||||
|
echo "Starting application..."
|
||||||
|
|
||||||
|
# Start the application
|
||||||
|
exec node dist/main.js
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
# Prisma Migrate lockfile v1
|
# Please do not edit this file manually
|
||||||
|
# It should be added in your version-control system (i.e. Git)
|
||||||
provider = "postgresql"
|
provider = "postgresql"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue