fx icon indicating copy to clipboard operation
fx copied to clipboard

Expose migration methods for getting the sql definition

Open bf4 opened this issue 5 years ago • 1 comments

Useful when you want to interact with the sql in various ways.

For example, I use `strong_migrations`, so I made a mixin for all my migrations to get the function sql.
  # Could be simplified after https://github.com/teoljungberg/fx/pull/52 is merged
  def db_update_function(function_name, version:, revert_to_version:, replace: true)
    if replace
      reversible do |dir|
        dir.up do
          safely_execute function_sql(function_name, version: version)
        end
        dir.down do
          safely_execute function_sql(function_name, version: revert_to_version)
        end
      end
    else
      safety_assured do
        update_function function_name version: version, revert_to_version: revert_to_version
      end
    end
  end

I expect you may want a different name for the methods than function_sql and trigger_sql, but am using that as a discussion point. Notably, even though the trigger generator requires a table name, it doesn't appear to be used. (I've noticed an old issue regarding this.)

Tests pass. bin/setup; bin/appraisal rake on CRuby 2.6.2.

bf4 avatar Oct 22 '20 02:10 bf4

Anything you need from me?

bf4 avatar Apr 12 '22 15:04 bf4

I can see this being useful for strong_migrations, but I'd we can already do this by:

Fx::Adapters::Postgres.new(connection).functions.find { |function| funcition.name == :test }.definition

or similar. But that should be enough until we have more use-cases for this.

teoljungberg avatar Jan 21 '23 12:01 teoljungberg