aboutsummaryrefslogtreecommitdiff
path: root/internal/sqlutil
diff options
context:
space:
mode:
authorTill <2353100+S7evinK@users.noreply.github.com>2023-03-17 12:09:45 +0100
committerGitHub <noreply@github.com>2023-03-17 11:09:45 +0000
commit5579121c6f27105342a2aea05cf9a3119d73cecb (patch)
tree1d8b7bec90079b6f693585d306c19019ea426870 /internal/sqlutil
parentd88f71ab71a60348518f7fa6735ac9f0bfb472c3 (diff)
Preparations for removing `BaseDendrite` (#3016)
Preparations to actually remove/replace `BaseDendrite`. Quite a few changes: - SyncAPI accepts an `fulltext.Indexer` interface (fulltext is removed from `BaseDendrite`) - Caches are removed from `BaseDendrite` - Introduces a `Router` struct (likely to change) - also fixes #2903 - Introduces a `sqlutil.ConnectionManager`, which should remove `base.DatabaseConnection` later on - probably more
Diffstat (limited to 'internal/sqlutil')
-rw-r--r--internal/sqlutil/connection_manager.go54
-rw-r--r--internal/sqlutil/connection_manager_test.go56
2 files changed, 110 insertions, 0 deletions
diff --git a/internal/sqlutil/connection_manager.go b/internal/sqlutil/connection_manager.go
new file mode 100644
index 00000000..cefd9f80
--- /dev/null
+++ b/internal/sqlutil/connection_manager.go
@@ -0,0 +1,54 @@
+// Copyright 2023 The Matrix.org Foundation C.I.C.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package sqlutil
+
+import (
+ "database/sql"
+ "fmt"
+
+ "github.com/matrix-org/dendrite/setup/config"
+)
+
+type Connections struct {
+ db *sql.DB
+ writer Writer
+}
+
+func NewConnectionManager() Connections {
+ return Connections{}
+}
+
+func (c *Connections) Connection(dbProperties *config.DatabaseOptions) (*sql.DB, Writer, error) {
+ writer := NewDummyWriter()
+ if dbProperties.ConnectionString.IsSQLite() {
+ writer = NewExclusiveWriter()
+ }
+ if dbProperties.ConnectionString != "" || c.db == nil {
+ var err error
+ // Open a new database connection using the supplied config.
+ c.db, err = Open(dbProperties, writer)
+ if err != nil {
+ return nil, nil, err
+ }
+ c.writer = writer
+ return c.db, c.writer, nil
+ }
+ if c.db != nil && c.writer != nil {
+ // Ignore the supplied config and return the global pool and
+ // writer.
+ return c.db, c.writer, nil
+ }
+ return nil, nil, fmt.Errorf("no database connections configured")
+}
diff --git a/internal/sqlutil/connection_manager_test.go b/internal/sqlutil/connection_manager_test.go
new file mode 100644
index 00000000..610629d5
--- /dev/null
+++ b/internal/sqlutil/connection_manager_test.go
@@ -0,0 +1,56 @@
+package sqlutil_test
+
+import (
+ "reflect"
+ "testing"
+
+ "github.com/matrix-org/dendrite/internal/sqlutil"
+ "github.com/matrix-org/dendrite/setup/config"
+ "github.com/matrix-org/dendrite/test"
+)
+
+func TestConnectionManager(t *testing.T) {
+ test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
+ conStr, close := test.PrepareDBConnectionString(t, dbType)
+ t.Cleanup(close)
+ cm := sqlutil.NewConnectionManager()
+
+ dbProps := &config.DatabaseOptions{ConnectionString: config.DataSource(string(conStr))}
+ db, writer, err := cm.Connection(dbProps)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ switch dbType {
+ case test.DBTypeSQLite:
+ _, ok := writer.(*sqlutil.ExclusiveWriter)
+ if !ok {
+ t.Fatalf("expected exclusive writer")
+ }
+ case test.DBTypePostgres:
+ _, ok := writer.(*sqlutil.DummyWriter)
+ if !ok {
+ t.Fatalf("expected dummy writer")
+ }
+ }
+
+ // test global db pool
+ dbGlobal, writerGlobal, err := cm.Connection(&config.DatabaseOptions{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(db, dbGlobal) {
+ t.Fatalf("expected database connection to be reused")
+ }
+ if !reflect.DeepEqual(writer, writerGlobal) {
+ t.Fatalf("expected database writer to be reused")
+ }
+
+ // test invalid connection string configured
+ cm = sqlutil.NewConnectionManager()
+ _, _, err = cm.Connection(&config.DatabaseOptions{ConnectionString: "http://"})
+ if err == nil {
+ t.Fatal("expected an error but got none")
+ }
+ })
+}