Laravel migrations (#8868)

* Add migrations and seeds

* Fix spacing validation issues

* Fix linting

* Update tabs to spaces

* Update daily and install process

* Make build-base.php use the new migrations if empty or at dbschema 1000.
Seed 1000 into the database.
Temp fix for the route table index length (this table basically isn't used...)

* Fix typo in seed.
Hard code legacy schema checks to version 1000 (999 would have worked as is)

* Port association table no longer exists

* Make database validate again

* DB schema, remove as many DB::statement as possible

* update migrations
add librenms cli entry point (artisan)
update validate to check laravel migrations

* remove statements from users migration

* Fix up daily.sh and the 1000 migration

* Update migrations to current state
Take advantage of environment variables to set DB credentials.

* Fix style issues

* Update schema

* fix test db collation

* Fix migration table definition

* update db migrations

* Update migrations

* Update stats callback.  Just count the total migrations applied.

* Update 1000.sql.

* update migrations

* remove the graph type seeder, it is no longer needed

* update docs

* fix whitespace

* remove extra schema

* update tests

* fix sort

* add message

* dbSchema should actually be 1000

* add character set to db create

* Fix some artisan issues

* Update schema
This commit is contained in:
Paul Heinrichs
2019-01-14 07:44:23 -05:00
committed by Tony Murray
parent 29f8a8d4a3
commit ebe2ecf524
151 changed files with 6136 additions and 178 deletions

View File

@@ -1,17 +1,31 @@
<?php
/*
* LibreNMS Network Management and Monitoring System
* Copyright (C) 2006-2012, Observium Developers - http://www.observium.org
/**
* update.php
*
* Database update script
*
* 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.
*
* See COPYING for more details.
* 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/>.
*
* Copyright (C) 2006-2012, Observium Developers - http://www.observium.org
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017-2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
use Artisan;
use LibreNMS\Config;
use LibreNMS\Exceptions\DatabaseConnectException;
use LibreNMS\Exceptions\LockException;
@@ -20,7 +34,7 @@ use LibreNMS\Util\MemcacheLock;
if (!isset($init_modules) && php_sapi_name() == 'cli') {
// Not called from within discovery, let's load up the necessary stuff.
$init_modules = [];
$init_modules = ['laravel'];
require realpath(__DIR__ . '/../..') . '/includes/init.php';
}
@@ -35,51 +49,27 @@ try {
}
}
// only import build.sql to an empty database
$tables = dbFetchRows("SHOW TABLES");
$db_rev = get_db_schema();
if (empty($tables)) {
echo "-- Creating base database structure\n";
$step = 0;
$sql_fh = fopen('build.sql', 'r');
if ($sql_fh === false) {
echo 'ERROR: Cannot open SQL build script build.sql' . PHP_EOL;
$return = 1;
}
$migrate_opts = ['--force' => true, '--ansi' => true];
while (!feof($sql_fh)) {
$line = fgetss($sql_fh);
echo 'Step #' . $step++ . ' ...' . PHP_EOL;
if (!empty($line)) {
if (!dbQuery($line)) {
$return = 1;
}
}
}
fclose($sql_fh);
}
d_echo("DB Schema update started....\n");
if (db_schema_is_current()) {
d_echo("DB Schema already up to date.\n");
if ($db_rev === 0) {
$migrate_opts['--seed'] = true;
$return = Artisan::call('migrate', $migrate_opts);
echo Artisan::output();
} elseif ($db_rev == 1000) {
$return = Artisan::call('migrate', $migrate_opts);
echo Artisan::output();
} else {
// legacy update
d_echo("DB Schema update started....\n");
// Set Database Character set and Collation
dbQuery('ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_unicode_ci;');
$db_rev = get_db_schema();
$insert = ($db_rev == 0); // if $db_rev == 0, insert the first update
$updating = 0;
echo "-- Updating database schema\n";
foreach (get_schema_list() as $file_rev => $file) {
if ($file_rev > $db_rev) {
if (!$updating) {
echo "-- Updating database schema\n";
}
printf('%03d -> %03d ...', $db_rev, $file_rev);
$err = 0;
@@ -103,20 +93,17 @@ try {
$return = 1;
}//end if
$updating++;
$db_rev = $file_rev;
if ($insert) {
dbInsert(array('version' => $db_rev), 'dbSchema');
$insert = false;
if ($db_rev == 0) {
dbInsert(['version' => $file_rev], 'dbSchema');
} else {
dbUpdate(array('version' => $db_rev), 'dbSchema');
dbUpdate(['version' => $file_rev], 'dbSchema');
}
$db_rev = $file_rev;
}//end if
}//end foreach
if ($updating) {
echo "-- Done\n";
}
echo "-- Done\n";
// end legacy update
}
if (isset($schemaLock)) {