[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/resources/sql/patches/ -> 20130921.xmigratemaniphest.php (source)

   1  <?php
   2  
   3  $task_table = new ManiphestTask();
   4  $conn_w = $task_table->establishConnection('w');
   5  
   6  $rows = new LiskRawMigrationIterator($conn_w, 'maniphest_transaction');
   7  $conn_w->openTransaction();
   8  
   9  // NOTE: These were the correct table names at the time of this patch.
  10  $xaction_table_name = 'maniphest_transactionpro';
  11  $comment_table_name = 'maniphest_transaction_comment';
  12  
  13  foreach ($rows as $row) {
  14    $row_id = $row['id'];
  15    $task_id = $row['taskID'];
  16  
  17    echo "Migrating row {$row_id} (T{$task_id})...\n";
  18  
  19    $task_row = queryfx_one(
  20      $conn_w,
  21      'SELECT phid FROM %T WHERE id = %d',
  22      $task_table->getTableName(),
  23      $task_id);
  24    if (!$task_row) {
  25      echo "Skipping, no such task.\n";
  26      continue;
  27    }
  28  
  29    $task_phid = $task_row['phid'];
  30  
  31    $has_comment = strlen(trim($row['comments']));
  32  
  33    $xaction_type = $row['transactionType'];
  34    $xaction_old = $row['oldValue'];
  35    $xaction_new = $row['newValue'];
  36    $xaction_source = idx($row, 'contentSource', '');
  37    $xaction_meta = $row['metadata'];
  38  
  39    // Convert "aux" (auxiliary field) transactions to proper CustomField
  40    // transactions. The formats are very similar, except that the type constant
  41    // is different and the auxiliary key should be prefixed.
  42    if ($xaction_type == 'aux') {
  43      $xaction_meta = @json_decode($xaction_meta, true);
  44      $xaction_meta = nonempty($xaction_meta, array());
  45  
  46      $xaction_type = PhabricatorTransactions::TYPE_CUSTOMFIELD;
  47  
  48      $aux_key = idx($xaction_meta, 'aux:key');
  49      if (!preg_match('/^std:maniphest:/', $aux_key)) {
  50        $aux_key = 'std:maniphest:'.$aux_key;
  51      }
  52  
  53      $xaction_meta = array(
  54        'customfield:key' => $aux_key,
  55      );
  56  
  57      $xaction_meta = json_encode($xaction_meta);
  58    }
  59  
  60    // If this transaction did something other than just leaving a comment,
  61    // insert a new transaction for that action. If there was a comment (or
  62    // a comment in addition to an action) we'll insert that below.
  63    if ($row['transactionType'] != 'comment') {
  64      $xaction_phid = PhabricatorPHID::generateNewPHID(
  65        PhabricatorApplicationTransactionTransactionPHIDType::TYPECONST,
  66        ManiphestTaskPHIDType::TYPECONST);
  67  
  68      queryfx(
  69        $conn_w,
  70        'INSERT INTO %T (phid, authorPHID, objectPHID, viewPolicy, editPolicy,
  71            commentPHID, commentVersion, transactionType, oldValue, newValue,
  72            contentSource, metadata, dateCreated, dateModified)
  73          VALUES (%s, %s, %s, %s, %s, %s, %d, %s, %ns, %ns, %s, %s, %d, %d)',
  74        $xaction_table_name,
  75        $xaction_phid,
  76        $row['authorPHID'],
  77        $task_phid,
  78        'public',
  79        $row['authorPHID'],
  80        null,
  81        0,
  82        $xaction_type,
  83        $xaction_old,
  84        $xaction_new,
  85        $xaction_source,
  86        $xaction_meta,
  87        $row['dateCreated'],
  88        $row['dateModified']);
  89    }
  90  
  91    // Now, if the old transaction has a comment, we insert an explicit new
  92    // transaction for it.
  93    if ($has_comment) {
  94      $comment_phid = PhabricatorPHID::generateNewPHID(
  95        PhabricatorPHIDConstants::PHID_TYPE_XCMT,
  96        ManiphestTaskPHIDType::TYPECONST);
  97      $comment_version = 1;
  98  
  99      $comment_xaction_phid = PhabricatorPHID::generateNewPHID(
 100        PhabricatorApplicationTransactionTransactionPHIDType::TYPECONST,
 101        ManiphestTaskPHIDType::TYPECONST);
 102  
 103      // Insert the comment data.
 104      queryfx(
 105        $conn_w,
 106        'INSERT INTO %T (phid, transactionPHID, authorPHID, viewPolicy,
 107            editPolicy, commentVersion, content, contentSource, isDeleted,
 108            dateCreated, dateModified)
 109          VALUES (%s, %s, %s, %s, %s, %d, %s, %s, %d, %d, %d)',
 110        $comment_table_name,
 111        $comment_phid,
 112        $comment_xaction_phid,
 113        $row['authorPHID'],
 114        'public',
 115        $row['authorPHID'],
 116        $comment_version,
 117        $row['comments'],
 118        $xaction_source,
 119        0,
 120        $row['dateCreated'],
 121        $row['dateModified']);
 122  
 123      queryfx(
 124        $conn_w,
 125        'INSERT INTO %T (phid, authorPHID, objectPHID, viewPolicy, editPolicy,
 126            commentPHID, commentVersion, transactionType, oldValue, newValue,
 127            contentSource, metadata, dateCreated, dateModified)
 128          VALUES (%s, %s, %s, %s, %s, %s, %d, %s, %ns, %ns, %s, %s, %d, %d)',
 129        $xaction_table_name,
 130        $comment_xaction_phid,
 131        $row['authorPHID'],
 132        $task_phid,
 133        'public',
 134        $row['authorPHID'],
 135        $comment_phid,
 136        $comment_version,
 137        PhabricatorTransactions::TYPE_COMMENT,
 138        $xaction_old,
 139        $xaction_new,
 140        $xaction_source,
 141        $xaction_meta,
 142        $row['dateCreated'],
 143        $row['dateModified']);
 144    }
 145  }
 146  
 147  $conn_w->saveTransaction();
 148  echo "Done.\n";


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1