-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: repair create mysql table syntax error 🐛
- Loading branch information
1 parent
fd9d639
commit 67db9c7
Showing
1 changed file
with
21 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,43 @@ | ||
const pool = require('./db'); | ||
const pool = require('./db'); // 引入数据库连接池 | ||
|
||
async function checkAndCreateTable() { | ||
const connection = await pool.getConnection(); | ||
const connection = await pool.getConnection(); // 从连接池获取连接 | ||
try { | ||
// 检查表是否存在 | ||
const [rows] = await connection.execute( | ||
"SHOW TABLES LIKE 'files';" | ||
); | ||
|
||
if (rows.length === 0) { | ||
// 创建表 | ||
await connection.execute( | ||
`CREATE TABLE files ( | ||
id VARCHAR(50) DEFAULT NULL | ||
id VARCHAR(50) DEFAULT NULL, | ||
filename VARCHAR(255) NOT NULL, | ||
filesize BIGINT NOT NULL, | ||
filesize BIGINT(20) NOT NULL, | ||
filelocation VARCHAR(255) NOT NULL, | ||
created_by VARCHAR(255) NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_by VARCHAR(255) DEFAULT NULL, | ||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
is_public BOOLEAN DEFAULT FALSE, | ||
public_expiration TIMESTAMP DEFAULT NULL, | ||
updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
is_public TINYINT(1) DEFAULT '0', | ||
public_expiration TIMESTAMP NULL DEFAULT NULL, | ||
public_by VARCHAR(255) DEFAULT NULL, | ||
is_thumb BOOLEAN DEFAULT FALSE, | ||
is_thumb TINYINT(1) DEFAULT NULL, | ||
thumb_location VARCHAR(255) DEFAULT NULL, | ||
is_delete BOOLEAN NOT NULL DEFAULT FALSE | ||
);` | ||
is_delete TINYINT(1) NOT NULL DEFAULT '0', | ||
real_file_location VARCHAR(255) DEFAULT NULL, | ||
real_file_thumb_location VARCHAR(255) DEFAULT NULL | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;` | ||
); | ||
console.log('Table created successfully.'); | ||
console.log('Table "files" created successfully.'); | ||
} else { | ||
console.log('Table already exists.'); | ||
console.log('Table "files" already exists.'); | ||
} | ||
} catch (error) { | ||
console.error('Error creating table:', error); | ||
} finally { | ||
connection.release(); | ||
connection.release(); // 释放连接 | ||
} | ||
} | ||
|
||
module.exports = { checkAndCreateTable }; | ||
module.exports = { checkAndCreateTable }; // 导出函数 |