Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquibase: How to set the default value of a date column to be "now" in UTC format?

Tags:

sql

liquibase

How do you set the default value of a date column to be "now" in UTC format? I think the answer involves the defaultValueComputed attribute on the column element.

The documentation states:

defaultValueComputed A value that is returned from a function or procedure call. This attribute will contain the function to call.

What langauge is the function referred to supposed to be written in? Java? Is the function supposed to be the database vendor -specific date function I want to use? Is there any more documentation I can read on this topic?

like image 572
Jeff Avatar asked May 08 '14 18:05

Jeff


People also ask

How to set now() as default value for datetime datatype in MySQL?

How to set NOW () as default value for datetime datatype in MySQL? We can set the now () function as a default value with the help of dynamic default. First, we will create a table with data type” datetime”. After that, we will set now () as the default value for column “MyTime” as shown below.

How to ensure timestamps in database are always encoded in UTC?

take only aware datetime.datetime , return only aware datetime.datetime , never take or return naive datetime.datetime , ensure timestamps in database always to be encoded in UTC, and work as you’d expect. Note that for server_default=func.now () and func.now () to work :

How to use DATETIME type with a dynamic default value?

As of MySQL 5.6.5, you can use the DATETIME type with a dynamic default value: CREATE TABLE foo (creation_time DATETIME DEFAULT CURRENT_TIMESTAMP, modification_time DATETIME ON UPDATE CURRENT_TIMESTAMP) Or even combine both rules: modification_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

What is the default value of lastvisitdate in table users?

I have two columns in table users namely registerDate and lastVisitDate which consist of datetime data type. I would like to do the following. Set lastVisitDate default value to 0000-00-00 00:00:00 Instead of null which it uses by default.


1 Answers

Maybe this topic in the liquibase forum will help?

I think defaultValueComputed will take a database specific function to express "now". In mySQL it would be CURRENT_TIMESTAMP so it could look like this:

<createTable tableName="D_UserSession">     <column name="ts" type="TIMESTAMP" defaultValueComputed="CURRENT_TIMESTAMP"/> </createTable> 

(Copied from the forum post.)

like image 182
Jens Avatar answered Sep 16 '22 11:09

Jens