From 49ddd63dbd933e2eb84913b5f0c44099c45f33a0 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 12 Oct 2020 20:51:35 -0700 Subject: [PATCH] Do not allow running the up or seed commands if migrations have not been run --- .../Commands/Overrides/SeedCommand.php | 26 ++++++++ app/Console/Commands/Overrides/UpCommand.php | 23 +++++++ app/Console/RequiresDatabaseMigrations.php | 61 +++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 app/Console/Commands/Overrides/SeedCommand.php create mode 100644 app/Console/Commands/Overrides/UpCommand.php create mode 100644 app/Console/RequiresDatabaseMigrations.php diff --git a/app/Console/Commands/Overrides/SeedCommand.php b/app/Console/Commands/Overrides/SeedCommand.php new file mode 100644 index 000000000..91b555d4b --- /dev/null +++ b/app/Console/Commands/Overrides/SeedCommand.php @@ -0,0 +1,26 @@ +hasCompletedMigrations()) { + return $this->showMigrationWarning(); + } + + return parent::handle(); + } +} diff --git a/app/Console/Commands/Overrides/UpCommand.php b/app/Console/Commands/Overrides/UpCommand.php new file mode 100644 index 000000000..3a5f34610 --- /dev/null +++ b/app/Console/Commands/Overrides/UpCommand.php @@ -0,0 +1,23 @@ +hasCompletedMigrations()) { + return $this->showMigrationWarning(); + } + + return parent::handle(); + } +} diff --git a/app/Console/RequiresDatabaseMigrations.php b/app/Console/RequiresDatabaseMigrations.php new file mode 100644 index 000000000..f288ca1f1 --- /dev/null +++ b/app/Console/RequiresDatabaseMigrations.php @@ -0,0 +1,61 @@ +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(" +| @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | +| | +| Your database has not been properly migrated! | +| | +| @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | + +You must run the following command to finish migrating your database: + + 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; + } +}