catbase/util/migrate/fact.go

70 lines
1.2 KiB
Go

package main
import (
"github.com/rs/zerolog/log"
bh "github.com/timshannon/bolthold"
_ "modernc.org/sqlite"
"time"
"github.com/jmoiron/sqlx"
)
type SQLFactoid struct {
Factoid
Created int64
Accessed int64
}
type Factoid struct {
ID uint64 `boltholdKey:"ID"`
Fact string
Tidbit string
Verb string
Owner string
Created time.Time
Accessed time.Time
Count int
}
type Alias struct {
Fact string
Next string
}
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(f.ID, f); err != nil {
return err
}
}
log.Printf("Migrated %d facts", len(allFacts))
q = `select * from factoid_alias`
allAliases := []Alias{}
if err := db.Select(&allAliases, q); err != nil {
return err
}
for _, f := range allAliases {
if err := store.Insert(bh.NextSequence(), &f); err != nil {
return err
}
}
log.Printf("Migrated %d aliases", len(allAliases))
return nil
}