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.
1namespace App\Console; 2 3use Illuminate\Console\Scheduling\Schedule; 4use Illuminate\Foundation\Console\Kernel as ConsoleKernel; 5 6class Kernel extends ConsoleKernel 7{ 8 /** 9 * The Artisan commands provided by your application.10 *11 * @var array12 */13 protected $commands = [14 //15 ];16 17 /**18 * Define the application's command schedule.19 *20 * @param \Illuminate\Console\Scheduling\Schedule $schedule21 * @return void22 */23 protected function schedule(Schedule $schedule)24 {25 $schedule->command('migrate:fresh --force --seed')->weekly();26 }27 28 /**29 * Register the commands for the application.30 *31 * @return void32 */33 protected function commands()34 {35 $this->load(__DIR__.'/Commands');36 37 require base_path('routes/console.php');38 }39}
Frequency options are well documented on Laravel docs. You can change it to daily or hourly if you prefer.