Skip to content

Commit

Permalink
Define custom migration classes for Project and Bim::IfcModels::IfcMo…
Browse files Browse the repository at this point in the history
…del to avoid non existing enum type declaration errors.
  • Loading branch information
dombesz committed Dec 21, 2023
1 parent 9dc5db8 commit 546d98f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
1 change: 0 additions & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ class Project < ApplicationRecord

friendly_id :identifier, use: :finders

include ::Scopes::Scoped
scopes :allowed_to,
:visible

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20200220171133_rename_bim_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20210510193438_remove_project_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20220831073113_work_package_project_foreign_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions modules/bim/app/models/bim/ifc_models/ifc_model.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
13 changes: 11 additions & 2 deletions modules/bim/db/migrate/20210521080035_update_xkt_to_version8.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 546d98f

Please sign in to comment.