Laravel Packages

Refresh Your Laravel Database Without Dropping Every Table

Published
Refresh Your Laravel Database Without Dropping Every Table image

Laravel's built-in migrate:fresh drops every table before re-running your migrations, which is exactly what you want until it isn't. If a table holds seed data you don't want to regenerate, or test accounts you'd rather not recreate after every change, the all-or-nothing behaviour gets in the way. Custom Fresh, by Mahmoud Ramadan, adds a fresh:custom command that rebuilds the database while preserving the tables you name.

Choosing Which Tables Survive

Say you're building a subscription billing app and iterating on the schema all day, but you don't want to lose your test accounts or the seeded plans data on every refresh. You can name the tables to keep as a comma-separated argument or use the --keep flag. Both forms do the same thing:

php artisan fresh:custom users,plans
 
php artisan fresh:custom --keep=users,plans

Everything else is dropped, and the migrations run again, so the tables you list keep their rows while the rest of the schema is rebuilt from scratch.

Glob Patterns for Grouped Tables

Cashier alone creates subscriptions and subscription_items, and listing each one by name gets tedious. Custom Fresh accepts glob patterns, so a trailing wildcard matches any table sharing a prefix:

php artisan fresh:custom "users,plans,subscription_*"

Previewing Before You Commit

Because this command is destructive, you can ask it to report what it would do without touching the database. The --explain flag prints the tables it would keep and drop:

php artisan fresh:custom users,plans,subscription_* --explain

You can also point the command at a specific connection with --database, which is handy when some data lives in a separate database:

php artisan fresh:custom monthly_revenue --database=analytics
Screenshot showing fresh:custom command
Screenshot showing the Artisan command fresh:custom dry run with --explain and also running it without a dry run

Configuration and Events

Publishing the config file lets you set defaults instead of passing the same flags on every run. Tables in always_keep survive automatically, patterns applies your glob rules without spelling them out each time, and confirm_in lists the environments where the command pauses for a confirmation prompt before it drops anything:

return [
'always_keep' => ['users', 'plans'],
'patterns' => ['subscription_*'],
'confirm_in' => ['staging', 'production'],
];

The command also dispatches three events during a run, which you can listen for to log activity or trigger follow-up work: RefreshingDatabase before any tables are dropped, TablesDropped after the removal step, and DatabaseRefreshed once the migrations finish.

Installation

Install the package with Composer:

composer require ramadan/custom-fresh

Custom Fresh requires PHP 8.2 or higher and supports Laravel 10 through 13. You can read the documentation and view the source on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Validate and Convert HEIC Images in Laravel image

Validate and Convert HEIC Images in Laravel

Read article
Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues image

Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues

Read article
Reject Unexpected Array Keys with Laravel Validation image

Reject Unexpected Array Keys with Laravel Validation

Read article
Major performance improvements & security patches for Filament v4.12 and v5.7! image

Major performance improvements & security patches for Filament v4.12 and v5.7!

Read article
Laravel Boost Project Rules: Teach Agents Your Conventions image

Laravel Boost Project Rules: Teach Agents Your Conventions

Read article
Extract an Image's Dominant Color in Laravel image

Extract an Image's Dominant Color in Laravel

Read article