Class: PgSqlTriggers::DatabaseIntrospection

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_sql_triggers/database_introspection.rb

Constant Summary collapse

DEFAULT_EXCLUDED_TABLES =

Default tables to exclude from listing (Rails defaults and pg_sql_triggers internal tables)

%w[
  ar_internal_metadata
  schema_migrations
  pg_sql_triggers_registry
  trigger_migrations
].freeze

Instance Method Summary collapse

Instance Method Details

#excluded_tablesObject

Get list of all excluded tables (defaults + user-configured)



14
15
16
# File 'lib/pg_sql_triggers/database_introspection.rb', line 14

def excluded_tables
  (DEFAULT_EXCLUDED_TABLES + Array(PgSqlTriggers.excluded_tables)).uniq
end

#function_exists?(function_name) ⇒ Boolean

Check if function exists

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
# File 'lib/pg_sql_triggers/database_introspection.rb', line 109

def function_exists?(function_name)
  sql = <<~SQL.squish
    SELECT COUNT(*) as count
    FROM pg_proc
    WHERE proname = '#{sanitize(function_name)}'
  SQL

  result = ActiveRecord::Base.connection.execute(sql).first
  result["count"].to_i.positive?
end

#list_tablesObject

Get list of all user tables in the database



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pg_sql_triggers/database_introspection.rb', line 19

def list_tables
  sql = <<~SQL.squish
    SELECT table_name
    FROM information_schema.tables
    WHERE table_schema = 'public'
    AND table_type = 'BASE TABLE'
    ORDER BY table_name
  SQL

  result = ActiveRecord::Base.connection.execute(sql)
  tables = result.pluck("table_name")
  tables.reject { |table| excluded_tables.include?(table) }
rescue StandardError => e
  Rails.logger.error("Failed to fetch tables: #{e.message}") if defined?(Rails.logger)
  []
end

#table_columns(table_name) ⇒ Object

Get table columns



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pg_sql_triggers/database_introspection.rb', line 89

def table_columns(table_name)
  sql = <<~SQL.squish
    SELECT column_name, data_type, is_nullable
    FROM information_schema.columns
    WHERE table_schema = 'public'
    AND table_name = '#{sanitize(table_name)}'
    ORDER BY ordinal_position
  SQL

  result = ActiveRecord::Base.connection.execute(sql)
  result.map do |row|
    {
      name: row["column_name"],
      type: row["data_type"],
      nullable: row["is_nullable"] == "YES"
    }
  end
end

#table_triggers(table_name) ⇒ Object

Get triggers for a specific table



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/pg_sql_triggers/database_introspection.rb', line 204

def table_triggers(table_name)
  # From registry
  registry_triggers = PgSqlTriggers::TriggerRegistry.for_table(table_name)

  # From database
  db_triggers_sql = <<~SQL.squish
    SELECT#{' '}
      t.tgname as trigger_name,
      p.proname as function_name,
      pg_get_triggerdef(t.oid) as trigger_definition
    FROM pg_trigger t
    JOIN pg_class c ON t.tgrelid = c.oid
    JOIN pg_proc p ON t.tgfoid = p.oid
    JOIN pg_namespace n ON c.relnamespace = n.oid
    WHERE NOT t.tgisinternal
    AND c.relname = '#{sanitize(table_name)}'
    AND n.nspname = 'public'
    ORDER BY t.tgname
  SQL

  db_triggers = []
  begin
    result = ActiveRecord::Base.connection.execute(db_triggers_sql)
    result.each do |row|
      db_triggers << {
        trigger_name: row["trigger_name"],
        function_name: row["function_name"],
        definition: row["trigger_definition"]
      }
    end
  rescue StandardError => e
    Rails.logger.error("Failed to fetch database triggers: #{e.message}")
  end

  {
    table_name: table_name,
    registry_triggers: registry_triggers,
    database_triggers: db_triggers
  }
end

#tables_with_triggersObject

Get all tables with their triggers and functions



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/pg_sql_triggers/database_introspection.rb', line 140

