Skip to content

Commit f37e886

Browse files
ctranjohncarney
authored andcommitted
Add unsigned support for numeric data types (ctran#393)
1 parent b6246b5 commit f37e886

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

lib/annotate/annotate_models.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,7 @@ def schema_default(klass, column)
187187
# the type (and length), and any optional attributes
188188
def get_schema_info(klass, header, options = {})
189189
info = "# #{header}\n"
190-
info<< "#\n"
191-
if options[:format_markdown]
192-
info<< "# Table name: `#{klass.table_name}`\n"
193-
info<< "#\n"
194-
info<< "# ### Columns\n"
195-
else
196-
info<< "# Table name: #{klass.table_name}\n"
197-
end
198-
info<< "#\n"
190+
info << get_schema_header_text(klass, options)
199191

200192
max_size = klass.column_names.map(&:size).max || 0
201193
max_size += options[:format_rdoc] ? 5 : 1
@@ -220,9 +212,9 @@ def get_schema_info(klass, header, options = {})
220212
cols = classified_sort(cols) if options[:classified_sort]
221213
cols.each do |col|
222214
col_type = (col.type || col.sql_type).to_s
223-
224215
attrs = []
225216
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || hide_default?(col_type, options)
217+
attrs << 'unsigned' if col.respond_to?(:unsigned?) && col.unsigned?
226218
attrs << 'not null' unless col.null
227219
attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(col.name.to_sym) : col.name.to_sym == klass.primary_key.to_sym)
228220

@@ -280,6 +272,23 @@ def get_schema_info(klass, header, options = {})
280272
info << get_foreign_key_info(klass, options)
281273
end
282274

275+
info << get_schema_footer_text(klass, options)
276+
end
277+
278+
def get_schema_header_text(klass, options = {})
279+
info = "#\n"
280+
if options[:format_markdown]
281+
info << "# Table name: `#{klass.table_name}`\n"
282+
info << "#\n"
283+
info << "# ### Columns\n"
284+
else
285+
info<< "# Table name: #{klass.table_name}\n"
286+
end
287+
info << "#\n"
288+
end
289+
290+
def get_schema_footer_text(_klass, options = {})
291+
info = ""
283292
if options[:format_rdoc]
284293
info << "#--\n"
285294
info << "# #{END_MARK}\n"

spec/annotate/annotate_models_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def mock_column(name, type, options={})
118118
#
119119
EOS
120120
end
121+
121122
it "should get schema info with enum type " do
122123
klass = mock_class(:users, nil, [
123124
mock_column(:id, :integer),
@@ -135,6 +136,29 @@ def mock_column(name, type, options={})
135136
EOS
136137
end
137138

139+
it "should get schema info with unsigned" do
140+
klass = mock_class(:users, nil, [
141+
mock_column(:id, :integer),
142+
mock_column(:integer, :integer, :unsigned? => true),
143+
mock_column(:bigint, :bigint, :unsigned? => true),
144+
mock_column(:float, :float, :unsigned? => true),
145+
mock_column(:decimal, :decimal, :unsigned? => true, :precision => 10, :scale => 2),
146+
])
147+
148+
expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
149+
# Schema Info
150+
#
151+
# Table name: users
152+
#
153+
# id :integer not null
154+
# integer :integer unsigned, not null
155+
# bigint :bigint unsigned, not null
156+
# float :float unsigned, not null
157+
# decimal :decimal(10, 2) unsigned, not null
158+
#
159+
EOS
160+
end
161+
138162
it "should get schema info for integer and boolean with default" do
139163
klass = mock_class(:users, :id, [
140164
mock_column(:id, :integer),

0 commit comments

Comments
 (0)