2021-12-22 03:36:08 +00:00
|
|
|
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
|
|
|
|
}
|
2021-12-22 07:53:02 +00:00
|
|
|
if err := store.Insert(bh.NextSequence(), &it); err != nil {
|
2021-12-22 03:36:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:25:42 +00:00
|
|
|
aliases := []counter2.CounterAlias{}
|
2021-12-22 03:36:08 +00:00
|
|
|
log.Printf("Migrating %T", aliases)
|
|
|
|
if err := db.Select(&aliases, `select * from counter_alias`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, i := range all {
|
2021-12-22 07:53:02 +00:00
|
|
|
if err := store.Insert(bh.NextSequence(), &i); err != nil {
|
2021-12-22 03:36:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|