Added a new sync that uses hash

Added a new sync that uses hash and sqlc for the queries. Added db
migration. Started adding a config file.
This commit is contained in:
2024-12-19 12:11:20 +01:00
parent 8fa93d580d
commit 5ab19e16e5
26 changed files with 1527 additions and 45 deletions

View File

@@ -2,14 +2,21 @@ package db
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database/postgres"
_ "github.com/golang-migrate/migrate/database/postgres"
_ "github.com/golang-migrate/migrate/source/file"
"github.com/jackc/pgx/v5/pgxpool"
_ "github.com/lib/pq"
)
var dbpool *pgxpool.Pool
var ctx = context.Background()
var Dbpool *pgxpool.Pool
var Ctx = context.Background()
func InitDB(host string, port int, user string, password string, dbname string) {
@@ -20,14 +27,14 @@ func InitDB(host string, port int, user string, password string, dbname string)
fmt.Println(psqlInfo)
var err error
dbpool, err = pgxpool.New(ctx, psqlInfo)
Dbpool, err = pgxpool.New(Ctx, psqlInfo)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
var success string
err = dbpool.QueryRow(ctx, "select 'Successfully connected!'").Scan(&success)
err = Dbpool.QueryRow(Ctx, "select 'Successfully connected!'").Scan(&success)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
@@ -37,11 +44,11 @@ func InitDB(host string, port int, user string, password string, dbname string)
}
func CloseDb() {
dbpool.Close()
Dbpool.Close()
}
func Testf() {
rows, dbErr := dbpool.Query(ctx, "select game_name from game")
rows, dbErr := Dbpool.Query(Ctx, "select game_name from game")
if dbErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr)
os.Exit(1)
@@ -57,8 +64,60 @@ func Testf() {
}
func resetGameIdSeq() {
_, err := dbpool.Query(ctx, "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);")
_, err := Dbpool.Query(Ctx, "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
}
func Migrate_db(host string, port int, user string, password string, dbname string) {
migrationInfo := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=disable",
user, password, host, port, dbname)
fmt.Println("Migration Info: ", migrationInfo)
db, err := sql.Open("postgres", migrationInfo)
if err != nil {
log.Println(err)
}
driver, err := postgres.WithInstance(db, &postgres.Config{})
if err != nil {
log.Println(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./db/migrations/",
"postgres", driver)
if err != nil {
log.Println(err)
}
version, _, err := m.Version()
if err != nil {
log.Println("Migration version err: ", err)
}
fmt.Println("Migration version before: ", version)
err = m.Force(1)
//err = m.Up() // or m.Steps(2) if you want to explicitly set the number of migrations to run
if err != nil {
log.Println("Force err: ", err)
}
err = m.Migrate(2)
//err = m.Up() // or m.Steps(2) if you want to explicitly set the number of migrations to run
if err != nil {
log.Println("Migration err: ", err)
}
versionAfter, _, err := m.Version()
if err != nil {
log.Println("Migration version err: ", err)
}
fmt.Println("Migration version after: ", versionAfter)
fmt.Println("Migration done")
db.Close()
}

View File

@@ -12,7 +12,7 @@ import (
func GetGameName(gameId int) string {
var gameName = ""
err := dbpool.QueryRow(ctx,
err := Dbpool.QueryRow(Ctx,
"SELECT game_name FROM game WHERE id = $1", gameId).Scan(&gameName)
if err != nil {
if compareError.Error() != err.Error() {
@@ -28,7 +28,7 @@ func GetGameById(gameId int) (models.GameData, error) {
var numberOfSongs pgtype.Int4
var gameName, path string
var added, deleted, lastChanged, lastPlayed pgtype.Timestamp
err := dbpool.QueryRow(ctx,
err := Dbpool.QueryRow(Ctx,
"SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs "+
"FROM game WHERE id = $1 AND deleted IS NULL ", gameId).Scan(&id, &gameName, &added, &deleted, &lastChanged, &path, &timesPlayed, &lastPlayed, &numberOfSongs)
if err != nil {
@@ -51,7 +51,7 @@ func GetGameById(gameId int) (models.GameData, error) {
}
func SetGameDeletionDate() {
_, err := dbpool.Exec(ctx,
_, err := Dbpool.Exec(Ctx,
"UPDATE game SET deleted=$1 WHERE deleted IS NULL", time.Now())
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
@@ -59,14 +59,14 @@ func SetGameDeletionDate() {
}
func ClearGames() {
_, err := dbpool.Exec(ctx, "DELETE FROM game")
_, err := Dbpool.Exec(Ctx, "DELETE FROM game")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
}
func UpdateGameName(id int, name string, path string) {
_, err := dbpool.Exec(ctx,
_, err := Dbpool.Exec(Ctx,
"UPDATE game SET game_name=$1, path=$2, last_changed=$3 WHERE id=$4",
name, path, time.Now(), id)
if err != nil {
@@ -75,7 +75,7 @@ func UpdateGameName(id int, name string, path string) {
}
func RemoveDeletionDate(id int) {
_, err := dbpool.Exec(ctx,
_, err := Dbpool.Exec(Ctx,
"UPDATE game SET deleted=null WHERE id=$1", id)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
@@ -84,7 +84,7 @@ func RemoveDeletionDate(id int) {
func GetIdByGameName(name string) int {
var gameId = -1
err := dbpool.QueryRow(ctx,
err := Dbpool.QueryRow(Ctx,
"SELECT id FROM game WHERE game_name = $1", name).Scan(&gameId)
if err != nil {
if compareError.Error() != err.Error() {
@@ -97,7 +97,7 @@ func GetIdByGameName(name string) int {
func InsertGame(name string, path string) int {
gameId := -1
err := dbpool.QueryRow(ctx,
err := Dbpool.QueryRow(Ctx,
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
name, path, time.Now()).Scan(&gameId)
if err != nil {
@@ -105,7 +105,7 @@ func InsertGame(name string, path string) int {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
resetGameIdSeq()
err2 := dbpool.QueryRow(ctx,
err2 := Dbpool.QueryRow(Ctx,
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
name, path, time.Now()).Scan(&gameId)
if err2 != nil {
@@ -119,7 +119,7 @@ func InsertGame(name string, path string) int {
}
func InsertGameWithExistingId(id int, name string, path string) {
_, err := dbpool.Exec(ctx,
_, err := Dbpool.Exec(Ctx,
"INSERT INTO game(id, game_name, path, added) VALUES ($1, $2, $3, $4)",
id, name, path, time.Now())
if err != nil {
@@ -128,7 +128,7 @@ func InsertGameWithExistingId(id int, name string, path string) {
}
func FindAllGames() []models.GameData {
rows, err := dbpool.Query(ctx,
rows, err := Dbpool.Query(Ctx,
"SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs "+
"FROM game WHERE deleted IS NULL "+
"ORDER BY game_name")
@@ -165,7 +165,7 @@ func FindAllGames() []models.GameData {
}
func AddGamePlayed(id int) {
_, err := dbpool.Exec(ctx,
_, err := Dbpool.Exec(Ctx,
"UPDATE game SET times_played=times_played+1, last_played=now() WHERE id=$1", id)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)

View File

@@ -12,12 +12,12 @@ var compareError = errors.New("no rows in result set")
func ClearSongs(gameId int) {
if gameId == -1 {
_, err := dbpool.Exec(ctx, "DELETE FROM song")
_, err := Dbpool.Exec(Ctx, "DELETE FROM song")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
} else {
_, err := dbpool.Exec(ctx, "DELETE FROM song where game_id=$1", gameId)
_, err := Dbpool.Exec(Ctx, "DELETE FROM song where game_id=$1", gameId)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
@@ -25,7 +25,7 @@ func ClearSongs(gameId int) {
}
func AddSong(song models.SongData) {
_, err := dbpool.Exec(ctx,
_, err := Dbpool.Exec(Ctx,
"INSERT INTO song(game_id, song_name, path, file_name) VALUES ($1, $2, $3, $4)",
song.GameId, song.SongName, song.Path, song.FileName)
if err != nil {
@@ -35,7 +35,7 @@ func AddSong(song models.SongData) {
func CheckSong(songPath string) bool {
var path string
err := dbpool.QueryRow(ctx,
err := Dbpool.QueryRow(Ctx,
"SELECT path FROM song WHERE path = $1", songPath).Scan(&path)
if err != nil {
if compareError.Error() != err.Error() {
@@ -46,7 +46,7 @@ func CheckSong(songPath string) bool {
}
func UpdateSong(songName string, fileName string, path string) {
_, err := dbpool.Exec(ctx,
_, err := Dbpool.Exec(Ctx,
"UPDATE song SET song_name=$1, file_name=$2 WHERE path = $3",
songName, fileName, path)
if err != nil {
@@ -55,7 +55,7 @@ func UpdateSong(songName string, fileName string, path string) {
}
func FindSongsFromGame(id int) []models.SongData {
rows, err := dbpool.Query(ctx,
rows, err := Dbpool.Query(Ctx,
"SELECT song_name, path, file_name, times_played FROM song WHERE game_id = $1", id)
if err != nil {
if compareError.Error() != err.Error() {
@@ -90,7 +90,7 @@ func FindSongsFromGame(id int) []models.SongData {
}
func AddSongPlayed(id int, name string) {
_, err := dbpool.Exec(ctx,
_, err := Dbpool.Exec(Ctx,
"UPDATE song SET times_played=times_played+1 WHERE game_id=$1 AND song_name=$2", id, name)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
@@ -98,7 +98,7 @@ func AddSongPlayed(id int, name string) {
}
func FetchAllSongs() []models.SongData {
rows, err := dbpool.Query(ctx,
rows, err := Dbpool.Query(Ctx,
"SELECT song_name, path FROM song")
if err != nil {
if compareError.Error() != err.Error() {
@@ -128,7 +128,7 @@ func FetchAllSongs() []models.SongData {
}
func RemoveBrokenSong(song models.SongData) {
_, err := dbpool.Exec(ctx, "DELETE FROM song where path=$1", song.Path)
_, err := Dbpool.Exec(Ctx, "DELETE FROM song where path=$1", song.Path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
@@ -141,7 +141,7 @@ func RemoveBrokenSongs(songs []models.SongData) {
}
joined = strings.TrimSuffix(joined, ",")
_, err := dbpool.Exec(ctx, "DELETE FROM song where path in ($1)", joined)
_, err := Dbpool.Exec(Ctx, "DELETE FROM song where path in ($1)", joined)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}

View File

@@ -8,7 +8,7 @@ import (
)
func InsertSongInList(song models.SongListData) {
_, err := dbpool.Exec(ctx,
_, err := Dbpool.Exec(Ctx,
`INSERT INTO song_list (match_date, match_id, song_no, game_name, song_name) VALUES ($1, $2, $3, $4, $5)`,
song.MatchDate, song.MatchId, song.SongNo, song.GameName, song.SongName)
if err != nil {
@@ -17,7 +17,7 @@ func InsertSongInList(song models.SongListData) {
}
func GetSongList(matchId int) []models.SongListData {
rows, err := dbpool.Query(ctx,
rows, err := Dbpool.Query(Ctx,
"SELECT match_date, match_id, song_no, game_name, song_name "+
"FROM song_list WHERE match_date = $1"+
"ORDER BY song_no DESC", matchId)