2018-09-14 00:50:09 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-09-14 00:50:09 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkMigrations;
|
|
|
|
|
2021-03-12 13:52:43 +03:00
|
|
|
use Doctrine\DBAL\Exception;
|
2018-09-14 00:50:09 +03:00
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
|
|
use Doctrine\Migrations\AbstractMigration;
|
2018-10-28 10:24:06 +03:00
|
|
|
use PDO;
|
2019-08-11 15:29:22 +03:00
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
2018-09-14 00:50:09 +03:00
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto-generated Migration: Please modify to your needs!
|
|
|
|
*/
|
|
|
|
final class Version20180913205455 extends AbstractMigration
|
|
|
|
{
|
|
|
|
public function up(Schema $schema): void
|
|
|
|
{
|
2018-09-14 20:04:40 +03:00
|
|
|
// Nothing to create
|
2018-09-14 00:50:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-12 13:52:43 +03:00
|
|
|
* @throws Exception
|
2018-09-14 00:50:09 +03:00
|
|
|
*/
|
2018-09-14 20:04:40 +03:00
|
|
|
public function postUp(Schema $schema): void
|
2018-09-14 00:50:09 +03:00
|
|
|
{
|
|
|
|
$qb = $this->connection->createQueryBuilder();
|
2018-09-14 20:04:40 +03:00
|
|
|
$qb->select('id', 'remote_addr')
|
2018-09-14 00:50:09 +03:00
|
|
|
->from('visits');
|
|
|
|
$st = $this->connection->executeQuery($qb->getSQL());
|
|
|
|
|
|
|
|
$qb = $this->connection->createQueryBuilder();
|
|
|
|
$qb->update('visits', 'v')
|
|
|
|
->set('v.remote_addr', ':obfuscatedAddr')
|
|
|
|
->where('v.id=:id');
|
|
|
|
|
2018-10-28 10:24:06 +03:00
|
|
|
while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
|
2018-09-14 00:50:09 +03:00
|
|
|
$addr = $row['remote_addr'] ?? null;
|
|
|
|
if ($addr === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb->setParameters([
|
|
|
|
'id' => $row['id'],
|
2018-09-14 20:17:30 +03:00
|
|
|
'obfuscatedAddr' => $this->determineAddress((string) $addr),
|
2018-09-14 00:50:09 +03:00
|
|
|
])->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 20:17:30 +03:00
|
|
|
private function determineAddress(string $addr): ?string
|
2018-09-14 00:50:09 +03:00
|
|
|
{
|
|
|
|
if ($addr === IpAddress::LOCALHOST) {
|
|
|
|
return $addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-11-08 13:28:27 +03:00
|
|
|
return (string) IpAddress::fromString($addr)->getAnonymizedCopy();
|
2021-05-23 13:37:53 +03:00
|
|
|
} catch (InvalidArgumentException) {
|
2018-09-14 00:50:09 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function down(Schema $schema): void
|
|
|
|
{
|
2018-09-14 20:04:40 +03:00
|
|
|
// Nothing to rollback
|
2018-09-14 00:50:09 +03:00
|
|
|
}
|
2021-10-23 17:02:29 +03:00
|
|
|
|
|
|
|
public function isTransactional(): bool
|
|
|
|
{
|
|
|
|
return $this->connection->getDatabasePlatform()->getName() !== 'mysql';
|
|
|
|
}
|
2018-09-14 00:50:09 +03:00
|
|
|
}
|