db_postgres: fix constraints errors

Stop using ON CONFLICT DO UPDATE.

Closes: https://todo.sr.ht/~emersion/soju/138
This commit is contained in:
Simon Ser 2021-10-13 10:12:14 +02:00
parent eb41ed4171
commit ca9fa9198c

View File

@ -200,13 +200,21 @@ func (db *PostgresDB) GetUser(username string) (*User, error) {
func (db *PostgresDB) StoreUser(user *User) error { func (db *PostgresDB) StoreUser(user *User) error {
password := toNullString(user.Password) password := toNullString(user.Password)
realname := toNullString(user.Realname) realname := toNullString(user.Realname)
err := db.db.QueryRow(`
INSERT INTO "User" (username, password, admin, realname) var err error
VALUES ($1, $2, $3, $4) if user.ID == 0 {
ON CONFLICT (username) err = db.db.QueryRow(`
DO UPDATE SET password = $2, admin = $3, realname = $4 INSERT INTO "User" (username, password, admin, realname)
RETURNING id`, VALUES ($1, $2, $3, $4)
user.Username, password, user.Admin, realname).Scan(&user.ID) RETURNING id`,
user.Username, password, user.Admin, realname).Scan(&user.ID)
} else {
_, err = db.db.Exec(`
UPDATE "User"
SET password = $1, admin = $2, realname = $3
WHERE id = $4`,
password, user.Admin, realname, user.ID)
}
return err return err
} }
@ -279,20 +287,29 @@ func (db *PostgresDB) StoreNetwork(userID int64, network *Network) error {
} }
} }
err := db.db.QueryRow(` var err error
INSERT INTO "Network" ("user", name, addr, nick, username, realname, pass, connect_commands, if network.ID == 0 {
sasl_mechanism, sasl_plain_username, sasl_plain_password, sasl_external_cert, err = db.db.QueryRow(`
sasl_external_key, enabled) INSERT INTO "Network" ("user", name, addr, nick, username, realname, pass, connect_commands,
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) sasl_mechanism, sasl_plain_username, sasl_plain_password, sasl_external_cert,
ON CONFLICT sasl_external_key, enabled)
DO UPDATE SET name = $2, addr = $3, nick = $4, username = $5, realname = $6, pass = $7, VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
connect_commands = $8, sasl_mechanism = $9, sasl_plain_username = $10, RETURNING id`,
sasl_plain_password = $11, sasl_external_cert = $12, sasl_external_key = $13, userID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands,
enabled = $14 saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob,
RETURNING id`, network.SASL.External.PrivKeyBlob, network.Enabled).Scan(&network.ID)
userID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands, } else {
saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob, _, err = db.db.Exec(`
network.SASL.External.PrivKeyBlob, network.Enabled).Scan(&network.ID) UPDATE "Network"
SET name = $2, addr = $3, nick = $4, username = $5, realname = $6, pass = $7,
connect_commands = $8, sasl_mechanism = $9, sasl_plain_username = $10,
sasl_plain_password = $11, sasl_external_cert = $12, sasl_external_key = $13,
enabled = $14
WHERE id = $1`,
network.ID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands,
saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob,
network.SASL.External.PrivKeyBlob, network.Enabled)
}
return err return err
} }
@ -335,16 +352,25 @@ func (db *PostgresDB) ListChannels(networkID int64) ([]Channel, error) {
func (db *PostgresDB) StoreChannel(networkID int64, ch *Channel) error { func (db *PostgresDB) StoreChannel(networkID int64, ch *Channel) error {
key := toNullString(ch.Key) key := toNullString(ch.Key)
detachAfter := int64(math.Ceil(ch.DetachAfter.Seconds())) detachAfter := int64(math.Ceil(ch.DetachAfter.Seconds()))
err := db.db.QueryRow(`
INSERT INTO "Channel" (network, name, key, detached, detached_internal_msgid, relay_detached, reattach_on, var err error
detach_after, detach_on) if ch.ID == 0 {
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) err = db.db.QueryRow(`
ON CONFLICT (network, name) INSERT INTO "Channel" (network, name, key, detached, detached_internal_msgid, relay_detached, reattach_on,
DO UPDATE SET network = $1, name = $2, key = $3, detached = $4, detached_internal_msgid = $5, detach_after, detach_on)
relay_detached = $6, reattach_on = $7, detach_after = $8, detach_on = $9 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id`, RETURNING id`,
networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID), networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID),
ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn).Scan(&ch.ID) ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn).Scan(&ch.ID)
} else {
_, err = db.db.Exec(`
UPDATE "Channel"
SET name = $2, key = $3, detached = $4, detached_internal_msgid = $5,
relay_detached = $6, reattach_on = $7, detach_after = $8, detach_on = $9
WHERE id = $1`,
ch.ID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID),
ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn)
}
return err return err
} }
@ -379,18 +405,27 @@ func (db *PostgresDB) ListDeliveryReceipts(networkID int64) ([]DeliveryReceipt,
} }
func (db *PostgresDB) StoreClientDeliveryReceipts(networkID int64, client string, receipts []DeliveryReceipt) error { func (db *PostgresDB) StoreClientDeliveryReceipts(networkID int64, client string, receipts []DeliveryReceipt) error {
tx, err := db.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
_, err = tx.Exec("DELETE FROM DeliveryReceipt WHERE network = $1 AND client = $2",
networkID, client)
if err != nil {
return err
}
stmt, err := db.db.Prepare(` stmt, err := db.db.Prepare(`
INSERT INTO "DeliveryReceipt" (network, target, client, internal_msgid) INSERT INTO "DeliveryReceipt" (network, target, client, internal_msgid)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3, $4)
ON CONFLICT (network, target, client)
DO UPDATE SET internal_msgid = $4
RETURNING id`) RETURNING id`)
if err != nil { if err != nil {
return err return err
} }
defer stmt.Close() defer stmt.Close()
// No need for a transaction since all changes are atomic and don't break data coherence.
for i := range receipts { for i := range receipts {
rcpt := &receipts[i] rcpt := &receipts[i]
err := stmt.QueryRow(networkID, rcpt.Target, client, rcpt.InternalMsgID).Scan(&rcpt.ID) err := stmt.QueryRow(networkID, rcpt.Target, client, rcpt.InternalMsgID).Scan(&rcpt.ID)
@ -398,5 +433,6 @@ func (db *PostgresDB) StoreClientDeliveryReceipts(networkID int64, client string
return err return err
} }
} }
return nil
return tx.Commit()
} }