Upgrade.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\admin\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\Db;
  6. use think\console\Output;
  7. use app\common\controller\Version;
  8. class Upgrade extends Command
  9. {
  10. protected function configure()
  11. {
  12. $this->setName('upgrade')->setDescription('Version upgrades');
  13. }
  14. protected function execute(Input $input, Output $output)
  15. {
  16. // 没有版本表格的话 读取 创建
  17. $ve = file_get_contents(__DIR__."/../../common/versionsql/createversion");
  18. DB::execute($ve);
  19. // 旧版本号
  20. $version = DB::table('version')->where('id',1)->field('version')->find();
  21. if(empty($version)){
  22. $version['version'] = 1;
  23. DB::table('version')->insert(['id'=>1,'version'=>1]);
  24. }
  25. // 新版本号
  26. $oldversion = Version::getVersion();
  27. $output->writeln($version);
  28. if($version['version'] < $oldversion){
  29. $output->writeln('have version');
  30. $errarr = array();
  31. $ver = '';
  32. // 新版本大于旧版本
  33. for($version['version'];$version['version'] < $oldversion;$version['version']++){
  34. $name = "Version".$version['version'];
  35. if(file_exists(__DIR__."/../../common/versionsql/".$name)){
  36. $output->writeln("upgrating...");
  37. $info = file_get_contents(__DIR__."/../../common/versionsql/".$name);
  38. if(empty($info)){
  39. $output->writeln("\e[31m content is enpty \e[0m");
  40. continue;
  41. }else{
  42. $output->writeln("find the temp");
  43. }
  44. $sqlarr = explode(';',$info);
  45. foreach ($sqlarr as $k => $v) {
  46. if(empty($v)){
  47. continue;
  48. }
  49. $dir = ROOT_PATH . 'public' . DS . 'upgradelog';
  50. if (!file_exists($dir)){
  51. mkdir ($dir,0777,true);
  52. }
  53. try{
  54. $output->writeln($v);
  55. $result = DB::execute($v);
  56. $output->writeln("success");
  57. $file = ROOT_PATH . 'public' . DS . 'upgradelog/'.date('Ymd').'-success.log';
  58. fopen($file, "w");
  59. $log = date('Y-m-d H:i:s') . $v.'执行成功;\r';
  60. file_put_contents($file, $log,FILE_APPEND);
  61. }catch(\Exception $e){
  62. $output->writeln("\033[31m SQL error,--".$e->getMessage()." \033[0m");
  63. $file = ROOT_PATH . 'public' . DS . 'upgradelog/'.date('Ymd').'-fail.log';
  64. fopen($file, "w");
  65. $log = date('Y-m-d H:i:s') . $v.'执行失败,失败原因:'.$e->getMessage().';\r';
  66. file_put_contents($file, $log,FILE_APPEND);
  67. }
  68. }
  69. DB::table('version')->where('id',1)->update(['version'=>($version['version']+1)]);
  70. }else{
  71. $output->writeln("\033[31m file is not find".__DIR__."/../../common/versionsql/".$name." \033[0m");
  72. }
  73. }
  74. }else{
  75. $output->writeln("没有需要升级的版本");
  76. }
  77. }
  78. }