plc-mirror/pds/pds.go

37 lines
839 B
Go
Raw Normal View History

2024-02-15 17:10:39 +01:00
package pds
import (
2024-02-21 10:35:25 +01:00
"context"
"fmt"
2024-02-15 17:10:39 +01:00
"time"
"gorm.io/gorm"
2024-02-15 21:29:08 +01:00
"github.com/uabluerail/indexer/models"
2024-02-15 17:10:39 +01:00
)
2024-02-21 10:35:25 +01:00
const Unknown models.ID = 0
2024-02-15 17:10:39 +01:00
type PDS struct {
2024-02-15 21:29:08 +01:00
ID models.ID `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
2024-02-15 17:10:39 +01:00
Host string `gorm:"uniqueIndex"`
Cursor int64
FirstCursorSinceReset int64
LastList time.Time
CrawlLimit int
}
func AutoMigrate(db *gorm.DB) error {
return db.AutoMigrate(&PDS{})
}
2024-02-21 10:35:25 +01:00
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
}