Logging + database migrations system.

This commit is contained in:
Paul Makles
2020-08-11 21:08:01 +02:00
parent 750f8c6738
commit a8eb403280
9 changed files with 157 additions and 12 deletions

View File

@@ -42,6 +42,7 @@ pub struct Guild {
pub description: String,
pub owner: String,
// ? FIXME: ADD: pub channels: Vec<Channel>,
pub invites: Vec<Invite>,
pub bans: Vec<Ban>,

View File

@@ -0,0 +1,39 @@
use super::super::get_db;
use super::scripts::LATEST_REVISION;
use mongodb::options::CreateCollectionOptions;
use mongodb::bson::doc;
use log::info;
pub fn create_database() {
info!("Creating database.");
let db = get_db();
db.create_collection("users", None).expect("Failed to create users collection.");
db.create_collection("channels", None).expect("Failed to create channels collection.");
db.create_collection("guilds", None).expect("Failed to create guilds collection.");
db.create_collection("members", None).expect("Failed to create members collection.");
db.create_collection("messages", None).expect("Failed to create messages collection.");
db.create_collection("migrations", None).expect("Failed to create migrations collection.");
db.create_collection(
"pubsub",
CreateCollectionOptions::builder()
.capped(true)
.size(1_000_000)
.build()
)
.expect("Failed to create pubsub collection.");
db.collection("migrations")
.insert_one(
doc! {
"_id": 0,
"revision": LATEST_REVISION
},
None
)
.expect("Failed to save migration info.");
info!("Created database.");
}

View File

@@ -0,0 +1,19 @@
use super::get_connection;
pub mod init;
pub mod scripts;
pub fn run_migrations() {
let client = get_connection();
let list = client.list_database_names(
None,
None
).expect("Failed to fetch database names.");
if list.iter().position(|x| x == "revolt").is_none() {
init::create_database();
} else {
scripts::migrate_database();
}
}

View File

@@ -0,0 +1,53 @@
use super::super::get_collection;
use serde::{Serialize, Deserialize};
use mongodb::bson::{Bson, from_bson, doc};
use log::info;
#[derive(Serialize, Deserialize)]
struct MigrationInfo {
_id: i32,
revision: i32
}
pub const LATEST_REVISION: i32 = 1;
pub fn migrate_database() {
let migrations = get_collection("migrations");
let data = migrations.find_one(None, None)
.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);
migrations.update_one(
doc! {
"_id": info._id
},
doc! {
"$set": {
"revision": revision
}
},
None
).expect("Failed to commit migration information.");
info!("Migration complete. Currently at revision {}.", revision);
} else {
panic!("Database was configured incorrectly, possibly because initalization failed.")
}
}
pub fn run_migrations(revision: i32) -> i32 {
info!("Starting database migration.");
if revision <= 0 {
info!("Running migration [revision 0]: Test migration system.");
}
// Reminder to update LATEST_REVISION when adding new migrations.
LATEST_REVISION
}

View File

@@ -10,13 +10,8 @@ pub fn connect() {
Client::with_uri_str(&env::var("DB_URI").expect("DB_URI not in environment variables!"))
.expect("Failed to init db connection.");
client
.database("revolt")
.collection("migrations")
.find(doc! {}, None)
.expect("Failed to get migration data from database.");
DBCONN.set(client).unwrap();
migrations::run_migrations();
}
pub fn get_connection() -> &'static Client {
@@ -31,6 +26,8 @@ pub fn get_collection(collection: &str) -> Collection {
get_db().collection(collection)
}
pub mod migrations;
pub mod channel;
pub mod guild;
pub mod message;

View File

@@ -21,6 +21,7 @@ use std::thread;
fn main() {
dotenv::dotenv().ok();
env_logger::init();
database::connect();
notifications::start_worker();

View File

@@ -38,7 +38,6 @@ impl Handler for Server {
StateResult::Success(user_id) => {
let user = crate::database::user::fetch_user(&user_id).unwrap().unwrap();
self.user_id = Some(user_id);
self.sender.send(
json!({
"type": "authenticate",
@@ -47,12 +46,13 @@ impl Handler for Server {
.to_string(),
)?;
self.user_id = Some(user_id);
self.sender.send(
json!({
"type": "ready",
"data": {
// ! FIXME: rewrite
"user": user
"user": user,
}
})
.to_string(),