nykon
nykon

Reputation: 634

Liquibase scripts with PostgresQL and with H2

Currently my default Liquibase scripts are set without giving a dbms - this should be treated as 'is valid for all databases'.

Some scripts rely on database specific features like JSONB:

POSTGRES

<changeSet id="add_jsonb_field" author="me" dbms="postgresql">
        <preConditions />

        <addColumn tableName="postgresTable">
            <column name="someField" type="JSONB" />
        </addColumn>

        <rollback />
</changeSet>

H2

<changeSet id="add_text_field" author="me" dbms="h2">
        <preConditions />

        <addColumn tableName="postgresTable">
            <column name="someField" type="text" />
        </addColumn>

        <rollback />
</changeSet>

My assumption currently is that, with no dbms specified the script applies to any database. With dbms specified, it only applies to given database type.

So h2 should execute all general scripts and additionally the ones markes with h2.

Is my assumption correct or will h2 leave out scripts that do not carry a dbms marker?

Upvotes: 2

Views: 1953

Answers (1)

user330315
user330315

Reputation:

I would approach this differently to avoid having to write the <addColumn> twice.

In your "main" changelog (before any other changeSet), put the following property definitions:

  <property name="json_type" value="jsonb" dbms="postgresql"/>
  <property name="json_type" value="text" dbms="h2"/>

Then you only need a single changeset (without the dbms attribute) which uses the placeholder for the data type:

<changeSet id="add_json_column" author="me">
        <preConditions />

        <addColumn tableName="postgresTable">
            <column name="someField" type="${json_type}" />
        </addColumn>

        <rollback />
</changeSet>

Upvotes: 5

Related Questions