gpt4 book ai didi

mysql - 强制 liquibase 为 current_timestamp 而不是 now()

转载 作者:行者123 更新时间:2023-11-29 02:09:10 25 4
gpt4 key购买 nike

在 MySQL 中使用 liquibase-core:3.6.3。

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

不是我遇到的问题是 liquibase 生成的 SQL 不使用 CURRENT_TIMESTAMP 而是使用 NOW() 作为要调用的默认函数。这使得 timestamp(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,更新时间戳(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 部分?

最佳答案

如果您查看 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 参数。

关于mysql - 强制 liquibase 为 current_timestamp 而不是 now(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58816496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com