Class: Dbwatcher::Storage::TableStorage
- Inherits:
-
BaseStorage
- Object
- BaseStorage
- Dbwatcher::Storage::TableStorage
- Includes:
- Concerns::Validatable
- Defined in:
- lib/dbwatcher/storage/table_storage.rb
Overview
Handles retrieval and processing of table change data
This class provides access to database table changes by coordinating with the change processor to aggregate and filter table modifications from stored session data. Follows Ruby style guide patterns for storage class organization.
Constant Summary collapse
- DEFAULT_CHANGE_LIMIT =
Configuration constants
100
- SUPPORTED_OPERATIONS =
%w[INSERT UPDATE DELETE].freeze
Constants inherited from BaseStorage
BaseStorage::DEFAULT_PERMISSIONS, BaseStorage::JSON_FILE_EXTENSION
Instance Attribute Summary collapse
-
#change_processor ⇒ ChangeProcessor
readonly
Processor for handling table changes.
-
#session_storage ⇒ SessionStorage
readonly
Session storage dependency.
Attributes inherited from BaseStorage
Instance Method Summary collapse
-
#changes?(table_name) ⇒ Boolean
Checks if a table has any changes.
-
#count_changes(table_name) ⇒ Integer
Counts total changes for a table.
-
#count_changes_by_operation(table_name) ⇒ Hash
Counts changes by operation type.
-
#find_changes(table_name, **options) ⇒ Array<Hash>
Finds all changes for a specific table.
-
#find_changes_by_operation(table_name, operation, limit: DEFAULT_CHANGE_LIMIT) ⇒ Array<Hash>
Finds changes for a table with a specific operation.
-
#find_recent_changes(table_name, limit: DEFAULT_CHANGE_LIMIT, since: 1.day.ago) ⇒ Array<Hash>
Finds recent changes for a table.
-
#initialize(session_storage, storage_path = nil) ⇒ TableStorage
constructor
Initializes table storage with session storage dependency.
-
#load_changes(table_name) ⇒ Array<Hash>
deprecated
Deprecated.
Use #find_changes instead
-
#tables_with_changes ⇒ Array<String>
Lists all tables that have changes.
Methods included from Concerns::Validatable
included, #valid_id?, #valid_name?, #validate_id!, #validate_name!, #validate_presence!
Methods included from Concerns::Timestampable
#age, included, #initialize_timestamps, #recently_created?, #recently_updated?, #touch_updated_at
Methods included from Concerns::ErrorHandler
#safe_operation, #with_error_handling
Constructor Details
#initialize(session_storage, storage_path = nil) ⇒ TableStorage
Initializes table storage with session storage dependency
40 41 42 43 44 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 40 def initialize(session_storage, storage_path = nil) super(storage_path) @session_storage = session_storage @change_processor = ChangeProcessor.new(session_storage) end |
Instance Attribute Details
#change_processor ⇒ ChangeProcessor (readonly)
Returns processor for handling table changes.
31 32 33 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 31 def change_processor @change_processor end |
#session_storage ⇒ SessionStorage (readonly)
Returns session storage dependency.
34 35 36 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 34 def session_storage @session_storage end |
Instance Method Details
#changes?(table_name) ⇒ Boolean
Checks if a table has any changes
130 131 132 133 134 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 130 def changes?(table_name) return false unless valid_table_name?(table_name) count_changes(table_name).positive? end |
#count_changes(table_name) ⇒ Integer
Counts total changes for a table
100 101 102 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 100 def count_changes(table_name) find_changes(table_name).size end |
#count_changes_by_operation(table_name) ⇒ Hash
Counts changes by operation type
108 109 110 111 112 113 114 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 108 def count_changes_by_operation(table_name) changes = find_changes(table_name) SUPPORTED_OPERATIONS.each_with_object({}) do |operation, counts| counts[operation] = changes.count { |change| change[:operation] == operation } end end |
#find_changes(table_name, **options) ⇒ Array<Hash>
Finds all changes for a specific table
Retrieves and processes all database changes related to the specified table from stored session data. Returns an empty array if the table name is invalid or no changes are found.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 65 def find_changes(table_name, **) validate_table_name!(table_name) validate_operation!([:operation]) if [:operation] changes = change_processor.process_table_changes(table_name) apply_filters(changes, **) rescue StandardError => e log_error("Failed to load changes for table #{table_name}", e) [] end |
#find_changes_by_operation(table_name, operation, limit: DEFAULT_CHANGE_LIMIT) ⇒ Array<Hash>
Finds changes for a table with a specific operation
82 83 84 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 82 def find_changes_by_operation(table_name, operation, limit: DEFAULT_CHANGE_LIMIT) find_changes(table_name, operation: operation, limit: limit) end |
#find_recent_changes(table_name, limit: DEFAULT_CHANGE_LIMIT, since: 1.day.ago) ⇒ Array<Hash>
Finds recent changes for a table
92 93 94 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 92 def find_recent_changes(table_name, limit: DEFAULT_CHANGE_LIMIT, since: 1.day.ago) find_changes(table_name, limit: limit, since: since) end |
#load_changes(table_name) ⇒ Array<Hash>
Use #find_changes instead
Legacy method for backward compatibility
141 142 143 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 141 def load_changes(table_name) find_changes(table_name) end |
#tables_with_changes ⇒ Array<String>
Lists all tables that have changes
119 120 121 122 123 124 |
# File 'lib/dbwatcher/storage/table_storage.rb', line 119 def tables_with_changes change_processor.tables_with_changes rescue StandardError => e log_error("Failed to load tables with changes", e) [] end |