Skip to content

Commit 5419df9

Browse files
Fix Hasura codegen for tables with numerics in name (#115)
* Fix Hasura codegen for tables with numerics in name * Sentry stacktraces
1 parent 6295f3c commit 5419df9

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

src/dipdup/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def init_sentry(config: DipDupConfig) -> None:
5050
if not config.sentry:
5151
return
5252
if config.sentry.debug:
53-
level, event_level = logging.DEBUG, logging.WARNING
53+
level, event_level, attach_stacktrace = logging.DEBUG, logging.WARNING, True
5454
else:
55-
level, event_level = logging.INFO, logging.ERROR
55+
level, event_level, attach_stacktrace = logging.INFO, logging.ERROR, False
5656

5757
integrations = [
5858
AioHttpIntegration(),
@@ -66,6 +66,7 @@ def init_sentry(config: DipDupConfig) -> None:
6666
environment=config.sentry.environment,
6767
integrations=integrations,
6868
release=__version__,
69+
attach_stacktrace=attach_stacktrace,
6970
)
7071

7172

src/dipdup/hasura.py

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import importlib
33
import logging
4+
import re
45
from contextlib import suppress
56
from os.path import dirname, join
67
from typing import Any, Dict, Iterator, List, Optional, Tuple
@@ -16,6 +17,31 @@
1617
from dipdup.http import HTTPGateway
1718
from dipdup.utils import iter_files, iter_models
1819

20+
_get_fields_query = '''
21+
query introspectionQuery($name: String!) {
22+
__type(name: $name) {
23+
kind
24+
name
25+
fields {
26+
name
27+
description
28+
type {
29+
name
30+
kind
31+
ofType {
32+
name
33+
kind
34+
}
35+
}
36+
}
37+
}
38+
}
39+
'''.replace(
40+
'\n', ' '
41+
).replace(
42+
' ', ''
43+
)
44+
1945

2046
@dataclass
2147
class Field:
@@ -263,43 +289,30 @@ def _merge_metadata(self, existing: List[Dict[str, Any]], generated: List[Dict[s
263289
generated_dict = {key(t): t for t in generated}
264290
return list({**existing_dict, **generated_dict}.values())
265291

266-
async def _get_fields(self, name: str = 'query_root') -> List[Field]:
267-
query = '''
268-
query introspectionQuery($name: String!) {
269-
__type(name: $name) {
270-
kind
271-
name
272-
fields {
273-
name
274-
description
275-
type {
276-
name
277-
kind
278-
ofType {
279-
name
280-
kind
281-
}
282-
}
283-
}
284-
}
285-
}
286-
'''.replace(
287-
'\n', ' '
288-
).replace(
289-
' ', ''
290-
)
292+
async def _get_fields_json(self, name: str) -> List[Dict[str, Any]]:
291293
result = await self._hasura_request(
292294
endpoint='graphql',
293295
json={
294-
'query': query,
296+
'query': _get_fields_query,
295297
'variables': {'name': name},
296298
},
297299
)
298300
try:
299-
fields_json = result['data']['__type']['fields']
301+
return result['data']['__type']['fields']
300302
except TypeError as e:
301303
raise HasuraError(f'Unknown table `{name}`') from e
302304

305+
async def _get_fields(self, name: str = 'query_root') -> List[Field]:
306+
307+
try:
308+
fields_json = await self._get_fields_json(name)
309+
except HasuraError:
310+
# NOTE: An issue with decamelizing the table name?
311+
# NOTE: dex_quotes_15m -> dexQuotes15m -> dex_quotes15m -> FAIL
312+
# NOTE: Let's prefix every numeric with underscore. Won't help in complex cases but worth a try.
313+
alternative_name = ''.join([f"_{w}" if w.isnumeric() else w for w in re.split(r'(\d+)', name)])
314+
fields_json = await self._get_fields_json(alternative_name)
315+
303316
fields = []
304317
for field_json in fields_json:
305318
# NOTE: Exclude autogenerated aggregate and pk fields

0 commit comments

Comments
 (0)