i.brod
i.brod

Reputation: 4603

Codeigniter migrations: Is there a way to automate table modifications?

I have just learned how to use migrations in Codeigniter. It's nice to have tables automatically created for you in a fresh environment, but what about structural changes to an existing table?

What if i add or remove a column? Is there any way to automate this, without dropping the existing table(which will cause data loss naturally).

Upvotes: 0

Views: 41

Answers (1)

Alex
Alex

Reputation: 9265

If you want to add or remove a column simply use the db forge class: https://www.codeigniter.com/user_guide/database/forge.html

This section of the docs deals with adding/removing a column: https://www.codeigniter.com/user_guide/database/forge.html#modifying-tables

Add:

$fields = array(
        'preferences' => array('type' => 'TEXT')
);
$this->dbforge->add_column('table_name', $fields);

Remove:

$this->dbforge->drop_column('table_name', 'column_to_drop');

All you have to do is use these functions in the up and down functions of the migration class.

Upvotes: 1

Related Questions