How can I use the Oracle `user` and `sysdate` functions in a Liquibase insert change set?

时间:2019-04-17 02:15:00

标签: oracle liquibase

I have a changeSet that looks similar to the one below:

<changeSet author="cwilliams" id="xxx">
    <insert tableName="SOME_TABLE">
        <!-- ... -->
        <column name="CREATED_BY_USER_NAME" value="user"/>
        <column name="CREATED_DT" valueDate="SYSDATE"/>
        <!-- ... -->
    </insert>
</changeSet>

I'd like to set these two columns to the database user name and the current time, respectively. I put user and sysdate in as examples but I assume user would try to insert a user name of user and that's not what I want.

Is there a way to tell Liquibase to use the underlying database's functions for these values in a database agnostic way and, if not, is there a way to use specific Oracle functions for this purpose?

2 个答案:

答案 0 :(得分:2)

您可以使用valueComputed属性:

<changeSet author="cwilliams" id="xxx">
    <insert tableName="SOME_TABLE">
        <!-- ... -->
        <column name="CREATED_BY_USER_NAME" valueComputed="user"/>
        <column name="CREATED_DT" valueComputed="SYSDATE"/>
        <!-- ... -->
    </insert>
</changeSet>

另一种选择是,在为这些列创建表时定义默认值,然后将其完全保留在插入中。

答案 1 :(得分:0)

我不知道,我不使用Liquibase。

但是,如果您有权访问该架构,则可以创建一个BEFORE INSERT数据库触发器,例如

create or replace trigger trg_bi_sometab
  before insert on some_table
  for each row
begin
  :new.created_by_user_name := user;
  :new.created_dt := sysdate;
end trg_bi_sometab;
/

在每次插入时,都将使用适当的值填充这些列。