Codeigniter migrations + Multiple tables + One migration file per table

I want to start using the DBForge and migrations class built into CI, but I'm not sure how to go about creating a migration for all of my tables.

My thoughts in my installation process of migration contain a file for each of the following tables: advertisements, announcements, config, users, points

. When the user installs the application, it automatically launches through these migration files and creates tables.

IE: 001_advertisements, 001_announcements, 001_config, 001_users, 001_points

001_map_advertisements

class Migration_map_advertisements extends CI_Migration {

public function up(){
    $this->dbforge->add_field(array(
        'id' => array(
            'type' => 'INT',
            'constraint' => 11,
            'unsigned' => TRUE,
            'auto_increment' => TRUE
        ),
        'youtube_id' => array(
            'type' => 'VARCHAR',
            'constraint' => '255',
        ),
        'status' => array(
            'type' => 'int',
            'constraint' => 11,
            'null' => FALSE,
            'default' => 1
        ),
        'timestamp' => array(
            'type' => 'int',
            'constraint' => 11
        ),
        'type' => array(
            'type' => 'VARCHAR',
            'default' => 'video'
        ),
        'filename' => array(
            'type' => 'VARCHAR',
            'constraint' => '255'
        ),
        'url' => array(
            'type' => 'varchar',
            'constraint' => '255'
        ),
        'description' => array(
            'type' => 'varchar',
            'constraint' => 64
        ),
        'title' => array(
            'type' => 'varchar',
            'constraint' => 64
        )
    ));
    $this->dbforge->create_table('map_advertisements', TRUE);
}

public function down()
{
    $this->dbforge->drop_table('map_advertisements');
}

      

However, if I do this and try to start the protected controller:

class Developer extends ADMIN_Controller {
function __construct(){
    parent::__construct();
} #end constructor function
public function migrate($index){
    $this->load->library('migration');

    if ( !$this->migration->version($index) ){
        show_error($this->migration->error_string());
    }else{
        echo 'migrated';
    }

}
}

      

I am getting an error indicating that there are multiple migrations version 1

and this prevents the migration process from going through. Is there any other way to do this in CI?

+3


source to share


2 answers


Yes, you can only have one migration file per version. The migration is table independent, but more like "what schema changes are needed to go from version x to version x + 1 of my application?"



Combine all existing migration files into 1 and name it something like 001_initial_schema.php

. Then, after adding a new function, create a new schematic file for that function. If you have a deployment cycle (like weekly SCRUM sprints), it's nice to have one of these migration files to deploy.

+6


source


According to http://www.codeigniter.com/user_guide/libraries/migration.html , the best way to "version" your migrations is to use the YYYYMMDDHHMMSS format. This should effectively overcome versioning issues as expressed. It also assumes that if the file name was "201507071208_advertisements.php" then the containing migration class should be named "Migration_Advertisements".



+1


source







All Articles