0
0
FastAPIframework~20 mins

Alembic migrations in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Alembic 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 alembic upgrade head?

In a FastAPI project using Alembic, what is the effect of running the command alembic upgrade head?

AIt applies all pending migrations to update the database schema to the latest version.
BIt deletes all migration history but keeps the database schema unchanged.
CIt rolls back the last applied migration.
DIt creates a new migration file with the current database schema.
Attempts:
2 left
💡 Hint

Think about what 'upgrade' means in database migrations.

📝 Syntax
intermediate
2:00remaining
Identify the correct Alembic migration script snippet to add a new column

Which snippet correctly adds a new column email of type String(255) to the users table in an Alembic migration?

Aop.add_column('users', sa.Column('email', sa.String(255), nullable=False))
Bop.create_column('users', 'email', sa.String(255))
Cop.add('email', sa.String(255), table='users')
Dop.alter_table('users').add_column('email', sa.String(255))
Attempts:
2 left
💡 Hint

Look for the Alembic operation that adds a column to a table.

🔧 Debug
advanced
2:00remaining
Why does this Alembic migration raise an error?

Given this migration snippet, why does running it cause an error?

def upgrade():
    op.add_column('items', sa.Column('price', sa.Float))

def downgrade():
    op.drop_column('items', 'cost')
AThe 'sa.Float' type is invalid in Alembic migrations.
BThe upgrade is missing a nullable argument for the new column.
CThe downgrade tries to drop a column named 'cost' which does not exist; it should drop 'price'.
DThe migration is missing a revision ID and down_revision identifiers.
Attempts:
2 left
💡 Hint

Check the column names used in upgrade and downgrade functions.

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

Consider this Alembic migration upgrade function:

def upgrade():
    op.create_table(
        'orders',
        sa.Column('id', sa.Integer, primary_key=True),
        sa.Column('user_id', sa.Integer, nullable=False),
        sa.Column('total', sa.Float, nullable=False)
    )

What will be the state of the database after running this migration?

AThe database schema remains unchanged.
BThe 'orders' table is dropped from the database.
CThe migration will fail because the 'total' column type is invalid.
DA new table named 'orders' exists with columns 'id', 'user_id', and 'total'.
Attempts:
2 left
💡 Hint

Think about what op.create_table does.

🧠 Conceptual
expert
2:00remaining
Why use Alembic migrations in a FastAPI project?

Which is the best explanation for why Alembic migrations are important in a FastAPI project?

AThey automatically generate API endpoints based on database tables.
BThey allow you to track and apply incremental database schema changes safely over time.
CThey replace the need for an ORM by managing raw SQL queries.
DThey provide a graphical interface to edit database tables directly.
Attempts:
2 left
💡 Hint

Think about managing database changes as your app evolves.