This project is archived and is in readonly mode.
[PATCH] Migrations with Improper Names Generate Invalid Code
Reported by sabat (at area51) | June 11th, 2010 @ 08:49 PM | in 3.0.2
In beta 4, if you generate a migration and use a name for it that does not start with "add" or "remove", the template fails to generate valid code. The template should not create code for adding or removing attributes unless the generator knows for sure that the developer is intending to do an add or remove migration -- he may just want to create a table without generating a model (e.g. "rails g migration CreateArticleTable name:string author:string").
If you attempt that example in beta 4, you end up with bogus template code that looks like:
class CreateArticleTable < ActiveRecord::Migration
def self.up
_column :create_article_tables, :name
_column :create_article_tables, :author
end
def self.down
add_column :create_article_tables, :author
add_column :create_article_tables, :name
end
end
This should instead generate empty methods:
class CreateArticleTable < ActiveRecord::Migration
def self.up
end
def self.down
end
end
I've attached a patch that fixes the template to do this. I don't have any tests for it; it seemed awkward to try to create a test for the presence of method bodies.
Comments and changes to this ticket
-
sabat (at area51) June 11th, 2010 @ 08:59 PM
- Tag changed from beta4 migrations template to beta4, migrations, template
Fixed patch: I hadn't followed the rules for generating it. Sorry. :-o
-
sabat (at area51) June 11th, 2010 @ 09:00 PM
- Title changed from Migrations with Improper Names Generate Invalid Code to [PATCH] Migrations with Improper Names Generate Invalid Code
-
sabat (at area51) June 11th, 2010 @ 09:16 PM
Fixed a minor formatting problem in previous version of patch.
-
Rohit Arondekar June 16th, 2010 @ 09:10 AM
- Milestone set to 3.x
- Tag changed from beta4, migrations, template to rails 3, activerecord, migrations, template
- State changed from new to open
- Assigned user set to José Valim
Confirmed issue on Rails master.
Patch applies cleanly, and solves the problem.
However does the change require tests?
-
José Valim June 19th, 2010 @ 11:48 PM
I need to apply this for the upcoming release. Could someone please work on tests? :)
-
Rohit Arondekar June 20th, 2010 @ 03:34 AM
I had a go at it. Ended up adding only one test. Feedback required. :)
Aside: I found a failing test in Railties, http://pastie.org/1011980 Might not have the time to investigate today, will do ASAP.
-
José Valim June 20th, 2010 @ 09:22 AM
Yes, this test is failing on purpose. There is a tip, you can always run just generator tests with:
rake test TEST_DIR=generators
inside the railties dir. Your test looks good Rohit, I will apply both patches soon! :) -
Repository June 20th, 2010 @ 11:43 AM
- State changed from open to committed
(from [d06105063829d44adf641059ef97f54d7b690f4b]) Add test for migration generator with name not starting with add or remove. [#4835 state:committed]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/d06105063829d44adf641059ef97f5... -
Rohit Arondekar June 20th, 2010 @ 02:34 PM
Jose, thanks for the tip. Btw my patch didn't include the original fixing patch so that will have to be applied separately.
-
Rohit Arondekar June 20th, 2010 @ 02:39 PM
Oh my bad, the patch didn't have a reference to this ticket. :)
In case anyone is looking for the commit -> http://github.com/rails/rails/commit/451594784552bd6955687d8ff617c4...
-
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to Low
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 4835 [PATCH] Migrations with Improper Names Generate Invalid Code (from [d06105063829d44adf641059ef97f54d7b690f4b]) Add tes...