10
10
from tortoise .utils import get_schema_sql
11
11
12
12
import dipdup .codegen as codegen
13
+ import dipdup .utils as utils
13
14
from dipdup import __version__
14
15
from dipdup .config import (
15
16
ROLLBACK_HANDLER ,
25
26
from dipdup .exceptions import ConfigurationError
26
27
from dipdup .hasura import configure_hasura
27
28
from dipdup .models import IndexType , State
28
- from dipdup .utils import reindex , tortoise_wrapper
29
29
30
30
31
31
class DipDup :
@@ -41,7 +41,7 @@ async def init(self) -> None:
41
41
await codegen .generate_handlers (self ._config )
42
42
await codegen .cleanup (self ._config )
43
43
44
- async def run (self ) -> None :
44
+ async def run (self , reindex : bool ) -> None :
45
45
url = self ._config .database .connection_string
46
46
cache = isinstance (self ._config .database , SqliteDatabaseConfig )
47
47
models = f'{ self ._config .package } .models'
@@ -51,8 +51,8 @@ async def run(self) -> None:
51
51
except ModuleNotFoundError as e :
52
52
raise ConfigurationError (f'Package `{ self ._config .package } ` not found. Have you forgot to call `init`?' ) from e
53
53
54
- async with tortoise_wrapper (url , models ):
55
- await self .initialize_database ()
54
+ async with utils . tortoise_wrapper (url , models ):
55
+ await self .initialize_database (reindex )
56
56
57
57
await self ._config .initialize ()
58
58
@@ -120,7 +120,7 @@ async def run(self) -> None:
120
120
121
121
await asyncio .gather (* run_tasks )
122
122
123
- async def initialize_database (self ) -> None :
123
+ async def initialize_database (self , reindex : bool = False ) -> None :
124
124
self ._logger .info ('Initializing database' )
125
125
126
126
if isinstance (self ._config .database , PostgresDatabaseConfig ) and self ._config .database .schema_name :
@@ -134,6 +134,10 @@ async def initialize_database(self) -> None:
134
134
processed_schema_sql = '\n ' .join (sorted (schema_sql .replace (',' , '' ).split ('\n ' ))).encode ()
135
135
schema_hash = hashlib .sha256 (processed_schema_sql ).hexdigest ()
136
136
137
+ if reindex :
138
+ self ._logger .warning ('Started with `--reindex` argument, reindexing' )
139
+ await utils .reindex ()
140
+
137
141
try :
138
142
schema_state = await State .get_or_none (index_type = IndexType .schema , index_name = connection_name )
139
143
except OperationalError :
@@ -145,4 +149,4 @@ async def initialize_database(self) -> None:
145
149
await schema_state .save ()
146
150
elif schema_state .hash != schema_hash :
147
151
self ._logger .warning ('Schema hash mismatch, reindexing' )
148
- await reindex ()
152
+ await utils . reindex ()
0 commit comments