Symfony 4: Prevent Doctrine Fixtures from being loaded in DEV-Environment - doctrine-orm

If you want to prevent the database fixtures from being loaded accidentally in the wrong environment, the (probably) best way is to activate the DoctrineFixturesBundle only in certain environment(s).
Up to Symfony 3.4 this was done in app/AppKernel.php, as described at https://symfony.com/doc/3.4/best_practices/business-logic.html#data-fixtures
How can this be achieved in Symfony 4 (Symfony Flex), where bundles are being loaded automatically?

In Symfony 4, this can be configured in config/bundles.php, by editing the line
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
See https://symfony.com/doc/4.1/best_practices/business-logic.html#data-fixtures
When you remove the 'dev' => true, part, and then try to load the fixtures in DEV environment by running php bin/console doctrine:fixtures:load --env=dev, you will get:
Error thrown while running command "'doctrine:fixtures:load' --env=dev". Message: "There are no commands defined in the "doctrine:fixtures" namespace.
However, loading them in TEST environment still works: php bin/console doctrine:fixtures:load --env=test

Related

Rails Integration tests don't start fresh

I'm having difficulty running multiple integration tests on Rails 4 (now called system tests in Rails 5).
Environment: Rails 4 / Minitest / Capybara / Poltergeist running on the Puma server.
When I run a single test that creates a new record, it works every time.
RAILS_ENV="test" ruby -I test test/integration/requests_test.rb -n /create_new/
When I run the entire set of tests, the above test fails to create every time because the record already exists.
RAILS_ENV="test" ruby -I test test/integration/requests_test.rb
I confirmed this by adding puts Request.all.collect(&:name) at the start - when running the group, the record being created already in the DB.
Here's the core issue - the DB is not reliably fresh for every test. (It is fresh for my unit tests and my functional tests, in groups and as individuals.) How can I make sure that my integration tests also start fresh each time?
In case it's helpful, the command above seems to be running Puma in development mode, even though I've specified ENV['RAILS_ENV'] = 'test' in test_helper.rb.
Have you checked out the database_cleaner gem (https://github.com/DatabaseCleaner/database_cleaner)? You can use it to clear the database every time you run a new integration test in your RailsHelper config. I'd check out their documentation but mine looks like:
DatabaseCleaner.strategy = :truncation
RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.clean
end
config.after(:each) do
FactoryGirl.reload #if you're using FactoryGirl
end
end

how to properly enable open-uri in rails console and therefore get it work in a project

When I require open-uri in rails console by : require 'open-uri'
I get false.
Any suggestions, ideas please ?
Env :
Rails 4.2.4
ruby 2.2.1p85
You get false because the gem is already been loaded. Rails console load your environment.
bin/rails c
Loading development environment (Rails 5.0.0.alpha)
2.2.3 :001 > require 'open-uri'
=> false
If you try require it in a interactive ruby console (irb), the require command return true because the gem has not yet been loaded.
irb
2.2.3 :001 > require 'open-uri'
=> true
# If you try to require it a second time you'll get false because is already been loaded
2.2.3 :002 > require 'open-uri'
=> false
Open Uri is by default required in rails console. you can test it by calling open('http://google.com') if it runs successfull you already have 'open-uri'

How to correctly add ENV["SECRET_KEY"BASE"] in rails

I am having a difficult time setting up Rails 4.2 in production on a VM running on passenger and nginx, and not using RVM or anything similar.
I got Incomplete response received from application and looking in the nginx error log it said something about missing secret_key_base and secret_key although there is no reference to that last one any where in the config directory.
I ran export SECRET_KEY_BASE='...' and in rails c production ENV["SECRET_KEY_BASE"] displays the key but after restarting nginx I still get the same error.
Placing the key directly in secrets solved that problem but is there an actual way to do this correctly?
Solution:
The solution that worked for me is to place export SECRET_KEY_BASE="<string obtained from rake secret>" in .bashrc
If you use rbenv, there is another solution below in the accepted answer.
If you are using rbenv you can add the rbenv-vars plugin and add a .rbenv-vars file containing (and don't check that into your repo)
SECRET_KEY_BASE='...'
other solution is to add the the SECRET_KEY_BASE manually to the secrets.yml file and also ignore that file from your repo.
a third answer that saw mentioned is adding
export SECRET_KEY_BASE='...'
to one of these files .bashrc .bash_profile .profile
Your config/secrets.yml should have something like
development:
secret_key_base: f91fe2e2e4a9bf8f8b6aa1c296bb9ec10f2bc91c08965176a642ea0927400651ea993512f83d9823bcc046555e40b8c257f5f19fab8c59b5a02c9d230a369fe7
test:
secret_key_base: c116ac7c8f69018d1f4e10f632cac7a22348f0bd8ed8f21ca45460574d2f501f248418bc888e31556e16ba3ab58c3a7cba027140097abe3f511dddf6625fa8cd
# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
To set SECRET_KEY_BASE, first you'll need to generate it with
rake secret
Then take that output and edit your /etc/environment (depending on your distro, assuming Ubuntu here) and place it as such
SECRET_KEY_BASE=...
Restart your server and you should be gravy

What the difference between "RAILS_ENV=production rails c" and "rails c -e production"

I used to think I could just ssh to my server, change directory and run
rails c -e production
It would then prompt me
Loading production environment (Rails 4.1.2)
2.1.0 :001 >
Production environment I would think
But when I made a query in the console I get an access denied message from the mySql.
Then I tried starting the console like this
RAILS_ENV=production rails c
Get the same prompt and my queries all work.
What the purpose of the first statement then?
There's no difference unless you have code inside your app that uses ENV['RAILS_ENV'] instead of calling Rails.env to figure out what environment it has loaded.

Error deploying assets with Capistrano 2.15.5 on Rails 4

I'm having a bazaar error occur when deploying my Rails 4 application with Capistrano 2.15.5. This is an app that i am migrating from Rails 3.2, and this is my first deployment attempt with this version of Capistrano and Rails 4.
It appears to be seriously munging the release path, here is the output from Capistrano:
* executing "cp -- /www/awesome_app/shared/assets/manifest-4f237c854d9f335aac16f877929b75a8.json /www/awesome_app/releases/\\\e\\[34m20140417213248\\\e\\[39\\;49m\\\e\\[0m/assets_manifest.json"
servers: ["staging.private"]
[staging.private] executing command ** [out :: staging.private] cp: /www/awesome_app/releases/\033[34m20140417213248\033[39;49m\033[0m/assets_manifest.json: No such file or directory
command finished in 613ms
*** [deploy:update_code] rolling back
The asset manifest file gets created ok:
/www/awesome_app/shared/assets/manifest-4f237c854d9f335aac16f877929b75a8.json
But the cp command it is generating has a totally messed up path:
/www/awesome_app/releases/\\\e\\[34m20140417213248\\\e\\[39\\;49m\\\e\\[0m/assets_manifest.json
This path contains the date, as it should, but also a bunch of what look like escaped characters.
Has anyone seen this or know what is going on here?
thanks!
Answer: upgrade to Capistrano 3.x
After doing that, my rails 4 app deployed without issues.
Still no idea what the underlying problem here was.