Hi Everyone,
I want to share my migration experience (larvel 5.3) It’s to be good idea to implement in your site. If number of developers working in same script, no need of sharing your query changes to each to other. And also don’t worry about the table creation, fields changes in live server also. The single step will do this all the process for you. You just go to your folder path, hit the migrate command, it will do the all the changes will reflect in tables as well as field.
JUST, WE WILL GO SHORT TOUR WITH MIGRATION
HOW TO CREATE A TABLE USING MIGRATION:
If you want to create a table using migration, write a make migration command. php artisan make:migration is syntax and create users table is a migration file name. You can give the migration file name what ever you want, for over understanding, i have created a migration file name as create_users_table
Now, Please check your migration folder, you will get a new file with a create_users_table, some other prefix. You will see two predefied functions in this file, up() and down()
USE OF UP() FUNCTION IN MIGRATION FILE:
Common use of up function, you can able to create field for your table. What are fields you want to create table. You can write the field specification with character type like : Interger, string, enum, float, text
CREATE A FOREIGN FOR A TABLE:
There are two cases you will face, while creating a foreign. First one, you to create a field with foreign key. Second one, you want to create a foreign key for already created field
FOREIGN KEY FOR WITH NEW FIELD CREATION: (AND ALSO YOU CAN ABLE TO CREATE DESIRED ACTION ONUPDATE, ONDELETE – (CASCADE, NO ACTION) )
ADD A FOREIGN KEY FOR ALREADY CREATED FIELD: (IF THE FIELD IS ALREADY CREATED IN THIS TABLE MEANS, NO NEED TO CREATE AGAIN, WHEN YOUR ADDING A FOREIGN KEY)
USE OF DOWN() FUNCTION IN MIGRATION FILE:
The main purpose of down method in migration file is that, direct opposite of the up() function. There are number of cases will be there you want to write a correct functionality in down() function. Some of the scenario i have listed here,
- If your created a table in up function, you want delete a table in down function
- If your added foreign key in the up function, you want delete the foreign key in the down function
- If your created a field with foreign key means, you want delete a field as well as key in the down function
WHEN DOWN() FUNCTION WILL BE USEFULL IN MIGRATION FILE:
It would be usefull, when your going for rollback. You just refer a migration table, you will batch id for the migration (like : 1,2,3)
Remove the last migration you just use:
Same as the rollback define by step, batch id 2 defined as first migration, batch id 3 defined as second migration. you want to rollback the first migration (batch id is 2). Do is this step
ROLLBACK ALL THE MIGRATION:
If you want to rollback all of your migration by just hit this command
ROLLBACK ALL THE MIGRATION AND DO THE MIGRATION BY SINGLE COMMAND:
This command will first rollback your all the changes, and the second migrate the table.
IF YOUR DOING ANY DELETION OR CREATION IN MIGRATION FOLDER OR TABLE:
Based on the manual deletion and creation, if you face any error in the composer. While your migrating the table. YOU JUST DO IT, THIS COMMAND:
CREATING OR UPDATE, DELETE IN ALREADY EXISTING TABLE, BY CREATE A NEW MIGRATION FILE:
First you need to create a migration file by,
Now you will get a file in the migration folder with up(), down() function
Note : Best thing about creating migration schema, first create a parent table and then add a related foreign key for the table. It could be easy for you to understand the flow of the functionality
Comments
Post a Comment