|
1 | | -const pool = require('./db'); |
| 1 | +const pool = require('./db'); // 引入数据库连接池 |
2 | 2 |
|
3 | 3 | async function checkAndCreateTable() { |
4 | | - const connection = await pool.getConnection(); |
| 4 | + const connection = await pool.getConnection(); // 从连接池获取连接 |
5 | 5 | try { |
| 6 | + // 检查表是否存在 |
6 | 7 | const [rows] = await connection.execute( |
7 | 8 | "SHOW TABLES LIKE 'files';" |
8 | 9 | ); |
9 | 10 |
|
10 | 11 | if (rows.length === 0) { |
| 12 | + // 创建表 |
11 | 13 | await connection.execute( |
12 | 14 | `CREATE TABLE files ( |
13 | | - id VARCHAR(50) DEFAULT NULL |
| 15 | + id VARCHAR(50) DEFAULT NULL, |
14 | 16 | filename VARCHAR(255) NOT NULL, |
15 | | - filesize BIGINT NOT NULL, |
| 17 | + filesize BIGINT(20) NOT NULL, |
16 | 18 | filelocation VARCHAR(255) NOT NULL, |
17 | 19 | created_by VARCHAR(255) NOT NULL, |
18 | | - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 20 | + created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, |
19 | 21 | 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, |
23 | 25 | public_by VARCHAR(255) DEFAULT NULL, |
24 | | - is_thumb BOOLEAN DEFAULT FALSE, |
| 26 | + is_thumb TINYINT(1) DEFAULT NULL, |
25 | 27 | 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;` |
28 | 32 | ); |
29 | | - console.log('Table created successfully.'); |
| 33 | + console.log('Table "files" created successfully.'); |
30 | 34 | } else { |
31 | | - console.log('Table already exists.'); |
| 35 | + console.log('Table "files" already exists.'); |
32 | 36 | } |
| 37 | + } catch (error) { |
| 38 | + console.error('Error creating table:', error); |
33 | 39 | } finally { |
34 | | - connection.release(); |
| 40 | + connection.release(); // 释放连接 |
35 | 41 | } |
36 | 42 | } |
37 | | - |
38 | | -module.exports = { checkAndCreateTable }; |
| 43 | +module.exports = { checkAndCreateTable }; // 导出函数 |
0 commit comments