For some reason, there are two different database table API’s - it seems one is for creating new tables, and the other for modifying existing tables.
When you do
$installer->getConnection()->newTable()
you get an instance of Varien_Db_Ddl_Table
.
Its addColumn
method signature looks like this:
$table->addColumn('id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true,
), ‘Label');
When you write
$installer->getConnection()
, you get an instance of Magento_Db_Adapter_Pdo_Mysql
.
This API allows you to change or add columns to existing tables. It’s method signature is slightly different:
$installer->getConnection()->addColumn(
$installer->getTable('entityname'),
'my_column_name',
$columnOptions
);
Where the column options can be something like:
$columnOptions = array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_TEXT,
'LENGTH' => 255,
'NULLABLE' => true,
'COMMENT' => 'Image file name',
);
This would be pretty straightfoward, but there is some other weird stuff. For example, foreign keys can be added using both API's. It seems the two code bases are trying to do the same thing.
It was definitely some of the confusing parts of Magento for me. See also this Stackoverflow thread.