-
-
-function pgsqlRunScriptFile($sFilename)
-{
- global $aCMDResult;
- if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
-
- // Convert database DSN to psql parameters
- $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
- if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
- $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
- if (!$aCMDResult['verbose']) {
- $sCMD .= ' -q';
- }
- if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
- $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
- }
- if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
- $sCMD .= ' -U ' . $aDSNInfo['username'];
- }
- $procenv = null;
- if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
- $procenv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
- }
-
- $ahGzipPipes = null;
- if (preg_match('/\\.gz$/', $sFilename)) {
- $aDescriptors = array(
- 0 => array('pipe', 'r'),
- 1 => array('pipe', 'w'),
- 2 => array('file', '/dev/null', 'a')
- );
- $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
- if (!is_resource($hGzipProcess)) fail('unable to start zcat');
- $aReadPipe = $ahGzipPipes[1];
- fclose($ahGzipPipes[0]);
- } else {
- $sCMD .= ' -f '.$sFilename;
- $aReadPipe = array('pipe', 'r');
- }
-
- $aDescriptors = array(
- 0 => $aReadPipe,
- 1 => array('pipe', 'w'),
- 2 => array('file', '/dev/null', 'a')
- );
- $ahPipes = null;
- $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes, null, $procenv);
- if (!is_resource($hProcess)) fail('unable to start pgsql');
-
- // TODO: error checking
- while (!feof($ahPipes[1])) {
- echo fread($ahPipes[1], 4096);
- }
- fclose($ahPipes[1]);
-
- $iReturn = proc_close($hProcess);
- if ($iReturn > 0) {
- fail("pgsql returned with error code ($iReturn)");
- }
- if ($ahGzipPipes) {
- fclose($ahGzipPipes[1]);
- proc_close($hGzipProcess);
- }
-}
-
-function pgsqlRunScript($sScript, $bfatal = true)
-{
- global $aCMDResult;
- runSQLScript(
- $sScript,
- $bfatal,
- $aCMDResult['verbose'],
- $aCMDResult['ignore-errors']
- );
-}
-
-function pgsqlRunPartitionScript($sTemplate)
-{
- global $aCMDResult;
- $oDB =& getDB();
-
- $sSQL = 'select distinct partition from country_name';
- $aPartitions = chksql($oDB->getCol($sSQL));
- if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
-
- preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
- foreach ($aMatches as $aMatch) {
- $sResult = '';
- foreach ($aPartitions as $sPartitionName) {
- $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
- }
- $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
- }
-
- pgsqlRunScript($sTemplate);
-}
-
-function pgsqlRunRestoreData($sDumpFile)
-{
- // Convert database DSN to psql parameters
- $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
- if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
- $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
-
- $aDescriptors = array(
- 0 => array('pipe', 'r'),
- 1 => array('pipe', 'w'),
- 2 => array('file', '/dev/null', 'a')
- );
- $ahPipes = null;
- $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
- if (!is_resource($hProcess)) fail('unable to start pg_restore');
-
- fclose($ahPipes[0]);
-
- // TODO: error checking
- while (!feof($ahPipes[1])) {
- echo fread($ahPipes[1], 4096);
- }
- fclose($ahPipes[1]);
-
- $iReturn = proc_close($hProcess);
-}
-
-function pgsqlRunDropAndRestore($sDumpFile)
-{
- // Convert database DSN to psql parameters
- $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
- if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
- $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
- if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
- $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
- }
- if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
- $sCMD .= ' -U ' . $aDSNInfo['username'];
- }
- $procenv = null;
- if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
- $procenv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
- }
-
- $iReturn = runWithEnv($sCMD, $procenv);
-}
-
-function passthruCheckReturn($cmd)
-{
- $result = -1;
- passthru($cmd, $result);
-}
-
-function runWithEnv($cmd, $env)
-{
- $fds = array(0 => array('pipe', 'r'),
- 1 => STDOUT,
- 2 => STDERR);
- $pipes = null;
- $proc = @proc_open($cmd, $fds, $pipes, null, $env);
- if (!is_resource($proc)) {
- fail('unable to run command:' . $cmd);
- }
-
- fclose($pipes[0]); // no stdin
-
- $stat = proc_close($proc);
- return $stat;
-}
-
-function replace_tablespace($sTemplate, $sTablespace, $sSql)
-{
- if ($sTablespace) {
- $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
- } else {
- $sSql = str_replace($sTemplate, '', $sSql);
- }
-
- return $sSql;
-}
-
-function create_sql_functions($aCMDResult)
-{
- global $modulePath;
- $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
- $sTemplate = str_replace('{modulepath}', $modulePath, $sTemplate);
- if ($aCMDResult['enable-diff-updates']) {
- $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
- }
- if ($aCMDResult['enable-debug-statements']) {
- $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
- }
- if (CONST_Limit_Reindexing) {
- $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
- }
- if (!CONST_Use_US_Tiger_Data) {
- $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
- }
- if (!CONST_Use_Aux_Location_data) {
- $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
- }
- pgsqlRunScript($sTemplate);
-}