make load_or_create *only* load_or_create

Extracted the other logic to its current singular callsite for now.

The load_or_create function finally does nothing other than load or
create the database (and do some related checks, which is fine). This
paves the way for more/better database surgery tooling.
This commit is contained in:
Charles Hall 2024-09-24 17:44:33 -07:00
parent e9caf228b3
commit c2c6083277
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
3 changed files with 56 additions and 52 deletions

View file

@ -40,11 +40,14 @@ use crate::{
ruma_wrapper::{Ar, Ra},
server_server, well_known,
},
config,
config::{Config, ListenComponent, ListenTransport},
config::{self, Config, ListenComponent, ListenTransport},
database::KeyValueDatabase,
error, observability, services, utils,
utils::error::{Error, Result},
error, observability, services,
utils::{
self,
error::{Error, Result},
},
Services, SERVICES,
};
pub(crate) async fn run(args: ServeArgs) -> Result<(), error::ServeCommand> {
@ -70,8 +73,27 @@ pub(crate) async fn run(args: ServeArgs) -> Result<(), error::ServeCommand> {
.expect("should be able to increase the soft limit to the hard limit");
info!("Loading database");
let db = KeyValueDatabase::load_or_create(config, reload_handles)
.map_err(Error::DatabaseError)?;
let db = Box::leak(Box::new(
KeyValueDatabase::load_or_create(&config)
.map_err(Error::DatabaseError)?,
));
// This is the first and only time we initialize the SERVICE static
*SERVICES.write().unwrap() = Some(Box::leak(Box::new(
Services::build(db, config, reload_handles)
.map_err(Error::InitializeServices)?,
)));
// Matrix resource ownership is based on the server name; changing it
// requires recreating the database from scratch. This check needs to be
// done before background tasks are started to avoid data races.
if services().users.count().map(|x| x > 0).map_err(Error::NonZeroUsers)? {
let admin_bot = services().globals.admin_bot_user_id.as_ref();
if !services().users.exists(admin_bot).map_err(Error::AdminBotExists)? {
return Err(Error::Renamed);
}
}
db.apply_migrations().await.map_err(Error::DatabaseError)?;
info!("Starting background tasks");