Challenge 2: Write a Deployment Script — Possible Solution ==================================================================== #!/bin/bash set -e # stop immediately if any command fails # 1. Enable maintenance mode — visitors see a 503 page immediately php artisan down --render="errors::503" # 2. Pull the latest code git pull origin main # 3. Install/update dependencies (production-only, optimized autoloader) composer install --no-dev --optimize-autoloader # 4. Apply any pending database migrations php artisan migrate --force # --force is required because migrate normally prompts for confirmation # in production; this flag allows it to run unattended in a script. # 5. Cache config, routes, and views for production performance php artisan optimize # 6. Restart queue workers so they pick up the new code php artisan queue:restart # 7. Disable maintenance mode — site is live again php artisan up WHY THIS ORDER MATTERS -------------------------- - Maintenance mode goes UP FIRST, before anything else changes — this ensures no visitor sees a half-deployed, inconsistent state (old code running against a new database schema, or vice versa) at any point during the deployment. - Migrations run BEFORE optimize/queue:restart — the database schema needs to already match what the new code expects before that new code (including cached config/routes) becomes active. - queue:restart runs AFTER the new code is in place but BEFORE maintenance mode is lifted — this ensures that the moment real traffic resumes, queue workers are already running the new code, rather than processing new jobs with stale logic for however long it takes someone to notice and restart them manually. - Maintenance mode comes DOWN LAST — only once every other step has succeeded does real traffic get let back in; if any earlier step failed, `set -e` stops the script and the site stays safely in maintenance mode rather than serving a broken deploy to real visitors. WHY THIS WORKS -------------- - This sequence directly combines Chapter 5's queue:restart requirement, Chapter 8's optimize/maintenance-mode features, and the migrate workflow from Course 1 into one coherent, safety-ordered script — mirroring the same "test/build everything before it's deployable, then cut over" philosophy the TypeScript course's Deployment chapter taught in a different language. - set -e at the top of the script means any single failing command (a migration error, a composer install failure) stops the whole sequence immediately rather than continuing on and potentially bringing the site back up (php artisan up) in a broken state.