Skip to content

Commit 398561b

Browse files
authored
Merge pull request rails#42694 from p8/railties/generators-raise-on-invalid-index
Generators should raise an error if an attribute has an invalid index
2 parents 70bbc37 + 76f9b75 commit 398561b

File tree

5 files changed

+25
-36
lines changed

5 files changed

+25
-36
lines changed

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Raise an error in generators if an index type is invalid.
2+
3+
*Petrik de Heus*
4+
15
* `package.json` now uses a strict version constraint for Rails JavaScript packages on new Rails apps.
26

37
*Zachary Scott*, *Alex Ghiculescu*

railties/lib/rails/generators/generated_attribute.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ class GeneratedAttribute # :nodoc:
3333

3434
class << self
3535
def parse(column_definition)
36-
name, type, has_index = column_definition.split(":")
36+
name, type, index_type = column_definition.split(":")
3737

3838
# if user provided "name:index" instead of "name:string:index"
3939
# type should be set blank so GeneratedAttribute's constructor
4040
# could set it to :string
41-
has_index, type = type, nil if INDEX_OPTIONS.include?(type)
41+
index_type, type = type, nil if valid_index_type?(type)
4242

4343
type, attr_options = *parse_type_and_options(type)
4444
type = type.to_sym if type
@@ -47,20 +47,28 @@ def parse(column_definition)
4747
raise Error, "Could not generate field '#{name}' with unknown type '#{type}'."
4848
end
4949

50+
if index_type && !valid_index_type?(index_type)
51+
raise Error, "Could not generate field '#{name}' with unknown index '#{index_type}'."
52+
end
53+
5054
if type && reference?(type)
51-
if UNIQ_INDEX_OPTIONS.include?(has_index)
55+
if UNIQ_INDEX_OPTIONS.include?(index_type)
5256
attr_options[:index] = { unique: true }
5357
end
5458
end
5559

56-
new(name, type, has_index, attr_options)
60+
new(name, type, index_type, attr_options)
5761
end
5862

5963
def valid_type?(type)
6064
DEFAULT_TYPES.include?(type.to_s) ||
6165
ActiveRecord::Base.connection.valid_type?(type)
6266
end
6367

68+
def valid_index_type?(index_type)
69+
INDEX_OPTIONS.include?(index_type.to_s)
70+
end
71+
6472
def reference?(type)
6573
[:references, :belongs_to].include? type
6674
end

railties/test/generators/generated_attribute_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ def test_field_type_with_unknown_type_raises_error
6868
assert_match message, e.message
6969
end
7070

71+
def test_field_type_with_unknown_index_type_raises_error
72+
index_type = :unknown
73+
e = assert_raise Rails::Generators::Error do
74+
create_generated_attribute "string", "name", index_type
75+
end
76+
message = "Could not generate field 'name' with unknown index 'unknown'"
77+
assert_match message, e.message
78+
end
79+
7180
def test_default_value_is_integer
7281
assert_field_default_value :integer, 1
7382
end

railties/test/generators/migration_generator_test.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,6 @@ def test_add_migration_with_attributes_and_indices
159159
end
160160
end
161161

162-
def test_add_migration_with_attributes_and_wrong_index_declaration
163-
migration = "add_title_and_content_to_books"
164-
run_generator [migration, "title:string:inex", "content:text", "user_id:integer:unik"]
165-
166-
assert_migration "db/migrate/#{migration}.rb" do |content|
167-
assert_method :change, content do |change|
168-
assert_match(/add_column :books, :title, :string/, change)
169-
assert_match(/add_column :books, :content, :text/, change)
170-
assert_match(/add_column :books, :user_id, :integer/, change)
171-
end
172-
assert_no_match(/add_index :books, :title/, content)
173-
assert_no_match(/add_index :books, :user_id/, content)
174-
end
175-
end
176-
177162
def test_add_migration_with_attributes_without_type_and_index
178163
migration = "add_title_with_index_and_body_to_posts"
179164
run_generator [migration, "title:index", "body:text", "user_uuid:uniq"]

railties/test/generators/model_generator_test.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,6 @@ def test_migration_with_attributes_and_with_index
223223
end
224224
end
225225

226-
def test_migration_with_attributes_and_with_wrong_index_declaration
227-
run_generator ["product", "name:string", "supplier_id:integer:inex", "user_id:integer:unqu"]
228-
229-
assert_migration "db/migrate/create_products.rb" do |m|
230-
assert_method :change, m do |up|
231-
assert_match(/create_table :products/, up)
232-
assert_match(/t\.string :name/, up)
233-
assert_match(/t\.integer :supplier_id/, up)
234-
assert_match(/t\.integer :user_id/, up)
235-
236-
assert_no_match(/add_index :products, :name/, up)
237-
assert_no_match(/add_index :products, :supplier_id/, up)
238-
assert_no_match(/add_index :products, :user_id/, up)
239-
end
240-
end
241-
end
242-
243226
def test_migration_with_missing_attribute_type_and_with_index
244227
run_generator ["product", "name:index", "supplier_id:integer:index", "year:integer"]
245228

0 commit comments

Comments
 (0)