From 5ee01d06c0b067f2f07d0288c499897cd0df29f7 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 24 Oct 2023 20:04:19 +0200 Subject: migration tool --- tool/src/import/mod.rs | 5 +++ tool/src/main.rs | 16 ++++++++- tool/src/migrate.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 tool/src/migrate.rs (limited to 'tool/src') diff --git a/tool/src/import/mod.rs b/tool/src/import/mod.rs index e148c98..17f3137 100644 --- a/tool/src/import/mod.rs +++ b/tool/src/import/mod.rs @@ -1,3 +1,8 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2023 metamuffin +*/ pub mod infojson; pub mod tmdb; diff --git a/tool/src/main.rs b/tool/src/main.rs index 1e84bda..3a670f5 100644 --- a/tool/src/main.rs +++ b/tool/src/main.rs @@ -6,12 +6,14 @@ #![feature(file_create_new)] pub mod import; +pub mod migrate; use base64::Engine; -use clap::{Parser, Subcommand}; +use clap::{Parser, Subcommand, ValueEnum}; use import::import; use jellycommon::{config::GlobalConfig, Node, NodeKind, NodePrivate, NodePublic}; use log::{info, warn}; +use migrate::migrate; use rand::random; use std::{fmt::Debug, fs::File, io::Write, path::PathBuf}; @@ -61,6 +63,17 @@ enum Action { #[arg(short, long)] series: bool, }, + Migrate { + database: PathBuf, + mode: MigrateMode, + save_location: PathBuf, + }, +} + +#[derive(Debug, Clone, Copy, PartialEq, ValueEnum)] +enum MigrateMode { + Import, + Export, } fn main() -> anyhow::Result<()> { @@ -123,6 +136,7 @@ fn main() -> anyhow::Result<()> { Ok(()) } a @ Action::New { .. } => import(a, args.dry), + a @ Action::Migrate { .. } => migrate(a), } } diff --git a/tool/src/migrate.rs b/tool/src/migrate.rs new file mode 100644 index 0000000..7c24087 --- /dev/null +++ b/tool/src/migrate.rs @@ -0,0 +1,89 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2023 metamuffin +*/ +use crate::{Action, MigrateMode}; +use anyhow::bail; +use indicatif::ProgressIterator; +use jellybase::database::{typed_sled::Tree, Database}; +use log::info; +use serde::Serialize; +use std::{ + fs::File, + io::{BufRead, BufReader, BufWriter, Write}, + path::Path, +}; + +pub(crate) fn migrate(action: Action) -> anyhow::Result<()> { + match action { + Action::Migrate { + mode, + save_location, + database, + } => { + std::fs::create_dir_all(&save_location)?; + let db = Database::open(&database)?; + + info!("processing 'user'"); + process_tree(mode, &save_location.join("user"), &db.user)?; + info!("processing 'invite'"); + process_tree(mode, &save_location.join("invite"), &db.invite)?; + info!("processing 'node'"); + process_tree(mode, &save_location.join("node"), &db.node)?; + info!("done"); + Ok(()) + } + _ => unreachable!(), + } +} + +fn process_tree< + K: Serialize + for<'de> serde::Deserialize<'de>, + V: Serialize + for<'de> serde::Deserialize<'de>, +>( + mode: MigrateMode, + path: &Path, + tree: &Tree, +) -> anyhow::Result<()> { + match mode { + MigrateMode::Export => export_tree(path, tree), + MigrateMode::Import => import_tree(path, tree), + } +} + +fn export_tree< + K: Serialize + for<'de> serde::Deserialize<'de>, + V: Serialize + for<'de> serde::Deserialize<'de>, +>( + path: &Path, + tree: &Tree, +) -> anyhow::Result<()> { + let mut o = BufWriter::new(File::create(path)?); + let len = tree.len(); + for r in tree.iter().progress_count(len.try_into().unwrap()) { + let (k, v) = r?; + serde_json::to_writer(&mut o, &(k, v))?; + writeln!(&mut o)?; + } + Ok(()) +} + +fn import_tree< + K: Serialize + for<'de> serde::Deserialize<'de>, + V: Serialize + for<'de> serde::Deserialize<'de>, +>( + path: &Path, + tree: &Tree, +) -> anyhow::Result<()> { + if !tree.is_empty() { + bail!("tree not empty, `rm -rf` your db please :)") + } + let i = BufReader::new(File::open(path)?); + for l in i.lines() { + let l = l?; + let (k, v) = serde_json::from_str::<(K, V)>(&l)?; + tree.insert(&k, &v)?; + } + Ok(()) +} -- cgit v1.2.3-70-g09d2