Database Schema Helper Reference¶
The CiviMix\Schema\SchemaHelperInterface is the unified PHP interface for managing database tables, columns, indexes, and foreign keys in CiviCRM.
It is used in two different contexts:
- Extensions: Accessed via the upgrader's helper
E::schema(). (See Database Upgraders for details on writing extension upgrades). - CiviCRM Core: Accessed via
Civi::schemaHelper().
Core Upgrade Integration¶
In CiviCRM Core, database schema changes are typically handled in incremental PHP upgrade classes (such as CRM_Upgrade_Incremental_php_SixFourteen).
Tasks can be registered using base helper methods on CRM_Upgrade_Incremental_Base which internally delegate to Civi::schemaHelper(). For example:
class CRM_Upgrade_Incremental_php_SixFourteen extends CRM_Upgrade_Incremental_Base {
public function upgrade_6_14_alpha1($rev): void {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
// Add column using core task wrapper
$this->addTask('Add File.is_public', 'alterSchemaField', 'File', 'is_public', [
'title' => ts('File Is Public'),
'sql_type' => 'boolean',
'input_type' => 'Toggle',
'required' => TRUE,
'description' => ts('Controls whether file is stored in public or private directory.'),
'default' => FALSE,
], 'AFTER `description`');
// Drop index using core task wrapper
$this->addTask('Drop civicrm_activity.is_current_revision index', 'dropIndex', 'civicrm_activity', 'index_is_current_revision');
}
}
You can also retrieve and invoke Civi::schemaHelper() directly in custom static helper methods for complex checks or schema updates:
// Example: verify if a table exists
if (\Civi::schemaHelper()->tableExists('civicrm_my_table')) {
// ...
}
API Reference¶
The schema helper object implements CiviMix\Schema\SchemaHelperInterface and provides the following methods for schema management:
Core Methods¶
-
hasSchema(): boolChecks whether the extension/core defines any schema files in itsschema/directory. -
install(): voidInstalls all SQL tables defined in theschema/directory. -
uninstall(): voidUninstalls/drops all SQL tables defined in theschema/directory. -
generateInstallSql(): ?stringGenerates the raw SQL queries needed to install the tables. -
generateUninstallSql(): stringGenerates the raw SQL queries needed to drop the tables. -
arrayToSql(array $defn): stringConverts an entity or field definition array to its corresponding SQL statement.
Table & Field Methods¶
-
createEntityTable(string $filePath): boolCreates a database table based on a PHP schema file (relative to the root directory). E.g.upgrade/schema/10001-myEntity.entityType.php. -
alterSchemaField(string $entityName, string $fieldName, array $fieldSpec, ?string $position = NULL): boolAdds or modifies a column definition on the table associated with the given entity, based on the provided field specification array. -
dropSchemaField(string $entityName, string $fieldName): boolDrops a column from the table associated with the given entity. -
schemaFieldExists(string $entityName, string $fieldName): boolChecks whether a specific column exists in the table associated with the given entity. -
getTableName(string $entityName): ?stringResolves the database table name associated with the given entity name. -
tableExists(string $tableName): boolChecks whether a database table with the given name exists (case-insensitive). -
getExistingTables(array $tableNames): arrayGiven a list of table names, filters and returns the ones that actually exist in the database (matching case-insensitively but returning the canonical lowercase names). -
dropTable(string $tableName): boolDrops a table with the given name.
Index Methods¶
-
indexExists(string $tableName, string $indexName): boolChecks whether a specific index exists on a table. -
createIndex(string $tableName, string $indexName, array $indexDef): boolCreates an index on a table using the definition format (e.g.['fields' => ['col1' => TRUE], 'unique' => TRUE]). -
dropIndex(string $tableName, string $indexName): boolDrops a specific index from a table.
Foreign Key Methods¶
-
foreignKeyExists(string $tableName, string $foreignKeyName): boolChecks whether a foreign key constraint exists on a table. -
createForeignKey(string $tableName, string $fieldName, array $fieldSpec): boolCreates a foreign key constraint on a table for a field based on its schema specification. -
dropForeignKey(string $tableName, string $foreignKeyName): boolDrops a specific foreign key constraint from a table.