liquibase 中的约束
Constraints in liquibase
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet id="0.1.1.0" author="Bob" failOnError="true">
<createTable tableName="roles">
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
<column name="name" type="varchar(1024)">
<constraints nullable="false" unique="true"/>
</column>
<column name="fam" type="varchar(1024)">
<constraints nullable="false"/>
</column>
</createTable>
<rollback>
</rollback>
我有这样的迁移来创建 table,现在我有更改,需要进行其他迁移。在table中,name字段是唯一的,我要把这个去掉,就是name不应该是唯一的,我需要对fam做一个限制。一个家庭不应分配给两个同名的角色。但是具有不同名称的角色可以包含相同的 fam。提前致谢
如果您想对现有模式添加更改,您应该保持现有更改的原样并编写新的更改集。
所以为了实现需求
the name field is unique, I should get rid of this
您可以使用 <dropUniqueConstraint>
更改,但它需要 constraintName
属性。但是您在创建 name 的唯一约束时没有提供 uniqueConstraintName
,这使得实现变得棘手。给你的约束和索引命名总是一个好习惯。
因此,您可以执行以下操作:
- 创建一个新列,例如non_unique_name
- 将name的所有数据复制到non_unique_name
- 删除名称列
- 将 non_unique_name 列重命名为 name
变更集可能如下所示:
<changeSet id="0.1.1.1" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<not>
<columnExists tableName="roles" columnName="non_unique_name"/>
</not>
</preConditions>
<comment>create a new column, e.g. non_unique_name</comment>
<addColumn tableName="roles">
<column name="non_unique_name" type="varchar(1024)">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
<changeSet id="0.1.1.2" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<columnExists tableName="roles" columnName="non_unique_name"/>
<columnExists tableName="roles" columnName="name"/>
</preConditions>
<comment>copy all the data from name to non_unique_name</comment>
<update tableName="roles">
<column name="non_unique_name" valueComputed="name"/>
</update>
</changeSet>
<changeSet id="0.1.1.3" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<columnExists tableName="roles" columnName="name"/>
</preConditions>
<comment>drop name column</comment>
<dropColumn tableName="roles" columnName="name"/>
</changeSet>
<changeSet id="0.1.1.4" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<columnExists tableName="roles" columnName="non_unique_name"/>
</preConditions>
<comment>rename non_unique_name column to name</comment>
<renameColumn tableName="roles" oldColumnName="non_unique_name" newColumnName="name"
columnDataType="varchar(1024)"/>
</changeSet>
并且为了实现需求:
One dealer should not be assigned to two roles with the same name. But
roles with different names can contain the same dealer.
您可以使用 <addUniqueConstraint>
更改,使用它您可以添加复合唯一约束。 changeSet 可能如下所示:
<changeSet id="0.1.1.5" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<columnExists tableName="roles" columnName="dealer"/>
<columnExists tableName="roles" columnName="name"/>
</preConditions>
<comment>add unique constraint for roles.dealer and roles.name</comment>
<addUniqueConstraint tableName="roles" columnNames="dealer, name"
constraintName="roles_dealer_name_unique" />
</changeSet>
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet id="0.1.1.0" author="Bob" failOnError="true">
<createTable tableName="roles">
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
<column name="name" type="varchar(1024)">
<constraints nullable="false" unique="true"/>
</column>
<column name="fam" type="varchar(1024)">
<constraints nullable="false"/>
</column>
</createTable>
<rollback>
</rollback>
我有这样的迁移来创建 table,现在我有更改,需要进行其他迁移。在table中,name字段是唯一的,我要把这个去掉,就是name不应该是唯一的,我需要对fam做一个限制。一个家庭不应分配给两个同名的角色。但是具有不同名称的角色可以包含相同的 fam。提前致谢
如果您想对现有模式添加更改,您应该保持现有更改的原样并编写新的更改集。
所以为了实现需求
the name field is unique, I should get rid of this
您可以使用 <dropUniqueConstraint>
更改,但它需要 constraintName
属性。但是您在创建 name 的唯一约束时没有提供 uniqueConstraintName
,这使得实现变得棘手。给你的约束和索引命名总是一个好习惯。
因此,您可以执行以下操作:
- 创建一个新列,例如non_unique_name
- 将name的所有数据复制到non_unique_name
- 删除名称列
- 将 non_unique_name 列重命名为 name
变更集可能如下所示:
<changeSet id="0.1.1.1" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<not>
<columnExists tableName="roles" columnName="non_unique_name"/>
</not>
</preConditions>
<comment>create a new column, e.g. non_unique_name</comment>
<addColumn tableName="roles">
<column name="non_unique_name" type="varchar(1024)">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
<changeSet id="0.1.1.2" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<columnExists tableName="roles" columnName="non_unique_name"/>
<columnExists tableName="roles" columnName="name"/>
</preConditions>
<comment>copy all the data from name to non_unique_name</comment>
<update tableName="roles">
<column name="non_unique_name" valueComputed="name"/>
</update>
</changeSet>
<changeSet id="0.1.1.3" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<columnExists tableName="roles" columnName="name"/>
</preConditions>
<comment>drop name column</comment>
<dropColumn tableName="roles" columnName="name"/>
</changeSet>
<changeSet id="0.1.1.4" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<columnExists tableName="roles" columnName="non_unique_name"/>
</preConditions>
<comment>rename non_unique_name column to name</comment>
<renameColumn tableName="roles" oldColumnName="non_unique_name" newColumnName="name"
columnDataType="varchar(1024)"/>
</changeSet>
并且为了实现需求:
One dealer should not be assigned to two roles with the same name. But roles with different names can contain the same dealer.
您可以使用 <addUniqueConstraint>
更改,使用它您可以添加复合唯一约束。 changeSet 可能如下所示:
<changeSet id="0.1.1.5" author="Bob">
<preConditions onFail="MARK_RAN">
<tableExists tableName="roles"/>
<columnExists tableName="roles" columnName="dealer"/>
<columnExists tableName="roles" columnName="name"/>
</preConditions>
<comment>add unique constraint for roles.dealer and roles.name</comment>
<addUniqueConstraint tableName="roles" columnNames="dealer, name"
constraintName="roles_dealer_name_unique" />
</changeSet>