Skip to content

Commit 67db9c7

Browse files
committed
fix: repair create mysql table syntax error 🐛
1 parent fd9d639 commit 67db9c7

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

utils/checkAndCreateTable.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
1-
const pool = require('./db');
1+
const pool = require('./db'); // 引入数据库连接池
22

33
async function checkAndCreateTable() {
4-
const connection = await pool.getConnection();
4+
const connection = await pool.getConnection(); // 从连接池获取连接
55
try {
6+
// 检查表是否存在
67
const [rows] = await connection.execute(
78
"SHOW TABLES LIKE 'files';"
89
);
910

1011
if (rows.length === 0) {
12+
// 创建表
1113
await connection.execute(
1214
`CREATE TABLE files (
13-
id VARCHAR(50) DEFAULT NULL
15+
id VARCHAR(50) DEFAULT NULL,
1416
filename VARCHAR(255) NOT NULL,
15-
filesize BIGINT NOT NULL,
17+
filesize BIGINT(20) NOT NULL,
1618
filelocation VARCHAR(255) NOT NULL,
1719
created_by VARCHAR(255) NOT NULL,
18-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
20+
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
1921
updated_by VARCHAR(255) DEFAULT NULL,
20-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
21-
is_public BOOLEAN DEFAULT FALSE,
22-
public_expiration TIMESTAMP DEFAULT NULL,
22+
updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
23+
is_public TINYINT(1) DEFAULT '0',
24+
public_expiration TIMESTAMP NULL DEFAULT NULL,
2325
public_by VARCHAR(255) DEFAULT NULL,
24-
is_thumb BOOLEAN DEFAULT FALSE,
26+
is_thumb TINYINT(1) DEFAULT NULL,
2527
thumb_location VARCHAR(255) DEFAULT NULL,
26-
is_delete BOOLEAN NOT NULL DEFAULT FALSE
27-
);`
28+
is_delete TINYINT(1) NOT NULL DEFAULT '0',
29+
real_file_location VARCHAR(255) DEFAULT NULL,
30+
real_file_thumb_location VARCHAR(255) DEFAULT NULL
31+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`
2832
);
29-
console.log('Table created successfully.');
33+
console.log('Table "files" created successfully.');
3034
} else {
31-
console.log('Table already exists.');
35+
console.log('Table "files" already exists.');
3236
}
37+
} catch (error) {
38+
console.error('Error creating table:', error);
3339
} finally {
34-
connection.release();
40+
connection.release(); // 释放连接
3541
}
3642
}
37-
38-
module.exports = { checkAndCreateTable };
43+
module.exports = { checkAndCreateTable }; // 导出函数

0 commit comments

Comments
 (0)