I know how to create them via http://codeigniter.com/user_guide/libraries/migration.html
But once I've created my migration files, how do I run them?
I know how to create them via http://codeigniter.com/user_guide/libraries/migration.html
But once I've created my migration files, how do I run them?
I am not sure this is the right way to do it, But It works for me.
I created a controller named migrate
(controllers/migrate.php).
<?php defined("BASEPATH") or exit("No direct script access allowed");
class Migrate extends CI_Controller{
public function index($version){
$this->load->library("migration");
if(!$this->migration->version($version)){
show_error($this->migration->error_string());
}
}
}
Then from browser I will call this url to execute index
action in migrate
controller
Eg : http://localhost/index.php/migrate/index/1
ENVIRONMENT
–
Ryurik You're able to restrict access to your migration controller to command line with something along these lines (application/controllers/migrate.php
):
<?php if ( ! defined('BASEPATH')) exit("No direct script access allowed");
class Migrate extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->input->is_cli_request()
or exit("Execute via command line: php index.php migrate");
$this->load->library('migration');
}
public function index()
{
if(!$this->migration->latest())
{
show_error($this->migration->error_string());
}
}
}
then to execute your latest migration, cd into the root of your project directory and run:
php index.php migrate
but when you attempt to access via webserver example.com/migrate
you will see the text in the script above.
is_cli_request()
, verify your CI version and see if the call is different. Commenting it out would then allow migrations to be run in browser, which is what should be avoided. –
Barnabe I am not sure this is the right way to do it, But It works for me.
I created a controller named migrate
(controllers/migrate.php).
<?php defined("BASEPATH") or exit("No direct script access allowed");
class Migrate extends CI_Controller{
public function index($version){
$this->load->library("migration");
if(!$this->migration->version($version)){
show_error($this->migration->error_string());
}
}
}
Then from browser I will call this url to execute index
action in migrate
controller
Eg : http://localhost/index.php/migrate/index/1
ENVIRONMENT
–
Ryurik You can also run some version for down or up migrations:
if(!defined('BASEPATH')) exit('No direct script access allowed');
class Migrate extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->library('migration');
}
public function version($version)
{
if($this->input->is_cli_request())
{
$migration = $this->migration->version($version);
if(!$migration)
{
echo $this->migration->error_string();
}
else
{
echo 'Migration(s) done'.PHP_EOL;
}
}
else
{
show_error('You don\'t have permission for this action');;
}
}
}
For CLI run this command php index.php migrate version 5
, where 5
is version of migration. If version is more of current migration - migration up, else - down to entered version.
Configure application/database.php to your database name settings.
Create application/config mirate.php
<?php defined("BASEPATH") or exit("No direct script access allowed");
class Migrate extends CI_Controller
{
public function index()
{
if (ENVIRONMENT == 'development') {
$this->load->library('migration');
if (!$this->migration->current()) {
show_error($this->migration->error_string());
} else {
echo "success";
}
} else {
echo "go away";
}
}
}
In application\migration.php, change $config['migration_enabled'] = TRUE;
.
open CLI in folder and type php index.php migrate
I think I have the simplest solution around here. (This is for Codeigniter 3.1.11)
The solutions above either suggest doing the migration through url or cli.
Problem with the first one is you make this should-be-behind-the-scenes issue publicly available.
Problem with the second one is if you are deploying this project on shared hosting platform you don't have the chance to run it via cli.
So the simplest solution to my opinion is to let codeigniter do the work for us: (assuming that you have done your database settings and created migrations properly)
$config['migration_enabled'] = TRUE;
in /application/config/migration.php$config['migration_version'] = 20201019123900;
like this$config['migration_auto_latest'] = TRUE;
$autoload['libraries'] = array('migration');
or load it in controller constructor like $this->load->library('migration');
)Tata! There you go. You can check your database if your tables have been created correctly.
It should be OK for development environment to leave the settings like this.
BUT for production environment we should reset $config['migration_enabled'] = FALSE;
as it is pointed out in the comment section.
This solution might be criticized because migration library is loaded each time the application or the controller is run but I haven't said it's the best solution, I have said it's the simplest solution.
In application\migration.php, change $config['migration_enabled'] = TRUE; .this is the actual CI migration.php file path right
You said that application\migration.php, actual is application\config\migration.php.
<?php defined("BASEPATH") or exit("No direct script access allowed");
class Migrate extends CI_Controller
{
public function index()
{
if (ENVIRONMENT == 'development') {
$this->load->library('migration');
if (!$this->migration->current()) {
show_error($this->migration->error_string());
} else {
echo "success";
}
} else {
echo "go away";
}
}enter code here
}
A CI version 4 answer (2022):
In version 4 it is slightly different: https://codeigniter.com/user_guide/dbmgmt/migration.html
namespace App\Controllers;
use CodeIgniter\Controller;
use Throwable;
class Migrate extends Controller {
public function index(){
$migrate = \Config\Services::migrations();
try {
$migrate->latest();
$migrate->regress();
echo 'Migration complete';
} catch (Throwable $e) {
print'<pre>';print_r($e);print'</pre>';
}
}
}
Note I've included the regress option if you wish to roll back your migrations as well.
© 2022 - 2024 — McMap. All rights reserved.