Do not allow running the up or seed commands if migrations have not been run

This commit is contained in:
Dane Everitt 2020-10-12 20:51:35 -07:00
parent d5f2242c89
commit 49ddd63dbd
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 110 additions and 0 deletions

View file

@ -0,0 +1,26 @@
<?php
namespace Pterodactyl\Console\Commands\Overrides;
use Pterodactyl\Console\RequiresDatabaseMigrations;
use Illuminate\Database\Console\Seeds\SeedCommand as BaseSeedCommand;
class SeedCommand extends BaseSeedCommand
{
use RequiresDatabaseMigrations;
/**
* Block someone from running this seed command if they have not completed the migration
* process.
*
* @return int
*/
public function handle()
{
if (!$this->hasCompletedMigrations()) {
return $this->showMigrationWarning();
}
return parent::handle();
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Pterodactyl\Console\Commands\Overrides;
use Pterodactyl\Console\RequiresDatabaseMigrations;
use Illuminate\Foundation\Console\UpCommand as BaseUpCommand;
class UpCommand extends BaseUpCommand
{
use RequiresDatabaseMigrations;
/**
* @return bool|int
*/
public function handle()
{
if (!$this->hasCompletedMigrations()) {
return $this->showMigrationWarning();
}
return parent::handle();
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace Pterodactyl\Console;
/**
* @mixin \Illuminate\Console\Command
*/
trait RequiresDatabaseMigrations
{
/**
* Checks if the migrations have finished running by comparing the last migration file.
*
* @return bool
*/
protected function hasCompletedMigrations(): bool
{
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
$migrator = $this->getLaravel()->make('migrator');
$files = $migrator->getMigrationFiles(database_path('migrations'));
if (! $migrator->repositoryExists()) {
return false;
}
if (array_diff(array_keys($files), $migrator->getRepository()->getRan())) {
return false;
}
return true;
}
/**
* Throw a massive error into the console to hopefully catch the users attention and get
* them to properly run the migrations rather than ignoring all of the other previous
* errors...
*
* @return int
*/
protected function showMigrationWarning(): int
{
$this->getOutput()->writeln("<options=bold>
| @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| |
| Your database has not been properly migrated! |
| |
| @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |</>
You must run the following command to finish migrating your database:
<fg=green;options=bold>php artisan migrate --step --force</>
You will not be able to use Pterodactyl Panel as expected without fixing your
database state by running the command above.
");
$this->getOutput()->error("You must correct the error above before continuing.");
return 1;
}
}