teoljungberg / fx

Versioned database functions and triggers for Rails
MIT License
762 stars 76 forks source link

Inconsistent function order on schema dumps #142

Closed albaer closed 9 months ago

albaer commented 9 months ago

Hi! Thanks for all your work on this gem. We ran into an issue where the schema dumper would occasionally dump the functions in a different order, resulting in a diff that didn't really reflect any changes. Our workaround is something like this:

# This monkey patches the F(x) gem so that the functions are always dumped in
# alphabetical order by name, preventing schema diffs when nothing has actually changed.
# Original code: https://github.com/teoljungberg/fx/blob/master/lib/fx/schema_dumper/function.rb
module Fx
  module SchemaDumper
    module Function
      def functions(stream)
        dumpable_functions_in_database.sort_by(&:name).each do |function|
          stream.puts(function.to_schema)
        end
      end
    end
  end
end

It could also be done in the dumpable_functions_in_database method though. If other people have this issue or if you think it would be helpful, I'd be happy to open a PR along these lines.

teoljungberg commented 9 months ago

Yeah ordering has been an unsolved problem for a really long time, and I haven't decided how I want to have it work. Ideally I'd want something topological to allow tables to have default values work in a decent way - and I don't want to maintain different sorting options.

Which is why I suggested to do a patch as the one you have above, as I deem this to be public API and probably won't change it.

teoljungberg commented 9 months ago

To be extra-safe - I'd suggest people applying a patch such as the above to subscribe for release-notifications to this project and keep track of they need to remove it once this gets some kind of upstream support.

albaer commented 9 months ago

Thanks for the response! That sounds good. I would also be happy to set it up as a configuration option (defaulted to false) so it wouldn't change the API, but I definitely see your point about not maintaining multiple options. If you end up wanting some help on this, I'd be happy to do some work, otherwise we'll just keep an eye on things. Thanks again for all your great work on the gem!