2020-02-13 11:27:33 -06:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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 postgres
|
|
|
|
|
|
|
|
import (
|
2022-11-11 10:41:37 -06:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
2021-07-27 11:08:53 -05:00
|
|
|
"fmt"
|
2021-04-07 07:26:20 -05:00
|
|
|
"time"
|
2020-02-13 11:27:33 -06:00
|
|
|
|
2022-02-16 11:55:38 -06:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
|
2020-04-16 04:06:55 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-10-18 09:59:08 -05:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/postgres/deltas"
|
2022-02-18 07:51:59 -06:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/shared"
|
2020-02-13 11:27:33 -06:00
|
|
|
|
|
|
|
// Import the postgres database driver.
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewDatabase creates a new accounts and profiles database
|
2023-03-17 06:09:45 -05:00
|
|
|
func NewDatabase(ctx context.Context, conMan sqlutil.Connections, dbProperties *config.DatabaseOptions, serverName gomatrixserverlib.ServerName, bcryptCost int, openIDTokenLifetimeMS int64, loginTokenLifetime time.Duration, serverNoticesLocalpart string) (*shared.Database, error) {
|
|
|
|
db, writer, err := conMan.Connection(dbProperties)
|
2020-08-10 08:18:04 -05:00
|
|
|
if err != nil {
|
2020-02-13 11:27:33 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-15 12:09:41 -05:00
|
|
|
|
2022-10-18 09:59:08 -05:00
|
|
|
m := sqlutil.NewMigrator(db)
|
|
|
|
m.AddMigrations(sqlutil.Migration{
|
|
|
|
Version: "userapi: rename tables",
|
|
|
|
Up: deltas.UpRenameTables,
|
|
|
|
Down: deltas.DownRenameTables,
|
|
|
|
})
|
2022-11-11 10:41:37 -06:00
|
|
|
m.AddMigrations(sqlutil.Migration{
|
|
|
|
Version: "userapi: server names",
|
|
|
|
Up: func(ctx context.Context, txn *sql.Tx) error {
|
|
|
|
return deltas.UpServerNames(ctx, txn, serverName)
|
|
|
|
},
|
|
|
|
})
|
2023-03-17 06:09:45 -05:00
|
|
|
if err = m.Up(ctx); err != nil {
|
2022-10-18 09:59:08 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-18 07:51:59 -06:00
|
|
|
accountsTable, err := NewPostgresAccountsTable(db, serverName)
|
2020-09-04 09:16:13 -05:00
|
|
|
if err != nil {
|
2022-02-18 07:51:59 -06:00
|
|
|
return nil, fmt.Errorf("NewPostgresAccountsTable: %w", err)
|
2021-03-02 04:43:25 -06:00
|
|
|
}
|
2022-11-11 10:41:37 -06:00
|
|
|
accountDataTable, err := NewPostgresAccountDataTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresAccountDataTable: %w", err)
|
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
devicesTable, err := NewPostgresDevicesTable(db, serverName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresDevicesTable: %w", err)
|
2022-02-18 05:31:05 -06:00
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
keyBackupTable, err := NewPostgresKeyBackupTable(db)
|
2022-02-18 05:31:05 -06:00
|
|
|
if err != nil {
|
2022-02-18 07:51:59 -06:00
|
|
|
return nil, fmt.Errorf("NewPostgresKeyBackupTable: %w", err)
|
2022-02-18 05:31:05 -06:00
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
keyBackupVersionTable, err := NewPostgresKeyBackupVersionTable(db)
|
2022-02-18 05:31:05 -06:00
|
|
|
if err != nil {
|
2022-02-18 07:51:59 -06:00
|
|
|
return nil, fmt.Errorf("NewPostgresKeyBackupVersionTable: %w", err)
|
2022-02-18 05:31:05 -06:00
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
loginTokenTable, err := NewPostgresLoginTokenTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresLoginTokenTable: %w", err)
|
2022-02-18 05:31:05 -06:00
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
openIDTable, err := NewPostgresOpenIDTable(db, serverName)
|
2022-02-18 05:31:05 -06:00
|
|
|
if err != nil {
|
2022-02-18 07:51:59 -06:00
|
|
|
return nil, fmt.Errorf("NewPostgresOpenIDTable: %w", err)
|
2022-02-18 05:31:05 -06:00
|
|
|
}
|
2022-03-28 10:25:26 -05:00
|
|
|
profilesTable, err := NewPostgresProfilesTable(db, serverNoticesLocalpart)
|
2022-02-18 05:31:05 -06:00
|
|
|
if err != nil {
|
2022-02-18 07:51:59 -06:00
|
|
|
return nil, fmt.Errorf("NewPostgresProfilesTable: %w", err)
|
2022-02-18 05:31:05 -06:00
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
threePIDTable, err := NewPostgresThreePIDTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresThreePIDTable: %w", err)
|
|
|
|
}
|
2022-03-03 05:40:53 -06:00
|
|
|
pusherTable, err := NewPostgresPusherTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresPusherTable: %w", err)
|
|
|
|
}
|
|
|
|
notificationsTable, err := NewPostgresNotificationTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresNotificationTable: %w", err)
|
|
|
|
}
|
2022-05-04 12:04:28 -05:00
|
|
|
statsTable, err := NewPostgresStatsTable(db, serverName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresStatsTable: %w", err)
|
|
|
|
}
|
2022-11-11 10:41:37 -06:00
|
|
|
|
|
|
|
m = sqlutil.NewMigrator(db)
|
|
|
|
m.AddMigrations(sqlutil.Migration{
|
|
|
|
Version: "userapi: server names populate",
|
|
|
|
Up: func(ctx context.Context, txn *sql.Tx) error {
|
|
|
|
return deltas.UpServerNamesPopulate(ctx, txn, serverName)
|
|
|
|
},
|
|
|
|
})
|
2023-03-17 06:09:45 -05:00
|
|
|
if err = m.Up(ctx); err != nil {
|
2022-11-11 10:41:37 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-18 07:51:59 -06:00
|
|
|
return &shared.Database{
|
|
|
|
AccountDatas: accountDataTable,
|
|
|
|
Accounts: accountsTable,
|
|
|
|
Devices: devicesTable,
|
|
|
|
KeyBackups: keyBackupTable,
|
|
|
|
KeyBackupVersions: keyBackupVersionTable,
|
|
|
|
LoginTokens: loginTokenTable,
|
|
|
|
OpenIDTokens: openIDTable,
|
|
|
|
Profiles: profilesTable,
|
|
|
|
ThreePIDs: threePIDTable,
|
2022-03-03 05:40:53 -06:00
|
|
|
Pushers: pusherTable,
|
|
|
|
Notifications: notificationsTable,
|
2022-05-04 12:04:28 -05:00
|
|
|
Stats: statsTable,
|
2022-02-18 07:51:59 -06:00
|
|
|
ServerName: serverName,
|
|
|
|
DB: db,
|
2022-05-03 10:35:06 -05:00
|
|
|
Writer: writer,
|
2022-02-18 07:51:59 -06:00
|
|
|
LoginTokenLifetime: loginTokenLifetime,
|
|
|
|
BcryptCost: bcryptCost,
|
|
|
|
OpenIDTokenLifetimeMS: openIDTokenLifetimeMS,
|
|
|
|
}, nil
|
2022-02-18 05:31:05 -06:00
|
|
|
}
|
2023-02-20 07:58:03 -06:00
|
|
|
|
2023-03-17 06:09:45 -05:00
|
|
|
func NewKeyDatabase(conMan sqlutil.Connections, dbProperties *config.DatabaseOptions) (*shared.KeyDatabase, error) {
|
|
|
|
db, writer, err := conMan.Connection(dbProperties)
|
2023-02-20 07:58:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
otk, err := NewPostgresOneTimeKeysTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dk, err := NewPostgresDeviceKeysTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
kc, err := NewPostgresKeyChangesTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sdl, err := NewPostgresStaleDeviceListsTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
csk, err := NewPostgresCrossSigningKeysTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
css, err := NewPostgresCrossSigningSigsTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &shared.KeyDatabase{
|
|
|
|
OneTimeKeysTable: otk,
|
|
|
|
DeviceKeysTable: dk,
|
|
|
|
KeyChangesTable: kc,
|
|
|
|
StaleDeviceListsTable: sdl,
|
|
|
|
CrossSigningKeysTable: csk,
|
|
|
|
CrossSigningSigsTable: css,
|
|
|
|
Writer: writer,
|
|
|
|
}, nil
|
|
|
|
}
|