23

I am having problems changing a column length in my postgres db with liquibase.

I have a table account with a field description varchar(300). I want to change it to varchar(2000).

I have dropped and recreated the primary key in the same file so I don't have permissions issues or schema / db names or anything like this. For the sake of testing I have cleared the table of data.

I am running

<changeSet author="liquibase" id="sample">
    <modifyDataType
        columnName="description"
        newDataType="varchar(2000)"
        schemaName="accountschema"
        tableName="account"/>
</changeSet>

I'm getting this error text but I can't understand the issue. The only constraint the column had was a not null constraint and I successfully added a separate changelog to remove this constraint (ignoring the fact I don't see why this would affect extending the length of the field).

Can anyone point to what I am doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
gringogordo
  • 1,990
  • 6
  • 24
  • 60

4 Answers4

44

You can increase the size of your column like this:

<changeSet author="liquibase" id="sample">
    <modifyDataType
        columnName="description"
        newDataType="varchar(2000)"
        tableName="account"/>
</changeSet>
luiscla27
  • 4,956
  • 37
  • 49
aashii
  • 617
  • 5
  • 6
  • 1
    For MySQL when i use `modifyDataType` it also removes the **NOT NULL** constraint on the column. I use `addNotNullConstraint` on columns to adjust the data type where I need to keep it not null. – Gabe Gates Dec 13 '22 at 19:27
7

The schema defintion in your xml file doesn't allow <modifyDataType ... />.

The version of the xsd file should match the version of Liquibase you are using. The exception looks like you are using the xsd of version 1.9, see http://www.liquibase.org/documentation/xml_format.html

Roland Weisleder
  • 9,668
  • 7
  • 37
  • 59
  • Thanks. Sorry a based it on a colleagues code rather than starting from scratch and didn't even consider versioning. Many thanks. – gringogordo May 20 '16 at 13:03
6

In YAML syntax it would look like:

databaseChangeLog:
  - changeSet:
      id: sample
      author: liquibase
      changes:
        - modifyDataType:
            columnName: description
            newDataType: varchar(2000)
            tableName: account

for XML syntax see aashii's answer.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
1

In Oracle a TEMP column has to be used. Below is an example;

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
 <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
               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.5.xsd">

<changeSet id="example_id.001" author="Jasper">
    <preConditions>
        <not>
            <columnExists tableName="USERS" columnName="ADDRESS_TEMP"/>
        </not>
    </preConditions>
    <comment>change column ADDRESS_TEMP to 20 length</comment>

    <addColumn tableName="USERS ">
        <column name="ADDRESS_TEMP" type="varchar(2000)" />
    </addColumn>

    <sql>update USERS set ADDRESS_TEMP=ADDRESS</sql>
    <dropColumn tableName="USERS" columnName="ADDRESS" />

    <addColumn tableName="USERS">
        <column name="ADDRESS" type="${numeric_20_0}" >
            <constraints nullable="false"/>
        </column>
    </addColumn>

    <sql>update USERS set ADDRESS=ADDRESS_TEMP</sql>
    <dropColumn tableName="USERS" columnName="ADDRESS_TEMP" />
</changeSet>

</databaseChangeLog>
Jasper Lankhorst
  • 1,860
  • 1
  • 16
  • 22