Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RM-49 Reduce field_to_sqlalchemy_type complexity #224

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metrics/mypy_high_water_mark
Original file line number Diff line number Diff line change
@@ -1 +1 @@
92.3300
92.2800
33 changes: 15 additions & 18 deletions records_mover/records/schema/field/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,30 +167,27 @@ def field_to_sqlalchemy_type(field: 'RecordsSchemaField',
field_to_func_map = {
'integer': get_integer_type,
'decimal': get_decimal_type,
'boolean': cast(Callable[['RecordsSchemaField', 'DBDriver'], sqlalchemy.types.TypeEngine],
lambda field, driver: sqlalchemy.sql.sqltypes.BOOLEAN()),
'boolean': lambda field, driver: sqlalchemy.sql.sqltypes.BOOLEAN(),
'string': get_string_type,
'date': cast(Callable[['RecordsSchemaField', 'DBDriver'], sqlalchemy.types.TypeEngine],
lambda field, driver: sqlalchemy.sql.sqltypes.DATE()),
'datetime': cast(Callable[['RecordsSchemaField', 'DBDriver'], sqlalchemy.types.TypeEngine],
lambda field, driver: driver.type_for_date_plus_time(has_tz=False)),
'datetimetz': cast(
Callable[['RecordsSchemaField', 'DBDriver'], sqlalchemy.types.TypeEngine],
lambda field, driver: driver.type_for_date_plus_time(has_tz=True)),
'time': cast(Callable[['RecordsSchemaField', 'DBDriver'], sqlalchemy.types.TypeEngine],
lambda field, driver: sqlalchemy.sql.sqltypes.TIME(timezone=False)
if driver.supports_time_type()
else sqlalchemy.sql.sqltypes.VARCHAR(8)),
'timetz': cast(Callable[['RecordsSchemaField', 'DBDriver'], sqlalchemy.types.TypeEngine],
lambda field, driver: sqlalchemy.sql.sqltypes.TIME(timezone=True)
if driver.supports_time_type()
else sqlalchemy.sql.sqltypes.VARCHAR(8))
'date': lambda field, driver: sqlalchemy.sql.sqltypes.DATE(),
'datetime': lambda field, driver: driver.type_for_date_plus_time(has_tz=False),
'datetimetz': lambda field, driver: driver.type_for_date_plus_time(has_tz=True),
'time': lambda field, driver: (
sqlalchemy.sql.sqltypes.TIME(timezone=False)
if driver.supports_time_type()
else sqlalchemy.sql.sqltypes.VARCHAR(8)),
'timetz': lambda field, driver: (
sqlalchemy.sql.sqltypes.TIME(timezone=True)
if driver.supports_time_type()
else sqlalchemy.sql.sqltypes.VARCHAR(8))
}

func = field_to_func_map.get(field.field_type, None)
if not func:
raise NotImplementedError(f"Teach me how to handle records schema type {field.field_type}")
return func(field, driver)
typed_func = cast(Callable[['RecordsSchemaField', 'DBDriver'], sqlalchemy.types.TypeEngine],
func)
return typed_func(field, driver)


def field_to_sqlalchemy_column(field: 'RecordsSchemaField', driver: 'DBDriver') -> Column:
Expand Down