Creating database & schema before rspec - ruby-on-rails

I have a RoR 5 app which uses MySQL for testing and development. The MySQL server has a development database created, but the testing database is missing (thus specs aren't running).
I could create the database on my own, but I'm wondering if there is a way I can make RoR create it for me when I run rspec spec.

I would do it like this:
RAILS_ENV=test bundle exec rake db:create
RAILS_ENV=test bundle exec rake db:schema:load

Related

How to automatically migrate the development and test databases with `rails db:migrate` and Spring?

I understand that rails db:migrate only affects the development database.
When running rspec with guard-rspec and spring, the test environment doesn't automatically apply migrations. Instead, you have to have guard fail, manually run RAILS_ENV=test rails db:migrate, and run your rspec guard again. This is expected behavior according to this issue: https://github.com/rails/rails/issues/25804
How could I make it so that either rails db:migrate does so for both environment at once, or have spring rspec automatically run pending migrations for the test environment as well?
I'd rather avoid a bash/zsh/shell alias because it has to be set up manually on everyone's machine.
As #stuart said in the comments, the only way is to either RAILS_ENV=test rails db:migrate && RAILS_ENV=development rails db:migrate or rails db:migrate db:test:prepare.

couldn't run test in a modular rails app

Before I ask my question, I like you to see the structure of my modular rails app, in which apps are engines and plugged into Domain/Parent empty app having only configurations.
See image below:
However, when I run test with rails test or rake test in the domain/parent directory that has nothing aside configuration, I get:
However, I can run rake tasks like bundle install,rake db:migrate etc from the root of the app or domain/parent directory successfully because the engine app is plugged into the root of the app. So it baffles my why rails test or rake test does not work.
I proceeded and cd into engines, into the root app inside the engine and I ran rails test or rake test or rails test test/models/csv_importer/user_test.rb but I get a different thing. See it below:
I did the response but test didn't still run
How do I test a modular rails app? Any help is appreciated.
When I run: rake db:test:prepare from the root of the engine, I get:
Okay, I fix the problem. What I had to do is:
From the root of the engine application (which is csv_importer as shown in the image in the question), I ran the following rails tasks:
bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:migrate RAILS_ENV=test
Then when I run rails test, my test starts running.

Build all tables within testing database from schema.rb

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

Do I have to run rake db:test:load each time manually before runnings tests?

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.

rake db:migrate doesn't seem to work in production

I have two instances of my app: one for development, one for production. My development database is called snip_development and my production database is called snip.
I've been doing migrations all along in my development environment and it's been going just fine. I recently created a production instance of my app but rake db:migrate doesn't seem to have any effect. After I run rake db:migrate and log into my database server, I can see that snip_development has all the tables I expect it to but snip doesn't have any tables at all.
I suspect the problem is that rake db:migrate is running on snip_development instead of snip and that's why I'm not seeing anything happen.
How do I get my migrations to work on my production database?
Sometimes I forget about Google. The answer is this:
rake db:migrate RAILS_ENV=production
For me the answer above not works. I have to add bundle exec to make it works.
bundle exec rails db:migrate RAILS_ENV=production

Resources