Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Hasura codegen for tables with numerics in name #115

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/dipdup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def init_sentry(config: DipDupConfig) -> None:
if not config.sentry:
return
if config.sentry.debug:
level, event_level = logging.DEBUG, logging.WARNING
level, event_level, attach_stacktrace = logging.DEBUG, logging.WARNING, True
else:
level, event_level = logging.INFO, logging.ERROR
level, event_level, attach_stacktrace = logging.INFO, logging.ERROR, False

integrations = [
AioHttpIntegration(),
Expand All @@ -66,6 +66,7 @@ def init_sentry(config: DipDupConfig) -> None:
environment=config.sentry.environment,
integrations=integrations,
release=__version__,
attach_stacktrace=attach_stacktrace,
)


Expand Down
67 changes: 40 additions & 27 deletions src/dipdup/hasura.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import importlib
import logging
import re
from contextlib import suppress
from os.path import dirname, join
from typing import Any, Dict, Iterator, List, Optional, Tuple
Expand All @@ -16,6 +17,31 @@
from dipdup.http import HTTPGateway
from dipdup.utils import iter_files, iter_models

_get_fields_query = '''
query introspectionQuery($name: String!) {
__type(name: $name) {
kind
name
fields {
name
description
type {
name
kind
ofType {
name
kind
}
}
}
}
}
'''.replace(
'\n', ' '
).replace(
' ', ''
)


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

async def _get_fields(self, name: str = 'query_root') -> List[Field]:
query = '''
query introspectionQuery($name: String!) {
__type(name: $name) {
kind
name
fields {
name
description
type {
name
kind
ofType {
name
kind
}
}
}
}
}
'''.replace(
'\n', ' '
).replace(
' ', ''
)
async def _get_fields_json(self, name: str) -> List[Dict[str, Any]]:
result = await self._hasura_request(
endpoint='graphql',
json={
'query': query,
'query': _get_fields_query,
'variables': {'name': name},
},
)
try:
fields_json = result['data']['__type']['fields']
return result['data']['__type']['fields']
except TypeError as e:
raise HasuraError(f'Unknown table `{name}`') from e

async def _get_fields(self, name: str = 'query_root') -> List[Field]:

try:
fields_json = await self._get_fields_json(name)
except HasuraError:
# NOTE: An issue with decamelizing the table name?
# NOTE: dex_quotes_15m -> dexQuotes15m -> dex_quotes15m -> FAIL
# NOTE: Let's prefix every numeric with underscore. Won't help in complex cases but worth a try.
alternative_name = ''.join([f"_{w}" if w.isnumeric() else w for w in re.split(r'(\d+)', name)])
fields_json = await self._get_fields_json(alternative_name)

fields = []
for field_json in fields_json:
# NOTE: Exclude autogenerated aggregate and pk fields
Expand Down