From 546d98f5480a2bb4a28f11883d357250a432d102 Mon Sep 17 00:00:00 2001 From: Dombi Attila <83396+dombesz@users.noreply.github.com> Date: Thu, 21 Dec 2023 10:57:20 +0200 Subject: [PATCH] Define custom migration classes for Project and Bim::IfcModels::IfcModel to avoid non existing enum type declaration errors. --- app/models/project.rb | 1 - db/migrate/20200220171133_rename_bim_module.rb | 5 +++++ db/migrate/20210510193438_remove_project_setting.rb | 5 +++++ ...220831073113_work_package_project_foreign_key.rb | 5 +++++ modules/bim/app/models/bim/ifc_models/ifc_model.rb | 7 +++++++ .../20210521080035_update_xkt_to_version8.rb | 13 +++++++++++-- ...301_add_column_conversion_status_to_ifc_model.rb | 13 +++++++++++-- 7 files changed, 44 insertions(+), 5 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index 63a804ab551f..5d36fd09b13f 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -149,7 +149,6 @@ class Project < ApplicationRecord friendly_id :identifier, use: :finders - include ::Scopes::Scoped scopes :allowed_to, :visible diff --git a/db/migrate/20200220171133_rename_bim_module.rb b/db/migrate/20200220171133_rename_bim_module.rb index 88507a3b6be6..2aa6d2134cf5 100644 --- a/db/migrate/20200220171133_rename_bim_module.rb +++ b/db/migrate/20200220171133_rename_bim_module.rb @@ -27,6 +27,11 @@ #++ class RenameBimModule < ActiveRecord::Migration[6.0] + # Note: rails 7.1 breaks the class' ancestor chain, when a class with an enum definition + # without a database field is being referenced. + # Re-defining the Project class without the enum to avoid the issue. + class Project < ApplicationRecord; end + def up projects_with_bcf = EnabledModule.where(name: 'bcf').pluck(:project_id) # Delete all bcf to avoid duplicates diff --git a/db/migrate/20210510193438_remove_project_setting.rb b/db/migrate/20210510193438_remove_project_setting.rb index 480ff1aed628..d3ac6b580429 100644 --- a/db/migrate/20210510193438_remove_project_setting.rb +++ b/db/migrate/20210510193438_remove_project_setting.rb @@ -27,6 +27,11 @@ #++ class RemoveProjectSetting < ActiveRecord::Migration[6.1] + # Note: rails 7.1 breaks the class' ancestor chain, and raises an error, when a class + # with an enum definition without a database field is being referenced. + # Re-defining the Project class without the enum to avoid the issue. + class Project < ApplicationRecord; end + def up Project.where(name: 'sequential_project_identifiers').delete_all end diff --git a/db/migrate/20220831073113_work_package_project_foreign_key.rb b/db/migrate/20220831073113_work_package_project_foreign_key.rb index e0daf22e0bb1..a98f7c0e2181 100644 --- a/db/migrate/20220831073113_work_package_project_foreign_key.rb +++ b/db/migrate/20220831073113_work_package_project_foreign_key.rb @@ -27,6 +27,11 @@ #++ class WorkPackageProjectForeignKey < ActiveRecord::Migration[7.0] + # Note: rails 7.1 breaks the class' ancestor chain, and raises an error, when a class + # with an enum definition without a database field is being referenced. + # Re-defining the Project class without the enum to avoid the issue. + class Project < ApplicationRecord; end + def change reversible do |dir| dir.up do diff --git a/modules/bim/app/models/bim/ifc_models/ifc_model.rb b/modules/bim/app/models/bim/ifc_models/ifc_model.rb index c72073e8c27c..7d3a62d6bb4e 100644 --- a/modules/bim/app/models/bim/ifc_models/ifc_model.rb +++ b/modules/bim/app/models/bim/ifc_models/ifc_model.rb @@ -1,6 +1,13 @@ module Bim module IfcModels class IfcModel < ApplicationRecord + # Note: rails 7.1 breaks the class' ancestor chain, if it fails to infer the enum attribute's + # type. We reference the Project class in migrations prior to the `conversion_status` column being added + # to the database, which leads to rails failing to infer the enum's type. + # The `conversion_status`'s type needs to be declared so rails will do the correct type inference and + # not break the ancestor chain. Once this is fixed in rails, we can remove it. + attribute :conversion_status, :integer + enum conversion_status: { pending: 0, processing: 1, diff --git a/modules/bim/db/migrate/20210521080035_update_xkt_to_version8.rb b/modules/bim/db/migrate/20210521080035_update_xkt_to_version8.rb index 57fa697fd43f..c34f6dc39b96 100644 --- a/modules/bim/db/migrate/20210521080035_update_xkt_to_version8.rb +++ b/modules/bim/db/migrate/20210521080035_update_xkt_to_version8.rb @@ -31,11 +31,20 @@ class UpdateXktToVersion8 < ActiveRecord::Migration[6.1] 'Hospital - Structural (cc-by-sa-3.0 Autodesk Inc.)' => 'hospital_structural', 'Hospital - Mechanical (cc-by-sa-3.0 Autodesk Inc.)' => 'hospital_mechanical' }.freeze + # Note: rails 7.1 breaks the class' ancestor chain, and raises an error, when a class + # with an enum definition without a database field is being referenced. + # Re-defining the Project class without the enum to avoid the issue. + class IfcModel < ApplicationRecord + has_many :attachments, -> { where(container_type: 'Bim::IfcModels::IfcModel') }, + foreign_key: :container_id, + class_name: 'Attachment' + end + def up # Queue every IFC model for a new transformation. Rails.logger.info "Migrate all IFC models to the latest XKT version" - if Bim::IfcModels::IfcModel.count.zero? + if IfcModel.count.zero? Rails.logger.info("No IFC models to migrate") return end @@ -59,7 +68,7 @@ def down private def migrate_all_ifc_models - ::Bim::IfcModels::IfcModel.find_each do |ifc_model| + IfcModel.find_each do |ifc_model| cleanup_metadata_attachment(ifc_model) # We have seeded models that do not have an IFC attachment. They cannot get converted as an IFC file is # necessary. diff --git a/modules/bim/db/migrate/20211011204301_add_column_conversion_status_to_ifc_model.rb b/modules/bim/db/migrate/20211011204301_add_column_conversion_status_to_ifc_model.rb index cf3037a1d3ca..0ccea4018e78 100644 --- a/modules/bim/db/migrate/20211011204301_add_column_conversion_status_to_ifc_model.rb +++ b/modules/bim/db/migrate/20211011204301_add_column_conversion_status_to_ifc_model.rb @@ -27,15 +27,24 @@ #++ class AddColumnConversionStatusToIfcModel < ActiveRecord::Migration[6.1] + # Note: rails 7.1 breaks the class' ancestor chain, and raises an error, when a class + # with an enum definition without a database field is being referenced. + # Re-defining the Project class without the enum to avoid the issue. + class IfcModel < ApplicationRecord + has_many :attachments, -> { where(container_type: 'Bim::IfcModels::IfcModel') }, + foreign_key: :container_id, + class_name: 'Attachment' + end + def up add_column(:ifc_models, :conversion_status, :integer, default: 0) # default "pending" add_column(:ifc_models, :conversion_error_message, :text) - converted_models = ::Bim::IfcModels::IfcModel + converted_models = IfcModel .joins(:attachments) .where("attachments.description = 'xkt'") - not_converted_models = ::Bim::IfcModels::IfcModel + not_converted_models = IfcModel .where .not(id: converted_models)