Module: Arfi::Commands::FunctionsPaths

Included in:
Functions
Defined in:
lib/arfi/commands/functions_paths.rb,
sig/lib/arfi/commands/functions_paths.rbs

Overview

Path resolution helpers for Functions.

Constant Summary collapse

ROOT_DIR =

Returns:

  • ("db/functions")
DEFAULT_SCHEMA =

Returns:

  • ("public")

Instance Method Summary collapse

Instance Method Details

#adapter_canonical_path(root, schema, function_name) ⇒ String

Build the canonical path for an adapter-specific function.

Parameters:

  • root (Pathname)

    Project root directory (Rails.root/db/functions)

  • schema (String?)

    Schema name (PostgreSQL only)

  • function_name (String)

    Function name

Returns:

  • (String)

    Absolute canonical path

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/arfi/commands/functions_paths.rb', line 70

def adapter_canonical_path(root, schema, function_name)
  case adapter_opt
  when 'postgresql'
    root.join('postgresql', schema || DEFAULT_SCHEMA, "#{function_name}.sql").to_s
  when 'mysql', 'trilogy'
    raise ArgumentError, 'Schema-qualified functions are only supported for PostgreSQL.' if schema

    root.join('mysql', DEFAULT_SCHEMA, "#{function_name}.sql").to_s
  else
    raise Arfi::Errors::AdapterNotSupported
  end
end

#canonical_path(schema, function_name) ⇒ String

Resolve the canonical output path for a function file, respecting adapter option.

Parameters:

  • schema (String?)

    Schema name (optional)

  • function_name (String)

    Function name

Returns:

  • (String)

    Absolute path to the function file



15
16
17
18
19
20
21
22
# File 'lib/arfi/commands/functions_paths.rb', line 15

def canonical_path(schema, function_name)
  root = Rails.root.join(ROOT_DIR)
  if adapter_opt.nil?
    generic_canonical_path(root, schema, function_name)
  else
    adapter_canonical_path(root, schema, function_name)
  end
end

#function_paths(schema, function_name) ⇒ Array<String>

List all possible filesystem paths where a function file might exist, respecting adapter.

Parameters:

  • schema (String?)

    Schema name (optional)

  • function_name (String)

    Function name

Returns:

  • (Array<String>)

    List of absolute paths to check

Raises:



31
32
33
34
35
36
37
38
39
40
# File 'lib/arfi/commands/functions_paths.rb', line 31

def function_paths(schema, function_name)
  root = Rails.root.join(ROOT_DIR)
  out = case adapter_opt
        when nil then generic_function_paths(root, function_name)
        when 'postgresql' then postgresql_function_paths(root, schema, function_name)
        when 'mysql', 'trilogy' then mysql_function_paths(root, function_name)
        else raise Arfi::Errors::AdapterNotSupported
        end
  out.uniq
end

#generic_canonical_path(root, schema, function_name) ⇒ String

Build the canonical path for a generic (non-adapter) function.

Generic functions can only target the 'public' schema.

Parameters:

  • root (Pathname)

    Project root directory (Rails.root/db/functions)

  • schema (String?)

    Schema name (must be nil or 'public')

  • function_name (String)

    Function name

Returns:

  • (String)

    Absolute canonical path

Raises:

  • (ArgumentError)

    If schema is not public



52
53
54
55
56
57
58
59
# File 'lib/arfi/commands/functions_paths.rb', line 52

def generic_canonical_path(root, schema, function_name)
  sch = schema || DEFAULT_SCHEMA
  unless sch == DEFAULT_SCHEMA
    raise ArgumentError,
          "Generic functions can only target schema '#{DEFAULT_SCHEMA}'"
  end
  root.join(DEFAULT_SCHEMA, "#{function_name}.sql").to_s
end

#generic_function_paths(root, function_name) ⇒ Array<String>

List file paths to check when looking for a generic function file.

Parameters:

  • root (Pathname)

    Project root directory

  • function_name (String)

    Function name

Returns:

  • (Array<String>)

    Ordered list of paths to check



89
90
91
92
93
94
# File 'lib/arfi/commands/functions_paths.rb', line 89

def generic_function_paths(root, function_name)
  [
    root.join(DEFAULT_SCHEMA, "#{function_name}.sql").to_s,
    root.join("#{function_name}.sql").to_s
  ]
end

#mysql_function_paths(root, function_name) ⇒ Array<String>

List file paths to check when looking for a MySQL/Trilogy function file.

Parameters:

  • root (Pathname)

    Project root directory

  • function_name (String)

    Function name

Returns:

  • (Array<String>)

    Ordered list of paths to check



121
122
123
124
125
126
127
128
# File 'lib/arfi/commands/functions_paths.rb', line 121

def mysql_function_paths(root, function_name)
  [
    root.join('mysql', DEFAULT_SCHEMA, "#{function_name}.sql").to_s,
    root.join('mysql', "#{function_name}.sql").to_s,
    root.join(DEFAULT_SCHEMA, "#{function_name}.sql").to_s,
    root.join("#{function_name}.sql").to_s
  ]
end

#postgresql_function_paths(root, schema, function_name) ⇒ Array<String>

List file paths to check when looking for a PostgreSQL function file.

Parameters:

  • root (Pathname)

    Project root directory

  • schema (String?)

    Schema name (optional)

  • function_name (String)

    Function name

Returns:

  • (Array<String>)

    Ordered list of paths to check



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/arfi/commands/functions_paths.rb', line 103

def postgresql_function_paths(root, schema, function_name)
  sch = schema || DEFAULT_SCHEMA
  out = [
    root.join('postgresql', sch, "#{function_name}.sql").to_s,
    root.join('postgresql', DEFAULT_SCHEMA, "#{function_name}.sql").to_s,
    root.join(DEFAULT_SCHEMA, "#{function_name}.sql").to_s,
    root.join("#{function_name}.sql").to_s
  ]
  out.insert(2, root.join('postgresql', "#{function_name}.sql").to_s) if sch == DEFAULT_SCHEMA
  out
end