Module: PgSqlTriggers::Registry

Defined in:
lib/pg_sql_triggers/registry.rb,
lib/pg_sql_triggers/registry/manager.rb,
lib/pg_sql_triggers/registry/validator.rb

Overview

Registry module provides a unified API for querying and managing triggers.

Examples:

Query triggers

# List all triggers
triggers = PgSqlTriggers::Registry.list

# Get enabled/disabled triggers
enabled = PgSqlTriggers::Registry.enabled
disabled = PgSqlTriggers::Registry.disabled

# Get triggers for a specific table
user_triggers = PgSqlTriggers::Registry.for_table(:users)

# Check for drift
drift_info = PgSqlTriggers::Registry.diff

Manage triggers

# Enable a trigger
PgSqlTriggers::Registry.enable("users_email_validation",
                                actor: current_user,
                                confirmation: "EXECUTE TRIGGER_ENABLE")

# Disable a trigger
PgSqlTriggers::Registry.disable("users_email_validation",
                                 actor: current_user,
                                 confirmation: "EXECUTE TRIGGER_DISABLE")

# Drop a trigger
PgSqlTriggers::Registry.drop("old_trigger",
                              actor: current_user,
                              reason: "No longer needed",
                              confirmation: "EXECUTE TRIGGER_DROP")

# Re-execute a trigger
PgSqlTriggers::Registry.re_execute("drifted_trigger",
                                    actor: current_user,
                                    reason: "Fix drift",
                                    confirmation: "EXECUTE TRIGGER_RE_EXECUTE")

Defined Under Namespace

Classes: Manager, Validator

Class Method Summary collapse

Class Method Details

.diff(trigger_name = nil) ⇒ Hash

Checks for drift between DSL definitions and database state.

Parameters:

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

    Optional trigger name to check specific trigger, or nil for all triggers

Returns:

  • (Hash)

    Drift information with keys: :in_sync, :drifted, :manual_override, :disabled, :dropped, :unknown



87
88
89
# File 'lib/pg_sql_triggers/registry.rb', line 87

def self.diff(trigger_name = nil)
  Manager.diff(trigger_name)
end

.disable(trigger_name, actor:, confirmation: nil) ⇒ PgSqlTriggers::TriggerRegistry

Disables a trigger by name.

Parameters:

  • trigger_name (String)

    The name of the trigger to disable

  • actor (Hash)

    Information about who is performing the action (must have :type and :id keys)

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

    Optional confirmation text for kill switch protection

Returns:

Raises:



151
152
153
154
155
# File 'lib/pg_sql_triggers/registry.rb', line 151

def self.disable(trigger_name, actor:, confirmation: nil)
  check_permission!(actor, :disable_trigger)
  trigger = find_trigger!(trigger_name)
  trigger.disable!(confirmation: confirmation, actor: actor)
end

.disabledActiveRecord::Relation<PgSqlTriggers::TriggerRegistry>

Returns only disabled triggers.

Returns:



71
72
73
# File 'lib/pg_sql_triggers/registry.rb', line 71

def self.disabled
  Manager.disabled
end

.driftedArray<Hash>

Returns all triggers that have drifted from their expected state.

Returns:

  • (Array<Hash>)

    Array of drift result hashes for drifted triggers



94
95
96
# File 'lib/pg_sql_triggers/registry.rb', line 94

def self.drifted
  Manager.drifted
end

.drop(trigger_name, actor:, reason:, confirmation: nil) ⇒ true

Drops a trigger by name.

Parameters:

  • trigger_name (String)

    The name of the trigger to drop

  • actor (Hash)

    Information about who is performing the action (must have :type and :id keys)

  • reason (String)

    Required reason for dropping the trigger

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

    Optional confirmation text for kill switch protection

Returns:

  • (true)

    If drop succeeds

Raises:



168
169
170
171
172
# File 'lib/pg_sql_triggers/registry.rb', line 168

