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

Remove jsonb health_info and use usual columns.

Pavel Balashouc169a09aa22d
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index e14722388a9..f7809c71402 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -73,16 +73,11 @@ 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
+ enum health_status: {
+ unconfigured: 'unconfigured',
+ healthy: 'healthy',
+ unhealthy: 'unhealthy'
+ }.freeze
def self.shorten_provider_type(provider_type)
case /Storages::(?'provider_name'.*)Storage/.match(provider_type)
@@ -103,14 +98,6 @@ module Storages
update(health_status: 'healthy', health_changed_at: Time.now.utc, health_reason: nil)
end
- def healthy?
- health_status == 'healthy'
- end
-
- def unhealthy?
- health_status == 'unhealthy'
- end
-
def configured?
configuration_checks.values.all?
end
diff --git a/modules/storages/app/workers/storages/manage_nextcloud_integration_job_mixin.rb b/modules/storages/app/workers/storages/manage_nextcloud_integration_job_mixin.rb
index 01e8fd4025a..1c83b77a760 100644
--- a/modules/storages/app/workers/storages/manage_nextcloud_integration_job_mixin.rb
+++ b/modules/storages/app/workers/storages/manage_nextcloud_integration_job_mixin.rb
@@ -43,10 +43,12 @@ module Storages
result = GroupFolderPropertiesSyncService.call(storage)
result.match(
on_success: ->(_) do
- storage.mark_as_healthy
+ storage.mark_as_healthy unless storage.healthy?
end,
on_failure: ->(errors) do
- storage.mark_as_unhealthy(reason: errors.to_s)
+ if !storage.unhealthy? || storage.health_reason != errors.to_s
+ storage.mark_as_unhealthy(reason: errors.to_s)
+ end
end
)
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 942f2c09115..b35ac45f195 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
@@ -13,7 +13,7 @@
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
-# This program is distributed in the hope that it will be useful,
+# This program is distributed in the hope that itwill be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
@@ -26,15 +26,22 @@
#++
class AddHealthInfoToStorages < ActiveRecord::Migration[7.0]
- def change
- add_column(
- :storages,
- :health_info,
- :jsonb,
- null: false,
- default: -> do
- "jsonb_build_object('status', 'unhealthy', 'changed_at', now(), 'reason', 'Not configured')"
- end
- )
+ def up
+ execute <<-SQL.squish
+ CREATE TYPE storage_health_statuses AS ENUM ('unconfigured', 'healthy', 'unhealthy');
+ SQL
+ add_column(:storages, :health_status, :storage_health_statuses, null: false, default: 'unconfigured')
+ add_column(:storages, :health_changed_at, :datetime, null: false, default: -> { 'current_timestamp' })
+ add_column(:storages, :health_reason, :string)
+ end
+
+ def down
+ remove_column(:storages, :health_status)
+ remove_column(:storages, :health_changed_at)
+ remove_column(:storages, :health_reason)
+
+ execute <<-SQL.squish
+ DROP TYPE storage_health_statuses;
+ SQL
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 5a58b389c99..46e9dcc6154 100644
--- a/modules/storages/spec/models/storages/nextcloud_storage_spec.rb
+++ b/modules/storages/spec/models/storages/nextcloud_storage_spec.rb
@@ -42,11 +42,8 @@ RSpec.describe Storages::NextcloudStorage do
it { expect(storage).not_to be_a_provider_type_one_drive }
end
- describe 'health' do
+ describe 'health attributes' 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'
@@ -54,13 +51,11 @@ RSpec.describe Storages::NextcloudStorage do
expect do
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)
+ .and(change(storage, :health_status).from('unconfigured').to('healthy')))
+ expect(storage.healthy?).to be(true)
+ expect(storage.unhealthy?).to be(false)
end
-
Timecop.freeze(unhealthy_time) do
expect do
storage.mark_as_unhealthy(reason: 'thou_shall_not_pass_error')
@@ -68,8 +63,8 @@ RSpec.describe Storages::NextcloudStorage do
.and(change(storage, :health_status).from('healthy').to('unhealthy'))
.and(change(storage, :health_reason).from(nil).to('thou_shall_not_pass_error')))
end
- expect(storage.healthy?).to eq(false)
- expect(storage.unhealthy?).to eq(true)
+ expect(storage.healthy?).to be(false)
+ expect(storage.unhealthy?).to be(true)
end
end
diff --git a/modules/storages/spec/workers/storages/manage_nextcloud_integration_cron_job_spec.rb b/modules/storages/spec/workers/storages/manage_nextcloud_integration_cron_job_spec.rb
index 5b3fdf686d9..fd235b43014 100644
--- a/modules/storages/spec/workers/storages/manage_nextcloud_integration_cron_job_spec.rb
+++ b/modules/storages/spec/workers/storages/manage_nextcloud_integration_cron_job_spec.rb
@@ -68,8 +68,8 @@ RSpec.describe Storages::ManageNextcloudIntegrationCronJob, :webmock, type: :job
subject
storage1.reload
end.to(
- change(storage1, :health_changed_at).from(nil).to(Time.now.utc)
- .and(change(storage1, :health_status).from(nil).to('healthy'))
+ change(storage1, :health_changed_at).to(Time.now.utc)
+ .and(change(storage1, :health_status).from('unconfigured').to('healthy'))
)
end
end
@@ -85,8 +85,8 @@ RSpec.describe Storages::ManageNextcloudIntegrationCronJob, :webmock, type: :job
subject
storage1.reload
end.to(
- change(storage1, :health_changed_at).from(nil).to(Time.now.utc)
- .and(change(storage1, :health_status).from(nil).to('unhealthy'))
+ change(storage1, :health_changed_at).to(Time.now.utc)
+ .and(change(storage1, :health_status).from('unconfigured').to('unhealthy'))
.and(change(storage1, :health_reason).from(nil).to('not_found'))
)
end