CockroachDB 中 dropIndex 的 CASCADE

CASCADE for dropIndex in CockroachDB

Liquibase: 以下 liquibase 写了删除 unq_customer_id 约束。

 <dropIndex indexName="unq_customer_id"
            schemaName="public"
            tableName="CUSTOMER_LOGIN_EVENT"/>

我使用了 dropIndex,因为 CockroachDB 通过创建索引来实现唯一约束。 CASCADE 需要删除此索引。

但是,我不确定如何在上面的 liquibase 脚本中使用级联,因为 dropIndex XSD 没有 CASCADE.

的选项
<xsd:element name="dropIndex">
    <xsd:complexType>
       <xsd:attributeGroup ref="tableNameAttribute"/>
       <xsd:attributeGroup ref="indexName"/>
       <xsd:attribute name="associatedWith" type="xsd:string" use="optional" />
    </xsd:complexType>
</xsd:element> 

错误:

ERROR: index "unq_customer_id" is in use as unique constraint (use CASCADE if you really want to drop it) [Failed SQL: DROP INDEX public.unq_customer_id]

直接的答案是,当任何 Liquibase 函数中的逻辑不满足您的需求时,您的选择是:

  1. 使用 <sql> 标签准确指定 sql。这使您可以准确控制 运行 的内容,并允许您使用 Liquibase 处理您的数据库通过 SQL
  2. 公开的任何内容

示例:

<changeSet id="1" author="xyz">
   <sql>DROP INDEX customer_login_event@unq_customer_id cascade</sql>
<changeSet>
  1. 向您的变更集添加一个 <modifySql> 块。这允许您仍然使用 dropIndex 标签,但调整 SQL.

示例:

<changeSet id="1" author="xyz">
    <dropIndex indexName="unq_customer_id"
               schemaName="public"
               tableName="CUSTOMER_LOGIN_EVENT"/>
    <modifySql>
        <replace replace="unq_customer_id" with="customer_login_event@unq_customer_id"/>
        <append value=" CASCADE"/>
    </modifySql>
</changeSet>

但是,我还想问一下,在您的情况下,更好的答案是编写删除约束而不是索引的脚本吗? DROP...CASCADE 可能是一个可怕的调用,因为您不是 seeing/controlling 实际上被丢弃的东西。

他们确实通过创建索引来实现约束,但从您的角度来看,这应该只是一个实现细节。如果您最初创建了一个约束,更 readable/understandable/safer 的事情可能是删除应该导致 CockroachDB 删除相应索引的约束,而不是以透明删除约束的方式删除索引。