Patch dump v4

This commit is contained in:
ManyTheFish 2022-05-30 14:57:10 +02:00
parent b9a79eb858
commit 70916d6596
2 changed files with 29 additions and 3 deletions

View File

@ -1,8 +1,11 @@
use serde_json::{Map, Value};
use std::fs;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::path::Path;
use uuid::Uuid;
use crate::{AuthController, HeedAuthStore, Result};
@ -44,4 +47,27 @@ impl AuthController {
Ok(())
}
pub fn patch_dump_v4(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
let keys_file_src = src.as_ref().join(KEYS_PATH);
if !keys_file_src.exists() {
return Ok(());
}
fs::create_dir_all(&dst)?;
let keys_file_dst = dst.as_ref().join(KEYS_PATH);
let mut writer = File::create(&keys_file_dst)?;
let mut reader = BufReader::new(File::open(&keys_file_src)?).lines();
while let Some(key) = reader.next().transpose()? {
let mut key: Map<String, Value> = serde_json::from_str(&key)?;
let uid = Uuid::new_v4().to_string();
key.insert("uid".to_string(), Value::String(uid));
serde_json::to_writer(&mut writer, &key)?;
writer.write_all(b"\n")?;
}
Ok(())
}
}

View File

@ -6,6 +6,8 @@ use fs_extra::dir::{self, CopyOptions};
use log::info;
use tempfile::tempdir;
use meilisearch_auth::AuthController;
use crate::dump::{compat, Metadata};
use crate::options::IndexerOpts;
use crate::tasks::task::Task;
@ -43,9 +45,7 @@ pub fn load_dump(
patch_updates(&src, &patched_dir)?;
// Keys
if src.as_ref().join("keys").exists() {
fs::copy(src.as_ref().join("keys"), patched_dir.path().join("keys"))?;
}
AuthController::patch_dump_v4(&src, patched_dir.path())?;
super::v5::load_dump(
meta,