Use database migration utilities in Adonis to its complete potential. Carry out incremental updates - may it be a new table, or changes to existing table using distinct migration files that are version controlled.
You have a beautiful Todo application that has a todos
table with the following columns:
- description
- status
- end_date
Now, let’s say our users ask for a new field called planned_end_date
.
Your first instinct may be to add the column to the database, change controller code if required, and call it a day. But, what if you to start making changes at different times to -
- create a related entity called
todo sub-items
on 3-Jan - add
target_end_date
on 4-Jan - add
remarks
column on 15-Jan - add pre-default value to
target_end_date
on 26-Jan
The point is that there may be tens or hundreds of changes to be deployed to your target environment. It is quite easy to get lost in the details and not track all the changes that needs to be done in a higher environment.
That’s where Adonis migration files save significant effort and time, and make the whole migration process efficient.
In the above example of todos
table: the first migration file will look like this -
|
|
When the changes start pouring in, you don’t make changes in the database or in the same file. Instead you create a new file - todo_schema_4Jan.js
.
|
|
I have not provided anything in down
method but you could potentially drop newly created columns in the same file.
Apply changes to the database in dev/higher environments -
|
|
Repeat the process for other fields as changes come through.
There are several advantages in following this process -
- Complete clarity on schema changes to the entire development team
- Catch all changes to the schema. All files are version controlled
- Apply or rollback changes with ease. Everything is controlled through code