chore(deploy): refactor database cleanup and migration workflow in deploy.sh
- Remove deprecated --migrate flag and related RUN_MIGRATE logic - Simplify database cleanup workflow to always run migrations before cleaning - Add run_prisma_generate() function to update Prisma Client after migrations - Update documentation to reflect new workflow where --clean-database implies migration - Remove conditional migration logic that could cause double execution
This commit is contained in:
@@ -9,9 +9,8 @@
|
|||||||
# ./deploy.sh --flutter – bygg bara flutter web-app
|
# ./deploy.sh --flutter – bygg bara flutter web-app
|
||||||
# ./deploy.sh --importer – bygg bara importer-microservice
|
# ./deploy.sh --importer – bygg bara importer-microservice
|
||||||
# ./deploy.sh --seed – kör full seed på databasen (opt-in)
|
# ./deploy.sh --seed – kör full seed på databasen (opt-in)
|
||||||
# ./deploy.sh --migrate – kör Prisma-migrationer explicit (opt-in)
|
|
||||||
# ./deploy.sh --skip-migration – hoppa över automatisk startup-migrering i recipe-api
|
# ./deploy.sh --skip-migration – hoppa över automatisk startup-migrering i recipe-api
|
||||||
# ./deploy.sh --clean-database – kör underhålls-SQL som rensar data men behåller kategorier
|
# ./deploy.sh --clean-database – kör migration och därefter underhålls-SQL som rensar data men behåller kategorier
|
||||||
# ./deploy.sh --pull-always – kontrollera uppdateringar för basimages
|
# ./deploy.sh --pull-always – kontrollera uppdateringar för basimages
|
||||||
# ./deploy.sh --backend --seed – kombinera flaggor fritt (git pull körs alltid)
|
# ./deploy.sh --backend --seed – kombinera flaggor fritt (git pull körs alltid)
|
||||||
|
|
||||||
@@ -26,7 +25,6 @@ BUILD_BACKEND=false
|
|||||||
BUILD_FLUTTER=false
|
BUILD_FLUTTER=false
|
||||||
BUILD_IMPORTER=false
|
BUILD_IMPORTER=false
|
||||||
RUN_SEED=false
|
RUN_SEED=false
|
||||||
RUN_MIGRATE=false
|
|
||||||
RUN_CLEAN_DATABASE=false
|
RUN_CLEAN_DATABASE=false
|
||||||
SKIP_MIGRATION=false
|
SKIP_MIGRATION=false
|
||||||
PULL_IMAGES=false # --pull=false är standard (snabbt)
|
PULL_IMAGES=false # --pull=false är standard (snabbt)
|
||||||
@@ -101,13 +99,17 @@ run_prisma_migrate_deploy() {
|
|||||||
info "Migrationer slutförda utan fel."
|
info "Migrationer slutförda utan fel."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run_prisma_generate() {
|
||||||
|
info "Uppdaterar Prisma Client..."
|
||||||
|
docker exec recipe-api sh -lc "cd /app && npx prisma generate --schema prisma/schema.prisma"
|
||||||
|
}
|
||||||
|
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
case "$arg" in
|
case "$arg" in
|
||||||
--backend) BUILD_BACKEND=true; BUILD_ALL=false ;;
|
--backend) BUILD_BACKEND=true; BUILD_ALL=false ;;
|
||||||
--flutter) BUILD_FLUTTER=true; BUILD_ALL=false ;;
|
--flutter) BUILD_FLUTTER=true; BUILD_ALL=false ;;
|
||||||
--importer) BUILD_IMPORTER=true; BUILD_ALL=false ;;
|
--importer) BUILD_IMPORTER=true; BUILD_ALL=false ;;
|
||||||
--seed) RUN_SEED=true ;;
|
--seed) RUN_SEED=true ;;
|
||||||
--migrate) RUN_MIGRATE=true; BUILD_BACKEND=true; BUILD_ALL=false ;;
|
|
||||||
--skip-migration) SKIP_MIGRATION=true ;;
|
--skip-migration) SKIP_MIGRATION=true ;;
|
||||||
--clean-database) RUN_CLEAN_DATABASE=true; BUILD_BACKEND=true; BUILD_ALL=false ;;
|
--clean-database) RUN_CLEAN_DATABASE=true; BUILD_BACKEND=true; BUILD_ALL=false ;;
|
||||||
--pull-always) PULL_IMAGES=true ;;
|
--pull-always) PULL_IMAGES=true ;;
|
||||||
@@ -125,8 +127,8 @@ if [ "$BUILD_ALL" = true ]; then
|
|||||||
BUILD_IMPORTER=true
|
BUILD_IMPORTER=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Om explicit migration begärs, stäng av automigrering i containern för att undvika dubbelkörning.
|
# Om databasrensning begärs, stäng av automigrering i containern för att undvika dubbelkörning.
|
||||||
if [ "$RUN_MIGRATE" = true ]; then
|
if [ "$RUN_CLEAN_DATABASE" = true ]; then
|
||||||
SKIP_MIGRATION=true
|
SKIP_MIGRATION=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -136,7 +138,7 @@ fi
|
|||||||
require_cmd git
|
require_cmd git
|
||||||
require_cmd docker
|
require_cmd docker
|
||||||
|
|
||||||
if [ "$BUILD_BACKEND" = true ] || [ "$RUN_SEED" = true ] || [ "$RUN_CLEAN_DATABASE" = true ] || [ "$RUN_MIGRATE" = true ]; then
|
if [ "$BUILD_BACKEND" = true ] || [ "$RUN_SEED" = true ] || [ "$RUN_CLEAN_DATABASE" = true ]; then
|
||||||
require_cmd grep
|
require_cmd grep
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -176,17 +178,15 @@ fi
|
|||||||
info "Startar tjänster..."
|
info "Startar tjänster..."
|
||||||
"${COMPOSE_CMD[@]}" up -d
|
"${COMPOSE_CMD[@]}" up -d
|
||||||
|
|
||||||
# ── Prisma migreringar och databasrensning (opt-in) ─────────────────────────
|
# ── Databasrensning (opt-in) ──────────────────────────────────────────────────
|
||||||
if [ "$RUN_MIGRATE" = true ] || [ "$RUN_CLEAN_DATABASE" = true ]; then
|
if [ "$RUN_CLEAN_DATABASE" = true ]; then
|
||||||
CLEAN_SQL_FILE="backend/prisma/maintenance/clean-database.sql"
|
CLEAN_SQL_FILE="backend/prisma/maintenance/clean-database.sql"
|
||||||
|
|
||||||
wait_for_backend_prisma || fatal "Backend blev inte redo för Prisma-kommandon i tid."
|
wait_for_backend_prisma || fatal "Backend blev inte redo för Prisma-kommandon i tid."
|
||||||
|
|
||||||
if [ "$RUN_MIGRATE" = true ]; then
|
info "Säkerställer uppdaterat databasschema före rensning..."
|
||||||
run_prisma_migrate_deploy
|
run_prisma_migrate_deploy
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$RUN_CLEAN_DATABASE" = true ]; then
|
|
||||||
[ -f "$CLEAN_SQL_FILE" ] || fatal "Saknar $CLEAN_SQL_FILE"
|
[ -f "$CLEAN_SQL_FILE" ] || fatal "Saknar $CLEAN_SQL_FILE"
|
||||||
|
|
||||||
MARIADB_ROOT_PASSWORD="$(read_env_value MARIADB_ROOT_PASSWORD)"
|
MARIADB_ROOT_PASSWORD="$(read_env_value MARIADB_ROOT_PASSWORD)"
|
||||||
@@ -198,10 +198,14 @@ if [ "$RUN_MIGRATE" = true ] || [ "$RUN_CLEAN_DATABASE" = true ]; then
|
|||||||
info "Kör databasrensning från $CLEAN_SQL_FILE ..."
|
info "Kör databasrensning från $CLEAN_SQL_FILE ..."
|
||||||
docker exec -i recipe-db mariadb -uroot -p"$MARIADB_ROOT_PASSWORD" "$MARIADB_DATABASE" < "$CLEAN_SQL_FILE"
|
docker exec -i recipe-db mariadb -uroot -p"$MARIADB_ROOT_PASSWORD" "$MARIADB_DATABASE" < "$CLEAN_SQL_FILE"
|
||||||
info "Databasrensning klar (kategorier bevarade enligt SQL-filen)."
|
info "Databasrensning klar (kategorier bevarade enligt SQL-filen)."
|
||||||
|
|
||||||
|
run_prisma_generate
|
||||||
fi
|
fi
|
||||||
|
|
||||||
info "Uppdaterar Prisma Client..."
|
# Visa Prisma Client-output även vid vanlig deploy när automigrering är aktiv.
|
||||||
docker exec recipe-api sh -lc "cd /app && npx prisma generate --schema prisma/schema.prisma"
|
if [ "$RUN_CLEAN_DATABASE" = false ] && [ "$SKIP_MIGRATION" = false ] && [ "$BUILD_BACKEND" = true ]; then
|
||||||
|
wait_for_backend_prisma || fatal "Backend blev inte redo för Prisma-kommandon i tid."
|
||||||
|
run_prisma_generate
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ── Seed (opt-in) ─────────────────────────────────────────────────────────────
|
# ── Seed (opt-in) ─────────────────────────────────────────────────────────────
|
||||||
|
|||||||
Reference in New Issue
Block a user