Adding queues and storage is now postgres only

This commit is contained in:
Melody Becker 2024-09-15 12:24:36 +02:00
parent 7ec0ce5a45
commit 1c2472cc2c
11 changed files with 368 additions and 31 deletions

View file

@ -3,7 +3,7 @@ package storage
import (
"errors"
"github.com/glebarez/sqlite"
"github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/linstrom/storage/cache"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@ -18,41 +18,26 @@ type Storage struct {
var ErrInvalidData = errors.New("invalid data")
// Build a new storage using sqlite as database backend
func NewStorageSqlite(filePath string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(sqlite.Open(filePath))
func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(postgres.Open(dbUrl), &gorm.Config{
Logger: newGormLogger(log.Logger),
})
if err != nil {
return nil, err
}
return storageFromEmptyDb(db, cache)
}
func NewStoragePostgres(dbUrl string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(postgres.Open(dbUrl))
if err != nil {
return nil, err
}
return storageFromEmptyDb(db, cache)
}
func storageFromEmptyDb(db *gorm.DB, cache *cache.Cache) (*Storage, error) {
// AutoMigrate ensures the db is in a state where all the structs given here
// have their own tables and relations setup. It also updates tables if necessary
err := db.AutoMigrate(
err = db.AutoMigrate(
MediaMetadata{},
Account{},
RemoteServer{},
Note{},
Role{},
PasskeySession{},
InboundJob{},
OutboundJob{},
)
if err != nil {
return nil, err
}
// And finally, build the actual storage struct
return &Storage{
db: db,
cache: cache,
}, nil
return &Storage{db, cache}, nil
}