3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This file contains functions for creating and altering CiviCRM-tables structure.
22 * 'name' => TABLE_NAME,
23 * 'attributes' => ATTRIBUTES,
26 * 'name' => FIELD_NAME,
27 * // can be field, index, constraint
28 * 'type' => FIELD_SQL_TYPE,
29 * 'class' => FIELD_CLASS_TYPE,
30 * 'primary' => BOOLEAN,
31 * 'required' => BOOLEAN,
32 * 'searchable' => TRUE,
33 * 'fk_table_name' => FOREIGN_KEY_TABLE_NAME,
34 * 'fk_field_name' => FOREIGN_KEY_FIELD_NAME,
35 * 'comment' => COMMENT,
36 * 'default' => DEFAULT, )
40 class CRM_Core_BAO_SchemaHandler
{
43 * Create a CiviCRM-table
45 * @param array $params
48 * TRUE if successfully created, FALSE otherwise
51 public static function createTable($params) {
52 $sql = self
::buildTableSQL($params);
53 // do not i18n-rewrite
54 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
56 if (CRM_Core_Config
::singleton()->logging
) {
58 $logging = new CRM_Logging_Schema();
59 $logging->fixSchemaDifferencesFor($params['name']);
62 // always do a trigger rebuild for this table
63 Civi
::service('sql_triggers')->rebuild($params['name'], TRUE);
69 * @param array $params
73 public static function buildTableSQL($params) {
74 $sql = "CREATE TABLE {$params['name']} (";
75 if (isset($params['fields']) &&
76 is_array($params['fields'])
80 foreach ($params['fields'] as $field) {
81 $sql .= self
::buildFieldSQL($field, $separator, $prefix);
84 foreach ($params['fields'] as $field) {
85 $sql .= self
::buildPrimaryKeySQL($field, $separator, $prefix);
87 foreach ($params['fields'] as $field) {
88 $sql .= self
::buildSearchIndexSQL($field, $separator, $prefix);
90 if (isset($params['indexes'])) {
91 foreach ($params['indexes'] as $index) {
92 $sql .= self
::buildIndexSQL($index, $separator, $prefix);
95 foreach ($params['fields'] as $field) {
96 $sql .= self
::buildForeignKeySQL($field, $separator, $prefix, $params['name']);
99 $sql .= "\n) {$params['attributes']};";
104 * @param array $params
110 public static function buildFieldSQL($params, $separator, $prefix) {
113 $sql .= str_repeat(' ', 8);
115 $sql .= "`{$params['name']}` {$params['type']}";
117 if (!empty($params['required'])) {
121 if (!empty($params['attributes'])) {
122 $sql .= " {$params['attributes']}";
125 if (!empty($params['default']) &&
126 $params['type'] != 'text'
128 $sql .= " DEFAULT {$params['default']}";
131 if (!empty($params['comment'])) {
132 $sql .= " COMMENT '{$params['comment']}'";
139 * @param array $params
143 * @return NULL|string
145 public static function buildPrimaryKeySQL($params, $separator, $prefix) {
147 if (!empty($params['primary'])) {
149 $sql .= str_repeat(' ', 8);
151 $sql .= "PRIMARY KEY ( {$params['name']} )";
157 * @param array $params
160 * @param bool $indexExist
162 * @return NULL|string
164 public static function buildSearchIndexSQL($params, $separator, $prefix, $indexExist = FALSE) {
168 if ($params['type'] == 'text') {
172 //create index only for searchable fields during ADD,
173 //create index only if field is become searchable during MODIFY,
174 //drop index only if field is no longer searchable and it does not reference
175 //a forgein key (and indexExist is true)
176 if (!empty($params['searchable']) && !$indexExist) {
178 $sql .= str_repeat(' ', 8);
180 $sql .= "INDEX_{$params['name']} ( {$params['name']} )";
182 elseif (empty($params['searchable']) && empty($params['fk_table_name']) && $indexExist) {
184 $sql .= str_repeat(' ', 8);
185 $sql .= "DROP INDEX INDEX_{$params['name']}";
191 * @param array $params
197 public static function buildIndexSQL(&$params, $separator, $prefix) {
200 $sql .= str_repeat(' ', 8);
201 if ($params['unique']) {
202 $sql .= 'UNIQUE INDEX';
203 $indexName = 'unique';
207 $indexName = 'index';
211 foreach ($params as $name => $value) {
212 if (substr($name, 0, 11) == 'field_name_') {
213 $indexName .= "_{$value}";
214 $indexFields .= " $value,";
217 $indexFields = substr($indexFields, 0, -1);
219 $sql .= " $indexName ( $indexFields )";
224 * @param string $tableName
225 * @param string $fkTableName
229 public static function changeFKConstraint($tableName, $fkTableName) {
230 $fkName = "{$tableName}_entity_id";
231 if (strlen($fkName) >= 48) {
232 $fkName = substr($fkName, 0, 32) . "_" . substr(md5($fkName), 0, 16);
235 ALTER TABLE {$tableName}
236 DROP FOREIGN KEY `FK_{$fkName}`;";
238 CRM_Core_DAO
::executeQuery($dropFKSql);
241 ALTER TABLE {$tableName}
242 ADD CONSTRAINT `FK_{$fkName}` FOREIGN KEY (`entity_id`) REFERENCES {$fkTableName} (`id`) ON DELETE CASCADE;";
243 // CRM-7007: do not i18n-rewrite this query
244 CRM_Core_DAO
::executeQuery($addFKSql, [], TRUE, NULL, FALSE, FALSE);
250 * @param array $params
253 * @param string $tableName
255 * @return NULL|string
257 public static function buildForeignKeySQL($params, $separator, $prefix, $tableName) {
259 if (!empty($params['fk_table_name']) && !empty($params['fk_field_name'])) {
261 $sql .= str_repeat(' ', 8);
263 $fkName = "{$tableName}_{$params['name']}";
264 if (strlen($fkName) >= 48) {
265 $fkName = substr($fkName, 0, 32) . "_" . substr(md5($fkName), 0, 16);
268 $sql .= "CONSTRAINT FK_$fkName FOREIGN KEY ( `{$params['name']}` ) REFERENCES {$params['fk_table_name']} ( {$params['fk_field_name']} ) ";
269 $sql .= CRM_Utils_Array
::value('fk_attributes', $params);
277 * @param array $params
278 * @param bool $indexExist
279 * @param bool $triggerRebuild
283 public static function alterFieldSQL($params, $indexExist = FALSE, $triggerRebuild = TRUE) {
284 CRM_Core_Error
::deprecatedFunctionWarning('function no longer in use / supported');
285 // lets suppress the required flag, since that can cause sql issue
286 $params['required'] = FALSE;
288 $sql = self
::buildFieldChangeSql($params, $indexExist);
290 // CRM-7007: do not i18n-rewrite this query
291 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
293 $config = CRM_Core_Config
::singleton();
294 if ($config->logging
) {
295 // CRM-16717 not sure why this was originally limited to add.
296 // For example custom tables can have field length changes - which need to flow through to logging.
297 // Are there any modifies we DON'T was to call this function for (& shouldn't it be clever enough to cope?)
298 if ($params['operation'] == 'add' ||
$params['operation'] == 'modify') {
299 $logging = new CRM_Logging_Schema();
300 $logging->fixSchemaDifferencesFor($params['table_name'], [trim(strtoupper($params['operation'])) => [$params['name']]]);
304 if ($triggerRebuild) {
305 Civi
::service('sql_triggers')->rebuild($params['table_name'], TRUE);
312 * Delete a CiviCRM-table.
314 * @param string $tableName
315 * Name of the table to be created.
317 public static function dropTable($tableName) {
318 $sql = "DROP TABLE $tableName";
319 CRM_Core_DAO
::executeQuery($sql);
323 * @param string $tableName
324 * @param string $columnName
326 * @param bool $isUpgradeMode
329 public static function dropColumn($tableName, $columnName, $l18n = FALSE, $isUpgradeMode = FALSE) {
330 if (self
::checkIfFieldExists($tableName, $columnName)) {
331 $sql = "ALTER TABLE $tableName DROP COLUMN $columnName";
333 CRM_Core_DAO
::executeQuery($sql);
336 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
338 $locales = CRM_Core_I18n
::getMultilingual();
340 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, NULL, $isUpgradeMode);
346 * @param string $tableName
347 * @param bool $dropUnique
349 public static function changeUniqueToIndex($tableName, $dropUnique = TRUE) {
351 $sql = "ALTER TABLE $tableName
352 DROP INDEX `unique_entity_id` ,
353 ADD INDEX `FK_{$tableName}_entity_id` ( `entity_id` )";
356 $sql = " ALTER TABLE $tableName
357 DROP INDEX `FK_{$tableName}_entity_id` ,
358 ADD UNIQUE INDEX `unique_entity_id` ( `entity_id` )";
360 CRM_Core_DAO
::executeQuery($sql);
367 * Tables to create index for in the format:
368 * ['civicrm_entity_table' => ['entity_id']]
370 * array['civicrm_entity_table' => array['entity_id', 'entity_table']]
371 * The latter will create a combined index on the 2 keys (in order).
373 * Side note - when creating combined indexes the one with the most variation
374 * goes first - so entity_table always goes after entity_id.
376 * It probably makes sense to consider more sophisticated options at some point
377 * but at the moment this is only being as enhanced as fast as the test is.
379 * @todo add support for length & multilingual on combined keys.
381 * @param string $createIndexPrefix
382 * @param array $substrLengths
384 public static function createIndexes($tables, $createIndexPrefix = 'index', $substrLengths = []) {
386 $locales = CRM_Core_I18n
::getMultilingual();
388 // if we're multilingual, cache the information on internationalised fields
389 static $columns = NULL;
390 if (!CRM_Utils_System
::isNull($locales) and $columns === NULL) {
391 $columns = CRM_Core_I18n_SchemaStructure
::columns();
394 foreach ($tables as $table => $fields) {
395 $query = "SHOW INDEX FROM $table";
396 $dao = CRM_Core_DAO
::executeQuery($query);
398 $currentIndexes = [];
399 while ($dao->fetch()) {
400 $currentIndexes[] = $dao->Key_name
;
403 // now check for all fields if the index exists
404 foreach ($fields as $field) {
405 $fieldName = implode('_', (array) $field);
407 if (is_array($field)) {
408 // No support for these for combined indexes as yet - add a test when you
414 // handle indices over substrings, CRM-6245
415 // $lengthName is appended to index name, $lengthSize is the field size modifier
416 $lengthName = isset($substrLengths[$table][$fieldName]) ?
"_{$substrLengths[$table][$fieldName]}" : '';
417 $lengthSize = isset($substrLengths[$table][$fieldName]) ?
"({$substrLengths[$table][$fieldName]})" : '';
421 "index_{$fieldName}{$lengthName}",
422 "FK_{$table}_{$fieldName}{$lengthName}",
423 "UI_{$fieldName}{$lengthName}",
424 "{$createIndexPrefix}_{$fieldName}{$lengthName}",
427 // skip to the next $field if one of the above $names exists; handle multilingual for CRM-4126
428 foreach ($names as $name) {
429 $regex = '/^' . preg_quote($name) . '(_[a-z][a-z]_[A-Z][A-Z])?$/';
430 if (preg_grep($regex, $currentIndexes)) {
435 // the index doesn't exist, so create it
436 // if we're multilingual and the field is internationalised, do it for every locale
437 // @todo remove is_array check & add multilingual support for combined indexes and add a test.
438 // Note combined indexes currently using this function are on fields like
439 // entity_id + entity_table which are not multilingual.
440 if (!is_array($field) && !CRM_Utils_System
::isNull($locales) and isset($columns[$table][$fieldName])) {
441 foreach ($locales as $locale) {
442 $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName}_{$locale} ON {$table} ({$fieldName}_{$locale}{$lengthSize})";
446 $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName} ON {$table} (" . implode(',', (array) $field) . "{$lengthSize})";
451 // run the queries without i18n-rewriting
452 $dao = new CRM_Core_DAO();
453 foreach ($queries as $query) {
454 $dao->query($query, FALSE);
459 * Get indexes for tables
460 * @param array $tables
461 * array of table names to find indexes for
463 * @return array('tableName' => array('index1', 'index2'))
465 public static function getIndexes($tables) {
467 foreach ($tables as $table) {
468 $query = "SHOW INDEX FROM $table";
469 $dao = CRM_Core_DAO
::executeQuery($query);
472 while ($dao->fetch()) {
473 $tableIndexes[$dao->Key_name
]['name'] = $dao->Key_name
;
474 $tableIndexes[$dao->Key_name
]['field'][] = $dao->Column_name
.
475 ($dao->Sub_part ?
'(' . $dao->Sub_part
. ')' : '');
476 $tableIndexes[$dao->Key_name
]['unique'] = ($dao->Non_unique
== 0 ?
1 : 0);
478 $indexes[$table] = $tableIndexes;
484 * Drop an index if one by that name exists.
486 * @param string $tableName
487 * @param string $indexName
489 public static function dropIndexIfExists($tableName, $indexName) {
490 if (self
::checkIfIndexExists($tableName, $indexName)) {
491 CRM_Core_DAO
::executeQuery("DROP INDEX $indexName ON $tableName");
496 * @param int $customFieldID
497 * @param string $tableName
498 * @param string $columnName
501 * @throws CRM_Core_Exception
503 public static function alterFieldLength($customFieldID, $tableName, $columnName, $length) {
504 // first update the custom field tables
506 UPDATE civicrm_custom_field
511 1 => [$length, 'Integer'],
512 2 => [$customFieldID, 'Integer'],
514 CRM_Core_DAO
::executeQuery($sql, $params);
517 SELECT is_required, default_value
518 FROM civicrm_custom_field
521 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
526 if ($dao->is_required
) {
527 $clause = " NOT NULL";
530 if (!empty($dao->default_value
)) {
531 $clause .= " DEFAULT '{$dao->default_value}'";
533 // now modify the column
535 ALTER TABLE {$tableName}
536 MODIFY {$columnName} varchar( $length )
539 CRM_Core_DAO
::executeQuery($sql);
542 throw new CRM_Core_Exception(ts('Could Not Find Custom Field Details for %1, %2, %3',
553 * Check if the table has an index matching the name.
555 * @param string $tableName
556 * @param string $indexName
560 public static function checkIfIndexExists($tableName, $indexName) {
561 $result = CRM_Core_DAO
::executeQuery(
562 "SHOW INDEX FROM $tableName WHERE key_name = %1 AND seq_in_index = 1",
563 [1 => [$indexName, 'String']]
565 if ($result->fetch()) {
572 * Check if the table has a specified column.
574 * @param string $tableName
575 * @param string $columnName
576 * @param bool $i18nRewrite
577 * Whether to rewrite the query on multilingual setups.
581 public static function checkIfFieldExists($tableName, $columnName, $i18nRewrite = TRUE) {
582 $query = "SHOW COLUMNS FROM $tableName LIKE '%1'";
583 $dao = CRM_Core_DAO
::executeQuery($query, [1 => [$columnName, 'Alphanumeric']], TRUE, NULL, FALSE, $i18nRewrite);
584 return (bool) $dao->fetch();
588 * Check if a foreign key Exists
589 * @param string $table_name
590 * @param string $constraint_name
591 * @return bool TRUE if FK is found
593 public static function checkFKExists($table_name, $constraint_name) {
594 $config = CRM_Core_Config
::singleton();
595 $dsn = CRM_Utils_SQL
::autoSwitchDSN($config->dsn
);
596 $dbUf = DB
::parseDSN($dsn);
598 SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
599 WHERE TABLE_SCHEMA = %1
601 AND CONSTRAINT_NAME = %3
602 AND CONSTRAINT_TYPE = 'FOREIGN KEY'
605 1 => [$dbUf['database'], 'String'],
606 2 => [$table_name, 'String'],
607 3 => [$constraint_name, 'String'],
609 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, NULL, FALSE, FALSE);
618 * Remove a foreign key from a table if it exists.
621 * @param $constraint_name
625 public static function safeRemoveFK($table_name, $constraint_name) {
626 if (self
::checkFKExists($table_name, $constraint_name)) {
627 CRM_Core_DAO
::executeQuery("ALTER TABLE {$table_name} DROP FOREIGN KEY {$constraint_name}", [], TRUE, NULL, FALSE, FALSE);
634 * Add index signature hash to DAO file calculation.
636 * @param string $table table name
637 * @param array $indices index array spec
639 public static function addIndexSignature($table, &$indices) {
640 foreach ($indices as $indexName => $index) {
641 $indices[$indexName]['sig'] = $table . "::" .
642 (array_key_exists('unique', $index) ?
$index['unique'] : 0) . "::" .
643 implode("::", $index['field']);
648 * Compare the indices specified in the XML files with those in the DB.
650 * @param bool $dropFalseIndices
651 * If set - this function deletes false indices present in the DB which mismatches the expected
652 * values of xml file so that civi re-creates them with correct values using createMissingIndices() function.
653 * @param array|FALSE $tables
654 * An optional array of tables - if provided the results will be restricted to these tables.
657 * index specifications
659 public static function getMissingIndices($dropFalseIndices = FALSE, $tables = FALSE) {
660 $requiredSigs = $existingSigs = [];
661 // Get the indices defined (originally) in the xml files
662 $requiredIndices = CRM_Core_DAO_AllCoreTables
::indices();
664 if ($tables !== FALSE) {
665 $requiredIndices = array_intersect_key($requiredIndices, array_fill_keys($tables, TRUE));
667 foreach ($requiredIndices as $table => $indices) {
668 $reqSigs[] = CRM_Utils_Array
::collect('sig', $indices);
670 CRM_Utils_Array
::flatten($reqSigs, $requiredSigs);
672 // Get the indices in the database
673 $existingIndices = CRM_Core_BAO_SchemaHandler
::getIndexes(array_keys($requiredIndices));
675 foreach ($existingIndices as $table => $indices) {
676 CRM_Core_BAO_SchemaHandler
::addIndexSignature($table, $indices);
677 $extSigs[] = CRM_Utils_Array
::collect('sig', $indices);
679 CRM_Utils_Array
::flatten($extSigs, $existingSigs);
682 $missingSigs = array_diff($requiredSigs, $existingSigs);
684 //CRM-20774 - Drop index key which exist in db but the value varies.
685 $existingKeySigs = array_intersect_key($missingSigs, $existingSigs);
686 if ($dropFalseIndices && !empty($existingKeySigs)) {
687 foreach ($existingKeySigs as $sig) {
688 $sigParts = explode('::', $sig);
689 foreach ($requiredIndices[$sigParts[0]] as $index) {
690 if ($index['sig'] == $sig && !empty($index['name'])) {
691 self
::dropIndexIfExists($sigParts[0], $index['name']);
698 // Get missing indices
699 $missingIndices = [];
700 foreach ($missingSigs as $sig) {
701 $sigParts = explode('::', $sig);
702 if (array_key_exists($sigParts[0], $requiredIndices)) {
703 foreach ($requiredIndices[$sigParts[0]] as $index) {
704 if ($index['sig'] == $sig) {
705 $missingIndices[$sigParts[0]][] = $index;
711 return $missingIndices;
715 * Create missing indices.
717 * @param array $missingIndices as returned by getMissingIndices()
719 public static function createMissingIndices($missingIndices) {
721 foreach ($missingIndices as $table => $indexList) {
722 foreach ($indexList as $index) {
723 $queries[] = "CREATE " .
724 (array_key_exists('unique', $index) && $index['unique'] ?
'UNIQUE ' : '') .
725 "INDEX {$index['name']} ON {$table} (" .
726 implode(", ", $index['field']) .
731 /* FIXME potential problem if index name already exists, so check before creating */
732 $dao = new CRM_Core_DAO();
733 foreach ($queries as $query) {
734 $dao->query($query, FALSE);
739 * Build the sql to alter the field.
741 * @param array $params
742 * @param bool $indexExist
746 public static function buildFieldChangeSql($params, $indexExist) {
747 $sql = str_repeat(' ', 8);
748 $sql .= "ALTER TABLE {$params['table_name']}";
749 return $sql . self
::getFieldAlterSQL($params, $indexExist);
753 * Get the sql to alter an individual field.
755 * This will need to have an ALTER TABLE statement appended but by getting
756 * by individual field we can do one or many.
758 * @param array $params
759 * @param bool $indexExist
763 public static function getFieldAlterSQL($params, $indexExist) {
765 switch ($params['operation']) {
768 $sql .= self
::buildFieldSQL($params, $separator, "ADD COLUMN ");
770 $sql .= self
::buildPrimaryKeySQL($params, $separator, "ADD PRIMARY KEY ");
771 $sql .= self
::buildSearchIndexSQL($params, $separator, "ADD INDEX ");
772 $sql .= self
::buildForeignKeySQL($params, $separator, "ADD ", $params['table_name']);
777 $sql .= self
::buildFieldSQL($params, $separator, "MODIFY ");
779 $sql .= self
::buildSearchIndexSQL($params, $separator, "ADD INDEX ", $indexExist);
783 $sql .= " DROP COLUMN `{$params['name']}`";
784 if (!empty($params['primary'])) {
785 $sql .= ", DROP PRIMARY KEY";
787 if (!empty($params['fk_table_name'])) {
788 $sql .= ", DROP FOREIGN KEY FK_{$params['fkName']}";
796 * Performs the utf8mb4 migration.
798 * @param bool $revert
799 * Being able to revert if primarily for unit testing.
800 * @param array $patterns
801 * Defaults to ['civicrm\_%'] but can be overridden to specify any pattern. eg ['civicrm\_%', 'civi%\_%', 'veda%\_%'].
802 * @param array $databaseList
803 * Allows you to specify an alternative database to the configured CiviCRM database.
807 public static function migrateUtf8mb4($revert = FALSE, $patterns = ['civicrm\_%'], $databaseList = NULL) {
808 $newCharSet = $revert ?
'utf8' : 'utf8mb4';
809 $newCollation = $revert ?
'utf8_unicode_ci' : 'utf8mb4_unicode_ci';
810 $newBinaryCollation = $revert ?
'utf8_bin' : 'utf8mb4_bin';
812 $dao = new CRM_Core_DAO();
813 $databases = $databaseList ??
[$dao->_database
];
815 $tableNameLikePatterns = [];
816 $logTableNameLikePatterns = [];
818 foreach ($patterns as $pattern) {
819 $pattern = CRM_Utils_Type
::escape($pattern, 'String');
820 $tableNameLikePatterns[] = "Name LIKE '{$pattern}'";
821 $logTableNameLikePatterns[] = "Name LIKE 'log\_{$pattern}'";
824 foreach ($databases as $database) {
825 CRM_Core_DAO
::executeQuery("ALTER DATABASE $database CHARACTER SET = $newCharSet COLLATE = $newCollation");
826 $dao = CRM_Core_DAO
::executeQuery("SHOW TABLE STATUS FROM `{$database}` WHERE Engine = 'InnoDB' AND (" . implode(' OR ', $tableNameLikePatterns) . ")");
827 while ($dao->fetch()) {
828 $tables["{$database}.{$dao->Name}"] = [
829 'Engine' => $dao->Engine
,
833 // If we specified a list of databases assume the user knows what they are doing.
834 // If they specify the database they should also specify the pattern.
835 if (!$databaseList) {
836 $dsn = defined('CIVICRM_LOGGING_DSN') ? CRM_Utils_SQL
::autoSwitchDSN(CIVICRM_LOGGING_DSN
) : CRM_Utils_SQL
::autoSwitchDSN(CIVICRM_DSN
);
837 $dsn = DB
::parseDSN($dsn);
838 $logging_database = $dsn['database'];
839 $dao = CRM_Core_DAO
::executeQuery("SHOW TABLE STATUS FROM `{$logging_database}` WHERE Engine <> 'MyISAM' AND (" . implode(' OR ', $logTableNameLikePatterns) . ")");
840 while ($dao->fetch()) {
841 $tables["{$logging_database}.{$dao->Name}"] = [
842 'Engine' => $dao->Engine
,
846 foreach ($tables as $table => $param) {
847 $query = "ALTER TABLE $table";
848 $dao = CRM_Core_DAO
::executeQuery("SHOW FULL COLUMNS FROM $table", [], TRUE, NULL, FALSE, FALSE);
851 $tableCollation = $newCollation;
852 while ($dao->fetch()) {
853 if (!$dao->Collation ||
$dao->Collation
=== $newCollation ||
$dao->Collation
=== $newBinaryCollation) {
856 if (strpos($dao->Collation
, 'utf8') !== 0) {
860 if (strpos($dao->Collation
, '_bin') !== FALSE) {
861 $tableCollation = $newBinaryCollation;
864 $tableCollation = $newCollation;
866 if ($dao->Null === 'YES') {
873 if ($dao->Default !== NULL) {
875 $default = "DEFAULT %$index";
876 $params[$index] = [$dao->Default, 'String'];
878 elseif ($dao->Null === 'YES') {
879 $default = 'DEFAULT NULL';
882 $params[$index] = [$dao->Comment
, 'String'];
883 $query .= " MODIFY `{$dao->Field}` {$dao->Type} CHARACTER SET $newCharSet COLLATE $tableCollation $null $default {$dao->Extra} COMMENT %$index,";
885 $query .= " CHARACTER SET = $newCharSet COLLATE = $tableCollation";
886 if ($param['Engine'] === 'InnoDB') {
887 $query .= ' ROW_FORMAT = Dynamic KEY_BLOCK_SIZE = 0';
889 // Disable i18n rewrite.
890 CRM_Core_DAO
::executeQuery($query, $params, TRUE, NULL, FALSE, FALSE);
892 // Rebuild triggers and other schema reconciliation if needed.
893 $logging = new CRM_Logging_Schema();
894 $logging->fixSchemaDifferences();
899 * Get the database collation.
903 public static function getDBCollation() {
904 return CRM_Core_DAO
::singleValueQuery('SELECT @@collation_database');
908 * Get the collation actually being used by the tables in the database.
910 * The db collation may not match the collation used by the tables, get what is
911 * set on the tables (represented by civicrm_contact).
915 public static function getInUseCollation() {
916 if (!isset(\Civi
::$statics[__CLASS__
][__FUNCTION__
])) {
917 $dao = CRM_Core_DAO
::executeQuery('SHOW TABLE STATUS LIKE \'civicrm_contact\'');
919 \Civi
::$statics[__CLASS__
][__FUNCTION__
] = $dao->Collation
;
921 return \Civi
::$statics[__CLASS__
][__FUNCTION__
];
925 * Get the database collation.
929 public static function getDBCharset() {
930 return CRM_Core_DAO
::singleValueQuery('SELECT @@character_set_database');