2

After some changes I've made today in my schema.yml, each of one followed by the diff, migrate, build commands, the diff operation stopped working.

The last successful migration was the 243rd.

Now, every new change I make, when I give the diff command, the result is always the same:

/usr/bin/php /.../symfony --color doctrine:generate-migrations-diff
>> doctrine  generating migration diff
>> file+     /tmp/doctrine_schema_92228.yml
Done.

No new file is created in lib/migration/doctrine, so I cannot use the migrate command to commit the changes to the db.

I tried to clear the cache, clean model files, build all classes, and also reboot.

Any ideas?

j0k
  • 22,600
  • 28
  • 79
  • 90
  • 1
    Did you generate your classes with the new schema before running the diff? If so, rollback your schema, regenerate your classes, re-apply your schema change and rerun diff. – j0k Jun 14 '13 at 14:55
  • Thanks for the suggestion; no, I always diff-migrate-build in this sequence; now I'm going to try and rebuild the schema from model, after a full backup ;-) – coronatilorenzo Jun 14 '13 at 15:35
  • Update: I rebuilt schema.yml from db; then I tried build-model, but now I get this warning: `/usr/bin/php /.../symfony --color doctrine:build-model >> doctrine generating model classes >> file+ /tmp/doctrine_schema_93565.yml PHP Notice: Undefined index: alias in /.../lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Import/Builder.php on line 685 ` I'm confused... – coronatilorenzo Jun 14 '13 at 15:52
  • Wow, clear your cache? (I've no idea why you got this error) – j0k Jun 14 '13 at 17:56
  • Currently I have the similar problem - the diff command does not generate any additional files. I just gave up looking for a solution and started writing my migration classes manually. Usually my changes to the DB are not that significant so it's relatively easy to write then :) – Michal Trojanowski Jun 17 '13 at 14:32

1 Answers1

3

This is the best way I have come across to make migrations and success everytime. Cost me a lot to guess but works perfectly and works with multiple databases. I can be wrong in some sentences so feel free to add or correct me in anything you see.

MIGRATIONS, the safest way :)

If you need to work Migrations for multiple databases apply these patches and clear symfony cache, they work perfectly:
doctrine_core.r7687
doctrine_manager.r7657

A. BACKUP PROJECT AND DATABASE:

  1. Save Symfony project Files. (optional but safe way).
  2. Save Database Table Schemas only.
  3. Save Database Table Data only.
  4. Save Database Table Schema with Data.

B. HOW TO MAKE CHANGES TO .yml FILES:

  1. .yml files cannot contain strange symbols, like ñ, ´, ```... or non UTF characters..

  2. Always shows spaces and tabs in Notepad++ or Sublime. There cannot be tabs!!

  3. You CANT have two modules with the same name, even in different databases. Never set two modules with same name or you will have a lot of problems.

  4. If you want to work with multiple databases, you must specify the connection attribute at the beginning of your schema.yml file:

    connection: doctrine_master
    
  5. Working with multiple databases, again you must set the binding connection for the module with the right connection:

    Tbtest001:
      connection: doctrine_master
      tableName: tb_test001
    
  6. Setting the right variable value and type in schema.yml: Schema Files Variables, models and types

  7. Working with multiple databases, take care and modify only one schema.yml for only one database each time!

  8. If you are adding a new table with relations to another table, its recommended to do it in two steps, two migrations. First only add the table and migrate. Then Add the relation and Migrate again. It is the saftest way.

  9. You can have different schemas.yml in different places.

C. MIGRATING THE CHANGES:

  1. Install this plugin, because it has fixes and improvements for checking changes: idlDoctrineMigrationPlugin

  2. Make a new table for each database for your project. Needed for the plugin to work:

    name: migration_version , column: version  (int (11)). (autoincrement=false).
    
  3. In version column, set its value to the lastest migration version you have now. You must do this step for every database where you have the table migration_version:

    UPDATE databasetest.migration_version SET databasetest.migration_version.version='31';
    UPDATE databasetest2.migration_version SET databasetest2.migration_version.version='31';
    
  4. Clear Symfony cache:

    symfony cc
    
  5. Make the migration difference (you need the plugin above and version tables created)

    symfony model:diff > migratediff.log
    
  6. Check if the lastest generated changes are right in the following files:

    .\lib\migration\doctrine\XXXXXX_versionXXX.php
    .\data\migration\history\XXXXXXXXXX.yml
    
  7. Proceed with the migration UP by specifing a number!, NEVER make migrate UP!. Also take in mind the new parameter --connection. It works now if you applied the above patches and it will migrate only the right databases:

    symfony doctrine:migrate 32 --connection=doctrine_master > migrateUP.log
    
  8. Rebuild models, Forms, Filters, Delete old models..

    symfony doctrine:build-model
    symfony doctrine:build-forms
    symfony doctrine:build-filters
    symfony doctrine:clean-model-files
    symfony cc
    
  9. Set all databases to the lastest migration number in their table migration_version:

    UPDATE databasetest.migration_version SET databasetest.migration_version.version='32';
    UPDATE databasetest2.migration_version SET databasetest2.migration_version.version='32';
    
  10. Optional step, if you want to know the lastest SQL query send to the database after the migration:

    symfony doctrine:build-sql [--application[="..."]] [--env="..."]
    

D. LINKS AND FILES:

Correct way to do a migrations diff
Doctrine migrations fallback
http://trac.symfony-project.org/ticket/7272
http://trac.symfony-project.org/ticket/7689
http://trac.symfony-project.org/ticket/8604
http://php-opensource-help.blogspot.com/2010/07/how-to-get-connection-from-doctrine-to.html
http://www.doctrine-project.org/documentation/manual/1_1/en/connections
http://forum.symfony-project.org/viewtopic.php?t=29361&p=104098

Main Files involved in migrations:
Migration.php, Builder.php, sfTaskMigration.php

Community
  • 1
  • 1
xtrm
  • 966
  • 9
  • 22
  • Wow, I'm going to try as soon as I have some time; in the meantime, I found that running migrations-diff on my machine makes php5 crash 8-| – coronatilorenzo Jun 28 '13 at 09:32
  • The plugin may not work in some systems, so you will need to find out why crashes and fix the code. Works for me in Windows 7, php 5.4, symfony 1.14.17. There are probably a lot more ways to migrate, and even if the plugin doesnt work for you, you can still migrate with the original command and follow to the next steps. – xtrm Jun 28 '13 at 09:52