Skip to content

cronn/liquibase-postgres-enum-extension

Repository files navigation

CI Maven Central Apache 2.0 codecov Valid Gradle Wrapper

Liquibase Extension for native PostgreSQL enums

This library provides support for native PostgreSQL enums in Liquibase. This library was designed to be used in conjunction with our liquibase-changelog-generator library such that in many cases the required changelogs are generated automatically. Furthermore, this library was designed such that it works with Hibernate’s support for native PostgreSQL enums that was introduced in Hibernate 6.2.

Usage

Add the following Maven runtime dependency to your project:

<dependency>
    <groupId>de.cronn</groupId>
    <artifactId>liquibase-postgres-enum-extension</artifactId>
    <version>1.1</version>
    <scope>runtime</scope>
</dependency>

Hibernate

For Hibernate entities, we recommend annotating attributes of enum type as shown below:

@JdbcType(PostgreSQLEnumJdbcType.class)
Status status;

Commands

The library provides additional liquibase commands, that can be used within a changeSet:

Creating an enum type

<ext:createPostgresEnumType name="color" values="RED, GREEN, BLUE"/>

Adding one or more values to an existing enum type

<ext:addPostgresEnumValues enumTypeName="color" valuesToAdd="BLACK, WHITE"/>

Renaming an existing enum value

<ext:renamePostgresEnumValue enumTypeName="color" oldValue="BLACK" newValue="KEY"/>

Removing one or more values of an existing enum

PostgreSQL does not yet support removing of values of an existing enum. Instead, we implement a workaround by replacing the enum with a new enum type that has different values (as described in this blog post).

⚠ You need to be extra careful when you drop an enum value! First, you need to update the existing tables to make sure that the value is not used anymore, typically using an UPDATE statement.

<ext:modifyPostgresEnumType name="color" newValues="CYAN, MAGENTA, YELLOW, KEY"/>

Renaming an existing enum type

<ext:renamePostgresEnumType oldName="color" newName="colour"/>

Dropping an existing enum type

<ext:dropPostgresEnumType name="color"/>

Requirements

  • Java 17+
  • Liquibase 4.27+

About

Liquibase Extension for native PostgreSQL enums

Resources

License

Stars

Watchers

Forks