You are here

function _flashnode_db_add_column in Flash Node 5.3

Same name and namespace in other branches
  1. 5.6 flashnode.install \_flashnode_db_add_column()
  2. 5.2 flashnode.install \_flashnode_db_add_column()

Add a column to a database using syntax appropriate for PostgreSQL. Save result of SQL commands in $ret array.

This code is the same as update.php except we replace update_sql with db_query. We have to repeat it here as during a module install we don't have the function available to us.

1 call to _flashnode_db_add_column()
_flashnode_migrate_from_flash in ./flashnode.install
If flashnode is being installed in place of flash then to Drupal it looks like the first time this module has been installed. In reality we have an existing table we want to keep with our existing flash content, so we have to simulate the update…

File

./flashnode.install, line 414

Code

function _flashnode_db_add_column(&$ret, $table, $column, $type, $attributes = array()) {
  if (array_key_exists('not null', $attributes) and $attributes['not null']) {
    $not_null = 'NOT NULL';
  }
  if (array_key_exists('default', $attributes)) {
    if (is_null($attributes['default'])) {
      $default_val = 'NULL';
      $default = 'default NULL';
    }
    elseif ($attributes['default'] === FALSE) {
      $default = '';
    }
    else {
      $default_val = "{$attributes['default']}";
      $default = "default {$attributes['default']}";
    }
  }
  db_query("ALTER TABLE {" . $table . "} ADD {$column} {$type}");
  if ($default) {
    db_query("ALTER TABLE {" . $table . "} ALTER {$column} SET {$default}");
  }
  if ($not_null) {
    if ($default) {
      db_query("UPDATE {" . $table . "} SET {$column} = {$default_val}");
    }
    db_query("ALTER TABLE {" . $table . "} ALTER {$column} SET NOT NULL");
  }
}