|
8 | 8 | import aiohttp |
9 | 9 | from aiohttp import ClientConnectorError, ClientOSError |
10 | 10 | from tortoise import Model, fields |
| 11 | +from tortoise.transactions import get_connection |
11 | 12 |
|
12 | 13 | from dipdup.config import DipDupConfig, PostgresDatabaseConfig, pascal_to_snake |
13 | 14 | from dipdup.exceptions import ConfigurationError |
@@ -146,6 +147,8 @@ async def configure_hasura(config: DipDupConfig): |
146 | 147 |
|
147 | 148 | if config.hasura is None: |
148 | 149 | raise ConfigurationError('`hasura` config section missing') |
| 150 | + if not isinstance(config.database, PostgresDatabaseConfig): |
| 151 | + raise RuntimeError |
149 | 152 |
|
150 | 153 | _logger.info('Configuring Hasura') |
151 | 154 | url = config.hasura.url.rstrip("/") |
@@ -200,7 +203,30 @@ async def configure_hasura(config: DipDupConfig): |
200 | 203 | ), |
201 | 204 | headers=headers, |
202 | 205 | ) |
203 | | - if not result.get('message') == 'success': |
| 206 | + if result.get('message') != 'success': |
204 | 207 | _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