N°1740 - Class Query invalid Datamodel - Attention: Setup mandatory when applying this (core datamodel migration)

This commit is contained in:
Eric
2018-11-13 11:18:10 +01:00
parent 63b08b0e70
commit f0e5128fb5
4 changed files with 97 additions and 8 deletions

View File

@@ -47,13 +47,12 @@ abstract class Query extends cmdbAbstractObject
MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeText("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeQueryAttCodeSet("fields", array("allowed_values"=>null,"max_items" => 1000, "query_field" => "oql", "sql"=>"fields", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array('oql'))));
// Display lists
MetaModel::Init_SetZListItems('details', array('name', 'description', 'fields')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('details', array('name', 'description')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('description')); // Attributes to be displayed for a list
// Search criteria
MetaModel::Init_SetZListItems('standard_search', array('name', 'description', 'fields')); // Criteria of the std search form
MetaModel::Init_SetZListItems('standard_search', array('name', 'description')); // Criteria of the std search form
MetaModel::Init_SetZListItems('default_search', array('name', 'description')); // Criteria of the default search form
// MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
}
@@ -78,6 +77,7 @@ class QueryOQL extends Query
MetaModel::Init_Params($aParams);
MetaModel::Init_InheritAttributes();
MetaModel::Init_AddAttribute(new AttributeOQL("oql", array("allowed_values"=>null, "sql"=>"oql", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeQueryAttCodeSet("fields", array("allowed_values"=>null,"max_items" => 1000, "query_field" => "oql", "sql"=>"fields", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array('oql'))));
// Display lists
MetaModel::Init_SetZListItems('details', array('name', 'description', 'oql', 'fields')); // Attributes to be displayed for the complete details

View File

@@ -459,6 +459,11 @@ class CMDBSource
return $res;
}
public static function CacheReset($sTable)
{
self::_TablesInfoCacheReset($sTable);
}
/**
* @return \mysqli
*/
@@ -967,9 +972,16 @@ class CMDBSource
// Cache the information about existing tables, and their fields
private static $m_aTablesInfo = array();
private static function _TablesInfoCacheReset()
private static function _TablesInfoCacheReset($sTableName = null)
{
self::$m_aTablesInfo = array();
if (is_null($sTableName))
{
self::$m_aTablesInfo = array();
}
else
{
self::$m_aTablesInfo[strtolower($sTableName)] = null;
}
}
/**

View File

@@ -565,9 +565,19 @@ class ApplicationInstaller
}
}
protected static function DoUpdateDBSchema(
$aSelectedModules, $sModulesDir, $aParamValues, $sTargetEnvironment = '', $bOldAddon = false, $sAppRootUrl = ''
)
/**
* @param $aSelectedModules
* @param $sModulesDir
* @param $aParamValues
* @param string $sTargetEnvironment
* @param bool $bOldAddon
* @param string $sAppRootUrl
*
* @throws \ConfigException
* @throws \CoreException
* @throws \MySQLException
*/
protected static function DoUpdateDBSchema($aSelectedModules, $sModulesDir, $aParamValues, $sTargetEnvironment = '', $bOldAddon = false, $sAppRootUrl = '')
{
SetupPage::log_info("Update Database Schema for environment '$sTargetEnvironment'.");
$sMode = $aParamValues['mode'];
@@ -588,6 +598,9 @@ class ApplicationInstaller
$oProductionEnv = new RunTimeEnvironment($sTargetEnvironment);
$oProductionEnv->InitDataModel($oConfig, true); // load data model only
// Migrate columns
self::MoveColumns($sDBPrefix);
// Migrate application data format
//
// priv_internalUser caused troubles because MySQL transforms table names to lower case under Windows
@@ -743,6 +756,18 @@ class ApplicationInstaller
SetupPage::log_info("Database Schema Successfully Updated for environment '$sTargetEnvironment'.");
}
/**
* @param string $sDBPrefix
*
* @throws \CoreException
* @throws \MySQLException
*/
protected static function MoveColumns($sDBPrefix)
{
// In 2.6.0 the 'fields' attribute has been moved from Query to QueryOQL for dependencies reasons
ModuleInstallerAPI::MoveColumnInDB($sDBPrefix.'priv_query', 'fields', $sDBPrefix.'priv_query_oql', 'fields');
}
protected static function AfterDBCreate(
$sModulesDir, $aParamValues, $sAdminUser, $sAdminPwd, $sAdminLanguage, $aSelectedModules, $sTargetEnvironment,
$bOldAddon

View File

@@ -224,4 +224,56 @@ abstract class ModuleInstallerAPI
}
}
/**
* Move a column from a table to another table providing:
* - The id matches
* - The original column exists
* - The destination column does not exist
*
* The values are copied as is.
*
* @param $sOrigTable
* @param $sOrigColumn
* @param $sDstTable
* @param $sDstColumn
*
* @throws \MySQLException
* @throws \CoreException
*/
public static function MoveColumnInDB($sOrigTable, $sOrigColumn, $sDstTable, $sDstColumn)
{
if (!MetaModel::DBExists(false))
{
// Install from scratch, no migration
return;
}
if (!CMDBSource::IsTable($sOrigTable) || !CMDBSource::IsField($sOrigTable, $sOrigColumn))
{
// Original field is not present
return;
}
if (!CMDBSource::IsTable($sDstTable) || CMDBSource::IsField($sDstTable, $sDstColumn))
{
// Destination field is already created
return;
}
// Create the destination field
$sSpec = CMDBSource::GetFieldSpec($sOrigTable, $sOrigColumn);
$sQueryAdd = "ALTER TABLE `{$sDstTable}` ADD `{$sDstColumn}` {$sSpec}";
CMDBSource::Query($sQueryAdd);
// Copy the data
$sQueryUpdate = "UPDATE `{$sDstTable}` AS d LEFT JOIN `{$sOrigTable}` AS o ON d.id = o.id SET d.`{$sDstColumn}` = o.`{$sOrigColumn}` WHERE 1";
CMDBSource::Query($sQueryUpdate);
// Drop original field
$sQueryDrop = "ALTER TABLE `{$sOrigTable}` DROP `{$sOrigColumn}`";
CMDBSource::Query($sQueryDrop);
CMDBSource::CacheReset($sOrigTable);
CMDBSource::CacheReset($sDstTable);
}
}