Skip to content

Commit 208646a

Browse files
amerritt14drwl
andauthored
feat: identify unique indexes in simple_indexes option (#214)
When generating simple indexes, there is no identifier for unique indexes. This PR updates the `simple_index` option to annotate with `uniquely indexed` for unique indexes. Screenshot from a project I tested with: ![image](https://github.com/user-attachments/assets/7647959e-1cc9-41d5-bd9d-134a96404dee) Co-authored-by: Andrew W. Lee <git@drewlee.com>
1 parent 90df34c commit 208646a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,16 @@ def build
7070
sorted_column_indices&.each do |index|
7171
indexed_columns = index.columns.reject { |i| i == @column.name }
7272

73-
attrs << if indexed_columns.empty?
73+
index_text = if index.unique
74+
"uniquely indexed"
75+
else
7476
"indexed"
77+
end
78+
79+
attrs << if indexed_columns.empty?
80+
index_text
7581
else
76-
"indexed => [#{indexed_columns.join(", ")}]"
82+
"#{index_text} => [#{indexed_columns.join(", ")}]"
7783
end
7884
end
7985
end

spec/lib/annotate_rb/model_annotator/column_annotation/attributes_builder_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@
153153

154154
it { is_expected.to match_array(expected_result) }
155155
end
156+
157+
context "with a column including an index with a unique constraint" do
158+
let(:column) { mock_column("name", :string) }
159+
let(:expected_result) { ["not null", "primary key", "uniquely indexed"] }
160+
161+
let(:column_indices) do
162+
[
163+
mock_index("index_rails_02e851e3b8", columns: ["name"], unique: true)
164+
]
165+
end
166+
167+
it { is_expected.to match_array(expected_result) }
168+
end
156169
end
157170

158171
context "when the hide_default_column_types option is 'skip' with a json column" do

0 commit comments

Comments
 (0)