Module: CoreExtensions::ActiveRecord::MigrationHelpers

Defined in:
lib/core_extensions/active_record/migration_helpers.rb

Defined Under Namespace

Classes: DatabaseObjectPaths

Instance Method Summary collapse

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")


62
63
64
# File 'lib/core_extensions/active_record/migration_helpers.rb', line 62

def load_function(filename)
  load_sql_file(DatabaseObjectPaths.functions, filename)
end

#load_sql_file(paths, filename) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/core_extensions/active_record/migration_helpers.rb', line 76

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
  raise "Cannot file #{filename} in #{paths.join(', ')}" unless found
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")


72
73
74
# File 'lib/core_extensions/active_record/migration_helpers.rb', line 72

def load_trigger(filename)
  load_sql_file(DatabaseObjectPaths.triggers, filename)
end

#within_public_schemaObject



49
50
51
52
53
54
# File 'lib/core_extensions/active_record/migration_helpers.rb', line 49

def within_public_schema
  original_schema_search_path = connection.schema_search_path
  connection.schema_search_path = "public"
  yield if block_given?
  connection.schema_search_path = original_schema_search_path
end

#within_renalware_schema(suffix: nil) ⇒ Object

Used in migrations to ensure the objects created/updated/found are in the correct schema. We ensure that the original schema_search_path defined in the database.yml in the host app is restored - this particularyl important for migations in an engine.

Within this renalware core engine, use:

def change

within_renalware_schema do
  ...make database changes
  ...any new objects will be created in the renalware schema
end

end

Within another engine eg renalware-diaverum:

def change

within_renalware_schema(:diaverum) do
  ...make database changes
  ...any new objects will be created in the renalware_diaverum schema
end

end

If using from a host app eg renalware_blt (actualy this is optional):

def change

within_renalware_schema(:blt) do
  ...make database changes
  ...any new objects will be created in the renalware_blt schema
end

end



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/core_extensions/active_record/migration_helpers.rb', line 37

def within_renalware_schema(suffix: nil)
  schemas = if suffix.present?
              "renalware_#{suffix},renalware,public"
            else
              "renalware,public"
            end
  original_schema_search_path = connection.schema_search_path
  connection.schema_search_path = schemas
  yield if block_given?
  connection.schema_search_path = original_schema_search_path
end