catbase/util/migrate/counter.go

45 lines
895 B
Go

package main
import (
"database/sql"
"github.com/jmoiron/sqlx"
bh "github.com/timshannon/bolthold"
counter2 "github.com/velour/catbase/plugins/counter"
"log"
)
type ItemSQL struct {
counter2.Item
UserID sql.NullString
}
func migrateCounter(db *sqlx.DB, store *bh.Store) error {
all := []ItemSQL{}
log.Printf("Migrating %T", all)
if err := db.Select(&all, `select * from counter`); err != nil {
return err
}
for _, i := range all {
it := i.Item
if i.UserID.Valid {
it.UserID = i.UserID.String
}
if err := store.Insert(bh.NextSequence(), &it); err != nil {
return err
}
}
aliases := []counter2.CounterAlias{}
log.Printf("Migrating %T", aliases)
if err := db.Select(&aliases, `select * from counter_alias`); err != nil {
return err
}
for _, i := range all {
if err := store.Insert(bh.NextSequence(), &i); err != nil {
return err
}
}
return nil
}