views:

380

answers:

3

I have been researching a way to get the SQL statements that are built by a generated Migration file. These extend Doctrine_Migration_Base. Essentially I would like to save the SQL as change scripts.

The execution path leads me to Doctrine_Export which has methods that build the SQL statement and executes them. I have found no way of asking for just them. The export methods found in Doctrine_Export only operate on Doctrine_Record models and not Migration scripts.

From the command line './doctrine migrate version#' the path goes:

  • Doctrine_Cli::run(cmd)
  • Doctrine_Task_Migrate::setArguments(args)
  • Doctrine_Task_Migrate::execute()
  • Doctrine_Migration::migrate(to)
  • Doctrine_Migration_Process::Doctrine_Export::various create, drop, alter methods with sql equivalents.

Has anyone tackled this before? I really would not like to change Doctrine base files. Any help is greatly appreciated.

A: 

Could you make a dev server, and do the migration on that, storing a SQL Trace as you go?you don't have to keep the results, but you would get a list of every command.

Rob Farley
Interesting. I will look into that and get back to you. I feel I might have to modify the base Doctrine files anyway as their CLI implementation is lacking. There is a dryRun option available that Doctrine_Task_Migrate doesn't even bother checking for.Basically the 2 options I am going for are:Add the dryRun option, run a migration with dryRun and SQL Trace to capture the statements.OrAlong with the dryRun option, add another option to just output theSQL statements, this would require a modified Doctrine_Migration_Process file, maybe I'll call it Doctrine_Migration_GetSQL.
Rob
Sounds valid. Certainly SQL Trace is a good way of getting the commands.
Rob Farley
A: 

Taking into account Rob Farley's suggestion, I modified:

  • Doctrine_Core::migrate
  • Doctrine_Task_Migrate::execute

When the execute method is called the optional argument 'dryRun' is checked. If true then a 'Doctrine_Connection_Profiler' instance is created. The 'dryRun' value is then passed onto the 'Doctrine_Core::migrate' method. The 'dryRun' value of true allows the changes to rollback when done executing the SQL statements. When the method returns, the profiler is parsed and non-empty SQL statements not containing 'migration_version' are saved and displayed to the terminal.

Rob
A: 

I've just faced the same problem - i need the sql from migrations. The idea with 'Doctrine_Connection_Profiler' helped me a lot:) thx

Hermann Bier
Glad this post helped someone else.
Rob