bvanelli / actualpy

Python API implementation for Actual server - reference https://actualbudget.org/
20 stars 4 forks source link

SimpleFin column is None killing apply_changes function. #20

Closed JaxOfDiamonds closed 3 months ago

JaxOfDiamonds commented 3 months ago

simpleFin creates attributes with None for the column. This causes the setattr(entry, column, message.get_value()) to fail.

Snippet from apply_changes with my temporary fix.

    def apply_changes(self, messages: list[Message]):
        """Applies a list of sync changes, based on what the sync method returned on the remote."""
        if not self.session_maker:
            raise UnknownFileId("No valid file available, download one with download_budget()")
        with self.session_maker() as s:
            for message in messages:
                if message.dataset == "prefs":
                    # write it to metadata.json instead
                    self.update_metadata({message.row: message.get_value()})
                    continue
                table = get_class_by_table_name(message.dataset)
                if table is None:
                    raise ActualError(
                        f"Actual is at a version not supported by the library: '{message.dataset}' not found"
                    )
                column = get_attribute_by_table_name(message.dataset, message.column)
                entry = s.get(table, message.row)
                if not entry:
                    entry = table(id=message.row)

>>>>>
                if 'simpleFin' == message.get_value():
                    print('-----------------')
                    print(entry)
                    print(column)
                    print(message.get_value())
                    print('=================')
                    continue
>>>>>

                setattr(entry, column, message.get_value())
                s.add(entry)
                # this seems to be required for sqlmodel, remove if not needed anymore when querying from cache
                s.flush()
            s.commit()

Sample output from above code addition:

-----------------
id='382u498237489273' account_id='ACT-23904309242030' name='Signature Visa' balance_current=None balance_available=None balance_limit=None mask=None official_name='Signature Visa' subtype=None bank='2839470293844' offbudget=0 closed=0 tombstone=0 sort_order=None type=None
None
simpleFin
=================
bvanelli commented 3 months ago

Thanks for the report, I missed that on my model, apparently this migration was added 3 months ago, while the first model is from somewhere end of last year.

https://github.com/actualbudget/actual/blob/b803731bc52dbf3628b2129c7881cebabd24488e/packages/loot-core/migrations/1704572023730_add_account_sync_source.sql#L3

I updated the models and changed the error to be more descriptive of what is missing.

bvanelli commented 3 months ago

I merged the changes, feel free to reopen the issue if you still encounter the same problem on main branch.