feat: Adding migration tool for mariadb to mongodb
This commit is contained in:
parent
e12b0416d4
commit
44ebfe4fd2
4 changed files with 174 additions and 1 deletions
|
@ -7,7 +7,7 @@ import (
|
||||||
"git.wh64.net/muffin/goMuffin/utils"
|
"git.wh64.net/muffin/goMuffin/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MUFFIN_VERSION = "5.0.0-gopher_preview.250407a"
|
const MUFFIN_VERSION = "5.0.0-gopher_preview.250408a"
|
||||||
|
|
||||||
var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]
|
var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]
|
||||||
|
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -5,11 +5,13 @@ go 1.24.1
|
||||||
require (
|
require (
|
||||||
github.com/LoperLee/golang-hangul-toolkit v1.1.0
|
github.com/LoperLee/golang-hangul-toolkit v1.1.0
|
||||||
github.com/bwmarrin/discordgo v0.28.1
|
github.com/bwmarrin/discordgo v0.28.1
|
||||||
|
github.com/go-sql-driver/mysql v1.9.2
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
go.mongodb.org/mongo-driver/v2 v2.1.0
|
go.mongodb.org/mongo-driver/v2 v2.1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
github.com/golang/snappy v1.0.0 // indirect
|
github.com/golang/snappy v1.0.0 // indirect
|
||||||
github.com/gorilla/websocket v1.5.3 // indirect
|
github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
github.com/klauspost/compress v1.18.0 // indirect
|
github.com/klauspost/compress v1.18.0 // indirect
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -1,9 +1,13 @@
|
||||||
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
github.com/LoperLee/golang-hangul-toolkit v1.1.0 h1:JEyLpLyA2hDQwWY9oCprHClnKIdkYVOSJzAat2uFX/A=
|
github.com/LoperLee/golang-hangul-toolkit v1.1.0 h1:JEyLpLyA2hDQwWY9oCprHClnKIdkYVOSJzAat2uFX/A=
|
||||||
github.com/LoperLee/golang-hangul-toolkit v1.1.0/go.mod h1:CDbZ23/IL4v2ovWIOb7xDEiFcSc0pIIbbYTpg+gP+Sk=
|
github.com/LoperLee/golang-hangul-toolkit v1.1.0/go.mod h1:CDbZ23/IL4v2ovWIOb7xDEiFcSc0pIIbbYTpg+gP+Sk=
|
||||||
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
|
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
|
||||||
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/go-sql-driver/mysql v1.9.2 h1:4cNKDYQ1I84SXslGddlsrMhc8k4LeDVj6Ad6WRjiHuU=
|
||||||
|
github.com/go-sql-driver/mysql v1.9.2/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
|
||||||
github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs=
|
github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs=
|
||||||
github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
|
167
scripts/dbMigrate.go
Normal file
167
scripts/dbMigrate.go
Normal file
|
@ -0,0 +1,167 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.wh64.net/muffin/goMuffin/configs"
|
||||||
|
"git.wh64.net/muffin/goMuffin/databases"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
|
)
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
// 이 스크립트는 MariaDB -> MongoDB로의 전환을 위해 만들었음.
|
||||||
|
func main() {
|
||||||
|
mariaURL := os.Getenv("PREVIOUS_DATABASE_URL")
|
||||||
|
mongoURL := configs.Config.DatabaseURL
|
||||||
|
|
||||||
|
dbConnectionQuery := "?parseTime=true"
|
||||||
|
|
||||||
|
wg.Add(3)
|
||||||
|
|
||||||
|
// statement -> text
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
var text, persona string
|
||||||
|
var createdAt time.Time
|
||||||
|
mariaDB, err := sql.Open("mysql", mariaURL+dbConnectionQuery)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mongoDB, err := mongo.Connect(options.Client().ApplyURI(mongoURL))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer mongoDB.Disconnect(context.TODO())
|
||||||
|
defer mariaDB.Close()
|
||||||
|
rows, err := mariaDB.Query("select text, persona, created_at from statement;")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
i := 1
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
fmt.Printf("statement %d\n", i)
|
||||||
|
err = rows.Scan(&text, &persona, &createdAt)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if text == "" {
|
||||||
|
text = "살ㄹ려주세요"
|
||||||
|
}
|
||||||
|
databases.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||||
|
Text: text,
|
||||||
|
Persona: persona,
|
||||||
|
CreatedAt: createdAt,
|
||||||
|
})
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// nsfw_content -> text
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
var text, persona string
|
||||||
|
var createdAt time.Time
|
||||||
|
mariaDB, err := sql.Open("mysql", mariaURL+dbConnectionQuery)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mongoDB, err := mongo.Connect(options.Client().ApplyURI(mongoURL))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer mongoDB.Disconnect(context.TODO())
|
||||||
|
defer mariaDB.Close()
|
||||||
|
rows, err := mariaDB.Query("select text, persona, created_at from nsfw_content;")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
i := 1
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
fmt.Printf("nsfw_content %d\n", i)
|
||||||
|
err = rows.Scan(&text, &persona, &createdAt)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if text == "" {
|
||||||
|
text = "살ㄹ려주세요"
|
||||||
|
}
|
||||||
|
databases.Texts.InsertOne(context.TODO(), databases.InsertText{
|
||||||
|
Text: text,
|
||||||
|
Persona: persona,
|
||||||
|
CreatedAt: createdAt,
|
||||||
|
})
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// learn -> learn
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
var command, result, userId string
|
||||||
|
var createdAt time.Time
|
||||||
|
mariaDB, err := sql.Open("mysql", mariaURL+dbConnectionQuery)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mongoDB, err := mongo.Connect(options.Client().ApplyURI(mongoURL))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer mongoDB.Disconnect(context.TODO())
|
||||||
|
defer mariaDB.Close()
|
||||||
|
rows, err := mariaDB.Query("select command, result, user_id, created_at from learn;")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
i := 1
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
fmt.Printf("learn %d\n", i)
|
||||||
|
err = rows.Scan(&command, &result, &userId, &createdAt)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
databases.Learns.InsertOne(context.TODO(), databases.InsertLearn{
|
||||||
|
Command: command,
|
||||||
|
Result: result,
|
||||||
|
UserId: userId,
|
||||||
|
CreatedAt: createdAt,
|
||||||
|
})
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 모든 고루틴이 끝날 떄 까지 대기
|
||||||
|
wg.Wait()
|
||||||
|
}
|
Loading…
Reference in a new issue