init.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const sqlite3 = require('sqlite3').verbose();
  2. const path = require('path');
  3. const dbPath = path.join(__dirname, 'maa_test.db');
  4. // 建立資料庫連線
  5. const db = new sqlite3.Database(dbPath, (err) => {
  6. if (err) {
  7. console.error('❌ 資料庫連線失敗:', err.message);
  8. } else {
  9. console.log('✅ 已連接到SQLite資料庫');
  10. }
  11. });
  12. // 初始化資料庫表格
  13. const initDatabase = () => {
  14. return new Promise((resolve, reject) => {
  15. db.serialize(() => {
  16. // 建立專案表格
  17. db.run(`
  18. CREATE TABLE IF NOT EXISTS projects (
  19. id INTEGER PRIMARY KEY AUTOINCREMENT,
  20. name TEXT NOT NULL,
  21. description TEXT,
  22. status TEXT DEFAULT 'active',
  23. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  24. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
  25. )
  26. `);
  27. // 建立問題表格
  28. db.run(`
  29. CREATE TABLE IF NOT EXISTS issues (
  30. id INTEGER PRIMARY KEY AUTOINCREMENT,
  31. project_id INTEGER,
  32. title TEXT NOT NULL,
  33. description TEXT,
  34. status TEXT DEFAULT 'open',
  35. priority TEXT DEFAULT 'medium',
  36. assignee TEXT,
  37. reporter TEXT,
  38. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  39. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  40. FOREIGN KEY (project_id) REFERENCES projects (id)
  41. )
  42. `);
  43. // 建立問題評論表格
  44. db.run(`
  45. CREATE TABLE IF NOT EXISTS issue_comments (
  46. id INTEGER PRIMARY KEY AUTOINCREMENT,
  47. issue_id INTEGER,
  48. author TEXT NOT NULL,
  49. content TEXT NOT NULL,
  50. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  51. FOREIGN KEY (issue_id) REFERENCES issues (id)
  52. )
  53. `);
  54. // 插入範例資料
  55. db.run(`
  56. INSERT OR IGNORE INTO projects (id, name, description, status) VALUES
  57. (1, 'MAA核心功能', 'MAA自動化測試核心功能開發', 'active'),
  58. (2, 'UI介面優化', '使用者介面改進和優化', 'active'),
  59. (3, '效能測試', '系統效能測試和優化', 'planning')
  60. `);
  61. db.run(`
  62. INSERT OR IGNORE INTO issues (id, project_id, title, description, status, priority, assignee, reporter) VALUES
  63. (1, 1, '登入功能異常', '使用者無法正常登入系統', 'open', 'high', 'developer1', 'tester1'),
  64. (2, 1, '資料同步問題', '資料同步時出現延遲', 'in_progress', 'medium', 'developer2', 'tester2'),
  65. (3, 2, '按鈕樣式調整', '需要調整按鈕的顏色和大小', 'open', 'low', 'designer1', 'product1'),
  66. (4, 2, '響應式設計', '手機版介面需要優化', 'open', 'medium', 'designer2', 'product2'),
  67. (5, 3, '載入速度優化', '頁面載入速度過慢', 'closed', 'high', 'developer3', 'tester3')
  68. `);
  69. db.run(`
  70. INSERT OR IGNORE INTO issue_comments (issue_id, author, content) VALUES
  71. (1, 'developer1', '已確認問題,正在修復中'),
  72. (2, 'developer2', '問題已定位,預計明天完成修復'),
  73. (3, 'designer1', '設計稿已完成,等待開發實作'),
  74. (4, 'designer2', '需要更多時間進行響應式設計'),
  75. (5, 'developer3', '已優化完成,效能提升50%')
  76. `, (err) => {
  77. if (err) {
  78. console.error('❌ 範例資料插入失敗:', err);
  79. reject(err);
  80. } else {
  81. console.log('✅ 資料庫初始化完成');
  82. resolve();
  83. }
  84. });
  85. });
  86. });
  87. };
  88. module.exports = { db, initDatabase };