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

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

Import sql file to databas
public importDB ( string $sqlFilePath, Doctrine\DBAL\Connection $connection, mixed $logger = null ) : integer
$sqlFilePath string
$connection Doctrine\DBAL\Connection
$logger mixed
Результат integer
    public function importDB($sqlFilePath, $connection, $logger = null)
    {
        if (!($sqlFile = file_get_contents($sqlFilePath))) {
            return false;
        }
        if ($logger == null) {
            $logger = $this->logger;
        }
        $queries = $this->splitSQL($sqlFile);
        $errors = 0;
        foreach ($queries as $query) {
            $query = trim($query);
            if (!empty($query) && $query[0] != '#' && 0 !== strpos($query, "--")) {
                if (0 !== strpos(strtolower($query), "system")) {
                    try {
                        $connection->executeQuery($query);
                    } catch (\Exception $e) {
                        $errors++;
                        $this->errorQueries[] = $query;
                        $logger->addError('Error with query "' . $query . '"');
                        continue;
                    }
                }
                // if it started via the system command
                $command_parts = array();
                foreach (explode(" ", $query) as $query_part) {
                    $query_part = trim($query_part);
                    if ("" != $query_part) {
                        $command_parts[] = $query_part;
                    }
                }
                $command_script = "";
                $command_known = false;
                if (3 == count($command_parts)) {
                    if ("php" == strtolower($command_parts[1])) {
                        $command_known = true;
                        $command_script = trim($command_parts[2], ";");
                    }
                }
                if (!$command_known) {
                    continue;
                }
                $command_path = dirname($sqlFilePath);
                $command_path = $this->combinePaths($command_path, $command_script);
                require_once $command_path;
                if (isset($upgradeErrors) && is_array($upgradeErrors) && count($upgradeErrors) > 0) {
                    foreach ($upgradeErrors as $upgradeError) {
                        $errors++;
                        $this->errorQueries[] = $upgradeError;
                    }
                }
            }
        }
        return $errors;
    }

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;
 }