How do I run CodeIgniter migrations?

前端 未结 5 1608
我寻月下人不归
我寻月下人不归 2020-12-12 23:42

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?

相关标签:
5条回答
  • 2020-12-13 00:20

    This is simplest Codeigniter Database Migrations

    1. Configure application/database.php to your database name settings.

    2. 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";
              }
          }
      }
      
    3. In application\migration.php, change $config['migration_enabled'] = TRUE; .

    4. open CLI in folder and type php index.php migrate

    0 讨论(0)
  • 2020-12-13 00:24

    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)

    1. make $config['migration_enabled'] = TRUE; in /application/config/migration.php
    2. define migration version $config['migration_version'] = 20201019123900; like this
    3. set $config['migration_auto_latest'] = TRUE;
    4. autoload or manually load migration library (define it in /application/config/autoload.php like $autoload['libraries'] = array('migration'); or load it in controller constructor like $this->load->library('migration');)
    5. and just run your application (open it via browser)

    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.

    0 讨论(0)
  • 2020-12-13 00:25

    Using these pages as references: Running via the CLI and Migration Class 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 domain.com/migrate you will see the text in the script above.

    0 讨论(0)
  • 2020-12-13 00:36

    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.

    0 讨论(0)
  • 2020-12-13 00:44

    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

    0 讨论(0)
提交回复
热议问题