feat: migrate database from SQLite to MySQL with .env configuration

- Replace go-sqlite3 with go-sql-driver/mysql (pure Go, no CGO needed)
- Add joho/godotenv for .env file loading
- Rewrite database/db.go with MySQL-compatible DDL (BIGINT AUTO_INCREMENT, InnoDB, utf8mb4)
- Convert all SQLite-specific SQL: INSERT OR IGNORE → INSERT IGNORE,
  INSERT OR REPLACE → INSERT ... ON DUPLICATE KEY UPDATE,
  datetime('now') → NOW(), date arithmetic → DATE_SUB()
- Add MySQL config fields to config.go (host/port/user/password/database)
- Add .env.example with connection parameter template
- Update Dockerfile: remove CGO/gcc/musl dependency, smaller build
- Update docker-compose.yml: add MySQL service container with healthcheck
- Update CLAUDE.md documentation
This commit is contained in:
2026-06-20 22:40:46 +08:00
parent 6c60b767df
commit 00c1839635
14 changed files with 130 additions and 102 deletions
+1 -1
View File
@@ -126,7 +126,7 @@ func (h *AuthHandler) HandleRegister(c *gin.Context) {
// Seed default settings for the user
for key, val := range defaultUserSettings {
h.db.Exec(`INSERT OR IGNORE INTO user_settings (user_id, key, value) VALUES (?, ?, ?)`, userID, key, val)
h.db.Exec(`INSERT IGNORE INTO user_settings (user_id, key, value) VALUES (?, ?, ?)`, userID, key, val)
}
// Auto-login: set session
+1 -1
View File
@@ -73,7 +73,7 @@ func (h *GenerateHandler) Generate(c *gin.Context) {
}
// Update last_used
h.db.Exec(`UPDATE repositories SET last_used = datetime('now') WHERE id = ?`, id)
h.db.Exec(`UPDATE repositories SET last_used = NOW() WHERE id = ?`, id)
// Generate PR description (pass user ID for per-user LLM config)
pr, err := services.GeneratePR(h.db, localPath, req.Base, req.Head, user.ID, sendEvent)
+3 -3
View File
@@ -104,7 +104,7 @@ func (h *ReposHandler) CleanupRepos(c *gin.Context) {
if maxAgeDays == "" {
maxAgeDays = "7"
}
rows, err := h.db.Query(`SELECT id, local_path FROM repositories WHERE user_id = ? AND last_used < datetime('now', '-' || ? || ' days')`, user.ID, maxAgeDays)
rows, err := h.db.Query(`SELECT id, local_path FROM repositories WHERE user_id = ? AND last_used < DATE_SUB(NOW(), INTERVAL ? DAY)`, user.ID, maxAgeDays)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
@@ -244,7 +244,7 @@ func (h *ReposHandler) GetGraph(c *gin.Context) {
}
// Update last_used
h.db.Exec(`UPDATE repositories SET last_used = datetime('now') WHERE id = ?`, id)
h.db.Exec(`UPDATE repositories SET last_used = NOW() WHERE id = ?`, id)
repo, err := services.OpenRepo(localPath)
if err != nil {
@@ -297,7 +297,7 @@ func (h *ReposHandler) GetDiff(c *gin.Context) {
}
// Update last_used
h.db.Exec(`UPDATE repositories SET last_used = datetime('now') WHERE id = ?`, id)
h.db.Exec(`UPDATE repositories SET last_used = NOW() WHERE id = ?`, id)
repo, err := services.OpenRepo(localPath)
if err != nil {
+1 -1
View File
@@ -105,7 +105,7 @@ func (h *ReviewHandler) Review(c *gin.Context) {
}
// Update last_used
h.db.Exec(`UPDATE repositories SET last_used = datetime('now') WHERE id = ?`, id)
h.db.Exec(`UPDATE repositories SET last_used = NOW() WHERE id = ?`, id)
// Run AI review (pass user ID for per-user LLM config)
reviewResult, err := services.GenerateReview(h.db, localPath, req.Base, req.Head, topN, concurrency, user.ID, sendEvent)
+1 -1
View File
@@ -56,7 +56,7 @@ func (h *SettingsHandler) UpdateSettings(c *gin.Context) {
return
}
for key, val := range body {
_, err := tx.Exec(`INSERT OR REPLACE INTO user_settings (user_id, key, value) VALUES (?, ?, ?)`, user.ID, key, val)
_, err := tx.Exec(`INSERT INTO user_settings (user_id, key, value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE value = VALUES(value)`, user.ID, key, val)
if err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})