Commit 38fd773b authored by Sean McGivern's avatar Sean McGivern

Fix ShaAttribute concern when there is no table

When this is added to a new model, it would fail before the migrations were run
- including when trying to run migrations in production mode!
parent de2d5ce6
No related merge requests found
......@@ -3,6 +3,8 @@ module ShaAttribute
module ClassMethods
def sha_attribute(name)
return unless table_exists?
column = columns.find { |c| c.name == name.to_s }
# In case the table doesn't exist we won't be able to find the column,
......
......@@ -13,15 +13,34 @@ describe ShaAttribute do
end
describe '#sha_attribute' do
it 'defines a SHA attribute for a binary column' do
expect(model).to receive(:attribute)
.with(:sha1, an_instance_of(Gitlab::Database::ShaAttribute))
context' when the table exists' do
before do
allow(model).to receive(:table_exists?).and_return(true)
end
model.sha_attribute(:sha1)
it 'defines a SHA attribute for a binary column' do
expect(model).to receive(:attribute)
.with(:sha1, an_instance_of(Gitlab::Database::ShaAttribute))
model.sha_attribute(:sha1)
end
it 'raises ArgumentError when the column type is not :binary' do
expect { model.sha_attribute(:name) }.to raise_error(ArgumentError)
end
end
it 'raises ArgumentError when the column type is not :binary' do
expect { model.sha_attribute(:name) }.to raise_error(ArgumentError)
context' when the table does not exist' do
before do
allow(model).to receive(:table_exists?).and_return(false)
end
it 'does nothing' do
expect(model).not_to receive(:columns)
expect(model).not_to receive(:attribute)
model.sha_attribute(:name)
end
end
end
end
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