Module: Arfi::PostgreSQL::DatabaseStatementsPatch

Defined in:
lib/arfi/extensions/active_record/connection_adapters/postgresql/database_statements.rb

Overview

ActiveRecord adapter patch for PostgreSQL.

When a query fails with ‘PG::UndefinedFunction`, ARFI checks whether the missing function is managed by ARFI (file exists under db/functions). If yes, ARFI loads function SQL files via SqlFunctionLoader.load! and retries the failed query once.

Constant Summary collapse

ARFI_UNDEFINED_FUNCTION =
/
  function\s+([a-zA-Z0-9_."]+)\s*\(.*?\)\s+does\s+not\s+exist
/ix.freeze
THREAD_GUARD_KEY =
:arfi_reloading_functions

Instance Method Summary collapse

Instance Method Details

#exec_query(*args, **kwargs) ⇒ Object

Execute a SQL query, reloading missing ARFI-managed functions and retrying on PG::UndefinedFunction.

Parameters:

  • args (Array<Object>)

    Positional arguments forwarded to the original exec_query

  • kwargs (Object)

    Keyword arguments forwarded to the original exec_query

Returns:

  • (Object)

    Query result

  • (Object)

    if StandardError (retry successful)

Raises:

  • (StandardError)

    If the error is not recoverable



34
35
36
37
38
39
# File 'lib/arfi/extensions/active_record/connection_adapters/postgresql/database_statements.rb', line 34

def exec_query(*args, **kwargs)
  super
rescue StandardError => e
  raise unless arfi_try_reload_and_retry?(e)
  retry
end

#execute(*args, **kwargs) ⇒ Object

Execute raw SQL, reloading missing ARFI-managed functions and retrying on PG::UndefinedFunction.

Parameters:

  • args (Array<Object>)

    Positional arguments forwarded to the original execute

  • kwargs (Object)

    Keyword arguments forwarded to the original execute

Returns:

  • (Object)

    Execution result

  • (Object)

    if StandardError (retry successful)

Raises:

  • (StandardError)

    If the error is not recoverable



48
49
50
51
52
53
# File 'lib/arfi/extensions/active_record/connection_adapters/postgresql/database_statements.rb', line 48

def execute(*args, **kwargs)
  super
rescue StandardError => e
  raise unless arfi_try_reload_and_retry?(e)
  retry
end

#internal_exec_query(*args, **kwargs) ⇒ Object

Execute an internal query, reloading missing ARFI-managed functions and retrying on PG::UndefinedFunction.

Parameters:

  • args (Array<Object>)

    Positional arguments forwarded to the original internal_exec_query

  • kwargs (Object)

    Keyword arguments forwarded to the original internal_exec_query

Returns:

  • (Object)

    Query result

  • (Object)

    if StandardError (retry successful)

Raises:

  • (StandardError)

    If the error is not recoverable



76
77
78
79
80
81
# File 'lib/arfi/extensions/active_record/connection_adapters/postgresql/database_statements.rb', line 76

def internal_exec_query(*args, **kwargs)
  super
rescue StandardError => e
  raise unless arfi_try_reload_and_retry?(e)
  retry
end

#raw_execute(*args, **kwargs) ⇒ Object

Execute a raw SQL statement, reloading missing ARFI-managed functions and retrying on PG::UndefinedFunction.

Parameters:

  • args (Array<Object>)

    Positional arguments forwarded to the original raw_execute

  • kwargs (Object)

    Keyword arguments forwarded to the original raw_execute

Returns:

  • (Object)

    Execution result

  • (Object)

    if StandardError (retry successful)

Raises:

  • (StandardError)

    If the error is not recoverable



62
63
64
65
66
67
# File 'lib/arfi/extensions/active_record/connection_adapters/postgresql/database_statements.rb', line 62

def raw_execute(*args, **kwargs)
  super
rescue StandardError => e
  raise unless arfi_try_reload_and_retry?(e)
  retry
end