Rafael Beraldo
Posted on May 6, 2023
In Symfony/Doctrine, when running the command php bin/console make:migration
it generates the migrations for the current driver only, unfortunately it doesn't generate database-agnostic code.
But it can be easily customized. Let's setup it to generate two migrations, one for MySQL (prod/dev) and one for SQLite (test).
Setup
Setup your MySQL in your .env
like you would usually do, but set a MIGRATIONS_PATH="migrations/mysql"
property to it.
Then create a .env.test
and set:
APP_ENV=test
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
MIGRATIONS_PATH="migrations/sqlite"
In your config/packages/doctrine_migrations.yaml
you change default path to get from the env files:
doctrine_migrations:
migrations_paths:
'DoctrineMigrations': '%kernel.project_dir%/%env(string:MIGRATIONS_PATH)%'
enable_profiler: false
Also enable MakerBundle for test env in config/bundles.php
:
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true, 'test' => true],
Running migrations
Now you can work with migrations using:
# for MySQL
php bin/console make:migration
php bin/console doctrine:migration:migrate
# for SQLite
php bin/console make:migration --env=test
php bin/console doctrine:migration:migrate --env=test
Doing this, Symfony will generate migrations for MySQL in migrations/mysql
and migrations/sqlite
for SQLite.
Posted on May 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.