Reset migrations; [point of no return for old databases]
This commit is contained in:
@@ -9,6 +9,10 @@ pub async fn create_database() {
|
|||||||
info!("Creating database.");
|
info!("Creating database.");
|
||||||
let db = get_db();
|
let db = get_db();
|
||||||
|
|
||||||
|
db.create_collection("accounts", None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create accounts collection.");
|
||||||
|
|
||||||
db.create_collection("users", None)
|
db.create_collection("users", None)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create users collection.");
|
.expect("Failed to create users collection.");
|
||||||
@@ -17,14 +21,6 @@ pub async fn create_database() {
|
|||||||
.await
|
.await
|
||||||
.expect("Failed to create channels collection.");
|
.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)
|
db.create_collection("messages", None)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create messages collection.");
|
.expect("Failed to create messages collection.");
|
||||||
@@ -43,6 +39,39 @@ pub async fn create_database() {
|
|||||||
.await
|
.await
|
||||||
.expect("Failed to create pubsub collection.");
|
.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(
|
db.run_command(
|
||||||
doc! {
|
doc! {
|
||||||
"createIndexes": "users",
|
"createIndexes": "users",
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ struct MigrationInfo {
|
|||||||
revision: i32,
|
revision: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const LATEST_REVISION: i32 = 4;
|
pub const LATEST_REVISION: i32 = 0;
|
||||||
|
|
||||||
pub async fn migrate_database() {
|
pub async fn migrate_database() {
|
||||||
let migrations = get_collection("migrations");
|
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.");
|
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.
|
// Reminder to update LATEST_REVISION when adding new migrations.
|
||||||
LATEST_REVISION
|
LATEST_REVISION
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user