Skip to content

Commit 2ffee5f

Browse files
Add opt-int database seeding support (#404)
* Adds opt-in seeding * Updated seeders to execute correctly. Configured defaults. * Add documentation for Laravel db:seed automation option --------- Co-authored-by: Jay Rogers <jay@521dimensions.com>
1 parent 8abfd75 commit 2ffee5f

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

docs/content/docs/4.laravel/1.laravel-automations.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ In order for this script to run,`AUTORUN_ENABLED` must be set to `true`. Once th
2323
| `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+ |
2424
| `AUTORUN_LARAVEL_MIGRATION_TIMEOUT` | `30` | Number of seconds to wait for database connection before timing out during migrations. |
2525
| `AUTORUN_LARAVEL_OPTIMIZE` | `true` | `php artisan optimize`: Optimizes the application. |
26+
| `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. |
2627
| `AUTORUN_LARAVEL_ROUTE_CACHE` | `true` | `php artisan route:cache`: Caches the routes. |
2728
| `AUTORUN_LARAVEL_STORAGE_LINK` | `true` | `php artisan storage:link`: Creates a symbolic link from `public/storage` to `storage/app/public`. |
2829
| `AUTORUN_LARAVEL_VIEW_CACHE` | `true` | `php artisan view:cache`: Caches the views. |
@@ -65,6 +66,11 @@ This command caches all configuration files into a single file, which can then b
6566

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

69+
## php artisan db:seed
70+
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.
71+
72+
[Read more about seeding →](https://laravel.com/docs/12.x/seeding)
73+
6874
## php artisan route:cache
6975
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.
7076

src/common/etc/entrypoint.d/50-laravel-automations.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ script_name="laravel-automations"
2424
: "${AUTORUN_LARAVEL_MIGRATION_ISOLATION:=false}"
2525
: "${AUTORUN_LARAVEL_MIGRATION_TIMEOUT:=30}"
2626

27+
# Set default values for seeders
28+
: "${AUTORUN_LARAVEL_SEED:=false}"
29+
2730
# Set default values for Laravel version
2831
INSTALLED_LARAVEL_VERSION=""
2932

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

139-
echo "🛠️ Running optimizations: \"php artisan optimize ${except:+--except=${except}}\"..."
142+
echo "🚀 Running optimizations: \"php artisan optimize ${except:+--except=${except}}\"..."
140143
if ! php "$APP_BASE_DIR/artisan" optimize ${except:+--except=${except}}; then
141144
echo "$script_name: ❌ Laravel optimize failed"
142145
return 1
@@ -174,6 +177,18 @@ artisan_optimize() {
174177
return $has_error
175178
}
176179

180+
artisan_seed(){
181+
# Run the default seeder if "true", otherwise use value as custom seeder
182+
if [ "${AUTORUN_LARAVEL_SEED}" = "true" ]; then
183+
echo "🚀 Running default seeder: \"php artisan db:seed\""
184+
php "${APP_BASE_DIR}/artisan" db:seed --force
185+
else
186+
echo "🚀 Running custom seeder: \"php artisan db:seed --seeder=${AUTORUN_LARAVEL_SEED}\""
187+
echo "ℹ️ Your application must have a seeder class named \"${AUTORUN_LARAVEL_SEED}\" or this command will fail."
188+
php "${APP_BASE_DIR}/artisan" db:seed --seeder="${AUTORUN_LARAVEL_SEED}"
189+
fi
190+
}
191+
177192
get_laravel_version() {
178193
# Return cached version if already set
179194
if [ -n "$INSTALLED_LARAVEL_VERSION" ]; then
@@ -314,6 +329,10 @@ if laravel_is_installed; then
314329
artisan_migrate
315330
fi
316331

332+
if [ "$AUTORUN_LARAVEL_SEED" != "false" ]; then
333+
artisan_seed
334+
fi
335+
317336
if [ "$AUTORUN_LARAVEL_OPTIMIZE" = "true" ] || \
318337
[ "$AUTORUN_LARAVEL_CONFIG_CACHE" = "true" ] || \
319338
[ "$AUTORUN_LARAVEL_ROUTE_CACHE" = "true" ] || \

0 commit comments

Comments
 (0)