aboutsummaryrefslogtreecommitdiff
path: root/currentstateserver/storage/postgres/storage.go
blob: 0cd7e5553490eb9466bc8219afb80d71f3d9d525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package postgres

import (
	"database/sql"

	"github.com/matrix-org/dendrite/currentstateserver/storage/shared"
	"github.com/matrix-org/dendrite/internal/config"
	"github.com/matrix-org/dendrite/internal/sqlutil"
)

type Database struct {
	shared.Database
	db *sql.DB
	sqlutil.PartitionOffsetStatements
}

// NewDatabase creates a new sync server database
func NewDatabase(dbProperties *config.DatabaseOptions) (*Database, error) {
	var d Database
	var err error
	if d.db, err = sqlutil.Open(dbProperties); err != nil {
		return nil, err
	}
	if err = d.PartitionOffsetStatements.Prepare(d.db, "currentstate"); err != nil {
		return nil, err
	}
	currRoomState, err := NewPostgresCurrentRoomStateTable(d.db)
	if err != nil {
		return nil, err
	}
	d.Database = shared.Database{
		DB:               d.db,
		CurrentRoomState: currRoomState,
	}
	return &d, nil
}