sync for backup

This commit is contained in:
Melody Becker 2024-08-22 19:57:53 +02:00
parent 1fbdf7fc9d
commit a9916acea5
Signed by: mstar
SSH key fingerprint: SHA256:vkXfS9FG2pVNVfvDrzd1VW9n8VJzqqdKQGljxxX8uK8
92 changed files with 35330 additions and 882 deletions

View file

@ -1,6 +1,7 @@
package storage
import (
"errors"
"fmt"
"github.com/glebarez/sqlite"
@ -14,6 +15,8 @@ type Storage struct {
db *gorm.DB
}
var ErrAccountNotFound = errors.New("account not found")
// Build a new storage using sqlite as database backend
func NewStorageSqlite(filePath string) (*Storage, error) {
db, err := gorm.Open(sqlite.Open(filePath))
@ -34,12 +37,14 @@ func NewStoragePostgres(dbUrl string) (*Storage, error) {
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
db.AutoMigrate(
if err := db.AutoMigrate(
placeholderMediaFile,
placeholderUser(),
placeholderUser,
placeholderNote,
placeholderServer,
)
); err != nil {
return nil, fmt.Errorf("problem while auto migrating: %w", err)
}
// 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
@ -62,7 +67,14 @@ func storageFromEmptyDb(db *gorm.DB) (*Storage, error) {
}, nil
}
// TODO: Placeholder. Update to proper implementation later. Including signature
func (s *Storage) FindLocalAccount(handle string) (string, error) {
return handle, nil
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")
}