Reputation: 1088
My migration is not working. I receive "Migration Work" in my browser but no blog table created which is what I expect.
This checks the revision number in migration.php and then looks for a file that begins with that number (eg. 001_) in migrations folder.
<?php
// this is controller/Migration.php
class Migration extends CI_Controller {
function index() {
$this->load->library('migration');
if ( ! $this->migration->current()) {
show_error($this->migration->error_string());
} else {
echo "Migration Worked";
}
}
}
This is 002_install_blog.php It creates the blog table.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Install_blog extends CI_Migration {
public function up()
{
// Drop table 'blog' if it exists
$this->dbforge->drop_table('blog', TRUE);
// Table structure for table 'blog'
$this->dbforge->add_field(array(
'id' => array(
'type' => 'MEDIUMINT',
'constraint' => '8',
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'slug' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'body' => array(
'type' => 'TEXT',
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('blog');
}
public function down()
{
$this->dbforge->drop_table('blog', TRUE);
}
}
This is migration.php. It holds the migration version. It is set to 2 in order to use 002_install_blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['migration_enabled'] = TRUE;
$config['migration_type'] = 'sequential';
$config['migration_table'] = 'migrations';
$config['migration_auto_latest'] = TRUE;
$config['migration_version'] = '2';
$config['migration_path'] = APPPATH.'migrations/';
Upvotes: 1
Views: 2269
Reputation: 1088
I figured this out. I have a table called 'migrations'. I incorrectly dropped my table inside of mysql and did not by running the migration file. Then, I ran the migration file, however, the version number in the 'migrations' table was still set to 2. I set the version number to 1 manually and then ran the migration file. Looked at the version number in the migrations table after running it and sure enough the version was 2.
Moral of the story. No need to drop tables manually. It should be all handled in the migration file!
Upvotes: 2