【问题标题】:How to drop all the tables in Codeigniter using Migration library如何使用迁移库删除 Codeigniter 中的所有表
【发布时间】:2019-04-16 09:42:44
【问题描述】:

我在 Codeigniter 3 中使用 CI 提供的 Migration 库和在线教程的帮助为数据库创建了迁移文件。我的迁移文件如下所示,

<?php

class Migration_Leave_tracker extends CI_Migration {

    public function up() {
        $this->dbforge->add_field(array(
            'id' => array(
                'type' => 'INT',
                'constraint' => 11,
                'auto_increment' => TRUE
            ),
            'emp_id' => array(
                'type' => 'INT',
                'constraint' => 11,
            ),
            'start_date' => array(
                'type' => 'DATE',
            ),
            'end_date' => array(
                'type' => 'DATE',
            ),
            'created_date' => array(
                'type' => 'DATETIME',
            ),
        ));
        $this->dbforge->add_key('id', TRUE);
        $this->dbforge->create_table('leave_tracker');
    }

    public function down() {
        $this->dbforge->drop_table('leave_tracker');
    }

}

在这里你可以看到我的迁移文件有两种方法,一种是up(),用于创建表,另一种是down(),用于删除表。

我有另一个控制器可以运行迁移,

public function migrate($version = null) {
        $this->load->library('migration');

        if ($version != null) {
            if ($this->migration->version($version) === FALSE) {
                show_error($this->migration->error_string());
            } else {
                echo "Migrations run successfully" . PHP_EOL;
            }

            return;
        }

        if ($this->migration->latest() === FALSE) {
            show_error($this->migration->error_string());
        } else {
            echo "Migrations run successfully" . PHP_EOL;
        }
    }

根据 CI 文档,这段代码将创建所有迁移,我猜在后台它将调用所有迁移类的 up() 方法来创建表。

现在我的问题是如何创建一个方法,该方法将使用迁移类的drop() 方法删除数据库中的所有表。我在文档中找不到任何参考。

【问题讨论】:

  • 通常迁移文件只会删除在类的up() 中创建的表(在down() 中)。
  • @DFriend 这就是我想要的,但问题是如何做到这一点。
  • 我对您的陈述感到困惑,“...将删除数据库中的所有表”。 (强调我的)我想出了一种方法来做到这一点,但那是一段时间前的事了。我会在时间允许的时候寻找代码并在这里分享。可能是一两天,

标签: php codeigniter-3 database-migration


【解决方案1】:

我使用了以下迁移控制器,效果很好。

class Migrator extends CI_Controller
{
    public function __construct($config = array())
    {
        parent::__construct($config);
        $this->load->library('migration');
    }

    public function migrate($version = NULL)
    {

        $outcome = $this->migration->version($version);

        if(is_string($outcome))
        {
            echo "Migration to version $outcome succeeded.";
        }
        elseif($outcome === TRUE)
        {
            echo "No migration was possible. Target version is the same as current version.";
        }
        else
        {
            echo $this->migration->error_string();
        }
    }

    public function latest() //you could this for migration::current() too
    {
        $this->migration->latest();
    }

}

假设在迁移中使用“顺序”编号并且class Migration_Leave_tracker 在文件001_Migration_Leave_tracker.php

然后浏览到http://example.com/migrator/migrate/1 将运行Migration_Leave_tracker::up()

要恢复原状,只需使用较低的序列号调用migrate,例如。 http://example.com/migrator/migrate/0 这将导致 Migration_Leave_tracker::down()` 被调用。 (至少它对我有用。)

时间戳编号也可以使用,但对于“最终关闭”,请使用零作为 URL 的参数。换句话说,只需使用http://example.com/migrator/migrate/0 就像为顺序编号所做的那样。

【讨论】:

  • 感谢它的帮助。我不知道如果提供 0 作为迁移版本,那么它会删除表。
  • 它没有记录,但可能应该记录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 2019-10-15
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 2011-04-30
  • 2011-10-29
相关资源
最近更新 更多