Changeset 497 in code for trunk/db.go


Ignore:
Timestamp:
Apr 13, 2021, 4:15:30 PM (4 years ago)
Author:
contact
Message:

Store last internal msg ID in DB when detaching

References: https://todo.sr.ht/~emersion/soju/98

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/db.go

    r489 r497  
    110110
    111111type Channel struct {
    112         ID       int64
    113         Name     string
    114         Key      string
    115         Detached bool
     112        ID   int64
     113        Name string
     114        Key  string
     115
     116        Detached              bool
     117        DetachedInternalMsgID string
    116118
    117119        RelayDetached MessageFilter
     
    162164        key VARCHAR(255),
    163165        detached INTEGER NOT NULL DEFAULT 0,
     166        detached_internal_msgid VARCHAR(255),
    164167        relay_detached INTEGER NOT NULL DEFAULT 0,
    165168        reattach_on INTEGER NOT NULL DEFAULT 0,
     
    246249                );
    247250        `,
     251        "ALTER TABLE Channel ADD COLUMN detached_internal_msgid VARCHAR(255)",
    248252}
    249253
     
    547551        defer db.lock.RUnlock()
    548552
    549         rows, err := db.db.Query(`SELECT id, name, key, detached, relay_detached, reattach_on, detach_after, detach_on
     553        rows, err := db.db.Query(`SELECT
     554                        id, name, key, detached, detached_internal_msgid,
     555                        relay_detached, reattach_on, detach_after, detach_on
    550556                FROM Channel
    551557                WHERE network = ?`, networkID)
     
    558564        for rows.Next() {
    559565                var ch Channel
    560                 var key sql.NullString
     566                var key, detachedInternalMsgID sql.NullString
    561567                var detachAfter int64
    562                 if err := rows.Scan(&ch.ID, &ch.Name, &key, &ch.Detached, &ch.RelayDetached, &ch.ReattachOn, &detachAfter, &ch.DetachOn); err != nil {
     568                if err := rows.Scan(&ch.ID, &ch.Name, &key, &ch.Detached, &detachedInternalMsgID, &ch.RelayDetached, &ch.ReattachOn, &detachAfter, &ch.DetachOn); err != nil {
    563569                        return nil, err
    564570                }
    565571                ch.Key = key.String
     572                ch.DetachedInternalMsgID = detachedInternalMsgID.String
    566573                ch.DetachAfter = time.Duration(detachAfter) * time.Second
    567574                channels = append(channels, ch)
     
    584591        if ch.ID != 0 {
    585592                _, err = db.db.Exec(`UPDATE Channel
    586                         SET network = ?, name = ?, key = ?, detached = ?, relay_detached = ?, reattach_on = ?, detach_after = ?, detach_on = ?
     593                        SET network = ?, name = ?, key = ?, detached = ?, detached_internal_msgid = ?, relay_detached = ?, reattach_on = ?, detach_after = ?, detach_on = ?
    587594                        WHERE id = ?`,
    588                         networkID, ch.Name, key, ch.Detached, ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn, ch.ID)
     595                        networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID), ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn, ch.ID)
    589596        } else {
    590597                var res sql.Result
    591                 res, err = db.db.Exec(`INSERT INTO Channel(network, name, key, detached, relay_detached, reattach_on, detach_after, detach_on)
     598                res, err = db.db.Exec(`INSERT INTO Channel(network, name, key, detached, detached_internal_msgid, relay_detached, reattach_on, detach_after, detach_on)
    592599                        VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
    593                         networkID, ch.Name, key, ch.Detached, ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn)
     600                        networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID), ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn)
    594601                if err != nil {
    595602                        return err
Note: See TracChangeset for help on using the changeset viewer.