repair.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use mysqli;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\Output;
  8. class repair extends Command
  9. {
  10. protected function configure()
  11. {
  12. // 指令配置
  13. $this->setName('repair')
  14. ->setDescription('修复数据库差异');
  15. }
  16. protected function execute(Input $input, Output $output)
  17. {
  18. self::repair();
  19. print_r("\033[1;31m数据库差异信息修复完毕\033[0m\n\r\033[1;42m请尝试刷新网站检查是否正常\033[0m\n");
  20. }
  21. public static function repair()
  22. {
  23. //默认的一些基础数据
  24. $sqlFile = joinPath(root_path(), 'install.sql');
  25. $sql_file_content = file_get_contents($sqlFile);
  26. // 解析SQL文件内容并执行
  27. $sql_statements = explode(';', trim($sql_file_content));
  28. try {
  29. $conn = new mysqli(env('database.hostname'), env('database.username'), env('database.password'), env('database.database'), (int)env('database.hostport', 3306));
  30. } catch (\Exception $exception) {
  31. print_r("数据库连接失败咯,请正确配置数据库\n");
  32. exit();
  33. }
  34. foreach ($sql_statements as $sql_statement) {
  35. if (!empty($sql_statement)) {
  36. try {
  37. $conn->query($sql_statement);
  38. } catch (\Exception $exception) {
  39. //不用管
  40. }
  41. }
  42. }
  43. }
  44. }