From 95327bd25bcf0055b0f25757453b15ed0a98647a Mon Sep 17 00:00:00 2001 From: Ignacio chiazzo cardarello Date: Tue, 3 Sep 2024 23:05:29 -0300 Subject: [PATCH 1/2] Allow running annotate for when the columns are frozen --- lib/annotate/annotate_models.rb | 2 ++ spec/lib/tasks/annotate_models_spec.rb | 27 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index dc2901a3..d87c639d 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -940,6 +940,8 @@ def get_attributes(column, column_type, klass, options) attrs << 'not null' unless column.null attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(column.name.to_sym) : column.name.to_sym == klass.primary_key.to_sym) + column_type = column_type.dup if column_type.frozen? + if column_type == 'decimal' column_type << "(#{column.precision}, #{column.scale})" elsif !%w[spatial geometry geography].include?(column_type) diff --git a/spec/lib/tasks/annotate_models_spec.rb b/spec/lib/tasks/annotate_models_spec.rb index 03f82391..8b705223 100644 --- a/spec/lib/tasks/annotate_models_spec.rb +++ b/spec/lib/tasks/annotate_models_spec.rb @@ -33,4 +33,31 @@ it { is_expected.to be_truthy } end end + + context 'when column_type is frozen' do + let(:klass) do + Class.new(ActiveRecord::Base) do + self.table_name = 'users' + end + end + + before do + allow(klass).to receive(:columns).and_return([ + double('Column', name: 'id', type: 'integer'.freeze, sql_type: 'integer', limit: nil, null: false, default: nil, comment: nil) + ]) + allow(klass).to receive(:table_exists?).and_return(true) + allow(klass).to receive(:primary_key).and_return('id') + end + + it 'does not raise an error when modifying column_type' do + expect { + AnnotateModels.get_schema_info(klass, "Schema Info", {}) + }.not_to raise_error + end + + it 'includes the column information in the schema info' do + schema_info = AnnotateModels.get_schema_info(klass, "Schema Info", {}) + expect(schema_info).to include('id :integer') + end + end end From 7ae754151f4349b7809a4644c29bd85e956a9f20 Mon Sep 17 00:00:00 2001 From: Ignacio chiazzo cardarello Date: Tue, 3 Sep 2024 23:37:51 -0300 Subject: [PATCH 2/2] add tests proving that frozen columns work and undo checking for frozen records --- lib/annotate/annotate_models.rb | 2 -- spec/lib/tasks/annotate_models_spec.rb | 8 +++----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index d87c639d..dc2901a3 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -940,8 +940,6 @@ def get_attributes(column, column_type, klass, options) attrs << 'not null' unless column.null attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(column.name.to_sym) : column.name.to_sym == klass.primary_key.to_sym) - column_type = column_type.dup if column_type.frozen? - if column_type == 'decimal' column_type << "(#{column.precision}, #{column.scale})" elsif !%w[spatial geometry geography].include?(column_type) diff --git a/spec/lib/tasks/annotate_models_spec.rb b/spec/lib/tasks/annotate_models_spec.rb index 8b705223..2c6bceda 100644 --- a/spec/lib/tasks/annotate_models_spec.rb +++ b/spec/lib/tasks/annotate_models_spec.rb @@ -43,20 +43,18 @@ before do allow(klass).to receive(:columns).and_return([ - double('Column', name: 'id', type: 'integer'.freeze, sql_type: 'integer', limit: nil, null: false, default: nil, comment: nil) + instance_double('Column', name: 'id', type: 'integer'.freeze, sql_type: 'integer', limit: nil, null: false, default: nil, comment: nil) ]) allow(klass).to receive(:table_exists?).and_return(true) allow(klass).to receive(:primary_key).and_return('id') end it 'does not raise an error when modifying column_type' do - expect { - AnnotateModels.get_schema_info(klass, "Schema Info", {}) - }.not_to raise_error + expect { AnnotateModels.get_schema_info(klass, 'Schema Info', {}) }.not_to raise_error end it 'includes the column information in the schema info' do - schema_info = AnnotateModels.get_schema_info(klass, "Schema Info", {}) + schema_info = AnnotateModels.get_schema_info(klass, 'Schema Info', {}) expect(schema_info).to include('id :integer') end end