dongmeng9048 2017-04-05 16:18
浏览 27
已采纳

如果数据库为空,如何运行Codeigniter迁移

I'm moving my Codeigniter 3.1.4 development environment into a Docker container at the moment. When running a docker image for the first time, I would like Codeigniter to automatically create tables in the database if the database is empty rather than me having to export my current development database and then import it into the Mysql docker.

I already use the migration library during development (via the command-line) to make changes to the database schema. It sounds to me like the migration library would be perfect for this task, but I can't think of where I should call it from. E.g. - do I add some code to the end of database.php to check if the database exists and then call my migration controller somehow?

  • 写回答

1条回答 默认 最新

  • duandu6497 2017-04-25 18:53
    关注

    I found one way to accomplish this. In MY_Controller class, which is a class that extends CI_Controller, I check the database to see if the table ci_sessions exists. If not, I make the assumption that the database is empty and I should run the migration. Here is an example:

    class MY_Controller extends CI_Controller {
    
        function __construct() {
            parent::__construct();
    
            // If ci_sessions doesn't exist, run db migration (via command-line)
            if (!$this->db->table_exists('ci_sessions')) {
                $output = shell_exec("php index.php migrate");
                if (strncmp($output, "Migration worked!", 17) != 0) {
                    exit($output);
                }
            }
    
            $this->load->library ( 'session' );
            $this->load->model ( 'user_model' );
    
        }
    }
    

    Notice that I'm running the migration via a shell command. This is in order to prevent URL access to the migration controller. There might be a neater way of doing this but I haven't figured out how yet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序