Liquibase VARCHAR2 length in chars Liquibase VARCHAR2 length in chars oracle oracle

Liquibase VARCHAR2 length in chars


There isn't a built in way in liquibase to define a datatype as "varchar 255, char type" and have it output VARCHAR(255) on postgresql and VARCHAR2(255 CHAR) on oracle.

Liquibase is designed to be extendable (liquibase.org/extensions) and you should be able to override how the string "VARCHAR(255)" in the changelog is converted to a database-specific type but it will require some java coding.

Alternately, changelog parameters would allow you to dynamically add CHAR to the definition depending on the database. If you add <property name="varcharUnit" value="CHAR" dbms="oracle"/> to the top of your changelog, then anywhere in your changelog file you can use type="VARCHAR(255 ${varcharUnit})" and it will evaluate to "VARCHAR(255 CHAR)" on oracle and VARCHAR(255) everywhere else. It is more verbose and you need to remember to always add the variable compared to the extension method but it is more straightforward.


Credit goes to Nathan for the inspiration; I'm just adding some clarity.

<property name="char" value="CHAR" dbms="oracle"/><property name="char" value=""/><changeSet … >    <createTable tableName="User">        <column            name="guid"            type="VARCHAR(32)"        />        <column            name="name"            type="VARCHAR(50 ${char})"        />