Challenge - 5 Problems
Migration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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');
});
};Attempts:
2 left
💡 Hint
Look at the 'up' function and what it does to the 'users' table.
✗ Incorrect
The 'up' function adds a new string column 'nickname' to the 'users' table. It sets it as not nullable and gives a default value 'guest'. The 'down' function reverses this by dropping the column.
📝 Syntax
intermediate2: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
});
};Attempts:
2 left
💡 Hint
Check how functions are called in JavaScript.
✗ Incorrect
'table.timestamps' is a function and must be called with parentheses 'table.timestamps()' to add created_at and updated_at columns.
🔧 Debug
advanced2: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();
});
};Attempts:
2 left
💡 Hint
Think about what happens to existing data when adding a non-nullable column without a default.
✗ Incorrect
Adding a non-nullable column without a default value fails if existing rows do not have a value for that column, causing a constraint violation.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how you undo a migration if something goes wrong.
✗ Incorrect
The 'down' function defines how to undo the changes made by 'up', enabling safe rollback of database schema changes.
❓ state_output
expert3: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');
});
};Attempts:
2 left
💡 Hint
Check what happens when you drop a column and add another in one migration.
✗ Incorrect
The migration removes the 'description' column and adds a new 'summary' column with constraints and default value. This is valid and will succeed.