Understanding Migration Scripts in AdonisJs

In this tutorial, we are going to learn about the migration scripts in AdonisJs with example.
Submitted by Radib Kar, on January 12, 2021

In our last tutorial, we learnt about database migration. In this tutorial, we will see the detail of a migration script in AdonisJs.

Let's start with the default created migration script for user table. In our last tutorial, we already saw that running the migration scripts created two tables under the schema. Let's first review that & then we try to add more to the existing migration script.

The below is the code for the user table migration scripts which created by default in the boilerplate.

AdonisJs | Understanding Migration Scripts (1)

Here, you can see two functions under the class, namely up() & down().

up() will be invoked when you will run the migrations script to create the table & down() will be invoked when you will try to drop the table via reset or rollback commands.

Now in the up() function,

we can see we have specified the table attributes.

There are also two code snippets which are like below,

table.increments()
table.timestamps()

The first one creates an id which will be the primary key for the table and it will be incremental.

Like it will start from 1 & then for the next entries it will keep incrementing.

The second one generates two extra attributes namely "created_at" & "updated_at" which holds the timestamps.

AdonisJs | Understanding Migration Scripts (2)

The other code snippets are to define attributes for the table. Here we have:

table.string('username', 80).notNullable().unique()
table.string('email', 254).notNullable().unique()
table.string('password', 60).notNullable()

These above three lines define three attributes for the user table.

  1. username : type: string, character length: 80, it's unique and can't be NULL
  2. email : type: string, character length: 254, it's unique and can't be NULL
  3. password : type: string, character length: 60, it can't be NULL

So it's clear that notNullable() is to define not Nullable & unique() is to define that the attribute would be unique.

Now as a task, I would like to ask you to add two more attributes for this table.

  1. "gender" which will be of type "string" & can't be Nullable
  2. "age" which will be of type integer (check this reference to find what to use )

Once you are done, reset the migrations by

adonis migration:reset

Then, run the migrations again.

adonis migration:run

This should generate the below user table.

AdonisJs | Understanding Migration Scripts (3)

&

Your code changes should be similar like below,

up () {
        this.create('users', (table) => {
        table.increments()
        table.string('username', 80).notNullable().unique()
        table.string('email', 254).notNullable().unique()
        table.string('password', 60).notNullable()
        table.string('gender', 20).notNullable()
        table.integer('age').notNullable()
        table.timestamps()
    })
}

So, I hope you have built some good idea of what migration is & how it helps to create tables. I would recommend running SQL query on your MYSQL workbench to check whether there is any data created in the table or not. Of course, there is no data at all til now, we only have the table structure.

In our next tutorial, we will create another migration scripts from scratch to create a new table under the schema.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.