From 64637a40818fd52856bdc8ff43b226e6c71e8445 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Tue, 1 Dec 2020 19:00:41 +0100
Subject: [PATCH] [feat] dex:migrate: clear DBs before migration

---
 rust-bins/duniter-dbex/src/migrate.rs     |  4 ++++
 rust-libs/tools/kv_typed/src/db_schema.rs | 12 +++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/rust-bins/duniter-dbex/src/migrate.rs b/rust-bins/duniter-dbex/src/migrate.rs
index 591b7a81d..2e7b671d8 100644
--- a/rust-bins/duniter-dbex/src/migrate.rs
+++ b/rust-bins/duniter-dbex/src/migrate.rs
@@ -28,6 +28,10 @@ pub(crate) fn migrate(profile_path: PathBuf) -> anyhow::Result<()> {
     let start_time = Instant::now();
     let dbs = duniter_dbs::open_dbs(Some(profile_path.as_path()));
 
+    // Clear bc_db and gva_db
+    dbs.bc_db.clear()?;
+    dbs.gva_db.clear()?;
+
     let data_path = profile_path.join(crate::DATA_DIR);
     let duniter_js_db = BcV1Db::<LevelDb>::open(LevelDbConf {
         db_path: data_path.as_path().join("leveldb"),
diff --git a/rust-libs/tools/kv_typed/src/db_schema.rs b/rust-libs/tools/kv_typed/src/db_schema.rs
index 925c04d7c..3c4835031 100644
--- a/rust-libs/tools/kv_typed/src/db_schema.rs
+++ b/rust-libs/tools/kv_typed/src/db_schema.rs
@@ -141,6 +141,7 @@ macro_rules! db_schema {
                     $(type [<$col_name ColRw>]: DbCollectionRw;)*
                     type DbRo: Sized;
 
+                    fn clear(&self) -> KvResult<()>;
                     fn get_ro_handler(&self) -> Self::DbRo;
                     fn open(
                         backend_conf: <<Self as [<$db_name DbWritable>]>::Backend as kv_typed::backend::Backend>::Conf,
@@ -156,6 +157,11 @@ macro_rules! db_schema {
                     $(type [<$col_name ColRw>] = ColRw<B::Col, [<$col_name Event>]>;)*
                     type DbRo = [<$db_name DbRo>]<B>;
 
+                    #[inline(always)]
+                    fn clear(&self) -> KvResult<()> {
+                        $(self.collections.[<$col_name:snake>].clear()?;)*
+                        Ok(())
+                    }
                     #[inline(always)]
                     fn get_ro_handler(&self) -> Self::DbRo {
                         [<$db_name DbRo>] {
@@ -182,11 +188,15 @@ macro_rules! db_schema {
                             },
                         })
                     }
+                    #[inline(always)]
                     fn save(&self) -> KvResult<()> {
                         $(self.collections.[<$col_name:snake>].save()?;)*
                         Ok(())
                     }
-                    $(fn [<$col_name:snake _write>](&self) -> &ColRw<B::Col, [<$col_name Event>]> { &self.collections.[<$col_name:snake>] })*
+                    $(
+                        #[inline(always)]
+                        fn [<$col_name:snake _write>](&self) -> &ColRw<B::Col, [<$col_name Event>]> { &self.collections.[<$col_name:snake>] }
+                    )*
                 }
             }
         }
-- 
GitLab