Home »
AdonisJs
Database Migrations in AdonisJs
Here, we are going to learn about the database migrations in AdonisJs, what is migration script, etc?
Submitted by Radib Kar, on January 11, 2021
In our AdonisJs series, we have already learnt how to get started and what the structure details of the boilerplate created are? The very first step towards learning to develop a backend with Adonis.js is learning what database migrations are.
This tutorial is focused to discuss details on database migrations.
Database Migrations in AdonisJs
Since we have already created the boilerplate, we have default migration scripts created already. In the last tutorial, I also discussed the directory structure and we found that by default we have two migration scripts created.
Let's now discuss this in detail.
What is a migration script?
A migration script is a special feature in adonis that helps us to run DDL statements like creating, dropping tables under a schema.
So if you have a created schema, you don't need to create or drop tables manually when required.
Now, first come to the .env file where you will see some environment variables listed regarding database setups like below:
First, change the DB_CONNECTION from SQLite to MySQL(As we will use MySQL).
This is what created by default. DB_HOST & DB_PORT should be same as your MySQL workbench set up.
DB_USER is root by default
DB_PASSWORD = the password you set for your workbench
DB_DATABASE = the schema name which is adonis by default. You have to create a schema with the same name in your MySQL workbench
This is the setup required before getting started with adonis. Below is the .env set up for me(OOPs!, I exposed my DB password :( )
Okay so, we are done with the setup.
Let's now review migration-related few commands.
For that hit the below command in your terminal.
Adonis -help
As output, you will find a detailed list of commands & there you will find migration-related commands too like below.
So, I won't repeat each command and what it does as you can already see that.
But first, we will use the command
adonis migration:run
This command runs all the pending migrations. Since we have all migrations pending, it will create the tables for us. Before going in deep into the migration scripts, let's just run this command.
If you successfully run this command you should see the following:
Otherwise, you might see the following errors,
1.
This is saying that your MySQL module is not installed. So to install the MySQL module successfully, use the below command:
npm install mysql -save
It will install the MySQL module locally which means if you create another new application, again you have to install the module there.
2.
This is because you haven't created the schema which is mentioned in the .env file(DB_DATABASE). So, please create a schema with the same name with help of MySQL workbench.
Once your migrations have been run successfully, open the MySQL workbench & you should see the two tables created users & tokens like below:
Congrats, you have successfully run the default migration scripts. This tutorial ends here, but obviously, migrations not. Please follow our next article to learn more about adonis migrations & create your migration script.