diff --git a/.env.example b/.env.example index 061d06f..065c3fe 100644 --- a/.env.example +++ b/.env.example @@ -7,6 +7,7 @@ APP_URL=http://localhost:8080 APP_PORT=8080 SEED_DEV_DATA=false +RUN_DB_SEED_ON_FIRST_START=true DB_CONNECTION=mysql DB_HOST=mariadb diff --git a/README.md b/README.md index e2d7aeb..bc497e6 100644 --- a/README.md +++ b/README.md @@ -94,27 +94,17 @@ chmod -R 775 storage bootstrap/cache # 4. Avvia i container (build al primo avvio) docker compose up -d --build - -# 5. Installa le dipendenze PHP -docker compose exec app composer install - -# 6. Installa le dipendenze Node.js -docker compose exec app npm install - -# 7. Pubblica gli asset dei pacchetti Spatie -docker compose exec app php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" -docker compose exec app php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations" - -# 8. Esegui le migrazioni e i seeder -docker compose exec app php artisan migrate --seed - -# 9. Crea il symbolic link per lo storage pubblico -docker compose exec app php artisan storage:link - -# 10. Compila gli asset frontend -docker compose exec app npm run build ``` +Il codice applicativo e montato direttamente dal filesystem host, quindi ogni modifica locale e immediatamente visibile nel container (senza rebuild ad ogni edit). + +Al primo avvio del container `app` vengono eseguiti automaticamente: + +- setup `.env` e `APP_KEY` +- install/build dipendenze +- `php artisan migrate --force` +- `php artisan db:seed --force` **solo al primo avvio** (marker persistente) + > **Nota**: La `APP_KEY` viene generata automaticamente al primo avvio se assente nel `.env`. Al termine l'applicazione sarà disponibile su: **http://localhost:8080** @@ -140,6 +130,7 @@ docker compose up -d --build | `APP_KEY` | (generata) | Chiave AES-256 per cifratura. **Mai condividere** | | `APP_PORT` | `8080` | Porta host per l'applicazione | | `SEED_DEV_DATA` | `false` | Se `true`, `php artisan db:seed` include anche i dati demo | +| `RUN_DB_SEED_ON_FIRST_START` | `true` | Se `true`, esegue il seed automatico solo al primo avvio container | | `DB_DATABASE` | `termanager2` | Nome database MariaDB | | `DB_USERNAME` | `termanager2` | Utente database | | `DB_PASSWORD` | `secret` | Password database | diff --git a/app/Models/Territorio.php b/app/Models/Territorio.php index 6f61ab2..9798bb6 100644 --- a/app/Models/Territorio.php +++ b/app/Models/Territorio.php @@ -168,9 +168,8 @@ class Territorio extends Model $soglia = Setting::getValue('giorni_per_smarrito', 120); return $query->attivi() - ->whereHas('assegnazioni', function ($q) use ($soglia) { - $q->whereNull('returned_at') - ->where('assigned_at', '<=', now()->subDays($soglia)); + ->whereHas('assegnazioneCorrente', function ($q) use ($soglia) { + $q->where('assigned_at', '<=', now()->subDays($soglia)); }); } diff --git a/docker-compose.yml b/docker-compose.yml index 920177a..fae50ed 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,8 +7,7 @@ services: restart: unless-stopped working_dir: /var/www/html volumes: - - app_code:/var/www/html - - storage_data:/var/www/html/storage/app + - ./:/var/www/html networks: - termanager2 depends_on: @@ -24,6 +23,8 @@ services: start_period: 45s environment: - PHP_OPCACHE_VALIDATE_TIMESTAMPS=1 + - SEED_DEV_DATA=${SEED_DEV_DATA:-false} + - RUN_DB_SEED_ON_FIRST_START=${RUN_DB_SEED_ON_FIRST_START:-true} nginx: build: @@ -34,7 +35,7 @@ services: ports: - "${APP_PORT:-8080}:80" volumes: - - app_code:/var/www/html:ro + - ./:/var/www/html:ro networks: - termanager2 depends_on: @@ -90,14 +91,10 @@ services: - termanager2 volumes: - app_code: - driver: local db_data: driver: local redis_data: driver: local - storage_data: - driver: local networks: termanager2: diff --git a/docker/php/entrypoint.sh b/docker/php/entrypoint.sh index 2e39d88..0423a68 100755 --- a/docker/php/entrypoint.sh +++ b/docker/php/entrypoint.sh @@ -37,37 +37,10 @@ upsert_env() { fi } -sync_app_code() { - local env_backup="/tmp/termanager2.env.backup" - - rm -f "$env_backup" - - if [ -f /var/www/html/.env ]; then - cp /var/www/html/.env "$env_backup" - fi - - cp -a /app-src/. /var/www/html/ - - if [ -f "$env_backup" ]; then - mv "$env_backup" /var/www/html/.env - fi -} - # ----------------------------------------------- -# 0. Sync application code from image to volume +# 0. Application code is bind-mounted from host # ----------------------------------------------- -IMAGE_BUILD_FILE="/app-src/.image-build-id" -VOLUME_BUILD_FILE="/var/www/html/.image-build-id" - -if [ ! -f /var/www/html/artisan ]; then - echo "[*] Syncing application code to volume..." - sync_app_code -elif [ -f "$IMAGE_BUILD_FILE" ] && { [ ! -f "$VOLUME_BUILD_FILE" ] || ! cmp -s "$IMAGE_BUILD_FILE" "$VOLUME_BUILD_FILE"; }; then - echo "[*] New image detected. Syncing updated application code to volume..." - sync_app_code -else - echo "[✓] Application code already in volume." -fi +echo "[✓] Using bind-mounted application code from host." # ----------------------------------------------- # 0b. Create required directories & fix permissions @@ -184,6 +157,25 @@ fi echo "[*] Running database migrations..." retry 10 3 php artisan migrate --force +# ----------------------------------------------- +# 7b. Seed database on first container startup only +# ----------------------------------------------- +SEED_MARKER_FILE="/var/www/html/storage/app/.db_seeded" +RUN_DB_SEED_ON_FIRST_START="${RUN_DB_SEED_ON_FIRST_START:-true}" + +if [ "$RUN_DB_SEED_ON_FIRST_START" = "true" ]; then + if [ ! -f "$SEED_MARKER_FILE" ]; then + echo "[*] First startup detected. Running database seed..." + retry 5 3 php artisan db:seed --force + touch "$SEED_MARKER_FILE" + echo "[✓] Database seed completed and startup marker saved." + else + echo "[✓] Seed already executed on a previous startup." + fi +else + echo "[i] RUN_DB_SEED_ON_FIRST_START=false, skipping automatic seed." +fi + # ----------------------------------------------- # 8. Cache config/routes/views # ----------------------------------------------- diff --git a/resources/views/livewire/home.blade.php b/resources/views/livewire/home.blade.php index 043d0b8..4fb096f 100644 --- a/resources/views/livewire/home.blade.php +++ b/resources/views/livewire/home.blade.php @@ -113,16 +113,19 @@