mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Logging + database migrations system.
This commit is contained in:
39
src/database/migrations/init.rs
Normal file
39
src/database/migrations/init.rs
Normal 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.");
|
||||
}
|
||||
19
src/database/migrations/mod.rs
Normal file
19
src/database/migrations/mod.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
53
src/database/migrations/scripts.rs
Normal file
53
src/database/migrations/scripts.rs
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user