Class: Analytics::CycleAnalytics::StageEventHash
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Analytics::CycleAnalytics::StageEventHash
- Defined in:
- app/models/analytics/cycle_analytics/stage_event_hash.rb
Constant Summary
Constants inherited from ApplicationRecord
Constants included from HasCheckConstraints
HasCheckConstraints::NOT_NULL_CHECK_PATTERN
Constants included from ResetOnColumnErrors
ResetOnColumnErrors::MAX_RESET_PERIOD
Class Method Summary collapse
- .cleanup_if_unused(id) ⇒ Object
-
.record_id_by_hash_sha256(organization_id, hash) ⇒ Object
Creates or queries the id of the corresponding stage event hash code.
- .unused_hashes_for(id) ⇒ Object
Methods inherited from ApplicationRecord
===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order
Methods included from Organizations::Sharding
Methods included from ResetOnColumnErrors
#reset_on_union_error, #reset_on_unknown_attribute_error
Methods included from Gitlab::SensitiveSerializableHash
Class Method Details
.cleanup_if_unused(id) ⇒ Object
38 39 40 41 42 |
# File 'app/models/analytics/cycle_analytics/stage_event_hash.rb', line 38 def self.cleanup_if_unused(id) unused_hashes_for(id) .where(id: id) .delete_all end |
.record_id_by_hash_sha256(organization_id, hash) ⇒ Object
Creates or queries the id of the corresponding stage event hash code
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/analytics/cycle_analytics/stage_event_hash.rb', line 11 def self.record_id_by_hash_sha256(organization_id, hash) hash_record = find_by(organization_id: organization_id, hash_sha256: hash) return hash_record.id if hash_record casted_organization_id = Arel::Nodes .build_quoted(organization_id, arel_table[:organization_id]) .to_sql casted_hash_code = Arel::Nodes .build_quoted(hash, arel_table[:hash_sha256]) .to_sql # Atomic, safe insert without retrying query = <<~SQL WITH insert_cte AS MATERIALIZED ( INSERT INTO #{quoted_table_name} (organization_id, hash_sha256) VALUES (#{casted_organization_id}, #{casted_hash_code}) ON CONFLICT DO NOTHING RETURNING ID ) SELECT ids.id FROM ( (SELECT id FROM #{quoted_table_name} WHERE organization_id=#{casted_organization_id} AND hash_sha256=#{casted_hash_code} LIMIT 1) UNION ALL (SELECT id FROM insert_cte LIMIT 1) ) AS ids LIMIT 1 SQL connection.execute(query).first['id'] end |
.unused_hashes_for(id) ⇒ Object
44 45 46 47 48 |
# File 'app/models/analytics/cycle_analytics/stage_event_hash.rb', line 44 def self.unused_hashes_for(id) stage_exists_query = ::Analytics::CycleAnalytics::Stage.where(stage_event_hash_id: id).select('1').limit(1) where.not('EXISTS (?)', stage_exists_query) end |