Class: QueryGuard::Migrations::DatabaseAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/query_guard/migrations/database_adapter.rb

Overview

Abstract interface for database metadata adapters.

Adapters implement this interface to provide table metadata (row counts, lock behavior, etc.) from different database systems.

Example:

adapter = PostgreSQLAdapter.new(connection)
rows = adapter.estimate_table_rows(:users)

Adapters should:

  1. Handle connection failures gracefully
  2. Return nil or empty hash if data unavailable
  3. Cache results where possible
  4. Never raise exceptions during metadata queries

Direct Known Subclasses

NullDatabaseAdapter, PostgreSQLAdapter

Instance Method Summary collapse

Instance Method Details

#connected? ⇒ Boolean

Check if connection is healthy

Returns:

  • (Boolean) —

    True if adapter can query database

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/query_guard/migrations/database_adapter.rb', line 57

def connected?
  raise NotImplementedError, "Subclasses must implement connected?"
end

#estimate_lock_risk(table_name) ⇒ Symbol?

Get lock impact estimate for a table

Scores table's lock risk based on size and activity. Based on PostgreSQL's vacuum and maintenance overhead.

Parameters:

  • table_name (String, Symbol) —

    Name of the table

Returns:

  • (Symbol, nil) —

    Risk level: :low, :medium, :high, :critical

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/query_guard/migrations/database_adapter.rb', line 35

def estimate_lock_risk(table_name)
  raise NotImplementedError, "Subclasses must implement estimate_lock_risk"
end

#estimate_table_rows(table_name) ⇒ Integer?

Get estimated row count for a table

Parameters:

  • table_name (String, Symbol) —

    Name of the table

Returns:

  • (Integer, nil) —

    Estimated number of rows, or nil if unavailable

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/query_guard/migrations/database_adapter.rb', line 24

def estimate_table_rows(table_name)
  raise NotImplementedError, "Subclasses must implement estimate_table_rows"
end

#list_tables ⇒ Array<String>

Get all table names in current schema

Returns:

  • (Array<String>) —

    List of table names

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/query_guard/migrations/database_adapter.rb', line 50

def list_tables
  raise NotImplementedError, "Subclasses must implement list_tables"
end

#table_exists?(table_name) ⇒ Boolean

Check if table exists in database

Parameters:

  • table_name (String, Symbol) —

    Name of the table

Returns:

  • (Boolean) —

    True if table exists

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/query_guard/migrations/database_adapter.rb', line 43

def table_exists?(table_name)
  raise NotImplementedError, "Subclasses must implement table_exists?"
end