我有以下变更集:
ax.get_xlim()
我想检查是否已存在唯一约束:
<changeSet author="test" id="1517488978218-28">
<addUniqueConstraint columnNames="name_" constraintName="UK_52q0nm9af3039btfxjt8q6ahj" tableName="settings_table"/>
</changeSet>
但它不起作用,因为我不知道数据库名称。 我需要在SQL查询中获取数据库名称,我该如何获取它? 我的数据源初始化代码:
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">select distinct CONSTRAINT_NAME from information_schema.TABLE_CONSTRAINTS where constraint_type = 'UNIQUE' AND CONSTRAINT_NAME ='UK_52q0nm9af3039btfxjt8q6ahj' AND table_schema = '${databasename}'</sqlCheck>
</preConditions>
答案 0 :(得分:0)
我写下以下CustomPrecondition:
public class UniqueConstraintExists implements CustomPrecondition {
private String constraintName;
private String tableName;
public void setConstraintName(String constraintName) {
this.constraintName = constraintName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
@Override
public void check(Database database) throws CustomPreconditionFailedException, CustomPreconditionErrorException {
if(database instanceof MySQLDatabase){
String defaultCatalogName = database.getDefaultCatalogName();
String sql = String.format("select count(distinct CONSTRAINT_NAME) " +
"from information_schema.TABLE_CONSTRAINTS " +
"where constraint_type = 'UNIQUE' AND CONSTRAINT_NAME ='%s' AND TABLE_NAME='%s' AND table_schema = '%s'",
constraintName, tableName, defaultCatalogName);
RawSqlStatement statement = new RawSqlStatement(sql);
int count = 0;
try {
count = ExecutorService.getInstance().getExecutor(database).queryForInt(statement);
} catch (DatabaseException e) {
throw new CustomPreconditionErrorException("Unique constraint " + constraintName + " get count exception",e);
}
if(count == 1){
//exists
return;
}else if(count == 0){
//not exists
throw new CustomPreconditionFailedException(String.format("Unique constraint %s not exists", constraintName));
}else{
//impossible
throw new CustomPreconditionErrorException("Count of constraint instances " + constraintName + " more than 1 or less than 0, count: " + count);
}
}
throw new CustomPreconditionErrorException("Unsupported database for UniqueConstraintExists precondition: " + database.getDatabaseProductName() + ", mysql and mariadb are only supported");
}
}
变更集:
<changeSet author="macmonitor.by" id="1517488978218-28">
<preConditions onFail="MARK_RAN">
<not>
<customPrecondition className="by.macmonitor.liquibase.UniqueConstraintExists">
<param name="constraintName" value="UK_52q0nm9af3039btfxjt8q6ahj"/>
<param name="tableName" value="settings_table"/>
</customPrecondition>
</not>
</preConditions>
<addUniqueConstraint columnNames="name_" constraintName="UK_52q0nm9af3039btfxjt8q6ahj"
tableName="settings_table"/>
</changeSet>