Fix issues discovered by UT

This commit is contained in:
Till Faelligen 2022-10-27 09:19:11 +02:00
parent 8408b788c8
commit 4378b529e3
No known key found for this signature in database
GPG key ID: 3DF82D8AB9211D4E
5 changed files with 27 additions and 21 deletions

View file

@ -21,11 +21,11 @@ import (
) )
func UpPulishedAppservice(ctx context.Context, tx *sql.Tx) error { func UpPulishedAppservice(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `ALTER TABLE roomserver_published ADD COLUMN IF NOT EXISTS appservice_id TEXT;`) _, err := tx.ExecContext(ctx, `ALTER TABLE roomserver_published ADD COLUMN IF NOT EXISTS appservice_id TEXT NOT NULL;`)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute upgrade: %w", err) return fmt.Errorf("failed to execute upgrade: %w", err)
} }
_, err = tx.ExecContext(ctx, `ALTER TABLE roomserver_published ADD COLUMN IF NOT EXISTS network_id TEXT;`) _, err = tx.ExecContext(ctx, `ALTER TABLE roomserver_published ADD COLUMN IF NOT EXISTS network_id TEXT NOT NULL;`)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute upgrade: %w", err) return fmt.Errorf("failed to execute upgrade: %w", err)
} }

View file

