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 postgres
|
|
|
|
|
|
|
|
import (
|
2021-07-27 17:08:53 +01:00
|
|
|
"fmt"
|
2021-04-07 13:26:20 +01:00
|
|
|
"time"
|
2020-02-13 17:27:33 +00:00
|
|
|
|
2022-02-16 17:55:38 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
|
2020-04-16 10:06:55 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-02-18 11:31:05 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/postgres/deltas"
|
2022-02-18 13:51:59 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/shared"
|
2020-02-13 17:27:33 +00:00
|
|
|
|
|
|
|
// Import the postgres database driver.
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewDatabase creates a new accounts and profiles database
|
2022-02-18 13:51:59 +00:00
|
|
|
func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserverlib.ServerName, bcryptCost int, openIDTokenLifetimeMS int64, loginTokenLifetime time.Duration) (*shared.Database, error) {
|
2020-08-10 14:18:04 +01:00
|
|
|
db, err := sqlutil.Open(dbProperties)
|
|
|
|
if err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-15 18:09:41 +01:00
|
|
|
|
2022-02-18 13:51:59 +00:00
|
|
|
m := sqlutil.NewMigrations()
|
|
|
|
if _, err = db.Exec(accountsSchema); err != nil {
|
|
|
|
// do this so that the migration can and we don't fail on
|
|
|
|
// preparing statements for columns that don't exist yet
|
2020-10-15 18:09:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
deltas.LoadIsActive(m)
|
2022-02-18 11:31:05 +00:00
|
|
|
//deltas.LoadLastSeenTSIP(m)
|
2022-02-16 17:55:38 +00:00
|
|
|
deltas.LoadAddAccountType(m)
|
2020-10-15 18:09:41 +01:00
|
|
|
if err = m.RunDeltas(db, dbProperties); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-18 13:51:59 +00:00
|
|
|
accountDataTable, err := NewPostgresAccountDataTable(db)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewPostgresAccountDataTable: %w", err)
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
accountsTable, err := NewPostgresAccountsTable(db, serverName)
|
2020-09-04 15:16:13 +01:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewPostgresAccountsTable: %w", err)
|
2021-03-02 10:43:25 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
devicesTable, err := NewPostgresDevicesTable(db, serverName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresDevicesTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
keyBackupTable, err := NewPostgresKeyBackupTable(db)
|
2022-02-18 11:31:05 +00:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewPostgresKeyBackupTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
keyBackupVersionTable, err := NewPostgresKeyBackupVersionTable(db)
|
2022-02-18 11:31:05 +00:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewPostgresKeyBackupVersionTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
loginTokenTable, err := NewPostgresLoginTokenTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresLoginTokenTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
openIDTable, err := NewPostgresOpenIDTable(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("NewPostgresOpenIDTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
profilesTable, err := NewPostgresProfilesTable(db)
|
2022-02-18 11:31:05 +00:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, fmt.Errorf("NewPostgresProfilesTable: %w", err)
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
threePIDTable, err := NewPostgresThreePIDTable(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NewPostgresThreePIDTable: %w", err)
|
|
|
|
}
|
|
|
|
return &shared.Database{
|
|
|
|
AccountDatas: accountDataTable,
|
|
|
|
Accounts: accountsTable,
|
|
|
|
Devices: devicesTable,
|
|
|
|
KeyBackups: keyBackupTable,
|
|
|
|
KeyBackupVersions: keyBackupVersionTable,
|
|
|
|
LoginTokens: loginTokenTable,
|
|
|
|
OpenIDTokens: openIDTable,
|
|
|
|
Profiles: profilesTable,
|
|
|
|
ThreePIDs: threePIDTable,
|
|
|
|
ServerName: serverName,
|
|
|
|
DB: db,
|
|
|
|
Writer: sqlutil.NewDummyWriter(),
|
|
|
|
LoginTokenLifetime: loginTokenLifetime,
|
|
|
|
BcryptCost: bcryptCost,
|
|
|
|
OpenIDTokenLifetimeMS: openIDTokenLifetimeMS,
|
|
|
|
}, nil
|
2022-02-18 11:31:05 +00:00
|
|
|
}
|