feat: Add 5Why_ prefix to all database tables
- Rename all tables with 5Why_ prefix for namespace isolation - Update models: User.js, Analysis.js, AuditLog.js - Update routes: llmConfig.js - Update scripts: seed-test-users.js, add-deepseek-config.js, add-ollama-config.js - Add migrate-table-prefix.js script for database migration - Update db_schema.sql with new table names - Update views: 5Why_user_analysis_stats, 5Why_recent_analyses Tables renamed: - users -> 5Why_users - analyses -> 5Why_analyses - analysis_perspectives -> 5Why_analysis_perspectives - analysis_whys -> 5Why_analysis_whys - llm_configs -> 5Why_llm_configs - system_settings -> 5Why_system_settings - audit_logs -> 5Why_audit_logs - sessions -> 5Why_sessions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,7 +13,7 @@ class Analysis {
|
||||
|
||||
try {
|
||||
const [result] = await pool.execute(
|
||||
`INSERT INTO analyses (user_id, finding, job_content, output_language, status)
|
||||
`INSERT INTO 5Why_analyses (user_id, finding, job_content, output_language, status)
|
||||
VALUES (?, ?, ?, ?, 'pending')`,
|
||||
[user_id, finding, job_content, output_language]
|
||||
);
|
||||
@@ -30,7 +30,7 @@ class Analysis {
|
||||
static async findById(id) {
|
||||
try {
|
||||
const [rows] = await pool.execute(
|
||||
'SELECT * FROM analyses WHERE id = ?',
|
||||
'SELECT * FROM 5Why_analyses WHERE id = ?',
|
||||
[id]
|
||||
);
|
||||
return rows[0] || null;
|
||||
@@ -45,7 +45,7 @@ class Analysis {
|
||||
static async updateStatus(id, status, errorMessage = null) {
|
||||
try {
|
||||
await pool.execute(
|
||||
'UPDATE analyses SET status = ?, error_message = ? WHERE id = ?',
|
||||
'UPDATE 5Why_analyses SET status = ?, error_message = ? WHERE id = ?',
|
||||
[status, errorMessage, id]
|
||||
);
|
||||
} catch (error) {
|
||||
@@ -66,7 +66,7 @@ class Analysis {
|
||||
try {
|
||||
// 更新主分析記錄
|
||||
await connection.execute(
|
||||
`UPDATE analyses
|
||||
`UPDATE 5Why_analyses
|
||||
SET problem_restatement = ?, analysis_result = ?, processing_time = ?, status = 'completed'
|
||||
WHERE id = ?`,
|
||||
[problem_restatement, JSON.stringify(analysis_result), processing_time, id]
|
||||
@@ -76,7 +76,7 @@ class Analysis {
|
||||
if (analysis_result.analyses && Array.isArray(analysis_result.analyses)) {
|
||||
for (const perspective of analysis_result.analyses) {
|
||||
const [perspectiveResult] = await connection.execute(
|
||||
`INSERT INTO analysis_perspectives
|
||||
`INSERT INTO 5Why_analysis_perspectives
|
||||
(analysis_id, perspective, perspective_icon, root_cause, permanent_solution,
|
||||
logic_check_forward, logic_check_backward, logic_valid)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
@@ -99,7 +99,7 @@ class Analysis {
|
||||
for (const why of perspective.whys) {
|
||||
if (why && why.level) {
|
||||
await connection.execute(
|
||||
`INSERT INTO analysis_whys
|
||||
`INSERT INTO 5Why_analysis_whys
|
||||
(perspective_id, level, question, answer, is_verified, verification_note)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
[
|
||||
@@ -136,8 +136,8 @@ class Analysis {
|
||||
*/
|
||||
static async getByUserId(userId, page = 1, limit = 10, filters = {}) {
|
||||
const offset = (page - 1) * limit;
|
||||
let query = 'SELECT * FROM analyses WHERE user_id = ?';
|
||||
let countQuery = 'SELECT COUNT(*) as total FROM analyses WHERE user_id = ?';
|
||||
let query = 'SELECT * FROM 5Why_analyses WHERE user_id = ?';
|
||||
let countQuery = 'SELECT COUNT(*) as total FROM 5Why_analyses WHERE user_id = ?';
|
||||
const whereParams = [userId];
|
||||
const whereClauses = [];
|
||||
|
||||
@@ -168,8 +168,9 @@ class Analysis {
|
||||
query += ' ORDER BY created_at DESC LIMIT ? OFFSET ?';
|
||||
|
||||
try {
|
||||
const [rows] = await pool.execute(query, [...whereParams, limit, offset]);
|
||||
const [countResult] = await pool.execute(countQuery, whereParams);
|
||||
// 使用 pool.query 而非 pool.execute,因為 LIMIT/OFFSET 需要數字類型
|
||||
const [rows] = await pool.query(query, [...whereParams, parseInt(limit), parseInt(offset)]);
|
||||
const [countResult] = await pool.query(countQuery, whereParams);
|
||||
|
||||
return {
|
||||
data: rows,
|
||||
@@ -192,10 +193,10 @@ class Analysis {
|
||||
const offset = (page - 1) * limit;
|
||||
let query = `
|
||||
SELECT a.*, u.username, u.employee_id
|
||||
FROM analyses a
|
||||
JOIN users u ON a.user_id = u.id
|
||||
FROM 5Why_analyses a
|
||||
JOIN 5Why_users u ON a.user_id = u.id
|
||||
`;
|
||||
let countQuery = 'SELECT COUNT(*) as total FROM analyses a JOIN users u ON a.user_id = u.id';
|
||||
let countQuery = 'SELECT COUNT(*) as total FROM 5Why_analyses a JOIN 5Why_users u ON a.user_id = u.id';
|
||||
const whereParams = [];
|
||||
const whereClauses = [];
|
||||
|
||||
@@ -223,8 +224,9 @@ class Analysis {
|
||||
query += ' ORDER BY a.created_at DESC LIMIT ? OFFSET ?';
|
||||
|
||||
try {
|
||||
const [rows] = await pool.execute(query, [...whereParams, limit, offset]);
|
||||
const [countResult] = await pool.execute(countQuery, whereParams);
|
||||
// 使用 pool.query 而非 pool.execute,因為 LIMIT/OFFSET 需要數字類型
|
||||
const [rows] = await pool.query(query, [...whereParams, parseInt(limit), parseInt(offset)]);
|
||||
const [countResult] = await pool.query(countQuery, whereParams);
|
||||
|
||||
return {
|
||||
data: rows,
|
||||
@@ -251,14 +253,14 @@ class Analysis {
|
||||
|
||||
// 取得分析角度
|
||||
const [perspectives] = await pool.execute(
|
||||
'SELECT * FROM analysis_perspectives WHERE analysis_id = ? ORDER BY id',
|
||||
'SELECT * FROM 5Why_analysis_perspectives WHERE analysis_id = ? ORDER BY id',
|
||||
[id]
|
||||
);
|
||||
|
||||
// 為每個角度取得 Whys
|
||||
for (const perspective of perspectives) {
|
||||
const [whys] = await pool.execute(
|
||||
'SELECT * FROM analysis_whys WHERE perspective_id = ? ORDER BY level',
|
||||
'SELECT * FROM 5Why_analysis_whys WHERE perspective_id = ? ORDER BY level',
|
||||
[perspective.id]
|
||||
);
|
||||
perspective.whys = whys;
|
||||
@@ -277,7 +279,7 @@ class Analysis {
|
||||
*/
|
||||
static async delete(id) {
|
||||
try {
|
||||
await pool.execute('DELETE FROM analyses WHERE id = ?', [id]);
|
||||
await pool.execute('DELETE FROM 5Why_analyses WHERE id = ?', [id]);
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw new Error(`Error deleting analysis: ${error.message}`);
|
||||
@@ -289,9 +291,10 @@ class Analysis {
|
||||
*/
|
||||
static async getRecent(limit = 100) {
|
||||
try {
|
||||
const [rows] = await pool.execute(
|
||||
'SELECT * FROM recent_analyses LIMIT ?',
|
||||
[limit]
|
||||
// 使用 pool.query 而非 pool.execute,因為 LIMIT 需要數字類型
|
||||
const [rows] = await pool.query(
|
||||
'SELECT * FROM 5Why_recent_analyses LIMIT ?',
|
||||
[parseInt(limit)]
|
||||
);
|
||||
return rows;
|
||||
} catch (error) {
|
||||
@@ -312,7 +315,7 @@ class Analysis {
|
||||
COUNT(CASE WHEN status = 'processing' THEN 1 END) as processing,
|
||||
AVG(processing_time) as avg_processing_time,
|
||||
MAX(created_at) as last_analysis_at
|
||||
FROM analyses
|
||||
FROM 5Why_analyses
|
||||
`;
|
||||
|
||||
const params = [];
|
||||
|
||||
Reference in New Issue
Block a user