如何运行 CodeIgniter 迁移?
我知道如何通过 http://codeigniter.com/user_guide/libraries/migration.html< 创建它们/a>
但是一旦我创建了迁移文件,我该如何运行它们呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您还可以运行某些版本进行向下或向上迁移:
对于 CLI,运行此命令
php index.php migrate version 5
,其中5
是迁移版本。如果版本大于当前迁移的版本 - 向上迁移,否则 - 向下迁移到输入的版本。You can also run some version for down or up migrations:
For CLI run this command
php index.php migrate version 5
, where5
is version of migration. If version is more of current migration - migration up, else - down to entered version.这是最简单的 Codeigniter 数据库迁移
将 application/database.php 配置为您的数据库名称设置。
创建应用程序/配置 mirate.php
在 application\migration.php 中,更改
$config['migration_enabled'] = TRUE;
。在文件夹中打开 CLI 并输入
php index.php migrate
This is simplest Codeigniter Database Migrations
Configure application/database.php to your database name settings.
Create application/config mirate.php
In application\migration.php, change
$config['migration_enabled'] = TRUE;
.open CLI in folder and type
php index.php migrate
我想我这里有最简单的解决方案。 (适用于 Codeigniter 3.1.11)
上述解决方案建议通过 url 或 cli 进行迁移。
第一个问题是你将这个应该是幕后的问题公开。
第二个问题是,如果您在共享托管平台上部署此项目,您将没有机会通过 cli 运行它。
因此,我认为最简单的解决方案是让 codeigniter 为我们完成工作:
(假设您已完成数据库设置并正确创建迁移)
$config['migration_enabled'] = TRUE;
$config['migration_version'] = 20201019123900;
像这样$config['migration_auto_latest'] = TRUE;
$autoload['libraries'] = array('migration');
或者将其加载到控制器构造函数中,例如$this->load->library('migration');
)Tata!就这样吧。您可以检查数据库是否正确创建了表。
开发环境保持这样的设置应该没问题。
但对于生产环境,我们应该重置
$config['migration_enabled'] = FALSE;
,正如评论部分指出的那样。这个解决方案可能会受到批评,因为每次运行应用程序或控制器时都会加载迁移库,但我并没有说这是最好的解决方案,我说过这是最简单的解决方案。
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.
在 application\migration.php 中,更改 $config['migration_enabled'] = TRUE; .这是实际的 CI migration.php 文件路径对吧
你说的是 application\migration.php ,实际是application\config\migration.php。
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.
CI 版本 4 的答案 (2022):
在版本 4 中略有不同:https://codeigniter.com/user_guide/dbmgmt/migration.html
注意,如果您还想回滚迁移,我已经包含了回归选项。
A CI version 4 answer (2022):
In version 4 it is slightly different: https://codeigniter.com/user_guide/dbmgmt/migration.html
Note I've included the regress option if you wish to roll back your migrations as well.
您可以通过以下方式将对迁移控制器的访问限制为命令行(
application/controllers/migrate.php
):然后要执行最新的迁移,请 cd 进入项目的根目录目录并运行:
但是当您尝试通过网络服务器
example.com/migrate
访问时,您将看到上面脚本中的文本。You're able to restrict access to your migration controller to command line with something along these lines (
application/controllers/migrate.php
):then to execute your latest migration, cd into the root of your project directory and run:
but when you attempt to access via webserver
example.com/migrate
you will see the text in the script above.我不确定这是否是正确的方法,但它对我有用。
我创建了一个名为
migrate
(controllers/migrate.php) 的控制器。然后从浏览器中我将调用此网址以在
migrate
控制器中执行index
操作例如:http://localhost/index.php/migrate/index/1
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).Then from browser I will call this url to execute
index
action inmigrate
controllerEg : http://localhost/index.php/migrate/index/1