How to implement database migration in CodeIgniter-4 Commands?

How to implement database migration in CodeIgniter-4 Commands?

migration in CodeIgniter-4 Commands

Websites have become more demanding for businesses, now-a-days they deliver more values than past years. As per reports by w3techs, PHP – Most admired scripting language for frontend and backend which is widely used as compared to other programming languages. Around 77% of all the websites on the internet are developed in PHP and other PHP Frameworks.

When the time comes to decide with which PHP framework one should go for web development projects, it should be an easy-to-decide task. With help of these PHP frameworks website development tasks can be versatile & faster. Amongst them, CodeIgniter is one of the most widely used PHP frameworks.

Modern day programming is used for developing large scale projects nowadays and the work is distributed under several developers. Each of them have to work with either a separate database or work on the same Database. And the developers need to change and also keep track of the database for the same project. In this case database migration comes in picture.

Database migrations in Codeigniter-4 are used to track and run the changes of the database with the help of Command Line Interface with the help of Spark. Spark is the built-in official commands provided by Codeigniter-4.

See Also: Developers’ guide for establishing and adhering to Coding Standards

These are some of the most used database migration spark commands used in Codeigniter-4. Open the project directory in Command Prompt and run the following commands:

1. php spark make:migration [name-of-migration]

This automatically creates the migration file for the project under app/Database/Migrations/ directory in the project.

Example:

php spark make:migration Testmigrate
In this file there are mainly two functions

  1. up()
  2. down()
Example:
Public function up()
{
$this->forge->addField([
            'blog_unique_id' => [
                'type'           => 'BIGINT',
                'constraint'     => 20,
                'auto_increment' => true,
            ],
            'blog_title' => [
                'type'       => 'VARCHAR',
                'constraint' => '150',
            ],
            'blog_details' => [
                'type' => 'TEXT',
                'null' => true,
            ],
        ]);
        $this->forge->addKey(blog_unique_id, true);
        $this->forge->createTable('tbl_blog');
}

In down() functions handles the rollback operations performs on the database 

Example
public function down()
    {
        $this->forge->dropTable('tbl_blog');
    }

In the given example the in down method there are dropTable blog for remove table from Database

See Also: 8 Advantages of Custom PHP Development

2. php spark migrate

Migrate command is used to execute the migrations and perform Database Operations. The above command runs and executes all the migrations. It executes only up() method in migrations file

3. php spark migrate:rollback

The above Command runs and executes the down() method of the migration.

4. php spark migrate:refresh

The above Command Refreshes the database by simply performing rollback first and then the migration. It means first it executes the down() method and then the up() method of the migration 

5. php spark migrate:status

It displays the graphical representation of the status of all the migrations 

Example

php spark migrate:status

6. php spark make:seeder [name-of-seeder]

This automatically creates the migration file for the project under app/Database/Seeds/ directory in the project. The seeder is used for

Example:
php spark make:seeder SeederTest

//Example:
public function run()
{
$this->db->table('tbl_blog)->truncate(); 
$this->db->table('tbl_blog')->insert([
    "blog_title" => "hello",
    "blog_details" => 'world', 
]); 
}

In the above example, remove all the fields from Database and then insert a new blog with blog_title of ‘hello’ and blog_details with ‘world’.

7. php spark db:seed 

Seed command is used to execute the seeding and perform Database Operations. The above command runs and executes all the seeds at time. This needs the seeder name for the parameter

8. php spark db:seed [name-of-seeder]

The above command runs and executes the individual seeds at a time by the file path parameter. 

For more details please review official documentation by codeigniter4 Team below.

https://codeigniter4.github.io/userguide/dbmgmt/migration.html

Final Thoughts:

Are you working on a CodeIgniter project and want to get your database migrated or facing issues while altering the database? SpryBit is here with a solution for you, we have expertise in CodeIgniter Development. Contact our experts today!

CodeIgniter,PHP,Software Development,Web Development
, , , , ,

2 Replies to “How to implement database migration in CodeIgniter-4 Commands?”

  1. Nikunj Gondaliya says:

    Thank you ojash for valuable information for codeIgniter 4 migration commands.

  2. Keval Moradiya says:

    Excellent article! Learned valuable tips for migrating data to CodeIgniter 4

Leave a Reply

Your email address will not be published. Required fields are marked *

Specially Thank you! for visiting.

Any Project on your Mind?

Contact us OR call us to get FREE estimate