• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Database Migration confusion

#1
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
Reply

#2
@MeltedRolo,

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

#3
You can create migrations that simply alter a table. See the full docs for Forge.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#4
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.
Reply

#5
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;");
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#6
Thanks for the replies guys, very helpfull.
Reply

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

Thanks again.
Reply

#8
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();
}
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.