diff --git a/src/database/migrations/init.rs b/src/database/migrations/init.rs index e8b2b89db48032c865aea9825ca8ccd15951655b..4ced3423fced9f8e99579a8270f1bf8c111fa995 100644 --- a/src/database/migrations/init.rs +++ b/src/database/migrations/init.rs @@ -9,6 +9,10 @@ pub async fn create_database() { info!("Creating database."); let db = get_db(); + db.create_collection("accounts", None) + .await + .expect("Failed to create accounts collection."); + db.create_collection("users", None) .await .expect("Failed to create users collection."); @@ -17,14 +21,6 @@ pub async fn create_database() { .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."); @@ -43,6 +39,39 @@ pub async fn create_database() { .await .expect("Failed to create pubsub collection."); + db.run_command( + doc! { + "createIndexes": "accounts", + "indexes": [ + { + "key": { + "email": 1 + }, + "name": "email", + "unique": true, + "collation": { + "locale": "en", + "strength": 2 + } + }, + { + "key": { + "email_normalised": 1 + }, + "name": "email_normalised", + "unique": true, + "collation": { + "locale": "en", + "strength": 2 + } + } + ] + }, + None, + ) + .await + .expect("Failed to create account index."); + db.run_command( doc! { "createIndexes": "users", diff --git a/src/database/migrations/scripts.rs b/src/database/migrations/scripts.rs index f952e749e143fd7dd131333e9f9288c1c032427c..7ebcb5c9ee0f743644cc77a97947a52f2cc27554 100644 --- a/src/database/migrations/scripts.rs +++ b/src/database/migrations/scripts.rs @@ -12,7 +12,7 @@ struct MigrationInfo { revision: i32, } -pub const LATEST_REVISION: i32 = 4; +pub const LATEST_REVISION: i32 = 0; pub async fn migrate_database() { let migrations = get_collection("migrations"); @@ -55,116 +55,6 @@ pub async fn run_migrations(revision: i32) -> i32 { info!("Running migration [revision 0]: Test migration system."); } - if revision <= 1 { - info!("Running migration [revision 1]: Add channels to guild object."); - - let col = get_collection("guilds"); - let mut guilds = col - .find( - None, - FindOptions::builder().projection(doc! { "_id": 1 }).build(), - ) - .await - .expect("Failed to fetch guilds."); - - let mut result = get_collection("channels") - .find( - doc! { - "type": 2 - }, - FindOptions::builder() - .projection(doc! { "_id": 1, "guild": 1 }) - .build(), - ) - .await - .expect("Failed to fetch channels."); - - let mut channels = vec![]; - 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.") - .to_string(); - - channels.push((id, gid)); - } - - 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."); - - let list: Vec<String> = channels - .iter() - .filter(|x| x.1 == id) - .map(|x| x.0.clone()) - .collect(); - - col.update_one( - doc! { - "_id": id - }, - doc! { - "$set": { - "channels": list - } - }, - None, - ) - .await - .expect("Failed to update guild."); - } - } - - if revision <= 2 { - info!("Running migration [revision 2]: Add username index to users."); - - get_db() - .run_command( - doc! { - "createIndexes": "users", - "indexes": [ - { - "key": { - "username": 1 - }, - "name": "username", - "unique": true, - "collation": { - "locale": "en", - "strength": 2 - } - } - ] - }, - None, - ) - .await - .expect("Failed to create username index."); - } - - if revision <= 3 { - info!("Running migration [revision 3]: Changed enum tag type to channel_type."); - - get_collection("channels") - .update_many( - doc! { }, - doc! { - "$rename": { - "type": "channel_type" - } - }, - None - ) - .await - .expect("Failed to migrate channel type."); - } - // Reminder to update LATEST_REVISION when adding new migrations. LATEST_REVISION }