Skip to content

Commit 63147e1

Browse files
committed
Load columns from schema to avoid AR ignored/only column configuration
1 parent ce79f8d commit 63147e1

File tree

12 files changed

+69
-1
lines changed

12 files changed

+69
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Annotate model options:
155155
--without-table-comments exclude table comments in model annotations
156156
--classes-default-to-s class Custom classes to be represented with `to_s`, may be used multiple times
157157
--nested-position Place annotations directly above nested classes or modules instead of at the top of the file.
158+
--load-columns-from-schema Load columns from schema to avoid ActiveRecord ignored/only column configuration
158159
159160
Annotate routes options:
160161
Usage: annotaterb routes [options]

lib/annotate_rb/model_annotator/model_wrapper.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ def database_name
4848

4949
# Returns the unmodified model columns
5050
def raw_columns
51-
@raw_columns ||= @klass.columns
51+
@raw_columns ||= if @options[:load_columns_from_schema]
52+
@klass.load_schema
53+
@klass.schema_cache.columns_hash(@klass.table_name).values.freeze
54+
else
55+
@klass.columns
56+
end
5257
end
5358

5459
def primary_key

lib/annotate_rb/options.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def from(options = {}, state = {})
5151
show_indexes: true, # ModelAnnotator
5252
show_indexes_include: false, # ModelAnnotator
5353
show_virtual_columns: false, # ModelAnnotator
54+
load_columns_from_schema: false, # ModelAnnotator
5455
simple_indexes: false, # ModelAnnotator
5556
sort: false, # ModelAnnotator
5657
timestamp: false, # RouteAnnotator
@@ -119,6 +120,7 @@ def from(options = {}, state = {})
119120
:ignore_model_sub_dir,
120121
:ignore_unknown_models,
121122
:include_version,
123+
:load_columns_from_schema,
122124
:show_check_constraints,
123125
:show_complete_foreign_keys,
124126
:show_foreign_keys,

lib/annotate_rb/parser.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ def add_model_options_to_parser(option_parser)
274274
"Place annotations directly above nested classes or modules instead of at the top of the file.") do
275275
@options[:nested_position] = true
276276
end
277+
278+
option_parser.on("--load-columns-from-schema",
279+
"Load columns from schema to avoid ActiveRecord ignored/only column configuration") do
280+
@options[:load_columns_from_schema] = true
281+
end
277282
end
278283

279284
def add_route_options_to_parser(option_parser)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
22

33
class TestDefault < ApplicationRecord
4+
self.ignored_columns = ["ignored_column"]
45
end

spec/dummyapp/db/migrate/20230630123456_create_test_tables.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def change
1010
t.float :float, default: 12.34
1111
t.integer :integer, default: 99
1212
t.string :string, default: 'hello world!'
13+
t.string :ignored_column
1314

1415
t.timestamps
1516
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "integration_spec_helper"
4+
5+
RSpec.describe "Load columns from schema with ignored columns", type: "aruba" do
6+
let(:command_timeout_seconds) { 10 }
7+
let(:model_file) { "app/models/test_default.rb" }
8+
9+
before do
10+
copy_dummy_app_into_aruba_working_directory
11+
reset_database
12+
run_migrations
13+
end
14+
15+
it "does not include ignored columns by default" do
16+
run_command_and_stop("bundle exec annotaterb models #{model_file} --force", fail_on_error: true, exit_timeout: command_timeout_seconds)
17+
18+
annotated_content = read_file(model_file)
19+
20+
expect(annotated_content).to include("string")
21+
expect(annotated_content).not_to include("# ignored_column")
22+
end
23+
24+
it "includes ignored columns when --load-columns-from-schema is used" do
25+
run_command_and_stop("bundle exec annotaterb models #{model_file} --load-columns-from-schema --force", fail_on_error: true, exit_timeout: command_timeout_seconds)
26+
27+
annotated_content = read_file(model_file)
28+
29+
expect(annotated_content).to include("string")
30+
expect(annotated_content).to include("# ignored_column")
31+
end
32+
end

spec/lib/annotate_rb/options_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@
6767
end
6868
end
6969

70+
context 'when default value of "load_columns_from_schema" is not set' do
71+
let(:key) { :load_columns_from_schema }
72+
73+
it "returns false" do
74+
expect(subject[key]).to eq(false)
75+
end
76+
end
77+
7078
context 'when default value of "show_complete_foreign_keys" is set' do
7179
let(:key) { :show_complete_foreign_keys }
7280
let(:options) { {key => true} }

spec/lib/annotate_rb/parser_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ module AnnotateRb # rubocop:disable Metrics/ModuleLength
359359
end
360360
end
361361

362+
%w[--load-columns-from-schema].each do |option|
363+
describe option do
364+
let(:args) { [option] }
365+
366+
it "sets load_columns_from_schema to true" do
367+
expect(result).to include(load_columns_from_schema: true)
368+
end
369+
end
370+
end
371+
362372
%w[-R --require].each do |option|
363373
describe option do
364374
let(:option) { "require" }

spec/templates/mysql2/test_default.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
# updated_at :datetime not null
1717
#
1818
class TestDefault < ApplicationRecord
19+
self.ignored_columns = ["ignored_column"]
1920
end

0 commit comments

Comments
 (0)