Merge pull request #23045 from braders/chartenabled-var-expected
[civicrm-core.git] / CRM / Extension / Upgrader / SchemaTrait.php
CommitLineData
81ab7ee3
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12/**
13 * The SchemaTrait provides utilities for altering tables during an upgrade.
14 */
15trait CRM_Extension_Upgrader_SchemaTrait {
16
17 /**
18 * Add a column to a table if it doesn't already exist
19 *
20 * @param string $table
21 * @param string $column
22 * @param string $properties
23 *
24 * @return bool
25 */
26 public static function addColumn($table, $column, $properties) {
27 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column, FALSE)) {
28 $query = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
29 CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
30 }
31 return TRUE;
32 }
33
34 /**
35 * Drop a column from a table if it exists.
36 *
37 * @param string $table
38 * @param string $column
39 * @return bool
40 */
41 public static function dropColumn($table, $column) {
42 if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column, FALSE)) {
43 CRM_Core_DAO::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`",
44 [], TRUE, NULL, FALSE, FALSE);
45 }
46 return TRUE;
47 }
48
49 /**
50 * Add an index to one or more columns.
51 *
52 * @param string $table
53 * @param string|array $columns
54 * @param string $prefix
55 * @return bool
56 */
57 public static function addIndex($table, $columns, $prefix = 'index') {
58 $tables = [$table => (array) $columns];
59 CRM_Core_BAO_SchemaHandler::createIndexes($tables, $prefix);
60 return TRUE;
61 }
62
63 /**
64 * Drop index from a table if it exists.
65 *
66 * @param string $table
67 * @param string $indexName
68 * @return bool
69 */
70 public static function dropIndex($table, $indexName) {
71 CRM_Core_BAO_SchemaHandler::dropIndexIfExists($table, $indexName);
72 return TRUE;
73 }
74
75}