Skip to content

Commit

Permalink
Improve models validation (#156)
Browse files Browse the repository at this point in the history
* Improve models validation

* Changelog
  • Loading branch information
droserasprout authored Oct 9, 2021
1 parent 649ca3c commit 1bdefa6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

### Added

* New index type `head` allowing to handle head (reduced block header info) updates
* New index type `head` allowing to handle head (reduced block header info) updates.

### Improved

* Raise `DatabaseConfigurationError` exception when project models are not compatible with GraphQL.

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ async def on_update_expiry_map(
assert store_expiry_map.key
assert store_expiry_map.value

expiry = store_expiry_map.value.__root__
timestamp = store_expiry_map.value.__root__
record_name = bytes.fromhex(store_expiry_map.key.__root__).decode()
await models.Expiry.update_or_create(
id=record_name,
defaults=dict(expiry=expiry),
defaults=dict(timestamp=timestamp),
)
2 changes: 1 addition & 1 deletion src/demo_tezos_domains_big_map/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TLD(Model):

class Expiry(Model):
id = fields.CharField(max_length=255, pk=True)
expiry = fields.DatetimeField(null=True)
timestamp = fields.DatetimeField(null=True)


class Domain(Model):
Expand Down
5 changes: 3 additions & 2 deletions src/dipdup/dipdup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from dipdup.models import IndexStatus, OperationData, Schema
from dipdup.scheduler import add_job, create_scheduler
from dipdup.utils import slowdown
from dipdup.utils.database import generate_schema, get_schema_hash, set_schema, tortoise_wrapper, validate_models
from dipdup.utils.database import generate_schema, get_schema_hash, prepare_models, set_schema, tortoise_wrapper, validate_models


class IndexDispatcher:
Expand Down Expand Up @@ -351,7 +351,8 @@ async def _initialize_schema(self) -> None:
await self._ctx.fire_hook('on_restart')

async def _set_up_database(self, stack: AsyncExitStack, reindex: bool) -> None:
# NOTE: Must be called before Tortoise.init
# NOTE: Must be called before entering Tortoise context
prepare_models(self._config.package)
validate_models(self._config.package)

url = self._config.database.connection_string
Expand Down
28 changes: 19 additions & 9 deletions src/dipdup/utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,26 @@ async def move_table(conn: BaseDBAsyncClient, name: str, schema: str, new_schema
await conn.execute_script(f'ALTER TABLE {schema}.{name} SET SCHEMA {new_schema}')


def prepare_models(package: str) -> None:
for _, model in iter_models(package):
# NOTE: Generate missing table names before Tortoise does
model._meta.db_table = model._meta.db_table or pascal_to_snake(model.__name__)


def validate_models(package: str) -> None:
"""Validate project models"""
"""Check project's models for common mistakes"""
for _, model in iter_models(package):
# NOTE: Resolve missing table names before Tortoise does
if model._meta.db_table is None:
model._meta.db_table = pascal_to_snake(model.__name__)
table_name = model._meta.db_table

if table_name != pascal_to_snake(table_name):
raise DatabaseConfigurationError('Table name must be in snake_case', model)

name = model._meta.db_table
if name != pascal_to_snake(name):
raise DatabaseConfigurationError('Table names should be in snake_case', model)
for field in model._meta.fields_map.values():
if field.model_field_name != pascal_to_snake(field.model_field_name):
raise DatabaseConfigurationError('Column names should be in snake_case', model)
field_name = field.model_field_name

if field_name != pascal_to_snake(field_name):
raise DatabaseConfigurationError('Model fields must be in snake_case', model)

# NOTE: Leads to GraphQL issues
if field_name == table_name:
raise DatabaseConfigurationError('Model fields must differ from table name', model)

0 comments on commit 1bdefa6

Please sign in to comment.