VersionUpdate.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\input\Option;
  6. use think\console\Output;
  7. use think\Exception;
  8. use think\facade\Db;
  9. class VersionUpdate extends Command
  10. {
  11. protected function configure()
  12. {
  13. $this->setName('version:update')
  14. ->setDescription('crmeb_merchant 版本更新命令')
  15. ->addOption('package', 'p', Option::VALUE_REQUIRED, '指定更新包的路径');
  16. }
  17. protected function execute(Input $input, Output $output)
  18. {
  19. $flag = $output->confirm($input, '更新之前请务必做好数据库和代码的备份,防止数据或代码在更新中被覆盖 !!!', false);
  20. if (!$flag) return;
  21. $flag = $output->confirm($input, '请确保swoole服务和队列服务已关闭,防止意外报错', false);
  22. if (!$flag) return;
  23. $version = str_replace('CRMEB-MERCHANT-v', '', get_crmeb_version('no'));
  24. ini_set('memory_limit', '-1');
  25. set_time_limit(0);
  26. $packagePath = $input->getOption('package') ?: 'auto_update.zip';
  27. $updateFilePath = app()->getRootPath() . ltrim($packagePath, '/= ');
  28. $updatePath = dirname($updateFilePath);
  29. $unzipPath = $updatePath . '/_update_runtime_' . str_replace('.', '_', $version);
  30. if (!is_file($updateFilePath)) {
  31. $output->warning($updateFilePath . ' 更新包不存在');
  32. return;
  33. }
  34. $zip = new \ZipArchive();
  35. if ($zip->open($updateFilePath) === true) {
  36. $zip->extractTo($unzipPath);
  37. $zip->close();
  38. } else {
  39. $output->warning($updateFilePath . ' 更新包打开失败');
  40. return;
  41. }
  42. $unlink = function () use ($unzipPath) {
  43. @unlink($unzipPath . '/update.sql');
  44. @unlink($unzipPath . '/update.zip');
  45. @unlink($unzipPath . '/.env');
  46. @rmdir($unzipPath);
  47. };
  48. if (!is_file($unzipPath . '/.env')) {
  49. $output->warning('文件不完整');
  50. $unlink();
  51. return;
  52. }
  53. $env = parse_ini_file($unzipPath . '/.env', true) ?: [];
  54. if (($env['NAME'] ?? '') !== 'CRMEB-MERCHANT' || ((($env['OLD_VERSION'] ?? '') && ($env['OLD_VERSION'] ?? '') !== $version))) {
  55. if (($env['TYPE'] ?? '') !== 'MODEL') {
  56. $output->warning('版本号对比失败,请检查当前版本号(.version/更新包)是否被修改');
  57. $unlink();
  58. return;
  59. }
  60. }
  61. $confirm = [];
  62. if (isset($env['confirm'])) {
  63. $confirm = is_array($env['confirm']) ? $env['confirm'] : [$env['confirm']];
  64. }
  65. foreach ($confirm as $item) {
  66. if (!$output->confirm($input, $item, false)) {
  67. $unlink();
  68. return;
  69. }
  70. }
  71. if (is_file($unzipPath . '/update.sql')) {
  72. $str = preg_replace('/--.*/i', '', file_get_contents($unzipPath . '/update.sql'));
  73. $str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);
  74. $sqlList = explode(";\n", $str);
  75. } else {
  76. $sqlList = [];
  77. }
  78. $output->info('开始更新');
  79. $pre = env('database.prefix');
  80. try {
  81. Db::transaction(function () use ($pre, $output, $unzipPath, $sqlList) {
  82. $count = count($sqlList);
  83. foreach ($sqlList as $idx => $sql) {
  84. $sql = trim($sql, " \xEF\xBB\xBF\r\n");
  85. if (!$sql) continue;
  86. if ($pre && $pre !== 'eb_') {
  87. str_replace('eb_', $pre, $sql);
  88. }
  89. Db::query($sql . ';');
  90. if (!($idx % 50)) {
  91. $output->info("导入中($idx/$count)");
  92. }
  93. }
  94. if ($count) {
  95. $output->info('数据库更新成功');
  96. }
  97. $zip = new \ZipArchive();
  98. if ($zip->open($unzipPath . '/update.zip') === true) {
  99. $zip->extractTo(app()->getRootPath());
  100. $zip->close();
  101. } else {
  102. throw new Exception('更新文件覆盖失败');
  103. }
  104. });
  105. } catch (\Throwable $e) {
  106. $output->warning('更新失败:' . $e->getMessage());
  107. $unlink();
  108. return;
  109. }
  110. $unlink();
  111. $output->info('版本更新成功, 请重启swoole服务和队列服务');
  112. update_crmeb_compiled();
  113. }
  114. }