added background scan

This commit is contained in:
majomi 2024-06-30 17:27:01 +02:00
parent 3707111538
commit 9452aaf8d4
Signed by: majomi
SSH Key Fingerprint: SHA256:VYTIGShChSqrGhLmmBZAmfYtjdyj+HLrfJ1Hb6d/0O8
13 changed files with 333 additions and 0 deletions

0
docker-compose.yml Normal file
View File

15
go.mod Normal file
View File

@ -0,0 +1,15 @@
module git.m3d.pw/majomi/mmm
go 1.22.4
require (
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8
github.com/jackc/pgx/v5 v5.6.0
)
require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

30
go.sum Normal file
View File

@ -0,0 +1,30 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8 h1:OtSeLS5y0Uy01jaKK4mA/WVIYtpzVm63vLVAPzJXigg=
github.com/dhowden/tag v0.0.0-20240417053706-3d75831295e8/go.mod h1:apkPC/CR3s48O2D7Y++n1XWEpgPNNCjXYga3PPbJe2E=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

32
internal/db/db.go Normal file
View File

@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
package db
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}

View File

@ -0,0 +1,26 @@
CREATE TABLE IF NOT EXISTS track
(
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
path TEXT UNIQUE NOT NULL,
album_artist TEXT NOT NULL,
title TEXT NOT NULL,
album TEXT NOT NULL,
disc INTEGER[][],
year INTEGER NOT NULL,
artist TEXT NOT NULL,
genre TEXT NOT NULL,
lyrics TEXT NOT NULL,
composer TEXT NOT NULL
);
--
-- CREATE TABLE IF NOT EXISTS album
-- (
-- id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
-- path TEXT UNIQUE
-- );
CREATE TABLE IF NOT EXISTS artist
(
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT UNIQUE
);

33
internal/db/models.go Normal file
View File

@ -0,0 +1,33 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
package db
import (
"github.com/jackc/pgx/v5/pgtype"
)
type Album struct {
ID int64
Path pgtype.Text
}
type Artist struct {
ID int64
Name pgtype.Text
}
type Track struct {
ID int64
Path string
AlbumArtist string
Title string
Album string
Disc [][]int32
Year int32
Artist string
Genre string
Lyrics string
Composer string
}

18
internal/db/querier.go Normal file
View File

@ -0,0 +1,18 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
package db
import (
"context"
)
type Querier interface {
AddTrack(ctx context.Context, arg AddTrackParams) (Track, error)
Exists(ctx context.Context, path string) (bool, error)
GetAlbumTracks(ctx context.Context, album string) ([]Track, error)
GetTrackByID(ctx context.Context, id int64) (Track, error)
}
var _ Querier = (*Queries)(nil)

View File

@ -0,0 +1,25 @@
-- name: GetTrackByID :one
SELECT *
FROM track
WHERE id = $1
LIMIT 1;
-- name: AddTrack :one
INSERT INTO track (path, album_artist, title, album, disc, year, artist, genre, lyrics, composer)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT DO NOTHING
RETURNING *;
INSERT INTO artist(name)
VALUES ($1);
-- name: GetAlbumTracks :many
SELECT *
FROM track
WHERE album = $1
ORDER BY track.title;
-- name: Exists :one
SELECT EXISTS (SELECT 1
FROM track
WHERE path = $1) as exists;

138
internal/db/track.sql.go Normal file
View File

@ -0,0 +1,138 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
// source: track.sql
package db
import (
"context"
)
const addTrack = `-- name: AddTrack :one
INSERT INTO track (path, album_artist, title, album, disc, year, artist, genre, lyrics, composer)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT DO NOTHING
RETURNING id, path, album_artist, title, album, disc, year, artist, genre, lyrics, composer
`
type AddTrackParams struct {
Path string
AlbumArtist string
Title string
Album string
Disc [][]int32
Year int32
Artist string
Genre string
Lyrics string
Composer string
}
func (q *Queries) AddTrack(ctx context.Context, arg AddTrackParams) (Track, error) {
row := q.db.QueryRow(ctx, addTrack,
arg.Path,
arg.AlbumArtist,
arg.Title,
arg.Album,
arg.Disc,
arg.Year,
arg.Artist,
arg.Genre,
arg.Lyrics,
arg.Composer,
)
var i Track
err := row.Scan(
&i.ID,
&i.Path,
&i.AlbumArtist,
&i.Title,
&i.Album,
&i.Disc,
&i.Year,
&i.Artist,
&i.Genre,
&i.Lyrics,
&i.Composer,
)
return i, err
}
const exists = `-- name: Exists :one
SELECT EXISTS (SELECT 1
FROM track
WHERE path = $1) as exists
`
func (q *Queries) Exists(ctx context.Context, path string) (bool, error) {
row := q.db.QueryRow(ctx, exists, path)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const getAlbumTracks = `-- name: GetAlbumTracks :many
SELECT id, path, album_artist, title, album, disc, year, artist, genre, lyrics, composer
FROM track
WHERE album = $1
ORDER BY track.title
`
func (q *Queries) GetAlbumTracks(ctx context.Context, album string) ([]Track, error) {
rows, err := q.db.Query(ctx, getAlbumTracks, album)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Track
for rows.Next() {
var i Track
if err := rows.Scan(
&i.ID,
&i.Path,
&i.AlbumArtist,
&i.Title,
&i.Album,
&i.Disc,
&i.Year,
&i.Artist,
&i.Genre,
&i.Lyrics,
&i.Composer,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getTrackByID = `-- name: GetTrackByID :one
SELECT id, path, album_artist, title, album, disc, year, artist, genre, lyrics, composer
FROM track
WHERE id = $1
LIMIT 1
`
func (q *Queries) GetTrackByID(ctx context.Context, id int64) (Track, error) {
row := q.db.QueryRow(ctx, getTrackByID, id)
var i Track
err := row.Scan(
&i.ID,
&i.Path,
&i.AlbumArtist,
&i.Title,
&i.Album,
&i.Disc,
&i.Year,
&i.Artist,
&i.Genre,
&i.Lyrics,
&i.Composer,
)
return i, err
}

5
main.go Normal file
View File

@ -0,0 +1,5 @@
package main
func main() {
}

11
sqlc.yaml Normal file
View File

@ -0,0 +1,11 @@
version: "2"
sql:
- engine: "postgresql"
queries: "internal/db/queries"
schema: "internal/db/migrations"
gen:
go:
package: "db"
out: "internal/db"
sql_package: "pgx/v5"
emit_interface: true

0
table.sql Normal file
View File