How can I store required 'base' or 'initial' data for a database (in particular Symfony)? -


i use doctrine migrations bundle track changes in database structure. ensure when i'm deploying / adding new server application that:

  • (a) database schema date (doctrine:migrations:migrate)
  • (b) database contains pre-defined set of data

for (b) example roles. want set of roles present. realize possible database migrations, don't idea of mixing schema changes data changes. if use mysql migrations have create equivalent sqlite migration test database.

another option i'm aware of data fixtures. reading documentation feeling fixtures more loading test data. if changed role name don't know how updated using fixtures (since either delete data in database before loading or append it). if use append unique keys problem.

i'm considering creating sort of command takes set of configuration files , ensures tables in consistent state matching config files - if option exists i'd use of course.

what best way handle loading , managing required data database?

if you're using doctrine migrations, can generate initial migration whole database schema, should generate migrations (doctrine:migrations:generate or doctrine:migrations:diff) changes made in database structure , add there queries migrate existing data.

fixtures designed pre-populate data (with doctrine:fixtures:load) and, in opinion, should kept up-to-date latest database schema , executed after doctrine:migrations:migrate / doctrine:schema:create.

so finally:

  • create base migration initial database schema (instead of executing doctrine:schema:create generate migration file , migrate it)
  • create new migrations each database schema change , migrating existing data (such role name changing)
  • keep fixtures up-to-date latest schema (you can use --append option , update fixtures instead of deleting database data first)

then, when deploying new instance can run doctrine:schema:create, doctrine:migrations:version --add --all --no-interaction (mark migrations migrated, because have created latest schema) , doctrine:fixtures:load populate data database (also latest version, data migrations doctrine migrations files not required).

note: existing instances should not use doctrine:schema:update, doctrine:migrations:migrate. in our app block usage of command, in app/console:

use symfony\component\console\output\consoleoutput; use symfony\component\console\helper\formatterhelper;  // deny using doctrine:schema:update command if(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 command! use doctrine:migrations:migrate instead!'], 'error', true);      $output->writeln($formattedblock);     die(); } 

this figured out experience. hope find useful :-)


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -