I am trying to run some custom SQL during the setup of my Rails integration tests to prepare a legacy database (e.g., to create its tables, create any required views, etc.), which is not part of my schema.rb (nor do any migrations for it exist).
Are there any best practices for doing so? Googling has not been very enlightening so far ;-)
The reason why there are not any migrations is that in the development and production RAILS_ENV the database already exists for legacy reasons. If there is a way to run these migrations only for RAILS_ENV=='test', that would maybe also help.
You can pass the Rails environment to Rake on the command line. For example, to only run migrations for the test environment, do:
rake RAILS_ENV=test db:migrate
Related
I am hosting a project on heroku, written in rails.
I have a fixture file (comments.yml) that I use during testing, but which I do not want to include when seeding my database (via 'heroku run rails db:seed').
How can I communicate to heroku that I want it to ignore 'comments.yml' when seeding?
You could use environment variables and control it in your seed script:
rake db:seed comments=true
and later in your seeds.rb:
unless ENV["comments"]
I am trying to implement testing and realized I need to create a testing database.
I successfully created the schema.rb file from the database by running rake db:schema:dump.
I also successfully created the new testing database.
Now what I need to do (I think) is do something like a rake db:schema:load, but I want to specify that I am doing this for the test database (not the development database or the production database. I don't want to delete any of the data there!)
I attempted rake db:test:schema:load but that was not working.
Searches online advise using commands that appear to be deprecated for rails 4.1 or later.
Answer based on feedback:
RAILS_ENV=test rake db:schema:load
If you want to specify that you are creating the "test" database:
RAILS_ENV=test rake db:create
The RAILS_ENV environmental variable will allow you to specify which environment you wish to use. These environments will be defined in your "config/database.yml". Ensure that you have a test environment setup and a database specified under it. This can be done to any task that you would like to affect a particular environment.
RAILS_ENV=test rake any:task:here
I started building my app in PHP but was convinced by some developer friends to change to RoR instead. Since I already had my database structure designed and created in mysql, I never used any rails migrations to create tables; I just created the appropriate models to match the existing database schema.
So, now I am at the point where I want to test deployment and, of course, I have no migrations to rake to recreate the db on, for example, Heroku.
I know that I can simply go back and recreate the database by creating migrations but my app has dozens of tables with hundreds of fields in total.
Is there any way to create a set of migrations based on my existing db schema, or will I just have to knuckle under and build the migrations one by one to recreate the structure via rails' migrations.
Actually, there are some rake tasks to do the work:
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
You can run: heroku run rake db:schema:load.
We need constantly update our database schema in production for rails 3.1.3 app. The first db schema was created with the following rails command:
$rake RAILS_ENV=production db:schema:load
Question is: can we still use the command above to update db schema in production while safely retaining all current data?
Thanks so much.
I never used rake db:schema:load in production, but according to this answer to another question here on SO, I don't think you want to do that.
On the other hand, I have used RAILS_ENV=production rake db:migrate several times on the server with data already in the database and never experienced any problems.
I'm new to Ruby on Rails.
I'm trying to set up a simple WebApp via Scaffolding. And using RSpec for my tests. Now after the scaffold command:
rails generate scaffold VideoSegment file_path:string short_name:string description:string
I ran rake db:migrate, but thats clear, bringing the data to my development database.
But the tests where not green before I did:
rake db:test:load
To bring the schema of my development database to the test database. Isn't there a way to automate this step? Or do I have to load test database again after each scaffold?
PS: Of course I know Scaffold is not doing the finest things, but for my proof of concept need it's sufficient.
Thanks for any suggestions.
Whenever you run rspec it will prepare the test schema for you using the task: db:test:prepare
So after generating migrations you have to do rake db:migrate to update the development schema and then run you spec which will automatically prepare the test database for you.