Oliver Servín

Weekly auto-reset a Laravel demo app

I have worked on a client's work where I needed to present a public demo of the work in progress App, but wanted to reset the demo after a week by restoring the database to its clean state.

I did it with a simply scheduled task in the schedule method of my application's App\Console\Kernel class.

namespace App\Console;
 
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
 
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
 
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('migrate:fresh --force --seed')->weekly();
}
 
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
 
require base_path('routes/console.php');
}
}

Frequency options are well documented on Laravel docs. You can change it to daily or hourly if you prefer.