Class: Dbwatcher::Storage::TableStorage

Inherits:
BaseStorage show all
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.

Examples:

Basic usage

storage = TableStorage.new(session_storage)
changes = storage.find_changes("users")
changes.each { |change| puts "#{change[:operation]} on #{change[:table]}" }

Advanced filtering

recent_changes = storage.find_recent_changes("users", limit: 10)
filtered_changes = storage.find_changes_by_operation("users", "INSERT")

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

Attributes inherited from BaseStorage

#file_manager, #storage_path

Instance Method Summary collapse

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

Parameters:

  • session_storage (SessionStorage)

    storage instance for session data

  • storage_path (String, nil) (defaults to: nil)

    custom storage path (optional)



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_processorChangeProcessor (readonly)

Returns processor for handling table changes.

Returns:



31
32
33
# File 'lib/dbwatcher/storage/table_storage.rb', line 31

def change_processor
  @change_processor
end

#session_storageSessionStorage (readonly)

Returns session storage dependency.

Returns:



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

Parameters:

  • table_name (String)

    name of the table to check

Returns:

  • (Boolean)

    true if table has 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

Parameters:

  • table_name (String)

    name of the table

Returns:

  • (Integer)

    number of changes for the 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

Parameters:

  • table_name (String)

    name of the table

Returns:

  • (Hash)

    hash with operation types as keys and counts as values



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.

Examples:

changes = storage.find_changes("users")
puts "Found #{changes.length} changes for users table"

With filtering

recent_inserts = storage.find_changes("users", operation: "INSERT", limit: 50)

Parameters:

  • table_name (String)

    name of the table to load changes for

  • options (Hash)

    filtering options

Options Hash (**options):

  • :limit (Integer)

    maximum number of changes to return

  • :operation (String)

    filter by operation type (INSERT, UPDATE, DELETE)

  • :since (Time)

    only return changes after this time

Returns:

  • (Array<Hash>)

    array of change records for the table



65
66
67
68
69
70
71
72
73
74
# File 'lib/dbwatcher/storage/table_storage.rb', line 65

def find_changes(table_name, **options)
  validate_table_name!(table_name)
  validate_operation!(options[:operation]) if options[:operation]

  changes = change_processor.process_table_changes(table_name)
  apply_filters(changes, **options)
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

Parameters:

  • table_name (String)

    name of the table

  • operation (String)

    operation type (INSERT, UPDATE, DELETE)

  • limit (Integer) (defaults to: DEFAULT_CHANGE_LIMIT)

    maximum number of changes to return

Returns:

  • (Array<Hash>)

    filtered change records



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

Parameters:

  • table_name (String)

    name of the table

  • limit (Integer) (defaults to: DEFAULT_CHANGE_LIMIT)

    maximum number of changes to return

  • since (Time) (defaults to: 1.day.ago)

    only return changes after this time

Returns:

  • (Array<Hash>)

    recent change records



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>

Deprecated.

Use #find_changes instead

Legacy method for backward compatibility

Parameters:

  • table_name (String)

    name of the table to load changes for

Returns:

  • (Array<Hash>)

    array of change records



141
142
143
# File 'lib/dbwatcher/storage/table_storage.rb', line 141

def load_changes(table_name)
  find_changes(table_name)
end

#tables_with_changesArray<String>

Lists all tables that have changes

Returns:

  • (Array<String>)

    array of table names with 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