Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

ProviderMerchant does not support color (#3725)

Mikael Møller600d57e8b610
diff --git a/app/models/family/data_exporter.rb b/app/models/family/data_exporter.rb
index b02ad94ba2..fef900f2c4 100644
--- a/app/models/family/data_exporter.rb
+++ b/app/models/family/data_exporter.rb
@@ -315,7 +315,8 @@ class Family::DataExporter
# Export provider-assigned merchants (auto-detected by bank sync/AI enrichment)
# referenced by this family's transactions and recurring transactions. These
# are shared across every family on the instance, so only the ones actually
- # in use here are included -- see #3113.
+ # in use here are included -- see #3113. ProviderMerchant does not support
+ # color, so none is exported for it.
referenced_provider_merchants.each do |merchant|
lines << {
type: "ProviderMerchant",
@@ -324,7 +325,6 @@ class Family::DataExporter
name: merchant.name,
source: merchant.source,
provider_merchant_id: merchant.provider_merchant_id,
- color: merchant.color,
logo_url: merchant.logo_url,
website_url: merchant.website_url,
created_at: merchant.created_at,
diff --git a/app/models/family/data_importer.rb b/app/models/family/data_importer.rb
index 04c1c49867..711e87416d 100644
--- a/app/models/family/data_importer.rb
+++ b/app/models/family/data_importer.rb
@@ -528,6 +528,7 @@ class Family::DataImporter
end
end
+ # ProviderMerchant does not support color, so a color in the file is not read.
# The savepoint keeps a lost race from aborting the surrounding import transaction.
# A race usually surfaces as the name-uniqueness validation (RecordInvalid) rather
# than the index (RecordNotUnique); either way, reuse the winner if it exists and
@@ -538,7 +539,6 @@ class Family::DataImporter
name: data["name"],
source: source,
provider_merchant_id: data["provider_merchant_id"].presence,
- color: data["color"],
logo_url: data["logo_url"],
website_url: data["website_url"]
)
diff --git a/app/models/provider_merchant.rb b/app/models/provider_merchant.rb
index 97e7b723ea..f88e7bdfb9 100644
--- a/app/models/provider_merchant.rb
+++ b/app/models/provider_merchant.rb
@@ -10,7 +10,7 @@ class ProviderMerchant < Merchant
by_provider_id || find_by(name: data["name"], source: source)
end
- # color is deliberately not compared: nothing ever sets it on a ProviderMerchant.
+ # color is not compared: ProviderMerchant does not support color.
def import_diff(data)
%w[website_url name provider_merchant_id].filter_map do |field|
imported_value = data[field].presence
@@ -20,6 +20,27 @@ class ProviderMerchant < Merchant
end
end
+ # ProviderMerchant does not support color. merchants.color is a legacy column from
+ # when every merchant was family-owned, and FamilyMerchant is the only type that
+ # uses it. It is switched off in three overlapping places, so no path can store or
+ # show one for this type:
+ #
+ # ProviderMerchant does not support color: reads are always nil, so a stale value
+ # on an old row never reaches a view, and writes are discarded.
+ def color = nil
+
+ def color=(_value)
+ super(nil)
+ end
+
+ # ProviderMerchant does not support color: this clears a stale stored value so the
+ # next save writes NULL (see NullProviderMerchantColors for the one-off cleanup).
+ before_validation { self.color = nil }
+
+ # ProviderMerchant does not support color: states the contract. It can't fail today,
+ # because the accessors and the hook above have already discarded any value.
+ validates :color, absence: true
+
# Convert this ProviderMerchant to a FamilyMerchant for a specific family.
# Only affects transactions belonging to that family.
# Returns the newly created FamilyMerchant.
diff --git a/db/migrate/20260925220151_null_provider_merchant_colors.rb b/db/migrate/20260925220151_null_provider_merchant_colors.rb
new file mode 100644
index 0000000000..e7fe5847db
--- /dev/null
+++ b/db/migrate/20260925220151_null_provider_merchant_colors.rb
@@ -0,0 +1,22 @@
+# ProviderMerchant does not support color. merchants.color is a legacy column from
+# when every merchant was family-owned (it was `null: false` with a default); the
+# migration that introduced the shared ProviderMerchant type made it nullable and
+# dropped the default, and nothing since has set it for that type. FamilyMerchant
+# is the only type that uses it, so its rows are left alone.
+#
+# The model now discards any color written to a ProviderMerchant, so this only
+# tidies rows that predate that.
+class NullProviderMerchantColors < ActiveRecord::Migration[8.1]
+ def up
+ execute <<~SQL
+ UPDATE merchants
+ SET color = NULL
+ WHERE type = 'ProviderMerchant'
+ AND color IS NOT NULL
+ SQL
+ end
+
+ # A ProviderMerchant has no color to put back.
+ def down
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 972a883b43..47eb7613d4 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[8.1].define(version: 2026_09_24_140000) do
+ActiveRecord::Schema[8.1].define(version: 2026_09_25_220151) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "pgcrypto"
diff --git a/test/migrations/null_provider_merchant_colors_migration_test.rb b/test/migrations/null_provider_merchant_colors_migration_test.rb
new file mode 100644
index 0000000000..8615f6ab30
--- /dev/null
+++ b/test/migrations/null_provider_merchant_colors_migration_test.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+require "test_helper"
+require Rails.root.join("db/migrate/20260925220151_null_provider_merchant_colors")
+
+class NullProviderMerchantColorsMigrationTest < ActiveSupport::TestCase
+ setup do
+ @provider_merchant = ProviderMerchant.create!(name: "Legacy", source: "plaid")
+ @provider_merchant.update_column(:color, "#654321")
+ @family_merchant = families(:dylan_family).merchants.create!(name: "Mine", color: "#4da568")
+ end
+
+ test "clears the color stored on provider merchants" do
+ run_migration
+
+ assert_nil ProviderMerchant.where(id: @provider_merchant.id).pick(:color)
+ end
+
+ test "leaves family merchants and everything else on the row alone" do
+ run_migration
+
+ assert_equal "#4da568", @family_merchant.reload.color
+ assert_equal "Legacy", ProviderMerchant.find(@provider_merchant.id).name
+ end
+
+ test "can be run again" do
+ 2.times { run_migration }
+
+ assert_nil ProviderMerchant.where(id: @provider_merchant.id).pick(:color)
+ end
+
+ private
+
+ def run_migration
+ ActiveRecord::Migration.suppress_messages do
+ NullProviderMerchantColors.new.up
+ end
+ end
+end
diff --git a/test/models/family/data_exporter_test.rb b/test/models/family/data_exporter_test.rb
index 39b9c786e7..dd633c6858 100644
--- a/test/models/family/data_exporter_test.rb
+++ b/test/models/family/data_exporter_test.rb
@@ -884,6 +884,21 @@ class Family::DataExporterTest < ActiveSupport::TestCase
end
end
+ test "does not export a color for a provider merchant" do
+ provider_merchant = ProviderMerchant.create!(name: "Colorless", source: "plaid")
+ @account.entries.create!(
+ date: Date.parse("2024-05-02"), amount: 5, name: "Colorless purchase", currency: "USD",
+ entryable: Transaction.new(merchant: provider_merchant)
+ )
+
+ Zip::File.open_buffer(@exporter.generate_export) do |zip|
+ data = zip.read("all.ndjson").split("\n").map { |line| JSON.parse(line) }
+ .find { |record| record["type"] == "ProviderMerchant" && record.dig("data", "id") == provider_merchant.id }["data"]
+
+ assert_not data.key?("color")
+ end
+ end
+
test "exports provider merchants referenced by recurring transactions in NDJSON" do
provider_merchant = ProviderMerchant.create!(name: "Recurring Provider Merchant", source: "ai")
@family.recurring_transactions.create!(
diff --git a/test/models/provider_merchant_test.rb b/test/models/provider_merchant_test.rb
index d08e4d5d61..e01f1ff2f9 100644
--- a/test/models/provider_merchant_test.rb
+++ b/test/models/provider_merchant_test.rb
@@ -74,4 +74,35 @@ class ProviderMerchantTest < ActiveSupport::TestCase
)
assert_empty without_id.import_diff({ "name" => "Acme" })
end
+
+ test "does not support color: writes are discarded and the stored value is NULL" do
+ assert_nil ProviderMerchant.new(name: "New", source: "plaid", color: "#123456").color
+
+ created = ProviderMerchant.create!(name: "Colored", source: "plaid", color: "#123456")
+
+ assert_nil created.color
+ assert_nil ProviderMerchant.where(id: created.id).pick(:color)
+ end
+
+ test "does not support color: a stale value on an old row is never read and is cleared on the next save" do
+ legacy = ProviderMerchant.create!(name: "Legacy", source: "plaid")
+ legacy.update_column(:color, "#654321")
+
+ assert_nil ProviderMerchant.find(legacy.id).color, "a stale value must not reach a view"
+ assert_equal "#654321", ProviderMerchant.where(id: legacy.id).pick(:color), "still stored until the row is saved"
+
+ ProviderMerchant.find(legacy.id).update!(website_url: "https://legacy.example")
+
+ assert_nil ProviderMerchant.where(id: legacy.id).pick(:color)
+ assert_equal "https://legacy.example", legacy.reload.website_url
+ end
+
+ test "family merchants are unaffected and converting a provider merchant still gives the copy a color" do
+ assert_match(/\A#[0-9A-Fa-f]{6}\z/, @family.merchants.create!(name: "Mine").color)
+
+ converted = @provider_merchant.convert_to_family_merchant_for(@family, name: "Acme Mine", color: "#4da568")
+
+ assert_equal "#4da568", converted.color
+ assert_nil @provider_merchant.reload.color
+ end
end
diff --git a/test/models/sure_import_test.rb b/test/models/sure_import_test.rb
index 3f040383a5..5dd7cbc2dc 100644
--- a/test/models/sure_import_test.rb
+++ b/test/models/sure_import_test.rb
@@ -863,6 +863,17 @@ class SureImportTest < ActiveSupport::TestCase
assert_equal existing.id, @family.entries.find_by!(name: "Amazon purchase").entryable.merchant_id
end
+ test "a color in the file is not read when a provider merchant is created" do
+ attach_ndjson(provider_merchant_ndjson(color: "#123456"))
+
+ assert_difference -> { ProviderMerchant.count }, 1 do
+ @import.publish
+ end
+
+ assert_equal "complete", @import.status
+ assert_nil ProviderMerchant.where(name: "AMZN MKTP", source: "plaid").pick(:color)
+ end
+
{
"RecordNotUnique" => -> { ActiveRecord::RecordNotUnique.new("duplicate key") },
"RecordInvalid" => -> { ActiveRecord::RecordInvalid.new(ProviderMerchant.new.tap { |merchant| merchant.errors.add(:name, :taken) }) }