Indexing 使用Liquibase使用降序键列创建索引

Indexing 使用Liquibase使用降序键列创建索引,indexing,rdbms,liquibase,database-agnostic,Indexing,Rdbms,Liquibase,Database Agnostic,我想知道是否有一种通用的方法可以使用liquibase创建“有序索引”。将生成此类SQL语句的内容: CREATE INDEX idx_name ON my_table (m_column DESC) 对于oracle、postgresql、mysql和sql server,我需要它 如果没有,我将不得不为每个RDBM手动使用SQL标记。我刚刚查看了liquibase源代码,还没有找到任何关于索引列排序的处理方法。因此,我建议您使用sql和块(我相信大多数dbms对于create index具

我想知道是否有一种通用的方法可以使用liquibase创建“有序索引”。将生成此类SQL语句的内容:

CREATE INDEX idx_name ON my_table (m_column DESC)
对于oracle、postgresql、mysql和sql server,我需要它


如果没有,我将不得不为每个RDBM手动使用
SQL
标记。

我刚刚查看了liquibase源代码,还没有找到任何关于索引列排序的处理方法。因此,我建议您使用
sql
和块(我相信大多数dbms对于
create index
具有相同的语法,因此您可能不需要
modifySql
):


上仍有未解决的liquibase索引排序功能请求

一种可能的解决方法(自1.9起):


请注意,在liquibase 3.2中验证硬化之前,还有更短的变通方法可用:

<createIndex tableName="my_table" indexName="my_index">
    <column name="registration_time DESC"/>
    <column name="id ASC"/>
</createIndex>

要利用Liquibase
createIndex
并支持不同的RDBMSE,您需要使用
modifySql
(如@Vadzim所指出的,直到修复为止)

对于Oracle和PostgreSQL,语法将非常简单(除非PostgreSQL在“始终引用列名”模式下使用):

(在oracle、postgresql、sql server和h2上测试)

然而,,上面的内容看起来很难看。我想使用简单的
会产生更可读的结果,老实说,它的可移植性似乎并不差。

我提交了一份报告,使其成为Liquibase 3.4.0,它不仅可以为索引指定降键列,还可以为主键和唯一键指定降键列这甚至适用于带有引用列名的数据库,如Microsoft SQL Server

它如何工作的示例

使用这个Maven依赖项

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>3.4.1</version>
</dependency>

org.liquibase
液化酶核心
3.4.1

确保将引用的XSD更新为3.4

<?xml version="1.0" ?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    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.4.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

...

</databaseChangeLog>

...

看看重构。我知道如何创建索引,我需要一种按子句指定顺序的方法。您是否尝试将顺序附加到列名中,如
中所示?这当然可能会导致可移植性问题。这不起作用,它会生成:
在my_表上创建索引idx_name(**“m_column DESC”**)
当然,这是可行的,但liquibase的目的是避免这种事情,让软件生成它。虽然可以利用liquibase的
createIndex
,但我会毫不犹豫地直接使用
sql
。如果你坚持不这样做,请参阅我的答案以获得替代方案。如果名称被引用,例如SQL Server上的
[column\u name]
。如果您今天尝试了这一点,请确保将xml文件中的schema/xsd更新为3.4,否则您将得到错误。@macalase根据xsd 3.5更新了答案,而
降序属性再次消失
<modifySql>
    <replace replace="COL1NAME" with="COL1NAME ASC"/>
    <replace replace="COL2NAME" with="COL2NAME DESC"/>
</modifySql>
<modifySql>
    <regExpReplace replace="\bCOL1NAME\b[^,) ]*" with="$0 ASC" />
    <regExpReplace replace="\bCOL2NAME\b[^,) ]*" with="$0 DESC" />
</modifySql>
<createIndex tableName="my_table" indexName="my_index">
    <column name="col1"/>
    <column name="col2" descending="true"/>
</createIndex>

<addPrimaryKey tableName="my_table" columnNames="col1, col2 DESC"/>

<addUniqueConstraint tableName="my_table" columnNames="col1, col2 DESC"/>
<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>3.4.1</version>
</dependency>
<?xml version="1.0" ?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    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.4.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

...

</databaseChangeLog>