From 505f7d8ae6116c5dc1cfa50a5caba6333672be20 Mon Sep 17 00:00:00 2001 From: sentriz Date: Tue, 23 Sep 2025 23:05:14 +0100 Subject: [PATCH] feat(build): add Cgo free database --- cmd/gonic/gonic.go | 1 - cmd/gonic/gonic_dep.go | 6 ++- cmd/gonic/gonic_dep_nowasm.go | 2 + db/db.go | 45 ------------------- db/migrations.go | 9 ---- go.mod | 2 + go.sum | 4 ++ .../artistinfocache/artistinfocache_test.go | 3 ++ mockfs/mockfs.go | 22 +-------- 9 files changed, 18 insertions(+), 76 deletions(-) diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index b01efc2..6eb2f7d 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -26,7 +26,6 @@ import ( "github.com/google/shlex" "github.com/gorilla/securecookie" - _ "github.com/jinzhu/gorm/dialects/sqlite" "github.com/sentriz/gormstore" "golang.org/x/sync/errgroup" diff --git a/cmd/gonic/gonic_dep.go b/cmd/gonic/gonic_dep.go index 9899987..abd44dd 100644 --- a/cmd/gonic/gonic_dep.go +++ b/cmd/gonic/gonic_dep.go @@ -3,7 +3,11 @@ package main import ( - // CGo-free Wasm tagger + // Cgo-free Wasm database + _ "github.com/ncruces/go-sqlite3/driver" + _ "github.com/ncruces/go-sqlite3/embed" + + // Cgo-free Wasm tagger "go.senan.xyz/gonic/tags/wrtag" ) diff --git a/cmd/gonic/gonic_dep_nowasm.go b/cmd/gonic/gonic_dep_nowasm.go index f72351c..e4afbec 100644 --- a/cmd/gonic/gonic_dep_nowasm.go +++ b/cmd/gonic/gonic_dep_nowasm.go @@ -3,6 +3,8 @@ package main import ( + // Cgo database + _ "github.com/mattn/go-sqlite3" // Cgo tagger "go.senan.xyz/gonic/tags/taglib" diff --git a/db/db.go b/db/db.go index 1261b93..5c793da 100644 --- a/db/db.go +++ b/db/db.go @@ -1,7 +1,6 @@ package db import ( - "context" "errors" "fmt" "log" @@ -13,7 +12,6 @@ import ( "time" "github.com/jinzhu/gorm" - "github.com/mattn/go-sqlite3" // TODO: remove this dep "go.senan.xyz/gonic/server/ctrlsubsonic/specid" @@ -43,7 +41,6 @@ type DB struct { } func New(path string, options url.Values) (*DB, error) { - // https://github.com/mattn/go-sqlite3#connection-string url := url.URL{ Scheme: "file", Opaque: path, @@ -620,45 +617,3 @@ func join[T fmt.Stringer](in []T, sep string) string { } return strings.Join(strs, sep) } - -func Dump(ctx context.Context, db *gorm.DB, to string) error { - dest, err := New(to, url.Values{}) - if err != nil { - return fmt.Errorf("create dest db: %w", err) - } - defer dest.Close() - - connSrc, err := db.DB().Conn(ctx) - if err != nil { - return fmt.Errorf("getting src raw conn: %w", err) - } - defer connSrc.Close() - - connDest, err := dest.DB.DB().Conn(ctx) - if err != nil { - return fmt.Errorf("getting dest raw conn: %w", err) - } - defer connDest.Close() - - err = connDest.Raw(func(connDest any) error { - return connSrc.Raw(func(connSrc any) error { - connDestq := connDest.(*sqlite3.SQLiteConn) - connSrcq := connSrc.(*sqlite3.SQLiteConn) - bk, err := connDestq.Backup("main", connSrcq, "main") - if err != nil { - return fmt.Errorf("create backup db: %w", err) - } - for done, _ := bk.Step(-1); !done; { //nolint: revive - } - if err := bk.Finish(); err != nil { - return fmt.Errorf("finishing dump: %w", err) - } - return nil - }) - }) - if err != nil { - return fmt.Errorf("backing up: %w", err) - } - - return nil -} diff --git a/db/migrations.go b/db/migrations.go index 8c28745..352954f 100644 --- a/db/migrations.go +++ b/db/migrations.go @@ -2,7 +2,6 @@ package db import ( - "context" "errors" "fmt" "log" @@ -59,7 +58,6 @@ func (db *DB) Migrate(ctx MigrationContext) error { construct(ctx, "202206101425", migrateUser), construct(ctx, "202207251148", migrateStarRating), construct(ctx, "202211111057", migratePlaylistsQueuesToFullID), - constructNoTx(ctx, "202212272312", backupDBPre016), construct(ctx, "202304221528", migratePlaylistsToM3U), construct(ctx, "202305301718", migratePlayCountToLength), construct(ctx, "202307281628", migrateAlbumArtistsMany2Many), @@ -738,13 +736,6 @@ func migratePlaylistsPaths(tx *gorm.DB, ctx MigrationContext) error { return nil } -func backupDBPre016(tx *gorm.DB, ctx MigrationContext) error { - if !ctx.Production { - return nil - } - return Dump(context.Background(), tx, fmt.Sprintf("%s.%d.bak", ctx.DBPath, time.Now().Unix())) -} - func migrateAlbumTagArtistString(tx *gorm.DB, _ MigrationContext) error { return tx.AutoMigrate(Album{}).Error } diff --git a/go.mod b/go.mod index b7a8498..1cbff98 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/microcosm-cc/bluemonday v1.0.27 github.com/mitchellh/mapstructure v1.5.0 github.com/mmcdole/gofeed v1.3.0 + github.com/ncruces/go-sqlite3 v0.29.0 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/philippta/go-template v0.0.0-20220911145045-4556aca435e4 github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be @@ -56,6 +57,7 @@ require ( github.com/mmcdole/goxpp v1.1.1 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/ncruces/julianday v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tetratelabs/wazero v1.9.0 // indirect go.senan.xyz/taglib v0.8.0 // indirect diff --git a/go.sum b/go.sum index 21349e0..81a1a20 100644 --- a/go.sum +++ b/go.sum @@ -115,6 +115,10 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/ncruces/go-sqlite3 v0.29.0 h1:1tsLiagCoqZEfcHDeKsNSv5jvrY/Iu393pAnw2wLNJU= +github.com/ncruces/go-sqlite3 v0.29.0/go.mod h1:r1hSvYKPNJ+OlUA1O3r8o9LAawzPAlqeZiIdxTBBBJ0= +github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M= +github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/philippta/go-template v0.0.0-20220911145045-4556aca435e4 h1:SvVjZyjXBOjqjCdMiC9ndyWb709aP5qU4Qbun40GCxA= diff --git a/infocache/artistinfocache/artistinfocache_test.go b/infocache/artistinfocache/artistinfocache_test.go index 3e4edeb..1882c09 100644 --- a/infocache/artistinfocache/artistinfocache_test.go +++ b/infocache/artistinfocache/artistinfocache_test.go @@ -12,6 +12,9 @@ import ( "go.senan.xyz/gonic/lastfm" "go.senan.xyz/gonic/lastfm/mockclient" "go.senan.xyz/gonic/mockfs" + + _ "github.com/ncruces/go-sqlite3/driver" + _ "github.com/ncruces/go-sqlite3/embed" ) func TestInfoCache(t *testing.T) { diff --git a/mockfs/mockfs.go b/mockfs/mockfs.go index 1827aa3..0b618cf 100644 --- a/mockfs/mockfs.go +++ b/mockfs/mockfs.go @@ -2,7 +2,6 @@ package mockfs import ( - "context" "errors" "fmt" "os" @@ -79,8 +78,8 @@ func newMockFS(tb testing.TB, dirs []string, excludePattern string) *MockFS { } } -func (m *MockFS) DB() *db.DB { return m.db } -func (m *MockFS) TmpDir() string { return m.dir } +func (m *MockFS) DB() *db.DB { return m.db } +func (m *MockFS) TmpDir() string { return m.dir } func (m *MockFS) TagReader() tags.Reader { return m.tagReader } func (m *MockFS) ScanAndClean() *scanner.State { @@ -299,23 +298,6 @@ func (m *MockFS) SetTags(path string, cb func(*TagInfo)) { cb(m.tagReader.paths[absPath]) } -func (m *MockFS) DumpDB(suffix ...string) { - var p []string - p = append(p, - "gonic", "dump", - strings.ReplaceAll(m.t.Name(), string(filepath.Separator), "-"), - fmt.Sprint(time.Now().UnixNano()), - ) - p = append(p, suffix...) - - destPath := filepath.Join(os.TempDir(), strings.Join(p, "-")) - if err := db.Dump(context.Background(), m.db.DB, destPath); err != nil { - m.t.Fatalf("dumping db: %v", err) - } - - m.t.Error(destPath) -} - type tagReader struct { paths map[string]*TagInfo }