forked from jmug/stoatchat
chore: add database migrations for safety
This commit is contained in:
@@ -57,6 +57,14 @@ pub async fn create_database(db: &MongoDb) {
|
||||
.await
|
||||
.expect("Failed to create user_settings collection.");
|
||||
|
||||
db.create_collection("safety_reports", None)
|
||||
.await
|
||||
.expect("Failed to create safety_reports collection.");
|
||||
|
||||
db.create_collection("safety_snapshots", None)
|
||||
.await
|
||||
.expect("Failed to create safety_snapshots collection.");
|
||||
|
||||
db.create_collection("bots", None)
|
||||
.await
|
||||
.expect("Failed to create bots collection.");
|
||||
@@ -103,18 +111,18 @@ pub async fn create_database(db: &MongoDb) {
|
||||
},
|
||||
"name": "content"
|
||||
},
|
||||
{
|
||||
"key": {
|
||||
"channel": 1_i32
|
||||
},
|
||||
"name": "channel"
|
||||
},
|
||||
{
|
||||
"key": {
|
||||
"channel": 1_i32,
|
||||
"_id": 1_i32
|
||||
},
|
||||
"name": "channel_id_compound"
|
||||
},
|
||||
{
|
||||
"key": {
|
||||
"author": 1_i32
|
||||
},
|
||||
"name": "author"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -16,7 +16,7 @@ struct MigrationInfo {
|
||||
revision: i32,
|
||||
}
|
||||
|
||||
pub const LATEST_REVISION: i32 = 18;
|
||||
pub const LATEST_REVISION: i32 = 21;
|
||||
|
||||
pub async fn migrate_database(db: &MongoDb) {
|
||||
let migrations = db.col::<Document>("migrations");
|
||||
@@ -661,6 +661,103 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
|
||||
.expect("Failed to update server members.");
|
||||
}
|
||||
|
||||
if revision <= 18 {
|
||||
info!("Running migration [revision 18 / 27-02-2022]: Create author index on messages. Drop plain channel index if exists.");
|
||||
|
||||
if db
|
||||
.db()
|
||||
.run_command(
|
||||
doc! {
|
||||
"dropIndexes": "messages",
|
||||
"index": ["channel"]
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
info!("Failed to drop `messages.channel` index but this is ok since that means it's probably gone.");
|
||||
}
|
||||
|
||||
db.db()
|
||||
.run_command(
|
||||
doc! {
|
||||
"createIndexes": "messages",
|
||||
"indexes": [
|
||||
{
|
||||
"key": {
|
||||
"author": 1_i32,
|
||||
},
|
||||
"name": "author"
|
||||
}
|
||||
]
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("Failed to create messages author index.");
|
||||
}
|
||||
|
||||
if revision <= 19 {
|
||||
info!("Running migration [revision 19 / 27-02-2023]: Create report / snapshot collections, migrate to new model if applicable.");
|
||||
|
||||
// TODO: make these fail once production is migrated
|
||||
if db
|
||||
.db()
|
||||
.create_collection("safety_reports", None)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
info!("Failed to create safety_reports collection but this is expected in production.");
|
||||
}
|
||||
|
||||
if db
|
||||
.db()
|
||||
.create_collection("safety_snapshots", None)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
info!(
|
||||
"Failed to create safety_snapshots collection but this is expected in production."
|
||||
);
|
||||
}
|
||||
|
||||
db.col::<Document>("safety_reports")
|
||||
.update_many(
|
||||
doc! {},
|
||||
doc! {
|
||||
"$set": {
|
||||
"status": "Created"
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if revision <= 20 {
|
||||
info!("Running migration [revision 20 / 28-02-2023]: Add index `snapshot.report_id`.");
|
||||
|
||||
db.db()
|
||||
.run_command(
|
||||
doc! {
|
||||
"createIndexes": "safety_snapshots",
|
||||
"indexes": [
|
||||
{
|
||||
"key": {
|
||||
"report_id": 1_i32
|
||||
},
|
||||
"name": "report_id"
|
||||
}
|
||||
]
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("Failed to create safety snapshot index.");
|
||||
}
|
||||
|
||||
// Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`.
|
||||
|
||||
// Reminder to update LATEST_REVISION when adding new migrations.
|
||||
|
||||
Reference in New Issue
Block a user