📅  最后修改于: 2023-12-03 15:14:11.480000             🧑  作者: Mango
CodeIgniter 4 Migrations is a database migration tool that allows you to manage changes and updates to your database schema over time. Migrations can help you keep track of changes to your database and ensure that all developers have the same database structure when working on the same project.
To use Migrations in your CodeIgniter 4 project, you must first enable it in your App\Config\Services.php
file. Add the following line to the $aliases
array:
'Migrations' => CodeIgniter\Database\Migrations\MigrationRunner::class,
Once you have enabled Migrations, you can start creating migration files. Migration files are PHP files that use the Migration
class and must be placed in the App\Database\Migrations
directory.
To create a new migration, you can use the following command:
php spark migrate:create MigrationName
Replace MigrationName
with the name of your migration. This will create a new PHP file in the App\Database\Migrations
directory with the name yyyymmddHHiiMigrationName.php
, where yyyymmddHHii
is the current date and time.
Migration files contain two methods: up()
and down()
. The up()
method is used to apply the changes to the database, and the down()
method is used to rollback the changes.
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class Sample_migration extends Migration
{
public function up()
{
// Code to apply the changes to the database
}
public function down()
{
// Code to rollback the changes
}
}
To run your migrations, use the following command:
php spark migrate
This will apply all pending migrations. If you want to rollback the last migration, you can use the following command:
php spark migrate:rollback
CodeIgniter 4 Migrations is a tool that can make managing your database schema changes much easier. By using migrations, you can keep track of changes to your database and ensure that all developers have the same database structure when working on the same project.