Skip to content

Commit

Permalink
fix touch with sql update strategy
Browse files Browse the repository at this point in the history
note:
ActiveRecord 6.0 introduced `update_all(Hash)`
Fixing depth_cache_column support in a string is simple.
It is the escaping of dates that has me moving to the hash syntax.

Since Rails 5.2 support dropped 10 months ago,
Deciding to drop 5.2 update_strategy=sql support
  • Loading branch information
kbrock committed Apr 11, 2023
1 parent d7b577a commit dbd23f2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/run_test_suite.yml
Expand Up @@ -97,6 +97,7 @@ jobs:
UPDATE_STRATEGY: sql
run: |
bundle exec rake
if: ${{ matrix.activerecord != 52 }}
- name: run pg tests (ANCESTRY_COLUMN=ancestry_alt)
env:
DB: pg
Expand All @@ -110,6 +111,8 @@ jobs:
UPDATE_STRATEGY: sql
run: |
bundle exec rake
FORMAT=${{ matrix.format }} UPDATE_STRATEGY=sql bundle exec rake
if: ${{ matrix.activerecord != 52 }}
- name: run mysql tests
env:
DB: mysql2
Expand Down
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -46,8 +46,10 @@ When using `STI` all classes are returned from the scopes unless you specify oth
## Supported Rails versions

- Ancestry 2.x supports Rails 4.1 and earlier
- Ancestry 3.x supports Rails 5.0 and 4.2
- Ancestry 4.x only supports rails 5.2 and higher
- Ancestry 3.x supports Rails 4.2 and 5.0
- Ancestry 4.x supports Rails 5.2 through 7.0
- Ancestry 5.0 supports Rails 6.0 and higher
Rails 5.2 with `update_strategy=ruby` is still being tested in 5.0.

# Installation

Expand Down
15 changes: 10 additions & 5 deletions lib/ancestry/materialized_path_pg.rb
Expand Up @@ -7,20 +7,25 @@ def update_descendants_with_new_ancestry
if !ancestry_callbacks_disabled? && sane_ancestor_ids?
old_ancestry = self.class.generate_ancestry( path_ids_before_last_save )
new_ancestry = self.class.generate_ancestry( path_ids )
update_clause = [
"#{self.class.ancestry_column} = regexp_replace(#{self.class.ancestry_column}, '^#{Regexp.escape(old_ancestry)}', '#{new_ancestry}')"
]
update_clause = {
self.class.ancestry_column => Arel.sql("regexp_replace(#{self.class.ancestry_column}, '^#{Regexp.escape(old_ancestry)}', '#{new_ancestry}')")
}

current_time = current_time_from_proper_timezone
timestamp_attributes_for_update_in_model.each do |column|
update_clause[column] = current_time
end

if self.class.respond_to?(:depth_cache_column)
depth_cache_column = self.class.depth_cache_column
depth_change = self.class.ancestry_depth_change(old_ancestry, new_ancestry)

if depth_change != 0
update_clause << "#{depth_cache_column} = #{depth_cache_column} + #{depth_change}"
update_clause[depth_cache_column] = Arel.sql("#{depth_cache_column} + #{depth_change}")
end
end

unscoped_descendants_before_last_save.update_all update_clause.join(', ')
unscoped_descendants_before_last_save.update_all update_clause
end
end
end
Expand Down
1 change: 0 additions & 1 deletion test/concerns/touching_test.rb
Expand Up @@ -51,7 +51,6 @@ def test_touch_option_enabled_propagates_with_modification
end

def test_touch_propogates_multiple_levels
skip "no callbacks for sql update strategy" if Ancestry.default_update_strategy == :sql
AncestryTestDatabase.with_model(:extra_columns => {:name => :string, :updated_at => :datetime}, :touch => true) do |model|

way_back = Time.new(1984)
Expand Down

0 comments on commit dbd23f2

Please sign in to comment.