Module: CoreExtensions::ActiveRecord::MigrationHelpers
- Defined in:
- lib/core_extensions/active_record/migration_helpers.rb
Defined Under Namespace
Classes: DatabaseObjectPaths
Instance Method Summary collapse
-
#load_function(filename) ⇒ Object
Can be called from a migration to load in a function from a sql file at e.g.
- #load_sql_file(paths, filename) ⇒ Object
-
#load_trigger(filename) ⇒ Object
Can be called from a migration to load in a trigger from a sql file at e.g.
Instance Method Details
#load_function(filename) ⇒ Object
Can be called from a migration to load in a function from a sql file at e.g. db/functions/my_function_v01.sql
Example usage:
load_function("my_function_v01.sql")
12 13 14 |
# File 'lib/core_extensions/active_record/migration_helpers.rb', line 12 def load_function(filename) load_sql_file(DatabaseObjectPaths.functions, filename) end |
#load_sql_file(paths, filename) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/core_extensions/active_record/migration_helpers.rb', line 26 def load_sql_file(paths, filename) found = false paths.each do |path| file_path = path.join(filename) if File.exist?(file_path) connection.execute(File.read(file_path)) found = true end end unless found raise "Cannot file #{filename} in #{paths.join(', ')}" end end |
#load_trigger(filename) ⇒ Object
Can be called from a migration to load in a trigger from a sql file at e.g. db/functions/my_trigger_v01.sql
Example usage:
load_trigger("my_trigger_v01.sql")
22 23 24 |
# File 'lib/core_extensions/active_record/migration_helpers.rb', line 22 def load_trigger(filename) load_sql_file(DatabaseObjectPaths.triggers, filename) end |