Class: ProjectUnification::CachedRebuilder
- Inherits:
-
Object
- Object
- ProjectUnification::CachedRebuilder
- Defined in:
- lib/project_unification/cached_rebuilder.rb
Constant Summary collapse
- MODELS_WITH_CACHED_FIELDS =
Models with cached_ columns that DO NOT need rebuilding after unification, and why:
AnatomicalPart cached_otu_id: OTU primary keys are unchanged by the bulk project_id update; moved TaxonNameRelationships form a self-contained subtree so current_otu resolves identically post-unification. cached: derives from name/uri_label, project-independent. CollectingEvent cached_level*_geographic_name: derives from geographic_area_id, which is unchanged. Descriptor cached_gene_attribute_sql: derives from gene attribute structure, not project context. GeographicItem cached_total_area: not project-scoped. Identifier cached_numeric_identifier: derives from the identifier string value, project-independent. Observation cached_column_label, cached_row_label: derive from observation matrix column/row labels, which are stable strings unchanged by migration. ObservationMatrixColumn cached_observation_matrix_column_item_id: stable reference whose primary key is unchanged. ObservationMatrixRow cached_observation_matrix_row_item_id: same. SledImage cached_total_*: counts of associated CollectionObjects, which moved intact with their sled_image_id FK. Source cached_author_string, cached_nomenclature_date: community data, not project-scoped.TODO: the only model we're rebuilding cached_* values on is TaxonName, and it probably isn't needed there either. Confirm.
%w[ TaxonName ].freeze
Instance Attribute Summary collapse
-
#project_id ⇒ Object
readonly
Returns the value of attribute project_id.
Instance Method Summary collapse
-
#initialize(project_id) ⇒ CachedRebuilder
constructor
A new instance of CachedRebuilder.
-
#rebuild_all ⇒ Hash
Rebuild all cached fields for models in the project.
- #rebuild_model(klass) ⇒ Object private
Constructor Details
#initialize(project_id) ⇒ CachedRebuilder
Returns a new instance of CachedRebuilder.
52 53 54 |
# File 'lib/project_unification/cached_rebuilder.rb', line 52 def initialize(project_id) @project_id = project_id end |
Instance Attribute Details
#project_id ⇒ Object (readonly)
Returns the value of attribute project_id.
8 9 10 |
# File 'lib/project_unification/cached_rebuilder.rb', line 8 def project_id @project_id end |
Instance Method Details
#rebuild_all ⇒ Hash
Rebuild all cached fields for models in the project
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/project_unification/cached_rebuilder.rb', line 58 def rebuild_all stats = { models_rebuilt: 0, records_updated: 0, errors: [] } MODELS_WITH_CACHED_FIELDS.each do |model_name| klass = model_name.safe_constantize next unless klass begin count = rebuild_model(klass) stats[:models_rebuilt] += 1 stats[:records_updated] += count rescue => e stats[:errors] << { model: model_name, error: e. } end end stats end |
#rebuild_model(klass) ⇒ Object (private)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/project_unification/cached_rebuilder.rb', line 86 def rebuild_model(klass) return 0 unless klass.column_names.any? { |c| c.start_with?('cached_') } return 0 unless klass.column_names.include?('project_id') count = 0 klass.where(project_id: project_id).find_in_batches(batch_size: 100) do |batch| batch.each do |record| if record.respond_to?(:set_cached) begin record.set_cached count += 1 rescue => e Rails.logger.error("Failed to rebuild cached for #{klass.name} #{record.id}: #{e.}") end end end end count end |