refactor: use a dynamic model registration process so we can use build tags to exclude/include

This commit is contained in:
Derrick Hammer 2024-02-28 21:20:05 -05:00
parent 4fe856fd03
commit bb167ea360
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
16 changed files with 79 additions and 20 deletions

View File

@ -4,13 +4,14 @@ import (
"context"
"fmt"
"git.lumeweb.com/LumeWeb/portal/db/models"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
"git.lumeweb.com/LumeWeb/portal/config"
"git.lumeweb.com/LumeWeb/portal/db/models"
"github.com/go-gorm/caches/v4"
"go.uber.org/fx"
"gorm.io/driver/mysql"
@ -60,22 +61,7 @@ func NewDatabase(lc fx.Lifecycle, params DatabaseParams) *gorm.DB {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return db.AutoMigrate(
&models.APIKey{},
&models.Blocklist{},
&models.DNSLink{},
&models.Download{},
&models.EmailVerification{},
&models.PasswordReset{},
&models.Pin{},
&models.PublicKey{},
&models.Upload{},
&models.User{},
&models.S3Upload{},
&models.S5Challenge{},
&models.TusLock{},
&models.TusUpload{},
)
return db.AutoMigrate(models.GetModels()...)
},
})

View File

@ -2,6 +2,10 @@ package models
import "gorm.io/gorm"
func init() {
registerModel(&APIKey{})
}
type APIKey struct {
gorm.Model
UserID uint

View File

@ -1,10 +1,15 @@
package models
import (
"gorm.io/gorm"
"time"
"gorm.io/gorm"
)
func init() {
registerModel(&Blocklist{})
}
type Blocklist struct {
gorm.Model
IP string

View File

@ -2,6 +2,10 @@ package models
import "gorm.io/gorm"
func init() {
registerModel(&DNSLink{})
}
type DNSLink struct {
gorm.Model
UserID uint `gorm:"uniqueIndex:idx_user_id_upload"`

View File

@ -1,10 +1,15 @@
package models
import (
"gorm.io/gorm"
"time"
"gorm.io/gorm"
)
func init() {
registerModel(&Download{})
}
type Download struct {
gorm.Model
UserID uint

View File

@ -6,6 +6,10 @@ import (
"gorm.io/gorm"
)
func init() {
registerModel(&EmailVerification{})
}
type EmailVerification struct {
gorm.Model

11
db/models/models.go Normal file
View File

@ -0,0 +1,11 @@
package models
var registered []interface{}
func registerModel(model interface{}) {
registered = append(registered, model)
}
func GetModels() []interface{} {
return registered
}

View File

@ -6,6 +6,10 @@ import (
"gorm.io/gorm"
)
func init() {
registerModel(&PasswordReset{})
}
type PasswordReset struct {
gorm.Model

View File

@ -2,6 +2,10 @@ package models
import "gorm.io/gorm"
func init() {
registerModel(&Pin{})
}
type Pin struct {
gorm.Model
UploadID uint

View File

@ -2,6 +2,10 @@ package models
import "gorm.io/gorm"
func init() {
registerModel(&PublicKey{})
}
type PublicKey struct {
gorm.Model
UserID uint

View File

@ -2,6 +2,10 @@ package models
import "gorm.io/gorm"
func init() {
registerModel(&S3Upload{})
}
type S3Upload struct {
gorm.Model
UploadID string `gorm:"unique;not null"`

View File

@ -2,6 +2,11 @@ package models
import "gorm.io/gorm"
func init() {
registerModel(&S5Challenge{})
}
type S5Challenge struct {
gorm.Model
Challenge string

View File

@ -3,11 +3,17 @@ package models
import (
"context"
"errors"
"time"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"time"
)
func init() {
registerModel(&TusLock{})
}
var (
ErrTusLockBusy = errors.New("lock is currently held by another process")
)

View File

@ -2,6 +2,11 @@ package models
import "gorm.io/gorm"
func init() {
registerModel(&TusUpload{})
}
type TusUpload struct {
gorm.Model
Hash []byte `gorm:"type:binary(32);uniqueIndex:idx_hash_deleted"`

View File

@ -2,6 +2,10 @@ package models
import "gorm.io/gorm"
func init() {
registerModel(&Upload{})
}
type Upload struct {
gorm.Model
UserID uint

View File

@ -8,6 +8,10 @@ import (
"gorm.io/gorm"
)
func init() {
registerModel(&User{})
}
type User struct {
gorm.Model
FirstName string