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?
This worked for me:
I found it thanks to this answer: https://stackoverflow.com/a/9100388/3107952
As liquibase is common changelog for any database, to make it generic you should not depend on any specific database like oracle, postegres, mysql instead it should be generic enough to work for any/every database.
Below is how it should be implemented :
This should work for all databases, for oracle, it inserts SYSTIMESTAMP as DATA_DEFAULT.
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 beCURRENT_TIMESTAMP
so it could look like this:(Copied from the forum post.)
I used function the database vendor. For Oracle it`s a sysdate:
This works with SQlite:
Adding '$now' didn't work for me. I am using SQlite as the DB.
In MySQL, to use a DATETIME column with fractions of second like
DATETIME(6)
(microseconds precision), use default value ofNOW(6)
(caution:CURRENT_TIMESTAMP(6)
for some reason produces an error with me using liquibase 3.5.3):Note that the value will be stored internally in UTC, but read using the server's timezone settings (
@@global.time_zone
,@@session.time_zone
).