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

Add db type alias information to db types endpoint #1190

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add db type alias information to db types endpoint
  • Loading branch information
dmos62 committed Mar 17, 2022
commit af4fa7ce96f5ec6102434c725dbcf55627a244ba
34 changes: 34 additions & 0 deletions db/types/aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from frozendict import frozendict


# Copied (and reordered) from table in
# https://www.postgresql.org/docs/11/datatype.html
canonical_to_aliases = frozendict(
{
'boolean': ['bool'],
'bit varying': ['varbit'],
'smallint': ['int2'],
'integer': ['int', 'int4'],
'bigint': ['int8'],
'smallserial': ['serial2'],
'serial': ['serial4'],
'bigserial': ['serial8'],
'real': ['float4'],
'double precision': ['float8'],
'numeric': ['decimal'],
'character': ['char'],
'character varying': ['varchar'],
'time with timezone': ['timetz'],
'timestamp with timezone': ['timestamptz'],
}
)

alias_to_canonical = frozendict(
{
alias: canonical
for canonical, aliases
in canonical_to_aliases.items()
for alias
in aliases
}
)
20 changes: 17 additions & 3 deletions db/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlalchemy import create_engine

from db import constants

from db.types.aliases import canonical_to_aliases, alias_to_canonical
from db.functions import hints

from frozendict import frozendict
Expand All @@ -14,7 +14,21 @@
VARCHAR = 'varchar'


class PostgresType(Enum):
class DatabaseType:
@property
def is_canonical(self):
return self.value not in aliases

@property
def aliases(self):
return canonical_to_aliases.get(self.value, [])

@property
def alias_of(self):
return alias_to_canonical.get(self.value)


class PostgresType(DatabaseType, Enum):
"""
This only includes built-in Postgres types that SQLAlchemy supports.
SQLAlchemy doesn't support XML. See zzzeek's comment on:
Expand Down Expand Up @@ -66,7 +80,7 @@ class PostgresType(Enum):
UUID = 'uuid'


class MathesarCustomType(Enum):
class MathesarCustomType(DatabaseType, Enum):
"""
This is a list of custom Mathesar DB types.
Keys returned by get_available_types are of the format 'mathesar_types.VALUE'
Expand Down
9 changes: 8 additions & 1 deletion mathesar/api/serializers/db_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ class DBTypeSerializer(serializers.Serializer):
hints = serializers.ListField(child=serializers.DictField())

def to_representation(self, db_type):
# TODO solve db type casing holistically
# https://github.com/centerofci/mathesar/issues/1036
uppercase_id = db_type.value.upper()
uppercased_aliases = [db_type_id.upper() for db_type_id in db_type.aliases]
uppercased_alias_of = db_type.alias_of.upper() if db_type.alias_of else None
return {
"id": db_type.value,
"id": uppercase_id,
"aliases": uppercased_aliases,
"alias_of": uppercased_alias_of,
"hints": db_types_hinted.get(db_type, None),
}