public function test_deleteAllTables() { USVN_Db_Utils::loadFile($this->db, $this->testfile); $this->assertEquals(3, sizeof($this->db->listTables())); USVN_Db_Utils::deleteAllTables($this->db); $this->assertEquals(0, sizeof($this->db->listTables())); }
/** * This method will test connection to the database, load database schemas * and finally write the config file. * * Throw an exception in case of problems. * * @param string Path to the USVN config file * @param string Path to the SQL files * @param string Database host * @param string Database user * @param string Database password * @param string Database name * @param string Database table prefix (ex: usvn_) * @param string Database type (mysql or sqlite) * @param boolean Create the database before installing * @throw USVN_Exception */ public static function installDb($config_file, $path_sql, $host, $user, $password, $database, $prefix, $adapter, $createdb) { $params = array('host' => $host, 'username' => $user, 'password' => $password, 'dbname' => $database); if ($createdb && ($adapter == 'PDO_MYSQL' || $adapter == 'MYSQLI')) { try { $tmp_params = $params; if ($adapter == 'PDO_MYSQL' || $adapter == 'MYSQLI') { $tmp_params['dbname'] = 'mysql'; } $db = Zend_Db::factory($adapter, $tmp_params); if ($adapter == 'PDO_MYSQL') { $db->query("CREATE DATABASE `{$database}`;"); } else { if ($adapter == 'MYSQLI') { $cnx = $db->getConnection(); $cnx->query("CREATE DATABASE `{$database}`;"); } } $db->closeConnection(); } catch (Exception $e) { throw new USVN_Exception(T_("Can't create database\n") . $e->getMessage()); } } try { $db = Zend_Db::factory($adapter, $params); $db->getConnection(); } catch (Exception $e) { throw new USVN_Exception(T_("Can't connect to database.\n") . ' ' . $e->getMessage()); } Zend_Db_Table::setDefaultAdapter($db); USVN_Db_Table::$prefix = $prefix; try { if ($adapter == 'PDO_MYSQL' || $adapter == 'MYSQLI') { USVN_Db_Utils::loadFile($db, $path_sql . '/mysql.sql'); } else { if ($adapter == 'PDO_SQLITE') { USVN_Db_Utils::loadFile($db, $path_sql . '/sqlite.sql'); } else { throw new USVN_Exception(T_("Invalid adapter %s.\n") . $adapter); } } } catch (Exception $e) { try { USVN_Db_Utils::deleteAllTables($db, $prefix); } catch (Exception $e2) { } $db->closeConnection(); throw new USVN_Exception(T_("Can't load SQL file.\n") . $e->getMessage()); } $db->closeConnection(); try { $config = Install::_loadConfig($config_file); $array = array('adapterName' => $adapter, 'prefix' => $prefix, 'options' => array('host' => $host, 'username' => $user, 'password' => $password, 'dbname' => $database)); if ($adapter == 'PDO_SQLITE') { unset($array['options']['host']); unset($array['options']['username']); unset($array['options']['password']); } $config->database = $array; $config->save(); } catch (Exception $e) { USVN_Db_Utils::deleteAllTables($db, $prefix); $db->closeConnection(); throw new USVN_Exception(T_("Can't write config file %s.\n") . ' ' . $e->getMessage(), $config_file); } }