teoljungberg / fx

Versioned database functions and triggers for Rails
MIT License
763 stars 77 forks source link

Add support to replace functions with dependencies #52

Closed sobrinho closed 1 year ago

sobrinho commented 4 years ago

This will add support to update functions without dropping and recreating dependencies.

sobrinho commented 4 years ago

@teoljungberg in other words, today we have to do this:

def up
  drop_trigger :blah, on: :blah
  update_function :blah, version: 2
  create_trigger :blah, on: :blah
end

def down
  drop_trigger :blah, on: :blah
  update_function :blah, version: 1
  create_trigger :blah, on: :blah
end

And after the PR, we just do:

def change
  update_function :blah, version: 2, revert_to_version: 1, replace: true
end
bf4 commented 4 years ago

I was just hacking the gem for the feature in this PR. I'm happy to take it over if OP @sobrinho is taking a break.

Basic impl looks good to me

sobrinho commented 4 years ago

I will do the requested changes in a couple of hours :)

bf4 commented 4 years ago

I'm happy to take this over if it's gonna be stale much longer. I've added some work arounds in my app, but I'd like to use it :)

teoljungberg commented 4 years ago

Please do @bf4. I'd like this feature too.

sobrinho commented 4 years ago

@teoljungberg would you review it again?

Sorry about the delay, I have been busy in the last days.

teoljungberg commented 1 year ago

As we can do this today, in a bit of a clunkier fashion, I think it's fine and we can leave this as is.

sobrinho commented 1 year ago

@teoljungberg this is a small effort in terms of code and maintenance in exchange of a big convenience for heavy users. Would you reconsider it?

teoljungberg commented 1 year ago

I'd love to hear from others to reconsider it, and as I said

As we can do this today, in a bit of a clunkier fashion, I think it's fine and we can leave this as is.

I don't see this as something that needs to be considered right now. Without having more input.

bf4 commented 1 year ago

@teoljungberg for me, I've worked around this by writing my own update functions as described in #63

  # 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

if this PR (or something like it) were merged, even as an opt-in (rather than opt-out), I could change my code to

  def db_update_function(function_name, version:, revert_to_version:, replace: true)
    safety_assured do
      update_function function_name version: version, revert_to_version: revert_to_version, replace: replace
    end
  end

because we use CREATE OR REPLACE FUNCTION for all our function files

maybe call drop_first: true as your default on update_function to make it opt in and descriptive?

teoljungberg commented 1 year ago

Thanks for the additional input @bf4 - good to have for future context.

trcarden commented 1 year ago

I'd love to hear from others to reconsider it, and as I said

As we can do this today, in a bit of a clunkier fashion, I think it's fine and we can leave this as is.

I don't see this as something that needs to be considered right now. Without having more input.

@teoljungberg here is a bit more input:

I am going to workaround by just calling create_function again (much like this PR does) directly since the SQL for creation has the replacement statements built in.