From 1816db8c1f9328af92336ed05e25f1b1b78ab07d Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Tue, 31 May 2022 15:39:09 +0200 Subject: [PATCH] Move dump v4 patcher into v4.rs --- meilisearch-auth/src/dump.rs | 30 +----------------- meilisearch-lib/src/dump/loaders/v4.rs | 44 +++++++++++++++++++------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/meilisearch-auth/src/dump.rs b/meilisearch-auth/src/dump.rs index 127e65280..7e607e574 100644 --- a/meilisearch-auth/src/dump.rs +++ b/meilisearch-auth/src/dump.rs @@ -1,11 +1,9 @@ use serde_json::Deserializer; -use serde_json::{Map, Value}; -use std::fs; + use std::fs::File; use std::io::BufReader; use std::io::Write; use std::path::Path; -use uuid::Uuid; use crate::{AuthController, HeedAuthStore, Result}; @@ -46,30 +44,4 @@ impl AuthController { Ok(()) } - - pub fn patch_dump_v4(src: impl AsRef, dst: impl AsRef) -> 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 reader = BufReader::new(File::open(&keys_file_src)?); - for key in Deserializer::from_reader(reader).into_iter() { - let mut key: Map = key?; - - // generate a new uuid v4 and insert it in the key. - let uid = serde_json::to_value(Uuid::new_v4()).unwrap(); - key.insert("uid".to_string(), uid); - - serde_json::to_writer(&mut writer, &key)?; - writer.write_all(b"\n")?; - } - - Ok(()) - } } diff --git a/meilisearch-lib/src/dump/loaders/v4.rs b/meilisearch-lib/src/dump/loaders/v4.rs index 3caa7a9e9..0744df7ea 100644 --- a/meilisearch-lib/src/dump/loaders/v4.rs +++ b/meilisearch-lib/src/dump/loaders/v4.rs @@ -1,12 +1,12 @@ use std::fs::{self, create_dir_all, File}; -use std::io::Write; +use std::io::{BufReader, Write}; use std::path::Path; use fs_extra::dir::{self, CopyOptions}; use log::info; +use serde_json::{Deserializer, Map, Value}; use tempfile::tempdir; - -use meilisearch_auth::AuthController; +use uuid::Uuid; use crate::dump::{compat, Metadata}; use crate::options::IndexerOpts; @@ -26,14 +26,10 @@ pub fn load_dump( let options = CopyOptions::default(); // Indexes - dir::copy(src.as_ref().join("indexes"), patched_dir.path(), &options)?; + dir::copy(src.as_ref().join("indexes"), &patched_dir, &options)?; // Index uuids - dir::copy( - src.as_ref().join("index_uuids"), - patched_dir.path(), - &options, - )?; + dir::copy(src.as_ref().join("index_uuids"), &patched_dir, &options)?; // Metadata fs::copy( @@ -45,11 +41,11 @@ pub fn load_dump( patch_updates(&src, &patched_dir)?; // Keys - AuthController::patch_dump_v4(&src, patched_dir.path())?; + patch_keys(&src, &patched_dir)?; super::v5::load_dump( meta, - patched_dir.path(), + &patched_dir, dst, index_db_size, meta_env_size, @@ -79,3 +75,29 @@ fn patch_updates(src: impl AsRef, dst: impl AsRef) -> anyhow::Result Ok(()) } + +fn patch_keys(src: impl AsRef, dst: impl AsRef) -> anyhow::Result<()> { + let keys_file_src = src.as_ref().join("keys"); + + if !keys_file_src.exists() { + return Ok(()); + } + + fs::create_dir_all(&dst)?; + let keys_file_dst = dst.as_ref().join("keys"); + let mut writer = File::create(&keys_file_dst)?; + + let reader = BufReader::new(File::open(&keys_file_src)?); + for key in Deserializer::from_reader(reader).into_iter() { + let mut key: Map = key?; + + // generate a new uuid v4 and insert it in the key. + let uid = serde_json::to_value(Uuid::new_v4()).unwrap(); + key.insert("uid".to_string(), uid); + + serde_json::to_writer(&mut writer, &key)?; + writer.write_all(b"\n")?; + } + + Ok(()) +}