Add migration to create a link table between anime_sets and media

This commit is contained in:
Timothy Warren 2020-04-22 17:52:07 -04:00
parent e17846f4a4
commit b614505499
5 changed files with 77 additions and 58 deletions

View File

@ -65,7 +65,7 @@
"phpstan/phpstan": "^0.12.0",
"phpunit/phpunit": "^9.1.1",
"roave/security-advisories": "dev-master",
"robmorgan/phinx": "^0.10.6",
"robmorgan/phinx": "^0.11.0",
"sebastian/phpcpd": "^5.0.2",
"spatie/phpunit-snapshot-assertions": "^4.1.0",
"squizlabs/php_codesniffer": "^3.2.2",

View File

@ -15,14 +15,12 @@ $_SERVER['HTTP_HOST'] = 'localhost';
try
{
(new Console([
'cache:clear' => Command\CacheClear::class,
'cache:refresh' => Command\CachePrime::class,
'clear:cache' => Command\CacheClear::class,
'clear:thumbnails' => Command\ClearThumbnails::class,
'refresh:cache' => Command\CachePrime::class,
'refresh:thumbnails' => Command\UpdateThumbnails::class,
'regenerate-thumbnails' => Command\UpdateThumbnails::class,
'lists:sync' => Command\SyncLists::class,
'sync:lists' => Command\SyncLists::class
]))->run();
}
catch (\Exception $e)

View File

@ -1,54 +0,0 @@
<?php
use Phinx\Migration\AbstractMigration;
class AddMangaCollectionTables extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
// Create manga_set table
$manga_set = $this->table('manga_set', ['id' => FALSE, 'primary_key' => ['hummingbird_id']]);
$manga_set->addColumn('hummingbird_id', 'biginteger')
->addColumn('slug', 'string', ['comment' => "URL slug used for image caching and generating links"])
->addColumn('title', 'string')
->addColumn('alternate_title', 'string', ['null' => TRUE])
->addColumn('media_id', 'integer', ['default' => 3, 'null' => TRUE])
->addColumn('show_type', 'string', ['default' => 'TV', 'null' => TRUE, 'comment' => "TV Series/OVA/etc"])
->addColumn('age_rating', 'string', ['default' => 'PG13', 'null' => TRUE])
->addColumn('cover_image', 'string', ['null' => TRUE])
->addColumn('episode_count', 'integer', ['null' => TRUE])
->addColumn('episode_length', 'integer', ['null' => TRUE])
->addColumn('notes', 'text', ['null' => TRUE])
->addForeignKey('media_id', 'media', 'id')
->create();
// Create genre_manga_set_link table
$genre_manga_set_link = $this->table('genre_manga_set_link', ['id' => FALSE, 'primary_key' => ['hummingbird_id', 'genre_id']]);
$genre_manga_set_link->addColumn('hummingbird_id', 'biginteger')
->addColumn('genre_id', 'integer')
->addForeignKey('hummingbird_id', 'manga_set', 'hummingbird_id')
->addForeignKey('genre_id', 'genres', 'id')
->create();
}
}

View File

@ -0,0 +1,75 @@
<?php
use Phinx\Migration\AbstractMigration;
class ReorganizeAnimeCollectionMedia extends AbstractMigration
{
public function up()
{
// Create the new link table
if ( ! $this->hasTable('anime_set_media_link'))
{
$newLinkTable = $this->table('anime_set_media_link', [
'id' => FALSE,
'primary_key' => ['hummingbird_id', 'media_id']
]);
$newLinkTable->addColumn('hummingbird_id', 'biginteger')
->addColumn('media_id', 'biginteger')
->addForeignKey('media_id', 'media', 'id')
->addForeignKey('hummingbird_id', 'anime_set', 'hummingbird_id')
->create();
}
// Get the old link entries
$insertRows = [];
$rows = $this->fetchAll('SELECT hummingbird_id, media_id from anime_set');
// Filter the numeric keys out of the row results
foreach ($rows as $row)
{
$keys = array_keys($row);
foreach ($keys as $k)
{
if (is_numeric($k))
{
unset($row[$k]);
}
}
$insertRows[] = $row;
}
// And put them in the new table
$linkTable = $this->table('anime_set_media_link');
$linkTable->insert($insertRows)->save();
// Get the rows where you have the combined media type (DVD & Bluray)
// and replace those rows with the individual entries
$linkRows = $this->fetchAll('SELECT hummingbird_id FROM anime_set_media_link WHERE media_id=1');
$insertRows = [];
foreach ($linkRows as $row)
{
$insertRows[] = [
'hummingbird_id' => $row['hummingbird_id'],
'media_id' => 2,
];
$insertRows[] = [
'hummingbird_id' => $row['hummingbird_id'],
'media_id' => 3,
];
}
$linkTable->insert($insertRows)->save();
// Finally, delete the old combined media type rows
$this->execute('DELETE FROM anime_set_media_link WHERE media_id=1');
}
public function down()
{
if ($this->hasTable('anime_set_media_link'))
{
$this->table('anime_set_media_link')->drop()->save();
}
}
}