class RevokeVariantOverrideswithoutPermissions < ActiveRecord::Migration
  def up
    # This process was executed when the permission_revoked_at colum was created (see AddPermissionRevokedAtToVariantOverrides)
    #   It needs to be repeated due to #2739
    variant_override_hubs = Enterprise.where(id: VariantOverride.all.map(&:hub_id).uniq)

    variant_override_hubs.each do |hub|
      permitting_producer_ids = hub.relationships_as_child
        .with_permission(:create_variant_overrides).map(&:parent_id)

      variant_overrides_with_revoked_permissions = VariantOverride.for_hubs(hub)
        .joins(variant: :product).where("spree_products.supplier_id NOT IN (?)", permitting_producer_ids)

      variant_overrides_with_revoked_permissions.update_all(permission_revoked_at: Time.now)
    end
  end
end

class RevokeVariantOverrideswithoutPermissions < ActiveRecord::Migration
  def up
    # This process was executed when the permission_revoked_at colum was created (see AddPermissionRevokedAtToVariantOverrides)
    #   It needs to be repeated due to #2739
    variant_override_hubs = Enterprise.where(id: VariantOverride.select(:hub_id).uniq)

    variant_override_hubs.find_each do |hub|
      permitting_producer_ids = hub.relationships_as_child
        .with_permission(:create_variant_overrides).pluck(:parent_id)

      variant_overrides_with_revoked_permissions = VariantOverride.for_hubs(hub)
        .joins(variant: :product).where("spree_products.supplier_id NOT IN (?)", permitting_producer_ids)

      variant_overrides_with_revoked_permissions.update_all(permission_revoked_at: Time.now)
    end
  end
end

Speed up database queries and make them scale
Speed up database queries and make them scale

This commit makes use of three ActiveRecord features:

1. Using `select` instead of `all.map` enables ActiveRecord to nest one
select into the other, resulting in one more efficient query instead of
two.

2. Using `find_each` saves memory by loading records in batches.
https://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-find_each

3. Using `pluck` creates only an array, avoiding loading all the other
columns of the records into objects.

Running this on the current Canadian database, fixes the following
variant overrides:

```
[]
[]
[]
[]
[]
[]
[925, 924, 966, 965]
[]
[]
[]
[]
[462,
 863,
 464,
 822,
 949,
 947,
 944,
 939,
 942,
 946,
 945,
 943,
 438,
 937,
 938,
 941,
 940,
 467,
 952,
 875,
 453,
 953,
 454,
 951,
 487,
 460,
 457,
 528,
 527,
 486,
 459,
 458,
 461,
 529,
 530,
 950,
 642,
 384,
 380,
 643,
 385,
 381,
 644,
 386,
 382,
 960,
 959,
 379,
 640,
 377,
 375,
 532,
 639,
 376,
 374,
 646,
 390,
 389,
 637,
 406,
 408,
 647,
 391,
 393,
 633,
 396,
 400,
 398,
 645,
 388,
 387,
 648,
 394,
 392,
 536,
 632,
 399,
 397,
 395,
 634,
 403,
 401,
 635,
 404,
 402,
 636,
 407,
 405,
 535,
 534,
 638,
 410,
 409,
 948,
 533,
 537,
 531,
 877,
 880,
 894,
 893,
 672,
 671,
 673,
 674,
 703,
 714,
 715,
 716,
 717,
 862,
 864,
 879,
 876,
 865,
 881,
 878,
 463,
 954,
 866,
 823,
 957,
 958,
 955,
 956,
 899,
 897]
[]
[969]
```
