2020-01-03 15:07:05 +01:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-21 00:40:52 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-01-03 15:07:05 +01:00
|
|
|
package postgres
|
2017-03-29 15:05:43 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2017-12-15 16:42:55 +01:00
|
|
|
|
2017-03-29 15:05:43 +02:00
|
|
|
// Import the postgres database driver.
|
|
|
|
_ "github.com/lib/pq"
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-12-11 15:02:17 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/postgres/deltas"
|
2020-05-13 18:28:42 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/shared"
|
2017-03-29 15:05:43 +02:00
|
|
|
)
|
|
|
|
|
2020-01-23 18:51:10 +01:00
|
|
|
// SyncServerDatasource represents a sync server datasource which manages
|
2019-07-12 16:59:53 +02:00
|
|
|
// both the database for PDUs and caches for EDUs.
|
|
|
|
type SyncServerDatasource struct {
|
2020-05-14 10:53:55 +02:00
|
|
|
shared.Database
|
2020-08-21 11:42:08 +02:00
|
|
|
db *sql.DB
|
|
|
|
writer sqlutil.Writer
|
2017-03-29 15:05:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 17:11:37 +02:00
|
|
|
// NewDatabase creates a new sync server database
|
2020-08-10 15:18:04 +02:00
|
|
|
func NewDatabase(dbProperties *config.DatabaseOptions) (*SyncServerDatasource, error) {
|
2019-07-12 16:59:53 +02:00
|
|
|
var d SyncServerDatasource
|
2017-03-29 15:05:43 +02:00
|
|
|
var err error
|
2020-08-10 15:18:04 +02:00
|
|
|
if d.db, err = sqlutil.Open(dbProperties); err != nil {
|
2017-03-29 15:05:43 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-21 11:42:08 +02:00
|
|
|
d.writer = sqlutil.NewDummyWriter()
|
2020-05-14 10:53:55 +02:00
|
|
|
accountData, err := NewPostgresAccountDataTable(d.db)
|
|
|
|
if err != nil {
|
2017-08-02 17:21:35 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:53:55 +02:00
|
|
|
events, err := NewPostgresEventsTable(d.db)
|
|
|
|
if err != nil {
|
2017-03-30 16:29:23 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 17:11:37 +02:00
|
|
|
currState, err := NewPostgresCurrentRoomStateTable(d.db)
|
|
|
|
if err != nil {
|
2017-04-05 11:30:13 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-13 18:28:42 +02:00
|
|
|
invites, err := NewPostgresInvitesTable(d.db)
|
|
|
|
if err != nil {
|
2017-09-20 16:36:41 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-10 15:39:18 +02:00
|
|
|
peeks, err := NewPostgresPeeksTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 17:11:37 +02:00
|
|
|
topology, err := NewPostgresTopologyTable(d.db)
|
|
|
|
if err != nil {
|
2020-01-23 18:51:10 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 17:11:37 +02:00
|
|
|
backwardExtremities, err := NewPostgresBackwardsExtremitiesTable(d.db)
|
2020-04-28 16:50:24 +02:00
|
|
|
if err != nil {
|
2020-01-23 18:51:10 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 18:50:19 +02:00
|
|
|
sendToDevice, err := NewPostgresSendToDeviceTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-26 16:34:41 +02:00
|
|
|
filter, err := NewPostgresFilterTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-09 19:46:11 +01:00
|
|
|
receipts, err := NewPostgresReceiptsTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-20 21:43:20 +01:00
|
|
|
memberships, err := NewPostgresMembershipsTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-03 12:40:53 +01:00
|
|
|
notificationData, err := NewPostgresNotificationDataTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-07 16:08:19 +02:00
|
|
|
ignores, err := NewPostgresIgnoresTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-06 13:11:19 +02:00
|
|
|
presence, err := NewPostgresPresenceTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-11 15:02:17 +01:00
|
|
|
m := sqlutil.NewMigrations()
|
|
|
|
deltas.LoadFixSequences(m)
|
2021-01-13 18:29:46 +01:00
|
|
|
deltas.LoadRemoveSendToDeviceSentColumn(m)
|
2020-12-11 15:02:17 +01:00
|
|
|
if err = m.RunDeltas(d.db, dbProperties); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:53:55 +02:00
|
|
|
d.Database = shared.Database{
|
2020-05-14 17:11:37 +02:00
|
|
|
DB: d.db,
|
2020-09-01 17:58:21 +02:00
|
|
|
Writer: d.writer,
|
2020-05-14 17:11:37 +02:00
|
|
|
Invites: invites,
|
2020-09-10 15:39:18 +02:00
|
|
|
Peeks: peeks,
|
2020-05-14 17:11:37 +02:00
|
|
|
AccountData: accountData,
|
|
|
|
OutputEvents: events,
|
|
|
|
Topology: topology,
|
|
|
|
CurrentRoomState: currState,
|
|
|
|
BackwardExtremities: backwardExtremities,
|
2020-06-26 16:34:41 +02:00
|
|
|
Filter: filter,
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 18:50:19 +02:00
|
|
|
SendToDevice: sendToDevice,
|
2020-11-09 19:46:11 +01:00
|
|
|
Receipts: receipts,
|
2021-01-20 21:43:20 +01:00
|
|
|
Memberships: memberships,
|
2022-03-03 12:40:53 +01:00
|
|
|
NotificationData: notificationData,
|
2022-04-07 16:08:19 +02:00
|
|
|
Ignores: ignores,
|
2022-04-06 13:11:19 +02:00
|
|
|
Presence: presence,
|
2020-05-13 18:28:42 +02:00
|
|
|
}
|
2017-09-20 16:36:41 +02:00
|
|
|
return &d, nil
|
2017-03-30 16:29:23 +02:00
|
|
|
}
|