-
-
-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'];
- }
- $aProcEnv = null;
- if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
- $aProcEnv = 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, $aProcEnv);
- 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'];
- }
- $aProcEnv = null;
- if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
- $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
- }
-
- $iReturn = runWithEnv($sCMD, $aProcEnv);
-}
-
-function passthruCheckReturn($sCmd)
-{
- $iResult = -1;
- passthru($sCmd, $iResult);
-}
-
-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 $sModulePath;
- $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
- $sTemplate = str_replace('{modulepath}', $sModulePath, $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);
-}