Welcome Guest, Not a member yet? Register   Sign In
Migrations - very confusing
#1

(This post was last modified: 02-05-2016, 08:55 AM by Wouter60.)

In my application/config/migration.php file, I have the following settings:
PHP Code:
$config['migration_enabled'] = TRUE;
$config['migration_type'] = 'sequential';
$config['migration_table'] = 'migrations';
$config['migration_auto_latest'] = FALSE;
$config['migration_version'] = 0;
$config['migration_path'] = APPPATH.'migrations/'

Now, in my applicatons/migrations folder I have 2 files:
001_do_step1.php
002_do_step2.php

Migration step 001 has already run, it adds a field to one of my tables in the database.
Now I want to add another field, so I try to run the migration for step 2:

migrate/do_migration/002

However, I get the error that I try to add a duplicate field,

Code:
Error Number: 1060
Duplicate column name 'field1'
ALTER TABLE `table1` ADD `field1` TINYINT(1) NOT NULL DEFAULT 0
Filename: ./application/migrations/001_do_step1.php
Line Number: 24

Why doesn't it migrate using the file that starts with 002 ???
Reply
#2

(This post was last modified: 02-05-2016, 09:06 AM by keulu.)

in my app,

my config migration_version is set to 1 and it's run all my 001_* files. did you try to use version 2 ?

I use this piece of code

PHP Code:
$this->load->library('migration');
        
if (
$this->migration->current() === FALSE)
{
    
show_error($this->migration->error_string());
    exit;
}
$version $this->migration->current();
echo 
'Migration Done for Migration_id : ' $version
Reply
#3

Yes I did. Has no effect. I'm really puzzled by this.
Reply
#4

did your classname is good ?

001_do_step1.php -> class Do_Step1 extends CI_Migration
002_do_step2.php -> class Do_Step2 extends CI_Migration
Reply
#5

you can use $this->migration->version(xxx); instead of current()

http://www.codeigniter.com/user_guide/li...n::version
Reply
#6

If I rename 001_do_step1.php to 001_do_step1.txt, it works.
Your example still generated the "duplicate field" error, because it calls $this->migration->currunt() twice!
I've got it working now with this code:
PHP Code:
    public function do_migrate()
    {
        
$version $this->migration->current();
        if (! 
$version) {
            
show_error($this->migration->error_string());
            exit;            
        }
        echo 
'Migration ' $version ' successful';
    } 

I'm still wondering how to do a roll-back. Each migration file has an "up" method and a "down" method. When is the "up" method called?
Reply
#7

If you need to roll back, you have to use $this->migration->version(<VERSION>);
So in your case latest version is 2, and you need to go back to 1, you would call $this->migration->version(1);
It will automatically call "down" method from 002_do_step2.php.

I personally prefer "latest()" - that way I don't have to change $config['migration_version'] each time I need to migrate to a newer version.
Reply
#8

(This post was last modified: 02-10-2016, 10:09 AM by siburny.)

One more note: $config['migration_version'] = 0 is the version you want to migrate to. Database has a table called "Migrations" and column "version" lists current version you are on (if it's not there it always defaults to 0!).
Reply
#9

This video can help you.
[Video: https://youtu.be/i07XXM37VFk]
Reply




Theme © iAndrew 2016 - Forum software by © MyBB