[REF] - Add helper function for the repetitive task of fetching multilingual
[civicrm-core.git] / CRM / Core / BAO / SchemaHandler.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
db01bf2f 19 * This file contains functions for creating and altering CiviCRM-tables structure.
6a488035
TO
20 *
21 * $table = array(
db01bf2f 22 * 'name' => TABLE_NAME,
23 * 'attributes' => ATTRIBUTES,
24 * 'fields' => array(
25 * array(
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, )
37 * ...
38 * ));
6a488035
TO
39 */
40class CRM_Core_BAO_SchemaHandler {
41
42 /**
c490a46a 43 * Create a CiviCRM-table
6a488035 44 *
c490a46a 45 * @param array $params
6a488035 46 *
72b3a70c
CW
47 * @return bool
48 * TRUE if successfully created, FALSE otherwise
6a488035 49 *
6a488035 50 */
6477f387 51 public static function createTable($params) {
6a488035
TO
52 $sql = self::buildTableSQL($params);
53 // do not i18n-rewrite
be2fb01f 54 CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
6a488035 55
6477f387 56 if (CRM_Core_Config::singleton()->logging) {
6a488035 57 // logging support
8d7a9d07 58 $logging = new CRM_Logging_Schema();
fcc20cec 59 $logging->fixSchemaDifferencesFor($params['name']);
6a488035
TO
60 }
61
62 // always do a trigger rebuild for this table
dfcc817d 63 Civi::service('sql_triggers')->rebuild($params['name'], TRUE);
6a488035
TO
64
65 return TRUE;
66 }
67
b5c2afd0 68 /**
c490a46a 69 * @param array $params
b5c2afd0
EM
70 *
71 * @return string
72 */
6477f387 73 public static function buildTableSQL($params) {
6a488035
TO
74 $sql = "CREATE TABLE {$params['name']} (";
75 if (isset($params['fields']) &&
76 is_array($params['fields'])
77 ) {
78 $separator = "\n";
79 $prefix = NULL;
80 foreach ($params['fields'] as $field) {
81 $sql .= self::buildFieldSQL($field, $separator, $prefix);
82 $separator = ",\n";
83 }
84 foreach ($params['fields'] as $field) {
85 $sql .= self::buildPrimaryKeySQL($field, $separator, $prefix);
86 }
87 foreach ($params['fields'] as $field) {
88 $sql .= self::buildSearchIndexSQL($field, $separator, $prefix);
89 }
90 if (isset($params['indexes'])) {
91 foreach ($params['indexes'] as $index) {
92 $sql .= self::buildIndexSQL($index, $separator, $prefix);
93 }
94 }
95 foreach ($params['fields'] as $field) {
96 $sql .= self::buildForeignKeySQL($field, $separator, $prefix, $params['name']);
97 }
98 }
99 $sql .= "\n) {$params['attributes']};";
100 return $sql;
101 }
102
b5c2afd0 103 /**
c490a46a 104 * @param array $params
b5c2afd0
EM
105 * @param $separator
106 * @param $prefix
107 *
108 * @return string
109 */
4f89ebb0 110 public static function buildFieldSQL($params, $separator, $prefix) {
6a488035
TO
111 $sql = '';
112 $sql .= $separator;
113 $sql .= str_repeat(' ', 8);
114 $sql .= $prefix;
115 $sql .= "`{$params['name']}` {$params['type']}";
116
a7488080 117 if (!empty($params['required'])) {
6a488035
TO
118 $sql .= " NOT NULL";
119 }
120
a7488080 121 if (!empty($params['attributes'])) {
6a488035
TO
122 $sql .= " {$params['attributes']}";
123 }
124
a7488080 125 if (!empty($params['default']) &&
6a488035
TO
126 $params['type'] != 'text'
127 ) {
128 $sql .= " DEFAULT {$params['default']}";
129 }
130
a7488080 131 if (!empty($params['comment'])) {
6a488035
TO
132 $sql .= " COMMENT '{$params['comment']}'";
133 }
134
135 return $sql;
136 }
137
b5c2afd0 138 /**
c490a46a 139 * @param array $params
b5c2afd0
EM
140 * @param $separator
141 * @param $prefix
142 *
e60f24eb 143 * @return NULL|string
b5c2afd0 144 */
4f89ebb0 145 public static function buildPrimaryKeySQL($params, $separator, $prefix) {
6a488035 146 $sql = NULL;
a7488080 147 if (!empty($params['primary'])) {
6a488035
TO
148 $sql .= $separator;
149 $sql .= str_repeat(' ', 8);
150 $sql .= $prefix;
151 $sql .= "PRIMARY KEY ( {$params['name']} )";
152 }
153 return $sql;
154 }
155
b5c2afd0 156 /**
c490a46a 157 * @param array $params
b5c2afd0
EM
158 * @param $separator
159 * @param $prefix
160 * @param bool $indexExist
161 *
e60f24eb 162 * @return NULL|string
b5c2afd0 163 */
4f89ebb0 164 public static function buildSearchIndexSQL($params, $separator, $prefix, $indexExist = FALSE) {
6a488035
TO
165 $sql = NULL;
166
167 // dont index blob
168 if ($params['type'] == 'text') {
169 return $sql;
170 }
171
172 //create index only for searchable fields during ADD,
173 //create index only if field is become searchable during MODIFY,
7ab8180f
MM
174 //drop index only if field is no longer searchable and it does not reference
175 //a forgein key (and indexExist is true)
a7488080 176 if (!empty($params['searchable']) && !$indexExist) {
6a488035
TO
177 $sql .= $separator;
178 $sql .= str_repeat(' ', 8);
179 $sql .= $prefix;
180 $sql .= "INDEX_{$params['name']} ( {$params['name']} )";
181 }
7ab8180f 182 elseif (empty($params['searchable']) && empty($params['fk_table_name']) && $indexExist) {
6a488035
TO
183 $sql .= $separator;
184 $sql .= str_repeat(' ', 8);
185 $sql .= "DROP INDEX INDEX_{$params['name']}";
186 }
187 return $sql;
188 }
189
b5c2afd0 190 /**
c490a46a 191 * @param array $params
b5c2afd0
EM
192 * @param $separator
193 * @param $prefix
194 *
195 * @return string
196 */
00be9182 197 public static function buildIndexSQL(&$params, $separator, $prefix) {
6a488035
TO
198 $sql = '';
199 $sql .= $separator;
200 $sql .= str_repeat(' ', 8);
201 if ($params['unique']) {
202 $sql .= 'UNIQUE INDEX';
203 $indexName = 'unique';
204 }
205 else {
206 $sql .= 'INDEX';
207 $indexName = 'index';
208 }
209 $indexFields = NULL;
210
211 foreach ($params as $name => $value) {
212 if (substr($name, 0, 11) == 'field_name_') {
213 $indexName .= "_{$value}";
214 $indexFields .= " $value,";
215 }
216 }
217 $indexFields = substr($indexFields, 0, -1);
218
219 $sql .= " $indexName ( $indexFields )";
220 return $sql;
221 }
222
b5c2afd0 223 /**
100fef9d
CW
224 * @param string $tableName
225 * @param string $fkTableName
b5c2afd0
EM
226 *
227 * @return bool
228 */
00be9182 229 public static function changeFKConstraint($tableName, $fkTableName) {
6a488035
TO
230 $fkName = "{$tableName}_entity_id";
231 if (strlen($fkName) >= 48) {
232 $fkName = substr($fkName, 0, 32) . "_" . substr(md5($fkName), 0, 16);
233 }
234 $dropFKSql = "
235ALTER TABLE {$tableName}
236 DROP FOREIGN KEY `FK_{$fkName}`;";
237
fccb50a6 238 CRM_Core_DAO::executeQuery($dropFKSql);
6a488035
TO
239
240 $addFKSql = "
241ALTER 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
be2fb01f 244 CRM_Core_DAO::executeQuery($addFKSql, [], TRUE, NULL, FALSE, FALSE);
6a488035
TO
245
246 return TRUE;
247 }
248
b5c2afd0 249 /**
c490a46a 250 * @param array $params
b5c2afd0
EM
251 * @param $separator
252 * @param $prefix
100fef9d 253 * @param string $tableName
b5c2afd0 254 *
e60f24eb 255 * @return NULL|string
b5c2afd0 256 */
4f89ebb0 257 public static function buildForeignKeySQL($params, $separator, $prefix, $tableName) {
6a488035 258 $sql = NULL;
8cc574cf 259 if (!empty($params['fk_table_name']) && !empty($params['fk_field_name'])) {
6a488035
TO
260 $sql .= $separator;
261 $sql .= str_repeat(' ', 8);
262 $sql .= $prefix;
263 $fkName = "{$tableName}_{$params['name']}";
264 if (strlen($fkName) >= 48) {
265 $fkName = substr($fkName, 0, 32) . "_" . substr(md5($fkName), 0, 16);
266 }
267
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);
270 }
271 return $sql;
272 }
273
b5c2afd0 274 /**
ead0c08f 275 * @deprecated
276 *
c490a46a 277 * @param array $params
b5c2afd0
EM
278 * @param bool $indexExist
279 * @param bool $triggerRebuild
280 *
281 * @return bool
282 */
6477f387 283 public static function alterFieldSQL($params, $indexExist = FALSE, $triggerRebuild = TRUE) {
3111965c 284 CRM_Core_Error::deprecatedFunctionWarning('function no longer in use / supported');
6a488035
TO
285 // lets suppress the required flag, since that can cause sql issue
286 $params['required'] = FALSE;
287
d992b2f5 288 $sql = self::buildFieldChangeSql($params, $indexExist);
6a488035
TO
289
290 // CRM-7007: do not i18n-rewrite this query
be2fb01f 291 CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
6a488035
TO
292
293 $config = CRM_Core_Config::singleton();
294 if ($config->logging) {
721a43a4
EM
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') {
8d7a9d07 299 $logging = new CRM_Logging_Schema();
fcc20cec 300 $logging->fixSchemaDifferencesFor($params['table_name'], [trim(strtoupper($params['operation'])) => [$params['name']]]);
6a488035
TO
301 }
302 }
303
22e263ad 304 if ($triggerRebuild) {
dfcc817d 305 Civi::service('sql_triggers')->rebuild($params['table_name'], TRUE);
6a488035
TO
306 }
307
308 return TRUE;
309 }
310
311 /**
49186f94 312 * Delete a CiviCRM-table.
6a488035 313 *
d3e86119 314 * @param string $tableName
6a0b768e 315 * Name of the table to be created.
6a488035 316 */
00be9182 317 public static function dropTable($tableName) {
6a488035 318 $sql = "DROP TABLE $tableName";
49186f94 319 CRM_Core_DAO::executeQuery($sql);
6a488035
TO
320 }
321
b5c2afd0 322 /**
100fef9d
CW
323 * @param string $tableName
324 * @param string $columnName
3abab0f8 325 * @param bool $l18n
41ace555 326 * @param bool $isUpgradeMode
87568e34 327 *
b5c2afd0 328 */
41ace555 329 public static function dropColumn($tableName, $columnName, $l18n = FALSE, $isUpgradeMode = FALSE) {
242055d3
CW
330 if (self::checkIfFieldExists($tableName, $columnName)) {
331 $sql = "ALTER TABLE $tableName DROP COLUMN $columnName";
3abab0f8 332 if ($l18n) {
87568e34
SL
333 CRM_Core_DAO::executeQuery($sql);
334 }
335 else {
be2fb01f 336 CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
87568e34 337 }
394d18d3
CW
338 $locales = CRM_Core_I18n::getMultilingual();
339 if ($locales) {
41ace555 340 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, NULL, $isUpgradeMode);
1285e488 341 }
242055d3 342 }
6a488035
TO
343 }
344
b5c2afd0 345 /**
100fef9d 346 * @param string $tableName
b5c2afd0
EM
347 * @param bool $dropUnique
348 */
00be9182 349 public static function changeUniqueToIndex($tableName, $dropUnique = TRUE) {
6a488035
TO
350 if ($dropUnique) {
351 $sql = "ALTER TABLE $tableName
352DROP INDEX `unique_entity_id` ,
353ADD INDEX `FK_{$tableName}_entity_id` ( `entity_id` )";
354 }
355 else {
356 $sql = " ALTER TABLE $tableName
357DROP INDEX `FK_{$tableName}_entity_id` ,
358ADD UNIQUE INDEX `unique_entity_id` ( `entity_id` )";
359 }
49186f94 360 CRM_Core_DAO::executeQuery($sql);
6a488035
TO
361 }
362
b5c2afd0 363 /**
7181119b 364 * Create indexes.
365 *
b5c2afd0 366 * @param $tables
7181119b 367 * Tables to create index for in the format:
368 * array('civicrm_entity_table' => 'entity_id')
369 * OR
370 * array('civicrm_entity_table' => array('entity_id', 'entity_table'))
371 * The latter will create a combined index on the 2 keys (in order).
372 *
373 * Side note - when creating combined indexes the one with the most variation
374 * goes first - so entity_table always goes after entity_id.
375 *
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.
378 *
379 * @todo add support for length & multilingual on combined keys.
380 *
b5c2afd0 381 * @param string $createIndexPrefix
fe891bc6 382 * @param array $substrLengths
b5c2afd0 383 */
be2fb01f
CW
384 public static function createIndexes($tables, $createIndexPrefix = 'index', $substrLengths = []) {
385 $queries = [];
394d18d3 386 $locales = CRM_Core_I18n::getMultilingual();
6a488035
TO
387
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();
392 }
393
394 foreach ($tables as $table => $fields) {
395 $query = "SHOW INDEX FROM $table";
396 $dao = CRM_Core_DAO::executeQuery($query);
397
be2fb01f 398 $currentIndexes = [];
6a488035
TO
399 while ($dao->fetch()) {
400 $currentIndexes[] = $dao->Key_name;
401 }
402
403 // now check for all fields if the index exists
404 foreach ($fields as $field) {
7181119b 405 $fieldName = implode('_', (array) $field);
406
407 if (is_array($field)) {
408 // No support for these for combined indexes as yet - add a test when you
409 // want to add that.
410 $lengthName = '';
411 $lengthSize = '';
412 }
413 else {
414 // handle indices over substrings, CRM-6245
415 // $lengthName is appended to index name, $lengthSize is the field size modifier
fe891bc6
AS
416 $lengthName = isset($substrLengths[$table][$fieldName]) ? "_{$substrLengths[$table][$fieldName]}" : '';
417 $lengthSize = isset($substrLengths[$table][$fieldName]) ? "({$substrLengths[$table][$fieldName]})" : '';
7181119b 418 }
6a488035 419
be2fb01f 420 $names = [
7181119b 421 "index_{$fieldName}{$lengthName}",
422 "FK_{$table}_{$fieldName}{$lengthName}",
423 "UI_{$fieldName}{$lengthName}",
424 "{$createIndexPrefix}_{$fieldName}{$lengthName}",
be2fb01f 425 ];
6a488035
TO
426
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)) {
431 continue 2;
432 }
433 }
434
435 // the index doesn't exist, so create it
436 // if we're multilingual and the field is internationalised, do it for every locale
7181119b 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])) {
6a488035 441 foreach ($locales as $locale) {
7181119b 442 $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName}_{$locale} ON {$table} ({$fieldName}_{$locale}{$lengthSize})";
6a488035
TO
443 }
444 }
445 else {
7181119b 446 $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName} ON {$table} (" . implode(',', (array) $field) . "{$lengthSize})";
6a488035
TO
447 }
448 }
449 }
450
451 // run the queries without i18n-rewriting
8d7a9d07 452 $dao = new CRM_Core_DAO();
6a488035
TO
453 foreach ($queries as $query) {
454 $dao->query($query, FALSE);
455 }
456 }
457
49186f94
AS
458 /**
459 * Get indexes for tables
460 * @param array $tables
461 * array of table names to find indexes for
462 *
463 * @return array('tableName' => array('index1', 'index2'))
464 */
465 public static function getIndexes($tables) {
be2fb01f 466 $indexes = [];
49186f94
AS
467 foreach ($tables as $table) {
468 $query = "SHOW INDEX FROM $table";
469 $dao = CRM_Core_DAO::executeQuery($query);
470
be2fb01f 471 $tableIndexes = [];
49186f94
AS
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);
477 }
478 $indexes[$table] = $tableIndexes;
49186f94
AS
479 }
480 return $indexes;
481 }
482
50969d52 483 /**
484 * Drop an index if one by that name exists.
485 *
486 * @param string $tableName
487 * @param string $indexName
488 */
489 public static function dropIndexIfExists($tableName, $indexName) {
490 if (self::checkIfIndexExists($tableName, $indexName)) {
491 CRM_Core_DAO::executeQuery("DROP INDEX $indexName ON $tableName");
492 }
493 }
494
b5c2afd0 495 /**
100fef9d
CW
496 * @param int $customFieldID
497 * @param string $tableName
498 * @param string $columnName
b5c2afd0
EM
499 * @param $length
500 *
ac15829d 501 * @throws CRM_Core_Exception
b5c2afd0 502 */
00be9182 503 public static function alterFieldLength($customFieldID, $tableName, $columnName, $length) {
6a488035
TO
504 // first update the custom field tables
505 $sql = "
506UPDATE civicrm_custom_field
507SET text_length = %1
508WHERE id = %2
509";
be2fb01f
CW
510 $params = [
511 1 => [$length, 'Integer'],
512 2 => [$customFieldID, 'Integer'],
513 ];
6a488035
TO
514 CRM_Core_DAO::executeQuery($sql, $params);
515
516 $sql = "
517SELECT is_required, default_value
518FROM civicrm_custom_field
519WHERE id = %2
520";
521 $dao = CRM_Core_DAO::executeQuery($sql, $params);
522
523 if ($dao->fetch()) {
524 $clause = '';
525
526 if ($dao->is_required) {
527 $clause = " NOT NULL";
528 }
529
530 if (!empty($dao->default_value)) {
531 $clause .= " DEFAULT '{$dao->default_value}'";
532 }
533 // now modify the column
534 $sql = "
535ALTER TABLE {$tableName}
536MODIFY {$columnName} varchar( $length )
537 $clause
538";
539 CRM_Core_DAO::executeQuery($sql);
540 }
541 else {
ac15829d 542 throw new CRM_Core_Exception(ts('Could Not Find Custom Field Details for %1, %2, %3',
be2fb01f 543 [
353ffa53
TO
544 1 => $tableName,
545 2 => $columnName,
546 3 => $customFieldID,
be2fb01f 547 ]
353ffa53 548 ));
6a488035
TO
549 }
550 }
96025800 551
50969d52 552 /**
553 * Check if the table has an index matching the name.
554 *
555 * @param string $tableName
6a8758a6 556 * @param string $indexName
50969d52 557 *
49186f94 558 * @return bool
50969d52 559 */
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",
be2fb01f 563 [1 => [$indexName, 'String']]
50969d52 564 );
565 if ($result->fetch()) {
566 return TRUE;
567 }
568 return FALSE;
569 }
570
82f5a856 571 /**
49186f94 572 * Check if the table has a specified column.
82f5a856
SL
573 *
574 * @param string $tableName
575 * @param string $columnName
eed7e803
CW
576 * @param bool $i18nRewrite
577 * Whether to rewrite the query on multilingual setups.
82f5a856 578 *
49186f94 579 * @return bool
82f5a856 580 */
eed7e803 581 public static function checkIfFieldExists($tableName, $columnName, $i18nRewrite = TRUE) {
5e282b34
CW
582 $query = "SHOW COLUMNS FROM $tableName LIKE '%1'";
583 $dao = CRM_Core_DAO::executeQuery($query, [1 => [$columnName, 'Alphanumeric']], TRUE, NULL, FALSE, $i18nRewrite);
63d76404 584 return (bool) $dao->fetch();
82f5a856
SL
585 }
586
169475b7 587 /**
f5078cc0
SL
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
169475b7 592 */
f5078cc0 593 public static function checkFKExists($table_name, $constraint_name) {
169475b7
SL
594 $config = CRM_Core_Config::singleton();
595 $dbUf = DB::parseDSN($config->dsn);
596 $query = "
597 SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
598 WHERE TABLE_SCHEMA = %1
599 AND TABLE_NAME = %2
600 AND CONSTRAINT_NAME = %3
601 AND CONSTRAINT_TYPE = 'FOREIGN KEY'
602 ";
be2fb01f
CW
603 $params = [
604 1 => [$dbUf['database'], 'String'],
605 2 => [$table_name, 'String'],
606 3 => [$constraint_name, 'String'],
607 ];
1494e986 608 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, FALSE);
169475b7
SL
609
610 if ($dao->fetch()) {
27e82c24 611 return TRUE;
f5078cc0 612 }
27e82c24 613 return FALSE;
f5078cc0
SL
614 }
615
616 /**
617 * Remove a foreign key from a table if it exists.
618 *
619 * @param $table_name
620 * @param $constraint_name
621 *
622 * @return bool
623 */
624 public static function safeRemoveFK($table_name, $constraint_name) {
27e82c24 625 if (self::checkFKExists($table_name, $constraint_name)) {
1494e986 626 CRM_Core_DAO::executeQuery("ALTER TABLE {$table_name} DROP FOREIGN KEY {$constraint_name}", [], TRUE, NULL, FALSE, FALSE);
9cd5a579 627 return TRUE;
169475b7 628 }
9cd5a579 629 return FALSE;
169475b7
SL
630 }
631
6b86d84f
AS
632 /**
633 * Add index signature hash to DAO file calculation.
634 *
635 * @param string $table table name
636 * @param array $indices index array spec
637 */
638 public static function addIndexSignature($table, &$indices) {
639 foreach ($indices as $indexName => $index) {
640 $indices[$indexName]['sig'] = $table . "::" .
641 (array_key_exists('unique', $index) ? $index['unique'] : 0) . "::" .
642 implode("::", $index['field']);
643 }
644 }
645
49186f94
AS
646 /**
647 * Compare the indices specified in the XML files with those in the DB.
648 *
3d4602c3
JP
649 * @param bool $dropFalseIndices
650 * If set - this function deletes false indices present in the DB which mismatches the expected
651 * values of xml file so that civi re-creates them with correct values using createMissingIndices() function.
138b4c4c 652 * @param array|FALSE $tables
653 * An optional array of tables - if provided the results will be restricted to these tables.
3d4602c3 654 *
49186f94
AS
655 * @return array
656 * index specifications
657 */
138b4c4c 658 public static function getMissingIndices($dropFalseIndices = FALSE, $tables = FALSE) {
be2fb01f 659 $requiredSigs = $existingSigs = [];
49186f94
AS
660 // Get the indices defined (originally) in the xml files
661 $requiredIndices = CRM_Core_DAO_AllCoreTables::indices();
be2fb01f 662 $reqSigs = [];
138b4c4c 663 if ($tables !== FALSE) {
664 $requiredIndices = array_intersect_key($requiredIndices, array_fill_keys($tables, TRUE));
665 }
49186f94
AS
666 foreach ($requiredIndices as $table => $indices) {
667 $reqSigs[] = CRM_Utils_Array::collect('sig', $indices);
668 }
669 CRM_Utils_Array::flatten($reqSigs, $requiredSigs);
670
671 // Get the indices in the database
672 $existingIndices = CRM_Core_BAO_SchemaHandler::getIndexes(array_keys($requiredIndices));
be2fb01f 673 $extSigs = [];
49186f94
AS
674 foreach ($existingIndices as $table => $indices) {
675 CRM_Core_BAO_SchemaHandler::addIndexSignature($table, $indices);
676 $extSigs[] = CRM_Utils_Array::collect('sig', $indices);
677 }
678 CRM_Utils_Array::flatten($extSigs, $existingSigs);
679
680 // Compare
681 $missingSigs = array_diff($requiredSigs, $existingSigs);
f4f835ed 682
3d4602c3 683 //CRM-20774 - Drop index key which exist in db but the value varies.
f4f835ed 684 $existingKeySigs = array_intersect_key($missingSigs, $existingSigs);
3d4602c3 685 if ($dropFalseIndices && !empty($existingKeySigs)) {
f4f835ed
JP
686 foreach ($existingKeySigs as $sig) {
687 $sigParts = explode('::', $sig);
688 foreach ($requiredIndices[$sigParts[0]] as $index) {
3d4602c3
JP
689 if ($index['sig'] == $sig && !empty($index['name'])) {
690 self::dropIndexIfExists($sigParts[0], $index['name']);
f4f835ed
JP
691 continue;
692 }
693 }
694 }
695 }
696
49186f94 697 // Get missing indices
be2fb01f 698 $missingIndices = [];
49186f94
AS
699 foreach ($missingSigs as $sig) {
700 $sigParts = explode('::', $sig);
f3490f40 701 if (array_key_exists($sigParts[0], $requiredIndices)) {
702 foreach ($requiredIndices[$sigParts[0]] as $index) {
703 if ($index['sig'] == $sig) {
704 $missingIndices[$sigParts[0]][] = $index;
705 continue;
706 }
49186f94
AS
707 }
708 }
709 }
3d4602c3 710 return $missingIndices;
49186f94
AS
711 }
712
713 /**
714 * Create missing indices.
715 *
716 * @param array $missingIndices as returned by getMissingIndices()
717 */
718 public static function createMissingIndices($missingIndices) {
be2fb01f 719 $queries = [];
49186f94
AS
720 foreach ($missingIndices as $table => $indexList) {
721 foreach ($indexList as $index) {
722 $queries[] = "CREATE " .
723 (array_key_exists('unique', $index) && $index['unique'] ? 'UNIQUE ' : '') .
724 "INDEX {$index['name']} ON {$table} (" .
725 implode(", ", $index['field']) .
726 ")";
727 }
728 }
729
730 /* FIXME potential problem if index name already exists, so check before creating */
731 $dao = new CRM_Core_DAO();
732 foreach ($queries as $query) {
733 $dao->query($query, FALSE);
734 }
49186f94
AS
735 }
736
d992b2f5 737 /**
738 * Build the sql to alter the field.
739 *
740 * @param array $params
741 * @param bool $indexExist
742 *
743 * @return string
744 */
745 public static function buildFieldChangeSql($params, $indexExist) {
746 $sql = str_repeat(' ', 8);
747 $sql .= "ALTER TABLE {$params['table_name']}";
8e2949c4 748 return $sql . self::getFieldAlterSQL($params, $indexExist);
749 }
750
751 /**
752 * Get the sql to alter an individual field.
753 *
754 * This will need to have an ALTER TABLE statement appended but by getting
755 * by individual field we can do one or many.
756 *
757 * @param array $params
758 * @param bool $indexExist
759 *
760 * @return string
761 */
762 public static function getFieldAlterSQL($params, $indexExist) {
763 $sql = '';
d992b2f5 764 switch ($params['operation']) {
765 case 'add':
766 $separator = "\n";
767 $sql .= self::buildFieldSQL($params, $separator, "ADD COLUMN ");
768 $separator = ",\n";
769 $sql .= self::buildPrimaryKeySQL($params, $separator, "ADD PRIMARY KEY ");
770 $sql .= self::buildSearchIndexSQL($params, $separator, "ADD INDEX ");
771 $sql .= self::buildForeignKeySQL($params, $separator, "ADD ", $params['table_name']);
772 break;
773
774 case 'modify':
775 $separator = "\n";
776 $sql .= self::buildFieldSQL($params, $separator, "MODIFY ");
777 $separator = ",\n";
778 $sql .= self::buildSearchIndexSQL($params, $separator, "ADD INDEX ", $indexExist);
779 break;
780
781 case 'delete':
782 $sql .= " DROP COLUMN `{$params['name']}`";
783 if (!empty($params['primary'])) {
784 $sql .= ", DROP PRIMARY KEY";
785 }
786 if (!empty($params['fk_table_name'])) {
787 $sql .= ", DROP FOREIGN KEY FK_{$params['fkName']}";
788 }
789 break;
790 }
791 return $sql;
792 }
793
a0a5d4da 794 /**
795 * Performs the utf8mb4 migration.
796 *
797 * @param bool $revert
798 * Being able to revert if primarily for unit testing.
799 *
800 * @return bool
801 */
802 public static function migrateUtf8mb4($revert = FALSE) {
803 $newCharSet = $revert ? 'utf8' : 'utf8mb4';
804 $newCollation = $revert ? 'utf8_unicode_ci' : 'utf8mb4_unicode_ci';
805 $newBinaryCollation = $revert ? 'utf8_bin' : 'utf8mb4_bin';
806 $tables = [];
807 $dao = new CRM_Core_DAO();
808 $database = $dao->_database;
809 CRM_Core_DAO::executeQuery("ALTER DATABASE $database CHARACTER SET = $newCharSet COLLATE = $newCollation");
810 $dao = CRM_Core_DAO::executeQuery("SHOW TABLE STATUS WHERE Engine = 'InnoDB' AND Name LIKE 'civicrm\_%'");
811 while ($dao->fetch()) {
812 $tables[$dao->Name] = [
813 'Engine' => $dao->Engine,
814 ];
815 }
816 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN);
817 $logging_database = $dsn['database'];
818 $dao = CRM_Core_DAO::executeQuery("SHOW TABLE STATUS FROM `$logging_database` WHERE Engine <> 'MyISAM' AND Name LIKE 'log\_civicrm\_%'");
819 while ($dao->fetch()) {
820 $tables["$logging_database.{$dao->Name}"] = [
821 'Engine' => $dao->Engine,
822 ];
823 }
824 foreach ($tables as $table => $param) {
825 $query = "ALTER TABLE $table";
826 $dao = CRM_Core_DAO::executeQuery("SHOW FULL COLUMNS FROM $table", [], TRUE, NULL, FALSE, FALSE);
827 $index = 0;
828 $params = [];
829 $tableCollation = $newCollation;
830 while ($dao->fetch()) {
831 if (!$dao->Collation || $dao->Collation === $newCollation || $dao->Collation === $newBinaryCollation) {
832 continue;
833 }
834 if (strpos($dao->Collation, 'utf8') !== 0) {
835 continue;
836 }
837
838 if (strpos($dao->Collation, '_bin') !== FALSE) {
839 $tableCollation = $newBinaryCollation;
840 }
841 else {
842 $tableCollation = $newCollation;
843 }
844 if ($dao->Null === 'YES') {
845 $null = 'NULL';
846 }
847 else {
848 $null = 'NOT NULL';
849 }
850 $default = '';
851 if ($dao->Default !== NULL) {
852 $index++;
853 $default = "DEFAULT %$index";
854 $params[$index] = [$dao->Default, 'String'];
855 }
856 elseif ($dao->Null === 'YES') {
857 $default = 'DEFAULT NULL';
858 }
859 $index++;
860 $params[$index] = [$dao->Comment, 'String'];
861 $query .= " MODIFY `{$dao->Field}` {$dao->Type} CHARACTER SET $newCharSet COLLATE $tableCollation $null $default {$dao->Extra} COMMENT %$index,";
862 }
863 $query .= " CHARACTER SET = $newCharSet COLLATE = $tableCollation";
864 if ($param['Engine'] === 'InnoDB') {
41a26a7c 865 $query .= ' ROW_FORMAT = Dynamic KEY_BLOCK_SIZE = 0';
a0a5d4da 866 }
867 // Disable i18n rewrite.
868 CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, FALSE);
869 }
870 return TRUE;
871 }
872
25374ca7 873 /**
874 * Get the database collation.
875 *
876 * @return string
877 */
878 public static function getDBCollation() {
879 return CRM_Core_DAO::singleValueQuery('SELECT @@collation_database');
880 }
881
882 /**
883 * Get the database collation.
884 *
885 * @return string
886 */
887 public static function getDBCharset() {
888 return CRM_Core_DAO::singleValueQuery('SELECT @@character_set_database');
889 }
890
6a488035 891}