143 lines
3.9 KiB
PHP
143 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Imports;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use RuntimeException;
|
|
use App\Services\Imports\Concerns\ParsesSqlDump;
|
|
use App\Services\Imports\Concerns\ImportsAdminZoneCustomers;
|
|
use App\Services\Imports\Concerns\ImportsAdminZoneDns;
|
|
|
|
class AdminZoneDnsSqlImporter
|
|
{
|
|
use ParsesSqlDump;
|
|
use ImportsAdminZoneCustomers;
|
|
use ImportsAdminZoneDns;
|
|
|
|
private ?int $canonicalServerId = null;
|
|
|
|
public function import(
|
|
string $path,
|
|
bool $preview = false,
|
|
bool $importCustomers = true,
|
|
bool $importDns = true,
|
|
): array {
|
|
if (! is_file($path)) {
|
|
throw new RuntimeException("SQL file not found: {$path}");
|
|
}
|
|
|
|
$this->assertRequiredSchema();
|
|
|
|
$tables = $this->parseSqlDump($path, [
|
|
'users',
|
|
'dns_manager_domain',
|
|
'dns_domain_user',
|
|
'dns_manager_ns',
|
|
]);
|
|
|
|
$summary = [
|
|
'source' => [
|
|
'users' => count($tables['users']),
|
|
'dns_manager_domain' => count($tables['dns_manager_domain']),
|
|
'dns_domain_user' => count($tables['dns_domain_user']),
|
|
'dns_manager_ns' => count($tables['dns_manager_ns']),
|
|
],
|
|
'customers' => [
|
|
'created' => 0,
|
|
'updated' => 0,
|
|
'restored' => 0,
|
|
'skipped' => 0,
|
|
],
|
|
'dns_domains' => [
|
|
'created' => 0,
|
|
'updated' => 0,
|
|
'skipped' => 0,
|
|
],
|
|
'dns_records' => [
|
|
'created' => 0,
|
|
'updated' => 0,
|
|
'skipped' => 0,
|
|
],
|
|
'dns_links' => [
|
|
'created' => 0,
|
|
'updated' => 0,
|
|
'skipped' => 0,
|
|
],
|
|
];
|
|
|
|
if (! $importCustomers && ! $importDns) {
|
|
return $summary;
|
|
}
|
|
|
|
$callback = function () use ($tables, &$summary, $preview, $importCustomers, $importDns) {
|
|
$customerIdMap = [];
|
|
$domainIdMap = [];
|
|
|
|
if ($importCustomers) {
|
|
$customerIdMap = $this->importCustomers($tables['users'], $summary, $preview);
|
|
}
|
|
|
|
if ($importDns) {
|
|
$domainIdMap = $this->importDnsDomains($tables['dns_manager_domain'], $summary, $preview);
|
|
$this->importDnsRecords($tables['dns_manager_ns'], $domainIdMap, $summary, $preview);
|
|
$this->importDnsCustomerLinks($tables['dns_domain_user'], $customerIdMap, $domainIdMap, $summary, $preview);
|
|
}
|
|
};
|
|
|
|
if ($preview) {
|
|
$callback();
|
|
return $summary;
|
|
}
|
|
|
|
DB::transaction($callback);
|
|
|
|
return $summary;
|
|
}
|
|
|
|
private function assertRequiredSchema(): void
|
|
{
|
|
foreach (['username', 'email_verified_at', 'type'] as $column) {
|
|
if (! Schema::hasColumn('customers', $column)) {
|
|
throw new RuntimeException(
|
|
"Missing customers.{$column}. Run migrations before importing admin_zonedns data."
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function normalizeDateTime(mixed $value): ?string
|
|
{
|
|
$value = $this->nullableString($value);
|
|
if (! $value || $value === '0000-00-00 00:00:00') {
|
|
return null;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
private function nullableString(mixed $value, ?string $default = null): ?string
|
|
{
|
|
if ($value === null) {
|
|
return $default;
|
|
}
|
|
|
|
$value = trim((string) $value);
|
|
|
|
if ($value === '') {
|
|
return $default;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
private function nullableInt(mixed $value): ?int
|
|
{
|
|
if ($value === null || $value === '') {
|
|
return null;
|
|
}
|
|
|
|
return (int) $value;
|
|
}
|
|
}
|