chore: Enhancing migration speed

This commit is contained in:
Siwoo Jeon 2025-04-13 11:10:19 +09:00
parent 128a0a02a4
commit 98f664d08c
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA

View file

@ -9,8 +9,9 @@ import (
"time" "time"
"git.wh64.net/muffin/goMuffin/configs" "git.wh64.net/muffin/goMuffin/configs"
"git.wh64.net/muffin/goMuffin/databases"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/mongo/options"
) )
@ -21,6 +22,7 @@ var wg sync.WaitGroup
func DBMigrate() { func DBMigrate() {
mariaURL := os.Getenv("PREVIOUS_DATABASE_URL") mariaURL := os.Getenv("PREVIOUS_DATABASE_URL")
mongoURL := configs.Config.DatabaseURL mongoURL := configs.Config.DatabaseURL
dbName := configs.Config.DBName
dbConnectionQuery := "?parseTime=true" dbConnectionQuery := "?parseTime=true"
@ -30,8 +32,7 @@ func DBMigrate() {
go func() { go func() {
defer wg.Done() defer wg.Done()
var text, persona string newDataList := []any{}
var createdAt time.Time
mariaDB, err := sql.Open("mysql", mariaURL+dbConnectionQuery) mariaDB, err := sql.Open("mysql", mariaURL+dbConnectionQuery)
if err != nil { if err != nil {
panic(err) panic(err)
@ -54,6 +55,9 @@ func DBMigrate() {
i := 1 i := 1
for rows.Next() { for rows.Next() {
var text, persona string
var createdAt time.Time
fmt.Printf("statement %d\n", i) fmt.Printf("statement %d\n", i)
err = rows.Scan(&text, &persona, &createdAt) err = rows.Scan(&text, &persona, &createdAt)
if err != nil { if err != nil {
@ -63,21 +67,25 @@ func DBMigrate() {
if text == "" { if text == "" {
text = "살ㄹ려주세요" text = "살ㄹ려주세요"
} }
databases.Texts.InsertOne(context.TODO(), databases.InsertText{
Text: text, newDataList = append(newDataList, bson.M{
Persona: persona, "text": text,
CreatedAt: createdAt, "persona": persona,
"created_at": createdAt,
}) })
i++ i++
} }
_, err = mongoDB.Database(dbName).Collection("text").InsertMany(context.TODO(), newDataList)
if err != nil {
panic(err)
}
}() }()
// nsfw_content -> text // nsfw_content -> text
go func() { go func() {
defer wg.Done() defer wg.Done()
var text, persona string newDataList := []any{}
var createdAt time.Time
mariaDB, err := sql.Open("mysql", mariaURL+dbConnectionQuery) mariaDB, err := sql.Open("mysql", mariaURL+dbConnectionQuery)
if err != nil { if err != nil {
panic(err) panic(err)
@ -100,6 +108,9 @@ func DBMigrate() {
i := 1 i := 1
for rows.Next() { for rows.Next() {
var text, persona string
var createdAt time.Time
fmt.Printf("nsfw_content %d\n", i) fmt.Printf("nsfw_content %d\n", i)
err = rows.Scan(&text, &persona, &createdAt) err = rows.Scan(&text, &persona, &createdAt)
if err != nil { if err != nil {
@ -109,21 +120,26 @@ func DBMigrate() {
if text == "" { if text == "" {
text = "살ㄹ려주세요" text = "살ㄹ려주세요"
} }
databases.Texts.InsertOne(context.TODO(), databases.InsertText{
Text: text, newDataList = append(newDataList, bson.M{
Persona: persona, "text": text,
CreatedAt: createdAt, "persona": persona,
"created_at": createdAt,
}) })
i++ i++
} }
_, err = mongoDB.Database(dbName).Collection("text").InsertMany(context.TODO(), newDataList)
if err != nil {
panic(err)
}
}() }()
// learn -> learn // learn -> learn
go func() { go func() {
defer wg.Done() defer wg.Done()
var command, result, userId string newDataList := []any{}
var createdAt time.Time
mariaDB, err := sql.Open("mysql", mariaURL+dbConnectionQuery) mariaDB, err := sql.Open("mysql", mariaURL+dbConnectionQuery)
if err != nil { if err != nil {
panic(err) panic(err)
@ -146,22 +162,31 @@ func DBMigrate() {
i := 1 i := 1
for rows.Next() { for rows.Next() {
var command, result, userId string
var createdAt time.Time
fmt.Printf("learn %d\n", i) fmt.Printf("learn %d\n", i)
err = rows.Scan(&command, &result, &userId, &createdAt) err = rows.Scan(&command, &result, &userId, &createdAt)
if err != nil { if err != nil {
panic(err) panic(err)
} }
databases.Learns.InsertOne(context.TODO(), databases.InsertLearn{ newDataList = append(newDataList, bson.M{
Command: command, "command": command,
Result: result, "result": result,
UserId: userId, "user_id": userId,
CreatedAt: createdAt, "created_at": createdAt,
}) })
i++ i++
} }
_, err = mongoDB.Database(dbName).Collection("learn").InsertMany(context.TODO(), newDataList)
if err != nil {
panic(err)
}
}() }()
// 모든 고루틴이 끝날 떄 까지 대기 // 모든 고루틴이 끝날 떄 까지 대기
wg.Wait() wg.Wait()
fmt.Println("데이터 마이그레이션이 끝났어요.")
} }