Skip to content

Commit 43d2c87

Browse files
authored
Add support for table comments (#50)
This PR makes the following changes: * Adds support for table comments * Adds 2 new options keys: `with_column_comments` and `with_table_comments` * Checks for `with_comment: true, with_column_comments: true` to add column comments * Checks for `with_comment: true, with_table_comments: true` to add table comments * Changes `Options#load_defaults`, it defaults `with_column_comments` and `with_table_comments` to use the value set in `with_comment` if not previously set
1 parent 9ed5796 commit 43d2c87

File tree

11 files changed

+429
-18
lines changed

11 files changed

+429
-18
lines changed

dummyapp/Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
annotaterb (4.3.0)
4+
annotaterb (4.3.1)
55

66
GEM
77
remote: https://rubygems.org/

lib/annotate_rb/model_annotator/annotation_builder.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ def schema_header_text
7070
info << "#"
7171

7272
if @options[:format_markdown]
73-
info << "# Table name: `#{@model.table_name}`"
73+
info << "# Table name: `#{table_name}`"
7474
info << "#"
7575
info << "# ### Columns"
7676
else
77-
info << "# Table name: #{@model.table_name}"
77+
info << "# Table name: #{table_name}"
7878
end
7979
info << "#\n" # We want the last line break
8080

@@ -94,6 +94,20 @@ def schema_footer_text
9494

9595
info.join("\n")
9696
end
97+
98+
private
99+
100+
def table_name
101+
table_name = @model.table_name
102+
display_table_comments = @options[:with_comment] && @options[:with_table_comments]
103+
104+
if display_table_comments && @model.has_table_comments?
105+
table_comment = "(#{@model.table_comments.gsub(/\n/, "\\n")})"
106+
table_name = "#{table_name}#{table_comment}"
107+
end
108+
109+
table_name
110+
end
97111
end
98112
end
99113
end

lib/annotate_rb/model_annotator/column_annotation/annotation_builder.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def build
2626
column_attributes = AttributesBuilder.new(@column, @options, is_primary_key, column_indices, column_defaults).build
2727
formatted_column_type = TypeBuilder.new(@column, @options, column_defaults).build
2828

29-
col_name = if @model.with_comments? && @column.comment
29+
display_column_comments = @options[:with_comment] && @options[:with_column_comments]
30+
col_name = if display_column_comments && @model.with_comments? && @column.comment
3031
"#{@column.name}(#{@column.comment.gsub(/\n/, '\\n')})"
3132
else
3233
@column.name

lib/annotate_rb/model_annotator/model_wrapper.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ def table_exists?
4949
@klass.table_exists?
5050
end
5151

52+
def table_comments
53+
@klass.connection.table_comment(@klass.table_name)
54+
end
55+
56+
def has_table_comments?
57+
@klass.connection.respond_to?(:table_comment) &&
58+
@klass.connection.table_comment(@klass.table_name).present?
59+
end
60+
5261
def column_defaults
5362
@klass.column_defaults
5463
end
@@ -110,8 +119,7 @@ def retrieve_indexes_from_table
110119
end
111120

112121
def with_comments?
113-
@with_comments ||= @options[:with_comment] &&
114-
raw_columns.first.respond_to?(:comment) &&
122+
@with_comments ||= raw_columns.first.respond_to?(:comment) &&
115123
raw_columns.map(&:comment).any? { |comment| !comment.nil? }
116124
end
117125

lib/annotate_rb/options.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ def from(options = {}, state = {})
5151
sort: false, # ModelAnnotator
5252
timestamp: false, # RouteAnnotator
5353
trace: false, # ModelAnnotator, but is part of Core
54-
with_comment: true # ModelAnnotator
54+
with_comment: true, # ModelAnnotator
55+
with_column_comments: nil, # ModelAnnotator
56+
with_table_comments: nil # ModelAnnotator
5557
}.freeze
5658

