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.
Related
I'm a total newbie with Ruby, Rails, Rake, MySQL (on Ubuntu/bash/command line) and devise (warden 1.2.3, devise 3.0.4). I must continue with what have been done so far : the Rails 4 project is working and linked to a MySQL database (full, populated with users and data). I'm able to change controllers and views.
I must add devise to the project. I'm reading this : https://github.com/plataformatec/devise . My class name is User. So I did rails generate devise User.
When I do rake db:migrate I have this error : Mysql2::Error: Table 'users' already exists: ... of course, since I already have a users table in my project.
Whatever I read ( Devise with Rails 4 ), it's always about create the User model, never how to plug devise on a pre-existing User model.
What document should I read ? Which file I must edit to tell devise "this is my user model and table:" ?
If Ruby/Rails is brand new for you, and you're lost.. (like I was...).
Look for the migration file (migration-name.rb) in "/yourproject/db/migrate/". This file was generated by rails generate devise User (User can by any Model, like UserAdmin or Client ...). This file tells you the modifications Devise will do to the database (what it needs).
Look at this file carefully and make sure that your database match the requirements. If you run rake db:migrate rake will attempt to modify your database. Maybe this is your error (attempt to create a table User, since your table User is already created).
Modify your migration file. You may also change your database manually and delete the migration file (if you don't care to keep track of your migrations); in this last case, of course, you don't have to run the rake command.
Follow the Devise getting-started guide, and intelligently apply the manual steps to your current project where necessary.
https://github.com/plataformatec/devise#getting-started
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
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.
My Rails 3.0 app has three new migrations pending, X and Y and Z, but some manual intervention is required after running X and before running Y and Z.
Is there a way to tell rake "run anything pending up to and including X" (then do my manual stuff)?
After that manual cleanup of course I can just run the normal rake db:migrate and it'll catch up with the rest.
(In the future, if we clone and rebuild the app from scratch someday it is not a problem to run them all at once. It's a a matter of one-time massaging the legacy data between those two migrations.)
rake db:migrate:up VERSION=X
# do your stuff
rake db:migrate
Source: Migrations-Guide
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