Search code examples
db2liquibasedb2-luw

Can Liquibase use DB2's multiple tablespaces?


In DB2 LUW it is recommended that tables, indexes and "long objects" (ie LOBs) should be placed in separate tablespaces. Our existing SQL scripts have lines like this:

CREATE TABLE "APPLICATIONTABLE"(
    APPLICATIONID INTEGER NOT NULL,
    APPLICATIONNAME VARCHAR(30) NOT NULL
)IN USERSPACE1 INDEX IN USERSPACE2 LONG IN USERSPACE3;

In a changeset we can specifiy a tablespace:

<createTable tableName="APPLICATIONTABLE" tablespace="${tablespace.data}">
    <column name="APPLICATIONID" type="NUMBER(4, 0)">
        <constraints nullable="false"/>
    </column>
    <column name="APPLICATIONNAME" type="VARCHAR(30)">
        <constraints nullable="false"/>
    </column>
</createTable>

But not, as far as I can see, DB2's multiple tablespaces. Is there any way to do this?


Solution

  • You could use a sql change which liquibase doc describes as:

    The ‘sql’ tag allows you to specify whatever sql you want. It is useful for complex changes that aren’t supported through Liquibase’s automated refactoring tags and to work around bugs and limitations of Liquibase. The SQL contained in the sql tag can be multi-line.

    It looks like this (copied from the liquibase doc):

    <changeSet author="liquibase-docs" id="sql-example">
        <sql dbms="h2, oracle"
                endDelimiter="\nGO"
                splitStatements="true"
                stripComments="true">insert into person (name) values ('Bob')
            <comment>What about Bob?</comment>
        </sql>
    </changeSet>
    

    EDIT:

    Just saw another option after reading this answer. You could use the modifySql tag. Maybe leave out tablespace info and just append it like this (untested code - just a suggestion):

    <createTable tableName="APPLICATIONTABLE">
        <column name="APPLICATIONID" type="NUMBER(4, 0)">
            <constraints nullable="false"/>
        </column>
        <column name="APPLICATIONNAME" type="VARCHAR(30)">
            <constraints nullable="false"/>
        </column>
        <modifySql dbms="db2">
            <append value=" IN USERSPACE1 INDEX IN USERSPACE2 LONG IN USERSPACE3"/>
        </modifySql>
    </createTable>