Newscoop\Installer\Services\DatabaseService::searchDbRolls PHP Метод

searchDbRolls() публичный Метод

Search for db roll in rols directory
public searchDbRolls ( string $roll_base_dir, string $last_db_roll ) : array
$roll_base_dir string
$last_db_roll string
Результат array
    public function searchDbRolls($roll_base_dir, $last_db_roll)
    {
        $rolls = array();
        // roll_name => roll_path
        $roll_dir_names = scandir($roll_base_dir);
        if (empty($roll_dir_names)) {
            return $rolls;
        }
        $avoid_starts = array('.', '_');
        $some_top_files = false;
        foreach ($roll_dir_names as $one_rol_dir) {
            $cur_rol_path = $roll_base_dir . DIRECTORY_SEPARATOR . $one_rol_dir;
            if (is_file($cur_rol_path) && 'sql' == pathinfo($cur_rol_path, PATHINFO_EXTENSION)) {
                $some_top_files = true;
            }
            if (!is_dir($cur_rol_path) || in_array(substr($one_rol_dir, 0, 1), $avoid_starts)) {
                continue;
            }
            if (!empty($last_db_roll) && $one_rol_dir <= $last_db_roll) {
                continue;
            }
            $rolls[$one_rol_dir] = $cur_rol_path;
        }
        ksort($rolls);
        if (empty($last_db_roll)) {
            if ($some_top_files) {
                $rolls = array_merge(array('.' => $roll_base_dir), $rolls);
            }
        }
        return $rolls;
    }

Usage Example

Пример #1
0
 /**
  * Upgrade database
  *
  * @param array   $versionsArray
  * @param boolean $silent
  * @param boolean $showRolls
  *
  * @return boolean
  */
 public function upgradeDatabase($versionsArray, $silent = false, $showRolls = false)
 {
     $databaseService = new Services\DatabaseService($this->monolog);
     $lockFileName = __FILE__;
     $lockFile = fopen($lockFileName, "r");
     if ($lockFile === false) {
         return "Unable to create single process lock control!";
     }
     if (!flock($lockFile, LOCK_EX | LOCK_NB)) {
         // do an exclusive lock
         return "The upgrade process is already running.";
     }
     // keeping the last imported version throughout the upgrade process
     $last_db_version = $versionsArray['version'];
     // keeping the last imported roll throughout the upgrade process
     $last_db_roll = $versionsArray['roll'];
     $first = true;
     $errorsCount = 0;
     $temp = 0;
     $skipped = array();
     $sqlVersions = array_map('basename', glob($this->newscoopDir . '/install/Resources/sql/upgrade/[2-9].[0-9]*'));
     usort($sqlVersions, array($databaseService, 'versionCompare'));
     foreach ($sqlVersions as $index => $db_version) {
         if (-1 == $databaseService->versionCompare($db_version, $last_db_version)) {
             continue;
         }
         $last_db_version = $db_version;
         $last_db_roll = '';
         $cur_old_roll = '';
         // the roll of the running version that was imported before the upgrade ($old_roll or '')
         if ($first) {
             $last_db_roll = $versionsArray['roll'];
             $cur_old_roll = $last_db_roll;
             if (!$silent) {
                 $db_ver_roll_info = "{$db_version}";
                 if (!in_array($last_db_roll, array('', '.'))) {
                     $db_ver_roll_info .= ", roll {$last_db_roll}";
                 }
                 $this->logger->addNotice('* Upgrading the database from version ' . $db_ver_roll_info . '...');
             }
             $first = false;
         }
         $output = array();
         $upgrade_base_dir = $this->newscoopDir . "/install/Resources/sql/upgrade/{$db_version}/";
         $rolls = $databaseService->searchDbRolls($upgrade_base_dir, $cur_old_roll);
         // run upgrade scripts
         $sql_scripts = array("tables.sql", "data-required.sql", "data-optional.sql", "tables-post.sql");
         foreach ($rolls as $upgrade_dir_roll => $upgrade_dir_path) {
             $upgrade_dir = $upgrade_dir_path . DIRECTORY_SEPARATOR;
             $last_db_roll = $upgrade_dir_roll;
             if ($showRolls || !$silent) {
                 $this->logger->addNotice('* importing database roll ' . $last_db_version . ' / ' . $last_db_roll);
             }
             foreach ($sql_scripts as $index => $script) {
                 if (!is_file($upgrade_dir . $script)) {
                     continue;
                 }
                 $error_queries = array();
                 $errorsCount = $databaseService->importDB($upgrade_dir . $script, $this->connection, $this->logger);
                 $temp = $temp + $errorsCount;
                 if ($errorsCount) {
                     $this->logger->addError('* ' . $script . ' (' . $db_version . ') errors');
                 }
             }
             $saveResult = $databaseService->saveDatabaseVersion($this->connection, $last_db_version, $last_db_roll);
             if ($saveResult) {
                 $this->logger->addNotice('* version is updated to ' . $last_db_version . '/' . $last_db_roll);
             }
         }
     }
     if (!$silent) {
         $this->logger->addNotice('* importing database is done');
     }
     flock($lockFile, LOCK_UN);
     // release the lock
     $errorsCount = $temp;
     if ($errorsCount) {
         return $databaseService->errorQueries;
     }
     return true;
 }