mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-28 06:57:22 +08:00
add seeders and migrations + rhilip/bencode
This commit is contained in:
@@ -230,6 +230,11 @@ class NexusDB
|
||||
return Capsule::table($table);
|
||||
}
|
||||
|
||||
public static function transaction(\Closure $callback, $attempts = 1)
|
||||
{
|
||||
return Capsule::connection(self::ELOQUENT_CONNECTION_NAME)->transaction($callback, $attempts);
|
||||
}
|
||||
|
||||
public static function getMysqlColumnInfo($table, $column)
|
||||
{
|
||||
static $driver;
|
||||
|
||||
@@ -18,7 +18,7 @@ class Install
|
||||
protected $initializeTables = [
|
||||
'adminpanel', 'agent_allowed_exception', 'agent_allowed_family', 'allowedemails', 'audiocodecs', 'bannedemails', 'categories',
|
||||
'caticons', 'codecs', 'countries', 'downloadspeed', 'faq', 'isp', 'language', 'media', 'modpanel', 'processings', 'rules', 'schools',
|
||||
'searchbox', 'secondicons', 'sources', 'standards', 'stylesheets', 'sysoppanel', 'teams', 'torrents_state', 'uploadspeed', 'agent_allowed_family',
|
||||
'searchbox', 'secondicons', 'sources', 'standards', 'stylesheets', 'sysoppanel', 'teams', 'torrents_state', 'uploadspeed',
|
||||
];
|
||||
|
||||
protected $envNames = ['DB_HOST', 'DB_PORT', 'DB_USERNAME', 'DB_PASSWORD', 'DB_DATABASE', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DB'];
|
||||
@@ -99,6 +99,19 @@ class Install
|
||||
return array_column($matches, 0, 1);
|
||||
}
|
||||
|
||||
public function listAllTableCreateFromMigrations()
|
||||
{
|
||||
$tables = [];
|
||||
foreach (glob(ROOT_PATH . "database/migrations/*.php") as $path) {
|
||||
$filename = basename($path);
|
||||
$count = preg_match('/create_(.*)_table.php/', $filename, $matches);
|
||||
if ($count) {
|
||||
$tables[$matches[1]] = '';
|
||||
}
|
||||
}
|
||||
return $tables;
|
||||
}
|
||||
|
||||
public function listExistsTable()
|
||||
{
|
||||
$sql = 'show tables';
|
||||
@@ -444,7 +457,8 @@ class Install
|
||||
public function listShouldCreateTable()
|
||||
{
|
||||
$existsTable = $this->listExistsTable();
|
||||
$tableCreate = $this->listAllTableCreate();
|
||||
// $tableCreate = $this->listAllTableCreate();
|
||||
$tableCreate = $this->listAllTableCreateFromMigrations();
|
||||
$shouldCreateTable = [];
|
||||
foreach ($tableCreate as $table => $sql) {
|
||||
if (in_array($table, $existsTable)) {
|
||||
@@ -482,7 +496,7 @@ class Install
|
||||
}
|
||||
$linkResult = symlink($path, $linkName);
|
||||
if ($linkResult === false) {
|
||||
throw new \RuntimeException("can't not make symbolic link: $linkName -> $path");
|
||||
throw new \RuntimeException("can not make symbolic link: $linkName -> $path");
|
||||
}
|
||||
$this->doLog("[CREATE SYMBOLIC LINK] success make symbolic link: $linkName -> $path");
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
<?php
|
||||
ini_set('error_reporting', E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
$rootpath = dirname(dirname(__DIR__)) . '/';
|
||||
define('ROOT_PATH', $rootpath);
|
||||
define('IN_NEXUS', true);
|
||||
$isPost = $_SERVER['REQUEST_METHOD'] == 'POST';
|
||||
require $rootpath . 'vendor/autoload.php';
|
||||
require $rootpath . 'nexus/Database/helpers.php';
|
||||
require ROOT_PATH . 'nexus/Install/install_update_start.php';
|
||||
|
||||
$isPost = $_SERVER['REQUEST_METHOD'] == 'POST';
|
||||
$install = new \Nexus\Install\Install();
|
||||
$currentStep = $install->currentStep();
|
||||
$maxStep = $install->maxStep();
|
||||
@@ -28,7 +24,7 @@ if ($currentStep == 1) {
|
||||
|
||||
if ($currentStep == 2) {
|
||||
$envExampleFile = $rootpath . ".env.example";
|
||||
$dbstructureFile = $rootpath . "_db/dbstructure_v1.6.sql";
|
||||
// $dbstructureFile = $rootpath . "_db/dbstructure_v1.6.sql";
|
||||
$envExampleData = readEnvFile($envExampleFile);
|
||||
$envFormControls = $install->listEnvFormControls();
|
||||
$newData = array_column($envFormControls, 'value', 'name');
|
||||
@@ -49,12 +45,12 @@ if ($currentStep == 2) {
|
||||
'current' => $envExampleFile,
|
||||
'result' => $install->yesOrNo(file_exists($envExampleFile) && is_readable($envExampleFile)),
|
||||
],
|
||||
[
|
||||
'label' => basename($dbstructureFile),
|
||||
'required' => 'exists && readable',
|
||||
'current' => $dbstructureFile,
|
||||
'result' => $install->yesOrNo(file_exists($dbstructureFile) && is_readable($dbstructureFile)),
|
||||
],
|
||||
// [
|
||||
// 'label' => basename($dbstructureFile),
|
||||
// 'required' => 'exists && readable',
|
||||
// 'current' => $dbstructureFile,
|
||||
// 'result' => $install->yesOrNo(file_exists($dbstructureFile) && is_readable($dbstructureFile)),
|
||||
// ],
|
||||
];
|
||||
$fails = array_filter($tableRows, function ($value) {return $value['result'] == 'NO';});
|
||||
$pass = empty($fails);
|
||||
@@ -64,7 +60,19 @@ if ($currentStep == 3) {
|
||||
$shouldCreateTable = $install->listShouldCreateTable();
|
||||
while ($isPost) {
|
||||
try {
|
||||
$install->createTable($shouldCreateTable);
|
||||
// $install->createTable($shouldCreateTable);
|
||||
if (!WITH_LARAVEL) {
|
||||
throw new \RuntimeException('Larvel is not avaliable.');
|
||||
}
|
||||
$command = "php " . ROOT_PATH . "artisan install:migrate";
|
||||
$result = exec($command, $output, $result_code);
|
||||
$install->doLog(sprintf('command: %s, result_code: %s, result: %s', $command, $result_code, $result));
|
||||
$install->doLog("output: " . json_encode($output));
|
||||
if ($result_code != 0) {
|
||||
throw new \RuntimeException(json_encode($output));
|
||||
} else {
|
||||
$install->doLog("[CREATE TABLE] success.");
|
||||
}
|
||||
$install->nextStep();
|
||||
} catch (\Exception $exception) {
|
||||
$error = $exception->getMessage();
|
||||
@@ -117,7 +125,17 @@ if ($currentStep == 4) {
|
||||
try {
|
||||
$install->createSymbolicLinks($symbolicLinks);
|
||||
$install->saveSettings($settings);
|
||||
$install->importInitialData();
|
||||
// $install->importInitialData();
|
||||
//use seed
|
||||
$command = "php " . ROOT_PATH . "artisan install:init_data";
|
||||
$result = exec($command, $output, $result_code);
|
||||
$install->doLog(sprintf('command: %s, result_code: %s, result: %s', $command, $result_code, $result));
|
||||
$install->doLog("output: " . json_encode($output));
|
||||
if ($result_code != 0) {
|
||||
throw new \RuntimeException(json_encode($output));
|
||||
} else {
|
||||
$install->doLog("[INIT DATA] success.");
|
||||
}
|
||||
$install->nextStep();
|
||||
} catch (\Exception $e) {
|
||||
$error = $e->getMessage();
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
ini_set('error_reporting', E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
define('IN_NEXUS', true);
|
||||
require ROOT_PATH . 'vendor/autoload.php';
|
||||
require ROOT_PATH . 'nexus/Database/helpers.php';
|
||||
require ROOT_PATH . 'include/constants.php';
|
||||
$withLaravel = false;
|
||||
if (file_exists(ROOT_PATH . '.env')) {
|
||||
require ROOT_PATH . 'include/eloquent.php';
|
||||
$withLaravel = true;
|
||||
}
|
||||
define('WITH_LARAVEL', $withLaravel);
|
||||
@@ -1,20 +1,9 @@
|
||||
<?php
|
||||
ini_set('error_reporting', E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
$rootpath = dirname(dirname(__DIR__)) . '/';
|
||||
define('ROOT_PATH', $rootpath);
|
||||
define('IN_NEXUS', true);
|
||||
$isPost = $_SERVER['REQUEST_METHOD'] == 'POST';
|
||||
require $rootpath . 'vendor/autoload.php';
|
||||
require $rootpath . 'nexus/Database/helpers.php';
|
||||
require $rootpath . 'include/constants.php';
|
||||
$withLaravel = false;
|
||||
if (file_exists($rootpath . '.env')) {
|
||||
require $rootpath . 'include/eloquent.php';
|
||||
$withLaravel = true;
|
||||
}
|
||||
define('WITH_LARAVEL', $withLaravel);
|
||||
require ROOT_PATH . 'nexus/Install/install_update_start.php';
|
||||
|
||||
$isPost = $_SERVER['REQUEST_METHOD'] == 'POST';
|
||||
$update = new \Nexus\Install\Update();
|
||||
$currentStep = $update->currentStep();
|
||||
$maxStep = $update->maxStep();
|
||||
@@ -35,7 +24,7 @@ if ($currentStep == 1) {
|
||||
|
||||
if ($currentStep == 2) {
|
||||
$envExampleFile = $rootpath . ".env.example";
|
||||
$dbstructureFile = $rootpath . "_db/dbstructure_v1.6.sql";
|
||||
// $dbstructureFile = $rootpath . "_db/dbstructure_v1.6.sql";
|
||||
$envExampleData = readEnvFile($envExampleFile);
|
||||
$envFormControls = $update->listEnvFormControls();
|
||||
$newData = array_column($envFormControls, 'value', 'name');
|
||||
@@ -56,19 +45,20 @@ if ($currentStep == 2) {
|
||||
'current' => $envExampleFile,
|
||||
'result' => $update->yesOrNo(file_exists($envExampleFile) && is_readable($envExampleFile)),
|
||||
],
|
||||
[
|
||||
'label' => basename($dbstructureFile),
|
||||
'required' => 'exists && readable',
|
||||
'current' => $dbstructureFile,
|
||||
'result' => $update->yesOrNo(file_exists($dbstructureFile) && is_readable($dbstructureFile)),
|
||||
],
|
||||
// [
|
||||
// 'label' => basename($dbstructureFile),
|
||||
// 'required' => 'exists && readable',
|
||||
// 'current' => $dbstructureFile,
|
||||
// 'result' => $update->yesOrNo(file_exists($dbstructureFile) && is_readable($dbstructureFile)),
|
||||
// ],
|
||||
];
|
||||
$fails = array_filter($tableRows, function ($value) {return $value['result'] == 'NO';});
|
||||
$pass = empty($fails);
|
||||
}
|
||||
|
||||
if ($currentStep == 3) {
|
||||
$createTables = $update->listAllTableCreate();
|
||||
// $createTables = $update->listAllTableCreate();
|
||||
$createTables = $update->listAllTableCreateFromMigrations();
|
||||
$existsTables = $update->listExistsTable();
|
||||
$tableRows = [];
|
||||
$toCreateTable = $toAlterTable = $toUpdateTable = [];
|
||||
@@ -84,22 +74,8 @@ if ($currentStep == 3) {
|
||||
$toCreateTable[$table] = $tableCreate;
|
||||
continue;
|
||||
}
|
||||
$tableShouldHaveFields = $update->listTableFieldsFromCreateTable($tableCreate);
|
||||
$tableHaveFields = $update->listTableFieldsFromDb($table);
|
||||
foreach ($tableShouldHaveFields as $field => $fieldCreate) {
|
||||
if (!isset($tableHaveFields[$field])) {
|
||||
//Field not exists
|
||||
$tableRows[] = [
|
||||
"label" => "Field: $table.$field",
|
||||
"required" => "exists",
|
||||
"current" => "",
|
||||
"result" => 'NO',
|
||||
];
|
||||
$toAlterTable[$table][$field] = "add column $fieldCreate";
|
||||
continue;
|
||||
}
|
||||
$fieldInfo = $tableHaveFields[$field];
|
||||
//Field invalid
|
||||
foreach ($tableHaveFields as $field => $fieldInfo) {
|
||||
if ($fieldInfo['Type'] == 'datetime' && $fieldInfo['Default'] == '0000-00-00 00:00:00') {
|
||||
$tableRows[] = [
|
||||
'label' => "Field: $table.$field",
|
||||
@@ -107,45 +83,22 @@ if ($currentStep == 3) {
|
||||
'current' => '0000-00-00 00:00:00',
|
||||
'result' => 'NO',
|
||||
];
|
||||
$toAlterTable[$table][$field] = "modify $fieldCreate";
|
||||
$toAlterTable[$table][$field] = "modify $field datetime default null";
|
||||
$toUpdateTable[$table][$field] = "null";
|
||||
continue;
|
||||
}
|
||||
//Field invalid
|
||||
// if ($fieldInfo['Null'] == 'NO' && $fieldInfo['Default'] === null && $fieldInfo['Key'] != 'PRI') {
|
||||
// $typePrefix = $fieldInfo['Type'];
|
||||
// if (($pos = strpos($typePrefix, '(')) !== false) {
|
||||
// $typePrefix = substr($typePrefix, 0, $pos);
|
||||
// }
|
||||
// if (preg_match('/varchar/', $typePrefix)) {
|
||||
// $tableRows[] = [
|
||||
// 'label' => "Field: $table.$field",
|
||||
// 'required' => "default ''",
|
||||
// 'current' => 'null',
|
||||
// 'result' => 'NO',
|
||||
// ];
|
||||
// $toAlterTable[$table][$field] = "modify $field {$fieldInfo['Type']} not null default ''";
|
||||
// continue;
|
||||
// }
|
||||
// if (preg_match('/int/', $typePrefix)) {
|
||||
// $tableRows[] = [
|
||||
// 'label' => "Field: $table.$field",
|
||||
// 'required' => "default 0",
|
||||
// 'current' => 'null',
|
||||
// 'result' => 'NO',
|
||||
// ];
|
||||
// $toAlterTable[$table][$field] = "modify $field {$fieldInfo['Type']} not null default 0";
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
while ($isPost) {
|
||||
try {
|
||||
sql_query('SET sql_mode=(SELECT REPLACE(@@sql_mode,"NO_ZERO_DATE", ""))');
|
||||
foreach ($toCreateTable as $query) {
|
||||
$update->doLog("[CREATE TABLE] $query");
|
||||
sql_query($query);
|
||||
$command = "php " . ROOT_PATH . "artisan install:migrate";
|
||||
$result = exec($command, $output, $result_code);
|
||||
$update->doLog(sprintf('command: %s, result_code: %s, result: %s', $command, $result_code, $result));
|
||||
$update->doLog("output: " . json_encode($output));
|
||||
if ($result_code != 0) {
|
||||
throw new \RuntimeException(json_encode($output));
|
||||
} else {
|
||||
$update->doLog("[MIGRATE] success.");
|
||||
}
|
||||
foreach ($toAlterTable as $table => $modies) {
|
||||
$query = "alter table $table " . implode(', ', $modies);
|
||||
|
||||
Reference in New Issue
Block a user