2018-09-13 16:50:09 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2018-09-13 16:50:09 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkMigrations;
|
|
|
|
|
2021-03-12 04:52:43 -06:00
|
|
|
use Doctrine\DBAL\Exception;
|
2022-01-10 05:05:01 -06:00
|
|
|
use Doctrine\DBAL\Platforms\MySQLPlatform;
|
2018-09-13 16:50:09 -05:00
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
|
|
use Doctrine\Migrations\AbstractMigration;
|
2018-10-28 02:24:06 -05:00
|
|
|
use PDO;
|
2019-08-11 07:29:22 -05:00
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
2018-09-13 16:50:09 -05: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 12:04:40 -05:00
|
|
|
// Nothing to create
|
2018-09-13 16:50:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-12 04:52:43 -06:00
|
|
|
* @throws Exception
|
2018-09-13 16:50:09 -05:00
|
|
|
*/
|
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 {
|
2020-11-08 04:28:27 -06:00
|
|
|
return (string) IpAddress::fromString($addr)->getAnonymizedCopy();
|
2021-05-23 05:37:53 -05:00
|
|
|
} catch (InvalidArgumentException) {
|
2018-09-13 16:50:09 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2021-10-23 09:02:29 -05:00
|
|
|
|
|
|
|
public function isTransactional(): bool
|
|
|
|
{
|
2022-01-10 05:05:01 -06:00
|
|
|
return ! ($this->connection->getDatabasePlatform() instanceof MySQLPlatform);
|
2021-10-23 09:02:29 -05:00
|
|
|
}
|
2018-09-13 16:50:09 -05:00
|
|
|
}
|