add username migration

This commit is contained in:
Bence Pőcze 2023-09-24 00:35:15 +02:00
parent 5b045335a1
commit c25ba2dd28
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D
2 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,20 @@
<?php
use MapGuesser\PersistentData\Model\User;
use MapGuesser\Repository\UserRepository;
use MapGuesser\Util\UsernameGenerator;
use SokoWeb\Database\Query\Select;
$select = new Select(Container::$dbConnection);
$users = Container::$persistentDataManager->selectMultipleFromDb($select, User::class);
$userRepository = new UserRepository();
$usernameGenerator = new UsernameGenerator();
foreach ($users as $user) {
do {
$username = $usernameGenerator->generate();
} while ($userRepository->getByUsername($username));
$user->setUsername($username);
Container::$persistentDataManager->saveToDb($user);
}

View File

@ -0,0 +1,3 @@
ALTER TABLE `users`
ADD `username` VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL AFTER `email`,
ADD UNIQUE `username` (`username`);