Files
sibedas/app/Console/Commands/CheckSpatialPlanningConstraints.php
2025-06-18 02:54:41 +07:00

101 lines
4.0 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\SpatialPlanning;
use App\Models\RetributionProposal;
class CheckSpatialPlanningConstraints extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'spatial:check-constraints';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check spatial planning foreign key constraints and show statistics';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('Checking Spatial Planning Constraints...');
$this->newLine();
try {
// Get total spatial plannings
$totalSpatialPlannings = SpatialPlanning::count();
// Get spatial plannings with retribution proposals
$withProposals = SpatialPlanning::whereHas('retributionProposals')->count();
// Get spatial plannings without retribution proposals
$withoutProposals = SpatialPlanning::whereDoesntHave('retributionProposals')->count();
// Get total retribution proposals
$totalProposals = RetributionProposal::count();
// Get retribution proposals linked to spatial plannings
$linkedProposals = RetributionProposal::whereNotNull('spatial_planning_id')->count();
// Get standalone retribution proposals
$standaloneProposals = RetributionProposal::whereNull('spatial_planning_id')->count();
// Display statistics
$this->table(
['Metric', 'Count'],
[
['Total Spatial Plannings', $totalSpatialPlannings],
['├─ With Retribution Proposals', $withProposals],
['└─ Without Retribution Proposals', $withoutProposals],
['', ''],
['Total Retribution Proposals', $totalProposals],
['├─ Linked to Spatial Planning', $linkedProposals],
['└─ Standalone Proposals', $standaloneProposals],
]
);
$this->newLine();
// Show constraint implications
if ($withProposals > 0) {
$this->warn("⚠️ CONSTRAINT WARNING:");
$this->warn(" {$withProposals} spatial plannings have retribution proposals linked to them.");
$this->warn(" These cannot be deleted directly due to foreign key constraints.");
$this->newLine();
$this->info("💡 TRUNCATE OPTIONS:");
$this->info(" • Use --truncate to delete ALL data (spatial plannings + linked proposals)");
$this->info(" • Use --safe-truncate to delete only spatial plannings without proposals");
$this->info(" • Manual cleanup: Delete proposals first, then spatial plannings");
} else {
$this->info("✅ No foreign key constraints found.");
$this->info(" All spatial plannings can be safely truncated.");
}
$this->newLine();
// Show example commands
$this->info("📋 EXAMPLE COMMANDS:");
$this->info(" php artisan spatial:init --truncate # Delete all data (smart method)");
$this->info(" php artisan spatial:init --safe-truncate # Delete safe data only");
$this->info(" php artisan spatial:init --force-truncate # Force truncate (disable FK checks)");
$this->info(" php artisan spatial:init file.csv --truncate # Import with truncate");
return 0;
} catch (\Exception $e) {
$this->error('Error checking constraints: ' . $e->getMessage());
return 1;
}
}
}