5759
OTHER_OPTIONS = {
@@ -113,7 +115,9 @@ def from(options = {}, state = {})
113115
:sort,
114116
:timestamp,
115117
:trace,
116-
:with_comment
118+
:with_comment,
119+
:with_column_comments,
120+
:with_table_comments
117121
].freeze
118122

119123
OTHER_OPTION_KEYS = [
@@ -187,6 +191,10 @@ def load_defaults
187191
@options[:wrapper_open] ||= @options[:wrapper]
188192
@options[:wrapper_close] ||= @options[:wrapper]
189193

194+
# Set column and table comments to default to :with_comment, if not set
195+
@options[:with_column_comments] = @options[:with_comment] if @options[:with_column_comments].nil?
196+
@options[:with_table_comments] = @options[:with_comment] if @options[:with_table_comments].nil?
197+
190198
self
191199
end
192200

lib/annotate_rb/parser.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,31 @@ def add_model_options_to_parser(option_parser)
217217
"include database comments in model annotations") do
218218
@options[:with_comment] = true
219219
end
220+
221+
option_parser.on("--without-comment",
222+
"include database comments in model annotations") do
223+
@options[:with_comment] = false
224+
end
225+
226+
option_parser.on("--with-column-comments",
227+
"include column comments in model annotations") do
228+
@options[:with_column_comments] = true
229+
end
230+
231+
option_parser.on("--without-column-comments",
232+
"exclude column comments in model annotations") do
233+
@options[:with_column_comments] = false
234+
end
235+
236+
option_parser.on("--with-table-comments",
237+
"include table comments in model annotations") do
238+
@options[:with_table_comments] = true
239+
end
240+
241+
option_parser.on("--without-table-comments",
242+
"exclude table comments in model annotations") do
243+
@options[:with_table_comments] = false
244+
end
220245
end
221246

222247
def add_route_options_to_parser(option_parser)

spec/lib/annotate_rb/model_annotator/annotation_builder_spec.rb

Lines changed: 173 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@
13191319
end
13201320

13211321
let :options do
1322-
AnnotateRb::Options.new({classified_sort: false, with_comment: true})
1322+
AnnotateRb::Options.new({classified_sort: false, with_comment: true, with_column_comments: true})
13231323
end
13241324

13251325
let :columns do
@@ -1358,7 +1358,7 @@
13581358
end
13591359

13601360
let :options do
1361-
AnnotateRb::Options.new({classified_sort: false, with_comment: true})
1361+
AnnotateRb::Options.new({classified_sort: false, with_comment: true, with_column_comments: true})
13621362
end
13631363

13641364
let :columns do
@@ -1405,7 +1405,7 @@
14051405
end
14061406

14071407
let :options do
1408-
AnnotateRb::Options.new({classified_sort: false, with_comment: true})
1408+
AnnotateRb::Options.new({classified_sort: false, with_comment: true, with_column_comments: true})
14091409
end
14101410

14111411
let :columns do
@@ -1851,7 +1851,7 @@
18511851
end
18521852

18531853
let :options do
1854-
{format_rdoc: true, with_comment: true}
1854+
{format_rdoc: true, with_comment: true, with_column_comments: true}
18551855
end
18561856

18571857
let :columns do
@@ -1899,7 +1899,7 @@
18991899
end
19001900

19011901
let :options do
1902-
{format_markdown: true, with_comment: true}
1902+
{format_markdown: true, with_comment: true, with_column_comments: true}
19031903
end
19041904

