2018-02-06 06:16:24 +08:00
|
|
|
<?php
|
|
|
|
|
2019-04-08 07:53:24 +08:00
|
|
|
namespace App\Services\Installer;
|
2018-02-06 06:16:24 +08:00
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Service;
|
2019-08-05 20:27:53 +08:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2018-02-06 06:16:24 +08:00
|
|
|
use Nwidart\Modules\Facades\Module;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class MigrationService extends Service
|
2018-02-06 06:16:24 +08:00
|
|
|
{
|
|
|
|
protected function getMigrator()
|
|
|
|
{
|
|
|
|
$m = app('migrator');
|
|
|
|
$m->setConnection(config('database.default'));
|
|
|
|
return $m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all of the possible paths that migrations exist.
|
|
|
|
* Include looking in all of the modules Database/migrations directories
|
2019-04-08 08:23:44 +08:00
|
|
|
*
|
2018-02-06 06:16:24 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMigrationPaths(): array
|
|
|
|
{
|
|
|
|
$paths = [
|
2019-08-05 20:27:53 +08:00
|
|
|
'core' => App::databasePath().'/migrations',
|
2018-02-06 06:16:24 +08:00
|
|
|
];
|
|
|
|
|
2018-06-22 22:26:08 +08:00
|
|
|
$modules = Module::allEnabled();
|
2018-02-06 06:16:24 +08:00
|
|
|
foreach ($modules as $module) {
|
2019-04-08 08:23:44 +08:00
|
|
|
$module_path = $module->getPath().'/Database/migrations';
|
2019-04-08 11:12:45 +08:00
|
|
|
if (file_exists($module_path)) {
|
2018-02-06 06:16:24 +08:00
|
|
|
$paths[$module->getName()] = $module_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 00:01:31 +08:00
|
|
|
// Log::info('Update - migration paths', $paths);
|
2018-02-06 06:16:24 +08:00
|
|
|
|
|
|
|
return $paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return what migrations are available
|
|
|
|
*/
|
|
|
|
public function migrationsAvailable(): array
|
|
|
|
{
|
|
|
|
$migrator = $this->getMigrator();
|
|
|
|
$migration_dirs = $this->getMigrationPaths();
|
|
|
|
|
|
|
|
$files = $migrator->getMigrationFiles(array_values($migration_dirs));
|
|
|
|
$availMigrations = array_diff(array_keys($files), $migrator->getRepository()->getRan());
|
|
|
|
|
2019-10-24 00:01:31 +08:00
|
|
|
// Log::info('Migrations available:', $availMigrations);
|
2018-02-06 06:16:24 +08:00
|
|
|
|
|
|
|
return $availMigrations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run all of the migrations that are available. Just call artisan since
|
|
|
|
* it looks into all of the module directories, etc
|
|
|
|
*/
|
|
|
|
public function runAllMigrations()
|
|
|
|
{
|
|
|
|
$output = '';
|
|
|
|
|
2019-08-05 20:27:53 +08:00
|
|
|
Artisan::call('migrate');
|
|
|
|
$output .= trim(Artisan::output());
|
2018-02-06 06:16:24 +08:00
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|