Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Migration advice
  • Hey, 

    I have migrations, on one I did a default create a record on category and later in the last migration I added a field to category. The problem is that when I create a new app, the record can't work with the missed field (because model has it setup) without run the last migration.

    So the question, is the default records creation is the right place in migration ? How I can solve this issue on my migration code ?
  • Not sure I understand what you mean.

    Your entire database should be created by migrations, migrations should be incremental and reversable. So if you deploy your code and run all migrations, the database schema is at the version the code expects.

    What do you mean with "create a new app", and why is that app not up to date with your migrations?


  • ok.

    in migration 01 : I create category
    in migration 02 : I create default records in category :
    $category= \Model_Category::forge(array('status'=>'published', 'name'=> 'Toutes les rubriques', 'slug'=>'all', 'exposition'=>'all', 'post_count'=>0, 'order_id'=>NULL, 'permission'=>'public' ));
    $category->save();

    In migration 03 : I add a visibility field to category (so I change my model).

    All my app track this migration and run fine.
    But if I create a new app of this, it would like an error at migration 02 because Model has an extra 'visibility' field ?
  • You are talking about a new application against an already created database? 

    Have you checked what happens?

    My guess would be nothing, if there is no migration config file, but the database has a migration table, the config file will be synced with the database. The database status is leading.

    Having said that, multiple applications with migrations on the same database is waiting for a disaster to happen, so if I were you, I'd select one app as the primary app for that database, and make sure all migrations for that database are made in that app.
  • I just moved the records created in 02 to 03 after the field creation, and work. I guess its not good to put records creation in DB from migration.

    Its just an error you can't create things in Database when you have tables change in migrations because the migration doesn't track Model changes.
  • HarroHarro
    Accepted Answer
    I don't see why you can't do that, we create records all the time in migrations.

    Indeed, using Models might be a challenge, we always use direct DB calls to avoid issues like that.
  • // Indeed, using Models might be a challenge, we always use direct DB calls to avoid issues like that.

    ah perfect, I will try out from DB call, thanks !
  • How I use DB, 

    Using Model is  :
    $category= \Model_Category::forge(array('status'=>'published', 'name'=> 'Toutes les rubriques', 'slug'=>'all', 'exposition'=>'all', 'post_count'=>0, 'order_id'=>NULL, 'permission'=>'public' ));

    Using DB :
    $query = \DB::insert('categories', array('status'=>'published', 'name'=> 'Toutes les rubriques', 'slug'=>'all', 'exposition'=>'all', 'post_count'=>0, 'order_id'=>NULL, 'permission'=>'public' ) );

    or :
    $query = DB::insert('categories');
    $query->set(array( 'status'=>'published', 'name'=> 'Toutes les rubriques', 'slug'=>'all', 'exposition'=>'all', 'post_count'=>0, 'order_id'=>NULL, 'permission'=>'public'));

    it doesn't add record, need to execute it ?

  • ok it just need execute :)

    \DB::insert('categories')->set(array( 'status'=>'published', 'name'=> 'Toutes les rubriques', 'slug'=>'all', 'exposition'=>'all', 'post_count'=>0, 'order_id'=>NULL, 'permission'=>'public'))->execute();

    thanks for your input Harro
  • HarroHarro
    Accepted Answer
    ;-)

    You're welcome.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion