Skip to content

Loading…

Added :doctrine_em configuration option for custom Doctrine entity manager #322

Merged
merged 1 commit into from

2 participants

@EvanK

This implements the suggested configuration option in #321. It's worth noting that I'm a ruby novice, so this may not be the most polished solution. Suggestions or improvements from more seasoned ruby/capistrano devs would be welcome!

Update: per @willdurand's suggestion, simplified down to one em config option that would apply to all relevant doctrine commands

@willdurand
Collaborator

Why don't you just define one variable :doctrine_em instead of having weird custom variables?

@EvanK

In the case of the original issue, we found we needed a non default entity manager only in the specific case that we run a migration.

I was anticipating that someone might need differing SQL permissions for loading fixtures vs running migrations vs clearing caches vs altering schemas...which I suppose could be overkill

@EvanK EvanK Implemented :doctrine_em configuration option
* Added :doctrine_em symbol with default value of `false`
* Added `before` hooks for relevant symfony:doctrine:* commands to assemble --em flag
* Added flag variable to commands being run
* Added tests for --em flag in spec file

Fixes everzet/capifony#321
da982a8
@EvanK

@willdurand Refactored this feature per your recommendation. How's it look?

@willdurand willdurand merged commit e64b2a8 into everzet:master
@willdurand
Collaborator

Tip, top! Thank you Evan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 22, 2013
  1. @EvanK

    Implemented :doctrine_em configuration option

    EvanK committed
    * Added :doctrine_em symbol with default value of `false`
    * Added `before` hooks for relevant symfony:doctrine:* commands to assemble --em flag
    * Added flag variable to commands being run
    * Added tests for --em flag in spec file
    
    Fixes everzet/capifony#321
Showing with 67 additions and 11 deletions.
  1. +19 −0 lib/capifony_symfony2.rb
  2. +11 −11 lib/symfony2/doctrine.rb
  3. +37 −0 spec/capifony_symfony2_doctrine_spec.rb
