Skip to content

Commit

Permalink
Add --reindex option to run command (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout authored Jun 1, 2021
1 parent 051aee2 commit f420a93
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/dipdup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ async def cli(ctx, config: List[str], logging_config: str):


@cli.command(help='Run existing dipdup project')
@click.option('--reindex', is_flag=True)
@click.pass_context
@click_async
async def run(ctx) -> None:
async def run(ctx, reindex: bool) -> None:
config: DipDupConfig = ctx.obj.config
dipdup = DipDup(config)
await dipdup.run()
await dipdup.run(reindex)


@cli.command(help='Initialize new dipdup project')
Expand Down
16 changes: 10 additions & 6 deletions src/dipdup/dipdup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tortoise.utils import get_schema_sql

import dipdup.codegen as codegen
import dipdup.utils as utils
from dipdup import __version__
from dipdup.config import (
ROLLBACK_HANDLER,
Expand All @@ -25,7 +26,6 @@
from dipdup.exceptions import ConfigurationError
from dipdup.hasura import configure_hasura
from dipdup.models import IndexType, State
from dipdup.utils import reindex, tortoise_wrapper


class DipDup:
Expand All @@ -41,7 +41,7 @@ async def init(self) -> None:
await codegen.generate_handlers(self._config)
await codegen.cleanup(self._config)

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

async with tortoise_wrapper(url, models):
await self.initialize_database()
async with utils.tortoise_wrapper(url, models):
await self.initialize_database(reindex)

await self._config.initialize()

Expand Down Expand Up @@ -120,7 +120,7 @@ async def run(self) -> None:

await asyncio.gather(*run_tasks)

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

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

if reindex:
self._logger.warning('Started with `--reindex` argument, reindexing')
await utils.reindex()

try:
schema_state = await State.get_or_none(index_type=IndexType.schema, index_name=connection_name)
except OperationalError:
Expand All @@ -145,4 +149,4 @@ async def initialize_database(self) -> None:
await schema_state.save()
elif schema_state.hash != schema_hash:
self._logger.warning('Schema hash mismatch, reindexing')
await reindex()
await utils.reindex()
7 changes: 5 additions & 2 deletions src/dipdup/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def http_request(method: str, **kwargs):
return await response.json()


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

0 comments on commit f420a93

Please sign in to comment.