debug
This commit is contained in:
@@ -98,7 +98,7 @@ class DatabaseManage extends Command
|
||||
$Output->writeln("创建临时导入文件失败.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
$strCmd = "mysql -h{$strHost} -P{$strPort} -u{$strUserName} -p{$strPwd} {$strDb} < {$strTempFile}";
|
||||
|
||||
system($strCmd, $intResultCode);
|
||||
|
||||
176
code/extend/db/MongoMigrate.php
Normal file
176
code/extend/db/MongoMigrate.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace db;
|
||||
|
||||
use db\mongo\MongoBase;
|
||||
use ddl\DDLManage;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
|
||||
class MongoMigrate extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('mongo:manage')
|
||||
->addArgument('action', \think\console\input\Argument::OPTIONAL, 'The action to perform: migrate')
|
||||
->setDescription('Manage the mongo, migarte index or drop all index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param Input $input
|
||||
* @param Output $Output
|
||||
* @return void
|
||||
*/
|
||||
protected function execute(Input $input, Output $Output)
|
||||
{
|
||||
$strAction = $input->getArgument('action');
|
||||
|
||||
if ($strAction === 'migrate') {
|
||||
$this->migrate($Output);
|
||||
} else if ($strAction === 'drop') {
|
||||
$this->dropAllIndexes($Output);
|
||||
} else {
|
||||
$Output->writeln("Invalid action. Use 'migrate' or 'drop'");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param Output $Output
|
||||
* @return void
|
||||
*/
|
||||
protected function dropAllIndexes(Output $Output)
|
||||
{
|
||||
try {
|
||||
DDLManage::load('MongoBase');
|
||||
$Db = MongoBase::getInstance()->getDb();
|
||||
|
||||
$ColsInfo = $Db->listCollections();
|
||||
|
||||
foreach ($ColsInfo as $ColInfo) {
|
||||
$strColName = $ColInfo->getName();
|
||||
$Col = $Db->selectCollection($strColName);
|
||||
|
||||
|
||||
$arrIndexes = iterator_to_array($Col->listIndexes());
|
||||
$arrIndexesToDrop = [];
|
||||
|
||||
|
||||
foreach ($arrIndexes as $Index) {
|
||||
$strIndexName = $Index->getName();
|
||||
if ($strIndexName !== '_id_') {
|
||||
$arrIndexesToDrop[] = $strIndexName;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!empty($arrIndexesToDrop)) {
|
||||
foreach ($arrIndexesToDrop as $strIndexName) {
|
||||
$Col->dropIndex($strIndexName);
|
||||
$Output->writeln(" Dropped index: {$strIndexName} from collection: {$strColName}");
|
||||
}
|
||||
} else {
|
||||
$Output->writeln(" No non-_id indexes found in collection: {$strColName}");
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$Output->writeln("<error>Failed to drop indexes: {$e->getMessage()}</error>");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param Output $Output
|
||||
* @return void
|
||||
*/
|
||||
protected function migrate(Output $Output)
|
||||
{
|
||||
try {
|
||||
DDLManage::load('MongoBase');
|
||||
$strMigrationDir = root_path() . 'database/mongo/migrations';
|
||||
|
||||
|
||||
if (!is_dir($strMigrationDir)) {
|
||||
mkdir($strMigrationDir, 0755, true);
|
||||
$Output->writeln("<info>Created migration directory: {$strMigrationDir}</info>");
|
||||
}
|
||||
|
||||
|
||||
$arrFiles = glob($strMigrationDir . '/*.php');
|
||||
if (empty($arrFiles)) {
|
||||
$Output->writeln('<comment>No migration files found.</comment>');
|
||||
return 0;
|
||||
}
|
||||
|
||||
sort($arrFiles);
|
||||
|
||||
$Db = MongoBase::getInstance()->getDb();
|
||||
|
||||
foreach ($arrFiles as $strFile) {
|
||||
$Output->writeln("Processing migration: " . basename($strFile));
|
||||
|
||||
|
||||
$arrMigration = require $strFile;
|
||||
if (!is_array($arrMigration) || !isset($arrMigration['collection']) || !isset($arrMigration['indexes'])) {
|
||||
$Output->writeln("<error>Invalid migration format in {$strFile}</error>");
|
||||
continue;
|
||||
}
|
||||
|
||||
$strCollectionName = $arrMigration['collection'];
|
||||
$arrIndexes = $arrMigration['indexes'];
|
||||
|
||||
|
||||
$Col = $Db->selectCollection($strCollectionName);
|
||||
|
||||
|
||||
$arrExistingIndexes = iterator_to_array($Col->listIndexes());
|
||||
|
||||
$arrExistingIndexKeys = [];
|
||||
foreach ($arrExistingIndexes as $Index) {
|
||||
$strKey = json_encode($Index->getKey());
|
||||
$arrExistingIndexKeys[$strKey] = $Index->getName();
|
||||
}
|
||||
|
||||
|
||||
foreach ($arrIndexes as $arrIndex) {
|
||||
$arrKey = $arrIndex['key'];
|
||||
$arrOptions = $arrIndex['options'] ?? [];
|
||||
|
||||
|
||||
$strKey = json_encode($arrKey);
|
||||
|
||||
|
||||
if (array_key_exists($strKey, $arrExistingIndexKeys)) {
|
||||
$Output->writeln(" Index for " . json_encode($arrKey) . " already exists as {$arrExistingIndexKeys[$strKey]}, skipping...");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
$arrIndexNameParts = [];
|
||||
foreach ($arrKey as $field => $direction) {
|
||||
$arrIndexNameParts[] = "{$field}_{$direction}";
|
||||
}
|
||||
$strIndexName = implode('_', $arrIndexNameParts);
|
||||
|
||||
|
||||
$arrOptions['name'] = $strIndexName;
|
||||
$Col->createIndex($arrKey, $arrOptions);
|
||||
|
||||
$Output->writeln(" Created index: {$strIndexName} for " . json_encode($arrKey));
|
||||
}
|
||||
}
|
||||
|
||||
$Output->writeln('<info>Migration completed successfully!</info>');
|
||||
return 0;
|
||||
} catch (\Exception $e) {
|
||||
$Output->writeln("<error>Error: {$e->getMessage()}</error>");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
114
code/extend/db/NovelManage.php
Normal file
114
code/extend/db/NovelManage.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace db;
|
||||
|
||||
use app\model\CategoryModel;
|
||||
use app\model\ChapterModel;
|
||||
use database\seeders\SeedManager;
|
||||
use db\mongo\MongoBase;
|
||||
use ddl\DDLManage;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\Output;
|
||||
use think\console\input\Option;
|
||||
use think\facade\Db;
|
||||
|
||||
class NovelManage extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('novel:manage')
|
||||
->addArgument('action', \think\console\input\Argument::OPTIONAL, 'The action to perform: reset')
|
||||
->setDescription('Manage the novel data: reset or ...');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $Output)
|
||||
{
|
||||
$strAction = $input->getArgument('action');
|
||||
|
||||
if ($strAction === 'reset') {
|
||||
$this->resetData($Output);
|
||||
} else {
|
||||
$Output->writeln("Invalid action. Use 'reset' ");
|
||||
}
|
||||
}
|
||||
|
||||
private function resetData(Output $Output)
|
||||
{
|
||||
DDLManage::load('MongoBase');
|
||||
|
||||
$Db = MongoBase::getInstance()->getDb();
|
||||
|
||||
$NovelCol = $Db->selectCollection('novel');
|
||||
|
||||
$ChapterCol = $Db->selectCollection('chapter');
|
||||
|
||||
$ChapterModel = new ChapterModel;
|
||||
|
||||
$intBatchSize = 1000;
|
||||
|
||||
$LastId = null;
|
||||
|
||||
while (true) {
|
||||
|
||||
$arrFilter = [];
|
||||
if (!is_null($LastId)) {
|
||||
$arrFilter['_id'] = ['$gt' => $LastId];
|
||||
}
|
||||
$arrOptions = [
|
||||
'sort' => ['_id' => 1],
|
||||
'limit' => $intBatchSize,
|
||||
'noCursorTimeout' => true,
|
||||
];
|
||||
|
||||
$Cursor = $NovelCol->find($arrFilter, $arrOptions);
|
||||
$arrDocs = iterator_to_array($Cursor);
|
||||
|
||||
if (empty($arrDocs)) {
|
||||
echo "全部处理完毕。\n";
|
||||
break;
|
||||
}
|
||||
|
||||
$arrBulkOps = [];
|
||||
|
||||
foreach ($arrDocs as $arrDoc) {
|
||||
$arrUpdateData = [];
|
||||
|
||||
if (!empty($arrDoc['og_novel_category'])) {
|
||||
$arrUpdateData['category_pinyin'] = CategoryModel::getPinYinByZh($arrDoc['og_novel_category']);
|
||||
$arrUpdateData['category_sex'] = CategoryModel::checkSex($arrDoc['og_novel_category']);
|
||||
$arrUpdateData['category_sex_zh'] = CategoryModel::checkSexZh($arrDoc['og_novel_category']);
|
||||
}
|
||||
|
||||
$arrLasteChapter = $ChapterModel->getLatestChapter($arrDoc['n_id'], false);
|
||||
|
||||
if (!empty($arrLasteChapter)) {
|
||||
$arrUpdateData['n_have_chapter'] = 1;
|
||||
$arrUpdateData['og_novel_latest_chapter_name'] = $arrLasteChapter['name'];
|
||||
}
|
||||
|
||||
if (!empty($arrUpdateData)) {
|
||||
$arrBulkOps[] = [
|
||||
'updateOne' => [
|
||||
['_id' => $arrDoc['_id']],
|
||||
['$set' => $arrUpdateData],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$LastId = $arrDoc['_id'];
|
||||
}
|
||||
|
||||
if (!empty($arrBulkOps)) {
|
||||
$Result = $NovelCol->bulkWrite($arrBulkOps);
|
||||
printf("本批匹配 %d 条记录, 修改 %d 条记录\n", $Result->getMatchedCount(), $Result->getModifiedCount());
|
||||
} else {
|
||||
echo "本批无需要更新的记录。\n";
|
||||
}
|
||||
|
||||
echo "已处理一批,最后 _id: " . (string)$LastId . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user