Exemple #1
0
 /**
  * Check if $dbinfo provided the connection details for a Yioop/SeekQuarry
  * database. If it does provide a valid db connection but no data then try
  * to recreate the database from the default copy stored in /data dir.
  *
  * @param array $dbinfo has fields for DBMS, DB_USER, DB_PASSWORD, DB_HOST
  *     and DB_NAME
  * @param array $skip_list an array of table or index names not to bother
  *     creating or copying
  * @return bool returns true if can connect to/create a valid database;
  *     returns false otherwise
  */
 function migrateDatabaseIfNecessary($dbinfo, $skip_list = array())
 {
     $test_dbm = $this->testDatabaseManager($dbinfo);
     if ($test_dbm === false || $test_dbm === true) {
         return $test_dbm;
     }
     $this->initializeSql($test_dbm, $dbinfo);
     $copy_tables = array_diff(array_keys($this->create_statements), $skip_list);
     if (!($create_ok = $this->createDatabaseTables($test_dbm, $dbinfo))) {
         return false;
     }
     require_once BASE_DIR . "/models/datasources/sqlite3_manager.php";
     $default_dbm = new Sqlite3Manager();
     $default_dbm->connect("", "", "", BASE_DIR . "/data/default.db");
     if (!$default_dbm) {
         return false;
     }
     foreach ($copy_tables as $table_or_index) {
         if ($table_or_index != "CURRENT_WEB_INDEX" && stristr($table_or_index, "_INDEX")) {
             continue;
         }
         if (!DatasourceManager::copyTable($table_or_index, $default_dbm, $table_or_index, $test_dbm)) {
             return false;
         }
     }
     if (stristr($dbinfo["DB_HOST"], "pgsql") !== false) {
         /* For postgres count initial values of SERIAL sequences
              will be screwed up unless do
            */
         $auto_tables = array("ACTIVITY" => "ACTIVITY_ID", "GROUP_ITEM" => "GROUP_ITEM_ID", "GROUP_PAGE" => "GROUP_PAGE_ID", "GROUPS" => "GROUP_ID", "LOCALE" => "LOCALE_ID", "ROLE" => "ROLE_ID", "TRANSLATION" => "TRANSLATION_ID", "USERS" => "USER_ID");
         foreach ($auto_tables as $table => $auto_column) {
             $sql = "SELECT MAX({$auto_column}) AS NUM FROM {$table}";
             $result = $test_dbm->execute($sql);
             $row = $test_dbm->fetchArray($result);
             $next = $row['NUM'];
             $sequence = strtolower("{$table}_{$auto_column}_seq");
             $sql = "SELECT setval('{$sequence}', {$next})";
             $test_dbm->execute($sql);
         }
     }
     return true;
 }
Exemple #2
0
/**
 * Upgrades a Version 23 version of the Yioop! database to a Version 24 version
 * @param object $db datasource to use to upgrade
 */
function upgradeDatabaseVersion24(&$db)
{
    /** Get base class for profile_model.php*/
    require_once BASE_DIR . "/models/model.php";
    /** For ProfileModel::createDatabaseTables method */
    require_once BASE_DIR . "/models/profile_model.php";
    $db->execute("DELETE FROM VERSION WHERE ID < 23");
    $db->execute("UPDATE VERSION SET ID=24 WHERE ID=23");
    $profile_model = new ProfileModel(DB_NAME, false);
    $profile_model->db = $db;
    $dbinfo = array("DBMS" => DBMS, "DB_HOST" => DB_HOST, "DB_USER" => DB_USER, "DB_PASSWORD" => DB_PASSWORD, "DB_NAME" => DB_NAME);
    $profile_model->initializeSql($db, $dbinfo);
    foreach ($profile_model->create_statements as $object_name => $statement) {
        if (stristr($object_name, "_INDEX")) {
            if (!$db->execute($statement)) {
                echo $statement . " ERROR!";
                exit;
            }
        } else {
            if (!$db->execute("ALTER TABLE {$object_name} RENAME TO " . $object_name . "_OLD")) {
                echo "RENAME {$object_name} ERROR!";
                exit;
            }
            if (!$db->execute($statement)) {
                echo $statement . " ERROR!";
                exit;
            }
            DatasourceManager::copyTable($object_name . "_OLD", $db, $object_name, $db);
            $db->execute("DROP TABLE " . $object_name . "_OLD");
        }
    }
}