Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Set storages.health_info default on the database level.

Pavel Balashou7021560282bd
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index 0e522f56e9b..e14722388a9 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -73,6 +73,13 @@ module Storages
where.not(id: project.project_storages.pluck(:storage_id))
end
+ before_save do |storage|
+ # there is a default value set on the database level
+ # but with store_attributes defined new instance have {} as a default value
+ # health_info is set to be nil before save then database will set the default.
+ storage.health_info = nil if health_info == {}
+ end
+
store_attribute :health_info, :status, :string, prefix: :health
store_attribute :health_info, :changed_at, :datetime, prefix: :health
store_attribute :health_info, :reason, :string, prefix: :health
@@ -97,12 +104,10 @@ module Storages
end
def healthy?
- mark_as_healthy if health_status.nil?
health_status == 'healthy'
end
def unhealthy?
- mark_as_healthy if health_status.nil?
health_status == 'unhealthy'
end
diff --git a/modules/storages/db/migrate/20231109080454_add_health_info_to_storages.rb b/modules/storages/db/migrate/20231109080454_add_health_info_to_storages.rb
index a52b02f107a..942f2c09115 100644
--- a/modules/storages/db/migrate/20231109080454_add_health_info_to_storages.rb
+++ b/modules/storages/db/migrate/20231109080454_add_health_info_to_storages.rb
@@ -27,6 +27,14 @@
class AddHealthInfoToStorages < ActiveRecord::Migration[7.0]
def change
- add_column :storages, :health_info, :jsonb, null: false, default: {}
+ add_column(
+ :storages,
+ :health_info,
+ :jsonb,
+ null: false,
+ default: -> do
+ "jsonb_build_object('status', 'unhealthy', 'changed_at', now(), 'reason', 'Not configured')"
+ end
+ )
end
end
diff --git a/modules/storages/spec/models/storages/nextcloud_storage_spec.rb b/modules/storages/spec/models/storages/nextcloud_storage_spec.rb
index fb9cf6a3eb7..5a58b389c99 100644
--- a/modules/storages/spec/models/storages/nextcloud_storage_spec.rb
+++ b/modules/storages/spec/models/storages/nextcloud_storage_spec.rb
@@ -42,71 +42,34 @@ RSpec.describe Storages::NextcloudStorage do
it { expect(storage).not_to be_a_provider_type_one_drive }
end
- describe '#mark_as_unhealthy' do
- it 'fills health atrributes in' do
- Timecop.freeze('2023-03-14T15:17:00Z') do
+ describe 'health' do
+ it 'can be marked as healthy or unhealthy' do
+ # reload is here because health_info default value is set by the database
+ # and not returned with default save/create AR method
+ storage.reload
+ healthy_time = Time.parse '2021-03-14T15:17:00Z'
+ unhealthy_time = Time.parse '2023-03-14T15:17:00Z'
+
+ Timecop.freeze(healthy_time) do
expect do
- storage.mark_as_unhealthy(reason: 'thou_shall_not_pass_error')
- end.to(
- change(storage, :health_changed_at).from(nil).to(Time.now.utc)
- .and(change(storage, :health_status).from(nil).to('unhealthy'))
- .and(change(storage, :health_reason).from(nil).to('thou_shall_not_pass_error'))
- )
+ storage.mark_as_healthy
+ end.to(change(storage, :health_changed_at).to(healthy_time)
+ .and(change(storage, :health_status).from('unhealthy').to('healthy'))
+ .and(change(storage, :health_reason).from('Not configured').to(nil)))
+ expect(storage.healthy?).to eq(true)
+ expect(storage.unhealthy?).to eq(false)
end
- end
- end
- describe '#mark_as_healthy' do
- before do
- Timecop.freeze('2021-03-14T15:17:00Z') do
- storage.mark_as_unhealthy(reason: 'just nope')
- end
- end
- it 'fills health atrributes in' do
- Timecop.freeze('2023-03-14T15:17:00Z') do
+ Timecop.freeze(unhealthy_time) do
expect do
- storage.mark_as_healthy
- end.to(
- change(storage, :health_changed_at).from(Time.parse('2021-03-14T15:17:00Z')).to(Time.now.utc)
- .and(change(storage, :health_status).from('unhealthy').to('healthy'))
- .and(change(storage, :health_reason).from('just nope').to(nil))
- )
+ storage.mark_as_unhealthy(reason: 'thou_shall_not_pass_error')
+ end.to(change(storage, :health_changed_at).from(healthy_time).to(unhealthy_time)
+ .and(change(storage, :health_status).from('healthy').to('unhealthy'))
+ .and(change(storage, :health_reason).from(nil).to('thou_shall_not_pass_error')))
end
- end
- end
-
- describe '#healthy?' do
- it 'marks as healthy if health_status is nil' do
- expect do
- storage.healthy?
- end.to(change(storage, :health_status).from(nil).to('healthy'))
- expect(storage).to be_healthy
- end
-
- it 'does not mark as healthy if health_status has been defined earlier' do
- storage.mark_as_unhealthy
- expect do
- storage.healthy?
- end.not_to(change(storage, :health_status).from('unhealthy'))
- expect(storage).not_to be_healthy
- end
- end
-
- describe '#unhealthy?' do
- it 'marks as healthy if health_status is nil' do
- expect do
- storage.unhealthy?
- end.to(change(storage, :health_status).from(nil).to('healthy'))
- expect(storage).to be_healthy
- end
-
- it 'does not mark as healthy if health_status has been defined earlier' do
- storage.mark_as_healthy
- expect do
- storage.unhealthy?
- end.not_to(change(storage, :health_status).from('healthy'))
- expect(storage).not_to be_unhealthy
+ expect(storage.healthy?).to eq(false)
+ expect(storage.unhealthy?).to eq(true)
end
end