Add migration

This commit is contained in:
Niels Andriesse 2021-04-29 11:24:31 +10:00
parent 51b75100b8
commit 3d5dbfe981
2 changed files with 27 additions and 0 deletions

View File

@ -69,6 +69,8 @@ async fn main() {
fs::create_dir_all("./files").unwrap();
// Create default rooms
create_default_rooms().await;
// Perform migration
storage::perform_migration();
// Set up pruning jobs
let prune_pending_tokens_future = storage::prune_pending_tokens_periodically();
let prune_tokens_future = storage::prune_tokens_periodically();

View File

@ -286,6 +286,31 @@ pub async fn prune_files(file_expiration: i64) {
}
}
// Migration
pub fn perform_migration() {
let rooms = match get_all_room_ids() {
Ok(rooms) => rooms,
Err(_) => return,
};
for room in rooms {
let pool = pool_by_room_id(&room);
let conn = match pool.get() {
Ok(conn) => conn,
Err(e) => return error!("Couldn't perform migration due to error: {}.", e),
};
let stmt = format!("ALTER TABLE {} ADD is_deleted INTEGER", MESSAGES_TABLE);
match conn.execute(&stmt, params![]) {
Ok(_) => (),
Err(e) => return error!("Couldn't perform migration due to error: {}.", e),
};
// Log the result
info!("Successfully performed migration for room: {}.", room);
}
}
// Utilities
fn get_all_room_ids() -> Result<Vec<String>, Error> {
// Get a database connection
let conn = MAIN_POOL.get().map_err(|_| Error::DatabaseFailedInternally)?;