Class: Whodunit::Chronicles::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/whodunit/chronicles/configuration.rb

Overview

Configuration management for Chronicles

Provides a centralized configuration system with sensible defaults and validation for all Chronicles settings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/whodunit/chronicles/configuration.rb', line 14

def initialize
  @database_url = ENV.fetch('DATABASE_URL', nil)
  @audit_database_url = ENV.fetch('AUDIT_DATABASE_URL', nil)
  @adapter = :postgresql
  @publication_name = 'whodunit_audit'
  @replication_slot_name = 'whodunit_audit_slot'
  @batch_size = 100
  @max_retry_attempts = 3
  @retry_delay = 5
  @logger = Dry::Logger.new
  @table_filter = nil
  @schema_filter = nil
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def adapter
  @adapter
end

#audit_database_urlObject

Returns the value of attribute audit_database_url.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def audit_database_url
  @audit_database_url
end

#batch_sizeObject

Returns the value of attribute batch_size.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def batch_size
  @batch_size
end

#database_urlObject

Returns the value of attribute database_url.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def database_url
  @database_url
end

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def logger
  @logger
end

#max_retry_attemptsObject

Returns the value of attribute max_retry_attempts.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def max_retry_attempts
  @max_retry_attempts
end

#publication_nameObject

Returns the value of attribute publication_name.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def publication_name
  @publication_name
end

#replication_slot_nameObject

Returns the value of attribute replication_slot_name.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def replication_slot_name
  @replication_slot_name
end

#retry_delayObject

Returns the value of attribute retry_delay.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def retry_delay
  @retry_delay
end

#schema_filterObject

Returns the value of attribute schema_filter.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def schema_filter
  @schema_filter
end

#table_filterObject

Returns the value of attribute table_filter.



10
11
12
# File 'lib/whodunit/chronicles/configuration.rb', line 10

def table_filter
  @table_filter
end

Instance Method Details

#chronicle_table?(table_name, schema_name = 'public') ⇒ Boolean

Check if a table should be chronicled based on filters

Parameters:

  • table_name (String)

    The table name to check

  • schema_name (String) (defaults to: 'public')

    The schema name to check

Returns:

  • (Boolean)

    true if the table should be chronicled



46
47
48
49
50
51
# File 'lib/whodunit/chronicles/configuration.rb', line 46

def chronicle_table?(table_name, schema_name = 'public')
  return false if filtered_by_schema?(schema_name)
  return false if filtered_by_table?(table_name)

  true
end

#filtered_by_schema?(schema_name) ⇒ Boolean (private)

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/whodunit/chronicles/configuration.rb', line 79

def filtered_by_schema?(schema_name)
  return false unless schema_filter

  case schema_filter
  when Array
    !schema_filter.include?(schema_name)
  when String, Symbol
    schema_name != schema_filter.to_s
  when Proc
    !schema_filter.call(schema_name)
  else
    false
  end
end

#filtered_by_table?(table_name) ⇒ Boolean (private)

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/whodunit/chronicles/configuration.rb', line 94

def filtered_by_table?(table_name)
  return false unless table_filter

  case table_filter
  when Array
    !table_filter.include?(table_name)
  when String, Symbol
    table_name != table_filter.to_s
  when Regexp
    !table_filter.match?(table_name)
  when Proc
    !table_filter.call(table_name)
  else
    false
  end
end

#validate!Object

Validate configuration settings

Raises:



31
32
33
34
35
36
37
38
39
# File 'lib/whodunit/chronicles/configuration.rb', line 31

def validate!
  raise ConfigurationError, 'database_url is required' if database_url.nil?
  raise ConfigurationError, 'adapter must be :postgresql or :mysql' unless %i[postgresql mysql].include?(adapter)
  raise ConfigurationError, 'batch_size must be positive' unless batch_size.positive?
  raise ConfigurationError, 'max_retry_attempts must be positive' unless max_retry_attempts.positive?
  raise ConfigurationError, 'retry_delay must be positive' unless retry_delay.positive?

  validate_adapter_specific_settings!
end

#validate_adapter_specific_settings!Object (private)



55
56
57
58
59
60
61
62
# File 'lib/whodunit/chronicles/configuration.rb', line 55

def validate_adapter_specific_settings!
  case adapter
  when :postgresql
    validate_postgresql_settings!
  when :mysql
    validate_mysql_settings!
  end
end

#validate_mysql_settings!Object (private)



74
75
76
77
# File 'lib/whodunit/chronicles/configuration.rb', line 74

def validate_mysql_settings!
  # MySQL-specific validations can be added here in the future
  # For now, MySQL settings are less restrictive
end

#validate_postgresql_settings!Object (private)

Raises:



64
65
66
67
68
69
70
71
72
# File 'lib/whodunit/chronicles/configuration.rb', line 64

def validate_postgresql_settings!
  if publication_name && !/\A[a-zA-Z_][a-zA-Z0-9_]*\z/.match?(publication_name)
    raise ConfigurationError, 'publication_name must be a valid PostgreSQL identifier'
  end

  return unless replication_slot_name && !/\A[a-zA-Z_][a-zA-Z0-9_]*\z/.match?(replication_slot_name)

  raise ConfigurationError, 'replication_slot_name must be a valid PostgreSQL identifier'
end