Skip to content

Commit

Permalink
Migration fixes (librenms#9776)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
murrant authored and Tarik Karaoglu committed Feb 19, 2019
1 parent 49f206f commit ed25558
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
46 changes: 46 additions & 0 deletions LibreNMS/DB/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use LibreNMS\Config;
use Symfony\Component\Yaml\Yaml;
use \Schema as LaravelSchema;

class Schema
{
Expand All @@ -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
*
Expand Down
9 changes: 7 additions & 2 deletions LibreNMS/Validations/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand All @@ -49,11 +50,15 @@ public function validate(Validator $validator)
$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).",
Expand Down
33 changes: 33 additions & 0 deletions app/Console/MigrateInstallCommand.php
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions app/Providers/CliServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Console\MigrateInstallCommand;
use Illuminate\Foundation\Providers\ArtisanServiceProvider;

class CliServiceProvider extends ArtisanServiceProvider
Expand All @@ -12,6 +13,7 @@ public function register()
if (defined('LIBRENMS_CLI') && $this->app->environment() == 'production') {
$this->commands = array_intersect_key($this->commands, [
"Migrate" => true,
"MigrateInstall" => true,
]);

$this->registerCommands($this->commands);
Expand All @@ -28,4 +30,12 @@ protected function registerModelMakeCommand()
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']);
});
}
}
8 changes: 7 additions & 1 deletion html/includes/output/db-update.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@

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();
Expand Down

0 comments on commit ed25558

Please sign in to comment.