Creates and manages database migrations, including generating migration files, writing up/down migration logic, running schema changes, resolving migration conflicts, and testing rollbacks. Use when asked to add a column, rename a table, ALTER TABLE, create a migration file, run db migrate, handle schema changes, or work with migration tools like Alembic, Knex, Rails ActiveRecord, Flyway, or Liquibase.
72
90%
Does it follow best practices?
Impact
—
No eval scenarios have been run
Passed
No known issues
Follow these steps when creating or running a migration:
up migrationdown migrationdown migration and confirm the schema reverts cleanly// migrations/20240101_add_email_to_users.js
exports.up = function (knex) {
return knex.schema.table('users', (table) => {
table.string('email').notNullable().defaultTo('');
});
};
exports.down = function (knex) {
return knex.schema.table('users', (table) => {
table.dropColumn('email');
});
};Commands:
knex migrate:make add_email_to_users # generate file
knex migrate:latest # run up
knex migrate:rollback # run down
knex migrate:status # check applied migrations# alembic/versions/abc123_add_email_to_users.py
def upgrade():
with op.batch_alter_table('users') as batch_op:
batch_op.add_column(sa.Column('email', sa.String(255), nullable=False, server_default=''))
def downgrade():
with op.batch_alter_table('users') as batch_op:
batch_op.drop_column('email')Commands:
alembic revision --autogenerate -m "add_email_to_users" # generate
alembic upgrade head # run up
alembic downgrade -1 # run down
alembic current # check state# db/migrate/20240101000000_add_email_to_users.rb
class AddEmailToUsers < ActiveRecord::Migration[7.1]
def up
add_column :users, :email, :string, null: false, default: ''
end
def down
remove_column :users, :email
end
endCommands:
rails generate migration AddEmailToUsers email:string # generate
rails db:migrate # run up
rails db:rollback # run down
rails db:migrate:status # check stateup with a reversible down — if a step is irreversible (e.g., dropping a column with data), document it explicitly