Sha256: 33a8118c6107961bdb797bcbc9a74806695699cef29edd5bd2a502541edf919a

Contents?: true

Size: 1.69 KB

Versions: 33

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module CoreExtensions
  module ActiveRecord
    module MigrationHelpers
      # 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")
      #
      def load_function(filename)
        load_sql_file(DatabaseObjectPaths.functions, filename)
      end

      # 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")
      #
      def load_trigger(filename)
        load_sql_file(DatabaseObjectPaths.triggers, filename)
      end

      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

      # Make sure to look in the host Rails app as well as in the engine
      class DatabaseObjectPaths
        class << self
          def triggers
            [
              Rails.root.join("db", "triggers"),
              Renalware::Engine.root.join("db", "triggers")
            ]
          end

          def functions
            [
              Rails.root.join("db", "functions"),
              Renalware::Engine.root.join("db", "functions")
            ]
          end
        end
      end
    end
  end
end

ActiveRecord::Migration[5.1].send(:prepend, CoreExtensions::ActiveRecord::MigrationHelpers)

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
renalware-core-2.0.64 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.63 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.62 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.61 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.60 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.58 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.57 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.56 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.55 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.54 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.53 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.52 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.51 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.50 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.48 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.47 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.46 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.45 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.44 lib/core_extensions/active_record/migration_helpers.rb
renalware-core-2.0.43 lib/core_extensions/active_record/migration_helpers.rb