devture/mongodb-migrations-bundle
Installation:
composer require devture/mongodb-migrations-bundle
Ensure Devture\MongoDBMigrationsBundle\DevtureMongoDBMigrationsBundle is enabled in config/bundles.php.
Configuration:
Add MongoDB connection details to config/packages/devture_mongodb_migrations.yaml:
devture_mongodb_migrations:
db: '%env(MONGODB_URL)%'
migrations_namespace: 'App\Migrations'
migrations_table: 'migrations'
First Migration: Generate a migration file:
php bin/console make:migration
This creates a timestamped migration class in src/Migrations/ (e.g., 20240101000000_CreateUsersCollection.php).
Run Migration:
php bin/console mongodb:migrate
namespace App\Migrations;
use Devture\MongoDBMigrations\Migration;
use Devture\MongoDBMigrations\MongoDB\Index;
class AddEmailIndex extends Migration
{
public function up()
{
$this->collection('users')->ensureIndex(
new Index('email', ['unique' => true])
);
}
public function down()
{
$this->collection('users')->dropIndex('email_1');
}
}
Migration Development:
php bin/console make:migration for boilerplate.Devture\MongoDBMigrations\Migration for custom logic.up() (changes) and down() (rollbacks).Integration with Laravel:
// In AppServiceProvider@boot()
$this->app->bind('mongodb.migrations.connection', function () {
return \MongoDB\Client::fromConnectionString(env('MONGODB_URL'));
});
AppServiceProvider:
$this->app->register(\Devture\MongoDBMigrationsBundle\DevtureMongoDBMigrationsBundle::class);
Environment-Specific Migrations:
php bin/console mongodb:migrate --env=production for targeted environments.database/migrations/mongodb (Laravel convention).Seeding Data:
laravel/breeze or spatie/laravel-mongodb for post-migration seeding:
public function up()
{
$this->collection('users')->insertOne([
'name' => 'Admin',
'email' => 'admin@example.com',
]);
}
up() is safe to rerun (e.g., check for existing indexes).$this->transaction() for atomic operations:
$this->transaction(function () {
$this->collection('orders')->updateMany([], ['status' => 'pending']);
});
dependsOn():
public function dependsOn()
{
return ['20240101000000_CreateUsersCollection'];
}
Connection Issues:
MONGODB_URL is correctly set in .env (e.g., mongodb://user:pass@host:port/db).php bin/console mongodb:migrate --debug.Migration Table Conflicts:
migrations. Rename via config if needed:
devture_mongodb_migrations:
migrations_table: 'app_migrations'
PHP 8+ Compatibility:
^3.0 for PHP 8+ support. Avoid ^1.0 (PHP 5.6).Rollback Limitations:
down() may not reverse all changes (e.g., dropped collections). Document manual steps if needed.Namespace Collisions:
migrations_namespace in config matches your Laravel namespace (e.g., App\Migrations).--dry-run to preview changes:
php bin/console mongodb:migrate --dry-run
config/packages/devture_mongodb_migrations.yaml:
devture_mongodb_migrations:
debug: true
Laravel Events:
Trigger migrations on Migrating or Migrated events:
// In EventServiceProvider
protected $listen = [
'Migrating' => [
\App\Listeners\PreMigrationListener::class,
],
];
Custom Migrations:
Extend the base Migration class for reusable logic:
namespace App\Migrations;
use Devture\MongoDBMigrations\Migration;
class BaseMigration extends Migration
{
protected function ensureIndex(string $collection, string $field, array $options = [])
{
$this->collection($collection)->ensureIndex(new \Devture\MongoDBMigrations\MongoDB\Index($field, $options));
}
}
CI/CD Integration: Add to deployment pipeline:
# .github/workflows/deploy.yml
- run: php artisan mongodb:migrate --env=production --force
Backup Strategy: Always back up MongoDB before running migrations in production:
mongodump --uri="$MONGODB_URL" --out=/backup/$(date +%F)
Testing:
Use Laravel’s MigrateFresh or RefreshDatabase for testing:
use Illuminate\Foundation\Testing\RefreshDatabase;
class MigrationTest extends TestCase
{
use RefreshDatabase;
}
How can I help you explore Laravel packages today?