Class: Arfi::SqlFunctionLoader
- Inherits:
-
Object
- Object
- Arfi::SqlFunctionLoader
- 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
-
.task_name ⇒ Object
private
Returns the value of attribute task_name.
Class Method Summary collapse
-
.conn ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
private
Arfi::SqlFunctionLoader#conn -> ActiveRecord::ConnectionAdapters::AbstractAdapter.
-
.handle_db_population ⇒ void
private
Arfi::SqlFunctionLoader#handle_db_population -> void.
-
.load!(task_name: nil) ⇒ nil, void
Arfi::SqlFunctionLoader.load!-> (nil | void). -
.multi_db? ⇒ Boolean
private
Arfi::SqlFunctionLoader#multi_db? -> Boolean.
-
.populate_db ⇒ void
private
Arfi::SqlFunctionLoader#populate_db -> void.
-
.populate_multiple_db ⇒ void
private
Arfi::SqlFunctionLoader#populate_multiple_db -> void.
-
.raise_unless_supported_adapter ⇒ void
private
Arfi::SqlFunctionLoader#raise_unless_supported_adapter -> void.
-
.sql_files ⇒ Array<String>
private
Arfi::SqlFunctionLoader#sql_files -> Array<String>.
-
.sql_functions_by_adapter ⇒ Array<String>
private
Arfi::SqlFunctionLoader#sql_functions_by_adapter -> Array<String>.
Class Attribute Details
.task_name ⇒ Object (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
.conn ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter (private)
Arfi::SqlFunctionLoader#conn -> ActiveRecord::ConnectionAdapters::AbstractAdapter
Helper method to get 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_population ⇒ void (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.
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.
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_db ⇒ void (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_db ⇒ void (private)
This method returns an undefined value.
Arfi::SqlFunctionLoader#populate_multiple_db -> void
Loads user defined SQL functions into all databases.
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_adapter ⇒ void (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_files ⇒ Array<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.
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_adapter ⇒ Array<String> (private)
Arfi::SqlFunctionLoader#sql_functions_by_adapter -> Array<String>
Helper method to get list of SQL files for specific database adapter.
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 |