2024-04-21 12:22:35 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
2024-04-21 13:29:35 +08:00
|
|
|
"errors"
|
2024-04-21 12:22:35 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2024-04-21 13:29:35 +08:00
|
|
|
// 这个工具用来修复旧数据库没有 message_seq 字段的问题
|
2024-04-21 12:22:35 +08:00
|
|
|
func main() {
|
|
|
|
// Open SQLite database
|
2024-04-21 13:29:35 +08:00
|
|
|
db, err := sql.Open("sqlite3", "../data.db")
|
2024-04-21 12:22:35 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Create messages table if not exists
|
|
|
|
_, err = db.Exec(`
|
|
|
|
CREATE TABLE IF NOT EXISTS messages (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
message_id INTEGER UNIQUE NOT NULL,
|
|
|
|
message_seq INTEGER
|
|
|
|
)
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch messageIDs from database
|
2024-04-21 13:29:35 +08:00
|
|
|
rows, err := db.Query("SELECT message_id FROM messages WHERE message_seq IS NULL")
|
2024-04-21 12:22:35 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2024-04-21 13:29:35 +08:00
|
|
|
var messageIDs []int
|
2024-04-21 12:22:35 +08:00
|
|
|
for rows.Next() {
|
|
|
|
var messageID int
|
|
|
|
err := rows.Scan(&messageID)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-04-21 13:29:35 +08:00
|
|
|
messageIDs = append(messageIDs, messageID)
|
2024-04-21 12:22:35 +08:00
|
|
|
|
2024-04-21 13:29:35 +08:00
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, messageID := range messageIDs {
|
2024-04-21 12:22:35 +08:00
|
|
|
messageSeq, err := fetchMessageSeq(messageID)
|
|
|
|
if err != nil {
|
2024-04-21 13:29:35 +08:00
|
|
|
log.Fatal(err)
|
2024-04-21 12:22:35 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = insertOrUpdateMessageSeq(db, messageID, messageSeq)
|
|
|
|
if err != nil {
|
2024-04-21 13:29:35 +08:00
|
|
|
log.Fatal(err)
|
2024-04-21 12:22:35 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("MessageSeq for message_id %d is %d\n", messageID, messageSeq)
|
|
|
|
|
|
|
|
}
|
2024-04-21 13:29:35 +08:00
|
|
|
|
2024-04-21 12:22:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func fetchMessageSeq(messageID int) (int, error) {
|
|
|
|
url := fmt.Sprintf("http://47.92.132.159:5700/get_msg?message_id=%d", messageID)
|
2024-04-21 13:29:35 +08:00
|
|
|
fmt.Println("URL:", url)
|
2024-04-21 12:22:35 +08:00
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-04-21 13:29:35 +08:00
|
|
|
println("Body:", string(body))
|
2024-04-21 12:22:35 +08:00
|
|
|
|
|
|
|
var messageResp map[string]interface{}
|
|
|
|
err = json.Unmarshal(body, &messageResp)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-04-21 13:29:35 +08:00
|
|
|
data, ok := messageResp["data"].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return 0, errors.New("data is not a map")
|
|
|
|
}
|
|
|
|
message_seq := int(data["message_seq"].(float64))
|
|
|
|
// println(message_seq)
|
|
|
|
return message_seq, nil
|
2024-04-21 12:22:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func insertOrUpdateMessageSeq(db *sql.DB, messageID, messageSeq int) error {
|
|
|
|
// Try to insert messageSeq
|
|
|
|
// _, err := db.Exec("INSERT INTO messages (message_id, message_seq) VALUES (?, ?)", messageID, messageSeq)
|
|
|
|
// if err != nil {
|
|
|
|
// If messageID already exists, update messageSeq
|
|
|
|
_, err := db.Exec("UPDATE messages SET message_seq = ? WHERE message_id = ?", messageSeq, messageID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|