0
0
Expressframework~20 mins

Migrations for schema changes in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Migration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you run this migration script?
Consider this migration script using a common migration tool in Express apps. What will be the result after running it?
Express
exports.up = function(knex) {
  return knex.schema.table('users', function(table) {
    table.string('nickname').notNullable().defaultTo('guest');
  });
};

exports.down = function(knex) {
  return knex.schema.table('users', function(table) {
    table.dropColumn('nickname');
  });
};
ARemoves the 'nickname' column from 'users' table.
BAdds a new column 'nickname' to 'users' table with default 'guest' and disallows null values.
CCreates a new table called 'nickname' with a string column.
DRenames the 'nickname' column to 'guest' in 'users' table.
Attempts:
2 left
💡 Hint
Look at the 'up' function and what it does to the 'users' table.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this migration code
Which option correctly identifies the syntax error in this migration script?
Express
exports.up = function(knex) {
  return knex.schema.createTable('posts', function(table) {
    table.increments('id').primary()
    table.string('title').notNullable()
    table.text('content')
    table.timestamps
  });
};
AMissing parentheses after 'table.timestamps' to call the function.
BThe 'text' column type is invalid in this context.
CThe 'increments' method cannot be chained with 'primary()'.
DMissing semicolon after 'table.string('title').notNullable()'.
Attempts:
2 left
💡 Hint
Check how functions are called in JavaScript.
🔧 Debug
advanced
2:00remaining
Why does this migration fail when adding a non-nullable column?
This migration tries to add a non-nullable column 'age' to 'users' table but fails. Why?
Express
exports.up = function(knex) {
  return knex.schema.table('users', function(table) {
    table.integer('age').notNullable();
  });
};
ABecause the 'table' argument is missing in the callback function.
BBecause 'integer' is not a valid column type in knex migrations.
CBecause existing rows have no value for 'age', violating the non-null constraint.
DBecause the migration script is missing a return statement.
Attempts:
2 left
💡 Hint
Think about what happens to existing data when adding a non-nullable column without a default.
🧠 Conceptual
advanced
2:00remaining
What is the main purpose of the 'down' function in migration scripts?
Why do migration scripts include a 'down' function alongside the 'up' function?
ATo reverse the changes made by the 'up' function, allowing rollback of schema changes.
BTo apply additional schema changes after the 'up' function completes.
CTo validate the schema after migration is applied.
DTo seed the database with initial data.
Attempts:
2 left
💡 Hint
Think about how you undo a migration if something goes wrong.
state_output
expert
3:00remaining
What is the state of the 'products' table after running this migration?
After running this migration, what columns will the 'products' table have?
Express
exports.up = function(knex) {
  return knex.schema.table('products', function(table) {
    table.dropColumn('description');
    table.string('summary').notNullable().defaultTo('No summary');
  });
};
AOnly the 'description' column remains; 'summary' is not added due to error.
BBoth 'description' and 'summary' columns exist with 'summary' nullable.
CThe migration fails because you cannot drop and add columns in the same migration.
DThe 'description' column is removed; a new 'summary' column is added with default 'No summary' and not nullable.
Attempts:
2 left
💡 Hint
Check what happens when you drop a column and add another in one migration.