I'm playing around with rails migrations and I've ran into a problem which I haven't been able to find a solution to.
Basically, I use the rails generators to create and destroy migrations to add and remove variables in models.
As a result of using 'rails destroy migration', my schema is all messed up. It doesn't reflect my present migrations at all. Is this a limitation of the destroy mechanism, or have I missed some crucial rake db: commands? Is there a way to reconstruct the schema from the current migrations?
I'm having trouble putting the pieces together, a link or keywords would suffice.
Rollback your migrations,
rake db:rollback to rollback to the last migration.
rake db:rollback STEP=3 will revert the last 3 migrations.
More about rake tasks here
Related
sometimes when working in rails, I work on several things at once using git branches
sometimes, I'd like to test new ideas by implementing them and testing how and if they work accordingly. This involves sometimes adding models and migrations.
When switching branches, however, the migrations were already migrated to the DB and they stay, causing problems later on..
Is there a way to work with several branches and each to have different migration files, and before starting to work on a branch to "soft reset" the db only to the current migration files without losing data?
Normally, in development, I need some sample data that I keep in seed.rb which enables me to recreate the db, its structure and the sample data with a rake task.
Another thing I did was to keep more than one database. I would then just manually change the entry in database.yml according to the current git branch.
In my rails app I have a model Events. It has several columns which were created through following this guide https://devcenter.heroku.com/articles/export-from-heroku-postgres and then performing rake db:schema:dump.
Now I want to add some new columns to the Event model. I tried editing schema.rb and restarting the app but that didn't seem to work. Anyone know the proper way to proceed?
----Edit-------
Specifically I added this line to the Event model in schema.rb.
t.datetime "date_time"
When I click the link to add a event in the rails app I receive this error:
undefined method `date_time' for #<Event:0x007fdb59a87118>
The schema.rb file is a representation of the current state of the database schema, it is written by the database migration process, not read.
If you want to add a new column, create a migration:
$ rails generate migration AddNewEventStuff
$ vim db/migrate/add_new_event_stuff*.rb
Then add your columns:
class AddNewEventStuff < ActiveRecord::Migration
def change
add_column ...
end
end
Everything except the add_column should be there already. Once you have your migration, do a rake db:migrate and you're done. Now you should see some changes in your schema.rb.
See the Ruby on Rails Migration Guide for further details and different ways to build your migrations.
You should try heroku run rake db:schema:load. That said, it's probably the wrong way to go about. Instead, you should make changes to your schema via migrations, and then run a heroku run rake db:migrate on it.
For automated testing, RSpec and FactoryGirl are used.
Frequently, I need to manually play with my application. So, i need a convenient way to populate database with some data.
The most convenient way to do it - is fixtures, because they handling relationships between models very well.
I know, that i can load fixtures via rake db:fixtures:load command, but sometimes i need to populate only specific models (say, only customers --> orders --> products)
I'm looking for a command, like this:
rake db:fixtures:load --models=customers,orders,products
You can use:
rake db:fixtures:load FIXTURES=customers,orders,products
What about using the seed command?
rake db:seed
Here's a railscast explaining it in detail:
http://railscasts.com/episodes/179-seed-data
http://asciicasts.com/episodes/179-seed-data
I'm working on an application which uses data from db which has to be populated prior to the application being able to run. What I have to do is to populate few tables with few thousand rows but I'm not sure how I would do that in heroku because I have limited access to db for loading data.
What is the preferred way to do this?
Regards,
Johann
You can populate a Postgres database locally and then push it to heroku with heroku db:push see heroku help db:push
You may want to look into seeds, there's also a Railscast. I've never used this before, so you may alternately want to…
Create a rake task to suit your specific need. That way you can add the task to your Rails application and run heroku rake mytask. Here's a rake tutorial, and a Railscast on rake tasks to help get you started.
I used dm-rails gem that allows datamapper to hook into rails-3 ,generated a scaffold and a migration file ,did rake db:migrate for database migration but nothing happens no error no migration, can any one suggest me how to run migrations with datamapper and rails-3.
You have to get dm-rails that add among other things, 2 specific rake tasks:
rake db:automigrate
it performs destructive automigration, so it checks if table exists, and if so then drop it, and create one more time.
and
rake db:autoupgrade
It's upgrade your database schema to match your model properties.