Skip to content

Commit f420a93

Browse files
Add --reindex option to run command (#55)
1 parent 051aee2 commit f420a93

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/dipdup/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ async def cli(ctx, config: List[str], logging_config: str):
5656

5757

5858
@cli.command(help='Run existing dipdup project')
59+
@click.option('--reindex', is_flag=True)
5960
@click.pass_context
6061
@click_async
61-
async def run(ctx) -> None:
62+
async def run(ctx, reindex: bool) -> None:
6263
config: DipDupConfig = ctx.obj.config
6364
dipdup = DipDup(config)
64-
await dipdup.run()
65+
await dipdup.run(reindex)
6566

6667

6768
@cli.command(help='Initialize new dipdup project')

src/dipdup/dipdup.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from tortoise.utils import get_schema_sql
1111

1212
import dipdup.codegen as codegen
13+
import dipdup.utils as utils
1314
from dipdup import __version__
1415
from dipdup.config import (
1516
ROLLBACK_HANDLER,
@@ -25,7 +26,6 @@
2526
from dipdup.exceptions import ConfigurationError
2627
from dipdup.hasura import configure_hasura
2728
from dipdup.models import IndexType, State
28-
from dipdup.utils import reindex, tortoise_wrapper
2929

3030

3131
class DipDup:
@@ -41,7 +41,7 @@ async def init(self) -> None:
4141
await codegen.generate_handlers(self._config)
4242
await codegen.cleanup(self._config)
4343

44-
async def run(self) -> None:
44+
async def run(self, reindex: bool) -> None:
4545
url = self._config.database.connection_string
4646
cache = isinstance(self._config.database, SqliteDatabaseConfig)
4747
models = f'{self._config.package}.models'
@@ -51,8 +51,8 @@ async def run(self) -> None:
5151
except ModuleNotFoundError as e:
5252
raise ConfigurationError(f'Package `{self._config.package}` not found. Have you forgot to call `init`?') from e
5353

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)
5656

5757
await self._config.initialize()
5858

@@ -120,7 +120,7 @@ async def run(self) -> None:
120120

121121
await asyncio.gather(*run_tasks)
122122

123-
async def initialize_database(self) -> None:
123+
async def initialize_database(self, reindex: bool = False) -> None:
124124
self._logger.info('Initializing database')
125125

126126
if isinstance(self._config.database, PostgresDatabaseConfig) and self._config.database.schema_name:
@@ -134,6 +134,10 @@ async def initialize_database(self) -> None:
134134
processed_schema_sql = '\n'.join(sorted(schema_sql.replace(',', '').split('\n'))).encode()
135135
schema_hash = hashlib.sha256(processed_schema_sql).hexdigest()
136136

137+
if reindex:
138+
self._logger.warning('Started with `--reindex` argument, reindexing')
139+
await utils.reindex()
140+
137141
try:
138142
schema_state = await State.get_or_none(index_type=IndexType.schema, index_name=connection_name)
139143
except OperationalError:
@@ -145,4 +149,4 @@ async def initialize_database(self) -> None:
145149
await schema_state.save()
146150
elif schema_state.hash != schema_hash:
147151
self._logger.warning('Schema hash mismatch, reindexing')
148-
await reindex()
152+
await utils.reindex()

src/dipdup/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async def http_request(method: str, **kwargs):
6262
return await response.json()
6363

6464

65-
async def reindex():
65+
async def reindex() -> None:
6666
if isinstance(Tortoise._connections['default'], AsyncpgDBClient):
6767
async with in_transaction() as conn:
6868
await conn.execute_script(
@@ -78,4 +78,7 @@ async def reindex():
7878
)
7979
else:
8080
await Tortoise._drop_databases()
81-
os.execl(sys.executable, sys.executable, *sys.argv)
81+
# NOTE: Remove --reindex from arguments to avoid reindexing loop
82+
argv = sys.argv[:-1] if sys.argv[-1] == '--reindex' else sys.argv
83+
# NOTE: Tortoise can't recover after dropping database for some reason, restart.
84+
os.execl(sys.executable, sys.executable, *argv)

0 commit comments

Comments
 (0)