From a310d4b4658600b107a9f5fb3d828cf5a0654577 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 12 Dec 2024 12:30:31 +0800 Subject: [PATCH] fix(meta): ensure order when restoring metadata (#19763) (#19769) Co-authored-by: zwang28 <70626450+zwang28@users.noreply.github.com> --- .../src/backup_restore/restore_impl/v2.rs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/meta/src/backup_restore/restore_impl/v2.rs b/src/meta/src/backup_restore/restore_impl/v2.rs index ab67b0f1cb7c4..fc1009f519dd3 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?; @@ -153,7 +167,7 @@ macro_rules! for_all_auto_increment { }; } -macro_rules! reset_mysql_sequence { +macro_rules! reset_sql_sequence { ($metadata:ident, $db:ident, $( {$table:expr, $model:ident, $id_field:ident} ),*) => { $( match $db.get_database_backend() { @@ -186,7 +200,7 @@ async fn update_auto_inc( metadata: &MetadataV2, db: &impl sea_orm::ConnectionTrait, ) -> BackupResult<()> { - for_all_auto_increment!(metadata, db, reset_mysql_sequence); + for_all_auto_increment!(metadata, db, reset_sql_sequence); Ok(()) }