mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Migration fixes (#9776)
* add migrate:install to lnms, but hide it * Fix Database migration validation * remove extra import * Don't allow install to continue if db build fails
This commit is contained in:
@@ -27,6 +27,7 @@ namespace LibreNMS\DB;
|
||||
|
||||
use LibreNMS\Config;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use \Schema as LaravelSchema;
|
||||
|
||||
class Schema
|
||||
{
|
||||
@@ -39,6 +40,51 @@ class Schema
|
||||
private $relationships;
|
||||
private $schema;
|
||||
|
||||
/**
|
||||
* Check the database to see if the migrations have all been run
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isCurrent()
|
||||
{
|
||||
if (LaravelSchema::hasTable('migrations')) {
|
||||
return self::getMigrationFiles()->diff(self::getAppliedMigrations())->isEmpty();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for extra migrations and return them
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public static function getUnexpectedMigrations()
|
||||
{
|
||||
return self::getAppliedMigrations()->diff(self::getMigrationFiles());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
private static function getMigrationFiles()
|
||||
{
|
||||
$migrations = collect(glob(base_path('database/migrations/') . '*.php'))
|
||||
->map(function ($migration_file) {
|
||||
return basename($migration_file, '.php');
|
||||
});
|
||||
return $migrations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
private static function getAppliedMigrations()
|
||||
{
|
||||
$db = Eloquent::DB()->table('migrations')->pluck('migration');
|
||||
return $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the primary key column(s) for a table
|
||||
*
|
||||
|
||||
@@ -29,6 +29,7 @@ use Carbon\Carbon;
|
||||
use Carbon\CarbonInterval;
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\DB\Eloquent;
|
||||
use LibreNMS\DB\Schema;
|
||||
use LibreNMS\ValidationResult;
|
||||
use LibreNMS\Validator;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
@@ -49,11 +50,15 @@ class Database extends BaseValidation
|
||||
$latest = 1000;
|
||||
|
||||
if ($current === 0 || $current === $latest) {
|
||||
\Artisan::call('migrate', ['--pretend' => true, '--force' => true]);
|
||||
if (\Artisan::output() !== "Nothing to migrate.\n") {
|
||||
if (!Schema::isCurrent()) {
|
||||
$validator->fail("Your database is out of date!", './lnms migrate');
|
||||
return;
|
||||
}
|
||||
|
||||
if ($migrations = Schema::getUnexpectedMigrations()) {
|
||||
$validator->warn("Your database schema has extra migrations (" . $migrations->implode(', ') .
|
||||
"). If you just switched to the stable release from the daily release, your database is in between releases and this will be resolved with the next release.");
|
||||
}
|
||||
} elseif ($current < $latest) {
|
||||
$validator->fail(
|
||||
"Your database schema ($current) is older than the latest ($latest).",
|
||||
|
||||
33
app/Console/MigrateInstallCommand.php
Normal file
33
app/Console/MigrateInstallCommand.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* MigrateInstallCommand.php
|
||||
*
|
||||
* -Description-
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2019 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use Illuminate\Database\Console\Migrations\InstallCommand;
|
||||
|
||||
class MigrateInstallCommand extends InstallCommand
|
||||
{
|
||||
protected $hidden = true;
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Console\MigrateInstallCommand;
|
||||
use Illuminate\Foundation\Providers\ArtisanServiceProvider;
|
||||
|
||||
class CliServiceProvider extends ArtisanServiceProvider
|
||||
@@ -12,6 +13,7 @@ class CliServiceProvider extends ArtisanServiceProvider
|
||||
if (defined('LIBRENMS_CLI') && $this->app->environment() == 'production') {
|
||||
$this->commands = array_intersect_key($this->commands, [
|
||||
"Migrate" => true,
|
||||
"MigrateInstall" => true,
|
||||
]);
|
||||
|
||||
$this->registerCommands($this->commands);
|
||||
@@ -28,4 +30,12 @@ class CliServiceProvider extends ArtisanServiceProvider
|
||||
return new \App\Console\ModelMakeCommand($app['files']);
|
||||
});
|
||||
}
|
||||
|
||||
protected function registerMigrateInstallCommand()
|
||||
{
|
||||
// override so we can hide it
|
||||
$this->app->singleton('command.migrate.install', function ($app) {
|
||||
return new MigrateInstallCommand($app['migration.repository']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,9 +63,15 @@ try {
|
||||
|
||||
echo \Artisan::output();
|
||||
|
||||
echo $ret == 0 ? "\n\nSuccess!" : "\n\nError!";
|
||||
if ($ret == 0 && \LibreNMS\DB\Schema::isCurrent()) {
|
||||
echo "\n\nSuccess!";
|
||||
} else {
|
||||
echo "\n\nError!";
|
||||
http_response_code(500);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage() . "\n\nError!";
|
||||
http_response_code(500);
|
||||
}
|
||||
|
||||
ob_end_flush();
|
||||
|
||||
Reference in New Issue
Block a user