Esempio n. 1
0
 protected function _initDb()
 {
     $dbConfig = $this->_options['couchdb'];
     Geves_Model_Object::setDbConfig($dbConfig);
     if ($dbConfig['validate']) {
         // Make sure the DB exists
         try {
             $db = Sopha_Db::createDb($dbConfig['dbname'], $dbConfig['hostname'], $dbConfig['port']);
         } catch (Sopha_Db_Exception $ex) {
             if ($ex->getCode() != 412) {
                 throw $ex;
             }
             $db = Geves_Model_Object::getDb();
         }
         // Load design documents and create them
         $views = (require APPLICATION_PATH . '/configs/couchdb-views.php');
         if (!is_array($views)) {
             throw new ErrorException("Unable to configure database views: \$views is not properly defined");
         }
         foreach ($views as $view => $viewData) {
             try {
                 $db->create($viewData, array('_design', $view));
             } catch (Sopha_Db_Exception $ex) {
                 if ($ex->getCode() == 409) {
                     // document already exists
                     if ($dbConfig['replaceViews']) {
                         $view = $db->retrieve(array('_design', $view));
                         $view->fromArray($viewData);
                         $db->update($view);
                     }
                 } else {
                     throw $ex;
                 }
             }
         }
     }
 }