You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过单一Liquibase变更集实现PostgreSQL、Oracle大小写不敏感唯一索引

问题

我正在开发支持PostgreSQL、Oracle和MSSQL的changelog,创建唯一索引时需要用LOWER()函数实现大小写不敏感。目前针对PostgreSQL和Oracle分别维护了两个独立的变更集:

PostgreSQL 变更集:

<changeSet author="abc(generated)" id="1733309361372-323" dbms="postgresql">
    <createIndex indexName="INX_USERS_LOGIN_ID" tableName="USERS" unique="true">
        <column computed="true" name="lower((&quot;LOGIN_ID&quot;)::text)"/>
    </createIndex>
</changeSet>

Oracle 变更集:

<changeSet author="abc(generated)" id="1733309361372-323" dbms="oracle">
    <createIndex indexName="INX_USERS_LOGIN_ID" tableName="USERS" unique="true">
        <column computed="true" name="lower(&quot;LOGIN_ID&quot;)"/>
    </createIndex>
</changeSet>

想知道能不能合并成单一变更集,实现同时适配PostgreSQL和Oracle的方案。

解决方案

方法1:用<sql>标签配合数据库条件判断

直接使用数据库原生SQL,通过dbms属性指定执行分支,将两种数据库的索引创建逻辑合并到一个变更集:

<changeSet author="abc(generated)" id="1733309361372-323">
    <!-- PostgreSQL 专属逻辑 -->
    <sql dbms="postgresql">
        CREATE UNIQUE INDEX INX_USERS_LOGIN_ID ON USERS (LOWER(LOGIN_ID::TEXT));
    </sql>
    <!-- Oracle 专属逻辑 -->
    <sql dbms="oracle">
        CREATE UNIQUE INDEX INX_USERS_LOGIN_ID ON USERS (LOWER(LOGIN_ID));
    </sql>
</changeSet>

Liquibase会根据当前连接的数据库类型,自动执行对应分支的SQL语句,逻辑直观且易于维护。

方法2:利用Liquibase属性替换功能

先在changelog中定义数据库专属的表达式属性,再在变更集中动态引用:

  1. 定义属性
<property name="lower_login_id_expr" value="lower((&quot;LOGIN_ID&quot;)::text)" dbms="postgresql"/>
<property name="lower_login_id_expr" value="lower(&quot;LOGIN_ID&quot;)" dbms="oracle"/>
  1. 统一变更集
<changeSet author="abc(generated)" id="1733309361372-323">
    <createIndex indexName="INX_USERS_LOGIN_ID" tableName="USERS" unique="true">
        <column computed="true" name="${lower_login_id_expr}"/>
    </createIndex>
</changeSet>

Liquibase会自动识别当前数据库类型,替换为对应的表达式,保持变更集结构的统一性。

扩展:兼容MSSQL的补充

如果后续需要适配MSSQL,其函数索引语法与Oracle类似,可直接扩展上述方案:

  • 方法1中新增<sql dbms="mssql">CREATE UNIQUE INDEX INX_USERS_LOGIN_ID ON USERS (LOWER(LOGIN_ID));</sql>
  • 方法2中新增<property name="lower_login_id_expr" value="lower(&quot;LOGIN_ID&quot;)" dbms="mssql"/>

内容的提问来源于stack exchange,提问作者Piyush Bali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 17:40:05