diff --git a/_install/install_nntmux.php b/_install/install_nntmux.php index f0aa7ec11..e094e5e65 100644 --- a/_install/install_nntmux.php +++ b/_install/install_nntmux.php @@ -7,8 +7,8 @@ use nntmux\ColorCLI; use App\Models\Settings; use App\Extensions\util\Versions; -if (! defined('NN_INSTALLER')) { - define('NN_INSTALLER', true); +if (! \defined('NN_INSTALLER')) { + \define('NN_INSTALLER', true); } $error = false; @@ -18,16 +18,22 @@ if (file_exists(NN_ROOT.'_install/install.lock')) { exit(); } +if (! $error) { // Check if user selected right DB type. -if (env('DB_SYSTEM') !== 'mysql') { - ColorCLI::doEcho(ColorCLI::error('Invalid database system. Must be: mysql ; Not: '.env('DB_SYSTEM'))); + if (env('DB_SYSTEM') !== 'mysql') { + ColorCLI::doEcho(ColorCLI::error('Invalid database system. Must be: mysql ; Not: '.env('DB_SYSTEM'))); + $error = true; + } +} + +if (!(new Settings())->isDbVersionAtLeast(NN_MINIMUM_MARIA_VERSION) || !(new Settings())->isDbVersionAtLeast(NN_MINIMUM_MYSQL_VERSION)) { + ColorCLI::doEcho(ColorCLI::error('Version of MariaDB used is lower than required version: ' . NN_MINIMUM_MARIA_VERSION)); $error = true; } // Start inserting data into the DB. if (! $error) { ColorCLI::doEcho(ColorCLI::header('Migrating tables and populating them')); - passthru('php '.NN_ROOT.'artisan migrate:fresh'); - passthru('php '.NN_ROOT.'artisan db:seed'); + passthru('php '.NN_ROOT.'artisan migrate:fresh --seed'); } // Check one of the standard tables was created and has data. $ver = new Versions(); diff --git a/app/Models/Settings.php b/app/Models/Settings.php index 54a342eb0..f8c98b768 100644 --- a/app/Models/Settings.php +++ b/app/Models/Settings.php @@ -310,10 +310,10 @@ class Settings extends Model */ private function fetchDbVersion() { - $pdo = DB::connection()->getPdo(); - $result = $pdo->exec('SELECT VERSION() AS version'); + $result = DB::select('SELECT VERSION() AS version'); + if (! empty($result)) { - $dummy = explode('-', $result['version'], 2); + $dummy = explode('-', $result[0]->version, 2); $this->dbVersion = $dummy[0]; } } diff --git a/database/migrations/2018_01_22_215526_add_triggers.php b/database/migrations/2018_01_22_215526_add_triggers.php new file mode 100644 index 000000000..f60ab8484 --- /dev/null +++ b/database/migrations/2018_01_22_215526_add_triggers.php @@ -0,0 +1,89 @@ +config = new Configure('install'); + $error = false; - $pdo = new DB(); - $error = false; - // Connect to the SQL server. - try { - // HAS to be DB because settings table does not exist yet. - $pdo = new DB( - [ - 'checkVersion' => true, - 'createDb' => true, - 'dbhost' => env('DB_HOST'), - 'dbname' => env('DB_NAME'), - 'dbpass' => env('DB_PASSWORD'), - 'dbport' => env('DB_PORT'), - 'dbsock' => env('DB_SOCKET'), - 'dbtype' => env('DB_SYSTEM'), - 'dbuser' => env('DB_USER'), - ] - ); - } catch (\PDOException $e) { - ColorCLI::doEcho(ColorCLI::error('Unable to connect to MySQL server.')); - $error = true; - } catch (\RuntimeException $e) { - switch ($e->getCode()) { - case 1: - case 2: - case 3: - $error = true; - ColorCLI::doEcho(ColorCLI::alternate($e->getMessage())); - break; - default: - throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); - } - } + passthru('php '.NN_ROOT.'artisan migrate:fresh --seed'); - // Check if the MySQL version is correct. - if (!$error) { - try { - $goodVersion = $pdo->isDbVersionAtLeast(NN_MINIMUM_MYSQL_VERSION); - } catch (\PDOException $e) { - $goodVersion = false; - $error = true; - ColorCLI::doEcho(ColorCLI::error('Could not get version from MySQL server.')); - } + // Check one of the standard tables was created and has data. + $ver = new Versions(); + $patch = $ver->getSQLPatchFromFile(); + $updateSettings = false; + if ($patch > 0) { + $updateSettings = Settings::query()->where(['section' => '', 'subsection' => '', 'name' => 'sqlpatch'])->update(['value' => $patch]); + } + // If it all worked, continue the install process. + if ($updateSettings === 0) { + $message = 'Database updated successfully'; + } else { + $error = true; + $message = 'Could not update sqlpatch to '.$patch.' for your database.'; + } - if ($goodVersion === false) { - $error = true; - ColorCLI::doEcho(ColorCLI::error( - 'You are using an unsupported version of ' . - env('DB_SYSTEM') . - ' the minimum allowed version is ' . - NN_MINIMUM_MYSQL_VERSION - ) - ); - } - } + if (! $error) { -// Start inserting data into the DB. - if (!$error) { - $DbSetup = new DbUpdate( - [ - 'backup' => false, - 'db' => $pdo, - ] - ); - $pdo->exec('SET FOREIGN_KEY_CHECKS=0;'); + $covers_path = NN_RES.'covers'.DS; + $nzb_path = NN_RES.'nzb'.DS; + $tmp_path = NN_RES.'tmp'.DS; + $unrar_path = $tmp_path.'unrar'.DS; - $DbSetup->processSQLFile(); // Setup default schema - //Insert admin user into database - if (env('ADMIN_USER') === '' || env('ADMIN_PASS') === '' || env('ADMIN_EMAIL') === '') { - ColorCLI::doEcho(ColorCLI::error('Admin user data cannot be empty! Please edit .env file and fill in admin user details and run this script again!')); - exit(); + $nzbPathCheck = is_writable($nzb_path); + if ($nzbPathCheck === false) { + $error = true; + $message = $nzb_path.' is not writable. Please fix folder permissions'; } - $pdo->queryExec(sprintf('INSERT INTO users (username, email, password, user_roles_id, created_at) VALUES (%s, %s, %s, 2, NOW())', $pdo->escapeString(env('ADMIN_USER')), $pdo->escapeString(env('ADMIN_EMAIL')), $pdo->escapeString(User::hashPassword(env('ADMIN_PASS'))))); - ColorCLI::doEcho(ColorCLI::header('Migrating tables and populating them')); - passthru('php '.NN_ROOT.'artisan migrate'); - passthru('php '.NN_ROOT.'artisan db:seed'); - if (!$error) { - // Check one of the standard tables was created and has data. - $dbInstallWorked = false; - $reschk = $pdo->query('SELECT COUNT(id) AS num FROM tmux'); - if ($reschk === false) { - $error = true; - ColorCLI::doEcho(ColorCLI::warningOver('Could not select data from your database, check that tables and data are properly created/inserted.')); - } else { - foreach ($reschk as $row) { - if ($row['num'] > 0) { - $dbInstallWorked = true; - break; - } - } - } - $ver = new Versions(); - $patch = $ver->getSQLPatchFromFile(); - if ($dbInstallWorked) { - $updateSettings = false; - if ($patch > 0) { - $updateSettings = $pdo->queryExec( - "UPDATE settings SET value = '$patch' WHERE section = '' AND subsection = '' AND name = 'sqlpatch'" - ); - } - // If it all worked, continue the install process. - if ($updateSettings) { - $message = 'Database updated successfully'; - echo $message . PHP_EOL; - } else { - $error = true; - $message = 'Could not update sqlpatch to ' . $patch . ' for your database.'; - echo $message . PHP_EOL; - } - } else { - $error = true; - ColorCLI::doEcho(ColorCLI::warning('Could not select data from your database.')); - } - } - } + $lastchar = substr($nzb_path, \strlen($nzb_path) - 1); + if ($lastchar !== '/') { + $nzb_path .= '/'; + } - if (!$error) { - $covers_path = NN_RES . 'covers' . DS; - $nzb_path = NN_RES . 'nzb' . DS; - $tmp_path = NN_RES . 'tmp' . DS; - $unrar_path = $tmp_path . 'unrar' . DS; + if (! file_exists($unrar_path)) { + if (! @mkdir($unrar_path) && ! is_dir($unrar_path)) { + throw new \RuntimeException('Unable to create '.$unrar_path.' folder'); + } + } + $unrarPathCheck = is_writable($unrar_path); + if ($unrarPathCheck === false) { + $error = true; + $message = $unrar_path.' is not writable. Please fix folder permissions'; + } + $lastchar = substr($unrar_path, \strlen($unrar_path) - 1); + if ($lastchar !== '/') { + $unrar_path .= '/'; + } - $nzbPathCheck = is_writable($nzb_path); - if ($nzbPathCheck === false) { - $error = true; - $message = $nzb_path . ' is not writable. Please fix folder permissions'; - echo $message . PHP_EOL; - } + $coversPathCheck = is_writable($covers_path); + if ($coversPathCheck === false) { + $error = true; + $message = $covers_path.' is not writable. Please fix folder permissions'; + } - $lastchar = substr($nzb_path, \strlen($nzb_path) - 1); - if ($lastchar !== '/') { - $nzb_path .= '/'; - } + $lastchar = substr($covers_path, \strlen($covers_path) - 1); + if ($lastchar !== '/') { + $covers_path .= '/'; + } - if (!file_exists($unrar_path)) { - ColorCLI::doEcho(ColorCLI::primary('Creating missing ' . $unrar_path . ' folder')); - if (!@mkdir($unrar_path) && !is_dir($unrar_path)) { - throw new \RuntimeException('Unable to create ' . $unrar_path . ' folder'); - } - $message = 'Folder ' . $unrar_path . ' successfully created'; - echo $message; - } - $unrarPathCheck = is_writable($unrar_path); - if ($unrarPathCheck === false) { - $error = true; - $message = $unrar_path . ' is not writable. Please fix folder permissions'; - echo $message . PHP_EOL; - } + if (! $error) { + $sql1 = Settings::query()->where('setting', '=', 'nzbpath')->update(['value' => $nzb_path]); + $sql2 = Settings::query()->where('setting', '=', 'tmpunrarpath')->update(['value' => $unrar_path]); + $sql3 = Settings::query()->where('setting', '=', 'coverspath')->update(['value' => $covers_path]); + if ($sql1 === null || $sql2 === null || $sql3 === null) { + $error = true; + } else { + $message = 'Settings table updated successfully'; + } + } + } - $lastchar = substr($unrar_path, \strlen($unrar_path) - 1); - if ($lastchar !== '/') { - $unrar_path .= '/'; - } + if (! $error) { - $coversPathCheck = is_writable($covers_path); - if ($coversPathCheck === false) { - $error = true; - $message = $covers_path . ' is not writable. Please fix folder permissions'; - echo $message . PHP_EOL; - } + User::add(env('ADMIN_USER'), env('ADMIN_PASS'), env('ADMIN_EMAIL'), 2, '', '', '', ''); - $lastchar = substr($covers_path, \strlen($covers_path) - 1); - if ($lastchar !== '/') { - $covers_path .= '/'; - } - - if (!$error) { - - $sql1 = sprintf("UPDATE settings SET value = %s WHERE setting = 'nzbpath'", $pdo->escapeString($nzb_path)); - $sql2 = sprintf("UPDATE settings SET value = %s WHERE setting = 'tmpunrarpath'", $pdo->escapeString($unrar_path)); - $sql3 = sprintf("UPDATE settings SET value = %s WHERE setting = 'coverspath'", $pdo->escapeString($covers_path)); - if ($pdo->queryExec($sql1) === false || $pdo->queryExec($sql2) === false || $pdo->queryExec($sql3) === false) { - $error = true; - } else { - $message = 'Settings table updated successfully'; - echo $message . PHP_EOL; - } - } - } - - if (!$error) { - @file_put_contents(NN_ROOT . '_install/install.lock', ''); - $message = 'NNTmux installation completed successfully'; - $pdo->exec('SET FOREIGN_KEY_CHECKS=1;'); - echo $message . PHP_EOL; - } else { - $message = 'NNTmux installation failed. Please fix reported problems and run this script again'; - echo $message . PHP_EOL; - } + @file_put_contents(NN_ROOT.'_install/install.lock', ''); + passthru('php '.NN_ROOT.'artisan key:generate'); + $message = 'NNTmux installation completed successfully'; + } $this->assertEquals('NNTmux installation completed successfully', $message, 'Test Failed'); } }