Ejemplo n.º 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__);
     }
 }