This repository has been archived on 2024-12-02. You can view files and clone it, but cannot push or open issues or pull requests.
magnetico-go-migrator/magnetico-migrator.go

89 lines
2.1 KiB
Go

package main
import (
"database/sql"
"fmt"
"github.com/boramalper/magnetico/pkg/persistence"
_ "github.com/mattn/go-sqlite3"
"go.uber.org/zap"
"gopkg.in/alecthomas/kingpin.v2"
"net/url"
)
var (
goDatabaseUrl = kingpin.Flag("go-database", "magneticod Go version database URL.").Short('g').String()
pyDatabaseUrl = kingpin.Flag("py-database", "Python version database URL.").Short('p').Required().String()
)
func main() {
logger, _ := zap.NewDevelopment()
defer logger.Sync()
zap.ReplaceGlobals(logger)
kingpin.Parse()
goDatabase, err := persistence.MakeDatabase(*goDatabaseUrl, logger)
if err != nil {
logger.Panic("Could not open Go database.", zap.String("url", *goDatabaseUrl), zap.Error(err))
}
defer goDatabase.Close()
oldSqliteUrl, err := url.Parse(*pyDatabaseUrl)
if err != nil {
logger.Panic("Can't parse Python database URL.", zap.String("url", *pyDatabaseUrl), zap.Error(err))
}
pyDatabase, err := openSqliteDb(*oldSqliteUrl)
if err != nil {
logger.Panic("Couldn't open Python database.", zap.String("url", *pyDatabaseUrl), zap.Error(err))
}
defer pyDatabase.Close()
err = process(*pyDatabase, goDatabase)
if err != nil {
logger.Error("Error while processing data", zap.Error(err))
}
}
func process(pyDb sql.DB, goDb persistence.Database) error {
//torrentsTotal, err := getNumberOfTorrents(pyDb)
//if err != nil {
// zap.L().Panic("Couldn't count torrents", zap.Error(err))
//}
//
//bar := pb.StartNew(int(torrentsTotal))
// TODO
return nil
}
func getNumberOfTorrents(db sql.DB) (uint, error) {
rows, err := db.Query("SELECT COUNT(id) FROM torrents;")
if err != nil {
return 0, err
}
defer rows.Close()
if rows.Next() != true {
return 0, fmt.Errorf("No rows returned from `SELECT COUNT(id)`")
}
var n *uint
if err = rows.Scan(&n); err != nil {
return 0, err
}
// If the database is empty (i.e. 0 entries in 'torrents') then the query will return nil.
if n == nil {
return 0, nil
} else {
return *n, nil
}
}
func openSqliteDb(url_ url.URL) (*sql.DB, error) {
url_.Scheme = ""
return sql.Open("sqlite3", url_.String())
}