/** * Execute the console command. * * @param \Jumilla\Versionia\Laravel\Migrator $migrator * * @return mixed */ public function handle(Migrator $migrator) { if (!$this->confirmToProceed()) { return; } $group = $this->argument('group'); // check valid group if (!in_array($group, $migrator->migrationGroups())) { throw new UnexpectedValueException("Migation group '{$group}' is not defined."); } $migrator->makeLogTable(); $this->doAgain($migrator, $group); $seed = $this->option('seed'); if ($seed) { $this->call('database:seed', ['name' => $seed, '--force' => true]); } }
/** * Migrate dataase to latest version. * * @param \Jumilla\Versionia\Laravel\Migrator $migrator */ protected function migrateToLatest(Migrator $migrator) { $installed_migrations = $migrator->installedLatestMigrations(); $migration_count = 0; foreach ($migrator->migrationGroups() as $group) { // [$group => ['version'=>$version, 'class'=>$class]] to $version $latest_installed_version = data_get($installed_migrations, $group . '.version', Migrator::VERSION_NULL); foreach ($migrator->migrationVersions($group) as $version => $class) { if ($migrator->compareMigrationVersion($version, $latest_installed_version) > 0) { $this->line("<info>Up [{$group}/{$version}]</info> Run class <comment>{$class}</comment>"); $migrator->doUpgrade($group, $version, $class); ++$migration_count; } } } if ($migration_count == 0) { $this->line('<info>Nothing to migrate.</info>'); } }
/** * Execute clean and upgrade. * * @param \Jumilla\Versionia\Laravel\Migrator $migrator */ protected function doRefresh(Migrator $migrator) { // retreive installed versions $installed_migrations = $migrator->installedMigrationsByDesc(); // downgrade foreach ($installed_migrations as $group => $migrations) { foreach ($migrations as $data) { $this->infoDowngrade($group, $data->version, $data->class); $migrator->doDowngrade($group, $data->version); } } // upgrade foreach ($migrator->migrationGroups() as $group) { foreach ($migrator->migrationVersions($group) as $version => $class) { $this->infoUpgrade($group, $version, $class); $migrator->doUpgrade($group, $version, $class); } } }
/** * Show migration infomation. * * @param \Jumilla\Versionia\Laravel\Migrator $migrator */ protected function showMigrations(Migrator $migrator) { $this->line('<comment>Migrations</comment>'); $groups = $migrator->migrationGroups(); if (count($groups) > 0) { // get [$group => $items] $installed_migrations = $migrator->installedMigrationsByDesc(); foreach ($groups as $group) { // [$items] to [$installed_versions] $installed_versions = $installed_migrations->get($group, collect())->pluck('version'); // [$versions] to $latest_version $latest_installed_version = $installed_versions->get(0, Migrator::VERSION_NULL); // enum definition foreach ($migrator->migrationVersionsByDesc($group) as $version => $class) { $installed = $installed_versions->contains($version); if ($version === $latest_installed_version) { $mark = '*'; } else { $mark = $installed ? '-' : ' '; } if (!class_exists($class)) { $this->line("{$mark} <info>[{$group}/{$version}]</info> <error>{$class}</error>"); $this->line(''); $this->error('Error: Class not found.'); continue; } $migration = new $class(); if (!$migration instanceof Migration) { $this->line("{$mark} <info>[{$group}/{$version}]</info> <error>{$class}</error>"); $this->line(''); $this->error('Error: Must inherit from class "Illuminate\\Database\\Migrations\\Migration".'); continue; } $this->line("{$mark} <info>[{$group}/{$version}]</info> {$class}"); } $this->line(''); } } else { $this->info('Nothing.'); $this->line(''); } }