Skip to content

Commit d5ab1b4

Browse files
committed
use JSONB for PkgMeta, add JSON tag hints, switch to GORM/datatypes for
data types
1 parent 913d520 commit d5ab1b4

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

db/db.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/google/uuid"
9+
"gorm.io/datatypes"
910
"gorm.io/driver/postgres"
1011
"gorm.io/gorm"
1112
)
@@ -19,38 +20,38 @@ const (
1920

2021
// PkgMeta stores arbitrary metadata for a package.
2122
type PkgMeta struct {
22-
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();not null;primaryKey"`
23-
PkgID uuid.UUID `gorm:"not null;index;constraint:OnDelete:CASCADE"`
24-
Key string
25-
Val string
23+
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();not null;primaryKey" json:"id"`
24+
PkgID uuid.UUID `gorm:"not null;index;constraint:OnDelete:CASCADE" json:"pkg_id"`
25+
Key string `gorm:"not null;index" json:"key"`
26+
Data datatypes.JSON `gorm:"type:jsonb;default:'{}'" json:"data"`
2627
}
2728

2829
// Pkg is the package model. Uses UUID primary key instead of gorm.Model's uint.
2930
type Pkg struct {
30-
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();not null;primaryKey"`
31-
CreatedAt time.Time `gorm:"autoCreateTime"`
32-
UpdatedAt time.Time `gorm:"autoUpdateTime"`
33-
DeletedAt gorm.DeletedAt `gorm:"index"`
31+
ID datatypes.UUID `gorm:"type:uuid;default:gen_random_uuid();not null;primaryKey" json:"id"`
32+
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
33+
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
34+
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
3435

35-
Name string
36-
FullVer string
37-
Ver string // compatible version string between repos
38-
Arch string
36+
Name string `json:"name"`
37+
FullVer string `json:"full_ver"`
38+
Ver string `json:"ver"`
39+
Arch string `json:"arch"`
3940
// RepoID is a string (not a UUID). Repositories are identified by string IDs.
40-
RepoID string `gorm:"not null;index"`
41+
RepoID string `gorm:"not null;index" json:"repo_id"`
4142
// Foreign key relationship referencing Repo.ID (string).
42-
Repo Repo `gorm:"constraint:OnDelete:CASCADE;foreignKey:RepoID;references:ID"`
43+
Repo Repo `gorm:"constraint:OnDelete:CASCADE;foreignKey:RepoID;references:ID" json:"-"`
4344

4445
Metas []PkgMeta
4546
}
4647

4748
// Repo represents a package repository. Its ID is a string.
4849
type Repo struct {
49-
ID string `gorm:"primaryKey"`
50-
UpdAt time.Time `gorm:"autoUpdateTime"`
51-
Links string
52-
Type RepoType
53-
Fetch string
50+
ID string `gorm:"primaryKey" json:"id"`
51+
UpdAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
52+
Links string `json:"links"`
53+
Type RepoType `json:"type"`
54+
Fetch string `json:"fetch"`
5455
}
5556

5657
var DB *gorm.DB

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ require (
77
github.com/google/uuid v1.6.0
88
github.com/joho/godotenv v1.5.1
99
github.com/klauspost/compress v1.18.0
10+
gorm.io/datatypes v1.2.6
1011
gorm.io/driver/postgres v1.6.0
1112
gorm.io/gorm v1.30.3
1213
)
1314

1415
require (
16+
filippo.io/edwards25519 v1.1.0 // indirect
1517
github.com/bytedance/sonic v1.11.6 // indirect
1618
github.com/bytedance/sonic/loader v0.1.1 // indirect
1719
github.com/cloudwego/base64x v0.1.4 // indirect
@@ -21,6 +23,7 @@ require (
2123
github.com/go-playground/locales v0.14.1 // indirect
2224
github.com/go-playground/universal-translator v0.18.1 // indirect
2325
github.com/go-playground/validator/v10 v10.20.0 // indirect
26+
github.com/go-sql-driver/mysql v1.8.1 // indirect
2427
github.com/goccy/go-json v0.10.2 // indirect
2528
github.com/jackc/pgpassfile v1.0.0 // indirect
2629
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
@@ -45,4 +48,5 @@ require (
4548
golang.org/x/text v0.28.0 // indirect
4649
google.golang.org/protobuf v1.34.1 // indirect
4750
gopkg.in/yaml.v3 v3.0.1 // indirect
51+
gorm.io/driver/mysql v1.5.6 // indirect
4852
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
2+
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
13
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
24
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
35
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
@@ -20,6 +22,9 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
2022
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
2123
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
2224
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
25+
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
26+
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
27+
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
2328
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
2429
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
2530
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -95,8 +100,13 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
95100
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
96101
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
97102
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
103+
gorm.io/datatypes v1.2.6 h1:KafLdXvFUhzNeL2ncm03Gl3eTLONQfNKZ+wJ+9Y4Nck=
104+
gorm.io/datatypes v1.2.6/go.mod h1:M2iO+6S3hhi4nAyYe444Pcb0dcIiOMJ7QHaUXxyiNZY=
105+
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
106+
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
98107
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
99108
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
109+
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
100110
gorm.io/gorm v1.30.3 h1:QiG8upl0Sg9ba2Zatfjy0fy4It2iNBL2/eMdvEkdXNs=
101111
gorm.io/gorm v1.30.3/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
102112
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=

kudari/rpm.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func eachFetch(repo db.Repo, fetch string, ch chan []PackageXML) {
174174

175175
func rpmFetch(repo db.Repo) {
176176
chans := []chan []PackageXML{}
177-
for fetch := range strings.SplitSeq(repo.Fetch, "\n") {
177+
for _, fetch := range strings.Split(repo.Fetch, "\n") {
178178
ch := make(chan []PackageXML, 1)
179179
chans = append(chans, ch)
180180
go eachFetch(repo, fetch, ch)
@@ -184,7 +184,7 @@ func rpmFetch(repo db.Repo) {
184184
log.Printf("[%s] Failed to list packages: %v", repo.ID, err)
185185
return
186186
}
187-
allSlices := make([][]PackageXML, len(chans))
187+
allSlices := [][]PackageXML{}
188188
for _, ch := range chans {
189189
local_packages, ok := <-ch
190190
if !ok {
@@ -233,7 +233,7 @@ func rpmFetch(repo db.Repo) {
233233
var deletes []uuid.UUID
234234
for i, p := range pkgs {
235235
if !walked[i] {
236-
deletes = append(deletes, p.ID)
236+
deletes = append(deletes, uuid.UUID(p.ID))
237237
}
238238
}
239239
tx.Delete(&db.Pkg{}, "id IN (?)", deletes)

0 commit comments

Comments
 (0)