{"action":"created","issue":{"url":"https://api.github.com/repos/odoo/odoo/issues/25551","repository_url":"https://api.github.com/repos/odoo/odoo","labels_url":"https://api.github.com/repos/odoo/odoo/issues/25551/labels{/name}","comments_url":"https://api.github.com/repos/odoo/odoo/issues/25551/comments","events_url":"https://api.github.com/repos/odoo/odoo/issues/25551/events","html_url":"https://github.com/odoo/odoo/issues/25551","id":337236231,"node_id":"MDU6SXNzdWUzMzcyMzYyMzE=","number":25551,"title":"Apply monkey patch through _register_hook on multi-db Instance. Worker <-> Registry Relationship?","user":{"login":"BT-kaberer","id":14974044,"node_id":"MDQ6VXNlcjE0OTc0MDQ0","avatar_url":"https://avatars2.githubusercontent.com/u/14974044?v=4","gravatar_id":"","url":"https://api.github.com/users/BT-kaberer","html_url":"https://github.com/BT-kaberer","followers_url":"https://api.github.com/users/BT-kaberer/followers","following_url":"https://api.github.com/users/BT-kaberer/following{/other_user}","gists_url":"https://api.github.com/users/BT-kaberer/gists{/gist_id}","starred_url":"https://api.github.com/users/BT-kaberer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BT-kaberer/subscriptions","organizations_url":"https://api.github.com/users/BT-kaberer/orgs","repos_url":"https://api.github.com/users/BT-kaberer/repos","events_url":"https://api.github.com/users/BT-kaberer/events{/privacy}","received_events_url":"https://api.github.com/users/BT-kaberer/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-30T19:05:23Z","updated_at":"2018-07-02T01:03:59Z","closed_at":"2018-07-02T01:03:59Z","author_association":"CONTRIBUTOR","body":"Impacted versions:\r\n11.0\r\nSteps to reproduce:\r\nIt's a general question how to safely apply monkey patches on an odoo instance\r\n\r\n  ```\r\n  @api.model_cr\r\n    def _register_hook(self):\r\n        \"\"\" MonkeyPatch method only when module is installed on the DB.\r\n        The original function pointer to name_get is stored in 'origin'\r\n        attribute.\r\n        The patch need to be removed when module is uninstalled. This is\r\n        done in the uninstall hook during runtime. No server restart\r\n        needed.\r\n\r\n        \"\"\"\r\n        res = super(ResPartnerExt, self)._register_hook()\r\n\r\n        @api.multi\r\n        def name_get(self):\r\n            #.....\r\n            return res\r\n\r\n        origin = getattr(Partner.name_get, 'origin', None)\r\n        if origin != name_get:\r\n            Partner._patch_method('name_get', name_get)\r\n            _logger.info('ResPartner.name_get method patched!')\r\n\r\n        return res\r\n```\r\n\r\nAlso there exist a uninstall hook, to remove the monkey patch during runtime:\r\n\r\n```\r\ndef uninstall_hook(cr, registry):\r\n    # If the method was patched we revert it during runtime\r\n    if hasattr(Partner.name_get, 'origin'):\r\n        Partner._revert_method('name_get')\r\n```\r\n\r\nTo summarize:\r\n1) Monkey patch only applied if the module is actually installed on the DB\r\n2) Monkey patch will be removed during runtime when module is uninstalled\r\n\r\nBut:\r\nNow imagine a multi DB instance where the monkey patch module is installed on DB1 but not on DB2.\r\nThis seems to work if the DB's are separated (--dbfilter) and the user cannot switch the DB manually.\r\n\r\nHowever there is a case that is problematic:\r\nSo the user login to DB1 with the monkey patch applied. Then logs out and changes to DB2. The registry is reloaded but the monkey patch is still applied on DB2.\r\n\r\nI am not fully aware of the worker-registry relationship, so my question might seem naive. Any hint to figure this out is kindly appreciated!\r\n\r\n**What is the cleanest way to apply monkey patch on multi worker/multi DB instance without side effects?**\r\n\r\nThanks for your explanations."},"comment":{"url":"https://api.github.com/repos/odoo/odoo/issues/comments/401646501","html_url":"https://github.com/odoo/odoo/issues/25551#issuecomment-401646501","issue_url":"https://api.github.com/repos/odoo/odoo/issues/25551","id":401646501,"node_id":"MDEyOklzc3VlQ29tbWVudDQwMTY0NjUwMQ==","user":{"login":"pedrobaeza","id":7165771,"node_id":"MDQ6VXNlcjcxNjU3NzE=","avatar_url":"https://avatars2.githubusercontent.com/u/7165771?v=4","gravatar_id":"","url":"https://api.github.com/users/pedrobaeza","html_url":"https://github.com/pedrobaeza","followers_url":"https://api.github.com/users/pedrobaeza/followers","following_url":"https://api.github.com/users/pedrobaeza/following{/other_user}","gists_url":"https://api.github.com/users/pedrobaeza/gists{/gist_id}","starred_url":"https://api.github.com/users/pedrobaeza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pedrobaeza/subscriptions","organizations_url":"https://api.github.com/users/pedrobaeza/orgs","repos_url":"https://api.github.com/users/pedrobaeza/repos","events_url":"https://api.github.com/users/pedrobaeza/events{/privacy}","received_events_url":"https://api.github.com/users/pedrobaeza/received_events","type":"User","site_admin":false},"created_at":"2018-07-02T01:03:59Z","updated_at":"2018-07-02T01:03:59Z","author_association":"CONTRIBUTOR","body":"Sorry, this is not an Odoo issue, but a question. You should make it on Odoo official forums (https://www.odoo.com/forum/help-1) or mailing lists (https://odoo.com/groups), but not here.\r\n\r\nClosing this by the reason above."}}
{"actor":{"display_login":"pedrobaeza"}}