Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquibase adding default value and not null constraint

Tags:

liquibase

I am bit new to Liquibase. I came across a scenario where in one changeSet it is trying to add a default value first and not null constraint next to that.

But problem over here is both <addDefaultValue/> and <addNotNullConstraint/> tags have a default value attributes so eventually I am ending up with an exception.

Below is the changeSet that I have,

<changeSet id="f3047816-2d48-4341-a4ce-deface083cea" author="MineStar" failOnError="true">
  <preConditions onFailMessage="Ignored AlterColumn for REHANDLE of table LOCATION as column does not exist or already has a NOT NULL constraint." onFail="MARK_RAN">
    <columnExists tableName="LOCATION" columnName="REHANDLE"/>
    <ext:columnIsNullable tableName="LOCATION" columnName="REHANDLE"/>
  </preConditions>
  <comment>AHS-1373: AlterColumn LOCATION.REHANDLE - nullability changed from true to false - defaultValue changed from 'null' to '0'</comment>
  <addDefaultValue columnName="REHANDLE" columnDataType="BOOLEAN" defaultValueNumeric="0" tableName="LOCATION"/>
  <addNotNullConstraint columnName="REHANDLE" defaultNullValue="0" columnDataType="BOOLEAN" tableName="LOCATION"/>
</changeSet>

Here one more strange thing I could see is if I rearrange the order of adding default value and not null constraints tags I won't get any exception that is first adding not null constraint and then default value like below. But I should not do that as it effects checksum in database all that I can do is adding a new changeSet to resolve the exception.

<addNotNullConstraint columnName="REHANDLE" defaultNullValue="0" columnDataType="BOOLEAN" tableName="LOCATION"/>

<addDefaultValue columnName="REHANDLE" columnDataType="BOOLEAN" defaultValueNumeric="0" tableName="LOCATION"/>.
like image 334
Jai Avatar asked Nov 26 '14 07:11

Jai


People also ask

Does Liquibase support DML?

What are the supported features of the Liquibase Cloud Spanner extension? The Cloud Spanner Liquibase extension allows you to use Liquibase to target Cloud Spanner databases. The extension supports most of the available features of both Liquibase and Cloud Spanner and supports most DML and DDL commands.

How do I drop a column in Liquibase?

To drop a single column, use the simple form of this element where the tableName and columnName are specified as attributes. To drop several columns, specify the tableName as an attribute, and then specify a set of nested <column> tags. If nested <column> tags are present, the columnName attribute will be ignored.


1 Answers

There is a difference between the defautlNullValue in addNotNullConstraint and defaultValueNumeric in addDefaultValue. Using addDefaultValue just sets a default value for future rows inserted into the column but defaultNullValue in addNotNullConstraint will cause liquibase to generate an additional SQL statement of update location set rehandle=0 where rehandle is null to change the value of already existing rows so that a null constraint can be added.

I would think either order would work fine, what exception were you seeing?

like image 197
Nathan Voxland Avatar answered Oct 06 '22 21:10

Nathan Voxland