Commit 441fc007 by Cuong Tran Committed by GitHub

Add `unsigned` support for numeric data types (#393)

parent 593273e4
...@@ -187,15 +187,7 @@ module AnnotateModels ...@@ -187,15 +187,7 @@ module AnnotateModels
# the type (and length), and any optional attributes # the type (and length), and any optional attributes
def get_schema_info(klass, header, options = {}) def get_schema_info(klass, header, options = {})
info = "# #{header}\n" info = "# #{header}\n"
info<< "#\n" info << get_schema_header_text(klass, options)
if options[:format_markdown]
info<< "# Table name: `#{klass.table_name}`\n"
info<< "#\n"
info<< "# ### Columns\n"
else
info<< "# Table name: #{klass.table_name}\n"
end
info<< "#\n"
max_size = klass.column_names.map(&:size).max || 0 max_size = klass.column_names.map(&:size).max || 0
max_size += options[:format_rdoc] ? 5 : 1 max_size += options[:format_rdoc] ? 5 : 1
...@@ -220,9 +212,9 @@ module AnnotateModels ...@@ -220,9 +212,9 @@ module AnnotateModels
cols = classified_sort(cols) if options[:classified_sort] cols = classified_sort(cols) if options[:classified_sort]
cols.each do |col| cols.each do |col|
col_type = (col.type || col.sql_type).to_s col_type = (col.type || col.sql_type).to_s
attrs = [] attrs = []
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || hide_default?(col_type, options) attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || hide_default?(col_type, options)
attrs << 'unsigned' if col.respond_to?(:unsigned?) && col.unsigned?
attrs << 'not null' unless col.null attrs << 'not null' unless col.null
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) 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)
...@@ -280,6 +272,23 @@ module AnnotateModels ...@@ -280,6 +272,23 @@ module AnnotateModels
info << get_foreign_key_info(klass, options) info << get_foreign_key_info(klass, options)
end end
info << get_schema_footer_text(klass, options)
end
def get_schema_header_text(klass, options = {})
info = "#\n"
if options[:format_markdown]
info << "# Table name: `#{klass.table_name}`\n"
info << "#\n"
info << "# ### Columns\n"
else
info<< "# Table name: #{klass.table_name}\n"
end
info << "#\n"
end
def get_schema_footer_text(_klass, options = {})
info = ""
if options[:format_rdoc] if options[:format_rdoc]
info << "#--\n" info << "#--\n"
info << "# #{END_MARK}\n" info << "# #{END_MARK}\n"
......
...@@ -118,6 +118,7 @@ EOS ...@@ -118,6 +118,7 @@ EOS
# #
EOS EOS
end end
it "should get schema info with enum type " do it "should get schema info with enum type " do
klass = mock_class(:users, nil, [ klass = mock_class(:users, nil, [
mock_column(:id, :integer), mock_column(:id, :integer),
...@@ -135,6 +136,29 @@ EOS ...@@ -135,6 +136,29 @@ EOS
EOS EOS
end end
it "should get schema info with unsigned" do
klass = mock_class(:users, nil, [
mock_column(:id, :integer),
mock_column(:integer, :integer, :unsigned? => true),
mock_column(:bigint, :bigint, :unsigned? => true),
mock_column(:float, :float, :unsigned? => true),
mock_column(:decimal, :decimal, :unsigned? => true, :precision => 10, :scale => 2),
])
expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null
# integer :integer unsigned, not null
# bigint :bigint unsigned, not null
# float :float unsigned, not null
# decimal :decimal(10, 2) unsigned, not null
#
EOS
end
it "should get schema info for integer and boolean with default" do it "should get schema info for integer and boolean with default" do
klass = mock_class(:users, :id, [ klass = mock_class(:users, :id, [
mock_column(:id, :integer), mock_column(:id, :integer),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment