2018-09-13 16:50:09 -05:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkMigrations;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\DBALException;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
|
|
use Doctrine\Migrations\AbstractMigration;
|
2018-10-28 02:24:06 -05:00
|
|
|
use PDO;
|
2018-09-13 16:50:09 -05:00
|
|
|
use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto-generated Migration: Please modify to your needs!
|
|
|
|
*/
|
|
|
|
final class Version20180913205455 extends AbstractMigration
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
*/
|
|
|
|
public function up(Schema $schema): void
|
|
|
|
{
|
2018-09-14 12:04:40 -05:00
|
|
|
// Nothing to create
|
2018-09-13 16:50:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
* @throws DBALException
|
|
|
|
*/
|
2018-09-14 12:04:40 -05:00
|
|
|
public function postUp(Schema $schema): void
|
2018-09-13 16:50:09 -05:00
|
|
|
{
|
|
|
|
$qb = $this->connection->createQueryBuilder();
|
2018-09-14 12:04:40 -05:00
|
|
|
$qb->select('id', 'remote_addr')
|
2018-09-13 16:50:09 -05: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 02:24:06 -05:00
|
|
|
while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
|
2018-09-13 16:50:09 -05:00
|
|
|
$addr = $row['remote_addr'] ?? null;
|
|
|
|
if ($addr === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb->setParameters([
|
|
|
|
'id' => $row['id'],
|
2018-09-14 12:17:30 -05:00
|
|
|
'obfuscatedAddr' => $this->determineAddress((string) $addr),
|
2018-09-13 16:50:09 -05:00
|
|
|
])->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 12:17:30 -05:00
|
|
|
private function determineAddress(string $addr): ?string
|
2018-09-13 16:50:09 -05:00
|
|
|
{
|
|
|
|
if ($addr === IpAddress::LOCALHOST) {
|
|
|
|
return $addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return (string) IpAddress::fromString($addr)->getObfuscatedCopy();
|
|
|
|
} catch (WrongIpException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
*/
|
|
|
|
public function down(Schema $schema): void
|
|
|
|
{
|
2018-09-14 12:04:40 -05:00
|
|
|
// Nothing to rollback
|
2018-09-13 16:50:09 -05:00
|
|
|
}
|
|
|
|
}
|