Added address:purge, changing active_addresses index

This commit is contained in:
Deon George 2022-11-19 22:15:08 +11:00
parent 9de2469053
commit 289caa8225
3 changed files with 107 additions and 3 deletions
app
Console/Commands
Models
database/migrations

View File

@ -0,0 +1,105 @@
<?php
namespace App\Console\Commands;
use App\Models\Address;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class AddressPurge extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'address:purge'
.' {src : Source Address}'
.' {dst : Destination Address}'
.' {--f|force : Force}'
.' {--d|dryrun : Dry Run}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Permanently remove a duplicate address';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$src = Address::withTrashed()->findOrfail($this->argument('src'));
$dst = Address::withTrashed()->findOrfail($this->argument('dst'));
if (($src->system_id !== $dst->system_id) && ($src->system->name !== 'Discovered System')) {
$this->error(sprintf('FTN addresses are from different systems (%s/%s)',$src->system->name,$dst->system->name));
exit(1);
}
if (! $this->option('force') && ($src->ftn !== $dst->ftn)) {
$this->error(sprintf('FTN addresses are not the same (%s:%s)',$src->ftn,$dst->ftn));
exit(1);
}
if ($src->active) {
$this->error(sprintf('Source [%s] is still active',$src->ftn));
exit(1);
}
DB::beginTransaction();
// Find all echomail seenbys
$x = DB::update('update echomail_seenby set address_id=? where address_id=?',[$dst->id,$src->id]);
$this->info(sprintf('Updated [%d] echomail seenby records',$x));
// Find all echomail paths
$x = DB::update('update echomail_path set address_id=? where address_id=?',[$dst->id,$src->id]);
$this->info(sprintf('Updated [%d] echomail path records',$x));
// Find all echomails
$x = DB::update('update echomails set fftn_id=? where fftn_id=?',[$dst->id,$src->id]);
$this->info(sprintf('Updated [%d] echomail source records',$x));
// Find all netmails
$x = DB::update('update netmails set fftn_id=? where fftn_id=?',[$dst->id,$src->id]);
$this->info(sprintf('Updated [%d] netmail source records',$x));
// Find all netmails
$x = DB::update('update netmails set tftn_id=? where tftn_id=?',[$dst->id,$src->id]);
$this->info(sprintf('Updated [%d] netmail destination records',$x));
// Find all nodelist
$x = DB::update('update address_nodelist set address_id=? where address_id=?',[$dst->id,$src->id]);
$this->info(sprintf('Updated [%d] nodelist records',$x));
// Find all file seenbys
$x = DB::update('update file_seenby set address_id=? where address_id=?',[$dst->id,$src->id]);
$this->info(sprintf('Updated [%d] file seenby records',$x));
// Find all files
$x = DB::update('update files set fftn_id=? where fftn_id=?',[$dst->id,$src->id]);
$this->info(sprintf('Updated [%d] file source records',$x));
if ($this->option('dryrun')) {
$this->warn(sprintf('NOT deleting [%s] - DRY RUN',$src->ftn));
DB::rollBack();
} else {
if ($src->forceDelete()) {
$this->alert(sprintf('%s deleted.', $src->ftn));
DB::commit();
} else {
$this->warn(sprintf('Address [%s] didnt delete?',$src->ftn));
DB::rollBack();
}
}
return Command::SUCCESS;
}
}

View File

@ -114,6 +114,7 @@ class Address extends Model
case self::NODE_UNKNOWN:
case self::NODE_ACTIVE:
case self::NODE_POINT:
case self::NODE_PVT:
// Nodes dont have children, but must return a relationship instance
return $this->hasOne(self::class,NULL,'void');

View File

@ -39,9 +39,7 @@ class Addresses extends Migration
$table->softDeletes();
});
DB::statement("CREATE UNIQUE INDEX active_addresses ON addresses (zone_id,region_id,host_id,node_id,point_id) WHERE active = true");
DB::statement("CREATE UNIQUE INDEX addresses_active ON addresses (zone_id,host_id,node_id,point_id) WHERE active = true");
}
/**