How can I store required 'base' or 'initial' data for a database (in particular Symfony)?
If you're using Doctrine Migrations, you can generate initial migration with whole database schema, then you should generate migrations (doctrine:migrations:generate
or doctrine:migrations:diff
) for all changes that are made in database structure AND also add there queries that will migrate existing data.
Fixtures are designed to pre-populate data (with doctrine:fixtures:load
) and, in my opinion, they should be kept up-to-date with latest database schema and executed after doctrine:migrations:migrate
/ doctrine:schema:create
.
So finally:
- Create base migration with initial database schema (instead of executing
doctrine:schema:create
just generate migration file and migrate it) - Create new migrations for each database schema change AND for migrating existing data (such as role name changing)
- Keep fixtures up-to-date with latest schema (you can use
--append
option and only update fixtures instead of deleting all database data first)
Then, when deploying new instance you can run doctrine:schema:create
, then doctrine:migrations:version --add --all --no-interaction
(mark all migrations as migrated, because you have already created latest schema) and doctrine:fixtures:load
which will populate data to the database (also latest version, so data migrations from Doctrine migrations files are not required).
Note: Existing instances should NOT use doctrine:schema:update
, but only doctrine:migrations:migrate
. In our app we even block usage of this command, in app/console
:
use Symfony\Component\Console\Output\ConsoleOutput;use Symfony\Component\Console\Helper\FormatterHelper;// Deny using doctrine:schema:update commandif(in_array(trim($input->getFirstArgument()), ['doctrine:schema:update'])) { $formatter = new FormatterHelper(); $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, true); $formattedBlock = $formatter->formatBlock(['[[ WARNING! ]]', 'You should not use this command! Use doctrine:migrations:migrate instead!'], 'error', true); $output->writeln($formattedBlock); die();}
This is what I figured out from my experience. Hope you will find it useful :-)