add command nexus:update

This commit is contained in:
xiaomlove
2022-05-13 14:40:59 +08:00
parent 5110ef6e7d
commit 6eeac797a4
5 changed files with 178 additions and 24 deletions
+18 -1
View File
@@ -2,6 +2,7 @@
namespace Nexus\Database;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Facades\Cache;
@@ -262,7 +263,7 @@ class NexusDB
$connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
}
public static function schema(): \Illuminate\Database\Schema\Builder
private static function schema(): \Illuminate\Database\Schema\Builder
{
if (IN_NEXUS) {
return Capsule::schema(self::ELOQUENT_CONNECTION_NAME);
@@ -270,6 +271,22 @@ class NexusDB
throw new \RuntimeException('can not call this when not in nexus.');
}
public static function hasTable($table): bool
{
if (IN_NEXUS) {
return self::schema()->hasTable($table);
}
return Schema::hasTable($table);
}
public static function hasColumn($table, $column): bool
{
if (IN_NEXUS) {
return self::schema()->hasColumn($table, $column);
}
return Schema::hasColumn($table, $column);
}
public static function table($table): \Illuminate\Database\Query\Builder
{
if (IN_NEXUS) {
+28 -13
View File
@@ -2,6 +2,7 @@
namespace Nexus\Install;
use App\Models\Setting;
use App\Models\User;
use App\Repositories\UserRepository;
use Illuminate\Support\Str;
@@ -44,7 +45,9 @@ class Install
if (!session_id()) {
session_start();
}
$this->checkLock();
if (!$this->runningInConsole()) {
$this->checkLock();
}
$this->currentStep = min(intval($_REQUEST['step'] ?? 1) ?: 1, count($this->steps) + 1);
}
@@ -58,6 +61,11 @@ class Install
return $this->currentStep;
}
public function runningInConsole(): bool
{
return php_sapi_name() == 'cli';
}
public function canAccessStep($step)
{
for ($i = 1; $i < $step; $i++) {
@@ -144,9 +152,10 @@ class Install
{
$gdInfo = function_exists('gd_info') ? gd_info() : [];
$tableRows = [];
$phpVersionRequire = '>= ' . $this->minimumPhpVersion;
$tableRows[] = [
'label' => 'PHP version',
'required' => '>= ' . $this->minimumPhpVersion,
'required' => $phpVersionRequire,
'current' => PHP_VERSION,
'result' => $this->yesOrNo(version_compare(PHP_VERSION, $this->minimumPhpVersion, '>=')),
];
@@ -188,13 +197,13 @@ class Install
];
}
$fails = array_filter($tableRows, function ($value) {return in_array($value['required'], ['true', 'enabled']) && $value['result'] == 'NO';});
$fails = array_filter($tableRows, function ($value) use ($phpVersionRequire) {
return in_array($value['required'], ['true', 'enabled', $phpVersionRequire]) && $value['result'] == 'NO';
});
$pass = empty($fails);
return [
'table_rows' => $tableRows,
'fails' => $fails,
'pass' => $pass,
];
}
@@ -231,7 +240,7 @@ class Install
require $originalConfigFile;
$settings = require $defaultSettingsFile;
$settingsFromDb = [];
if (NexusDB::schema()->hasTable('settings') && get_row_count('settings') > 0) {
if (NexusDB::hasTable('settings') && Setting::query()->count() > 0) {
$settingsFromDb = get_setting();
}
$this->doLog("settings form db: " . json_encode($settingsFromDb));
@@ -278,6 +287,7 @@ class Install
'table_rows' => $tableRows,
'symbolic_links' => $symbolicLinks,
'settings' => $settings,
'fails' => $fails,
'pass' => $pass,
];
}
@@ -290,8 +300,13 @@ class Install
public function gotoStep($step)
{
nexus_redirect(getBaseUrl() . "?step=$step");
die(0);
if ($this->runningInConsole()) {
$this->currentStep = $step;
} else {
nexus_redirect(getBaseUrl() . "?step=$step");
die(0);
}
}
public function maxStep()
@@ -524,7 +539,7 @@ class Install
public function saveSettings($settings)
{
if (!NexusDB::schema()->hasTable('settings')) {
if (!NexusDB::hasTable('settings')) {
$this->runMigrate('database/migrations/2021_06_08_113437_create_settings_table.php');
}
foreach ($settings as $prefix => $group) {
@@ -635,9 +650,9 @@ class Install
public function getMysqlVersionInfo(): array
{
$res = mysql_query('select version() as v');
$result = mysql_fetch_assoc($res);
$version = $result['v'];
$sql = 'select version() as v';
$result = NexusDB::select($sql);
$version = $result[0]['v'];
$match = version_compare($version, '5.7.7', '>=');
return compact('version', 'match');
}
+9 -9
View File
@@ -99,7 +99,7 @@ class Update extends Install
$this->doLog("[ADD CUSTOM FIELD MENU] insert: " . json_encode($insert) . " to table: $table, id: $id");
}
//since beta8
if (WITH_LARAVEL && !NexusDB::schema()->hasColumn('categories', 'icon_id')) {
if (WITH_LARAVEL && !NexusDB::hasColumn('categories', 'icon_id')) {
$this->doLog('[INIT CATEGORY ICON_ID]');
$this->runMigrate('database/migrations/2022_03_08_040415_add_icon_id_to_categories_table.php');
$icon = Icon::query()->orderBy('id', 'asc')->first();
@@ -108,7 +108,7 @@ class Update extends Install
}
}
//fix base url, since beta8
if (WITH_LARAVEL && NexusDB::schema()->hasTable('settings')) {
if (WITH_LARAVEL && NexusDB::hasTable('settings')) {
$settingBasic = get_setting('basic');
if (isset($settingBasic['BASEURL']) && Str::startsWith($settingBasic['BASEURL'], 'localhost')) {
$this->doLog('[RESET CONFIG basic.BASEURL]');
@@ -137,11 +137,11 @@ class Update extends Install
* attendance change, do migrate
*/
if (WITH_LARAVEL) {
if (!NexusDB::schema()->hasTable('attendance')) {
if (!NexusDB::hasTable('attendance')) {
//no table yet, no need to migrate
$this->runMigrate('database/migrations/2021_06_08_113437_create_attendance_table.php');
}
if (!NexusDB::schema()->hasColumn('attendance', 'total_days')) {
if (!NexusDB::hasColumn('attendance', 'total_days')) {
$this->runMigrate('database/migrations/2021_06_13_215440_add_total_days_to_attendance_table.php');
$attendanceRep = new AttendanceRepository();
$count = $attendanceRep->migrateAttendance();
@@ -154,7 +154,7 @@ class Update extends Install
*
* add seed points to user
*/
if (WITH_LARAVEL && !NexusDB::schema()->hasColumn('users', 'seed_points')) {
if (WITH_LARAVEL && !NexusDB::hasColumn('users', 'seed_points')) {
$this->runMigrate('database/migrations/2021_06_24_013107_add_seed_points_to_users_table.php');
//Don't do this, initial seed points = 0;
// $result = $this->initSeedPoints();
@@ -166,7 +166,7 @@ class Update extends Install
*
* add id to agent_allowed_exception
*/
if (WITH_LARAVEL && !NexusDB::schema()->hasColumn('agent_allowed_exception', 'id')) {
if (WITH_LARAVEL && !NexusDB::hasColumn('agent_allowed_exception', 'id')) {
$this->runMigrate('database/migrations/2022_02_25_021356_add_id_to_agent_allowed_exception_table.php');
$this->doLog("[ADD_ID_TO_AGENT_ALLOWED_EXCEPTION]");
}
@@ -176,7 +176,7 @@ class Update extends Install
*
* init tag
*/
if (WITH_LARAVEL && !NexusDB::schema()->hasTable('tags')) {
if (WITH_LARAVEL && !NexusDB::hasTable('tags')) {
$this->runMigrate('database/migrations/2022_03_07_012545_create_tags_table.php');
$this->initTag();
$this->doLog("[INIT_TAG]");
@@ -205,7 +205,7 @@ class Update extends Install
*
* add attendance_card to users
*/
if (WITH_LARAVEL && !NexusDB::schema()->hasColumn('users', 'attendance_card')) {
if (WITH_LARAVEL && !NexusDB::hasColumn('users', 'attendance_card')) {
$this->runMigrate('database/migrations/2022_04_02_163930_create_attendance_logs_table.php');
$this->runMigrate('database/migrations/2022_04_03_041642_add_attendance_card_to_users_table.php');
$rep = new AttendanceRepository();
@@ -232,7 +232,7 @@ class Update extends Install
$this->doLog(__METHOD__ . ", laravel is not available");
return;
}
if (NexusDB::schema()->hasColumn('torrents', 'tags')) {
if (NexusDB::hasColumn('torrents', 'tags')) {
if (Torrent::query()->where('tags', '>', 0)->count() > 0 && TorrentTag::query()->count() == 0) {
$this->doLog("[MIGRATE_TORRENT_TAG]...");
$tagRep = new TagRepository();