View
19 lib/capifony_symfony2.rb
@@ -112,6 +112,9 @@ def self.load_into(configuration)
# Model manager: (doctrine, propel)
set :model_manager, "doctrine"
+ # Doctrine custom entity manager
+ set :doctrine_em, false
+
# Symfony2 version
set(:symfony_version) { guess_symfony_version }
@@ -191,6 +194,22 @@ def write(s)
end
end
+ [
+ "symfony:doctrine:cache:clear_metadata",
+ "symfony:doctrine:cache:clear_query",
+ "symfony:doctrine:cache:clear_result",
+ "symfony:doctrine:schema:create",
+ "symfony:doctrine:schema:drop",
+ "symfony:doctrine:schema:update",
+ "symfony:doctrine:load_fixtures",
+ "symfony:doctrine:migrations:migrate",
+ "symfony:doctrine:migrations:status",
+ ].each do |action|
+ before action do
+ set :doctrine_em_flag, doctrine_em ? " --em=#{doctrine_em}" : ""
+ end
+ end
+
["symfony:composer:install", "symfony:composer:update"].each do |action|
before action do
if copy_vendors
View
22 lib/symfony2/doctrine.rb
@@ -5,7 +5,7 @@
task :clear_metadata, :roles => :app, :except => { :no_release => true } do
capifony_pretty_print "--> Clearing Doctrine metadata cache"
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-metadata --env=#{symfony_env_prod}'"
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-metadata --env=#{symfony_env_prod}#{doctrine_em_flag}'"
capifony_puts_ok
end
@@ -13,7 +13,7 @@
task :clear_query, :roles => :app, :except => { :no_release => true } do
capifony_pretty_print "--> Clearing Doctrine query cache"
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-query --env=#{symfony_env_prod}'"
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-query --env=#{symfony_env_prod}#{doctrine_em_flag}'"
capifony_puts_ok
end
@@ -21,7 +21,7 @@
task :clear_result, :roles => :app, :except => { :no_release => true } do
capifony_pretty_print "--> Clearing Doctrine result cache"
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-result --env=#{symfony_env_prod}'"
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:cache:clear-result --env=#{symfony_env_prod}#{doctrine_em_flag}'"
capifony_puts_ok
end
end
@@ -51,7 +51,7 @@
task :create, :roles => :app, :except => { :no_release => true } do
capifony_pretty_print "--> Creating schema"
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:create --env=#{symfony_env_prod}'", :once => true
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:create --env=#{symfony_env_prod}#{doctrine_em_flag}'", :once => true
capifony_puts_ok
end
@@ -60,7 +60,7 @@
capifony_pretty_print "--> Droping schema"
if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to drop #{symfony_env_prod}'s database schema? (y/N)")
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:drop --force --env=#{symfony_env_prod}'", :once => true
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:drop --force --env=#{symfony_env_prod}#{doctrine_em_flag}'", :once => true
end
capifony_puts_ok
end
@@ -69,21 +69,21 @@
task :update, :roles => :app, :except => { :no_release => true } do
capifony_pretty_print "--> Updating schema"
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:update --force --env=#{symfony_env_prod}'", :once => true
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:schema:update --force --env=#{symfony_env_prod}#{doctrine_em_flag}'", :once => true
capifony_puts_ok
end
end
desc "Load data fixtures"
task :load_fixtures, :roles => :app, :except => { :no_release => true } do
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:fixtures:load --env=#{symfony_env_prod}'", :once => true
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:fixtures:load --env=#{symfony_env_prod}#{doctrine_em_flag}'", :once => true
end
namespace :migrations do
desc "Executes a migration to a specified version or the latest available version"
task :migrate, :roles => :app, :only => { :primary => true }, :except => { :no_release => true } do
currentVersion = nil
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} --no-ansi doctrine:migrations:status --env=#{symfony_env_prod}'", :once => true do |ch, stream, out|
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} --no-ansi doctrine:migrations:status --env=#{symfony_env_prod}#{doctrine_em_flag}'", :once => true do |ch, stream, out|
if stream == :out and out =~ /Current Version:.+\(([\w]+)\)/
currentVersion = Regexp.last_match(1)
end
@@ -99,18 +99,18 @@
on_rollback {
if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database back to version #{currentVersion}? (y/N)")
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:migrate #{currentVersion} --env=#{symfony_env_prod} --no-interaction'", :once => true
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:migrate #{currentVersion} --env=#{symfony_env_prod} --no-interaction#{doctrine_em_flag}'", :once => true
end
}
if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database? (y/N)")
- run "#{try_sudo} sh -c ' cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:migrate --env=#{symfony_env_prod} --no-interaction'", :once => true
+ run "#{try_sudo} sh -c ' cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:migrate --env=#{symfony_env_prod} --no-interaction#{doctrine_em_flag}'", :once => true
end
end
desc "Views the status of a set of migrations"
task :status, :roles => :app, :except => { :no_release => true } do
- run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:status --env=#{symfony_env_prod}'", :once => true
+ run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} doctrine:migrations:status --env=#{symfony_env_prod}#{doctrine_em_flag}'", :once => true
end
end
View
37 spec/capifony_symfony2_doctrine_spec.rb
@@ -99,6 +99,18 @@
it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:schema:update --force --env=prod\'') }
end
+ it "defines symfony:doctrine:load_fixtures task" do
+ @configuration.find_task('symfony:doctrine:load_fixtures').should_not == nil
+ end
+
+ context "when running symfony:doctrine:load_fixtures" do
+ before do
+ @configuration.find_and_execute_task('symfony:doctrine:load_fixtures')
+ end
+
+ it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:fixtures:load --env=prod\'') }
+ end
+
it "defines symfony:doctrine:migrations tasks" do
@configuration.find_task('symfony:doctrine:migrations:migrate').should_not == nil
@configuration.find_task('symfony:doctrine:migrations:status').should_not == nil
@@ -171,4 +183,29 @@
it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console init:acl --env=prod\'') }
end
+
+ context "when running symfony:doctrine:* with custom entity manager" do
+ before do
+ @configuration.set :doctrine_em, 'custom_em'
+
+ @configuration.find_and_execute_task('symfony:doctrine:cache:clear_metadata')
+ @configuration.find_and_execute_task('symfony:doctrine:cache:clear_query')
+ @configuration.find_and_execute_task('symfony:doctrine:cache:clear_result')
+ @configuration.find_and_execute_task('symfony:doctrine:schema:create')
+ @configuration.find_and_execute_task('symfony:doctrine:schema:drop')
+ @configuration.find_and_execute_task('symfony:doctrine:schema:update')
+ @configuration.find_and_execute_task('symfony:doctrine:load_fixtures')
+ @configuration.find_and_execute_task('symfony:doctrine:migrations:status')
+ end
+
+ it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:cache:clear-metadata --env=prod --em=custom_em\'') }
+ it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:cache:clear-query --env=prod --em=custom_em\'') }
+ it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:cache:clear-result --env=prod --em=custom_em\'') }
+ it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:schema:create --env=prod --em=custom_em\'') }
+ it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:schema:drop --force --env=prod --em=custom_em\'') }
+ it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:schema:update --force --env=prod --em=custom_em\'') }
+ it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:fixtures:load --env=prod --em=custom_em\'') }
+ it { should have_run(' sh -c \'cd /var/www/releases/20120927 && php app/console doctrine:migrations:status --env=prod --em=custom_em\'') }
+ end
+
end
Something went wrong with that request. Please try again.