|
1 | 1 | import asyncio |
2 | | -import hashlib |
3 | 2 | import logging |
4 | 3 | import os |
5 | | -import sys |
6 | 4 | from dataclasses import dataclass |
7 | 5 | from functools import wraps |
8 | 6 | from os.path import dirname, join |
9 | | -from typing import Dict |
10 | 7 |
|
11 | 8 | import click |
12 | | -from tortoise import Tortoise |
13 | | -from tortoise.exceptions import OperationalError |
14 | | -from tortoise.utils import get_schema_sql |
15 | 9 |
|
16 | | -import dipdup.codegen as codegen |
17 | 10 | from dipdup import __version__ |
18 | | -from dipdup.config import DipDupConfig, IndexTemplateConfig, LoggingConfig, PostgresDatabaseConfig, TzktDatasourceConfig |
19 | | -from dipdup.datasources.tzkt.datasource import TzktDatasource |
20 | | -from dipdup.hasura import configure_hasura |
21 | | -from dipdup.models import IndexType, State |
22 | | -from dipdup.utils import reindex, tortoise_wrapper |
| 11 | +from dipdup.config import DipDupConfig, LoggingConfig |
| 12 | +from dipdup.dipdup import DipDup |
23 | 13 |
|
24 | 14 | _logger = logging.getLogger(__name__) |
25 | 15 |
|
@@ -68,69 +58,14 @@ async def cli(ctx, config: str, logging_config: str): |
68 | 58 | @click_async |
69 | 59 | async def run(ctx) -> None: |
70 | 60 | config: DipDupConfig = ctx.obj.config |
71 | | - |
72 | | - url = config.database.connection_string |
73 | | - models = f'{config.package}.models' |
74 | | - async with tortoise_wrapper(url, models): |
75 | | - _logger.info('Initializing database') |
76 | | - |
77 | | - connection_name, connection = next(iter(Tortoise._connections.items())) |
78 | | - schema_sql = get_schema_sql(connection, False) |
79 | | - |
80 | | - if isinstance(config.database, PostgresDatabaseConfig) and config.database.schema_name: |
81 | | - await Tortoise._connections['default'].execute_script("CREATE SCHEMA IF NOT EXISTS {}".format(config.database.schema_name)) |
82 | | - await Tortoise._connections['default'].execute_script("SET search_path TO {}".format(config.database.schema_name)) |
83 | | - |
84 | | - # NOTE: Column order could differ in two generated schemas for the same models, drop commas and sort strings to eliminate this |
85 | | - processed_schema_sql = '\n'.join(sorted(schema_sql.replace(',', '').split('\n'))).encode() |
86 | | - schema_hash = hashlib.sha256(processed_schema_sql).hexdigest() |
87 | | - |
88 | | - try: |
89 | | - schema_state = await State.get_or_none(index_type=IndexType.schema, index_name=connection_name) |
90 | | - except OperationalError: |
91 | | - schema_state = None |
92 | | - |
93 | | - if schema_state is None: |
94 | | - await Tortoise.generate_schemas() |
95 | | - schema_state = State(index_type=IndexType.schema, index_name=connection_name, hash=schema_hash) |
96 | | - await schema_state.save() |
97 | | - elif schema_state.hash != schema_hash: |
98 | | - _logger.warning('Schema hash mismatch, reindexing') |
99 | | - await reindex() |
100 | | - |
101 | | - await config.initialize() |
102 | | - |
103 | | - _logger.info('Fetching indexer state for dapp `%s`', config.package) |
104 | | - datasources: Dict[TzktDatasourceConfig, TzktDatasource] = {} |
105 | | - |
106 | | - for index_name, index_config in config.indexes.items(): |
107 | | - assert not isinstance(index_config, IndexTemplateConfig) |
108 | | - _logger.info('Processing index `%s`', index_name) |
109 | | - if isinstance(index_config.datasource, TzktDatasourceConfig): |
110 | | - if index_config.tzkt_config not in datasources: |
111 | | - datasources[index_config.tzkt_config] = TzktDatasource(index_config.tzkt_config.url) |
112 | | - datasources[index_config.tzkt_config].add_index(index_config) |
113 | | - else: |
114 | | - raise NotImplementedError(f'Datasource `{index_config.datasource}` is not supported') |
115 | | - |
116 | | - _logger.info('Starting datasources') |
117 | | - run_tasks = [asyncio.create_task(d.start()) for d in datasources.values()] |
118 | | - |
119 | | - if config.hasura: |
120 | | - hasura_task = asyncio.create_task(configure_hasura(config)) |
121 | | - run_tasks.append(hasura_task) |
122 | | - |
123 | | - await asyncio.gather(*run_tasks) |
| 61 | + dipdup = DipDup(config) |
| 62 | + await dipdup.run() |
124 | 63 |
|
125 | 64 |
|
126 | 65 | @cli.command(help='Initialize new dipdap') |
127 | 66 | @click.pass_context |
128 | 67 | @click_async |
129 | 68 | async def init(ctx): |
130 | 69 | config: DipDupConfig = ctx.obj.config |
131 | | - |
132 | | - await codegen.create_package(config) |
133 | | - await codegen.fetch_schemas(config) |
134 | | - await codegen.generate_types(config) |
135 | | - await codegen.generate_handlers(config) |
136 | | - await codegen.cleanup(config) |
| 70 | + dipdup = DipDup(config) |
| 71 | + await dipdup.init() |
0 commit comments