Skip to content

Commit a390189

Browse files
Notify about required reindexing, wrap tortoise exceptions (#100)
1 parent ddc6929 commit a390189

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

src/dipdup/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
'1.0': '>=1.0.0, <=1.1.2',
66
'1.1': '>=2.0.0',
77
}
8+
spec_reindex_mapping = {
9+
'0.1': False,
10+
'1.0': False,
11+
'1.1': True,
12+
}

src/dipdup/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from fcache.cache import FileCache # type: ignore
1414
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
1515

16-
from dipdup import __spec_version__, __version__, spec_version_mapping
16+
from dipdup import __spec_version__, __version__, spec_version_mapping, spec_reindex_mapping
1717
from dipdup.codegen import DEFAULT_DOCKER_ENV_FILE, DEFAULT_DOCKER_IMAGE, DEFAULT_DOCKER_TAG, DipDupCodeGenerator
1818
from dipdup.config import DipDupConfig, LoggingConfig, PostgresDatabaseConfig
1919
from dipdup.dipdup import DipDup
@@ -71,7 +71,8 @@ async def cli(ctx, config: List[str], env_file: List[str], logging_config: str):
7171
if _config.spec_version not in spec_version_mapping:
7272
raise ConfigurationError('Unknown `spec_version`, correct ones: {}')
7373
if _config.spec_version != __spec_version__ and ctx.invoked_subcommand != 'migrate':
74-
raise MigrationRequiredError(None, _config.spec_version, __spec_version__)
74+
reindex = spec_reindex_mapping[__spec_version__]
75+
raise MigrationRequiredError(None, _config.spec_version, __spec_version__, reindex)
7576

7677
if _config.sentry:
7778
sentry_sdk.init(

src/dipdup/dipdup.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from dipdup.datasources.coinbase.datasource import CoinbaseDatasource
3333
from dipdup.datasources.datasource import IndexDatasource
3434
from dipdup.datasources.tzkt.datasource import TzktDatasource
35-
from dipdup.exceptions import ConfigurationError
35+
from dipdup.exceptions import ConfigurationError, ReindexingRequiredError
3636
from dipdup.hasura import HasuraGateway
3737
from dipdup.index import BigMapIndex, Index, OperationIndex
3838
from dipdup.models import BigMapData, HeadBlockData, IndexType, OperationData, State
@@ -287,16 +287,20 @@ async def _initialize_database(self, reindex: bool = False) -> None:
287287

288288
self._logger.info('Checking database schema')
289289
connection_name, connection = next(iter(Tortoise._connections.items()))
290-
schema_sql = get_schema_sql(connection, False)
291-
292-
# NOTE: Column order could differ in two generated schemas for the same models, drop commas and sort strings to eliminate this
293-
processed_schema_sql = '\n'.join(sorted(schema_sql.replace(',', '').split('\n'))).encode()
294-
schema_hash = hashlib.sha256(processed_schema_sql).hexdigest()
295290

296291
try:
297292
schema_state = await State.get_or_none(index_type=IndexType.schema, index_name=connection_name)
298293
except OperationalError:
299294
schema_state = None
295+
# TODO: Process exception in Tortoise
296+
except KeyError as e:
297+
raise ReindexingRequiredError(None) from e
298+
299+
schema_sql = get_schema_sql(connection, False)
300+
301+
# NOTE: Column order could differ in two generated schemas for the same models, drop commas and sort strings to eliminate this
302+
processed_schema_sql = '\n'.join(sorted(schema_sql.replace(',', '').split('\n'))).encode()
303+
schema_hash = hashlib.sha256(processed_schema_sql).hexdigest()
300304

301305
# NOTE: `State.index_hash` field contains schema hash when `index_type` is `IndexType.schema`
302306
if schema_state is None:

src/dipdup/exceptions.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from dipdup import spec_version_mapping
99

10+
_tab = '\n\n' + ('_' * 80) + '\n\n'
11+
1012
_migration_required_message = """Project migration required!
1113
1214
{version_table}
@@ -17,6 +19,14 @@
1719
See https://baking-bad.org/blog/ for additional release information.
1820
"""
1921

22+
_reindexing_required_message = """Reindexing required!
23+
24+
Recent changes in the framework have made it necessary to reindex the project.
25+
26+
1. Optionally backup a database
27+
2. Run `dipdup run --reindex`
28+
"""
29+
2030
_handler_import_message = """Failed to import `{obj}` from `{module}`.
2131
2232
Reasons in order of possibility:
@@ -52,8 +62,6 @@
5262
{error_context}
5363
"""
5464

55-
_tab = '\n\n' + ('_' * 80) + '\n\n'
56-
5765

5866
class DipDupError(ABC, Exception):
5967
exit_code = 1
@@ -86,12 +94,13 @@ def format_help(self) -> str:
8694

8795

8896
class MigrationRequiredError(DipDupError):
89-
"""Project and DipDup spec versions don't match """
97+
"""Project and DipDup spec versions don't match"""
9098

91-
def __init__(self, ctx, from_: str, to: str) -> None:
99+
def __init__(self, ctx, from_: str, to: str, reindex: bool = False) -> None:
92100
super().__init__(ctx)
93101
self.from_ = from_
94102
self.to = to
103+
self.reindex = reindex
95104

96105
def format_help(self) -> str:
97106
version_table = tabulate(
@@ -101,7 +110,16 @@ def format_help(self) -> str:
101110
],
102111
headers=['', 'spec_version', 'DipDup version'],
103112
)
104-
return _migration_required_message.format(version_table=version_table)
113+
message = _migration_required_message.format(version_table=version_table)
114+
if self.reindex:
115+
message += _tab +_reindexing_required_message
116+
return message
117+
118+
119+
class ReindexingRequiredError(DipDupError):
120+
"""Performed migration requires reindexing"""
121+
def format_help(self) -> str:
122+
return _reindexing_required_message
105123

106124

107125
class HandlerImportError(DipDupError):

0 commit comments

Comments
 (0)