Handle repos with unknown PDS

This commit is contained in:
Max Ignatenko 2024-02-21 09:35:25 +00:00
parent fa8b67bcc9
commit b2003530ba
4 changed files with 29 additions and 2 deletions

View file

@ -1,6 +1,8 @@
package pds
import (
"context"
"fmt"
"time"
"gorm.io/gorm"
@ -8,6 +10,8 @@ import (
"github.com/uabluerail/indexer/models"
)
const Unknown models.ID = 0
type PDS struct {
ID models.ID `gorm:"primarykey"`
CreatedAt time.Time
@ -22,3 +26,11 @@ type PDS struct {
func AutoMigrate(db *gorm.DB) error {
return db.AutoMigrate(&PDS{})
}
func EnsureExists(ctx context.Context, db *gorm.DB, host string) (*PDS, error) {
remote := PDS{Host: host}
if err := db.Model(&remote).Where(&PDS{Host: host}).FirstOrCreate(&remote).Error; err != nil {
return nil, fmt.Errorf("failed to get PDS record from DB for %q: %w", remote.Host, err)
}
return &remote, nil
}