+ $iReturn = proc_close($hProcess);
+ if ($bfatal && $iReturn > 0)
+ {
+ fail("pgsql returned with error code ($iReturn)");
+ }
+ }
+
+ 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;
+
+ $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 passthruCheckReturn($cmd)
+ {
+ $result = -1;
+ passthru($cmd, $result);
+ if ($result != 0) fail('Error executing external command: '.$cmd);
+ }
+
+ function replace_tablespace($sTemplate, $sTablespace, $sSql)
+ {
+ if ($sTablespace)
+ $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"',
+ $sSql);
+ else
+ $sSql = str_replace($sTemplate, '', $sSql);
+
+ return $sSql;