mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Switch to async Rust and break all logic.
This commit is contained in:
@@ -5,21 +5,32 @@ use log::info;
|
||||
use mongodb::bson::doc;
|
||||
use mongodb::options::CreateCollectionOptions;
|
||||
|
||||
pub fn create_database() {
|
||||
pub async fn create_database() {
|
||||
info!("Creating database.");
|
||||
let db = get_db();
|
||||
|
||||
db.create_collection("users", None)
|
||||
.await
|
||||
.expect("Failed to create users collection.");
|
||||
|
||||
db.create_collection("channels", None)
|
||||
.await
|
||||
.expect("Failed to create channels collection.");
|
||||
|
||||
db.create_collection("guilds", None)
|
||||
.await
|
||||
.expect("Failed to create guilds collection.");
|
||||
|
||||
db.create_collection("members", None)
|
||||
.await
|
||||
.expect("Failed to create members collection.");
|
||||
|
||||
db.create_collection("messages", None)
|
||||
.await
|
||||
.expect("Failed to create messages collection.");
|
||||
|
||||
db.create_collection("migrations", None)
|
||||
.await
|
||||
.expect("Failed to create migrations collection.");
|
||||
|
||||
db.create_collection(
|
||||
@@ -29,7 +40,8 @@ pub fn create_database() {
|
||||
.size(1_000_000)
|
||||
.build(),
|
||||
)
|
||||
.expect("Failed to create pubsub collection.");
|
||||
.await
|
||||
.expect("Failed to create pubsub collection.");
|
||||
|
||||
db.collection("migrations")
|
||||
.insert_one(
|
||||
@@ -39,6 +51,7 @@ pub fn create_database() {
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("Failed to save migration info.");
|
||||
|
||||
info!("Created database.");
|
||||
|
||||
@@ -3,16 +3,17 @@ use super::get_connection;
|
||||
pub mod init;
|
||||
pub mod scripts;
|
||||
|
||||
pub fn run_migrations() {
|
||||
pub async fn run_migrations() {
|
||||
let client = get_connection();
|
||||
|
||||
let list = client
|
||||
.list_database_names(None, None)
|
||||
.await
|
||||
.expect("Failed to fetch database names.");
|
||||
|
||||
if list.iter().position(|x| x == "revolt").is_none() {
|
||||
init::create_database();
|
||||
init::create_database().await;
|
||||
} else {
|
||||
scripts::migrate_database();
|
||||
scripts::migrate_database().await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use super::super::get_collection;
|
||||
|
||||
use log::info;
|
||||
use mongodb::bson::{doc, from_bson, Bson};
|
||||
use mongodb::options::FindOptions;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::rocket::futures::StreamExt;
|
||||
use mongodb::bson::{doc, from_bson, Bson};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct MigrationInfo {
|
||||
@@ -13,17 +14,18 @@ struct MigrationInfo {
|
||||
|
||||
pub const LATEST_REVISION: i32 = 2;
|
||||
|
||||
pub fn migrate_database() {
|
||||
pub async fn migrate_database() {
|
||||
let migrations = get_collection("migrations");
|
||||
let data = migrations
|
||||
.find_one(None, None)
|
||||
.await
|
||||
.expect("Failed to fetch migration data.");
|
||||
|
||||
if let Some(doc) = data {
|
||||
let info: MigrationInfo =
|
||||
from_bson(Bson::Document(doc)).expect("Failed to read migration information.");
|
||||
|
||||
let revision = run_migrations(info.revision);
|
||||
let revision = run_migrations(info.revision).await;
|
||||
|
||||
migrations
|
||||
.update_one(
|
||||
@@ -37,6 +39,7 @@ pub fn migrate_database() {
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("Failed to commit migration information.");
|
||||
|
||||
info!("Migration complete. Currently at revision {}.", revision);
|
||||
@@ -45,7 +48,7 @@ pub fn migrate_database() {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_migrations(revision: i32) -> i32 {
|
||||
pub async fn run_migrations(revision: i32) -> i32 {
|
||||
info!("Starting database migration.");
|
||||
|
||||
if revision <= 0 {
|
||||
@@ -56,14 +59,15 @@ pub fn run_migrations(revision: i32) -> i32 {
|
||||
info!("Running migration [revision 1]: Add channels to guild object.");
|
||||
|
||||
let col = get_collection("guilds");
|
||||
let guilds = col
|
||||
let mut guilds = col
|
||||
.find(
|
||||
None,
|
||||
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
|
||||
)
|
||||
.await
|
||||
.expect("Failed to fetch guilds.");
|
||||
|
||||
let result = get_collection("channels")
|
||||
let mut result = get_collection("channels")
|
||||
.find(
|
||||
doc! {
|
||||
"type": 2
|
||||
@@ -72,15 +76,17 @@ pub fn run_migrations(revision: i32) -> i32 {
|
||||
.projection(doc! { "_id": 1, "guild": 1 })
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.expect("Failed to fetch channels.");
|
||||
|
||||
let mut channels = vec![];
|
||||
for doc in result {
|
||||
while let Some(doc) = result.next().await {
|
||||
let channel = doc.expect("Failed to fetch channel.");
|
||||
let id = channel
|
||||
.get_str("_id")
|
||||
.expect("Failed to get channel id.")
|
||||
.to_string();
|
||||
|
||||
let gid = channel
|
||||
.get_str("guild")
|
||||
.expect("Failed to get guild id.")
|
||||
@@ -89,7 +95,7 @@ pub fn run_migrations(revision: i32) -> i32 {
|
||||
channels.push((id, gid));
|
||||
}
|
||||
|
||||
for doc in guilds {
|
||||
while let Some(doc) = guilds.next().await {
|
||||
let guild = doc.expect("Failed to fetch guild.");
|
||||
let id = guild.get_str("_id").expect("Failed to get guild id.");
|
||||
|
||||
@@ -110,6 +116,7 @@ pub fn run_migrations(revision: i32) -> i32 {
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("Failed to update guild.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user