0
0
NestJSframework~20 mins

Migrations in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this migration script execution?

Consider a NestJS migration that adds a new column to an existing table. What will be the state of the database after running this migration?

NestJS
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddAgeColumn1680000000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TABLE users ADD COLUMN age int`);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TABLE users DROP COLUMN age`);
  }
}
AThe 'users' table will have a new column named 'age' of type integer.
BThe migration will fail because 'ALTER TABLE' is not supported in migrations.
CThe 'users' table will be deleted and recreated with the 'age' column.
DNo changes will be made to the database because the migration script is empty.
Attempts:
2 left
💡 Hint

Think about what the up method does in a migration.

📝 Syntax
intermediate
2:00remaining
Which option contains a syntax error in a NestJS migration?

Identify the migration code snippet that will cause a syntax error when executed.

Aawait queryRunner.query(`ALTER TABLE users ADD age int`);
Bawait queryRunner.query('ALTER TABLE users DROP COLUMN age');
Cawait queryRunner.query(`ALTER TABLE users ADD COLUMN created_at timestamp`);
Dawait queryRunner.query(`ALTER TABLE users ADD COLUMN email varchar(255)`);
Attempts:
2 left
💡 Hint

Check the SQL syntax for adding a column.

🔧 Debug
advanced
2:00remaining
Why does this migration fail to rollback properly?

Given the migration below, why does running the down method cause an error?

NestJS
import { MigrationInterface, QueryRunner } from 'typeorm';

export class RenameUsernameToUser1680000000001 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TABLE users RENAME COLUMN username TO user`);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TABLE users RENAME COLUMN user TO username`);
  }
}
AThe down method should drop the 'user' column instead of renaming.
BThe migration fails because 'RENAME COLUMN' is not supported in SQL.
CThe down method tries to rename 'username' to 'user' again, but 'username' no longer exists after up migration.
DThe migration fails because the table 'users' does not exist.
Attempts:
2 left
💡 Hint

Think about the state of the database after the up method runs.

state_output
advanced
2:00remaining
What is the state of the database after running this migration?

This migration creates a new table. What will the database contain after running up?

NestJS
import { MigrationInterface, QueryRunner } from 'typeorm';

export class CreateProductsTable1680000000002 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`CREATE TABLE products (id int PRIMARY KEY, name varchar(100), price decimal)`);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE products`);
  }
}
AThe migration will create a table named 'product' (singular) instead of 'products'.
BThe migration will fail because 'decimal' is not a valid column type.
CThe 'products' table will be created but without any columns.
DThe database will have a new table named 'products' with columns 'id', 'name', and 'price'.
Attempts:
2 left
💡 Hint

Check the SQL syntax for creating tables and column definitions.

🧠 Conceptual
expert
2:00remaining
Which option best describes the purpose of the 'down' method in NestJS migrations?

Why is the down method important in a migration class?

AIt runs automatically before the <code>up</code> method to prepare the database.
BIt defines how to revert the changes made by the <code>up</code> method, allowing rollback of the migration.
CIt is used to seed initial data into the database after migration.
DIt validates the migration syntax before execution.
Attempts:
2 left
💡 Hint

Think about what happens if you want to undo a migration.