Reputation: 4130
now i'm trying to use migration into my Codeigniter project.
I have created a few migration files on application/migrations
, and Migrate.php
as a controller to make a table when i try to migrate a table.
I'll give the sample of migrations file and what is in Migrate.php
at the and of this thread.
The problem happen when i run the migration, i run with cli php index.php migrate current
. It shows that migration success
, but only migrations
table that is created. Anyone have same problem with me?
Migrate.php
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Migrate extends CI_Controller {
function __construct()
{
parent::__construct();
if(! $this->input->is_cli_request()) {
show_404();
exit;
}
$this->load->library('migration');
}
function current()
{
if ($this->migration->current()) {
log_message('error', 'Migration Success.');
echo "Migration Success";
} else {
log_message('error', $this->migration->error_string());
echo $this->migration->error_string();
}
}
function rollback($version)
{
if ($this->migration->version($version)) {
log_message('error', 'Migration Success.');
echo "Migration Success";
} else {
log_message('error', $this->migration->error_string());
echo $this->migration->error_string();
}
}
function latest()
{
if ($this->migration->latest()) {
log_message('error', 'Migration Success.');
echo "Migration Success";
} else {
log_message('error', $this->migration->error_string());
echo $this->migration->error_string();
}
}
}
Sample of migrations file.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Add_Facility_Types extends CI_Migration {
public function up()
{
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'name' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('facility_types');
}
public function down()
{
$this->dbforge->drop_table('facility_types');
}
}
Upvotes: 1
Views: 5124
Reputation: 2153
Each Migration is run in numeric order forward or backwards depending on the method taken. Two numbering styles are available:
Sequential: each migration is numbered in sequence, starting with 001. Each number must be three digits, and there must not be any gaps in the sequence. (This was the numbering scheme prior to CodeIgniter 3.0.)
Timestamp: each migration is numbered using the timestamp when the migration was created, in YYYYMMDDHHIISS format (e.g. 20121031100537). This helps prevent numbering conflicts when working in a team environment, and is the preferred scheme in CodeIgniter 3.0 and later. The desired style may be selected using the $config['migration_type'] setting in your application/config/migration.php file.
Regardless of which numbering style you choose to use, prefix your migration files with the migration number followed by an underscore and a descriptive name for the migration. For example:
001_add_blog.php (sequential numbering)
20121031100537_add_blog.php (timestamp numbering)
From the docs, you have to follow this file naming pattern, if you decided to use timestamps to you got to precede the file name with a complete datetime format then underscore filename.
That's why i prefer to use sequential file naming cause its way easier to use, got to the migration configs file and change the migration_type
to sequential and in the migration_version
give it the last file number.
lets say you have 001_create_sessions.php
, 002_create_users.php
and 003_create_post.php
.. then in the migration_version
give it 3 but don't forget sequential type.
Upvotes: 4