zend-blog-3-backend
38 строк · 1.9 Кб
1<?php
2
3declare(strict_types=1);
4
5namespace Application\Migrations;
6
7use Doctrine\DBAL\Schema\Schema;
8use Doctrine\Migrations\AbstractMigration;
9
10/**
11* Auto-generated Migration: Please modify to your needs!
12*/
13final class Version20230617072331 extends AbstractMigration
14{
15public function up(Schema $schema): void
16{
17// this up() migration is auto-generated, please modify it to your needs
18$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
19
20$this->addSql('ALTER TABLE tracking ADD ip_long INT UNSIGNED DEFAULT NULL AFTER ip_addr');
21$this->addSql('ALTER TABLE tracking_archive ADD ip_long INT UNSIGNED DEFAULT NULL AFTER ip_addr');
22$this->addSql('UPDATE tracking AS t, geo_location AS g SET t.ip_long = g.ip_long WHERE t.ip_addr IS NOT NULL AND g.ip_long = INET_ATON(t.ip_addr)');
23$this->addSql('ALTER TABLE tracking ADD CONSTRAINT FK_A87C621C28F0F5E7 FOREIGN KEY (ip_long) REFERENCES geo_location (ip_long) ON DELETE SET NULL');
24$this->addSql('CREATE INDEX IDX_A87C621C28F0F5E7 ON tracking (ip_long)');
25$this->addSql("UPDATE tracking_archive AS t, geo_location AS g SET t.ip_long = g.ip_long WHERE t.ip_addr IS NOT NULL AND REGEXP_LIKE(t.ip_addr, '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$') AND g.ip_long = INET_ATON(t.ip_addr)");
26}
27
28public function down(Schema $schema): void
29{
30// this down() migration is auto-generated, please modify it to your needs
31$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
32
33$this->addSql('ALTER TABLE tracking DROP FOREIGN KEY FK_A87C621C28F0F5E7');
34$this->addSql('DROP INDEX IDX_A87C621C28F0F5E7 ON tracking');
35$this->addSql('ALTER TABLE tracking DROP ip_long');
36$this->addSql('ALTER TABLE tracking_archive DROP ip_long');
37}
38}
39