public static function setupTable($tableName, $sqlCreate) { if (!self::execute($sqlCreate)) { SystemEvent::raise(SystemEvent::ERROR, "Problems executing table create. This is non recoverable, please fix the problem and try again.", __METHOD__); self::rollbackTransaction(); return false; } $tables = self::getTables(); if (!empty($tables[$tableName])) { $attributes = Database::getTableInfo($tableName); if (empty($attributes)) { SystemEvent::raise(SystemEvent::ERROR, "Problems accessing old data attributes. This is non recoverable, please fix the problem and try again.", __METHOD__); self::rollbackTransaction(); return false; } $attributesNew = Database::getTableInfo($tableName . 'NEW'); if (empty($attributes)) { SystemEvent::raise(SystemEvent::ERROR, "Problems accessing new data attributes. This is non recoverable, please fix the problem and try again.", __METHOD__); self::rollbackTransaction(); return false; } // // This makes sure that only coincident attributes are corresponded, // i.e., old attributes that don't match anything in the current // schema, or vice-versa, are left out of the migration // $attributesSql = ''; foreach ($attributes as $attribute) { if (!empty($attributesNew[$attribute])) { $attributesSql .= "{$attribute},"; } } $attributesSqlNew = ''; foreach ($attributesNew as $attributeNew) { if (!empty($attributes[$attributeNew])) { $attributesSqlNew .= "{$attributeNew},"; } } $attributesSql = rtrim($attributesSql, ','); $attributesSqlNew = rtrim($attributesSqlNew, ','); $sql = "INSERT INTO {$tableName}NEW ({$attributesSqlNew}) SELECT {$attributesSql} FROM {$tableName}"; if (!self::execute($sql)) { SystemEvent::raise(SystemEvent::ERROR, "Problems migrating old data. This is non recoverable, please fix the problem and try again.", __METHOD__); self::rollbackTransaction(); return false; } } $sql = "DROP TABLE IF EXISTS {$tableName}"; if (!self::execute($sql)) { SystemEvent::raise(SystemEvent::ERROR, "Problems removing old data. This is non recoverable, please fix the problem and try again.", __METHOD__); self::rollbackTransaction(); return false; } $sql = "ALTER TABLE {$tableName}NEW RENAME TO {$tableName}"; if (!self::execute($sql)) { SystemEvent::raise(SystemEvent::ERROR, "Problems setting up migrated data. This is non recoverable, please fix the problem and try again.", __METHOD__); self::rollbackTransaction(); return false; } return true; }