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;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\DBALException;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws DBALException
|
|
|
|
*/
|
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 {
|
|
|
|
return (string) IpAddress::fromString($addr)->getObfuscatedCopy();
|
2019-08-11 15:29:22 +03:00
|
|
|
} catch (InvalidArgumentException $e) {
|
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
|
|
|
}
|
|
|
|
}
|