Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Update Design Document Command

  • Loading branch information...
commit 00ffd6ac638e436e8dd29f1249b0882016c1e31c 1 parent 48f871f
@beberlei beberlei authored
View
1  bin/doctrine-couchdb.php
@@ -54,5 +54,6 @@
new \Doctrine\CouchDB\Tools\Console\Command\CompactDatabaseCommand(),
new \Doctrine\CouchDB\Tools\Console\Command\CompactViewCommand(),
new \Doctrine\CouchDB\Tools\Console\Command\MigrationCommand(),
+ new \Doctrine\ODM\CouchDB\Tools\Console\Command\UpdateDesignDocCommand(),
));
$cli->run();
View
8 lib/Doctrine/CouchDB/CouchDBClient.php
@@ -320,7 +320,7 @@ public function createViewQuery($designDocName, $viewName, DesignDocument $desig
}
/**
- * Create a design document from the definition at the named location.
+ * Create or update a design document from the given in memory definition.
*
* @param string $designDocName
* @param DesignDocument $designDoc
@@ -331,6 +331,12 @@ public function createDesignDocument($designDocName, DesignDocument $designDoc)
$data = $designDoc->getData();
$data['_id'] = '_design/' . $designDocName;
+ $response = $this->findDocument($data['_id']);
+ if ($response->status == 200) {
+ $docData = $response->body;
+ $data['_rev'] = $docData['_rev'];
+ }
+
return $this->httpClient->request(
"PUT",
sprintf("/%s/_design/%s", $this->databaseName, $designDocName),
View
54 lib/Doctrine/ODM/CouchDB/Tools/Console/Command/UpdateDesignDocCommand.php
@@ -0,0 +1,54 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\ODM\CouchDB\Tools\Console\Command;
+
+use Symfony\Component\Console\Input\InputArgument,
+ Symfony\Component\Console\Input\InputOption,
+ Symfony\Component\Console\Input\InputInterface,
+ Symfony\Component\Console\Output\OutputInterface,
+ Symfony\Component\Console\Command\Command;
+
+class UpdateDesignDocCommand extends Command
+{
+ protected function configure()
+ {
+ $this->setName('couchdb:odm:update-design-doc')
+ ->setDescription('Update design document')
+ ->setDefinition(array(
+ new InputArgument('docname', InputArgument::REQUIRED, 'Design doc name as registered in DocumentManager configuration.', null),
+ ));
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $dm = $this->getHelper('couchdb')->getDocumentManager();
+ $designDocData = $dm->getConfiguration()->getDesignDocument($input->getArgument('docname'));
+
+ $designDoc = new $designDocData['className']($designDocData['options']);
+
+ $response = $dm->getCouchDBClient()->createDesignDocument($input->getArgument('docname'), $designDoc);
+
+ if ($response->status < 300) {
+ $output->writeln("Design document was updated succesfully.");
+ } else {
+ throw new \RuntimeException($response->body['reason']);
+ }
+ }
+}
View
1  sandbox/doctrine.php
@@ -11,5 +11,6 @@
new \Doctrine\CouchDB\Tools\Console\Command\CompactDatabaseCommand(),
new \Doctrine\CouchDB\Tools\Console\Command\CompactViewCommand(),
new \Doctrine\CouchDB\Tools\Console\Command\MigrationCommand(),
+ new \Doctrine\ODM\CouchDB\Tools\Console\Command\UpdateDesignDocCommand(),
));
$cli->run();
Please sign in to comment.
Something went wrong with that request. Please try again.