-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Migrate from old serialized format to new table format (#11)
- Loading branch information
1 parent
ffb14b1
commit 4943ea9
Showing
5 changed files
with
128 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace App\Migration; | ||
|
||
use App\DTO\GameDetail; | ||
use PDO; | ||
|
||
final readonly class Migration3 implements Migration | ||
{ | ||
public function migrate(PDO $pdo): void | ||
{ | ||
$originalRows = $pdo->query('select * from games')->fetchAll(PDO::FETCH_ASSOC); | ||
/** @var array<GameDetail> $games */ | ||
$games = array_map( | ||
fn (array $row) => unserialize($row['serializedData']), | ||
$originalRows, | ||
); | ||
|
||
$pdo->exec('drop table games'); | ||
$pdo->exec('create table games (id integer primary key autoincrement, title string, cd_key string, game_id integer, unique (game_id))'); | ||
$pdo->exec('create table downloads ( | ||
id integer primary key autoincrement, | ||
language string, | ||
platform string, | ||
name string, | ||
size float, | ||
url string, | ||
md5 string, | ||
game_id integer, | ||
foreign key (game_id) references games(id) on delete cascade on update cascade | ||
)'); | ||
|
||
foreach ($games as $game) { | ||
$pdo->prepare('insert into games (game_id, title, cd_key) values (?, ?, ?)')->execute([ | ||
$game->id, | ||
$game->title, | ||
$game->cdKey ?: null, | ||
]); | ||
$rowId = $pdo->lastInsertId(); | ||
|
||
foreach ($game->downloads as $download) { | ||
$pdo->prepare('insert into downloads (language, platform, name, size, url, md5, game_id) values (?, ?, ?, ?, ?, ?, ?)')->execute([ | ||
$download->language, | ||
$download->platform, | ||
$download->name, | ||
$download->size, | ||
$download->url, | ||
$download->md5, | ||
$rowId, | ||
]); | ||
} | ||
} | ||
} | ||
|
||
public function getVersion(): int | ||
{ | ||
return 3; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters