mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
sqlitemigrate patches for larger datasets
This commit is contained in:
parent
8b9a9a1a42
commit
030181fb2f
@ -4,10 +4,11 @@ package main
|
|||||||
Usage:
|
Usage:
|
||||||
---
|
---
|
||||||
1. Set up a MySQL instance (see docker_mysql.sh for example)
|
1. Set up a MySQL instance (see docker_mysql.sh for example)
|
||||||
2. Create config file (e.g. migrate.yml) as shown below
|
2. Create config file (e.g. config.yml) as shown below
|
||||||
3. go run sqlite2mysql.go -config migrate.yml
|
3. go run sqlite2mysql.go -config config.yml
|
||||||
|
4. For postgres check https://wiki.postgresql.org/wiki/Fixing_Sequences
|
||||||
|
|
||||||
Example: migrate.yml
|
Example: config.yml
|
||||||
---
|
---
|
||||||
# SQLite
|
# SQLite
|
||||||
source:
|
source:
|
||||||
@ -21,19 +22,22 @@ target:
|
|||||||
user:
|
user:
|
||||||
password:
|
password:
|
||||||
name:
|
name:
|
||||||
|
dialect:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/jinzhu/configor"
|
"github.com/jinzhu/configor"
|
||||||
"github.com/muety/wakapi/models"
|
"github.com/muety/wakapi/models"
|
||||||
"github.com/muety/wakapi/repositories"
|
"github.com/muety/wakapi/repositories"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
@ -47,6 +51,7 @@ type dbConfig struct {
|
|||||||
User string
|
User string
|
||||||
Password string
|
Password string
|
||||||
Name string
|
Name string
|
||||||
|
Dialect string `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var cfg *config
|
var cfg *config
|
||||||
@ -75,22 +80,37 @@ func init() {
|
|||||||
dbSource = db
|
dbSource = db
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("attempting to open mysql database as Target")
|
if cfg.Target.Dialect == "postgres" {
|
||||||
if db, err := gorm.Open(mysql.New(mysql.Config{
|
log.Println("attempting to open postgresql database as Target")
|
||||||
DriverName: "mysql",
|
if db, err := gorm.Open(postgres.Open(fmt.Sprintf("user=%s password=%s host=%s port=%d dbname=%s sslmode=disable timezone=Europe/Berlin",
|
||||||
DSN: fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES",
|
|
||||||
cfg.Target.User,
|
cfg.Target.User,
|
||||||
cfg.Target.Password,
|
cfg.Target.Password,
|
||||||
cfg.Target.Host,
|
cfg.Target.Host,
|
||||||
cfg.Target.Port,
|
cfg.Target.Port,
|
||||||
cfg.Target.Name,
|
cfg.Target.Name,
|
||||||
"utf8mb4",
|
)), &gorm.Config{}); err != nil {
|
||||||
"Local",
|
log.Fatalln(err)
|
||||||
),
|
} else {
|
||||||
}), &gorm.Config{}); err != nil {
|
dbTarget = db
|
||||||
log.Fatalln(err)
|
}
|
||||||
} else {
|
} else {
|
||||||
dbTarget = db
|
log.Println("attempting to open mysql database as Target")
|
||||||
|
if db, err := gorm.Open(mysql.New(mysql.Config{
|
||||||
|
DriverName: "mysql",
|
||||||
|
DSN: fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES",
|
||||||
|
cfg.Target.User,
|
||||||
|
cfg.Target.Password,
|
||||||
|
cfg.Target.Host,
|
||||||
|
cfg.Target.Port,
|
||||||
|
cfg.Target.Name,
|
||||||
|
"utf8mb4",
|
||||||
|
"Local",
|
||||||
|
),
|
||||||
|
}), &gorm.Config{}); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
} else {
|
||||||
|
dbTarget = db
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,7 +211,25 @@ func main() {
|
|||||||
log.Println("Migrating heartbeats ...")
|
log.Println("Migrating heartbeats ...")
|
||||||
if data, err := heartbeatSource.GetAll(); err == nil {
|
if data, err := heartbeatSource.GetAll(); err == nil {
|
||||||
log.Printf("Got %d heartbeats loaded into memory. Batch-inserting them now ...\n", len(data))
|
log.Printf("Got %d heartbeats loaded into memory. Batch-inserting them now ...\n", len(data))
|
||||||
if err := heartbeatTarget.InsertBatch(data); err != nil {
|
|
||||||
|
var slice = make([]*models.Heartbeat, len(data))
|
||||||
|
for i, heartbeat := range data {
|
||||||
|
heartbeat = heartbeat.Hashed()
|
||||||
|
log.Println(heartbeat.String())
|
||||||
|
slice[i] = heartbeat
|
||||||
|
}
|
||||||
|
left := 0
|
||||||
|
right := 100
|
||||||
|
size := len(slice)
|
||||||
|
for right < size {
|
||||||
|
log.Printf("Inserting batch from %d", left)
|
||||||
|
if err := heartbeatTarget.InsertBatch(slice[left:right]); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
left += 100
|
||||||
|
right += 100
|
||||||
|
}
|
||||||
|
if err := heartbeatTarget.InsertBatch(slice[left:]); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user