execute_upgrade(); if($result['success']){ $mconfig=model('admin/Config'); $mconfig->setVersion($this->get_skycaiji_version()); } return $result; } /*获取版本号*/ public function get_skycaiji_version(){ $newProgramConfig=file_get_contents(config('app_path').'/common.php'); if(preg_match('/[\'\"]SKYCAIJI_VERSION[\'\"]\s*,\s*[\'\"](?P[\d\.]+?)[\'\"]/i', $newProgramConfig,$programVersion)){ $programVersion=$programVersion['v']; }else{ $programVersion=''; } return $programVersion; } /*判断存在索引*/ public function check_exists_index($name,$indexes){ if(empty($name)){ return false; } $exists_index=false; foreach ($indexes as $k=>$v){ if(strcasecmp($name,$v['Key_name'])==0){ $exists_index=true; break; } } return $exists_index; } /*判断存在字段*/ public function check_exists_field($name,$columns){ if(empty($name)){ return false; } $exists_column=false; foreach ($columns as $k=>$v){ if(strcasecmp($name,$v['Field'])==0){ $exists_column=true; break; } } return $exists_column; } /*修改字段类型*/ public function modify_field_type($field,$type,$modifySql,$columns){ foreach ($columns as $v){ if(strcasecmp($field,$v['Field'])==0){ if(strcasecmp($type,$v['Type'])!=0){ db()->execute($modifySql); } break; } } } public function execute_upgrade(){ $mconfig=model('admin/Config'); $dbVersion=$mconfig->getVersion(); $fileVersion=$this->get_skycaiji_version(); if(empty($dbVersion)){ return array('success'=>false,'msg'=>'未获取到数据库中的版本号'); } if(empty($fileVersion)){ return array('success'=>false,'msg'=>'未获取到项目文件的版本号'); } if(version_compare($dbVersion,$fileVersion)>=0){ return array('success'=>true,'msg'=>'数据库已是最新版本,无需更新'); } /*找出更新函数*/ $methods=get_class_methods($this); $upgradeDbMethods=array(); foreach ($methods as $method){ if(preg_match('/^upgrade_db_to(?P(\_\d+)+)$/',$method,$toVer)){ $toVer=str_replace('_', '.', trim($toVer['ver'],'_')); if(version_compare($toVer,$dbVersion)>=1){ if(version_compare($toVer,$fileVersion)<=0){ $upgradeDbMethods[$toVer]=$method; } } } } if(empty($upgradeDbMethods)){ return array('success'=>true,'msg'=>'暂无更新'); } ksort($upgradeDbMethods); foreach ($upgradeDbMethods as $newVer=>$upMethod){ try { $this->$upMethod(); $mconfig->setVersion($newVer); }catch (\Exception $ex){ return array('success'=>false,'msg'=>$ex->getMessage()); } } clear_dir(config('root_path').'/runtime'); return array('success'=>true,'msg'=>'升级完毕'); } public function upgrade_db_to_1_3(){ $db_prefix=config('database.prefix'); $proxy_table=$db_prefix.'proxy_ip'; $exists=db()->query("show tables like '{$proxy_table}'"); if(empty($exists)){ $addTable=<<execute($addTable); } /*修改表*/ $columns_collected=db()->query("SHOW COLUMNS FROM `{$db_prefix}collected`"); if(!$this->check_exists_field('titleMd5', $columns_collected)){ db()->execute("alter table `{$db_prefix}collected` add `titleMd5` varchar(32) NOT NULL DEFAULT ''"); } $indexes_collected=db()->query("SHOW INDEX FROM `{$db_prefix}collected`"); if(!$this->check_exists_index('ix_titlemd5', $indexes_collected)){ db()->execute("ALTER TABLE `{$db_prefix}collected` ADD INDEX ix_titlemd5 ( `titleMd5` )"); } $columns_task=db()->query("SHOW COLUMNS FROM `{$db_prefix}task`"); if(!$this->check_exists_field('config', $columns_task)){ db()->execute("alter table `{$db_prefix}task` add `config` mediumtext"); } $columns_collector=db()->query("SHOW COLUMNS FROM `{$db_prefix}collector`"); $this->modify_field_type('config', 'mediumtext', "alter table `{$db_prefix}collector` modify column `config` mediumtext", $columns_collector); $columns_release=db()->query("SHOW COLUMNS FROM `{$db_prefix}release`"); $this->modify_field_type('config', 'mediumtext', "alter table `{$db_prefix}release` modify column `config` mediumtext", $columns_release); $columns_rule=db()->query("SHOW COLUMNS FROM `{$db_prefix}rule`"); $this->modify_field_type('config', 'mediumtext', "alter table `{$db_prefix}rule` modify column `config` mediumtext", $columns_rule); } public function upgrade_db_to_2_2(){ $db_prefix=config('database.prefix'); $provider_table=$db_prefix.'provider'; $exists=db()->query("show tables like '{$provider_table}'"); if(empty($exists)){ $addTable=<<execute($addTable); } $app_table=$db_prefix.'app'; $exists=db()->query("show tables like '{$app_table}'"); if(empty($exists)){ $addTable=<<execute($addTable); } /*修改表*/ $columns_user=db()->query("SHOW COLUMNS FROM `{$db_prefix}user`"); if(!$this->check_exists_field('salt', $columns_user)){ db()->execute("alter table `{$db_prefix}user` add `salt` varchar(50) NOT NULL DEFAULT ''"); } $columns_rule=db()->query("SHOW COLUMNS FROM `{$db_prefix}rule`"); if(!$this->check_exists_field('provider_id', $columns_rule)){ db()->execute("alter table `{$db_prefix}rule` add `provider_id` int(11) NOT NULL DEFAULT '0'"); } $columns_rapp=db()->query("SHOW COLUMNS FROM `{$db_prefix}release_app`"); if(!$this->check_exists_field('provider_id', $columns_rapp)){ db()->execute("alter table `{$db_prefix}release_app` add `provider_id` int(11) NOT NULL DEFAULT '0'"); } $indexes_release_app=db()->query("SHOW INDEX FROM `{$db_prefix}release_app`"); if(!$this->check_exists_index('ix_app', $indexes_release_app)){ db()->execute("ALTER TABLE `{$db_prefix}release_app` ADD unique ix_app ( `app` )"); } } public function upgrade_db_to_2_3(){ $db_prefix=config('database.prefix'); $func_app_table=$db_prefix.'func_app'; $exists=db()->query("show tables like '{$func_app_table}'"); if(empty($exists)){ $addTable=<<execute($addTable); } $columns_proxyip=db()->query("SHOW COLUMNS FROM `{$db_prefix}proxy_ip`"); if(!$this->check_exists_field('type', $columns_proxyip)){ db()->execute("alter table `{$db_prefix}proxy_ip` add `type` varchar(20) NOT NULL DEFAULT ''"); } if(!$this->check_exists_field('addtime', $columns_proxyip)){ db()->execute("alter table `{$db_prefix}proxy_ip` add `addtime` int(11) NOT NULL DEFAULT '0'"); } if(!$this->check_exists_field('no', $columns_proxyip)){ db()->execute("alter table `{$db_prefix}proxy_ip` add `no` bigint(20) NOT NULL"); } $indexes_proxyip=db()->query("SHOW INDEX FROM `{$db_prefix}proxy_ip`"); if(!$this->check_exists_index('no', $indexes_proxyip)){ db()->execute("ALTER TABLE `{$db_prefix}proxy_ip` ADD INDEX no ( `no` )"); } db()->execute("alter table `{$db_prefix}proxy_ip` modify `no` bigint auto_increment"); if(!$this->check_exists_index('addtime_no', $indexes_proxyip)){ db()->execute("ALTER TABLE `{$db_prefix}proxy_ip` ADD INDEX addtime_no ( `addtime`,`no` )"); } if(!$this->check_exists_index('ix_num', $indexes_proxyip)){ db()->execute("ALTER TABLE `{$db_prefix}proxy_ip` ADD INDEX ix_num ( `num` )"); } if(!$this->check_exists_index('ix_time', $indexes_proxyip)){ db()->execute("ALTER TABLE `{$db_prefix}proxy_ip` ADD INDEX ix_time ( `time` )"); } } } ?>