We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
pymysql.err.ProgrammingError: ('DocType', 'Webhook')
```
> bench --site all migrate --skip-failing 
...
Migrating my-site

Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 104, in <module>
    main()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 19, in main
    click.Group(commands=commands)(prog_name='bench')
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 27, in _func
    ret = f(frappe._dict(ctx.obj), *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/commands/site.py", line 309, in migrate
    skip_search_index=skip_search_index
  File "/home/frappe/frappe-bench/apps/frappe/frappe/migrate.py", line 78, in migrate
    skip_search_index=skip_search_index
  File "/home/frappe/frappe-bench/apps/frappe/frappe/migrate.py", line 78, in migrate
    sync_languages()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/core/doctype/language/language.py", line 43, in sync_languages
    'language_name': l['name']
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 231, in insert
    self.run_method("before_insert")
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 870, in run_method
    run_webhooks(self, method)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/integrations/doctype/webhook/__init__.py", line 25, in run_webhooks
    filters={"enabled": True}
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1469, in get_all
    return get_list(doctype, *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1442, in get_list
    return frappe.model.db_query.DatabaseQuery(doctype).execute(*args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/db_query.py", line 102, in execute
    self.columns = self.get_table_columns()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/db_query.py", line 339, in get_table_columns
    return get_table_columns(self.doctype)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/meta.py", line 49, in get_table_columns
    return frappe.db.get_table_columns(doctype)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/database/database.py", line 902, in get_table_columns
    raise self.TableMissingError('DocType', doctype)
pymysql.err.ProgrammingError: ('DocType', 'Webhook')
```

Migrating from `version-13-beta` to `version-13` (13.17)

### Versions

```
> bench version
erpnext 13.17.0
frappe 13.17.1
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `frappe/integrations/doctype/webhook/__init__.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2017, Frappe Technologies and contributors
3 # License: MIT. See LICENSE
4 
5 import frappe
6 
7 
8 def run_webhooks(doc, method):
9 	'''Run webhooks for this method'''
10 	if frappe.flags.in_import or frappe.flags.in_patch or frappe.flags.in_install:
11 		return
12 
13 	if frappe.flags.webhooks_executed is None:
14 		frappe.flags.webhooks_executed = {}
15 
16 	if frappe.flags.webhooks is None:
17 		# load webhooks from cache
18 		webhooks = frappe.cache().get_value('webhooks')
19 		if webhooks is None:
20 			# query webhooks
21 			webhooks_list = frappe.get_all('Webhook',
22 						fields=["name", "`condition`", "webhook_docevent", "webhook_doctype"],
23 						filters={"enabled": True}
24 					)
25 
26 			# make webhooks map for cache
27 			webhooks = {}
28 			for w in webhooks_list:
29 				webhooks.setdefault(w.webhook_doctype, []).append(w)
30 			frappe.cache().set_value('webhooks', webhooks)
31 
32 		frappe.flags.webhooks = webhooks
33 
34 	# get webhooks for this doctype
35 	webhooks_for_doc = frappe.flags.webhooks.get(doc.doctype, None)
36 
37 	if not webhooks_for_doc:
38 		# no webhooks, quit
39 		return
40 
41 	def _webhook_request(webhook):
42 		if webhook.name not in frappe.flags.webhooks_executed.get(doc.name, []):
43 			frappe.enqueue("frappe.integrations.doctype.webhook.webhook.enqueue_webhook",
44 				enqueue_after_commit=True, doc=doc, webhook=webhook)
45 
46 			# keep list of webhooks executed for this doc in this request
47 			# so that we don't run the same webhook for the same document multiple times
48 			# in one request
49 			frappe.flags.webhooks_executed.setdefault(doc.name, []).append(webhook.name)
50 
51 	event_list = ["on_update", "after_insert", "on_submit", "on_cancel", "on_trash"]
52 
53 	if not doc.flags.in_insert:
54 		# value change is not applicable in insert
55 		event_list.append('on_change')
56 		event_list.append('before_update_after_submit')
57 
58 	from frappe.integrations.doctype.webhook.webhook import get_context
59 
60 	for webhook in webhooks_for_doc:
61 		trigger_webhook = False
62 		event = method if method in event_list else None
63 		if not webhook.condition:
64 			trigger_webhook = True
65 		elif frappe.safe_eval(webhook.condition, eval_locals=get_context(doc)):
66 			trigger_webhook = True
67 
68 		if trigger_webhook and event and webhook.webhook_docevent == event:
69 			_webhook_request(webhook)
70
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/frappe/integrations/doctype/webhook/__init__.py b/frappe/integrations/doctype/webhook/__init__.py
--- a/frappe/integrations/doctype/webhook/__init__.py
+++ b/frappe/integrations/doctype/webhook/__init__.py
@@ -7,7 +7,7 @@
 
 def run_webhooks(doc, method):
 	'''Run webhooks for this method'''
