catbase/util/migrate/fact.go

55 lines
1.0 KiB
Go

package main
import (
"github.com/rs/zerolog/log"
bh "github.com/timshannon/bolthold"
fact2 "github.com/velour/catbase/plugins/fact"
_ "modernc.org/sqlite"
"time"
"github.com/jmoiron/sqlx"
)
type SQLFactoid struct {
fact2.Factoid
Created int64
Accessed int64
}
func migrateFacts(db *sqlx.DB, store *bh.Store) error {
q := `select * from factoid`
allFacts := []SQLFactoid{}
if err := db.Select(&allFacts, q); err != nil {
return err
}
for _, sqlf := range allFacts {
f := sqlf.Factoid
f.Accessed = time.Unix(sqlf.Accessed, 0)
f.Created = time.Unix(sqlf.Created, 0)
if err := store.Insert(bh.NextSequence(), &f); err != nil {
return err
}
}
log.Printf("Migrated %d facts", len(allFacts))
q = `select * from factoid_alias`
allAliases := []fact2.FactAlias{}
if err := db.Select(&allAliases, q); err != nil {
return err
}
for _, f := range allAliases {
if err := store.Insert(f.Key(), &f); err != nil {
return err
}
}
log.Printf("Migrated %d aliases", len(allAliases))
return nil
}