Rails 4 Engine not migrating migrations - migration

I created a new rails 4 engine and added a model. I am trying to migrate the database using
RAILS_ENV=test rake db:migrate
and it comes back with no errors. However, when I run:
rspec spec
an error returns stating there are pending migrations.
Migrations are pending; run 'rake db:migrate RAILS_ENV=test' to resolve this issue. (ActiveRecord::PendingMigrationError)
It's true if I look at my database (tried on pg and sqlite) that they have not been run and no tables have been created. Running the suggested command listed above does not run the migrations.
There is only one migration in 'engine_name/db/migrate', and no migrations inside the dummy app.
I am using ruby 2.0 and rails 4.0.0.rc1.

You need to copy migrations into dummy app rake app:<engine_name>:install:migrations

Add code in your engine task
https://gist.github.com/doabit/5692865 .

I've experienced this same issue. doabit's fix worked for me. thanks!
There is an open issue for this with the rails core team.
https://github.com/rails/rails/issues/10622

Related

Adding a new Migration File using Rake (in the command line)

I know there's a way to automatically create the barebones of a migration file by entering some rake command in the command line but I can't seem to remember exactly what it is. I know it should look something like this...
rake db:create NAME=table_name
but that keeps failing. I also tried
rake db:create_migration NAME=table_name
but no luck with this either. The first attempt returns "Don't know how to build task 'table_name' and the second says "Don't know how to build task 'migration'
rake isn't the command for this, instead you can either:
manually create the file
run rails generate migration table_name in a rails app
or in my case where I'm not using rails, pliny-generate migration table_name

Uable to run migration on heroku on a postgre database

I need to add a property/column to a table in a production database(Postgre) on Heroku.com (Rails app) by doing migration.
When I do the migration it looks ok, but when I view the columns on the table it has has not added the column!
My development db is sqlite3 and the production db is postgre
I do the following:
heroku run rails generate migration AddUtc_OffsetToEvents utc_offset:integer RAILS_ENV=production --app app-name-1111
and it returns:
invoke active_record
create db/migrate/20130304070946_add_utc_offset_to_events.rb
And then I run the migration
heroku run rake db:migrate RAILS_ENV=production --app app-name-1111
And then:
heroku restart
When I run
heroku pg:psql HEROKU_POSTGRESQL_GRAY_URL --app app-name-1111
and check the columns of the table:
\d+ events
It still does not have the utc_offset column, and no errors are displyed while doing the previous cmds.
Any ideas or hints?
It looks like you are doing several calls to heroku run
Each time you do heroku run it spins up a completely new dyno with your latest code, and when run is over that dyno is destroyed. So the second heroku run does not have the migration filed created in the first.
Since you are already familiar with psql you can just use ALTER TABLE directly. Otherwise you'll need to check your migration into your code and git push heroku master it to heroku, then run it.
Why not just download the code, add the migration and push the changes ?
After, just run the heroku run rake db:migrate on the app.

How to manage migrations for a rails engine + dummy app

I just joined a project developing a rails engine, that also has a dummy app for testing.
foo/
foo/spec/dummy/
There are identical migrations in
foo/db/migrate/
foo/spec/dummy/db/migrate/
If I rake db:migrate from the dummy app, all is well. If I do the same from the engine (current directory = foo) I get an error about multiple migrations with the same name.
Q1) Are the Rakefiles borked? (should db:migrate recurse down to the dummy app?)
Q2) Should the migrations only be in one directory? If so, which one?
We are using Rails 3.2.9, ruby 1.9.3p194.
Question 1
The Rakefile should have an entry to account for the spec/dummy app. For example,
Bundler::GemHelper.install_tasks
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'
Here's more detailed example rakefile, https://github.com/twinge/questionnaire_engine/blob/engine2/Rakefile
Question 2
IMO, the migrations should only exist on the foo/db/migrate folder, and not the foo/spec/dummy/db/migrate. In fact, I don't version control the dummy's db/migrate or the db/schema.
Why? I use the dummy app the make sure a full on install of my engine works 100%. Therefore, if I version controlled the foo/spec/dummy db state, I would be testing as if there was a previous install.
Example Engine
https://github.com/twinge/questionnaire_engine/tree/engine2
For testing the dummy app, you can run your engine migrations for the test ENV using the following:
RAILS_ENV=test rake db:migrate

Problem with rake: "development database is not configured"

I am novice rails/terminal user and just did a clean install of Lion + Xcode + Rails. Unlike before (on Snow Leopard), I now get an error running rake db:migrate.
I have cloned my code through git which worked fine and created the database witht the "createdb" command but when I try run "rake db:migrate" in terminal it now comes up with this error:
rake aborted!
development database is not configured
My config/database.yml file looks like below in the development section which is exactly the way it looked before on Snow Leopard where it worked fine, so don't know if the error I am now getting is related to Lion.
development:
adapter: postgresql
database: my_db
username: rasmus
encoding: utf8
pool: 5
Can anyone help, please?
I got the same error and in my case it was because the database.yml was not indented correctly. All the configuration parameters should be indented.
Note, be sure to follow the proper spacing conventions. The database config is whitespace aware. Two spaces per attribute works fine. In the following code, note how each attribute has two spaces. Do not use tabs. If you don't use spaces for attributes, rake will not work and throw the same error.
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
database: db/production
pool: 5
timeout: 5000
password:
You might also want to look for syntax errors in the file. This is the error that will appear if you have a syntax error in your config/database.yml file and you try to do something like start the local web server or run rake db:migrate.
In my case I had accidentally removed the comment from a line at the top of the file and I was seeing this error since the uncommented line made this an invalid yml file.
Solved!
My "gem install pg" had not been run so basically I was missing the pg gem. After "gem install pg" in terminal everything works fine.
Here's a PEBCAK answer for Googlers - check your Gemfile and make sure you have specified your database adapter gem in the proper group in your Gemfile. I had mine defined for only :production and :staging, and at one point must have manually ran gem install pg on my development machine after switching from mysql. This morning I emptied all of the gems for the app and re-bundle install-ed them, then couldn't figure out why the database wouldn't connect. Moving the pg gem spec outside of any group and running bundle install resolved the problem.
A note to others who land on this question page: be sure that you are running the rake db command correctly, i.e:
rake db:migrate instead of rake db migrate
What worked in my case, having tried all the above when rake db:create failed, was to make sure that my Rakefile was properly configured.
This did the job:
require "sinatra/activerecord/rake"
require 'sinatra/asset_pipeline/task'
namespace :db do
task :load_config do
require "./app"
end
end

Rails. "unitialized Contsant Object::Contact"

I'm working through the Ruby on Rails Bible using Windows 7 and Rails 3 + mysql.
I created a database and a table in mysql directly as per instructions.
Then I created a model called Contact
Next in irb I entered:
my_contact=Contact.new and then I get the error:
"unitialized constant Object::Contact"
I think perhaps I have to precede the code with a require statement or perhaps I need to install a gem? Except I haven't a clue beyond that at this stage as I'm a newbie...
Instead of running irb manually, run rails console, this should load all dependencies for your app. Also make sure you have run rake db:migrate before starting the console.