linstrom/storage/storage.go

81 lines
2.4 KiB
Go
Raw Normal View History

2024-05-31 09:54:39 +00:00
package storage
import (
2024-08-22 17:57:53 +00:00
"errors"
2024-05-31 15:21:29 +00:00
"fmt"
2024-05-31 09:54:39 +00:00
"github.com/glebarez/sqlite"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
2024-05-31 15:21:29 +00:00
// Storage is responsible for all database, cache and media related actions
// and serves as the lowest layer of the cake
2024-05-31 09:54:39 +00:00
type Storage struct {
db *gorm.DB
}
2024-08-22 17:57:53 +00:00
var ErrAccountNotFound = errors.New("account not found")
2024-05-31 15:21:29 +00:00
// Build a new storage using sqlite as database backend
2024-05-31 09:54:39 +00:00
func NewStorageSqlite(filePath string) (*Storage, error) {
db, err := gorm.Open(sqlite.Open(filePath))
if err != nil {
return nil, err
}
2024-05-31 15:21:29 +00:00
return storageFromEmptyDb(db)
2024-05-31 09:54:39 +00:00
}
func NewStoragePostgres(dbUrl string) (*Storage, error) {
db, err := gorm.Open(postgres.Open(dbUrl))
if err != nil {
return nil, err
}
2024-05-31 15:21:29 +00:00
return storageFromEmptyDb(db)
}
func storageFromEmptyDb(db *gorm.DB) (*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
2024-08-22 17:57:53 +00:00
if err := db.AutoMigrate(
2024-05-31 15:21:29 +00:00
placeholderMediaFile,
2024-08-22 17:57:53 +00:00
placeholderUser,
2024-05-31 15:21:29 +00:00
placeholderNote,
placeholderServer,
2024-08-22 17:57:53 +00:00
); err != nil {
return nil, fmt.Errorf("problem while auto migrating: %w", err)
}
2024-05-31 15:21:29 +00:00
// Afterwards add the placeholder entries for each table.
// FirstOrCreate either creates a new entry or retrieves the first matching one
// We only care about the creation if there is none yet, so no need to carry the result over
if res := db.FirstOrCreate(placeholderMediaFile); res.Error != nil {
return nil, fmt.Errorf("failed to add placeholder media file: %w", res.Error)
}
if res := db.FirstOrCreate(placeholderUser()); res.Error != nil {
return nil, fmt.Errorf("failed to add placeholder media file: %w", res.Error)
}
if res := db.FirstOrCreate(placeholderNote); res.Error != nil {
return nil, fmt.Errorf("failed to add placeholder media file: %w", res.Error)
}
if res := db.FirstOrCreate(placeholderServer); res.Error != nil {
return nil, fmt.Errorf("failed to add placeholder media file: %w", res.Error)
}
// And finally, build the actual storage struct
2024-05-31 09:54:39 +00:00
return &Storage{
db: db,
}, nil
}
2024-08-22 17:57:53 +00:00
func (s *Storage) FindLocalAccount(handle string) (*User, error) {
acc := User{}
res := s.db.Where("handle = ?", handle).First(&acc)
if res.Error != nil {
return nil, fmt.Errorf("failed to query db: %w", res.Error)
}
if res.RowsAffected == 0 {
return nil, ErrAccountNotFound
}
return nil, errors.New("unimplemented")
2024-05-31 09:54:39 +00:00
}