12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace app\admin\command;
- use think\console\Command;
- use think\console\Input;
- use think\Db;
- use think\console\Output;
- use app\common\controller\Version;
- class Upgrade extends Command
- {
- protected function configure()
- {
- $this->setName('upgrade')->setDescription('Version upgrades');
- }
- protected function execute(Input $input, Output $output)
- {
- // 没有版本表格的话 读取 创建
- $ve = file_get_contents(__DIR__."/../../common/versionsql/createversion");
- DB::execute($ve);
- // 旧版本号
- $version = DB::table('version')->where('id',1)->field('version')->find();
- if(empty($version)){
- $version['version'] = 1;
- DB::table('version')->insert(['id'=>1,'version'=>1]);
- }
- // 新版本号
- $oldversion = Version::getVersion();
- $output->writeln($version);
- if($version['version'] < $oldversion){
- $output->writeln('have version');
- $errarr = array();
- $ver = '';
- // 新版本大于旧版本
- for($version['version'];$version['version'] < $oldversion;$version['version']++){
- $name = "Version".$version['version'];
- if(file_exists(__DIR__."/../../common/versionsql/".$name)){
- $output->writeln("upgrating...");
- $info = file_get_contents(__DIR__."/../../common/versionsql/".$name);
- if(empty($info)){
- $output->writeln("\e[31m content is enpty \e[0m");
- continue;
- }else{
- $output->writeln("find the temp");
- }
- $sqlarr = explode(';',$info);
- foreach ($sqlarr as $k => $v) {
- if(empty($v)){
- continue;
- }
- $dir = ROOT_PATH . 'public' . DS . 'upgradelog';
- if (!file_exists($dir)){
- mkdir ($dir,0777,true);
- }
- try{
- $output->writeln($v);
- $result = DB::execute($v);
- $output->writeln("success");
- $file = ROOT_PATH . 'public' . DS . 'upgradelog/'.date('Ymd').'-success.log';
- fopen($file, "w");
- $log = date('Y-m-d H:i:s') . $v.'执行成功;\r';
- file_put_contents($file, $log,FILE_APPEND);
- }catch(\Exception $e){
- $output->writeln("\033[31m SQL error,--".$e->getMessage()." \033[0m");
- $file = ROOT_PATH . 'public' . DS . 'upgradelog/'.date('Ymd').'-fail.log';
- fopen($file, "w");
- $log = date('Y-m-d H:i:s') . $v.'执行失败,失败原因:'.$e->getMessage().';\r';
- file_put_contents($file, $log,FILE_APPEND);
- }
- }
- DB::table('version')->where('id',1)->update(['version'=>($version['version']+1)]);
- }else{
- $output->writeln("\033[31m file is not find".__DIR__."/../../common/versionsql/".$name." \033[0m");
- }
- }
- }else{
- $output->writeln("没有需要升级的版本");
- }
- }
- }
|