def self.drop(trigger_name, actor:, reason:, confirmation: nil)
  check_permission!(actor, :drop_trigger)
  trigger = find_trigger!(trigger_name)
  trigger.drop!(reason: reason, confirmation: confirmation, actor: actor)
end

.droppedArray<Hash>

Returns all triggers that have been dropped from the database.

Returns:

  • (Array<Hash>)

    Array of drift result hashes for dropped triggers



115
116
117
# File 'lib/pg_sql_triggers/registry.rb', line 115

def self.dropped
  Manager.dropped
end

.enable(trigger_name, actor:, confirmation: nil) ⇒ PgSqlTriggers::TriggerRegistry

Enables a trigger by name.

Parameters:

  • trigger_name (String)

    The name of the trigger to enable

  • actor (Hash)

    Information about who is performing the action (must have :type and :id keys)

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

    Optional confirmation text for kill switch protection

Returns:

Raises:



136
137
138
139
140
# File 'lib/pg_sql_triggers/registry.rb', line 136

def self.enable(trigger_name, actor:, confirmation: nil)
  check_permission!(actor, :enable_trigger)
  trigger = find_trigger!(trigger_name)
  trigger.enable!(confirmation: confirmation, actor: actor)
end

.enabledActiveRecord::Relation<PgSqlTriggers::TriggerRegistry>

Returns only enabled triggers.

Returns:



64
65
66
# File 'lib/pg_sql_triggers/registry.rb', line 64

def self.enabled
  Manager.enabled
end

.for_table(table_name) ⇒ ActiveRecord::Relation<PgSqlTriggers::TriggerRegistry>

Returns triggers for a specific table.

Parameters:

  • table_name (String, Symbol)

    The table name to filter by

Returns:



79
80
81
# File 'lib/pg_sql_triggers/registry.rb', line 79

def self.for_table(table_name)
  Manager.for_table(table_name)
end

.in_syncArray<Hash>

Returns all triggers that are in sync with their expected state.

Returns:

  • (Array<Hash>)

    Array of drift result hashes for in-sync triggers



101
102
103
# File 'lib/pg_sql_triggers/registry.rb', line 101

def self.in_sync
  Manager.in_sync
end

.listActiveRecord::Relation<PgSqlTriggers::TriggerRegistry>

Returns all registered triggers.

Returns:



57
58
59
# File 'lib/pg_sql_triggers/registry.rb', line 57

def self.list
  Manager.list
end

.re_execute(trigger_name, actor:, reason:, confirmation: nil) ⇒ PgSqlTriggers::TriggerRegistry

Re-executes a trigger by name (drops and recreates it).

Parameters:

  • trigger_name (String)

    The name of the trigger to re-execute

  • actor (Hash)

    Information about who is performing the action (must have :type and :id keys)

  • reason (String)

    Required reason for re-executing the trigger

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

    Optional confirmation text for kill switch protection

Returns:

Raises:



185
186
187
188
189
# File 'lib/pg_sql_triggers/registry.rb', line 185

def self.re_execute(trigger_name, actor:, reason:, confirmation: nil)
  check_permission!(actor, :drop_trigger) # Re-execute requires same permission as drop
  trigger = find_trigger!(trigger_name)
  trigger.re_execute!(reason: reason, confirmation: confirmation, actor: actor)
end

.register(definition) ⇒ PgSqlTriggers::TriggerRegistry

Registers a trigger definition in the registry.

Parameters:

Returns:



50
51
52
# File 'lib/pg_sql_triggers/registry.rb', line 50

def self.register(definition)
  Manager.register(definition)
end

.unknown_triggersArray<Hash>

Returns all unknown (external) triggers not managed by this gem.

Returns:

  • (Array<Hash>)

    Array of drift result hashes for unknown triggers



108
109
110
# File 'lib/pg_sql_triggers/registry.rb', line 108

def self.unknown_triggers
  Manager.unknown_triggers
end

.validate!true

Validates all triggers in the registry.

Returns:

  • (true)

    If validation passes

Raises:



123
124
125
# File 'lib/pg_sql_triggers/registry.rb', line 123

def self.validate!
  Validator.validate!
end