title: Drupal - Importing a taxonomy with migrate module from a table and creating/updating existing terms
body: <p>I need to import a list of terms into my taxonomy from a source I loaded in the database.
  The problem is I allready have this taxonomy on my site (loaded wihtout migrate) with terms that are used by reference in other content, so I got to keep existing term and update them or create the new ones.</p>
  
  <p>To link my taxonomy source and the existing taxonomy I have an unique code for each term, so I added a code field to my vocabulary and filled it for each existing term.</p>
  
  <p>I am currently able to create and update terms with my current Migration class, but if the name of my term on the site and the name of the term in my source is different, the import will create a new term instead of updating its name even if the code is the same.</p>
  
  <p>Here my Migration Class :</p>
  
  <pre><code>class TotoMigration extends Migration  {
  
        private $list_term = array();
  
        public function __construct($arguments) {
          parent::__construct();
  
          $this-&gt;softDependencies = array('TotoParent');
  
          // get data from the custom table containing the new terms to create or update
          $query = db_select('toto', 'f')
            -&gt;fields('f', array(
                'CODE', // code
                'LIBLONG', // name
                'PARENT', // parent
            )
          );
          $this-&gt;source = new MigrateSourceSQL($query);
  
          $this-&gt;destination = new MigrateDestinationTerm('toto_tax');
  
          $this-&gt;map = new MigrateSQLMap($this-&gt;machineName,
              array(
                  'CODE' =&gt; array('type' =&gt; 'varchar',
                      'length' =&gt; 5,
                      'not null' =&gt; TRUE,
                      'description' =&gt; 'Code',
                  )
              ),
              MigrateDestinationTerm::getKeySchema()
          );
  
  
          $this-&gt;addFieldMapping('name', 'LIBLONG');
          $this-&gt;addFieldMapping('field_code', 'CODE');
          $this-&gt;addFieldMapping('parent', 'PARENT')
          -&gt;arguments(array('source_type' =&gt; 'tid'))
          -&gt;sourceMigration('TotoParent');
  
          // create a list of existing toto terms with code =&gt; tid
          $list_term = db_query("select fc.field_code_value, ttd.tid
                                                                  from taxonomy_term_data ttd
                                                                  left join taxonomy_term_hierarchy tth on tth.tid=ttd.tid
                                                                  left join field_data_field_code fc on fc.entity_id = ttd.tid
                                                                  where ttd.vid=10
                                                                  and tth.parent!=0;")-&gt;fetchAllKeyed();
        }
  
        public function prepareRow($row) {
          // Always include this fragment at the beginning of every prepareRow()
          // implementation, so parent classes can ignore rows.
          if (parent::prepareRow($row) === FALSE) {
            return FALSE;
          }
          // if the destination is not mapped in migrate we tell him where to go
          if (!isset($row-&gt;migrate_map_destid1) &amp;&amp; isset($list_term[$row-&gt;CODE])) {
            $row-&gt;migrate_map_destid1 = $list_term[$row-&gt;CODE];
          }
        }
      }
  </code></pre>
  
  <p>I then load the import with drush (and --update option).
  I must be missing something, if anyone got a clue it will be welcome.</p>