2012-02-16 12:19:40 +00:00
|
|
|
<?php
|
2019-01-14 07:44:23 -05:00
|
|
|
/**
|
|
|
|
* update.php
|
|
|
|
*
|
|
|
|
* Database update script
|
2012-02-16 12:19:40 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2019-01-14 07:44:23 -05:00
|
|
|
* 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
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-01-14 07:44:23 -05:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2012, Observium Developers - http://www.observium.org
|
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2019-01-14 07:44:23 -05:00
|
|
|
* @copyright 2017-2018 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
2012-02-16 12:19:40 +00:00
|
|
|
*/
|
2017-11-24 03:37:52 -06:00
|
|
|
if (! isset($init_modules) && php_sapi_name() == 'cli') {
|
2012-02-16 23:18:49 +00:00
|
|
|
// Not called from within discovery, let's load up the necessary stuff.
|
2019-03-14 08:06:27 -05:00
|
|
|
$init_modules = [];
|
2016-11-21 14:12:59 -06:00
|
|
|
require realpath(__DIR__ . '/../..') . '/includes/init.php';
|
2012-02-16 12:19:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 03:37:52 -06:00
|
|
|
$return = 0;
|
2017-07-18 14:07:21 -05:00
|
|
|
|
2020-10-07 07:36:35 -05:00
|
|
|
// make sure the cache_locks table exists before attempting to use a db lock
|
|
|
|
if (config('cache.default') == 'database' && ! \Schema::hasTable('cache_locks')) {
|
|
|
|
$skip_schema_lock = true;
|
|
|
|
}
|
2017-07-17 13:35:08 -05:00
|
|
|
|
2020-10-07 07:36:35 -05:00
|
|
|
$schemaLock = Cache::lock('schema', 86000);
|
|
|
|
if (! empty($skip_schema_lock) || $schemaLock->get()) {
|
2021-05-13 07:18:54 -05:00
|
|
|
$db_rev = \LibreNMS\DB\Schema::getLegacySchema();
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2019-01-14 07:44:23 -05:00
|
|
|
$migrate_opts = ['--force' => true, '--ansi' => true];
|
2017-07-01 15:28:29 -05:00
|
|
|
|
2019-01-14 07:44:23 -05:00
|
|
|
if ($db_rev === 0) {
|
|
|
|
$migrate_opts['--seed'] = true;
|
|
|
|
$return = Artisan::call('migrate', $migrate_opts);
|
|
|
|
echo Artisan::output();
|
2019-01-17 11:37:14 -06:00
|
|
|
} elseif ($db_rev < 1000) {
|
2019-01-14 07:44:23 -05:00
|
|
|
// legacy update
|
|
|
|
d_echo("DB Schema update started....\n");
|
|
|
|
|
2017-07-17 13:35:08 -05:00
|
|
|
// Set Database Character set and Collation
|
2018-04-11 10:15:13 -05:00
|
|
|
dbQuery('ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_unicode_ci;');
|
2017-07-01 15:28:29 -05:00
|
|
|
|
2019-01-14 07:44:23 -05:00
|
|
|
echo "-- Updating database schema\n";
|
2017-07-17 13:35:08 -05:00
|
|
|
foreach (get_schema_list() as $file_rev => $file) {
|
|
|
|
if ($file_rev > $db_rev) {
|
|
|
|
printf('%03d -> %03d ...', $db_rev, $file_rev);
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2017-07-17 13:35:08 -05:00
|
|
|
$err = 0;
|
2017-12-01 14:10:32 -06:00
|
|
|
if (($data = file_get_contents($file)) !== false) {
|
2017-07-17 13:35:08 -05:00
|
|
|
foreach (explode("\n", $data) as $line) {
|
|
|
|
if (trim($line)) {
|
|
|
|
d_echo("$line \n");
|
2017-07-01 15:28:29 -05:00
|
|
|
|
2017-07-17 13:35:08 -05:00
|
|
|
if ($line[0] != '#') {
|
2018-08-17 15:29:20 -05:00
|
|
|
if (! dbQuery($line)) {
|
2017-11-24 03:37:52 -06:00
|
|
|
$return = 2;
|
2017-07-17 13:35:08 -05:00
|
|
|
$err++;
|
|
|
|
}
|
2017-07-01 15:28:29 -05:00
|
|
|
}
|
2012-02-16 12:19:40 +00:00
|
|
|
}
|
2015-07-13 20:10:26 +02:00
|
|
|
}
|
2012-02-16 12:19:40 +00:00
|
|
|
|
2017-07-17 13:35:08 -05:00
|
|
|
echo " done ($err errors).\n";
|
|
|
|
} else {
|
|
|
|
echo " Could not open file! $file\n";
|
|
|
|
$return = 1;
|
|
|
|
}//end if
|
|
|
|
|
2019-01-14 07:44:23 -05:00
|
|
|
if ($db_rev == 0) {
|
|
|
|
dbInsert(['version' => $file_rev], 'dbSchema');
|
2017-07-17 13:35:08 -05:00
|
|
|
} else {
|
2019-01-14 07:44:23 -05:00
|
|
|
dbUpdate(['version' => $file_rev], 'dbSchema');
|
2017-07-17 13:35:08 -05:00
|
|
|
}
|
2019-01-14 07:44:23 -05:00
|
|
|
$db_rev = $file_rev;
|
2017-07-01 15:28:29 -05:00
|
|
|
}//end if
|
2017-07-17 13:35:08 -05:00
|
|
|
}//end foreach
|
2017-07-01 15:28:29 -05:00
|
|
|
|
2019-01-14 07:44:23 -05:00
|
|
|
echo "-- Done\n";
|
|
|
|
// end legacy update
|
2021-05-13 07:18:54 -05:00
|
|
|
$db_rev = \LibreNMS\DB\Schema::getLegacySchema();
|
2019-01-17 11:37:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($db_rev == 1000) {
|
|
|
|
$return = Artisan::call('migrate', $migrate_opts);
|
|
|
|
echo Artisan::output();
|
2015-09-01 23:38:10 +01:00
|
|
|
}
|
2017-05-03 14:48:23 +01:00
|
|
|
|
2020-10-07 07:36:35 -05:00
|
|
|
$schemaLock->release();
|
2017-07-17 13:35:08 -05:00
|
|
|
}
|