title: Realm migration introducing primary key
body: <p>Currently, my Realm database contains a plain array of <code>MyData</code> objects:</p>
  
  <pre><code>class MyData: Object {
      @objc dynamic var name = ""
      @objc dynamic var age = 0
  }
  </code></pre>
  
  <p>For instance, it may contain</p>
  
  <pre><code>[
      ["Alice", 12],
      ["Bob", 13],
      ["Alice", 22],
      ["Carolina", 13],
      ["Bob", 20]
  ]
  </code></pre>
  
  <p>I am going to rework it so that to make <code>name</code> unique keeping the biggest <code>age</code>:</p>
  
  <pre><code>[
      ["Alice", 22],
      ["Bob", 20],
      ["Carolina", 13]
  ]
  </code></pre>
  
  <p>also, I want to make <code>name</code> a primary key to avoid duplication in future.</p>
  
  <p>So I changed class description as</p>
  
  <pre><code>class MyData: Object {
      @objc dynamic var name = ""
      @objc dynamic var age = 0
      override static func primaryKey() -&gt; String? {
          return "name"
      }
  }
  </code></pre>
  
  <p>And now I need to provide a migration block:</p>
  
  <pre><code>let config = Realm.Configuration(
      schemaVersion: 1,
      migrationBlock: { migration, oldSchemaVersion in
          switch oldSchemaVersion {
          case 0:
             migration.enumerateObjects(ofType: MyData.className()) { (oldObject, newObject) in
              // how to do it?
          }
          default:
              break
      }
  })
  </code></pre>
  
  <p>I do not understand what to do in enumeration block. In my example, I have five "old" objects (without primary key), and I will have three "new" objects (with primary key), so how do I do this transformation?</p>