Migrating (renaming predicates, etc)

Hello! Very excited about Dgraph.

Is there a story for changing the shape of data in Dgraph? I’m currently playing around with Dgraph on a project I’m starting up and am trying to see how I would rename some predicates and update type names, etc. I can only find documentation on adding new predicates currently.

1 Like

Do you mean to update the value of the predicate or the predicate name itself?

Well, if the answer is to update the predicate itself and move the value to the new one, you can use Bulk Upsert. But it is on master and will be in next RC (this week I think). Also, you have to be careful with Bulk Upsert. Cuz it’s too powerful. If you have billions of billions of data it can block things. So I recommend do it by parts. You can limit it by paggination and adding a filter like e.g:
@filter(NOT has(TheNewPredicate) ).

Schema and dataset sample

<age>: int .
<email>: string @index(exact, trigram) .
<otherpredicate>: string .
<test>: string .
{
    set {
      _:blank1 <email> "test1@dgraph.io" .
      _:blank1 <age> "28" .
      _:blank1 <test> "This is my test1" .
      
      
      _:blank2 <email> "test2@dgraph.io" .
      _:blank2 <age> "30" .
      _:blank2 <test> "This is my test2" .
      
      
      _:blank3 <email> "test3@dgraph.io" .
      _:blank3 <age> "44" .
      _:blank3 <test> "This is my test3" .
      
      
      _:blank4 <email> "test4@dgraph.io" .
      _:blank4 <age> "21" .
      _:blank4 <test> "This is my test4" .
    }
}

Query

{
  q(func: regexp(email, /.*@dgraph.io$/)) {
    email
    age
    test
    <test>
  }
}

Result

{
  "data": {
    "q": [
      {
        "email": "test1@dgraph.io",
        "age": 28,
        "test": "This is my test1"
      },
      {
        "email": "test2@dgraph.io",
        "age": 30,
        "test": "This is my test2"
      },
      {
        "email": "test3@dgraph.io",
        "age": 44,
        "test": "This is my test3"
      },
      {
        "email": "test4@dgraph.io",
        "age": 21,
        "test": "This is my test4"
      }
    ]
  }
}

The Bulk Upsert

upsert {
  query {
    v as var(func: regexp(email, /.*@dgraph.io$/)) {
      getValues as test
    }
  }

  mutation {
    
  # We copy the values from the old predicate
    set {
       uid(v) <otherpredicate> val(getValues) .
    }

  # Now we delete the old predicate
    delete {
      uid(v) <test> * .
    }

  }
}

The query

{
  q(func: regexp(email, /.*@dgraph.io$/)){
    email
    age
    test
    <otherpredicate>
  }
}

Result

{
  "data": {
    "q": [
      {
        "email": "test1@dgraph.io",
        "age": 28,
        "otherpredicate": "This is my test1"
      },
      {
        "email": "test2@dgraph.io",
        "age": 30,
        "otherpredicate": "This is my test2"
      },
      {
        "email": "test3@dgraph.io",
        "age": 44,
        "otherpredicate": "This is my test3"
      },
      {
        "email": "test4@dgraph.io",
        "age": 21,
        "otherpredicate": "This is my test4"
      }
    ]
  }
}
6 Likes

Very cool! That looks exactly what I’d like to do. Thanks :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.