This commit is contained in:
Julio Cesar Laura Avendaño
2019-07-26 10:01:39 -04:00
parent b2f81d7873
commit 3c9c63429e
8 changed files with 271 additions and 175 deletions

View File

@@ -393,19 +393,20 @@ class database extends database_base
}
/**
* generate a sentence to add indexes or primary keys
* Generate a sentence to add indexes or primary keys
*
* @param string $table table name
* @param string $indexName index name
* @param array $keys array of keys
* @param string $indexType the index type
*
* @return string sql sentence
* @throws Exception
*/
public function generateAddKeysSQL($table, $indexName, $keys)
public function generateAddKeysSQL($table, $indexName, $keys, $indexType = 'INDEX')
{
try {
$indexType = 'INDEX';
if ($indexName === 'primaryKey' || $indexName === 'PRIMARY') {
$indexType = 'PRIMARY';
$indexName = 'KEY';
@@ -1037,10 +1038,11 @@ class database extends database_base
* @param string $tableName
* @param array $columns
* @param array $indexes
* @param array $fulltextIndexes
*
* @return string
*/
public function generateAddColumnsSql($tableName, $columns, $indexes = [])
public function generateAddColumnsSql($tableName, $columns, $indexes = [], $fulltextIndexes = [])
{
$indexesAlreadyAdded = [];
$sql = 'ALTER TABLE ' . $this->sQuoteCharacter . $tableName . $this->sQuoteCharacter . ' ';
@@ -1080,6 +1082,7 @@ class database extends database_base
}
$sql .= ', ';
}
// Add the normal indexes if are not "primaryKeys" already added
foreach ($indexes as $indexName => $indexColumns) {
$indexType = 'INDEX';
if ($indexName === 'primaryKey' || $indexName === 'PRIMARY') {
@@ -1097,6 +1100,15 @@ class database extends database_base
$sql = substr($sql, 0, -2);
$sql .= '), ';
}
// Add the "fulltext" indexes always
foreach ($fulltextIndexes as $indexName => $indexColumns) {
$sql .= 'ADD FULLTEXT ' . $indexName . ' (';
foreach ($indexColumns as $column) {
$sql .= $this->sQuoteCharacter . $column . $this->sQuoteCharacter . ', ';
}
$sql = substr($sql, 0, -2);
$sql .= '), ';
}
$sql = rtrim($sql, ', ');
return $sql;

View File

@@ -147,11 +147,13 @@ function run_upgrade($parameters, $args)
// The previous actions should be executed only the first time
$mainThread = false;
if ($numberOfWorkspaces === 1) {
// Displaying information of the unique workspace to upgrade
CLI::logging("UPGRADING DATABASE AND FILES OF WORKSPACE '{$workspace->name}' (1/1)\n");
}
}
if ($numberOfWorkspaces === 1) {
// Displaying information of the current workspace to upgrade
CLI::logging("UPGRADING DATABASE AND FILES OF WORKSPACE '{$workspace->name}' ($countWorkspace/$numberOfWorkspaces)\n");
// Build parameters
$arrayOptTranslation = [
'updateXml' => $updateXmlForms,
@@ -165,6 +167,9 @@ function run_upgrade($parameters, $args)
$workspace->upgrade($workspace->name, SYS_LANG, $arrayOptTranslation, $optionMigrateHistoryData);
$workspace->close();
} else {
// Displaying information of the current workspace to upgrade
CLI::logging("UPGRADING DATABASE AND FILES OF WORKSPACE '{$workspace->name}' ($countWorkspace/$numberOfWorkspaces)\n");
// Build arguments
$args = '--child';
$args .= $updateXmlForms ? '' : ' --no-xml';

View File

@@ -566,15 +566,14 @@ function run_database_import($args, $opts)
* Check if we need to execute an external program for each workspace
* If we apply the command for all workspaces we will need to execute one by one by redefining the constants
* @param string $args, workspaceName that we need to apply the database-upgrade
* @param string $opts
*
* @return void
*/
function run_database_upgrade($args, $opts)
function run_database_upgrade($args)
{
//Check if the command is executed by a specific workspace
if (count($args) === 1) {
database_upgrade('upgrade', $args);
database_upgrade($args);
} else {
$workspaces = get_workspaces_from_args($args);
foreach ($workspaces as $workspace) {
@@ -583,11 +582,6 @@ function run_database_upgrade($args, $opts)
}
}
function run_database_check($args, $opts)
{
database_upgrade("check", $args);
}
function run_migrate_new_cases_lists($args, $opts)
{
migrate_new_cases_lists("migrate", $args, $opts);
@@ -605,44 +599,32 @@ function run_migrate_list_unassigned($args, $opts)
/**
* This function is executed only by one workspace
* @param string $command, the specific actions must be: upgrade|check
* @param array $args, workspaceName for to apply the database-upgrade
*
* @return void
*/
function database_upgrade($command, $args)
function database_upgrade($args)
{
// Sanitize parameters sent
$filter = new InputFilter();
$command = $filter->xssFilterHard($command);
$args = $filter->xssFilterHard($args);
//Load the attributes for the workspace
$workspaces = get_workspaces_from_args($args);
$checkOnly = (strcmp($command, "check") == 0);
//Loop, read all the attributes related to the one workspace
$wsName = $workspaces[key($workspaces)]->name;
Bootstrap::setConstantsRelatedWs($wsName);
if ($checkOnly) {
print_r("Checking database in " . pakeColor::colorize($wsName, "INFO") . "\n");
} else {
print_r("Upgrading database in " . pakeColor::colorize($wsName, "INFO") . "\n");
}
// Load the attributes for the workspace
$workspaces = get_workspaces_from_args($args);
// Get the name of the first workspace
$wsName = $workspaces[key($workspaces)]->name;
// Initialize workspace values
Bootstrap::setConstantsRelatedWs($wsName);
// Print a informative message
print_r("Upgrading database in " . pakeColor::colorize($wsName, "INFO") . "\n");
// Loop to update the databases of all workspaces
foreach ($workspaces as $workspace) {
try {
$changes = $workspace->upgradeDatabase($checkOnly);
if ($changes != false) {
if ($checkOnly) {
echo "> " . pakeColor::colorize("Run upgrade", "INFO") . "\n";
echo " Tables (add = " . count($changes['tablesToAdd']);
echo ", alter = " . count($changes['tablesToAlter']) . ") ";
echo "- Indexes (add = " . count($changes['tablesWithNewIndex']) . "";
echo ", alter = " . count($changes['tablesToAlterIndex']) . ")\n";
} else {
echo "-> Schema fixed\n";
}
} else {
echo "> OK\n";
}
$workspace->upgradeDatabase();
} catch (Exception $e) {
G::outRes("> Error: " . CLI::error($e->getMessage()) . "\n");
}

View File

@@ -252,7 +252,7 @@ class WorkspaceTools
CLI::logging("* Start updating database schema...\n");
$start = microtime(true);
$this->upgradeDatabase();
$this->upgradeDatabase(false);
CLI::logging("* End updating database schema...(Completed on " . (microtime(true) - $start) . " seconds)\n");
CLI::logging("* Start updating translations...\n");
@@ -807,16 +807,17 @@ class WorkspaceTools
$oldSchema[$table][$field['Field']]['Default'] = $field['Default'];
}
//get indexes of each table SHOW INDEX FROM `ADDITIONAL_TABLES`; -- WHERE Key_name <> 'PRIMARY'
// Get indexes of each table SHOW INDEX FROM `ADDITIONAL_TABLES`;
$description = $database->executeQuery($database->generateTableIndexSQL($table));
foreach ($description as $field) {
if (!isset($oldSchema[$table]['INDEXES'])) {
$oldSchema[$table]['INDEXES'] = [];
$type = $field['Index_type'] != 'FULLTEXT' ? 'INDEXES' : 'FULLTEXT';
if (!isset($oldSchema[$table][$type])) {
$oldSchema[$table][$type] = [];
}
if (!isset($oldSchema[$table]['INDEXES'][$field['Key_name']])) {
$oldSchema[$table]['INDEXES'][$field['Key_name']] = [];
if (!isset($oldSchema[$table][$type][$field['Key_name']])) {
$oldSchema[$table][$type][$field['Key_name']] = [];
}
$oldSchema[$table]['INDEXES'][$field['Key_name']][] = $field['Column_name'];
$oldSchema[$table][$type][$field['Key_name']][] = $field['Column_name'];
}
}
@@ -1050,8 +1051,10 @@ class WorkspaceTools
/**
* Upgrade the workspace database to the latest system schema
*
* @param bool $includeIndexes
*/
public function upgradeDatabase()
public function upgradeDatabase($includeIndexes = true)
{
$this->initPropel(true);
P11835::$dbAdapter = $this->dbAdapter;
@@ -1059,7 +1062,7 @@ class WorkspaceTools
$systemSchema = System::getSystemSchema($this->dbAdapter);
$systemSchemaRbac = System::getSystemSchemaRbac($this->dbAdapter);// Get the RBAC Schema
$this->registerSystemTables(array_merge($systemSchema, $systemSchemaRbac));
$this->upgradeSchema($systemSchema, false, false, false); // Without add indexes
$this->upgradeSchema($systemSchema, false, false, $includeIndexes);
$this->upgradeSchema($systemSchemaRbac, false, true); // Perform upgrade to RBAC
$this->upgradeData();
$this->checkRbacPermissions();//check or add new permissions
@@ -1195,7 +1198,9 @@ class WorkspaceTools
$changes = System::compareSchema($workspaceSchema, $schema);
$changed = (count($changes['tablesToAdd']) > 0 || count($changes['tablesToAlter']) > 0 || count($changes['tablesWithNewIndex']) > 0 || count($changes['tablesToAlterIndex']) > 0);
$changed = (count($changes['tablesToAdd']) > 0 || count($changes['tablesToAlter']) > 0 ||
count($changes['tablesWithNewIndex']) > 0 || count($changes['tablesToAlterIndex']) > 0 ||
count($changes['tablesWithNewFulltext']) > 0 || count($changes['tablesToAlterFulltext']) > 0);
if ($checkOnly || (!$changed)) {
if ($changed) {
@@ -1229,6 +1234,7 @@ class WorkspaceTools
$tablesToAddColumns = [];
// Drop or change columns
foreach ($changes['tablesToAlter'] as $tableName => $actions) {
foreach ($actions as $action => $actionData) {
if ($action == 'ADD') {
@@ -1255,17 +1261,27 @@ class WorkspaceTools
}
}
// Add columns
if (!empty($tablesToAddColumns)) {
$upgradeQueries = [];
foreach ($tablesToAddColumns as $tableName => $tableColumn) {
// Normal indexes to add
$indexes = [];
if (!empty($changes['tablesWithNewIndex'][$tableName]) && $includeIndexes) {
$indexes = $changes['tablesWithNewIndex'][$tableName];
unset($changes['tablesWithNewIndex'][$tableName]);
}
// "fulltext" indexes to add
$fulltextIndexes = [];
if (!empty($changes['tablesWithNewFulltext'][$tableName]) && $includeIndexes) {
$fulltextIndexes = $changes['tablesWithNewFulltext'][$tableName];
unset($changes['tablesWithNewFulltext'][$tableName]);
}
// Instantiate the class to execute the query in background
$upgradeQueries[] = new RunProcessUpgradeQuery($this->name, $database->generateAddColumnsSql($tableName, $tableColumn, $indexes), $rbac);
$upgradeQueries[] = new RunProcessUpgradeQuery($this->name, $database->generateAddColumnsSql($tableName,
$tableColumn, $indexes, $fulltextIndexes), $rbac);
}
// Run queries in multiple threads
@@ -1282,14 +1298,24 @@ class WorkspaceTools
}
}
if (!empty($changes['tablesWithNewIndex']) && $includeIndexes) {
CLI::logging("-> " . count($changes['tablesWithNewIndex']) . " tables with indexes to add\n");
// Add indexes
if ((!empty($changes['tablesWithNewIndex']) || !empty($changes['tablesWithNewFulltext'])) && $includeIndexes) {
CLI::logging("-> " . (count($changes['tablesWithNewIndex']) + count($changes['tablesWithNewFulltext'])) .
" tables with indexes to add\n");
$upgradeQueries = [];
// Add normal indexes
foreach ($changes['tablesWithNewIndex'] as $tableName => $indexes) {
// Instantiate the class to execute the query in background
$upgradeQueries[] = new RunProcessUpgradeQuery($this->name, $database->generateAddColumnsSql($tableName, [], $indexes), $rbac);
}
// Add "fulltext" indexes
foreach ($changes['tablesWithNewFulltext'] as $tableName => $fulltextIndexes) {
// Instantiate the class to execute the query in background
$upgradeQueries[] = new RunProcessUpgradeQuery($this->name, $database->generateAddColumnsSql($tableName, [], [], $fulltextIndexes), $rbac);
}
// Run queries in multiple threads
$processesManager = new ProcessesManager($upgradeQueries);
$processesManager->run();
@@ -1304,16 +1330,30 @@ class WorkspaceTools
}
}
if (!empty($changes['tablesToAlterIndex']) && $includeIndexes) {
CLI::logging("-> " . count($changes['tablesToAlterIndex']) . " tables with indexes to alter\n");
// Change indexes
if ((!empty($changes['tablesToAlterIndex']) || !empty($changes['tablesToAlterFulltext'])) && $includeIndexes) {
CLI::logging("-> " . (count($changes['tablesToAlterIndex']) + count($changes['tablesToAlterFulltext'])) .
" tables with indexes to alter\n");
// Change normal indexes
foreach ($changes['tablesToAlterIndex'] as $tableName => $indexes) {
foreach ($indexes as $indexName => $indexFields) {
$database->executeQuery($database->generateDropKeySQL($tableName, $indexName));
$database->executeQuery($database->generateAddKeysSQL($tableName, $indexName, $indexFields));
}
}
// Change "fulltext" indexes
foreach ($changes['tablesToAlterFulltext'] as $tableName => $fulltextIndexes) {
foreach ($fulltextIndexes as $indexName => $indexFields) {
$database->executeQuery($database->generateDropKeySQL($tableName, $indexName));
$database->executeQuery($database->generateAddKeysSQL($tableName, $indexName, $indexFields, 'FULLTEXT'));
}
}
}
// Ending the schema update
CLI::logging("-> Schema Updated\n");
$this->closeDatabase();
return true;
}
@@ -2064,7 +2104,7 @@ class WorkspaceTools
// Upgrade the database schema and data
CLI::logging("* Start updating database schema...\n");
$start = microtime(true);
$workspace->upgradeDatabase();
$workspace->upgradeDatabase(false);
CLI::logging("* End updating database schema...(Completed on " . (microtime(true) - $start) . " seconds)\n");
CLI::logging("* Start checking MAFE requirements...\n");

View File

@@ -94,6 +94,15 @@
<parameter name="Seq_in_index" value="1"/>
</vendor>
</index>
<fulltext name="indexAppTitle">
<index-column name="APP_TITLE"/>
<vendor type="mysql">
<parameter name="Table" value="APPLICATION"/>
<parameter name="Non_unique" value="1"/>
<parameter name="Key_name" value="indexAppTitle"/>
<parameter name="Seq_in_index" value="1"/>
</vendor>
</fulltext>
<unique name="INDEX_APP_NUMBER">
<unique-column name="APP_NUMBER"/>
</unique>

View File

@@ -40,7 +40,8 @@ CREATE TABLE `APPLICATION`
KEY `indexApp`(`PRO_UID`, `APP_STATUS`, `APP_UID`),
KEY `indexAppNumber`(`APP_NUMBER`),
KEY `indexAppStatus`(`APP_STATUS`),
KEY `indexAppCreateDate`(`APP_CREATE_DATE`)
KEY `indexAppCreateDate`(`APP_CREATE_DATE`),
FULLTEXT `indexAppTitle`(`APP_TITLE`)
)ENGINE=InnoDB DEFAULT CHARSET='utf8' COMMENT='The application';
#-----------------------------------------------------------------------------
#-- APP_SEQUENCE

File diff suppressed because it is too large Load Diff

View File

@@ -225,7 +225,14 @@ class Delegation extends Model
$query->join('APPLICATION', function ($join) use ($filterBy, $search, $status, $query) {
$join->on('APP_DELEGATION.APP_NUMBER', '=', 'APPLICATION.APP_NUMBER');
if ($filterBy == 'APP_TITLE' && $search) {
$join->where('APPLICATION.APP_TITLE', 'LIKE', "%${search}%");
// Cleaning "fulltext" operators in order to avoid unexpected results
$search = str_replace(['-', '+', '<', '>', '(', ')', '~', '*', '"'], ['', '', '', '', '', '', '', '', ''], $search);
// Build the "fulltext" expression
$search = '+"' . preg_replace('/\s+/', '" +"', addslashes($search)) . '"';
// Searching using "fulltext" index
$join->whereRaw("MATCH(APPLICATION.APP_TITLE) AGAINST('{$search}' IN BOOLEAN MODE)");
}
// Based on the below, we can further limit the join so that we have a smaller data set based on join criteria
switch ($status) {