backend/build/docker-compose-migrate.yml

30 lines
1.0 KiB
YAML
Raw Normal View History

2025-11-14 14:40:56 +00:00
services:
migrate:
image: mongo:7.0
volumes:
- ../generate/database/mongo:/migrations:ro
working_dir: /app
environment:
- MONGO_HOST=${MONGO_HOST:-127.0.0.1:27017}
- MONGO_DB=${MONGO_DB:-digimon}
- MONGO_USER=${MONGO_USER:-root}
- MONGO_PASSWORD=${MONGO_PASSWORD:-example}
- MONGO_AUTH_DB=${MONGO_AUTH_DB:-admin}
network_mode: host
# 預設執行 up migration
command: >
sh -c "
if [ -z \"$$MONGO_USER\" ] || [ \"$$MONGO_USER\" = \"\" ]; then
MONGO_URI=\"mongodb://$$MONGO_HOST/$$MONGO_DB\"
else
MONGO_URI=\"mongodb://$$MONGO_USER:$$MONGO_PASSWORD@$$MONGO_HOST/$$MONGO_DB?authSource=$$MONGO_AUTH_DB\"
fi &&
echo \"執行 MongoDB migration (UP)...\" &&
echo \"連接: $$MONGO_URI\" &&
for file in $$(ls -1 /migrations/*.up.txt 2>/dev/null | sort); do
echo \"執行: $$(basename $$file)\" &&
mongosh \"$$MONGO_URI\" --file \"$$file\" || exit 1
done &&
echo \"✅ Migration UP 完成\"
"