19051905
let :columns do
@@ -1968,4 +1968,172 @@
19681968
end
19691969
end
19701970
end
1971+
1972+
describe "#schema_header_text" do
1973+
subject do
1974+
described_class.new(klass, options).schema_header_text
1975+
end
1976+
1977+
let(:table_exists) { true }
1978+
let(:table_comment) { "" }
1979+
1980+
let(:connection) do
1981+
indexes = []
1982+
foreign_keys = []
1983+
1984+
mock_connection_with_table_fields(
1985+
indexes,
1986+
foreign_keys,
1987+
table_exists,
1988+
table_comment
1989+
)
1990+
end
1991+
1992+
let :klass do
1993+
primary_key = nil
1994+
columns = []
1995+
1996+
mock_class_with_custom_connection(
1997+
:users,
1998+
primary_key,
1999+
columns,
2000+
connection
2001+
)
2002+
end
2003+
2004+
context "with no options set" do
2005+
let :options do
2006+
AnnotateRb::Options.new({})
2007+
end
2008+
2009+
let(:expected_header) do
2010+
<<~HEADER
2011+
#
2012+
# Table name: users
2013+
#
2014+
HEADER
2015+
end
2016+
2017+
it "returns the schema header" do
2018+
is_expected.to eq(expected_header)
2019+
end
2020+
end
2021+
2022+
context "with `with_comment: true`" do
2023+
context "with `with_table_comments: true` and table has comments" do
2024+
let :options do
2025+
AnnotateRb::Options.new({with_comment: true, with_table_comments: true})
2026+
end
2027+
2028+
let(:table_comment) { "table_comments" }
2029+
2030+
let(:expected_header) do
2031+
<<~HEADER
2032+
#
2033+
# Table name: users(table_comments)
2034+
#
2035+
HEADER
2036+
end
2037+
2038+
it "returns the header with the table comment" do
2039+
is_expected.to eq(expected_header)
2040+
end
2041+
end
2042+
2043+
context "with `with_table_comments: true` and table does not have comments" do
2044+
let :options do
2045+
AnnotateRb::Options.new({with_comment: true, with_table_comments: true})
2046+
end
2047+
2048+
let :klass do
2049+
primary_key = nil
2050+
columns = []
2051+
indexes = []
2052+
foreign_keys = []
2053+
2054+
mock_class(
2055+
:users,
2056+
primary_key,
2057+
columns,
2058+
indexes,
2059+
foreign_keys
2060+
)
2061+
end
2062+
2063+
let(:expected_header) do
2064+
<<~HEADER
2065+
#
2066+
# Table name: users
2067+
#
2068+
HEADER
2069+
end
2070+
2071+
it "returns the header without table comments" do
2072+
is_expected.to eq(expected_header)
2073+
end
2074+
end
2075+
2076+
context "with `with_table_comments: false` and table has comments" do
2077+
let :options do
2078+
AnnotateRb::Options.new({with_comment: true, with_table_comments: false})
2079+
end
2080+
2081+
let(:table_comment) { "table_comments" }
2082+
2083+
let(:expected_header) do
2084+
<<~HEADER
2085+
#
2086+
# Table name: users
2087+
#
2088+
HEADER
2089+
end
2090+
2091+
it "returns the header without the table comment" do
2092+
is_expected.to eq(expected_header)
2093+
end
2094+
end
2095+
end
2096+
2097+
context "with `with_comment: false`" do
2098+
context "with `with_table_comments: true` and table has comments" do
2099+
let :options do
2100+
AnnotateRb::Options.new({with_comment: false, with_table_comments: true})
2101+
end
2102+
2103+
let(:table_comment) { "table_comments" }
2104+
2105+
let(:expected_header) do
2106+
<<~HEADER
2107+
#
2108+
# Table name: users
2109+
#
2110+
HEADER
2111+
end
2112+
2113+
it "returns the header without the table comment" do
2114+
is_expected.to eq(expected_header)
2115+
end
2116+
end
2117+
2118+
context "with `with_table_comments: false` and table has comments" do
2119+
let :options do
2120+
AnnotateRb::Options.new({with_comment: false, with_table_comments: false})
2121+
end
2122+
2123+
let(:table_comment) { "table_comments" }
2124+
2125+
let(:expected_header) do
2126+
<<~HEADER
2127+
#
2128+
# Table name: users
2129+
#
2130+
HEADER
2131+
end
2132+
2133+
it "returns the header without the table comment" do
2134+
is_expected.to eq(expected_header)
2135+
end
2136+
end
2137+
end
2138+
end
19712139
end

0 commit comments

Comments
 (0)