2020-02-13 17:27:33 +00: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 sqlite3
|
|
|
|
|
|
|
|
import (
|
2022-11-11 16:41:37 +00:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
2021-07-27 16:08:53 +00:00
|
|
|
"fmt"
|
2021-04-07 12:26:20 +00:00
|
|
|
"time"
|
2020-02-13 17:27:33 +00:00
|
|
|
|
2020-04-16 09:06:55 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2023-04-19 14:50:33 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-08-21 09:42:08 +00:00
|
|
|
|
2022-02-18 13:51:59 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/shared"
|
2022-10-18 14:59:08 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/sqlite3/deltas"
|
2022-02-18 11:31:05 +00:00
|
|
|
)
|
|
|
|
|
2023-02-20 13:58:03 +00:00
|
|
|
// NewUserDatabase creates a new accounts and profiles database
|
2023-07-19 11:37:04 +00:00
|
|
|
func NewUserDatabase(ctx context.Context, conMan *sqlutil.Connections, dbProperties *config.DatabaseOptions, serverName spec.ServerName, bcryptCost int, openIDTokenLifetimeMS int64, loginTokenLifetime time.Duration, serverNoticesLocalpart string) (*shared.Database, error) {
|
2023-03-17 11:09:45 +00:00
|
|
|
db, writer, err := conMan.Connection(dbProperties)
|
2020-06-04 10:18:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-15 17:09:41 +00:00
|
|
|
|
2022-10-18 14:59:08 +00:00
|
|
|
m := sqlutil.NewMigrator(db)
|
|
|
|
m.AddMigrations(sqlutil.Migration{
|
|
|
|
Version: "userapi: rename tables",
|
|
|
|
Up: deltas.UpRenameTables,
|
|
|
|
Down: deltas.DownRenameTables,
|
|
|
|
})
|
2022-11-11 16:41:37 +00: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 11:09:45 +00:00
|
|
|
if err = m.Up(ctx); err != nil {
|
2022-10-18 14:59:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-22 16:37:21 +00:00
|
|
|
registationTokensTable, err := NewSQLiteRegistrationTokensTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewSQLiteRegistrationsTokenTable: %w", err)
|
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
accountsTable, err := NewSQLiteAccountsTable(db, serverName)
|
2020-09-04 14:16:13 +00:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewSQLiteAccountsTable: %w", err)
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
2022-11-11 16:41:37 +00:00
|
|
|
accountDataTable, err := NewSQLiteAccountDataTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewSQLiteAccountDataTable: %w", err)
|
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
devicesTable, err := NewSQLiteDevicesTable(db, serverName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewSQLiteDevicesTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
keyBackupTable, err := NewSQLiteKeyBackupTable(db)
|
2022-02-18 11:31:05 +00:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewSQLiteKeyBackupTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
keyBackupVersionTable, err := NewSQLiteKeyBackupVersionTable(db)
|
2022-02-18 11:31:05 +00:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewSQLiteKeyBackupVersionTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
loginTokenTable, err := NewSQLiteLoginTokenTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewSQLiteLoginTokenTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
openIDTable, err := NewSQLiteOpenIDTable(db, serverName)
|
2022-02-18 11:31:05 +00:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewSQLiteOpenIDTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-03-28 15:25:26 +00:00
|
|
|
profilesTable, err := NewSQLiteProfilesTable(db, serverNoticesLocalpart)
|
2022-02-18 11:31:05 +00:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewSQLiteProfilesTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
threePIDTable, err := NewSQLiteThreePIDTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewSQLiteThreePIDTable: %w", err)
|
|
|
|
}
|
2022-03-03 11:40:53 +00:00
|
|
|
pusherTable, err := NewSQLitePusherTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresPusherTable: %w", err)
|
|
|
|
}
|
|
|
|
notificationsTable, err := NewSQLiteNotificationTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresNotificationTable: %w", err)
|
|
|
|
}
|
2022-05-04 17:04:28 +00:00
|
|
|
statsTable, err := NewSQLiteStatsTable(db, serverName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewSQLiteStatsTable: %w", err)
|
|
|
|
}
|
2022-11-11 16:41:37 +00: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 11:09:45 +00:00
|
|
|
if err = m.Up(ctx); err != nil {
|
2022-11-11 16:41:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-18 13:51:59 +00: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 11:40:53 +00:00
|
|
|
Pushers: pusherTable,
|
|
|
|
Notifications: notificationsTable,
|
2022-05-04 17:04:28 +00:00
|
|
|
Stats: statsTable,
|
2022-02-18 13:51:59 +00:00
|
|
|
ServerName: serverName,
|
|
|
|
DB: db,
|
2022-05-03 15:35:06 +00:00
|
|
|
Writer: writer,
|
2022-02-18 13:51:59 +00:00
|
|
|
LoginTokenLifetime: loginTokenLifetime,
|
|
|
|
BcryptCost: bcryptCost,
|
|
|
|
OpenIDTokenLifetimeMS: openIDTokenLifetimeMS,
|
2023-06-22 16:37:21 +00:00
|
|
|
RegistrationTokens: registationTokensTable,
|
2022-02-18 13:51:59 +00:00
|
|
|
}, nil
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2023-02-20 13:58:03 +00:00
|
|
|
|
2023-07-19 11:37:04 +00:00
|
|
|
func NewKeyDatabase(conMan *sqlutil.Connections, dbProperties *config.DatabaseOptions) (*shared.KeyDatabase, error) {
|
2023-03-17 11:09:45 +00:00
|
|
|
db, writer, err := conMan.Connection(dbProperties)
|
2023-02-20 13:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
otk, err := NewSqliteOneTimeKeysTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dk, err := NewSqliteDeviceKeysTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
kc, err := NewSqliteKeyChangesTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sdl, err := NewSqliteStaleDeviceListsTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
csk, err := NewSqliteCrossSigningKeysTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
css, err := NewSqliteCrossSigningSigsTable(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
|
|
|
|
}
|