From bc80e4aa2c68d0b35d74fa2b6ab7b24390e5ce84 Mon Sep 17 00:00:00 2001 From: zwang28 <84491488@qq.com> Date: Thu, 13 Jun 2024 12:52:46 +0800 Subject: [PATCH] fix insert order when restoring to satisfy FK constraint --- .../meta_snapshot_builder_v2.rs | 2 +- .../src/backup_restore/restore_impl/v2.rs | 46 ++++++++++++------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/meta/src/backup_restore/meta_snapshot_builder_v2.rs b/src/meta/src/backup_restore/meta_snapshot_builder_v2.rs index 4a81ae9d6ad08..9e4ad6a0c05a1 100644 --- a/src/meta/src/backup_restore/meta_snapshot_builder_v2.rs +++ b/src/meta/src/backup_restore/meta_snapshot_builder_v2.rs @@ -33,7 +33,7 @@ fn map_db_err(e: DbErr) -> BackupError { macro_rules! define_set_metadata { ($( {$name:ident, $mod_path:ident::$mod_name:ident} ),*) => { - pub async fn set_metadata( + async fn set_metadata( metadata: &mut MetadataV2, txn: &sea_orm::DatabaseTransaction, ) -> BackupResult<()> { diff --git a/src/meta/src/backup_restore/restore_impl/v2.rs b/src/meta/src/backup_restore/restore_impl/v2.rs index 13492c56316a2..a431b455063bb 100644 --- a/src/meta/src/backup_restore/restore_impl/v2.rs +++ b/src/meta/src/backup_restore/restore_impl/v2.rs @@ -93,28 +93,40 @@ impl WriterModelV2ToMetaStoreV2 { } } -macro_rules! define_write_model_v2_to_meta_store_v2 { - ($( {$name:ident, $mod_path:ident::$mod_name:ident} ),*) => { - async fn write_model_v2_to_meta_store_v2( - metadata: &risingwave_backup::meta_snapshot_v2::MetadataV2, - db: &sea_orm::DatabaseConnection, - ) -> BackupResult<()> { - $( - insert_models(metadata.$name.clone(), db).await?; - )* - Ok(()) - } - }; -} - -risingwave_backup::for_all_metadata_models_v2!(define_write_model_v2_to_meta_store_v2); - #[async_trait::async_trait] impl Writer for WriterModelV2ToMetaStoreV2 { async fn write(&self, target_snapshot: MetaSnapshot) -> BackupResult<()> { let metadata = target_snapshot.metadata; let db = &self.meta_store.conn; - write_model_v2_to_meta_store_v2(&metadata, db).await?; + insert_models(metadata.seaql_migrations.clone(), db).await?; + insert_models(metadata.clusters.clone(), db).await?; + insert_models(metadata.version_stats.clone(), db).await?; + insert_models(metadata.compaction_configs.clone(), db).await?; + insert_models(metadata.hummock_sequences.clone(), db).await?; + insert_models(metadata.workers.clone(), db).await?; + insert_models(metadata.worker_properties.clone(), db).await?; + insert_models(metadata.users.clone(), db).await?; + insert_models(metadata.user_privileges.clone(), db).await?; + insert_models(metadata.objects.clone(), db).await?; + insert_models(metadata.object_dependencies.clone(), db).await?; + insert_models(metadata.databases.clone(), db).await?; + insert_models(metadata.schemas.clone(), db).await?; + insert_models(metadata.streaming_jobs.clone(), db).await?; + insert_models(metadata.fragments.clone(), db).await?; + insert_models(metadata.actors.clone(), db).await?; + insert_models(metadata.actor_dispatchers.clone(), db).await?; + insert_models(metadata.connections.clone(), db).await?; + insert_models(metadata.sources.clone(), db).await?; + insert_models(metadata.tables.clone(), db).await?; + insert_models(metadata.sinks.clone(), db).await?; + insert_models(metadata.views.clone(), db).await?; + insert_models(metadata.indexes.clone(), db).await?; + insert_models(metadata.functions.clone(), db).await?; + insert_models(metadata.system_parameters.clone(), db).await?; + insert_models(metadata.catalog_versions.clone(), db).await?; + insert_models(metadata.subscriptions.clone(), db).await?; + insert_models(metadata.session_parameters.clone(), db).await?; + insert_models(metadata.secrets.clone(), db).await?; // update_auto_inc must be called last. update_auto_inc(&metadata, db).await?; Ok(())