修复内容系统发布到 AI 官网失败:MySQL 严格模式不接受带时区的 ISO 时间(如 2026-10-19T06:55:53.409Z),写库前统一转成本地时间
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+16
-3
@@ -5,6 +5,19 @@ let db;
|
|||||||
|
|
||||||
const requiredConfig = ['DB_HOST', 'DB_PORT', 'DB_NAME', 'DB_USER', 'DB_PASSWORD'];
|
const requiredConfig = ['DB_HOST', 'DB_PORT', 'DB_NAME', 'DB_USER', 'DB_PASSWORD'];
|
||||||
|
|
||||||
|
// MySQL (strict mode) rejects ISO date strings with a time zone, e.g. "2026-10-19T06:55:53.409Z", in DATETIME
|
||||||
|
// columns. The models and the content ingest service write such strings, so they are turned into plain local
|
||||||
|
// date-times here. The server and MySQL both run on China time, which is how the existing rows are stored.
|
||||||
|
const ISO_DATETIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;
|
||||||
|
const pad = (value) => String(value).padStart(2, '0');
|
||||||
|
const toMySqlParam = (value) => {
|
||||||
|
if (typeof value !== 'string' || !ISO_DATETIME.test(value)) return value;
|
||||||
|
const date = new Date(value);
|
||||||
|
if (Number.isNaN(date.getTime())) return value;
|
||||||
|
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} `
|
||||||
|
+ `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
||||||
|
};
|
||||||
|
|
||||||
class MySqlStatement {
|
class MySqlStatement {
|
||||||
constructor(connection, sql) {
|
constructor(connection, sql) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
@@ -12,7 +25,7 @@ class MySqlStatement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
all(...params) {
|
all(...params) {
|
||||||
const result = this.connection.query(this.sql, params);
|
const result = this.connection.query(this.sql, params.map(toMySqlParam));
|
||||||
return Array.isArray(result) ? result : [];
|
return Array.isArray(result) ? result : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,7 +35,7 @@ class MySqlStatement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
run(...params) {
|
run(...params) {
|
||||||
const result = this.connection.query(this.sql, params) || {};
|
const result = this.connection.query(this.sql, params.map(toMySqlParam)) || {};
|
||||||
return {
|
return {
|
||||||
changes: result.affectedRows || 0,
|
changes: result.affectedRows || 0,
|
||||||
lastInsertRowid: result.insertId || 0,
|
lastInsertRowid: result.insertId || 0,
|
||||||
@@ -83,4 +96,4 @@ const closeDb = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { getDb, closeDb };
|
module.exports = { getDb, closeDb, toMySqlParam };
|
||||||
|
|||||||
Reference in New Issue
Block a user