Database Migrations
Guide to managing database migrations in Netasampark.
Overview
Netasampark uses Laravel's migration system for database version control and schema management.
Creating Migrations
Generate Migration
php artisan make:migration create_voters_table
php artisan make:migration add_phone_to_users_table
php artisan make:migration create_campaign_voters_table
Migration Structure
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('voters', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('phone', 20);
$table->string('email')->nullable();
$table->text('address')->nullable();
$table->string('constituency')->nullable();
$table->timestamps();
$table->index('phone');
$table->index('constituency');
});
}
public function down()
{
Schema::dropIfExists('voters');
}
};
Running Migrations
Run All Migrations
php artisan migrate
Run with Force (Production)
php artisan migrate --force
Rollback Last Migration
php artisan migrate:rollback
Rollback All Migrations
php artisan migrate:reset
Rollback and Re-run
php artisan migrate:refresh
Fresh Migration (Drop All)
php artisan migrate:fresh
Migration Best Practices
1. Always Include Down Method
public function down()
{
Schema::dropIfExists('table_name');
}
2. Use Transactions
DB::transaction(function () {
Schema::create('table1', function (Blueprint $table) {
// ...
});
Schema::create('table2', function (Blueprint $table) {
// ...
});
});
3. Add Indexes for Performance
$table->index('email');
$table->index(['status', 'created_at']);
4. Use Foreign Keys
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
Common Migration Patterns
Adding Columns
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->nullable()->after('email');
$table->boolean('is_verified')->default(false);
});
Modifying Columns
Schema::table('users', function (Blueprint $table) {
$table->string('email', 255)->change();
});
Dropping Columns
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('old_field');
});
Renaming Columns
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('old_name', 'new_name');
});
Data Migrations
Seeding Data
public function up()
{
Schema::create('roles', function (Blueprint $table) {
// ...
});
DB::table('roles')->insert([
['name' => 'admin'],
['name' => 'politician'],
['name' => 'team_member'],
]);
}
Updating Data
public function up()
{
DB::table('users')
->whereNull('email_verified_at')
->update(['email_verified_at' => now()]);
}
Production Migrations
Pre-Migration Checklist
- Backup database
- Test on staging
- Review migration SQL
- Plan rollback strategy
- Schedule maintenance window
Running in Production
# Backup first
mysqldump -u user -p database > backup.sql
# Run migration
php artisan migrate --force
# Verify
php artisan migrate:status
Troubleshooting
Migration Failed
# Check status
php artisan migrate:status
# Rollback
php artisan migrate:rollback
# Fix migration file
# Re-run
php artisan migrate
Foreign Key Errors
// Disable foreign key checks temporarily
Schema::disableForeignKeyConstraints();
// Run migration
Schema::enableForeignKeyConstraints();