Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ordering of triggers by name #251

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## master (unreleased)

- Support sorting of trigger names alphabetically (defaults to false)

```ruby
Logdize.sort_triggers_by_name = true
```

## 1.3.0 (2024-01-09)

- Add retrieving list of versions support. ([@tagirahmad][])
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,16 @@ The `log_data` column has the following format:

If you specify the limit in the trigger definition, then log size will not exceed the specified size. When a new change occurs, and there is no more room for it, the two oldest changes will be merged.

## Ordering of Triggers in schema.rb

By default, when generating `schema.rb`, Rails will order the triggers based on the id's of their respective tables. This can lead to unnecessary changes being made when utilizing `rails db:prepare`, since the ordering of the tables will now be based off the alphabetical ordering (see [#250](https://github.com/palkan/logidze/issues/250) for more details). To force the ordering to be consistent with `rails db:prepare`, Logidze can be configured to order the triggers alphabetically.

```ruby
# config/initializers/logidze.rb

Logidze.sort_triggers_by_name = true
```

## Troubleshooting

### `log_data` is nil when using Rails fixtures
Expand Down
3 changes: 3 additions & 0 deletions lib/logidze.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class << self
attr_accessor :ignore_log_data_by_default
# Whether #at should return self or nil when log_data is nil
attr_accessor :return_self_if_log_data_is_empty
# Determines if triggers are sorted by related table id or by name
attr_accessor :sort_triggers_by_name
# Determines what Logidze should do when upgrade is needed (:raise | :warn | :ignore)
attr_reader :on_pending_upgrade

Expand Down Expand Up @@ -68,4 +70,5 @@ def with_logidze_setting(name, value)
self.ignore_log_data_by_default = false
self.return_self_if_log_data_is_empty = true
self.on_pending_upgrade = :ignore
self.sort_triggers_by_name = false
end
13 changes: 13 additions & 0 deletions lib/logidze/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,18 @@ class Engine < Rails::Engine # :nodoc:
end
end
end

initializer "sort triggers by name" do |app|
if config.logidze.sort_triggers_by_name
ActiveSupport.on_load(:active_record) do
require "fx/adapters/postgres/triggers"
Fx::Adapters::Postgres::Triggers.singleton_class.prepend(Module.new do
def all(*args)
super.sort_by(&:name)
end
end)
end
end
end
end
end
Loading