Class: Spree::CategoryPermalinkDeduplicator

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/category_permalink_deduplicator.rb

Overview

Resolves duplicate category permalinks within a store.

Before 6.0 a permalink only had to be unique within its taxonomy, so one store could legitimately hold two "catalog" roots under different taxonomies. In 6.0 the permalink is the category's full path and is unique per store, so those rows have to be separated before the unique index can exist.

Runs on raw SQL rather than through Spree::Category: it is called from a migration, where the model's callbacks, validations and default scopes may not match the schema on disk.

Constant Summary collapse

DUPLICATE_GROUPS_WITH_TAXONOMY_SQL =

Spelled out per schema rather than interpolated from #effective_store_id: this query binds no values, so writing it as constants keeps it provably free of interpolated input (Brakeman flags the assembled form). The queries below interpolate the same fragments but bind their values via #sanitize.

<<~SQL.freeze
  SELECT COALESCE(c.store_id, t.store_id) AS sid, c.permalink
  FROM spree_categories c
  LEFT JOIN spree_taxonomies t ON t.id = c.taxonomy_id
  WHERE c.permalink IS NOT NULL AND COALESCE(c.store_id, t.store_id) IS NOT NULL
  GROUP BY COALESCE(c.store_id, t.store_id), c.permalink
  HAVING COUNT(*) > 1
SQL
TRANSLATION_GROUPS_WITH_TAXONOMY_SQL =
<<~SQL.freeze
  SELECT COALESCE(c.store_id, t.store_id) AS sid, tr.locale, tr.permalink
  FROM spree_category_translations tr
  INNER JOIN spree_categories c ON c.id = tr.spree_category_id
  LEFT JOIN spree_taxonomies t ON t.id = c.taxonomy_id
  WHERE tr.permalink IS NOT NULL AND COALESCE(c.store_id, t.store_id) IS NOT NULL
  GROUP BY COALESCE(c.store_id, t.store_id), tr.locale, tr.permalink
  HAVING COUNT(*) > 1
SQL
TRANSLATION_GROUPS_SQL =
<<~SQL.freeze
  SELECT c.store_id AS sid, tr.locale, tr.permalink
  FROM spree_category_translations tr
  INNER JOIN spree_categories c ON c.id = tr.spree_category_id
  WHERE tr.permalink IS NOT NULL AND c.store_id IS NOT NULL
  GROUP BY c.store_id, tr.locale, tr.permalink
  HAVING COUNT(*) > 1
SQL
DUPLICATE_GROUPS_SQL =
<<~SQL.freeze
  SELECT c.store_id AS sid, c.permalink
  FROM spree_categories c
  WHERE c.permalink IS NOT NULL AND c.store_id IS NOT NULL
  GROUP BY c.store_id, c.permalink
  HAVING COUNT(*) > 1
SQL

Instance Method Summary collapse

Constructor Details

#initialize(connection = ActiveRecord::Base.connection) ⇒ CategoryPermalinkDeduplicator

Returns a new instance of CategoryPermalinkDeduplicator.

Parameters:

  • connection (ActiveRecord::ConnectionAdapters::AbstractAdapter) (defaults to: ActiveRecord::Base.connection)


56
57
58
59
# File 'lib/spree/category_permalink_deduplicator.rb', line 56

def initialize(connection = ActiveRecord::Base.connection)
  @connection = connection
  @permalinks_by_store = {}
end

Instance Method Details

#call ⇒ Integer

Rewrites every duplicate permalink, keeping the lowest-id row in each group and re-prefixing each rewritten row's descendants so subtree paths stay consistent with their parents.

Returns:

  • (Integer) —

    number of categories whose permalink changed



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/spree/category_permalink_deduplicator.rb', line 66

def call
  return 0 unless connection.table_exists?('spree_categories')

  renamed = 0

  # Re-queried each round rather than snapshotted once: re-prefixing a
  # subtree can move a descendant onto a path some other row already holds,
  # and that collision has to be resolved too or the unique index cannot be
  # created. Each round strictly reduces the duplicates, so this terminates.
  while (groups = duplicate_groups).any?
    # A cascade rewrites descendants directly, so the cached permalink sets
    # are stale by the end of a round.
    @permalinks_by_store.clear
    moved = resolve_groups(groups)
    break if moved.zero? # defensive: never spin if a group cannot be moved

    renamed += moved
  end

  # Independent of the base pass: a store whose base permalinks are already
  # unique can still hold colliding translated ones, and the storefront
  # resolves categories through that table.
  renamed + rewrite_translations
end