/** * Checks the numeric value from the last SQL patch file, updating the versions file if desired. * * @param bool $update Whether to update the versions file. * * @return bool|int False if there is a problem, otherwise the number from the last patch file. */ public function checkSQLFileLatest($update = true) { $options = ['data' => nZEDb_RES . 'db' . DS . 'schema' . DS . 'data' . DS, 'ext' => 'sql', 'path' => nZEDb_RES . 'db' . DS . 'patches' . DS . 'mysql', 'regex' => '#^' . Misc::PATH_REGEX . '(?P<patch>\\d{4})~(?P<table>\\w+)\\.sql$#', 'safe' => true]; $files = Misc::getDirFiles($options); natsort($files); $last = preg_match($options['regex'], end($files), $matches) ? (int) $matches['patch'] : false; if ($update) { if ($last !== false && $this->_vers->sql->file->__toString() != $last) { echo $this->out->primary("Updating latest patch file to " . $last); $this->_vers->sql->file = $last; $this->_changes |= self::UPDATED_SQL_FILE_LAST; } if ($this->_vers->sql->file->__toString() != $last) { $this->_vers->sql->file = $last; $this->_changes |= self::UPDATED_SQL_DB_PATCH; } } return $last; }
public function processPatches(array $options = []) { $patched = 0; $defaults = ['data' => nZEDb_RES . 'db' . DS . 'schema' . DS . 'data' . DS, 'ext' => 'sql', 'path' => nZEDb_RES . 'db' . DS . 'patches' . DS . $this->_DbSystem, 'regex' => '#^' . Misc::PATH_REGEX . '(?P<patch>\\d{4})~(?P<table>\\w+)\\.sql$#', 'safe' => true]; $options += $defaults; $currentVersion = $this->settings->getSetting(['setting' => 'sqlpatch']); if (!is_numeric($currentVersion)) { exit("Bad sqlpatch value: '{$currentVersion}'\n"); } $files = empty($options['files']) ? Misc::getDirFiles($options) : $options['files']; if (count($files)) { natsort($files); $local = $this->pdo->isLocalDb() ? '' : 'LOCAL '; $data = $options['data']; echo $this->log->primary('Looking for unprocessed patches...'); foreach ($files as $file) { $setPatch = false; $fp = fopen($file, 'r'); $patch = fread($fp, filesize($file)); if (preg_match($options['regex'], str_replace('\\', '/', $file), $matches)) { $patch = (int) $matches['patch']; $setPatch = true; } else { if (preg_match('/UPDATE `?site`? SET `?value`? = \'?(?P<patch>\\d+)\'? WHERE `?setting`? = \'sqlpatch\'/i', $patch, $matches)) { $patch = (int) $matches['patch']; } else { throw new \RuntimeException("No patch information available, stopping!!"); } } if ($patch > $currentVersion) { echo $this->log->header('Processing patch file: ' . $file); if ($options['safe'] && !$this->backedUp) { $this->_backupDb(); } $this->splitSQL($file, ['local' => $local, 'data' => $data]); if ($setPatch) { $this->pdo->queryExec("UPDATE settings SET value = '{$patch}' WHERE setting = 'sqlpatch';"); } $patched++; } } } else { exit($this->log->error("\nHave you changed the path to the patches folder, or do you have the right permissions?\n")); } if ($patched === 0) { echo $this->log->info("Nothing to patch, you are already on version {$currentVersion}"); } return $patched; }