CodeIgniter Forums
Database Migration confusion - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Database Migration confusion (/thread-73438.html)



Database Migration confusion - MeltedRolo - 04-24-2019

Hi guys

Currently trying to wrap my head around database migrations, and I guess I just dont get how they are supposed to be used.

I can see that you can create database tables, and also drop them, but what I dont see in the documentation is, say you want to add a field to a table, have you got to create the table again in another file with the changes, or just issue an update?

If someone could explain how they use them it would be appreciated.

Thanks


RE: Database Migration confusion - php_rocs - 04-24-2019

@MeltedRolo,

Look for an Alter Table statement. This statement allows you to add a new column to a pre-existing table.


RE: Database Migration confusion - kilishan - 04-24-2019

You can create migrations that simply alter a table. See the full docs for Forge.


RE: Database Migration confusion - MeltedRolo - 04-24-2019

Ok

Let me explain my confusion further.

Say I design and release an app, that makes use of many tables in a database. I,m assuming for each table, there will be a seperate migration with an up and down method. Each migration has the same timestamp.

Then I release an update with a modified schema. I create the migration file that just updates one table and (I'm assuming here) that this then has a different timestamp.

This poses a couple of questions in my mind.

For the original schema, if I have foreign keys in my tables, they will need to be created in a specific order, how can this be accomplished?

Then, if someone installs the app for the first time, all the tables will need to be created and updated, how is this accomplished?

Thanks.


RE: Database Migration confusion - kilishan - 04-24-2019

You would need to disable foreign keys before the migration, then enable them after. Unfortunately, that's a hole in our current db layer (though I should be creating some methods for that in the next couple of weeks).

For mysql, you can use:

Code:
$db->query("SET FOREIGN_KEY_CHECKS=0;");

// do your migrations

$db->query("SET FOREIGN_KEY_CHECKS=1;");



RE: Database Migration confusion - MeltedRolo - 04-25-2019

Thanks for the replies guys, very helpfull.


RE: Database Migration confusion - MeltedRolo - 04-25-2019

These methods that will be added, will they just be concerning the foreign keys? or more than this?

Thanks again.


RE: Database Migration confusion - kilishan - 06-07-2019

Sorry - missed the last response.

I actually just added those new commands last night, so the latest in the develop branch has the following commands you can use during migrations:

Code:
public function up()
{
    $this->db->disableForeignKeyConstraints();

    // do your migrations here...

    $this->db->enableForeignKeyConstraints();
}