Database Migrations in Laravel
Database Migrations in Laravel
Welcome to a crucial step in enhancing your Laravel ToDo application! In this lesson, we will focus on database migrations, a fundamental feature that ensures your database schema is consistent and manageable. Building on our previous lesson, in which you learned to integrate a database within your Laravel app, we'll now explore how to maintain and evolve your database structure effectively.
Database migrations are an integral part of the development process, especially in collaborative environments where database changes need to be tracked and implemented seamlessly.
What You'll Learn
In this lesson, you will discover how to create, manage, and apply database migrations in Laravel. Migrations are essential because they allow developers to version-control database changes and easily share them within a team.
You might have noticed, that we used a strange file in the previous lesson located in the database/migrations directory. This file is a migration file, and it contains instructions for creating or modifying database tables. We'll dive deeper into this concept and explore how migrations can help you maintain a consistent database schema.
Before we proceed, let's understand the idea of database migrations and why they are crucial for your Laravel application.
Database migrations are a way to manage database changes in a structured and organized manner. They allow you to define the structure of your database tables using PHP code. By creating migration files, you can version-control your database schema and apply changes consistently across different environments. This ensures that your database remains in sync with your application code and evolves alongside it.
Let's look at the migrations file we used in the previous lesson:
Let's understand the key components of this migration file:
- The
CreateTodosTableclass extends theMigrationclass provided by Laravel. - The
upmethod is used to define the changes that should be applied to the database. In this case, we are creating atodostable with columns fortitle,description, and timestamps using theSchema::createmethod provided by Laravel.- The function takes two arguments: the table name and a closure that defines the table structure.
- The
Blueprintclass is used to define the columns of the table. - The
idmethod creates an auto-incrementing primary key column. - The
stringandtextmethods create columns of typeVARCHARandTEXT, respectively. TheVARCHARandTEXTare underlying SQL data types. - The
timestampsmethod createscreated_atandupdated_atcolumns to track record creation and updates.
- The
downmethod is used to define the rollback operation that should be applied when the migration is rolled back. In this case, we are dropping thetodostable using theSchema::dropIfExistsmethod provided by Laravel.
After defining the migration file, you can run the migration using the php artisan migrate command. This command will execute the up method of the migration file and create the todos table in the database. You can also roll back the migration using the php artisan migrate:rollback command, which will execute the down method and drop the todos table.
You might notice that the migration class contains a timestamp, which serves as a unique identifier for the migration and is a common convention used in database migrations. This is crucial for the following reasons:
- Uniqueness: It ensures that every migration has a unique name, even if two migrations are created with the same name but at different times.
- Execution Order: The timestamp allows migration tools to execute the migrations in chronological order. This prevents schema conflicts by ensuring that changes are applied in the correct sequence.
Additionally, we can apply changes to existing tables. For instance, adding a completed column is done with a new migration:
In this migration file, we are adding a completed column to the todos table. The up method uses the Schema::table method to modify an existing table. The boolean method creates a column of SQL type BOOLEAN, and the default method sets the default value of the column to false.
The down method defines the rollback operation, which drops the completed column from the todos table. Note, that the SQLite database does not support dropping columns, so you will see and empty down method in the practices.
Similarly, after doing any changes to the migration file, or creating a new one, you can run the migration using the php artisan migrate command to apply the changes to the database.
