Beispiel #1
0
 protected function changeField($table, $field, $newtype, $default)
 {
     $fi = $this->db->fieldInfo($table, $field);
     if (is_null($fi)) {
         $this->output("...ERROR: expected column {$table}.{$field} to exist\n");
         exit(1);
     }
     if ($fi->type() === $newtype) {
         $this->output("...column '{$table}.{$field}' is already of type '{$newtype}'\n");
     } else {
         $this->output("Changing column type of '{$table}.{$field}' from '{$fi->type()}' to '{$newtype}'\n");
         $sql = "ALTER TABLE {$table} ALTER {$field} TYPE {$newtype}";
         if (strlen($default)) {
             $res = array();
             if (preg_match('/DEFAULT (.+)/', $default, $res)) {
                 $sqldef = "ALTER TABLE {$table} ALTER {$field} SET DEFAULT {$res['1']}";
                 $this->db->query($sqldef);
                 $default = preg_replace('/\\s*DEFAULT .+/', '', $default);
             }
             $sql .= " USING {$default}";
         }
         $this->db->begin(__METHOD__);
         $this->db->query($sql);
         $this->db->commit(__METHOD__);
     }
 }
 /**
  * @param DatabasePostgres $dbw
  * @param int $id
  * @param LoggerInterface $logger
  */
 public function __construct(DatabasePostgres $dbw, $id, LoggerInterface $logger)
 {
     $this->dbw = $dbw;
     $this->logger = $logger;
     $this->id = $id;
     $this->didbegin = false;
     /* If we are not in a transaction, we need to be for savepoint trickery */
     if (!$dbw->trxLevel()) {
         $dbw->begin(__CLASS__, DatabasePostgres::TRANSACTION_INTERNAL);
         $this->didbegin = true;
     }
 }