Class: Arfi::SqlFunctionLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/arfi/sql_function_loader.rb

Overview

Arfi::SqlFunctionLoader is a class which loads user defined SQL functions into database.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.task_nameObject (private)

Returns the value of attribute task_name.



25
26
27
# File 'lib/arfi/sql_function_loader.rb', line 25

def task_name
  @task_name
end

Class Method Details

.connActiveRecord::ConnectionAdapters::AbstractAdapter (private)

Arfi::SqlFunctionLoader#conn -> ActiveRecord::ConnectionAdapters::AbstractAdapter

Helper method to get database connection.

Returns:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)

    Database connection.



148
149
150
151
152
153
154
# File 'lib/arfi/sql_function_loader.rb', line 148

def conn
  if Rails::VERSION::MAJOR < 7 || (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR < 2)
    ActiveRecord::Base.connection
  else
    ActiveRecord::Base.lease_connection
  end
end

.handle_db_populationvoid (private)

This method returns an undefined value.

Arfi::SqlFunctionLoader#handle_db_population -> void

Loads user defined SQL functions into database. This conditional branch was written this way because if we call db:migrate:db_name, then task_name will not be nil, but it will be zero if we call db:migrate. Then we check that the application has been configured to work with multiple databases in order to populate all databases, and only after this check can we populate the database in case the db:migrate (or any other) task has been called for configuration with a single database. Go to ‘lib/arfi/tasks/db.rake` for additional info.



54
55
56
57
58
59
60
# File 'lib/arfi/sql_function_loader.rb', line 54

def handle_db_population
  if task_name || (task_name && multi_db?) || task_name.nil?
    populate_db
  elsif multi_db?
    populate_multiple_db
  end
end

.load!(task_name: nil) ⇒ nil, void

Arfi::SqlFunctionLoader.load! -> (nil | void)

Loads user defined SQL functions into database.

Parameters:

  • task_name (String|nil) (defaults to: nil)

    Name of the task.

Returns:

  • (nil)

    if there is no ‘db/functions` directory.

  • (void)

    if there is no errors.



14
15
16
17
18
19
20
21
# File 'lib/arfi/sql_function_loader.rb', line 14

def load!(task_name: nil)
  self.task_name = task_name[/([^:]+$)/] if task_name
  return puts 'No SQL files found. Skipping db population with ARFI' unless sql_files.any?

  raise_unless_supported_adapter
  handle_db_population
  conn.close
end

.multi_db?Boolean (private)

Arfi::SqlFunctionLoader#multi_db? -> Boolean

Checks if the application has been configured to work with multiple databases.

Returns:

  • (Boolean)


67
68
69
# File 'lib/arfi/sql_function_loader.rb', line 67

def multi_db?
  ActiveRecord::Base.configurations.configurations.count { _1.env_name == Rails.env } > 1 # steep:ignore NoMethod
end

.populate_dbvoid (private)

This method returns an undefined value.

Arfi::SqlFunctionLoader#populate_db -> void

Loads user defined SQL functions into database.



96
97
98
99
100
101
102
# File 'lib/arfi/sql_function_loader.rb', line 96

def populate_db
  sql_files.each do |file|
    sql = File.read(file).strip
    conn.execute(sql)
    puts "[ARFI] Loaded: #{File.basename(file)} into #{conn.pool.db_config.env_name} #{conn.pool.db_config.name}"
  end
end

.populate_multiple_dbvoid (private)

This method returns an undefined value.

Arfi::SqlFunctionLoader#populate_multiple_db -> void

Loads user defined SQL functions into all databases.

See Also:

  • #multi_db?
  • #populate_db


80
81
82
83
84
85
86
87
# File 'lib/arfi/sql_function_loader.rb', line 80

def populate_multiple_db
  # steep:ignore:start
  ActiveRecord::Base.configurations.configurations.select { _1.env_name == Rails.env }.each do |config|
    ActiveRecord::Base.establish_connection(config)
    populate_db
  end
  # steep:ignore:end
end

.raise_unless_supported_adaptervoid (private)

This method returns an undefined value.

Arfi::SqlFunctionLoader#raise_unless_supported_adapter -> void

Checks if the database adapter is supported.



35
36
37
38
39
40
41
# File 'lib/arfi/sql_function_loader.rb', line 35

def raise_unless_supported_adapter
  allowed = %w[ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
               ActiveRecord::ConnectionAdapters::Mysql2Adapter].freeze
  return if allowed.include?(conn.class.to_s) # steep:ignore ArgumentTypeMismatch

  raise Arfi::Errors::AdapterNotSupported
end

.sql_filesArray<String> (private)

Arfi::SqlFunctionLoader#sql_files -> Array<String>

Helper method to get list of SQL files. Here we check if we need to populate all databases or just one.

Returns:

  • (Array<String>)

    List of SQL files.

See Also:

  • #load!
  • #multi_db?
  • #sql_functions_by_adapter


114
115
116
117
118
119
120
# File 'lib/arfi/sql_function_loader.rb', line 114

def sql_files
  if task_name || multi_db?
    sql_functions_by_adapter
  else
    Dir.glob(Rails.root.join('db', 'functions').join('*.sql'))
  end
end

.sql_functions_by_adapterArray<String> (private)

Arfi::SqlFunctionLoader#sql_functions_by_adapter -> Array<String>

Helper method to get list of SQL files for specific database adapter.

Returns:

  • (Array<String>)

    List of SQL files.

Raises:



130
131
132
133
134
135
136
137
138
139
# File 'lib/arfi/sql_function_loader.rb', line 130

def sql_functions_by_adapter
  case conn
  when ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
    Dir.glob(Rails.root.join('db', 'functions', 'postgresql').join('*.sql'))
  when ActiveRecord::ConnectionAdapters::Mysql2Adapter
    Dir.glob(Rails.root.join('db', 'functions', 'mysql').join('*.sql'))
  else
    raise Arfi::Errors::AdapterNotSupported
  end
end