问题
I'm currently developing a prototype for a Spring based plugin system. The idea is that plugins can use JPA entities and a liquibase changelog to maintain the database structure. In order to separate the tables created by plugins from the tables of the core system the plugins should be forced to use a prefix for table name.
For JPA/Hibernate that can be easily archived by using a naming strategy. But I've found no way to archive that for the liquibase changeset.
For example the plugin defines a changelog like follows
<changeSet id="2015-03-17-00-01" author="foo">
<createTable tableName="fooentity">
<column name="id" type="INT">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="name" type="VARCHAR(100)">
<constraints nullable="false" />
</column>
</createTable>
</changeSet>
The table should be created with name "plugin_fooentity". The plugin itself should not know anything about the prefix since the prefix is given by the plugin/core system.
Would be great if someone can give me a hint for a possible solution.
回答1:
Maybe you can use modifySql for this?
You would have to copy this to all changesets that you define but it should be possible.
It has a subtag called regExpReplace
which you could use to define a general term like create table (\w*?) .*
and replace this with create table plugin_$1
.
回答2:
For me it worked like this:
Using the modifySql statement
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>
来源:https://stackoverflow.com/questions/29099775/liquibase-table-name-prefix