PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 14, 2022

[FIXED] Codeigniter Migrations + Multiple Tables + One Migration File Per Table

 March 14, 2022     codeigniter, migration, mysql     No comments   

Issue

I want to begin using DBForge and migrations class thats built into CI but im not sure how to go about creating migrations for all of my tables.

My thoughts are in my installer process to have a migration file for each of the following tables: advertisements, announcements, config, users, points.. When the user is installing the app, it will automatically run through these migration files and create the 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 attempt to run a secured 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 get an error stating that there are multiple version 1 migrations and it fails the migration process. Is there another way to go about this in CI?


Solution

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

Combine all of your existing migration files into 1, and name it something like 001_initial_schema.php. Then, when add a new feature, create a new schema file for that feature. If you have a deployment cycle (like weekly SCRUM sprints), it's nice to have one of these migration files per deployment.



Answered By - landons
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing