Skip to content

Instantly share code, notes, and snippets.

@MrTrick
Created July 5, 2011 23:56
Show Gist options
  • Save MrTrick/1066246 to your computer and use it in GitHub Desktop.
Save MrTrick/1066246 to your computer and use it in GitHub Desktop.
Design doc updater for CGM - uses https://github.com/dready92/PHP-on-Couch/
<?
set_time_limit(0);
function usage($msg) {
echo $msg."\n";
echo "Usage: update_couch_design ADMIN_PASSWORD\n";
echo "Updates the configured couchdb database's design doc and views without blocking any other users.\n";
exit;
}
//Must run as a standalone script.
if (strpos($_SERVER['PHP_SELF'],'update_couch_design.php') === false)
die("update_couch_design.php must be run, not included");
//Check the input parameters
array_shift($argv); //Don't need the script name
//Administrator password
if (!$pass = array_shift($argv))
usage("Must quote the administrator password");
if ($argv)
usage("Too many arguments");
//Bootstrap the environment
require_once('coreless_bootstrap.inc');
//Load authorisation password (Cannot use Cgm_Document_Gateway, as design doc might be a different version)
$url = setCgmPrimaryDbUser('admin', $pass);
$config = Zend_Registry::get('configuration')->couchdb;
$cc = new couchClient($config->dsn, $config->db);
echo "Access granted.\n";
//Build and load the design into an object
echo "Building staging design...";
chdir('../install/couchapp');
$ddocstr = shell_exec('couchapp push --export');
$ddoc = json_decode($ddocstr);
$ddoc_staging = clone $ddoc;
$ddoc_staging->_id = "_design/couchapp_staging";
echo "done.\n";
//Create or overwrite the 'couchapp_staging' design doc.
echo "Uploading staging design...";
try { $ddoc_staging->_rev = $cc->getDoc("_design/couchapp_staging")->_rev; } catch (couchNotFoundException $e) {}
$ddoc_staging->_rev = $cc->storeDoc($ddoc_staging)->rev;
echo "done.\n";
//Force a rebuild
$t = time();
echo "Rebuilding all views (this may take a long time, you can track progress in the futon status window) ... ";
$cc->getView('couchapp_staging', 'by_type');
echo "done. Elapsed time: ".(time()-$t)." seconds\n";
//Push the new ddoc over the original 'couchapp' doc.
echo "Uploading production design...";
try { $ddoc->_rev = $cc->getDoc("_design/couchapp")->_rev; } catch (couchNotFoundException $e) {}
$ddoc->_rev = $cc->storeDoc($ddoc)->rev;
echo "done.\n";
//Verify that the production ddoc now follows the staging ddoc
echo "Checking view signatures...";
$info_staging = $cc->getDesignDocInfo('couchapp_staging');
$info = $cc->getDesignDocInfo('couchapp');
if ($info_staging->view_index->signature == $info->view_index->signature) {
echo "done. Both signatures are: ".$info->view_index->signature."\n";
} else {
echo "\nERROR! Staging signature (".$info_staging->view_index->signature.") does not match production signature (".$info->view_index->signature.")\n";
echo "You may need to manually test and fix the production design document.\n"; die;
}
//Check that the production views build immediately.
$t = time();
echo "Testing a production view (this should be immediate) ...";
$cc->getView('couchapp', 'by_type');
echo "done. Elapsed time: ".(time()-$t)." seconds\n";
//Cleanup
echo "Removing staging design...";
$cc->deleteDoc($ddoc_staging);
echo "done.\n";
echo "Removing unused view output...";
$cc->cleanupDatabaseViews();
echo "done.\n";
echo "Operation complete:\n";
echo " The db design has been updated to CGM API Version v".$ddoc->cgm_api_version.".\n";
echo " The db design doc rev is ".$ddoc->_rev."\n";
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment