From 509dd0eb5fb4fddd5692f86663c7edb6737569de Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 22 Nov 2022 14:17:25 +0100 Subject: [PATCH] (feat) soft destroy machines/spaces --- CHANGELOG.md | 1 + app/controllers/api/machines_controller.rb | 8 +++++++- app/controllers/api/spaces_controller.rb | 10 ++++++++-- app/models/machine.rb | 4 ++++ app/models/space.rb | 4 ++++ app/policies/machine_policy.rb | 2 +- app/policies/space_policy.rb | 5 ++++- app/services/machine_service.rb | 3 +++ .../20221122123557_add_deleted_at_to_machine.rb | 12 ++++++++++++ db/migrate/20221122123605_add_deleted_at_to_space.rb | 12 ++++++++++++ db/schema.rb | 6 +++++- 11 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20221122123557_add_deleted_at_to_machine.rb create mode 100644 db/migrate/20221122123605_add_deleted_at_to_space.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index cd1f52d77..f06621ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Changelog Fab-manager +- Soft destroy of spaces and machines - Fix a bug: in upgrade script, the error "the input device is not a TTY" is thrown when migrating the database - Fix a bug: broken display of machines pages - Fix a bug: some automated tests were randomly failing because ElasticSearch was not synced diff --git a/app/controllers/api/machines_controller.rb b/app/controllers/api/machines_controller.rb index a6baebc10..1879bd183 100644 --- a/app/controllers/api/machines_controller.rb +++ b/app/controllers/api/machines_controller.rb @@ -12,6 +12,8 @@ class API::MachinesController < API::ApiController def show @machine = Machine.includes(:machine_files, :projects).friendly.find(params[:id]) + + head :not_found if @machine.deleted_at end def create @@ -35,7 +37,11 @@ class API::MachinesController < API::ApiController def destroy authorize @machine - @machine.destroy + if @machine.destroyable? + @machine.destroy + else + @machine.soft_destroy! + end head :no_content end diff --git a/app/controllers/api/spaces_controller.rb b/app/controllers/api/spaces_controller.rb index 18a02cac5..75390c85d 100644 --- a/app/controllers/api/spaces_controller.rb +++ b/app/controllers/api/spaces_controller.rb @@ -6,11 +6,13 @@ class API::SpacesController < API::ApiController respond_to :json def index - @spaces = Space.includes(:space_image) + @spaces = Space.includes(:space_image).where(deleted_at: nil) end def show @space = Space.includes(:space_files, :projects).friendly.find(params[:id]) + + head :not_found if @space.deleted_at end def create @@ -36,7 +38,11 @@ class API::SpacesController < API::ApiController def destroy @space = get_space authorize @space - @space.destroy + if @space.destroyable? + @space.destroy + else + @space.soft_destroy! + end head :no_content end diff --git a/app/models/machine.rb b/app/models/machine.rb index 661f37256..0abd5e1c3 100644 --- a/app/models/machine.rb +++ b/app/models/machine.rb @@ -82,6 +82,10 @@ class Machine < ApplicationRecord reservations.empty? end + def soft_destroy! + update(deleted_at: DateTime.current) + end + def packs?(user) prepaid_packs.where(group_id: user.group_id) .where(disabled: [false, nil]) diff --git a/app/models/space.rb b/app/models/space.rb index 228c5fd5f..934b51754 100644 --- a/app/models/space.rb +++ b/app/models/space.rb @@ -66,6 +66,10 @@ class Space < ApplicationRecord reservations.empty? end + def soft_destroy! + update(deleted_at: DateTime.current) + end + private def update_gateway_product diff --git a/app/policies/machine_policy.rb b/app/policies/machine_policy.rb index 069da4f45..5a1ac8481 100644 --- a/app/policies/machine_policy.rb +++ b/app/policies/machine_policy.rb @@ -11,6 +11,6 @@ class MachinePolicy < ApplicationPolicy end def destroy? - user.admin? and record.destroyable? + user.admin? end end diff --git a/app/policies/space_policy.rb b/app/policies/space_policy.rb index d4174200d..6c69c0faa 100644 --- a/app/policies/space_policy.rb +++ b/app/policies/space_policy.rb @@ -1,3 +1,6 @@ +# frozen_string_literal: true + +# Check the access policies for API::SpacesController class SpacePolicy < ApplicationPolicy def create? user.admin? @@ -8,6 +11,6 @@ class SpacePolicy < ApplicationPolicy end def destroy? - user.admin? and record.destroyable? + user.admin? end end diff --git a/app/services/machine_service.rb b/app/services/machine_service.rb index 02f18c4f1..bc5720c8d 100644 --- a/app/services/machine_service.rb +++ b/app/services/machine_service.rb @@ -9,6 +9,9 @@ class MachineService else Machine.includes(:machine_image, :plans).order(sort_by) end + # do not include soft destroyed + machines = machines.where(deleted_at: nil) + if filters[:disabled].present? state = filters[:disabled] == 'false' ? [nil, false] : true machines = machines.where(disabled: state) diff --git a/db/migrate/20221122123557_add_deleted_at_to_machine.rb b/db/migrate/20221122123557_add_deleted_at_to_machine.rb new file mode 100644 index 000000000..23107b653 --- /dev/null +++ b/db/migrate/20221122123557_add_deleted_at_to_machine.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# Allow soft destroy of machines. +# Machines with existing reservation cannot be destroyed because we need them for rebuilding invoices, statistics, etc. +# This attribute allows to make a "soft destroy" of a Machine, marking it as destroyed so it doesn't appear anymore in +# the interface (as if it was destroyed) but still lives in the database so we can use it to build data. +class AddDeletedAtToMachine < ActiveRecord::Migration[5.2] + def change + add_column :machines, :deleted_at, :datetime + add_index :machines, :deleted_at + end +end diff --git a/db/migrate/20221122123605_add_deleted_at_to_space.rb b/db/migrate/20221122123605_add_deleted_at_to_space.rb new file mode 100644 index 000000000..f5f13b0aa --- /dev/null +++ b/db/migrate/20221122123605_add_deleted_at_to_space.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# Allow soft destroy of spaces. +# Spaces with existing reservation cannot be destroyed because we need them for rebuilding invoices, statistics, etc. +# This attribute allows to make a "soft destroy" of a Space, marking it as destroyed so it doesn't appear anymore in +# the interface (as if it was destroyed) but still lives in the database so we can use it to build data. +class AddDeletedAtToSpace < ActiveRecord::Migration[5.2] + def change + add_column :spaces, :deleted_at, :datetime + add_index :spaces, :deleted_at + end +end diff --git a/db/schema.rb b/db/schema.rb index 9e6a688de..effaba6fc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_10_03_133019) do +ActiveRecord::Schema.define(version: 2022_11_22_123605) do # These are extensions that must be enabled in order to support this database enable_extension "fuzzystrmatch" @@ -358,6 +358,8 @@ ActiveRecord::Schema.define(version: 2022_10_03_133019) do t.datetime "updated_at" t.string "slug" t.boolean "disabled" + t.datetime "deleted_at" + t.index ["deleted_at"], name: "index_machines_on_deleted_at" t.index ["slug"], name: "index_machines_on_slug", unique: true end @@ -878,6 +880,8 @@ ActiveRecord::Schema.define(version: 2022_10_03_133019) do t.datetime "updated_at", null: false t.text "characteristics" t.boolean "disabled" + t.datetime "deleted_at" + t.index ["deleted_at"], name: "index_spaces_on_deleted_at" end create_table "spaces_availabilities", id: :serial, force: :cascade do |t|