123
This commit is contained in:
114
code/extend/db/DatabaseManage.php
Normal file
114
code/extend/db/DatabaseManage.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace db;
|
||||
|
||||
use database\seeders\SeedManager;
|
||||
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 DatabaseManage extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('db:manage')
|
||||
->addArgument('action', \think\console\input\Argument::OPTIONAL, 'The action to perform: backup or init or seed')
|
||||
->setDescription('Manage the database: backup or initialize');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $Output)
|
||||
{
|
||||
$strAction = $input->getArgument('action');
|
||||
|
||||
if ($strAction === 'backup') {
|
||||
$this->backupDatabase($Output);
|
||||
} elseif ($strAction === 'init') {
|
||||
$this->initializeDatabase($Output);
|
||||
} elseif ($strAction === 'seed') {
|
||||
$this->seed($Output);
|
||||
} else {
|
||||
$Output->writeln("Invalid action. Use 'backup' or 'init'. or 'seed'");
|
||||
}
|
||||
}
|
||||
|
||||
private function seed(Output $Output)
|
||||
{
|
||||
$SeedManager = new SeedManager;
|
||||
$Output->writeln("开始数据播种" . PHP_EOL);
|
||||
$SeedManager->handle();
|
||||
$Output->writeln("数据播种结束");
|
||||
}
|
||||
|
||||
private function backupDatabase(Output $Output)
|
||||
{
|
||||
$arrConfig = config('database.connections.mysql');
|
||||
$strDb = $arrConfig['database'];
|
||||
$strUserName = $arrConfig['username'];
|
||||
$strPwd = $arrConfig['password'];
|
||||
$strHost = $arrConfig['hostname'];
|
||||
$strBackupFile = app()->getRootPath() . 'database/schema/mysql-schema.dump';
|
||||
|
||||
$strTempFile = tempnam(sys_get_temp_dir(), 'mysql-schema-') . '.dump';
|
||||
|
||||
$strCmd = "mysqldump -h{$strHost} -u{$strUserName} -p{$strPwd} --no-data {$strDb} > {$strTempFile}";
|
||||
|
||||
system($strCmd, $intResultCode);
|
||||
|
||||
if ($intResultCode === 0) {
|
||||
if (file_exists($strBackupFile)) {
|
||||
unlink($strBackupFile);
|
||||
}
|
||||
|
||||
if (rename($strTempFile, $strBackupFile)) {
|
||||
$Output->writeln("Database backup successful: {$strBackupFile}");
|
||||
} else {
|
||||
$Output->writeln("Failed to move backup file to destination.");
|
||||
}
|
||||
} else {
|
||||
$Output->writeln("Database backup failed.");
|
||||
unlink($strTempFile);
|
||||
}
|
||||
}
|
||||
|
||||
private function initializeDatabase(Output $Output)
|
||||
{
|
||||
$arrConfig = config('database.connections.mysql');
|
||||
$strDb = $arrConfig['database'];
|
||||
$strUserName = $arrConfig['username'];
|
||||
$strPwd = $arrConfig['password'];
|
||||
$strHost = $arrConfig['hostname'];
|
||||
$strPort = $arrConfig['hostport'];
|
||||
$strBackupFile = app()->getRootPath() . 'database/schema/mysql-schema.dump';
|
||||
|
||||
$checkTableQuery = "SHOW TABLES LIKE 'migrations'";
|
||||
$boolMigrateExists = Db::query($checkTableQuery);
|
||||
|
||||
if (!empty($boolMigrateExists)) {
|
||||
$Output->writeln("数据库已存在,跳过初始化导入");
|
||||
return;
|
||||
}
|
||||
|
||||
$strTempFile = tempnam(sys_get_temp_dir(), 'mysql-schema-') . '.dump';
|
||||
|
||||
if (!copy($strBackupFile, $strTempFile)) {
|
||||
$Output->writeln("创建临时导入文件失败.");
|
||||
return;
|
||||
}
|
||||
|
||||
$strCmd = "mysql -h{$strHost} -P{$strPort} -u{$strUserName} -p{$strPwd} {$strDb} < {$strTempFile}";
|
||||
|
||||
system($strCmd, $intResultCode);
|
||||
|
||||
if ($intResultCode === 0) {
|
||||
$Output->writeln("成功导入初始化文件");
|
||||
} else {
|
||||
$Output->writeln("初始化文件导入失败");
|
||||
}
|
||||
|
||||
unlink($strTempFile);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user