How to manually increment UNIX_TIMESTAMP() when insert a new row with liquibase? - mysql

I have a cart table with a cart_number column which I would like to be the current time in milliseconds.
I've created a database change log file and everything works fine but the problem is that the span of time between 2 inserts is too short to get a "unique" cart_number. Here is the liquibase insert:
<property name="now" value="UNIX_TIMESTAMP()" dbms="mysql"/>
<changeSet id="2" author="Me">
<insert tableName="cart">
<column name="user_id" value="1"/>
<column name="cart_number" valueDate="${now}"/>
</insert>
<insert tableName="cart">
<column name="user_id" value="2"/>
<column name="cart_number" valueDate="${now}"/>
</insert>
</changeSet>
And I know this isn't a solution, but I've tried to write something like ${now+1} but it didn't worked. Any help would be appreciated. Thank you in advance.

Related

Adding 1 day from the current date in liquibase property

I need to insert a date by adding 1 day to the current date using Liquibase.
I'm using Liquibase property for this. But it is always inserting the current date.
Following is the property I'm using
<property name="now" value="now() + interval 1 day" dbms="mysql" />
I also tried
<property name="now" value="now() + interval '1 day'" dbms="mysql" />
didn't work either.
In my changeset I've the insert script
<insert tableName="test">
<column name="start_date" value="${now}"/>
</insert>
The following worked for me
<property name="now" value="now()" dbms="mysql" />
In changeSet
<insert tableName="test">
<column name="start_date" valueComputed="DATE_ADD(${now}, INTERVAL 1 DAY)"/>
</insert>

How to set column default value to today's date in SQL using XML

I want to add a column to one of my SQL schema's tables.
I have tried the following:
<property name="now" value="now()" dbms="mysql"/>
<changeSet author="Trey Collier" id="SCHEJ-376">
<addColumn tableName="schedule">
<column name="dateLastPublished" type="DATETIME" defaultValueDate="${now}">
<constraints nullable="false" />
</column>
</addColumn>
</changeSet>
However, this inserts the date in the format YYYY-MM-DD hh:mmm:ss, but I only want the YYYY-MM-DD part of it. How can I change my current XML code to make this happen? Thanks
You should change defaultValueDate="${now}" with defaultValueDate="(DATE(CURRENT_TIMESTAMP))" and type="DATETIME" with type="DATE"

Liquibase - Add defaultValueComputed as CURRENT_TIMESTAMP to timestamp column

I am using liquibase 3.5.3 to run liquibase update command on MySql 5.5. I have below changeSet to create a table which has a column as Created_Time that should have a default value as CURRENT_TIMESTAMP.
<changeSet author="authorName" id="AutoGeneratedId">
<createTable tableName="aTable">
<column autoIncrement="true" name="Id" type="INT">
<constraints primaryKey="true"/>
</column>
<column name="Code" type="VARCHAR(45)"/>
<column defaultValueComputed="CURRENT_TIMESTAMP" name="Created_Time" type="TIMESTAMP(19)"/>
</createTable>
</changeSet>
While firing liquibase command it throws an exception as
Unexpected error running Liquibase: Invalid default value for 'Created_Time' [Failed SQL: CREATE TABLE aTable (Id INT AUTO_INCREMENT NOT NULL, Code VARCHAR(45) NULL, Created_Time TIMESTAMP(19) DEFAULT NOW() NULL, CONSTRAINT PK_ATABLE PRIMARY KEY (Id))]
Liquibase is converting CURRENT_TIMESTAMP into NOW() that might be causing this issue.
Can some please provide me any solution or alternative to this issue?
Add type as 'TIMESTAMP' as following
<column defaultValueComputed="CURRENT_TIMESTAMP" name="Created_Time" type="TIMESTAMP"/>
This is a weird scenario causes by how liquibase handles DateTime defaultValueComputed or defaultValueDate.
In short anything that starts with current_timestamp or the default function on your target engine to get the current timestamp will replace *the whole string* by only the default function call ignoring anything else you put.
We ended up with something like this:
<column name="created_at" type="datetime"
defaultValueComputed="NOW()">
<constraints nullable="false" />
</column>
<column name="updated_at" type="datetime"
defaultValueComputed="NOW() ON UPDATE NOW()">
<constraints nullable="false" />
</column>
LiquibaseDataType helper function

Boolean insertion without quotes - Liquibase over MySQL

I need to insert new record on this users table with Liquibase. It works well on PostgreSQL, but when it comes to MySQL the SQL output has quotes for the boolean field (u_administrator). Like this:
INSERT INTO users (u_name, ..., u_administrator) VALUES ('Administrator',..., '1')
<changeSet author="jmartins" id="AdminInsertion">
<insert tableName="users" dbms="postgresql">
<column name="u_name" value="Administrator"/>
...
<column name="u_administrator" value="true"/>
</insert>
<insert tableName="users" dbms="mysql">
<column name="u_name" value="Administrator"/>
...
<column name="u_administrator" value="1"/>
</insert>
</changeSet>
So I can I force liquibase to output "u_administrator" without quotes so it can insert correctly on MySQL. Do I need to go for a < sql> custom command?
Thanks anyway.
Use valueBoolean instead of value to tell liquibase it needs to use the database-specific value for a boolean value.

Can applied Liquibase changesets be replaced

If I use a mysql database with utf-8 and InnoDB but liquibase causes an error because row size of a table is too large.
I have a released changeset which causes the error because it exceeds the row size.
<changeSet author="author" id="id">
<addColumn tableName="TABLE">
<column name="COLUMN" type="VARCHAR(5000)" /> <!-- Row size is too long -->
</addColumn>
</changeSet>
The solution for the problem with the row size is to change "VARCHAR(5000)" into "TEXT", but the problem is that this changeset has been executed on some systems so i cannot just change it here.
Is there any solution to tell liquibase that it should replace the changeset by another if not yet executed?
OK found a solution for this. It is possible to use validCheckSum to tell Liquibase that this changeset has been modified.
I just modified the changeset and added the tag like this.
<changeSet author="author" id="id">
<validCheckSum>oldChecksum</validCheckSum>
<validCheckSum>newChecksum</validCheckSum>
<addColumn tableName="TABLE">
<column name="COLUMN" type="TEXT" />
</addColumn>
</changeSet>
After that liquibase accepted the change set as executed so it was possible to make a new changeset which modifies the column.
<changeSet author="author" id="anotherId">
<modifyDataType tableName="TABLE" columnName="COLUMN" newDataType="TEXT" />
</changeSet>