counter: fix ID null string regression

Also remove shared DB cache as it seems to not be necessary.
This commit is contained in:
Chris Sexton 2022-07-17 14:43:41 -04:00
parent 55ba4996eb
commit 2e0be87dd9
2 changed files with 13 additions and 9 deletions

View File

@ -109,7 +109,7 @@ func (mb *MockBot) UploadEmojy(emojy, path string) error { return nil
func (mb *MockBot) RegisterFilter(s string, f func(string) string) {} func (mb *MockBot) RegisterFilter(s string, f func(string) string) {}
func NewMockBot() *MockBot { func NewMockBot() *MockBot {
cfg := config.ReadConfig("file::memory:?mode=memory&cache=shared") cfg := config.ReadConfig(":memory:")
b := MockBot{ b := MockBot{
Cfg: cfg, Cfg: cfg,
Messages: make([]string, 0), Messages: make([]string, 0),

View File

@ -190,7 +190,7 @@ func GetUserItem(db *sqlx.DB, nick, id, itemName string) (Item, error) {
item.ID = -1 item.ID = -1
item.Nick = nick item.Nick = nick
item.Item = itemName item.Item = itemName
item.UserID.String = id item.UserID.Scan(id)
case nil: case nil:
default: default:
return Item{}, err return Item{}, err
@ -208,7 +208,7 @@ func GetUserItem(db *sqlx.DB, nick, id, itemName string) (Item, error) {
// Create saves a counter // Create saves a counter
func (i *Item) Create() error { func (i *Item) Create() error {
res, err := i.Exec(`insert into counter (nick, item, count, userid) values (?, ?, ?, ?);`, res, err := i.Exec(`insert into counter (nick, item, count, userid) values (?, ?, ?, ?);`,
i.Nick, i.Item, i.Count, i.UserID) i.Nick, i.Item, i.Count, i.UserID.String)
if err != nil { if err != nil {
return err return err
} }
@ -226,7 +226,9 @@ func (i *Item) Update(r *bot.Request, value int) error {
return i.Delete() return i.Delete()
} }
if i.ID == -1 { if i.ID == -1 {
i.Create() if err := i.Create(); err != nil {
return err
}
} }
log.Debug(). log.Debug().
Interface("i", i). Interface("i", i).
@ -286,21 +288,20 @@ func (p *CounterPlugin) migrate(r bot.Request) bool {
func setupDB(b bot.Bot) error { func setupDB(b bot.Bot) error {
db := b.DB() db := b.DB()
tx := db.MustBegin()
db.MustExec(`create table if not exists counter ( db.MustExec(`create table if not exists counter (
id integer primary key, id integer primary key,
nick string, nick string,
item string, item string,
count integer count integer,
userid string
);`) );`)
db.MustExec(`create table if not exists counter_alias ( db.MustExec(`create table if not exists counter_alias (
id integer PRIMARY KEY AUTOINCREMENT, id integer PRIMARY KEY AUTOINCREMENT,
item string NOT NULL UNIQUE, item string NOT NULL UNIQUE,
points_to string NOT NULL points_to string NOT NULL
);`) );`)
tx.Commit()
tx = db.MustBegin() tx := db.MustBegin()
count := 0 count := 0
err := tx.Get(&count, `SELECT count(*) FROM pragma_table_info('counter') where name='userid'`) err := tx.Get(&count, `SELECT count(*) FROM pragma_table_info('counter') where name='userid'`)
if err != nil { if err != nil {
@ -594,7 +595,10 @@ func (p *CounterPlugin) incrementCmd(r bot.Request) bool {
} }
log.Debug().Msgf("About to update item: %#v", item) log.Debug().Msgf("About to update item: %#v", item)
p.b.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, item.Count+1, item.Item)) p.b.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, item.Count+1, item.Item))
item.UpdateDelta(&r, 1) err = item.UpdateDelta(&r, 1)
if err != nil {
log.Error().Err(err).Msgf("Could not UpdateDelta")
}
return true return true
} }