Reputation: 1057
I am trying to add reset function to codeigniter's migration. Below is my code:
class Migration extends Backend_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('migration');
}
public function index()
{
//...
}
public function reset()
{
$this->migration->version(1);
$this->db->truncate('ci_sessions');
$this->migration->current();
}
}
It returns error:
Fatal error: Cannot redeclare class Migration_Create_geo_data in D:\web_projects\vProject\framework\application\migrations\002_create_geo_data.php on line 44
If I run them seperately, all are okay. When together it gives error. Any idea?
Upvotes: 1
Views: 2404
Reputation: 3070
Most likely, this error is a result of setting your migrations to create tables if they do not already exist AND cache data not being updated right away.
Your Migration script calls the DB_forge::create_table
method which takes two parameters. Parameter one is the table name and parameter two is optional. It is the if_not_exists
flag. The default value is false however; if it is set to true tables will only be created if they do not already exist.
If your tables are created with the if_not_exists
parameter set to false the caching issue will (probably) never happen:
$this->dbforge->create_table('table_name');
If tables are created with the if_not_exists
parameter set to true, you will need to force the cache to update when reloading your migrations.
$this->dbforge->create_table('table_name', TRUE);
Here are a couple options to avoid this issue:
create_table
methoddata_cache['table_names']
after the migration->version(0)
callIf you choose option 2, here is a method that works:
public function reset() {
$this->load->library('migration');
if (!$this->migration->version(0)) {
echo $this->migration->error_string();
}
// unset table cache - this will force it to update
unset($this->db->data_cache['table_names']);
if (!$this->migration->current()) {
echo $this->migration->error_string();
}
}
In addition to this, the migration files are autoloaded and saved in the session.
I changed this line in system/libraries/Migration.php: include $f[0];
to include_once $f[0];
Upvotes: 2
Reputation: 3675
Most likely, you made a migration by copy/pasting from an earlier one & now have two migration files with the same class declared
ie,
class Migration_Add_blog extends CI_Migration
in two files
Upvotes: 1