70 lines
4.1 KiB
PHP
70 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DoctrineMigrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Doctrine\Migrations\AbstractMigration;
|
|
|
|
/**
|
|
* Auto-generated Migration: Please modify to your needs!
|
|
*/
|
|
final class Version20260417132000 extends AbstractMigration
|
|
{
|
|
public function getDescription(): string
|
|
{
|
|
return 'Create DhcpServer entity and link to Subnet';
|
|
}
|
|
|
|
public function up(Schema $schema): void
|
|
{
|
|
// this up() migration is auto-generated, please modify it to your needs
|
|
$this->addSql('CREATE TABLE dhcp_server (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, ip VARCHAR(255) NOT NULL, api_port INT DEFAULT 8006 NOT NULL, comments VARCHAR(255) DEFAULT NULL, uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', UNIQUE INDEX UNIQ_IDENTIFIER_IP (ip), UNIQUE INDEX UNIQ_IDENTIFIER_NAME (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
|
|
$this->addSql('ALTER TABLE subnet ADD dhcp_server_id INT DEFAULT NULL');
|
|
$this->addSql('ALTER TABLE subnet ADD CONSTRAINT FK_50551F51C2C88941 FOREIGN KEY (dhcp_server_id) REFERENCES dhcp_server (id)');
|
|
$this->addSql('CREATE INDEX IDX_50551F51C2C88941 ON subnet (dhcp_server_id)');
|
|
$this->addSql('ALTER TABLE dhcp_server ADD migration_id VARCHAR(255) DEFAULT NULL, ADD created_at DATETIME NOT NULL, ADD updated_at DATETIME NOT NULL, ADD created_by VARCHAR(255) DEFAULT NULL, ADD updated_by VARCHAR(255) DEFAULT NULL, CHANGE api_port api_port INT DEFAULT NULL');
|
|
$this->addSql('CREATE UNIQUE INDEX UNIQ_BE36D128D17F50A6 ON dhcp_server (uuid)');
|
|
$this->addSql('ALTER TABLE dhcp_server RENAME INDEX uniq_identifier_ip TO UNIQ_DHCP_SERVER_IP');
|
|
$this->addSql('ALTER TABLE dhcp_server RENAME INDEX uniq_identifier_name TO UNIQ_DHCP_SERVER_NAME');
|
|
$this->addSql('ALTER TABLE subnet RENAME INDEX idx_50551f51c2c88941 TO IDX_91C24216786B311');
|
|
|
|
// Extract old legacy DHCP URL from env.json if exists, else fallback
|
|
$url = '127.0.0.1:8081';
|
|
$envFile = __DIR__ . '/../env.json';
|
|
if (file_exists($envFile)) {
|
|
$envData = json_decode(file_get_contents($envFile), true);
|
|
if (isset($envData['vars']['OG_DHCP_API_URL'])) {
|
|
$url = $envData['vars']['OG_DHCP_API_URL'];
|
|
}
|
|
}
|
|
|
|
// Normalize URL format before parsing since env.json stores it raw like '127.0.0.1:8081'
|
|
$url = (str_starts_with($url, 'http')) ? $url : 'http://' . $url;
|
|
$parsed = parse_url($url);
|
|
$ip = $parsed['host'] ?? '127.0.0.1';
|
|
$port = isset($parsed['port']) ? $parsed['port'] : 8081;
|
|
|
|
// Create a default instance to absorb existing subnets and prevent orphans
|
|
$this->addSql(sprintf("INSERT INTO dhcp_server (name, ip, api_port, uuid, migration_id, created_at, updated_at, created_by, updated_by) VALUES ('Servidor DHCP Local (Migrado)', '%s', %d, UUID(), NULL, NOW(), NOW(), 'ogadmin', 'ogadmin')", $ip, $port));
|
|
|
|
// Relink all existing orphan subnets to this newly created default server
|
|
$this->addSql("UPDATE subnet SET dhcp_server_id = (SELECT id FROM dhcp_server LIMIT 1) WHERE dhcp_server_id IS NULL");
|
|
}
|
|
|
|
public function down(Schema $schema): void
|
|
{
|
|
// this down() migration is auto-generated, please modify it to your needs
|
|
$this->addSql('ALTER TABLE subnet DROP FOREIGN KEY FK_50551F51C2C88941');
|
|
$this->addSql('DROP TABLE dhcp_server');
|
|
$this->addSql('DROP INDEX IDX_50551F51C2C88941 ON subnet');
|
|
$this->addSql('ALTER TABLE subnet DROP dhcp_server_id');
|
|
$this->addSql('DROP INDEX UNIQ_BE36D128D17F50A6 ON dhcp_server');
|
|
$this->addSql('ALTER TABLE dhcp_server DROP migration_id, DROP created_at, DROP updated_at, DROP created_by, DROP updated_by, CHANGE api_port api_port INT DEFAULT 8006 NOT NULL');
|
|
$this->addSql('ALTER TABLE dhcp_server RENAME INDEX uniq_dhcp_server_ip TO UNIQ_IDENTIFIER_IP');
|
|
$this->addSql('ALTER TABLE dhcp_server RENAME INDEX uniq_dhcp_server_name TO UNIQ_IDENTIFIER_NAME');
|
|
$this->addSql('ALTER TABLE subnet RENAME INDEX idx_91c24216786b311 TO IDX_50551F51C2C88941');
|
|
}
|
|
}
|