Functions for old database are moved to the separate package 'old'.

This commit is contained in:
Alexey Skobkin 2019-10-19 02:07:10 +03:00
parent a7b2344fce
commit 90d8005c9a
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
2 changed files with 64 additions and 56 deletions

View file

@ -3,7 +3,7 @@ package main
import (
"database/sql"
"encoding/hex"
"fmt"
"gitlab.com/skobkin/magnetico-go-migrator/old"
"github.com/cheggaaa/pb/v3"
_ "github.com/mattn/go-sqlite3"
@ -39,13 +39,13 @@ func main() {
logger.Panic("Can't parse Python database URL.", zap.String("url", *pyDatabaseUrl), zap.Error(err))
}
pyDatabase, err := openSqliteDb(*oldSqliteUrl)
pyDatabase, err := old.OpenSqliteDb(*oldSqliteUrl)
if err != nil {
logger.Panic("Couldn't open Python database.", zap.String("url", *pyDatabaseUrl), zap.Error(err))
}
defer pyDatabase.Close()
torrentsTotal, err := getNumberOfTorrents(*pyDatabase)
torrentsTotal, err := old.GetNumberOfTorrents(*pyDatabase)
if err != nil {
zap.L().Panic("Couldn't count torrents", zap.Error(err))
}
@ -82,7 +82,7 @@ func mergeDatabases(pyDb sql.DB, goDb persistence.Database) error {
continue
}
files, err := getFilesForTorrent(pyDb, torrent)
files, err := old.GetFilesForTorrent(pyDb, torrent)
if err != nil {
zap.L().Error("Error getting files for torrent.", zap.String("hash", hex.EncodeToString(torrent.InfoHash)), zap.Error(err))
@ -113,55 +113,3 @@ func importTorrent(goDb persistence.Database, torrent persistence.TorrentMetadat
return nil
}
func getFilesForTorrent(pyDb sql.DB, torrent persistence.TorrentMetadata) ([]persistence.File, error) {
files := make([]persistence.File, 0)
// Selecting files for torrent
fRows, err := pyDb.Query("SELECT f.path, f.size FROM files AS f WHERE f.torrent_id = ? ORDER BY f.id ASC;", torrent.ID)
if err != nil {
return files, nil
}
defer fRows.Close()
for fRows.Next() {
var file persistence.File
err := fRows.Scan(&file.Path, &file.Size)
if err != nil {
return files, err
}
files = append(files, file)
}
return files, 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())
}

60
old/sqlite.go Normal file
View file

@ -0,0 +1,60 @@
package old
import (
"database/sql"
"fmt"
"github.com/skobkin/magnetico/pkg/persistence"
"net/url"
)
func OpenSqliteDb(url_ url.URL) (*sql.DB, error) {
url_.Scheme = ""
return sql.Open("sqlite3", url_.String())
}
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 GetFilesForTorrent(pyDb sql.DB, torrent persistence.TorrentMetadata) ([]persistence.File, error) {
files := make([]persistence.File, 0)
// Selecting files for torrent
fRows, err := pyDb.Query("SELECT f.path, f.size FROM files AS f WHERE f.torrent_id = ? ORDER BY f.id ASC;", torrent.ID)
if err != nil {
return files, nil
}
defer fRows.Close()
for fRows.Next() {
var file persistence.File
err := fRows.Scan(&file.Path, &file.Size)
if err != nil {
return files, err
}
files = append(files, file)
}
return files, nil
}