98 lines
2.9 KiB
PHP
98 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class CleanMutationsData extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'mutations:clean {--force : Force cleanup without confirmation}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Clean mutations data to allow migration rollback';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
if (!$this->option('force')) {
|
|
if (!$this->confirm('This will delete ALL mutations data. Are you sure?')) {
|
|
$this->info('Operation cancelled.');
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// Delete mutations data in proper order (foreign key constraints)
|
|
$this->info('Cleaning mutations data...');
|
|
|
|
// 1. Delete stock logs related to mutations
|
|
if (Schema::hasTable('stock_logs')) {
|
|
$deleted = DB::table('stock_logs')
|
|
->where('source_type', 'App\\Models\\Mutation')
|
|
->delete();
|
|
$this->info("Deleted {$deleted} stock logs related to mutations");
|
|
}
|
|
|
|
// 2. Delete mutation details
|
|
if (Schema::hasTable('mutation_details')) {
|
|
$deleted = DB::table('mutation_details')->delete();
|
|
$this->info("Deleted {$deleted} mutation details");
|
|
}
|
|
|
|
// 3. Delete mutations
|
|
if (Schema::hasTable('mutations')) {
|
|
$deleted = DB::table('mutations')->delete();
|
|
$this->info("Deleted {$deleted} mutations");
|
|
}
|
|
|
|
// 4. Reset auto increment
|
|
if (Schema::hasTable('mutations')) {
|
|
DB::statement('ALTER TABLE mutations AUTO_INCREMENT = 1');
|
|
$this->info('Reset mutations auto increment');
|
|
}
|
|
|
|
if (Schema::hasTable('mutation_details')) {
|
|
DB::statement('ALTER TABLE mutation_details AUTO_INCREMENT = 1');
|
|
$this->info('Reset mutation_details auto increment');
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
$this->info('✅ Mutations data cleaned successfully!');
|
|
$this->info('You can now rollback and re-run migrations.');
|
|
|
|
return 0;
|
|
|
|
} catch (\Exception $e) {
|
|
DB::rollback();
|
|
$this->error('❌ Error cleaning mutations data: ' . $e->getMessage());
|
|
return 1;
|
|
}
|
|
}
|
|
}
|