Skip to content

Add opt-int database seeding support #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/content/docs/4.laravel/1.laravel-automations.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ In order for this script to run,`AUTORUN_ENABLED` must be set to `true`. Once th
| `AUTORUN_LARAVEL_MIGRATION_ISOLATION` | `false` | Run your migrations with the [`--isolated`](https://laravel.com/docs/12.x/migrations#running-migrations) flag. <br> **ℹ️ Note:** Requires Laravel v9.38.0+ |
| `AUTORUN_LARAVEL_MIGRATION_TIMEOUT` | `30` | Number of seconds to wait for database connection before timing out during migrations. |
| `AUTORUN_LARAVEL_OPTIMIZE` | `true` | `php artisan optimize`: Optimizes the application. |
| `AUTORUN_LARAVEL_SEED` | `false` | `php artisan db:seed`: Runs the default seeder. <br> **ℹ️ Note:** Set to `true` to run the default seeder. If you want to run a custom seeder, set this to the name of the seeder class. |
| `AUTORUN_LARAVEL_ROUTE_CACHE` | `true` | `php artisan route:cache`: Caches the routes. |
| `AUTORUN_LARAVEL_STORAGE_LINK` | `true` | `php artisan storage:link`: Creates a symbolic link from `public/storage` to `storage/app/public`. |
| `AUTORUN_LARAVEL_VIEW_CACHE` | `true` | `php artisan view:cache`: Caches the views. |
Expand Down Expand Up @@ -65,6 +66,11 @@ This command caches all configuration files into a single file, which can then b

[Read more about configuration caching →](https://laravel.com/docs/12.x/configuration#configuration-caching)

## php artisan db:seed
This command runs the default seeder. If you want to run a custom seeder, set `AUTORUN_LARAVEL_SEED` to the name of the seeder class.

[Read more about seeding →](https://laravel.com/docs/12.x/seeding)

## php artisan route:cache
This command caches the routes, dramatically decrease the time it takes to register all of your application's routes. After running this command, your cached routes file will be loaded on every request.

Expand Down
21 changes: 20 additions & 1 deletion src/common/etc/entrypoint.d/50-laravel-automations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ script_name="laravel-automations"
: "${AUTORUN_LARAVEL_MIGRATION_ISOLATION:=false}"
: "${AUTORUN_LARAVEL_MIGRATION_TIMEOUT:=30}"

# Set default values for seeders
: "${AUTORUN_LARAVEL_SEED:=false}"

# Set default values for Laravel version
INSTALLED_LARAVEL_VERSION=""

Expand Down Expand Up @@ -136,7 +139,7 @@ artisan_optimize() {
[ "$AUTORUN_LARAVEL_VIEW_CACHE" = "false" ] && except="${except:+${except},}views"
[ "$AUTORUN_LARAVEL_EVENT_CACHE" = "false" ] && except="${except:+${except},}events"

echo "🛠️ Running optimizations: \"php artisan optimize ${except:+--except=${except}}\"..."
echo "🚀 Running optimizations: \"php artisan optimize ${except:+--except=${except}}\"..."
if ! php "$APP_BASE_DIR/artisan" optimize ${except:+--except=${except}}; then
echo "$script_name: ❌ Laravel optimize failed"
return 1
Expand Down Expand Up @@ -174,6 +177,18 @@ artisan_optimize() {
return $has_error
}

artisan_seed(){
# Run the default seeder if "true", otherwise use value as custom seeder
if [ "${AUTORUN_LARAVEL_SEED}" = "true" ]; then
echo "🚀 Running default seeder: \"php artisan db:seed\""
php "${APP_BASE_DIR}/artisan" db:seed --force
else
echo "🚀 Running custom seeder: \"php artisan db:seed --seeder=${AUTORUN_LARAVEL_SEED}\""
echo "ℹ️ Your application must have a seeder class named \"${AUTORUN_LARAVEL_SEED}\" or this command will fail."
php "${APP_BASE_DIR}/artisan" db:seed --seeder="${AUTORUN_LARAVEL_SEED}"
fi
}

get_laravel_version() {
# Return cached version if already set
if [ -n "$INSTALLED_LARAVEL_VERSION" ]; then
Expand Down Expand Up @@ -314,6 +329,10 @@ if laravel_is_installed; then
artisan_migrate
fi

if [ "$AUTORUN_LARAVEL_SEED" != "false" ]; then
artisan_seed
fi

if [ "$AUTORUN_LARAVEL_OPTIMIZE" = "true" ] || \
[ "$AUTORUN_LARAVEL_CONFIG_CACHE" = "true" ] || \
[ "$AUTORUN_LARAVEL_ROUTE_CACHE" = "true" ] || \
Expand Down