@ -28,19 +28,20 @@ const publishedSchema = `
-- Stores which rooms are published in the room directory -- Stores which rooms are published in the room directory
CREATE TABLE IF NOT EXISTS roomserver_published ( CREATE TABLE IF NOT EXISTS roomserver_published (
-- The room ID of the room -- The room ID of the room
room_id TEXT NOT NULL PRIMARY KEY, room_id TEXT NOT NULL,
-- The appservice ID of the room -- The appservice ID of the room
appservice_id TEXT, appservice_id TEXT NOT NULL,
-- The network_id of the room -- The network_id of the room
network_id TEXT, network_id TEXT NOT NULL,
-- Whether it is published or not -- Whether it is published or not
published BOOLEAN NOT NULL DEFAULT false published BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (room_id, appservice_id, network_id)
); );
` `
const upsertPublishedSQL = "" + const upsertPublishedSQL = "" +
"INSERT INTO roomserver_published (room_id, published) VALUES ($1, $2) " + "INSERT INTO roomserver_published (room_id, appservice_id, network_id, published) VALUES ($1, $2, $3, $4) " +
"ON CONFLICT (room_id) DO UPDATE SET published=$2" "ON CONFLICT (room_id, appservice_id, network_id) DO UPDATE SET published=$4"
const selectAllPublishedSQL = "" + const selectAllPublishedSQL = "" +
"SELECT room_id FROM roomserver_published WHERE published = $1 ORDER BY room_id ASC" "SELECT room_id FROM roomserver_published WHERE published = $1 ORDER BY room_id ASC"
@ -81,7 +82,7 @@ func (s *publishedStatements) UpsertRoomPublished(
ctx context.Context, txn *sql.Tx, roomID string, published bool, ctx context.Context, txn *sql.Tx, roomID string, published bool,
) (err error) { ) (err error) {
stmt := sqlutil.TxStmt(txn, s.upsertPublishedStmt) stmt := sqlutil.TxStmt(txn, s.upsertPublishedStmt)
_, err = stmt.ExecContext(ctx, roomID, published) _, err = stmt.ExecContext(ctx, roomID, "", "", published)
return return
} }

View file

@ -23,10 +23,11 @@ import (
func UpPulishedAppservice(ctx context.Context, tx *sql.Tx) error { func UpPulishedAppservice(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, ` ALTER TABLE roomserver_published RENAME TO roomserver_published_tmp; _, err := tx.ExecContext(ctx, ` ALTER TABLE roomserver_published RENAME TO roomserver_published_tmp;
CREATE TABLE IF NOT EXISTS roomserver_published ( CREATE TABLE IF NOT EXISTS roomserver_published (
room_id TEXT NOT NULL PRIMARY KEY, room_id TEXT NOT NULL,
appservice_id TEXT, appservice_id TEXT NOT NULL,
network_id TEXT, network_id TEXT NOT NULL,
published BOOLEAN NOT NULL DEFAULT false published BOOLEAN NOT NULL DEFAULT false,
CONSTRAINT unique_published_idx PRIMARY KEY (room_id, appservice_id, network_id)
); );
INSERT INSERT
INTO roomserver_published ( INTO roomserver_published (

View file

@ -28,18 +28,20 @@ const publishedSchema = `
-- Stores which rooms are published in the room directory -- Stores which rooms are published in the room directory
CREATE TABLE IF NOT EXISTS roomserver_published ( CREATE TABLE IF NOT EXISTS roomserver_published (
-- The room ID of the room -- The room ID of the room
room_id TEXT NOT NULL PRIMARY KEY, room_id TEXT NOT NULL,
-- The appservice ID of the room -- The appservice ID of the room
appservice_id TEXT, appservice_id TEXT NOT NULL,
-- The network_id of the room -- The network_id of the room
network_id TEXT, network_id TEXT NOT NULL,
-- Whether it is published or not -- Whether it is published or not
published BOOLEAN NOT NULL DEFAULT false published BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (room_id, appservice_id, network_id)
); );
` `
const upsertPublishedSQL = "" + const upsertPublishedSQL = "" +
"INSERT OR REPLACE INTO roomserver_published (room_id, published) VALUES ($1, $2)" "INSERT INTO roomserver_published (room_id, appservice_id, network_id, published) VALUES ($1, $2, $3, $4)" +
" ON CONFLICT (room_id, appservice_id, network_id) DO UPDATE SET published = $4"
const selectAllPublishedSQL = "" + const selectAllPublishedSQL = "" +
"SELECT room_id FROM roomserver_published WHERE published = $1 ORDER BY room_id ASC" "SELECT room_id FROM roomserver_published WHERE published = $1 ORDER BY room_id ASC"
@ -83,7 +85,7 @@ func (s *publishedStatements) UpsertRoomPublished(
ctx context.Context, txn *sql.Tx, roomID string, published bool, ctx context.Context, txn *sql.Tx, roomID string, published bool,
) error { ) error {
stmt := sqlutil.TxStmt(txn, s.upsertPublishedStmt) stmt := sqlutil.TxStmt(txn, s.upsertPublishedStmt)
_, err := stmt.ExecContext(ctx, roomID, published) _, err := stmt.ExecContext(ctx, roomID, "", "", published)
return err return err
} }

View file

@ -2,16 +2,18 @@ package tables_test
import ( import (
"context" "context"
"fmt"
"sort" "sort"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/roomserver/storage/postgres" "github.com/matrix-org/dendrite/roomserver/storage/postgres"
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3" "github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
"github.com/matrix-org/dendrite/roomserver/storage/tables" "github.com/matrix-org/dendrite/roomserver/storage/tables"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/test" "github.com/matrix-org/dendrite/test"
"github.com/stretchr/testify/assert"
) )
func mustCreatePublishedTable(t *testing.T, dbType test.DBType) (tab tables.Published, close func()) { func mustCreatePublishedTable(t *testing.T, dbType test.DBType) (tab tables.Published, close func()) {
@ -74,6 +76,6 @@ func TestPublishedTable(t *testing.T) {
// should now be false, due to the upsert // should now be false, due to the upsert
publishedRes, err := tab.SelectPublishedFromRoomID(ctx, nil, room.ID) publishedRes, err := tab.SelectPublishedFromRoomID(ctx, nil, room.ID)
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, publishedRes) assert.False(t, publishedRes, fmt.Sprintf("expected room %s to be unpublished", room.ID))
}) })
} }