From fd492cee7bba9c9ccf0c3bc07cea966372371388 Mon Sep 17 00:00:00 2001 From: zwang28 <84491488@qq.com> Date: Wed, 11 Dec 2024 17:43:52 +0800 Subject: [PATCH] fix(meta): ensure order when restoring metadata --- src/meta/src/backup_restore/restore_impl/v2.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/meta/src/backup_restore/restore_impl/v2.rs b/src/meta/src/backup_restore/restore_impl/v2.rs index ab67b0f1cb7c..c9dc8e69ef97 100644 --- a/src/meta/src/backup_restore/restore_impl/v2.rs +++ b/src/meta/src/backup_restore/restore_impl/v2.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use itertools::Itertools; use risingwave_backup::error::{BackupError, BackupResult}; use risingwave_backup::meta_snapshot::MetaSnapshot; use risingwave_backup::meta_snapshot_v2::{MetaSnapshotV2, MetadataV2}; @@ -106,8 +107,21 @@ impl Writer for WriterModelV2ToMetaStoreV2 { 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.objects.clone(), db).await?; - insert_models(metadata.user_privileges.clone(), db).await?; + // The sort is required to pass table's foreign key check. + insert_models( + metadata.objects.iter().sorted_by_key(|o| o.oid).cloned(), + db, + ) + .await?; + insert_models( + metadata + .user_privileges + .iter() + .sorted_by_key(|u| u.id) + .cloned(), + db, + ) + .await?; insert_models(metadata.object_dependencies.clone(), db).await?; insert_models(metadata.databases.clone(), db).await?; insert_models(metadata.schemas.clone(), db).await?;