Add field last_update to channels

This commit is contained in:
Yaacov Akiba Slama
2021-04-05 18:31:02 +03:00
parent 8ca6ca87fd
commit ee75d838a1
5 changed files with 11 additions and 7 deletions

11
db.go
View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"time"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
@@ -70,13 +71,13 @@ func registerPayment(destination, paymentHash, paymentSecret []byte, incomingAmo
return nil
}
func insertChannel(chanID uint64, channelPoint string, nodeID []byte) error {
func insertChannel(chanID uint64, channelPoint string, nodeID []byte, lastUpdate time.Time) error {
_, err := pgxPool.Exec(context.Background(),
`INSERT INTO
channels (chanid, channel_point, nodeid)
VALUES ($1, $2, $3)
ON CONFLICT (chanid) DO NOTHING`,
chanID, channelPoint, nodeID)
channels (chanid, channel_point, nodeid, last_update)
VALUES ($1, $2, $3, $4)
ON CONFLICT (chanid) DO UPDATE SET last_update=$4`,
chanID, channelPoint, nodeID, lastUpdate)
if err != nil {
return fmt.Errorf("insertChannel(%v, %s, %x) error: %w",
chanID, channelPoint, nodeID, err)

View File

@@ -53,13 +53,14 @@ func channelsSynchronizeOnce() error {
return fmt.Errorf("client.ListChannels() error: %w", err)
}
log.Printf("channelsSynchronizeOnce - received channels")
lastUpdate := time.Now()
for _, c := range channels.Channels {
nodeID, err := hex.DecodeString(c.RemotePubkey)
if err != nil {
log.Printf("hex.DecodeString in channelsSynchronizeOnce error: %v", err)
continue
}
err = insertChannel(c.ChanId, c.ChannelPoint, nodeID)
err = insertChannel(c.ChanId, c.ChannelPoint, nodeID, lastUpdate)
if err != nil {
log.Printf("insertChannel(%v, %v, %x) in channelsSynchronizeOnce error: %v", c.ChanId, c.ChannelPoint, nodeID, err)
continue

View File

@@ -243,7 +243,7 @@ func resumeOrCancel(
OutgoingRequestedChanId: chanID,
OnionBlob: onionBlob,
})
err := insertChannel(chanID, channelPoint, destination)
err := insertChannel(chanID, channelPoint, destination, time.Now())
if err != nil {
log.Printf("insertChannel error: %v", err)
}

View File

@@ -0,0 +1 @@
ALTER TABLE public.channels DROP COLUMN last_update;

View File

@@ -0,0 +1 @@
ALTER TABLE public.channels ADD COLUMN last_update TIMESTAMP;