-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bridgev2: automatically update old portals when enabling split portals
- Loading branch information
Showing
9 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2024 Tulir Asokan | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package database | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
|
||
"github.com/rs/zerolog" | ||
"go.mau.fi/util/dbutil" | ||
|
||
"maunium.net/go/mautrix/bridgev2/networkid" | ||
) | ||
|
||
type Key string | ||
|
||
const ( | ||
KeySplitPortalsEnabled Key = "split_portals_enabled" | ||
) | ||
|
||
type KVQuery struct { | ||
BridgeID networkid.BridgeID | ||
*dbutil.Database | ||
} | ||
|
||
const ( | ||
getKVQuery = `SELECT value FROM kv_store WHERE bridge_id = $1 AND key = $2` | ||
setKVQuery = ` | ||
INSERT INTO kv_store (bridge_id, key, value) VALUES ($1, $2, $3) | ||
ON CONFLICT (bridge_id, key) DO UPDATE SET value = $3 | ||
` | ||
) | ||
|
||
func (kvq *KVQuery) Get(ctx context.Context, key Key) string { | ||
var value string | ||
err := kvq.QueryRow(ctx, getKVQuery, kvq.BridgeID, key).Scan(&value) | ||
if err != nil && !errors.Is(err, sql.ErrNoRows) { | ||
zerolog.Ctx(ctx).Err(err).Str("key", string(key)).Msg("Failed to get key from kvstore") | ||
} | ||
return value | ||
} | ||
|
||
func (kvq *KVQuery) Set(ctx context.Context, key Key, value string) { | ||
_, err := kvq.Exec(ctx, setKVQuery, kvq.BridgeID, key, value) | ||
if err != nil { | ||
zerolog.Ctx(ctx).Err(err). | ||
Str("key", string(key)). | ||
Str("value", value). | ||
Msg("Failed to set key in kvstore") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- v18 (compatible with v9+): Add generic key-value store | ||
CREATE TABLE kv_store ( | ||
bridge_id TEXT NOT NULL, | ||
key TEXT NOT NULL, | ||
value TEXT NOT NULL, | ||
|
||
PRIMARY KEY (bridge_id, key) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters