catbase/util/migrate/fact.go

55 lines
1.0 KiB
Go
Raw Normal View History

2021-12-22 03:36:08 +00:00
package main
import (
"github.com/rs/zerolog/log"
bh "github.com/timshannon/bolthold"
2021-12-22 07:53:02 +00:00
fact2 "github.com/velour/catbase/plugins/fact"
2021-12-22 03:36:08 +00:00
_ "modernc.org/sqlite"
"time"
"github.com/jmoiron/sqlx"
)
type SQLFactoid struct {
2021-12-22 07:53:02 +00:00
fact2.Factoid
2021-12-22 03:36:08 +00:00
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)
2021-12-22 07:53:02 +00:00
if err := store.Insert(bh.NextSequence(), &f); err != nil {
2021-12-22 03:36:08 +00:00
return err
}
}
log.Printf("Migrated %d facts", len(allFacts))
q = `select * from factoid_alias`
2021-12-22 07:53:02 +00:00
allAliases := []fact2.Alias{}
2021-12-22 03:36:08 +00:00
if err := db.Select(&allAliases, q); err != nil {
return err
}
for _, f := range allAliases {
2021-12-22 07:53:02 +00:00
if err := store.Insert(f.Key(), &f); err != nil {
2021-12-22 03:36:08 +00:00
return err
}
}
log.Printf("Migrated %d aliases", len(allAliases))
return nil
}