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
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 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 know that I can run specific migrations:
rake db:migrate:up VERSION=20080906120000
But can I run a specific migration against my test database? Will the following work?
RAILS_ENV=test rake db:migrate:up VERSION=20080906120000
In theory, running the migrations in default mode (which should affect the development database) and then running rake db:test:prepare should get the job done, but I found something strange with my test database after doing that, and I need to run a specific migration on the test database to aid my troubleshooting.
I'd just try out the above rake command, except I'm in the middle of a long data seeding run on my development database, and I can't risk the migration interfering with that, so I figured I'd see if anyone knows the answer before I can determine it myself. :)
In different order:
rake db:migrate:up VERSION=20080906120000 RAILS_ENV=test
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