def tables_with_triggers
  # Get all tables
  tables = list_tables

  # Get all triggers from registry
  triggers_by_table = PgSqlTriggers::TriggerRegistry.all.group_by(&:table_name)

  # Get actual database triggers
  db_triggers_sql = <<~SQL.squish
    SELECT#{' '}
      t.tgname as trigger_name,
      c.relname as table_name,
      p.proname as function_name,
      pg_get_triggerdef(t.oid) as trigger_definition
    FROM pg_trigger t
    JOIN pg_class c ON t.tgrelid = c.oid
    JOIN pg_proc p ON t.tgfoid = p.oid
    JOIN pg_namespace n ON c.relnamespace = n.oid
    WHERE NOT t.tgisinternal
    AND n.nspname = 'public'
    ORDER BY c.relname, t.tgname
  SQL

  db_triggers = {}
  begin
    result = ActiveRecord::Base.connection.execute(db_triggers_sql)
    result.each do |row|
      table_name = row["table_name"]
      db_triggers[table_name] ||= []
      db_triggers[table_name] << {
        trigger_name: row["trigger_name"],
        function_name: row["function_name"],
        definition: row["trigger_definition"]
      }
    end
  rescue StandardError => e
    Rails.logger.error("Failed to fetch database triggers: #{e.message}")
  end

  # Combine registry and database triggers
  tables.map do |table_name|
    registry_triggers = triggers_by_table[table_name] || []
    db_table_triggers = db_triggers[table_name] || []

    {
      table_name: table_name,
      registry_triggers: registry_triggers.map do |t|
        {
          id: t.id,
          trigger_name: t.trigger_name,
          function_name: t.definition.present? ? JSON.parse(t.definition)["function_name"] : nil,
          enabled: t.enabled,
          version: t.version,
          source: t.source,
          function_body: t.function_body
        }
      end,
      database_triggers: db_table_triggers,
      trigger_count: registry_triggers.count + db_table_triggers.count
    }
  end
end

#trigger_exists?(trigger_name) ⇒ Boolean

Check if trigger exists

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pg_sql_triggers/database_introspection.rb', line 121

def trigger_exists?(trigger_name)
  sql = <<~SQL.squish
    SELECT COUNT(*) as count
    FROM pg_trigger t
    JOIN pg_class c ON t.tgrelid = c.oid
    JOIN pg_namespace n ON c.relnamespace = n.oid
    WHERE t.tgname = '#{sanitize(trigger_name)}'
    AND n.nspname = 'public'
    AND NOT t.tgisinternal
  SQL

  result = ActiveRecord::Base.connection.execute(sql).first
  result["count"].to_i.positive?
rescue StandardError => e
  Rails.logger.error("Failed to check if trigger exists: #{e.message}") if defined?(Rails.logger)
  false
end

#validate_table(table_name) ⇒ Object

Validate that a table exists



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pg_sql_triggers/database_introspection.rb', line 37

def validate_table(table_name)
  return { valid: false, error: "Table name cannot be blank" } if table_name.blank?

  # Use case-insensitive comparison and sanitize input
  sanitized_name = sanitize(table_name)

  # First, check if table exists and get column count
  column_count_sql = <<~SQL.squish
    SELECT COUNT(*) as column_count
    FROM information_schema.columns
    WHERE table_schema = 'public'
    AND LOWER(table_name) = LOWER('#{sanitized_name}')
  SQL

  column_result = ActiveRecord::Base.connection.execute(column_count_sql).first
  column_count = column_result ? column_result["column_count"].to_i : 0

  if column_count.positive?
    # Get table comment separately
    comment_sql = <<~SQL.squish
      SELECT obj_description(c.oid, 'pg_class') as comment
      FROM pg_class c
      JOIN pg_namespace n ON n.oid = c.relnamespace
      WHERE n.nspname = 'public'
      AND LOWER(c.relname) = LOWER('#{sanitized_name}')
      AND c.relkind = 'r'
    SQL

    comment_result = ActiveRecord::Base.connection.execute(comment_sql).first
    comment = comment_result ? comment_result["comment"] : nil

    {
      valid: true,
      table_name: table_name,
      column_count: column_count,
      comment: comment
    }
  else
    {
      valid: false,
      error: "Table '#{table_name}' not found in database"
    }
  end
rescue StandardError => e
  Rails.logger.error("Table validation error for '#{table_name}': #{e.message}")
  {
    valid: false,
    error: e.message
  }
end