From 34541f29825199e3dcd375b04da3ee7ea246d41c Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Wed, 29 Apr 2020 15:11:14 +0530 Subject: [PATCH] Export: Ignore deleted predicates from schema (#5302) Fixes https://github.com/dgraph-io/dgraph/issues/5053 Fixes DGRAPH-1260 The current implementation of export would add dropped predicates and deleted types to the exported schema. This PR fixes it by ignoring the deleted predicates and types. (cherry picked from commit 23140a9f61116ead3defda05bbade9eeca161aef) --- worker/export.go | 5 +++++ worker/export_test.go | 22 ++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/worker/export.go b/worker/export.go index e09af4a4892..5924ab1e617 100644 --- a/worker/export.go +++ b/worker/export.go @@ -438,6 +438,10 @@ func export(ctx context.Context, in *pb.ExportRequest) error { stream := pstore.NewStreamAt(in.ReadTs) stream.LogPrefix = "Export" stream.ChooseKey = func(item *badger.Item) bool { + // Skip exporting delete data including Schema and Types. + if item.IsDeletedOrExpired() { + return false + } pk, err := x.Parse(item.Key()) if err != nil { glog.Errorf("error %v while parsing key %v during export. Skip.", err, hex.EncodeToString(item.Key())) @@ -567,6 +571,7 @@ func export(ctx context.Context, in *pb.ExportRequest) error { // prepended hasDataBefore = true } + if _, err := writer.gw.Write(kv.Value); err != nil { return err } diff --git a/worker/export_test.go b/worker/export_test.go index ec2e502708c..85f08ab7dbf 100644 --- a/worker/export_test.go +++ b/worker/export_test.go @@ -125,29 +125,32 @@ func initTestExport(t *testing.T, schemaStr string) { require.NoError(t, txn.Set(x.SchemaKey("friend"), val)) // Schema is always written at timestamp 1 require.NoError(t, txn.CommitAt(1, nil)) - txn.Discard() require.NoError(t, err) val, err = (&pb.SchemaUpdate{ValueType: pb.Posting_UID}).Marshal() require.NoError(t, err) txn = pstore.NewTransactionAt(math.MaxUint64, true) - txn.Set(x.SchemaKey("http://www.w3.org/2000/01/rdf-schema#range"), val) + err = txn.Set(x.SchemaKey("http://www.w3.org/2000/01/rdf-schema#range"), val) require.NoError(t, err) - txn.Set(x.SchemaKey("friend_not_served"), val) + require.NoError(t, txn.Set(x.SchemaKey("friend_not_served"), val)) + require.NoError(t, txn.Set(x.SchemaKey("age"), val)) require.NoError(t, txn.CommitAt(1, nil)) - txn.Discard() val, err = personType.Marshal() require.NoError(t, err) txn = pstore.NewTransactionAt(math.MaxUint64, true) - txn.Set(x.TypeKey("Person"), val) - require.NoError(t, err) + require.NoError(t, txn.Set(x.TypeKey("Person"), val)) require.NoError(t, txn.CommitAt(1, nil)) - txn.Discard() populateGraphExport(t) + + // Drop age predicate after populating DB. + // age should not exist in the exported schema. + txn = pstore.NewTransactionAt(math.MaxUint64, true) + require.NoError(t, txn.Delete(x.SchemaKey("age"))) + require.NoError(t, txn.CommitAt(1, nil)) } func getExportFileList(t *testing.T, bdir string) (dataFiles, schemaFiles []string) { @@ -197,7 +200,10 @@ func checkExportSchema(t *testing.T, schemaFileList []string) { func TestExportRdf(t *testing.T) { // Index the name predicate. We ensure it doesn't show up on export. - initTestExport(t, "name:string @index .") + initTestExport(t, ` + name:string @index . + age:int. + `) bdir, err := ioutil.TempDir("", "export") require.NoError(t, err)