/
indexer_metadata.go
45 lines (38 loc) · 1007 Bytes
/
indexer_metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package models
import (
"fmt"
"log"
"time"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// Indexer is a model for indexer metadata.
type IndexerMetadata struct {
gorm.Model
// LastFullIndexAt is the time that the indexer last completed a full index.
LastFullIndexAt time.Time
}
// Get gets the indexer metadata and assigns it to the receiver.
func (m *IndexerMetadata) Get(db *gorm.DB) error {
// Don't log "record not found" errors (will still return the error).
tx := db.Session(&gorm.Session{Logger: logger.New(
log.Default(),
logger.Config{IgnoreRecordNotFoundError: true},
)})
return tx.
First(&m, 1). // There should only ever be one row in this table.
Error
}
// Upsert updates or inserts the indexer metadata.
func (m *IndexerMetadata) Upsert(db *gorm.DB) error {
tx := db.
Assign(*m).
FirstOrCreate(&m, 1)
if err := tx.Error; err != nil {
return err
}
if tx.RowsAffected != 1 {
return fmt.Errorf("expected 1 row affected, got %d", tx.RowsAffected)
}
return nil
}