Skip to content

Instantly share code, notes, and snippets.

@Anahkiasen
Created September 15, 2015 10:01
Show Gist options
  • Save Anahkiasen/b278e0a2e1301e0d14d2 to your computer and use it in GitHub Desktop.
Save Anahkiasen/b278e0a2e1301e0d14d2 to your computer and use it in GitHub Desktop.
<?php
class LegacyMigrator
{
protected $songsConverter;
/**
* LegacyMigrator constructor.
*
* @param $songsConverter
*/
public function __construct(SongsConverter $songsConverter)
{
$this->songsConverter = $songsConverter;
}
/**
* Migrate songs from ccs_custom_database_3 to the songs table.
*/
public function migrateSongs()
{
$this->output->title('Ignition Songs Migration');
$songs = DB::table('ccs_custom_database_3')->get();
DB::transaction(function () use ($songs) {
$this->progressIterator($songs, function ($song) {
$converted = $this->songsConverter->convert($song);
DB::table('songs')->insert($converted);
});
});
}
}
<?php
namespace Forge\Console\Commands;
use Forge\Services\LegacyMigration\LegacyMigrator;
use Illuminate\Console\Command;
class MigrateLegacyDatabase extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'forge:migrate {mode=all}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrate songs from IPContent to our own format';
/**
* @var LegacyMigrator
*/
protected $legacyMigrator;
/**
* @param LegacyMigrator $legacyMigrator
*/
public function __construct(LegacyMigrator $legacyMigrator)
{
parent::__construct();
$this->legacyMigrator = $legacyMigrator;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$mode = $this->argument('mode');
$this->legacyMigrator->setOutput($this->output);
switch ($mode) {
case 'songs':
$this->legacyMigrator->migrateSongs();
break;
case 'comments':
$this->legacyMigrator->migrateComments();
break;
case 'users':
$this->legacyMigrator->migrateUsers();
break;
case 'requests':
$this->legacyMigrator->migrateRequests();
break;
default:
$this->legacyMigrator->migrateUsers();
$this->legacyMigrator->migrateSongs();
$this->legacyMigrator->migrateComments();
$this->legacyMigrator->migrateRequests();
}
$this->startReindex();
}
/**
* Reindex the ES database.
*/
private function startReindex()
{
$this->info('Reindexing ElasticSearch...');
$this->call('es:reindex');
}
}
<?php
namespace Forge\Services\LegacyMigration\Converters;
use Forge\Entities\Models\Album;
use Forge\Entities\Models\Arrangement;
use Forge\Entities\Models\Artist;
use Forge\Entities\Models\Tuning;
class SongsConverter extends AbstractConverter
{
/**
* Convert a song to the Ignition format.
*
* @param object $song
*
* @return array
*/
public function convert($song)
{
$artist = $this->createArtist($song);
return [
'id' => (int) $song->primary_id_field,
'name' => $this->decode($song->field_16),
'official' => $song->field_40 === 'Yes',
'pc_link' => $pcLink,
'mac_link' => strpos($song->field_27, 'mac') !== false ? $pcLink : null,
'ps3_link' => strpos($song->field_27, 'ps3') !== false ? $pcLink : null,
'xbox_link' => strpos($song->field_27, 'xbox') !== false ? $pcLink : null,
'official_link' => $officialLink,
'video_link' => $song->field_31,
'playthrough_link' => $song->field_32,
'version' => $song->field_18,
'downloads' => $song->downloads,
'authors_note' => $this->convertInvisionPowerTags($song->field_30),
'remote_cover' => $song->field_19,
'user_id' => (int) $song->member_id,
'artist_id' => (int) $artist->id,
'album_id' => (int) $album->id,
'genre_id' => 0,
'created_at' => $this->convertToDateTime($song->record_saved),
'updated_at' => $this->convertToDateTime($song->record_updated),
];
}
/**
* @param $song
*
* @return static
*/
protected function createArtist($song)
{
return Artist::firstOrCreate([
'name' => $this->decode($song->field_15),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment