How to manage migrations for a rails engine + dummy app - ruby-on-rails-3

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

Related

Upgrade to Rails 3 and lost ability to run Active Record database migrations

I have a corporate web application that, for various reasons, has Active Record connections to SQL Server as well as Mongoid Documents. There has been resistance to updating to Rails 3 until now (yes, I know, just in time for Rails 4). The app is mostly working fine and both databases play well with each other. However, I seem to have lost the ability to run SQL database migrations.
rake -T gives me the following db options:
rake db:drop # Drops all the collections for the database for the current Rails.env
rake db:mongoid:create_indexes # Create the indexes defined on your mongoid models
rake db:mongoid:drop # Drops the database for the current Rails.env
rake db:mongoid:purge # Drop all collections except the system collections
rake db:mongoid:remove_indexes # Remove the indexes defined on your mongoid models without questions!
rake db:purge # Drop all collections except the system collections
rake db:reseed # Delete data and seed
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, and initialize with the seed data
rake db:migrate just returns without any information whatsoever.
It looks like the mongoid gem might be the culprit as migrations work fine with a blank project and the only db options are mongoid related.
My question is: How do I restore ActiveRecord migrations after upgrading to rails 3 and mongoid 3.1.3?
Figured it out, at least the missing rake tasks part.
Turns out you need to have the require 'rails/all' in your config/application.rb file.
This returns the db tasks to rake.

Rails 4 Engine not migrating migrations

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

How to display RoR app code version on heroku?

For my Rails apps I normally deploy to production from a tagged version, and then display the tag in the user interface assigning the output of git describe --always to a variable in config/application.rb.
Now I'm moving an app over to Heroku, and deployment to heroku only happens using the master branch, so this trick won't work any more.
Are there any other ways to assign a version number to my code and display it on the UI when I've deployed to heroku?
Thanks,
Stewart
You can add a variable to the Heroku configuration by running this command locally whenever you push new changes to Heroku:
heroku config:add GIT_TAG=`git describe --always`
Then you can access this in your app's configuration:
version = ENV['GIT_TAG'] || `git describe --always`
When the app is running on Heroku, it will pick up the config variable (ENV['GIT_TAG']) and when it's running locally in development it will fall back to running git describe --always.
You will need to update the Heroku config variable each time you deploy, but I generally add this kind of thing to a deploy script or rake task (along with useful things like creating a new tag marking the deploy and running any new database migrations on Heroku).
Doesn't git tag fit your needs?
And why wouldn't the old trick work anymore?
If you want to display it on the UI then a git SHA output probably isn't particularly useful - you have two options, set a Heroku config variable with a user friendly version number in or a set a version number in your code that you increment when you deploy from master. You could probably wrap the deploy up in a rake task that incremented the version number either a file (and then readded it to git and commits it) or simply increments a value in a config variable.
Also, don't forget Heroku release management http://blog.heroku.com/archives/2010/11/17/releases/ which you may also be able to employ here to get the version number from that perhaps.

Register a script to use with the rails command, like `rails mycommand`

The rails command-line command provides a couple of commands, like rails generate, rails console etc. Now I'd like write a gem which registers my own command for use with rails mycommand.
Is this possible?
If so, any guides on how to do that?
NB: This is for rails 3+
regards, apeiros
Haven't done it, but here are some leads. In your Rails app, the script directory holds a file called 'rails' that has this line
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
It then requires this Rails file: https://github.com/rails/rails/blob/master/railties/lib/rails/commands.rb

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.