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
Related
I recently started using Rails, and created a few Models using the CLI which in turn created some migrations.
I ran the rake db:migrate command after adding all my columns in there, and then realized that I'd left out the associations.
So what did I do?
I went ahead and edited the migrations to include those keys.
I ran rake db:migrate again, and nothing changed in the schema.
Then I ran rake db:reset and then rake db:setup.
When that didn't work, I deleted my schema.rb (the darn thing wouldn't get updated!) and tried recreating it. When I realized that didn't work, I dropped the database, and killed the schema.
Now I'm stuck with some manually modified migrations, no schema.rb and no database.
How do I get the modified migrations to generate a schema, and play nice with Rails?
In development it does not matter to drop and rebuild your database. I do it often and I even have a rake task for that. The 3 command to chain are:
rake db:drop
rake db:create
rake db:migrate
# And a 4rth optional command to rebuild your test database
rake db:test:prepare
With this you should be good
Next time you need to modify a migration manually after migrating it, you should process by:
rake db:rollback
edit your migration
rake db:migrate
Following those steps will save you some headaches
Bonus info:
After you deployed your migration to your production server you cannot manually modify it, hence you must write another migration that will perform the modification (adding columns, etc...)
I am following the rails tutorial videos and I can't figure out what the db:test:prepare command actually does. Can someone provide an explanation?
The rake db:migrate above runs any pending migrations on the
development environment and updates db/schema.rb. The rake
db:test:load recreates the test database from the current
db/schema.rb. On subsequent attempts, it is a good idea to first run
db:test:prepare, as it first checks for pending migrations and warns
you appropriately.
-- http://guides.rubyonrails.org/testing.html
Basically it handles cloning the database so you don't have to run the migrations against test to update the test database.
Specifically, rake db:test:prepare will do the following:
Check for pending migrations and,
load the test schema
That is, it will look your db/schema.rb file to determine if any migrations that exist in your project that have not been run. Assuming there are no outstanding migrations, it will then empty the database and reload it based on the contents of the db/schema.rb file.
rake db:test:prepare is a good solution for PG issues like this.
“PG::UndefinedTable: ERROR: relation does not exist” with a correct Rails naming and convention" where I couldn't just execute rake db:migrate RAILS_ENV=production
When, for example you can't create test database for a bug discussed here: "PG undefinedtable error relation users does not exist"
All arround this error
"PG::UndefinedTable: ERROR: relation xxxxx does not exist”
I'm following along with some tutorials and they are teaching how to create and run migrations. All has been working so far but I have run in to a problem. But now when i have come back to it the migration will not work in the development database. I used rake db:migrate RAILS_ENV=production to see if my migrations work and on that database it can go through all the migrations and back down again with no problems are errors but when I run rake db:migrate RAILS_ENV=development it pauses for a few seconds then returns me to the console awaiting a command. No errors are shown or nothing is returned to show what it has done and when checking the DB nothing has changed. What can this be?
Use --trace
rake db:migrate --trace
Show your database.yml file please.
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
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