Skip to content

Commit f7bba80

Browse files
Track SQL views in Hasura (#71)
1 parent b868aa1 commit f7bba80

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/dipdup/hasura.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import aiohttp
99
from aiohttp import ClientConnectorError, ClientOSError
1010
from tortoise import Model, fields
11+
from tortoise.transactions import get_connection
1112

1213
from dipdup.config import DipDupConfig, PostgresDatabaseConfig, pascal_to_snake
1314
from dipdup.exceptions import ConfigurationError
@@ -146,6 +147,8 @@ async def configure_hasura(config: DipDupConfig):
146147

147148
if config.hasura is None:
148149
raise ConfigurationError('`hasura` config section missing')
150+
if not isinstance(config.database, PostgresDatabaseConfig):
151+
raise RuntimeError
149152

150153
_logger.info('Configuring Hasura')
151154
url = config.hasura.url.rstrip("/")
@@ -200,7 +203,30 @@ async def configure_hasura(config: DipDupConfig):
200203
),
201204
headers=headers,
202205
)
203-
if not result.get('message') == 'success':
206+
if result.get('message') != 'success':
204207
_logger.error('Can\'t configure Hasura instance: %s', result)
205-
else:
206-
_logger.info('Hasura instance has been configured')
208+
return
209+
210+
views = await get_connection(None).execute_query(
211+
f"SELECT table_name FROM information_schema.views WHERE table_schema = '{config.database.schema_name}'"
212+
)
213+
for view in views[1]:
214+
result = await http_request(
215+
session,
216+
'post',
217+
url=f'{url}/v1/query',
218+
data=json.dumps(
219+
{
220+
"type": "add_existing_table_or_view",
221+
"args": {
222+
"name": view[0],
223+
"schema": config.database.schema_name,
224+
},
225+
},
226+
),
227+
headers=headers,
228+
)
229+
if result.get('message') != 'success' and result.get('code') != 'already-tracked':
230+
_logger.error('Can\'t configure Hasura instance: %s', result)
231+
232+
_logger.info('Hasura instance has been configured')

0 commit comments

Comments
 (0)