-	if frappe.flags.in_import or frappe.flags.in_patch or frappe.flags.in_install:
+	if frappe.flags.in_import or frappe.flags.in_patch or frappe.flags.in_install or frappe.flags.in_migrate:
 		return
 
 	if frappe.flags.webhooks_executed is None:

{"golden_diff": "diff --git a/frappe/integrations/doctype/webhook/__init__.py b/frappe/integrations/doctype/webhook/__init__.py\n--- a/frappe/integrations/doctype/webhook/__init__.py\n+++ b/frappe/integrations/doctype/webhook/__init__.py\n@@ -7,7 +7,7 @@\n \n def run_webhooks(doc, method):\n \t'''Run webhooks for this method'''\n-\tif frappe.flags.in_import or frappe.flags.in_patch or frappe.flags.in_install:\n+\tif frappe.flags.in_import or frappe.flags.in_patch or frappe.flags.in_install or frappe.flags.in_migrate:\n \t\treturn\n \n \tif frappe.flags.webhooks_executed is None:\n", "issue": "pymysql.err.ProgrammingError: ('DocType', 'Webhook')\n```\r\n> bench --site all migrate --skip-failing \r\n...\r\nMigrating my-site\r\n\r\nTraceback (most recent call last):\r\n  File \"/usr/lib/python3.6/runpy.py\", line 193, in _run_module_as_main\r\n    \"__main__\", mod_spec)\r\n  File \"/usr/lib/python3.6/runpy.py\", line 85, in _run_code\r\n    exec(code, run_globals)\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py\", line 104, in <module>\r\n    main()\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py\", line 19, in main\r\n    click.Group(commands=commands)(prog_name='bench')\r\n  File \"/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py\", line 829, in __call__\r\n    return self.main(*args, **kwargs)\r\n  File \"/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py\", line 782, in main\r\n    rv = self.invoke(ctx)\r\n  File \"/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py\", line 1259, in invoke\r\n    return _process_result(sub_ctx.command.invoke(sub_ctx))\r\n  File \"/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py\", line 1259, in invoke\r\n    return _process_result(sub_ctx.command.invoke(sub_ctx))\r\n  File \"/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py\", line 1066, in invoke\r\n    return ctx.invoke(self.callback, **ctx.params)\r\n  File \"/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/core.py\", line 610, in invoke\r\n    return callback(*args, **kwargs)\r\n  File \"/home/frappe/frappe-bench/env/lib/python3.6/site-packages/click/decorators.py\", line 21, in new_func\r\n    return f(get_current_context(), *args, **kwargs)\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/commands/__init__.py\", line 27, in _func\r\n    ret = f(frappe._dict(ctx.obj), *args, **kwargs)\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/commands/site.py\", line 309, in migrate\r\n    skip_search_index=skip_search_index\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/migrate.py\", line 78, in migrate\r\n    skip_search_index=skip_search_index\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/migrate.py\", line 78, in migrate\r\n    sync_languages()\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/core/doctype/language/language.py\", line 43, in sync_languages\r\n    'language_name': l['name']\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py\", line 231, in insert\r\n    self.run_method(\"before_insert\")\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py\", line 870, in run_method\r\n    run_webhooks(self, method)\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/integrations/doctype/webhook/__init__.py\", line 25, in run_webhooks\r\n    filters={\"enabled\": True}\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py\", line 1469, in get_all\r\n    return get_list(doctype, *args, **kwargs)\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py\", line 1442, in get_list\r\n    return frappe.model.db_query.DatabaseQuery(doctype).execute(*args, **kwargs)\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/db_query.py\", line 102, in execute\r\n    self.columns = self.get_table_columns()\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/db_query.py\", line 339, in get_table_columns\r\n    return get_table_columns(self.doctype)\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/meta.py\", line 49, in get_table_columns\r\n    return frappe.db.get_table_columns(doctype)\r\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/database/database.py\", line 902, in get_table_columns\r\n    raise self.TableMissingError('DocType', doctype)\r\npymysql.err.ProgrammingError: ('DocType', 'Webhook')\r\n```\r\n\r\nMigrating from `version-13-beta` to `version-13` (13.17)\r\n\r\n### Versions\r\n\r\n```\r\n> bench version\r\nerpnext 13.17.0\r\nfrappe 13.17.1\r\n```\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n# Copyright (c) 2017, Frappe Technologies and contributors\n# License: MIT. See LICENSE\n\nimport frappe\n\n\ndef run_webhooks(doc, method):\n\t'''Run webhooks for this method'''\n\tif frappe.flags.in_import or frappe.flags.in_patch or frappe.flags.in_install:\n\t\treturn\n\n\tif frappe.flags.webhooks_executed is None:\n\t\tfrappe.flags.webhooks_executed = {}\n\n\tif frappe.flags.webhooks is None:\n\t\t# load webhooks from cache\n\t\twebhooks = frappe.cache().get_value('webhooks')\n\t\tif webhooks is None:\n\t\t\t# query webhooks\n\t\t\twebhooks_list = frappe.get_all('Webhook',\n\t\t\t\t\t\tfields=[\"name\", \"`condition`\", \"webhook_docevent\", \"webhook_doctype\"],\n\t\t\t\t\t\tfilters={\"enabled\": True}\n\t\t\t\t\t)\n\n\t\t\t# make webhooks map for cache\n\t\t\twebhooks = {}\n\t\t\tfor w in webhooks_list:\n\t\t\t\twebhooks.setdefault(w.webhook_doctype, []).append(w)\n\t\t\tfrappe.cache().set_value('webhooks', webhooks)\n\n\t\tfrappe.flags.webhooks = webhooks\n\n\t# get webhooks for this doctype\n\twebhooks_for_doc = frappe.flags.webhooks.get(doc.doctype, None)\n\n\tif not webhooks_for_doc:\n\t\t# no webhooks, quit\n\t\treturn\n\n\tdef _webhook_request(webhook):\n\t\tif webhook.name not in frappe.flags.webhooks_executed.get(doc.name, []):\n\t\t\tfrappe.enqueue(\"frappe.integrations.doctype.webhook.webhook.enqueue_webhook\",\n\t\t\t\tenqueue_after_commit=True, doc=doc, webhook=webhook)\n\n\t\t\t# keep list of webhooks executed for this doc in this request\n\t\t\t# so that we don't run the same webhook for the same document multiple times\n\t\t\t# in one request\n\t\t\tfrappe.flags.webhooks_executed.setdefault(doc.name, []).append(webhook.name)\n\n\tevent_list = [\"on_update\", \"after_insert\", \"on_submit\", \"on_cancel\", \"on_trash\"]\n\n\tif not doc.flags.in_insert:\n\t\t# value change is not applicable in insert\n\t\tevent_list.append('on_change')\n\t\tevent_list.append('before_update_after_submit')\n\n\tfrom frappe.integrations.doctype.webhook.webhook import get_context\n\n\tfor webhook in webhooks_for_doc:\n\t\ttrigger_webhook = False\n\t\tevent = method if method in event_list else None\n\t\tif not webhook.condition:\n\t\t\ttrigger_webhook = True\n\t\telif frappe.safe_eval(webhook.condition, eval_locals=get_context(doc)):\n\t\t\ttrigger_webhook = True\n\n\t\tif trigger_webhook and event and webhook.webhook_docevent == event:\n\t\t\t_webhook_request(webhook)\n", "path": "frappe/integrations/doctype/webhook/__init__.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n# Copyright (c) 2017, Frappe Technologies and contributors\n# License: MIT. See LICENSE\n\nimport frappe\n\n\ndef run_webhooks(doc, method):\n\t'''Run webhooks for this method'''\n\tif frappe.flags.in_import or frappe.flags.in_patch or frappe.flags.in_install or frappe.flags.in_migrate:\n\t\treturn\n\n\tif frappe.flags.webhooks_executed is None:\n\t\tfrappe.flags.webhooks_executed = {}\n\n\tif frappe.flags.webhooks is None:\n\t\t# load webhooks from cache\n\t\twebhooks = frappe.cache().get_value('webhooks')\n\t\tif webhooks is None:\n\t\t\t# query webhooks\n\t\t\twebhooks_list = frappe.get_all('Webhook',\n\t\t\t\t\t\tfields=[\"name\", \"`condition`\", \"webhook_docevent\", \"webhook_doctype\"],\n\t\t\t\t\t\tfilters={\"enabled\": True}\n\t\t\t\t\t)\n\n\t\t\t# make webhooks map for cache\n\t\t\twebhooks = {}\n\t\t\tfor w in webhooks_list:\n\t\t\t\twebhooks.setdefault(w.webhook_doctype, []).append(w)\n\t\t\tfrappe.cache().set_value('webhooks', webhooks)\n\n\t\tfrappe.flags.webhooks = webhooks\n\n\t# get webhooks for this doctype\n\twebhooks_for_doc = frappe.flags.webhooks.get(doc.doctype, None)\n\n\tif not webhooks_for_doc:\n\t\t# no webhooks, quit\n\t\treturn\n\n\tdef _webhook_request(webhook):\n\t\tif webhook.name not in frappe.flags.webhooks_executed.get(doc.name, []):\n\t\t\tfrappe.enqueue(\"frappe.integrations.doctype.webhook.webhook.enqueue_webhook\",\n\t\t\t\tenqueue_after_commit=True, doc=doc, webhook=webhook)\n\n\t\t\t# keep list of webhooks executed for this doc in this request\n\t\t\t# so that we don't run the same webhook for the same document multiple times\n\t\t\t# in one request\n\t\t\tfrappe.flags.webhooks_executed.setdefault(doc.name, []).append(webhook.name)\n\n\tevent_list = [\"on_update\", \"after_insert\", \"on_submit\", \"on_cancel\", \"on_trash\"]\n\n\tif not doc.flags.in_insert:\n\t\t# value change is not applicable in insert\n\t\tevent_list.append('on_change')\n\t\tevent_list.append('before_update_after_submit')\n\n\tfrom frappe.integrations.doctype.webhook.webhook import get_context\n\n\tfor webhook in webhooks_for_doc:\n\t\ttrigger_webhook = False\n\t\tevent = method if method in event_list else None\n\t\tif not webhook.condition:\n\t\t\ttrigger_webhook = True\n\t\telif frappe.safe_eval(webhook.condition, eval_locals=get_context(doc)):\n\t\t\ttrigger_webhook = True\n\n\t\tif trigger_webhook and event and webhook.webhook_docevent == event:\n\t\t\t_webhook_request(webhook)\n", "path": "frappe/integrations/doctype/webhook/__init__.py"}]}