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 +--------------------------------------------------------------------+
13 * The SchemaTrait provides utilities for altering tables during an upgrade.
15 trait CRM_Extension_Upgrader_SchemaTrait
{
18 * Add a column to a table if it doesn't already exist
20 * @param string $table
21 * @param string $column
22 * @param string $properties
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);
35 * Drop a column from a table if it exists.
37 * @param string $table
38 * @param string $column
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);
50 * Add an index to one or more columns.
52 * @param string $table
53 * @param string|array $columns
54 * @param string $prefix
57 public static function addIndex($table, $columns, $prefix = 'index') {
58 $tables = [$table => (array) $columns];
59 CRM_Core_BAO_SchemaHandler
::createIndexes($tables, $prefix);
64 * Drop index from a table if it exists.
66 * @param string $table
67 * @param string $indexName
70 public static function dropIndex($table, $indexName) {
71 CRM_Core_BAO_SchemaHandler
::dropIndexIfExists($table, $indexName);