Interview answer
Migrations are version-controlled database schema changes. They allow developers to create, modify, and roll back database structures consistently across environments.
Code
php artisan make:migration create_products_table
php artisan migrate
php artisan migrate:rollback
Example:
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('price', 10, 2);
$table->timestamps();
});
Adding a column:
Schema::table('products', function (Blueprint $table) {
$table->unsignedInteger('stock')->default(0);
});
Scenario
Suppose five developers work on the same project. Instead of manually modifying everyone's database, each schema change is committed as a migration. CI/CD can then execute:
php artisan migrate --force
This makes database changes reproducible.
0 comments:
Post a Comment
Thanks