From b0b50001bf6a48207761046b9fdfb33aac482c89 Mon Sep 17 00:00:00 2001 From: Yury Frolov Date: Wed, 21 Feb 2024 15:16:17 +0500 Subject: [PATCH] Check for attached relations before dropping distribution --- pkg/meta/meta.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/meta/meta.go b/pkg/meta/meta.go index 0bec076a6..2832a45d0 100644 --- a/pkg/meta/meta.go +++ b/pkg/meta/meta.go @@ -83,6 +83,12 @@ func processDrop(ctx context.Context, dstmt spqrparser.Statement, isCascade bool } if stmt.ID != "*" { + if ds, err := mngr.GetDistribution(ctx, stmt.ID); err != nil { + return err + } else if len(ds.Relations) != 0 && !isCascade { + fmt.Errorf("cannot drop distribution %s because there are relations attached to it\nHINT: Use DROP ... CASCADE to detach relations autoimatically", stmt.ID) + } + if err := mngr.DropDistribution(ctx, stmt.ID); err != nil { return cli.ReportError(err) } @@ -90,12 +96,18 @@ func processDrop(ctx context.Context, dstmt spqrparser.Statement, isCascade bool } dss, err := mngr.ListDistributions(ctx) + if err != nil { + return err + } ret := make([]string, 0) if err != nil { return err } for _, ds := range dss { - if stmt.ID == "*" && ds.Id != "default" { + if ds.Id != "default" { + if len(ds.Relations) != 0 && !isCascade { + fmt.Errorf("cannot drop distribution %s because there are relations attached to it\nHINT: Use DROP ... CASCADE to detach relations autoimatically", ds.ID) + } ret = append(ret, ds.ID()) err = mngr.DropDistribution(ctx, ds.Id) if err != nil {