Modifying Data Types
Relevant source files
The following files were used as context for generating this wiki page:
- src/main/java/liquibase/ext/clickhouse/sqlgenerator/ModifyDataTypeClickHouse.java
- src/main/java/liquibase/ext/clickhouse/database/ClickHouseDatabase.java
- src/main/java/liquibase/ext/clickhouse/sqlgenerator/CreateDatabaseChangeLogTableClickHouse.java
- src/main/java/liquibase/ext/clickhouse/sqlgenerator/CreateDatabaseChangeLogLockTableClickHouse.java
- src/main/java/liquibase/ext/clickhouse/sqlgenerator/SqlGeneratorUtil.java
- src/main/java/liquibase/ext/clickhouse/params/ParamsLoader.java
The "Modifying Data Types" module in the Liquibase ClickHouse extension manages how Liquibase handles changes to column data types. In the context of ClickHouse, this module specifically ensures that Liquibase does not attempt to perform standard SQL data type modifications on its internal tracking tables, as ClickHouse uses variable-length String types where other databases might use VARCHAR(N).
Sources: src/main/java/liquibase/ext/clickhouse/sqlgenerator/ModifyDataTypeClickHouse.java:31-34
Architecture and Core Logic
The extension provides a specialized generator, ModifyDataTypeClickHouse, which overrides the standard Liquibase ModifyDataTypeGenerator. Its primary purpose is to act as a "Noop" (No Operation) generator for ClickHouse databases. This is critical because Liquibase often tries to adjust the sizes of its own metadata columns (like ID, AUTHOR, and FILENAME) during standard operations.
Data Type Handling in ClickHouse
Unlike traditional RDBMS, ClickHouse internal tables for this extension are defined using the String type rather than fixed-length strings. Since String in ClickHouse is of variable length and does not require a size limit, any ALTER TABLE ... MODIFY COLUMN command intended to increase column size is unnecessary and could potentially cause errors if generated using standard SQL syntax.
Sources: src/main/java/liquibase/ext/clickhouse/sqlgenerator/ModifyDataTypeClickHouse.java:31-34, src/main/java/liquibase/ext/clickhouse/sqlgenerator/CreateDatabaseChangeLogTableClickHouse.java:51-65
Class Relationships and Logic Flow
The following diagram illustrates how the ModifyDataTypeClickHouse class integrates into the Liquibase SQL generation chain.
The diagram shows the inheritance from the core Liquibase generator and the dependency on the ClickHouseDatabase implementation for validation.
Sources: src/main/java/liquibase/ext/clickhouse/sqlgenerator/ModifyDataTypeClickHouse.java:36-53, src/main/java/liquibase/ext/clickhouse/database/ClickHouseDatabase.java:38-41
Execution Logic
When Liquibase processes a ModifyDataTypeStatement, it iterates through available generators. The ModifyDataTypeClickHouse generator is selected based on its priority and support criteria.
- Priority: The generator returns
PRIORITY_DATABASE, ensuring it takes precedence over the default Liquibase generator when a ClickHouse database is detected. - Support Check: It validates that the current database instance is an instance of
ClickHouseDatabase. - SQL Generation: It returns an empty array of
Sqlobjects, effectively suppressing any SQL output for data type modifications.
The flowchart describes the decision logic that leads to the suppression of data type modification SQL.
Sources: src/main/java/liquibase/ext/clickhouse/sqlgenerator/ModifyDataTypeClickHouse.java:38-53
Metadata Table Definitions
The suppression of data type modifications is closely tied to how the Liquibase metadata tables are initialized. The extension defines specific ClickHouse-compatible schemas that use the generic String type.
| Table Name | Column | ClickHouse Type | Description |
|---|---|---|---|
DATABASECHANGELOG | ID, AUTHOR, FILENAME | String | Primary identifiers for change sets |
DATABASECHANGELOG | MD5SUM, DESCRIPTION | Nullable(String) | Change set metadata |
DATABASECHANGELOGLOCK | LOCKEDBY | Nullable(String) | Information about the locking host |
Sources: src/main/java/liquibase/ext/clickhouse/sqlgenerator/CreateDatabaseChangeLogTableClickHouse.java:51-65, src/main/java/liquibase/ext/clickhouse/sqlgenerator/CreateDatabaseChangeLogLockTableClickHouse.java:51-57
Implementation Example
The generateSql implementation explicitly returns an empty array to prevent Liquibase from attempting to modify these String columns.
// From ModifyDataTypeClickHouse.java
@Override
public Sql[] generateSql(
ModifyDataTypeStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
return new Sql[0];
}
Sources: src/main/java/liquibase/ext/clickhouse/sqlgenerator/ModifyDataTypeClickHouse.java:51-54
Summary
The "Modifying Data Types" feature in this extension is a preventive measure. By overriding the default SQL generation with a Noop implementation, the extension ensures compatibility with ClickHouse's String type handling. This prevents unnecessary and potentially failing ALTER statements from being executed against the Liquibase tracking tables (DATABASECHANGELOG and DATABASECHANGELOGLOCK), maintaining the integrity of the migration process within ClickHouse clusters.