【问题标题】:Force liquibase to current_timestamp instead of now()强制 liquibase 使用 current_timestamp 而不是 now()
【发布时间】:2020-03-08 00:48:57
【问题描述】:

将 liquibase-core:3.6.3 与 MySQL 一起使用。

我第一次必须对列使用时间戳(3),而不是默认时间戳。由于我的时间戳列不可为空,如果未设置 DEFAULT 值,则添加 current_timestamp(直接在 MySQL 中或使用 liquibase)。

不是我面临 liquibase 生成的 SQL 不使用 CURRENT_TIMESTAMP 而是使用 NOW() 作为要调用的默认函数的问题。这使得时间戳(3)不可能。

有没有办法强制 liquibase 在输出中使用 CURRENT_TIMESTAMP 而不是 NOW()?

- changeSet:
      id: xxx
      author: xxx
      changes:
        - createTable:
            tableName: table_name
            columns:
              - column:
                  name: id
                  type: int unsigned
                  autoIncrement: false
                  constraints:
                    primaryKey: true
              - column:
                  name: updated
                  type: timestamp
                  defaultValueComputed: current_timestamp
                  constraints:
                    nullable: false

输出: CREATE TABLE db_name.table_name(id INT unsigned NOT NULL, updated timestamp DEFAULT NOW() NOT NULL, CONSTRAINT PK_RULE_STATE PRIMARY KEY (id));

这很好用。但是,将timestamp 更改为timestamp(3)

输出: CREATE TABLE db_name.table_name(id INT unsigned NOT NULL, updated timestamp(3) DEFAULT NOW() NOT NULL, CONSTRAINT PK_RULE_STATE PRIMARY KEY (id)); 其中 NOW() 不是有效值。

使用defaultValueDate进行相同的输出

如果我使用defaultValue: current_timestamp(),liquibase 足够聪明,可以将其检测为函数,因为它不使用文字字符串,但是,这仍然是 timestamp(3) 的重要值。最后,使用

- changeSet:
          id: xxx
          author: xxx
          changes:
            - createTable:
                tableName: table_name
                columns:
                  - column:
                      name: id
                      type: int unsigned
                      autoIncrement: false
                      constraints:
                        primaryKey: true
                  - column:
                      name: updated
                      type: timestamp(3)
                      defaultValue: current_timestamp(3)
                      constraints:
                        nullable: false

产生输出:

CREATE TABLE db_name.table_name(id INT unsigned NOT NULL, updated timestamp(3) DEFAULT DEFAULT 'current_timestamp(3)' NOT NULL, CONSTRAINT PK_RULE_STATE PRIMARY KEY (id));

我最后的机会是根本没有设置默认值。除了as this guy said long ago,MySQL 添加了ON UPDATE CURRENT_TIMESTAMP(3) 而我无法避免它之外,这很好用。


那么,有没有办法强制使用 current_timestamp(3) 或作为备份计划,以防止 MySQL 生成 ON UPDATE 部分?

【问题讨论】:

    标签: mysql timestamp liquibase liquibase-hibernate liquibase-sql


    【解决方案1】:

    如果您查看 liquibase 存储库中的数据库配置,您会发现 NOW() 确实设置为 MySQL 的 currentDateTimeFuntion

    public MySQLDatabase() {
        super.setCurrentDateTimeFunction("NOW()");
        ...
    }
    

    因此,强制 current_timestamp(3) 的一种方法是使用基于 MySQLDatabase 的自定义 liquibase.database.Database 实现:

    public class CustomMySQLDatabase extends MySQLDatabase {
    
        public CustomMySQLDatabase() {
            super();
            super.setCurrentDateTimeFunction("CURRENT_TIMESTAMP(3)");
        }
        ...
    

    之后你可以在调用liquibase时使用CustomMySQLDatabase作为--databaseClass参数。

    【讨论】:

      【解决方案2】:

      您可以使用带有replace 选项的modifySql 命令代替自定义的liquibase 类,将自动生成的NOW() 替换为CURRENT_TIMESTAMP(3)(或任何您需要的)。

      来自文档:

      ...有时生成的 SQL 需要根据您的特定需求略有不同

      更多详情请看这里:https://docs.liquibase.com/workflows/liquibase-community/modify-sql.html

      【讨论】:

        【解决方案3】:

        this GitHub issue 中所述,您可以将值NOW(3) 用于defaultValueComputed,如下例所示:

        <column defaultValueComputed="NOW(3)" name="DateTime" type="TIMESTAMP(3)">
            <constraints nullable="false" />
        </column>
        

        那么,结果如下:

        mysql> SHOW COLUMNS FROM myTable;
        +-----------+---------------+------+-----+----------------------+-------------------+
        | Field     | Type          | Null | Key | Default              | Extra             |
        +-----------+---------------+------+-----+----------------------+-------------------+
        | DateTime  | timestamp(3)  | NO   |     | CURRENT_TIMESTAMP(3) | DEFAULT_GENERATED |
        +-----------+---------------+------+-----+----------------------+-------------------+
        

        【讨论】:

          猜你喜欢
          • 2020-04-05
          • 2011-02-28
          • 1970-01-01
          • 2017-12-12
          • 1970-01-01
          • 2013-07-09
          • 2012-08-17
          • 1970-01-01
          相关资源
          最近更新 更多