Example #1
0
 /**
  * step 6 : 数据库更新操作
  *
  * 先执行update.sql,再跳转到update.php
  */
 public function dbAction()
 {
     $step = (int) Wekit::cache()->get('system_upgrade_db_step');
     $step || $this->installService->after($this->localFileList, Wekit::cache()->get('system_upgrade_ftp'), $this->fileList);
     $sqlFile = Wind::getRealPath('PUBLIC:update.sql', true);
     $success = 1;
     if (!file_exists($sqlFile)) {
         Wekit::cache()->set('system_upgrade_step', 6);
         PwSystemHelper::log('no db update', $this->version);
         $this->forwardRedirect(WindUrlHelper::createUrl('appcenter/upgrade/php'));
     }
     $lang = Wind::getComponent('i18n');
     try {
         /* @var $db WindConnection */
         $db = Wind::getComponent('db');
         if (!$step) {
             $sqlArray = PwSystemHelper::sqlParser(WindFile::read($sqlFile), $db->getConfig('charset', '', 'utf8'), $db->getTablePrefix(), $db->getConfig('engine', '', 'MYISAM'));
             WindFile::savePhpData(DATA_PATH . 'upgrade/sql.tmp', $sqlArray);
         } else {
             $sqlArray = (include DATA_PATH . 'upgrade/sql.tmp');
         }
         end($sqlArray);
         if ($step > key($sqlArray)) {
             Wekit::cache()->set('system_upgrade_step', 6);
             PwSystemHelper::log('db update success', $this->version);
             $this->forwardRedirect(WindUrlHelper::createUrl('appcenter/upgrade/php'));
         }
         $sql = $sqlArray[$step];
         if ($sql) {
             foreach ($sql as $v) {
                 if (empty($v)) {
                     continue;
                 }
                 if (preg_match('/^ALTER\\s+TABLE\\s+`?(\\w+)`?\\s+(DROP|ADD)\\s+(KEY|INDEX|UNIQUE)\\s+([\\w\\(\\),`]+)?/i', $v, $matches)) {
                     list($key, $fields) = explode('(', $matches[4]);
                     $fields = trim($fields, '),');
                     list($matches[3]) = explode(' ', $matches[3]);
                     $matches[3] = trim(strtoupper($matches[3]));
                     PwSystemHelper::log($matches[1] . ' ' . str_replace('`', '', $key) . ' ' . ($fields ? str_replace('`', '', $fields) : '') . ' ' . $matches[3], $this->version);
                     PwSystemHelper::alterIndex(array($matches[1], str_replace('`', '', $key), $fields ? str_replace('`', '', $fields) : '', $matches[3], $matches[2]), $db);
                 } elseif (preg_match('/^ALTER\\s+TABLE\\s+`?(\\w+)`?\\s+(CHANGE|DROP|ADD)\\s+`?(\\w+)`?/i', $v, $matches)) {
                     PwSystemHelper::log($matches[1] . ' ' . $matches[3], $this->version);
                     PwSystemHelper::alterField(array($matches[1], $matches[3], $v), $db);
                 } else {
                     PwSystemHelper::log('execute sql ' . $v, $this->version);
                     $db->execute($v);
                 }
             }
         }
     } catch (Exception $e) {
         if ($e instanceof WindForwardException) {
             throw $e;
         }
         $success = 0;
         $this->setOutput(1, 'error');
         PwSystemHelper::log('execute sql failed' . $e->getMessage(), $this->version);
         $this->setOutput($lang->getMessage('APPCENTER:upgrade.db.error', array(implode(';', $sql))), 'msg');
     }
     if ($success) {
         $this->setOutput($lang->getMessage('APPCENTER:upgrade.db.update', array($step, key($sqlArray))), 'msg');
     }
     Wekit::cache()->set('system_upgrade_db_step', ++$step);
 }