feat: Add id column to song table and prep for UUID migration
- Add id serial4 PK to song table (was composite PK) - Update queries to use soundtrack_id + path - Add UUID columns to soundtrack and song (nullable) - Add migration tracking table TODO: Run sqlc generate, then create backfill migration (000008) Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -7,19 +7,22 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const addHashToSong = `-- name: AddHashToSong :exec
|
||||
UPDATE song SET hash=$1 where path=$2
|
||||
UPDATE song SET hash=$1 where soundtrack_id = $2 AND path = $3
|
||||
`
|
||||
|
||||
type AddHashToSongParams struct {
|
||||
Hash string `json:"hash"`
|
||||
Path string `json:"path"`
|
||||
Hash string `json:"hash"`
|
||||
SoundtrackID int32 `json:"soundtrack_id"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
func (q *Queries) AddHashToSong(ctx context.Context, arg AddHashToSongParams) error {
|
||||
_, err := q.db.Exec(ctx, addHashToSong, arg.Hash, arg.Path)
|
||||
_, err := q.db.Exec(ctx, addHashToSong, arg.Hash, arg.SoundtrackID, arg.Path)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -62,11 +65,16 @@ func (q *Queries) AddSongPlayed(ctx context.Context, arg AddSongPlayedParams) er
|
||||
}
|
||||
|
||||
const checkSong = `-- name: CheckSong :one
|
||||
SELECT COUNT(*) FROM song WHERE path = $1
|
||||
SELECT COUNT(*) FROM song WHERE soundtrack_id = $1 AND path = $2
|
||||
`
|
||||
|
||||
func (q *Queries) CheckSong(ctx context.Context, path string) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, checkSong, path)
|
||||
type CheckSongParams struct {
|
||||
SoundtrackID int32 `json:"soundtrack_id"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
func (q *Queries) CheckSong(ctx context.Context, arg CheckSongParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, checkSong, arg.SoundtrackID, arg.Path)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
@@ -102,7 +110,7 @@ func (q *Queries) ClearSongsBySoundtrackId(ctx context.Context, soundtrackID int
|
||||
}
|
||||
|
||||
const fetchAllSongs = `-- name: FetchAllSongs :many
|
||||
SELECT soundtrack_id, song_name, path, times_played, hash, file_name FROM song
|
||||
SELECT soundtrack_id, song_name, path, times_played, hash, file_name, id, uuid FROM song
|
||||
`
|
||||
|
||||
func (q *Queries) FetchAllSongs(ctx context.Context) ([]Song, error) {
|
||||
@@ -121,6 +129,8 @@ func (q *Queries) FetchAllSongs(ctx context.Context) ([]Song, error) {
|
||||
&i.TimesPlayed,
|
||||
&i.Hash,
|
||||
&i.FileName,
|
||||
&i.ID,
|
||||
&i.Uuid,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -133,7 +143,7 @@ func (q *Queries) FetchAllSongs(ctx context.Context) ([]Song, error) {
|
||||
}
|
||||
|
||||
const findSongsFromSoundtrack = `-- name: FindSongsFromSoundtrack :many
|
||||
SELECT soundtrack_id, song_name, path, times_played, hash, file_name
|
||||
SELECT soundtrack_id, song_name, path, times_played, hash, file_name, id, uuid
|
||||
FROM song
|
||||
WHERE soundtrack_id = $1
|
||||
`
|
||||
@@ -154,6 +164,8 @@ func (q *Queries) FindSongsFromSoundtrack(ctx context.Context, soundtrackID int3
|
||||
&i.TimesPlayed,
|
||||
&i.Hash,
|
||||
&i.FileName,
|
||||
&i.ID,
|
||||
&i.Uuid,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -165,8 +177,28 @@ func (q *Queries) FindSongsFromSoundtrack(ctx context.Context, soundtrackID int3
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getSongById = `-- name: GetSongById :one
|
||||
SELECT soundtrack_id, song_name, path, times_played, hash, file_name, id, uuid FROM song WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSongById(ctx context.Context, id pgtype.Int4) (Song, error) {
|
||||
row := q.db.QueryRow(ctx, getSongById, id)
|
||||
var i Song
|
||||
err := row.Scan(
|
||||
&i.SoundtrackID,
|
||||
&i.SongName,
|
||||
&i.Path,
|
||||
&i.TimesPlayed,
|
||||
&i.Hash,
|
||||
&i.FileName,
|
||||
&i.ID,
|
||||
&i.Uuid,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSongWithHash = `-- name: GetSongWithHash :one
|
||||
SELECT soundtrack_id, song_name, path, times_played, hash, file_name FROM song WHERE hash = $1
|
||||
SELECT soundtrack_id, song_name, path, times_played, hash, file_name, id, uuid FROM song WHERE hash = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSongWithHash(ctx context.Context, hash string) (Song, error) {
|
||||
@@ -179,25 +211,32 @@ func (q *Queries) GetSongWithHash(ctx context.Context, hash string) (Song, error
|
||||
&i.TimesPlayed,
|
||||
&i.Hash,
|
||||
&i.FileName,
|
||||
&i.ID,
|
||||
&i.Uuid,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const removeBrokenSong = `-- name: RemoveBrokenSong :exec
|
||||
DELETE FROM song WHERE path = $1
|
||||
DELETE FROM song WHERE soundtrack_id = $1 AND path = $2
|
||||
`
|
||||
|
||||
func (q *Queries) RemoveBrokenSong(ctx context.Context, path string) error {
|
||||
_, err := q.db.Exec(ctx, removeBrokenSong, path)
|
||||
type RemoveBrokenSongParams struct {
|
||||
SoundtrackID int32 `json:"soundtrack_id"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
func (q *Queries) RemoveBrokenSong(ctx context.Context, arg RemoveBrokenSongParams) error {
|
||||
_, err := q.db.Exec(ctx, removeBrokenSong, arg.SoundtrackID, arg.Path)
|
||||
return err
|
||||
}
|
||||
|
||||
const removeBrokenSongs = `-- name: RemoveBrokenSongs :exec
|
||||
DELETE FROM song where path = any ($1)
|
||||
DELETE FROM song WHERE id = ANY($1)
|
||||
`
|
||||
|
||||
func (q *Queries) RemoveBrokenSongs(ctx context.Context, paths []string) error {
|
||||
_, err := q.db.Exec(ctx, removeBrokenSongs, paths)
|
||||
func (q *Queries) RemoveBrokenSongs(ctx context.Context, id pgtype.Int4) error {
|
||||
_, err := q.db.Exec(ctx, removeBrokenSongs, id)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user