113 lines
3.5 KiB
PHP
113 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\Mutation;
|
|
use App\Models\MutationDetail;
|
|
|
|
class ClearMutationsCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'mutations:clear {--force : Force the operation without confirmation}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Clear all mutations and mutation details, then reset auto increment IDs';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
// Show warning
|
|
$this->warn('⚠️ WARNING: This will permanently delete ALL mutations and mutation details!');
|
|
$this->warn('⚠️ This action cannot be undone!');
|
|
|
|
// Check for force flag
|
|
if (!$this->option('force')) {
|
|
if (!$this->confirm('Are you sure you want to continue?')) {
|
|
$this->info('Operation cancelled.');
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// Show current counts
|
|
$mutationCount = Mutation::count();
|
|
$detailCount = MutationDetail::count();
|
|
$trashedMutationCount = Mutation::onlyTrashed()->count();
|
|
|
|
$this->info("Current data:");
|
|
$this->info("- Mutations: {$mutationCount}");
|
|
$this->info("- Mutation Details: {$detailCount}");
|
|
if ($trashedMutationCount > 0) {
|
|
$this->info("- Soft Deleted Mutations: {$trashedMutationCount}");
|
|
}
|
|
|
|
if ($mutationCount === 0 && $detailCount === 0 && $trashedMutationCount === 0) {
|
|
$this->info('No data to clear.');
|
|
return 0;
|
|
}
|
|
|
|
$this->info('Starting cleanup process...');
|
|
|
|
try {
|
|
// Delete data within transaction
|
|
DB::beginTransaction();
|
|
|
|
// Delete mutation details first (foreign key constraint)
|
|
$this->info('🗑️ Deleting mutation details...');
|
|
MutationDetail::query()->delete();
|
|
|
|
// Delete mutations (including soft deleted ones)
|
|
$this->info('🗑️ Deleting mutations...');
|
|
Mutation::query()->delete();
|
|
|
|
// Force delete soft deleted mutations
|
|
$this->info('🗑️ Force deleting soft deleted mutations...');
|
|
Mutation::onlyTrashed()->forceDelete();
|
|
|
|
DB::commit();
|
|
|
|
// Reset auto increment outside transaction (DDL operations auto-commit)
|
|
$this->info('🔄 Resetting mutation_details auto increment...');
|
|
DB::statement('ALTER TABLE mutation_details AUTO_INCREMENT = 1');
|
|
|
|
$this->info('🔄 Resetting mutations auto increment...');
|
|
DB::statement('ALTER TABLE mutations AUTO_INCREMENT = 1');
|
|
|
|
$this->info('✅ Successfully cleared all mutations and reset auto increment!');
|
|
$this->info('📊 Final counts:');
|
|
$this->info('- Mutations: ' . Mutation::count());
|
|
$this->info('- Mutation Details: ' . MutationDetail::count());
|
|
|
|
return 0;
|
|
|
|
} catch (\Exception $e) {
|
|
if (DB::transactionLevel() > 0) {
|
|
DB::rollback();
|
|
}
|
|
$this->error('❌ Failed to clear mutations: ' . $e->getMessage());
|
|
return 1;
|
|
}
|
|
}
|
|
}
|