3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This file contains functions for creating and altering CiviCRM-tables structure.
38 * 'name' => TABLE_NAME,
39 * 'attributes' => ATTRIBUTES,
42 * 'name' => FIELD_NAME,
43 * // can be field, index, constraint
44 * 'type' => FIELD_SQL_TYPE,
45 * 'class' => FIELD_CLASS_TYPE,
46 * 'primary' => BOOLEAN,
47 * 'required' => BOOLEAN,
48 * 'searchable' => TRUE,
49 * 'fk_table_name' => FOREIGN_KEY_TABLE_NAME,
50 * 'fk_field_name' => FOREIGN_KEY_FIELD_NAME,
51 * 'comment' => COMMENT,
52 * 'default' => DEFAULT, )
56 class CRM_Core_BAO_SchemaHandler
{
59 * Create a CiviCRM-table
61 * @param array $params
64 * TRUE if successfully created, FALSE otherwise
67 public static function createTable($params) {
68 $sql = self
::buildTableSQL($params);
69 // do not i18n-rewrite
70 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
72 if (CRM_Core_Config
::singleton()->logging
) {
74 $logging = new CRM_Logging_Schema();
75 $logging->fixSchemaDifferencesFor($params['name']);
78 // always do a trigger rebuild for this table
79 Civi
::service('sql_triggers')->rebuild($params['name'], TRUE);
85 * @param array $params
89 public static function buildTableSQL($params) {
90 $sql = "CREATE TABLE {$params['name']} (";
91 if (isset($params['fields']) &&
92 is_array($params['fields'])
96 foreach ($params['fields'] as $field) {
97 $sql .= self
::buildFieldSQL($field, $separator, $prefix);
100 foreach ($params['fields'] as $field) {
101 $sql .= self
::buildPrimaryKeySQL($field, $separator, $prefix);
103 foreach ($params['fields'] as $field) {
104 $sql .= self
::buildSearchIndexSQL($field, $separator, $prefix);
106 if (isset($params['indexes'])) {
107 foreach ($params['indexes'] as $index) {
108 $sql .= self
::buildIndexSQL($index, $separator, $prefix);
111 foreach ($params['fields'] as $field) {
112 $sql .= self
::buildForeignKeySQL($field, $separator, $prefix, $params['name']);
115 $sql .= "\n) {$params['attributes']};";
120 * @param array $params
126 public static function buildFieldSQL($params, $separator, $prefix) {
129 $sql .= str_repeat(' ', 8);
131 $sql .= "`{$params['name']}` {$params['type']}";
133 if (!empty($params['required'])) {
137 if (!empty($params['attributes'])) {
138 $sql .= " {$params['attributes']}";
141 if (!empty($params['default']) &&
142 $params['type'] != 'text'
144 $sql .= " DEFAULT {$params['default']}";
147 if (!empty($params['comment'])) {
148 $sql .= " COMMENT '{$params['comment']}'";
155 * @param array $params
159 * @return NULL|string
161 public static function buildPrimaryKeySQL($params, $separator, $prefix) {
163 if (!empty($params['primary'])) {
165 $sql .= str_repeat(' ', 8);
167 $sql .= "PRIMARY KEY ( {$params['name']} )";
173 * @param array $params
176 * @param bool $indexExist
178 * @return NULL|string
180 public static function buildSearchIndexSQL($params, $separator, $prefix, $indexExist = FALSE) {
184 if ($params['type'] == 'text') {
188 //create index only for searchable fields during ADD,
189 //create index only if field is become searchable during MODIFY,
190 //drop index only if field is no longer searchable and it does not reference
191 //a forgein key (and indexExist is true)
192 if (!empty($params['searchable']) && !$indexExist) {
194 $sql .= str_repeat(' ', 8);
196 $sql .= "INDEX_{$params['name']} ( {$params['name']} )";
198 elseif (empty($params['searchable']) && empty($params['fk_table_name']) && $indexExist) {
200 $sql .= str_repeat(' ', 8);
201 $sql .= "DROP INDEX INDEX_{$params['name']}";
207 * @param array $params
213 public static function buildIndexSQL(&$params, $separator, $prefix) {
216 $sql .= str_repeat(' ', 8);
217 if ($params['unique']) {
218 $sql .= 'UNIQUE INDEX';
219 $indexName = 'unique';
223 $indexName = 'index';
227 foreach ($params as $name => $value) {
228 if (substr($name, 0, 11) == 'field_name_') {
229 $indexName .= "_{$value}";
230 $indexFields .= " $value,";
233 $indexFields = substr($indexFields, 0, -1);
235 $sql .= " $indexName ( $indexFields )";
240 * @param string $tableName
241 * @param string $fkTableName
245 public static function changeFKConstraint($tableName, $fkTableName) {
246 $fkName = "{$tableName}_entity_id";
247 if (strlen($fkName) >= 48) {
248 $fkName = substr($fkName, 0, 32) . "_" . substr(md5($fkName), 0, 16);
251 ALTER TABLE {$tableName}
252 DROP FOREIGN KEY `FK_{$fkName}`;";
254 CRM_Core_DAO
::executeQuery($dropFKSql);
257 ALTER TABLE {$tableName}
258 ADD CONSTRAINT `FK_{$fkName}` FOREIGN KEY (`entity_id`) REFERENCES {$fkTableName} (`id`) ON DELETE CASCADE;";
259 // CRM-7007: do not i18n-rewrite this query
260 CRM_Core_DAO
::executeQuery($addFKSql, [], TRUE, NULL, FALSE, FALSE);
266 * @param array $params
269 * @param string $tableName
271 * @return NULL|string
273 public static function buildForeignKeySQL($params, $separator, $prefix, $tableName) {
275 if (!empty($params['fk_table_name']) && !empty($params['fk_field_name'])) {
277 $sql .= str_repeat(' ', 8);
279 $fkName = "{$tableName}_{$params['name']}";
280 if (strlen($fkName) >= 48) {
281 $fkName = substr($fkName, 0, 32) . "_" . substr(md5($fkName), 0, 16);
284 $sql .= "CONSTRAINT FK_$fkName FOREIGN KEY ( `{$params['name']}` ) REFERENCES {$params['fk_table_name']} ( {$params['fk_field_name']} ) ";
285 $sql .= CRM_Utils_Array
::value('fk_attributes', $params);
293 * @param array $params
294 * @param bool $indexExist
295 * @param bool $triggerRebuild
299 public static function alterFieldSQL($params, $indexExist = FALSE, $triggerRebuild = TRUE) {
300 CRM_Core_Error
::deprecatedFunctionWarning('function no longer in use / supported');
301 // lets suppress the required flag, since that can cause sql issue
302 $params['required'] = FALSE;
304 $sql = self
::buildFieldChangeSql($params, $indexExist);
306 // CRM-7007: do not i18n-rewrite this query
307 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
309 $config = CRM_Core_Config
::singleton();
310 if ($config->logging
) {
311 // CRM-16717 not sure why this was originally limited to add.
312 // For example custom tables can have field length changes - which need to flow through to logging.
313 // Are there any modifies we DON'T was to call this function for (& shouldn't it be clever enough to cope?)
314 if ($params['operation'] == 'add' ||
$params['operation'] == 'modify') {
315 $logging = new CRM_Logging_Schema();
316 $logging->fixSchemaDifferencesFor($params['table_name'], [trim(strtoupper($params['operation'])) => [$params['name']]]);
320 if ($triggerRebuild) {
321 Civi
::service('sql_triggers')->rebuild($params['table_name'], TRUE);
328 * Delete a CiviCRM-table.
330 * @param string $tableName
331 * Name of the table to be created.
333 public static function dropTable($tableName) {
334 $sql = "DROP TABLE $tableName";
335 CRM_Core_DAO
::executeQuery($sql);
339 * @param string $tableName
340 * @param string $columnName
342 * @param bool $isUpgradeMode
345 public static function dropColumn($tableName, $columnName, $l18n = FALSE, $isUpgradeMode = FALSE) {
346 if (self
::checkIfFieldExists($tableName, $columnName)) {
347 $sql = "ALTER TABLE $tableName DROP COLUMN $columnName";
349 CRM_Core_DAO
::executeQuery($sql);
352 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
354 $domain = new CRM_Core_DAO_Domain();
356 if ($domain->locales
) {
357 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
358 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, NULL, $isUpgradeMode);
364 * @param string $tableName
365 * @param bool $dropUnique
367 public static function changeUniqueToIndex($tableName, $dropUnique = TRUE) {
369 $sql = "ALTER TABLE $tableName
370 DROP INDEX `unique_entity_id` ,
371 ADD INDEX `FK_{$tableName}_entity_id` ( `entity_id` )";
374 $sql = " ALTER TABLE $tableName
375 DROP INDEX `FK_{$tableName}_entity_id` ,
376 ADD UNIQUE INDEX `unique_entity_id` ( `entity_id` )";
378 CRM_Core_DAO
::executeQuery($sql);
385 * Tables to create index for in the format:
386 * array('civicrm_entity_table' => 'entity_id')
388 * array('civicrm_entity_table' => array('entity_id', 'entity_table'))
389 * The latter will create a combined index on the 2 keys (in order).
391 * Side note - when creating combined indexes the one with the most variation
392 * goes first - so entity_table always goes after entity_id.
394 * It probably makes sense to consider more sophisticated options at some point
395 * but at the moment this is only being as enhanced as fast as the test is.
397 * @todo add support for length & multilingual on combined keys.
399 * @param string $createIndexPrefix
400 * @param array $substrLengths
402 public static function createIndexes($tables, $createIndexPrefix = 'index', $substrLengths = []) {
404 $domain = new CRM_Core_DAO_Domain();
406 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
408 // if we're multilingual, cache the information on internationalised fields
409 static $columns = NULL;
410 if (!CRM_Utils_System
::isNull($locales) and $columns === NULL) {
411 $columns = CRM_Core_I18n_SchemaStructure
::columns();
414 foreach ($tables as $table => $fields) {
415 $query = "SHOW INDEX FROM $table";
416 $dao = CRM_Core_DAO
::executeQuery($query);
418 $currentIndexes = [];
419 while ($dao->fetch()) {
420 $currentIndexes[] = $dao->Key_name
;
423 // now check for all fields if the index exists
424 foreach ($fields as $field) {
425 $fieldName = implode('_', (array) $field);
427 if (is_array($field)) {
428 // No support for these for combined indexes as yet - add a test when you
434 // handle indices over substrings, CRM-6245
435 // $lengthName is appended to index name, $lengthSize is the field size modifier
436 $lengthName = isset($substrLengths[$table][$fieldName]) ?
"_{$substrLengths[$table][$fieldName]}" : '';
437 $lengthSize = isset($substrLengths[$table][$fieldName]) ?
"({$substrLengths[$table][$fieldName]})" : '';
441 "index_{$fieldName}{$lengthName}",
442 "FK_{$table}_{$fieldName}{$lengthName}",
443 "UI_{$fieldName}{$lengthName}",
444 "{$createIndexPrefix}_{$fieldName}{$lengthName}",
447 // skip to the next $field if one of the above $names exists; handle multilingual for CRM-4126
448 foreach ($names as $name) {
449 $regex = '/^' . preg_quote($name) . '(_[a-z][a-z]_[A-Z][A-Z])?$/';
450 if (preg_grep($regex, $currentIndexes)) {
455 // the index doesn't exist, so create it
456 // if we're multilingual and the field is internationalised, do it for every locale
457 // @todo remove is_array check & add multilingual support for combined indexes and add a test.
458 // Note combined indexes currently using this function are on fields like
459 // entity_id + entity_table which are not multilingual.
460 if (!is_array($field) && !CRM_Utils_System
::isNull($locales) and isset($columns[$table][$fieldName])) {
461 foreach ($locales as $locale) {
462 $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName}_{$locale} ON {$table} ({$fieldName}_{$locale}{$lengthSize})";
466 $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName} ON {$table} (" . implode(',', (array) $field) . "{$lengthSize})";
471 // run the queries without i18n-rewriting
472 $dao = new CRM_Core_DAO();
473 foreach ($queries as $query) {
474 $dao->query($query, FALSE);
479 * Get indexes for tables
480 * @param array $tables
481 * array of table names to find indexes for
483 * @return array('tableName' => array('index1', 'index2'))
485 public static function getIndexes($tables) {
487 foreach ($tables as $table) {
488 $query = "SHOW INDEX FROM $table";
489 $dao = CRM_Core_DAO
::executeQuery($query);
492 while ($dao->fetch()) {
493 $tableIndexes[$dao->Key_name
]['name'] = $dao->Key_name
;
494 $tableIndexes[$dao->Key_name
]['field'][] = $dao->Column_name
.
495 ($dao->Sub_part ?
'(' . $dao->Sub_part
. ')' : '');
496 $tableIndexes[$dao->Key_name
]['unique'] = ($dao->Non_unique
== 0 ?
1 : 0);
498 $indexes[$table] = $tableIndexes;
504 * Drop an index if one by that name exists.
506 * @param string $tableName
507 * @param string $indexName
509 public static function dropIndexIfExists($tableName, $indexName) {
510 if (self
::checkIfIndexExists($tableName, $indexName)) {
511 CRM_Core_DAO
::executeQuery("DROP INDEX $indexName ON $tableName");
516 * @param int $customFieldID
517 * @param string $tableName
518 * @param string $columnName
523 public static function alterFieldLength($customFieldID, $tableName, $columnName, $length) {
524 // first update the custom field tables
526 UPDATE civicrm_custom_field
531 1 => [$length, 'Integer'],
532 2 => [$customFieldID, 'Integer'],
534 CRM_Core_DAO
::executeQuery($sql, $params);
537 SELECT is_required, default_value
538 FROM civicrm_custom_field
541 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
546 if ($dao->is_required
) {
547 $clause = " NOT NULL";
550 if (!empty($dao->default_value
)) {
551 $clause .= " DEFAULT '{$dao->default_value}'";
553 // now modify the column
555 ALTER TABLE {$tableName}
556 MODIFY {$columnName} varchar( $length )
559 CRM_Core_DAO
::executeQuery($sql);
562 CRM_Core_Error
::fatal(ts('Could Not Find Custom Field Details for %1, %2, %3',
573 * Check if the table has an index matching the name.
575 * @param string $tableName
576 * @param array $indexName
580 public static function checkIfIndexExists($tableName, $indexName) {
581 $result = CRM_Core_DAO
::executeQuery(
582 "SHOW INDEX FROM $tableName WHERE key_name = %1 AND seq_in_index = 1",
583 [1 => [$indexName, 'String']]
585 if ($result->fetch()) {
592 * Check if the table has a specified column.
594 * @param string $tableName
595 * @param string $columnName
596 * @param bool $i18nRewrite
597 * Whether to rewrite the query on multilingual setups.
601 public static function checkIfFieldExists($tableName, $columnName, $i18nRewrite = TRUE) {
602 $query = "SHOW COLUMNS FROM $tableName LIKE '%1'";
603 $dao = CRM_Core_DAO
::executeQuery($query, [1 => [$columnName, 'Alphanumeric']], TRUE, NULL, FALSE, $i18nRewrite);
604 $result = $dao->fetch() ?
TRUE : FALSE;
609 * Check if a foreign key Exists
610 * @param string $table_name
611 * @param string $constraint_name
612 * @return bool TRUE if FK is found
614 public static function checkFKExists($table_name, $constraint_name) {
615 $config = CRM_Core_Config
::singleton();
616 $dbUf = DB
::parseDSN($config->dsn
);
618 SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
619 WHERE TABLE_SCHEMA = %1
621 AND CONSTRAINT_NAME = %3
622 AND CONSTRAINT_TYPE = 'FOREIGN KEY'
625 1 => [$dbUf['database'], 'String'],
626 2 => [$table_name, 'String'],
627 3 => [$constraint_name, 'String'],
629 $dao = CRM_Core_DAO
::executeQuery($query, $params);
638 * Remove a foreign key from a table if it exists.
641 * @param $constraint_name
645 public static function safeRemoveFK($table_name, $constraint_name) {
646 if (self
::checkFKExists($table_name, $constraint_name)) {
647 CRM_Core_DAO
::executeQuery("ALTER TABLE {$table_name} DROP FOREIGN KEY {$constraint_name}", []);
654 * Add index signature hash to DAO file calculation.
656 * @param string $table table name
657 * @param array $indices index array spec
659 public static function addIndexSignature($table, &$indices) {
660 foreach ($indices as $indexName => $index) {
661 $indices[$indexName]['sig'] = $table . "::" .
662 (array_key_exists('unique', $index) ?
$index['unique'] : 0) . "::" .
663 implode("::", $index['field']);
668 * Compare the indices specified in the XML files with those in the DB.
670 * @param bool $dropFalseIndices
671 * If set - this function deletes false indices present in the DB which mismatches the expected
672 * values of xml file so that civi re-creates them with correct values using createMissingIndices() function.
675 * index specifications
677 public static function getMissingIndices($dropFalseIndices = FALSE) {
678 $requiredSigs = $existingSigs = [];
679 // Get the indices defined (originally) in the xml files
680 $requiredIndices = CRM_Core_DAO_AllCoreTables
::indices();
682 foreach ($requiredIndices as $table => $indices) {
683 $reqSigs[] = CRM_Utils_Array
::collect('sig', $indices);
685 CRM_Utils_Array
::flatten($reqSigs, $requiredSigs);
687 // Get the indices in the database
688 $existingIndices = CRM_Core_BAO_SchemaHandler
::getIndexes(array_keys($requiredIndices));
690 foreach ($existingIndices as $table => $indices) {
691 CRM_Core_BAO_SchemaHandler
::addIndexSignature($table, $indices);
692 $extSigs[] = CRM_Utils_Array
::collect('sig', $indices);
694 CRM_Utils_Array
::flatten($extSigs, $existingSigs);
697 $missingSigs = array_diff($requiredSigs, $existingSigs);
699 //CRM-20774 - Drop index key which exist in db but the value varies.
700 $existingKeySigs = array_intersect_key($missingSigs, $existingSigs);
701 if ($dropFalseIndices && !empty($existingKeySigs)) {
702 foreach ($existingKeySigs as $sig) {
703 $sigParts = explode('::', $sig);
704 foreach ($requiredIndices[$sigParts[0]] as $index) {
705 if ($index['sig'] == $sig && !empty($index['name'])) {
706 self
::dropIndexIfExists($sigParts[0], $index['name']);
713 // Get missing indices
714 $missingIndices = [];
715 foreach ($missingSigs as $sig) {
716 $sigParts = explode('::', $sig);
717 if (array_key_exists($sigParts[0], $requiredIndices)) {
718 foreach ($requiredIndices[$sigParts[0]] as $index) {
719 if ($index['sig'] == $sig) {
720 $missingIndices[$sigParts[0]][] = $index;
726 return $missingIndices;
730 * Create missing indices.
732 * @param array $missingIndices as returned by getMissingIndices()
734 public static function createMissingIndices($missingIndices) {
736 foreach ($missingIndices as $table => $indexList) {
737 foreach ($indexList as $index) {
738 $queries[] = "CREATE " .
739 (array_key_exists('unique', $index) && $index['unique'] ?
'UNIQUE ' : '') .
740 "INDEX {$index['name']} ON {$table} (" .
741 implode(", ", $index['field']) .
746 /* FIXME potential problem if index name already exists, so check before creating */
747 $dao = new CRM_Core_DAO();
748 foreach ($queries as $query) {
749 $dao->query($query, FALSE);
754 * Build the sql to alter the field.
756 * @param array $params
757 * @param bool $indexExist
761 public static function buildFieldChangeSql($params, $indexExist) {
762 $sql = str_repeat(' ', 8);
763 $sql .= "ALTER TABLE {$params['table_name']}";
764 return $sql . self
::getFieldAlterSQL($params, $indexExist);
768 * Get the sql to alter an individual field.
770 * This will need to have an ALTER TABLE statement appended but by getting
771 * by individual field we can do one or many.
773 * @param array $params
774 * @param bool $indexExist
778 public static function getFieldAlterSQL($params, $indexExist) {
780 switch ($params['operation']) {
783 $sql .= self
::buildFieldSQL($params, $separator, "ADD COLUMN ");
785 $sql .= self
::buildPrimaryKeySQL($params, $separator, "ADD PRIMARY KEY ");
786 $sql .= self
::buildSearchIndexSQL($params, $separator, "ADD INDEX ");
787 $sql .= self
::buildForeignKeySQL($params, $separator, "ADD ", $params['table_name']);
792 $sql .= self
::buildFieldSQL($params, $separator, "MODIFY ");
794 $sql .= self
::buildSearchIndexSQL($params, $separator, "ADD INDEX ", $indexExist);
798 $sql .= " DROP COLUMN `{$params['name']}`";
799 if (!empty($params['primary'])) {
800 $sql .= ", DROP PRIMARY KEY";
802 if (!empty($params['fk_table_name'])) {
803 $sql .= ", DROP FOREIGN KEY FK_{$params['fkName']}";