I'm searching for a way to automatically add a prefix to my table names when liquibase executes my xml files. For example, if this is my initial-changelog.xml:
<changeSet id="initial_changeset" author="ME">
<createTable tableName="EMP">
<column name="ID" type="UUID">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="REFERENCE" type="VARCHAR(250)">
<constraints nullable="false"/>
</column>
</createTable>
<createIndex tableName="EMP" indexName="IDX_EMP_REFERENCE" unique="true">
<column name="ESS_REFERENCE"/>
</createIndex>
<createTable tableName="COMPANY_CAR">
<column name="ID" type="UUID">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="START_DATE" type="DATE">
<constraints nullable="false"/>
</column>
<column name="END_DATE" type="DATE">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
I would like to have my tables generated with a specific prefix, for example:
TBL_EMP
TBL_COMPANY_CAR
Can I specify a prefix in the XML? Or is there an other way to force it?
Also the tableName indexes should have the prefixes too...
I'm using liquibase with a MYSQL database using Java Spring JPA
TRY 1: I have tried this already but this doesn't overwrite the liquibase xml config:
hibernate.physical_naming_strategy=be.everesst.employee.adapter.jpa.config.CustomHibernateNamingStrategy
With this in my application.properties
file
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomHibernateNamingStrategy extends PhysicalNamingStrategy {
public static final String TABLE_NAME_PREFIX = "EMPLOYEE_";
@Override
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnvironment) {
return prefix(identifier);
}
private Identifier prefix(Identifier identifier) {
return Identifier.toIdentifier(TABLE_NAME_PREFIX + identifier.getText());
}
}
TRY 2: I have tried this alsobut this doesn't overwrite the liquibase xml config aswell:
spring.jpa.hibernate.naming.physical-strategy=be.everesst.employee.adapter.jpa.config.PrefixPhysicalNamingStrategy
With this in my application.properties
file
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
public class PrefixPhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl {
public static final String TABLE_NAME_PREFIX = "EMPLOYEE_";
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
Identifier newIdentifier = new Identifier(TABLE_NAME_PREFIX + name.getText(), name.isQuoted());
return super.toPhysicalTableName(newIdentifier, context);
}
FOUND A SOLUTION of Liquibase itself: using the modifySql statement
(I removed all the custom hibernate naming strategies!)
I created 2 properties:
<property name="table.prefix" value="TBL_"/>
<property name="schema.name" value="PUBLIC"/>
Then added the following statements:
<modifySql>
<regExpReplace replace="CREATE\ TABLE\ ${schema.name}.([\w]*)\ (.*)" with="CREATE TABLE ${schema.name}.${table.prefix}$1 $2"/>
</modifySql>
<modifySql>
<regExpReplace replace="CREATE\ UNIQUE\ INDEX\ ${schema.name}.([\w]*)\ ON\ PUBLIC.([\w]*)\((.*)\)" with="CREATE UNIQUE INDEX ${schema.name}.$1 ON ${schema.name}.${table.prefix}$2($3)"/>
</modifySql>
<modifySql>
<regExpReplace replace="CREATE\ INDEX\ ${schema.name}.([\w]*)\ ON\ ${schema.name}.([\w]*)\((.*)\)" with="CREATE INDEX ${schema.name}.$1 ON ${schema.name}.${table.prefix}$2($3)"/>
</modifySql>