Files
sure/db/migrate/20260115100001_backfill_entry_protection_flags.rb
LPW c391ba2b23 Harden SimpleFIN sync: protect user data, fix stuck syncs, optimize API calls (#671)
* Implement entry protection flags for sync overwrites

- Added `user_modified` and `import_locked` flags to `entries` table to prevent provider sync from overwriting user-edited and imported data.
- Introduced backfill migration to mark existing entries based on conditions.
- Enhanced sync and processing logic to respect protection flags, track skipped entries, and log detailed stats.
- Updated UI to display skipped/protected entries and reasons in sync summaries.

* Localize error details summary text and adjust `sync_account_later` method placement

* Restored schema.rb

---------

Co-authored-by: luckyPipewrench <luckypipewrench@proton.me>
2026-01-16 12:34:06 +01:00

35 lines
1.2 KiB
Ruby

class BackfillEntryProtectionFlags < ActiveRecord::Migration[7.2]
disable_ddl_transaction!
def up
# Backfill import_locked for entries that came from CSV/manual imports
# These entries have import_id set but typically no external_id or source
say_with_time "Marking CSV-imported entries as import_locked" do
execute <<-SQL.squish
UPDATE entries
SET import_locked = true
WHERE import_id IS NOT NULL
AND import_locked = false
SQL
end
# Backfill user_modified for entries where user has manually edited fields
# These entries have non-empty locked_attributes (set when user edits)
say_with_time "Marking user-edited entries as user_modified" do
execute <<-SQL.squish
UPDATE entries
SET user_modified = true
WHERE locked_attributes != '{}'::jsonb
AND locked_attributes IS NOT NULL
AND user_modified = false
SQL
end
end
def down
# Reversible but generally not needed
execute "UPDATE entries SET import_locked = false WHERE import_locked = true"
execute "UPDATE entries SET user_modified = false WHERE user_modified = true"
end
end