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 column type targets to API #366

Merged
merged 26 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
26a5007
modify type getter to support displaying DB names
mathemancer Jul 8, 2021
6be9ced
fix unfriendly type names bug, add test for it
mathemancer Jul 8, 2021
b60050d
pass DB types to API and use for column creation
mathemancer Jul 8, 2021
929d514
refactor, add function to get valid target_type for given type
mathemancer Jul 12, 2021
db30fb5
remove TEXT db type for now, improve valid target type map
mathemancer Jul 12, 2021
944922f
add method to MathesarColumn to get target_types for casting
mathemancer Jul 12, 2021
29fc95c
add method for getting valid target types to MathesarColumn
mathemancer Jul 14, 2021
4b69434
modify cast map getter to match with MathesarColumn
mathemancer Jul 14, 2021
e026789
add function to get table with MathesarColumns
mathemancer Jul 14, 2021
e6b3d3e
add mathesar_columns property to Table model
mathemancer Jul 14, 2021
593bdb5
use new enriched columns to return target_types in API
mathemancer Jul 14, 2021
58949cb
fix failing API tests
mathemancer Jul 14, 2021
3bf3638
remove unneeded print statement
mathemancer Jul 14, 2021
8cff309
remove unused engine init arg
mathemancer Jul 14, 2021
69e8021
fix flake8 errors
mathemancer Jul 14, 2021
51e18c2
Merge branch 'master' into column_type_targets
mathemancer Jul 14, 2021
e528311
move new enriched column behavior to sa_columns
mathemancer Jul 15, 2021
2ccdd34
add tests for new MathesarColumn methods
mathemancer Jul 18, 2021
bb6ca09
add test for getting cast map
mathemancer Jul 18, 2021
a59aad3
Merge branch 'master' into column_type_targets
mathemancer Jul 19, 2021
4093f7b
use column index getter in MathesarColumn class
mathemancer Jul 20, 2021
e9c510d
fix column API tests with new column index field
mathemancer Jul 20, 2021
ce20d87
Merge branch 'master' into column_type_targets
mathemancer Jul 20, 2021
3daf865
add tests for column index property
mathemancer Jul 20, 2021
824219d
Merge branch 'master' into column_type_targets
mathemancer Jul 20, 2021
6807e26
change field column_index to index in columns endpoint
mathemancer Jul 20, 2021
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
Prev Previous commit
Next Next commit
pass DB types to API and use for column creation
  • Loading branch information
mathemancer committed Jul 8, 2021
commit b60050d258a150eb4bdcb6b71e955f2ac076a265
11 changes: 7 additions & 4 deletions db/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ def get_column_index_from_name(table_oid, column_name, engine):
def create_column(engine, table_oid, column_data):
column_type = column_data[TYPE]
column_nullable = column_data.get(NULLABLE, True)
supported_types = alteration.get_supported_alter_column_types(engine)
sa_type = supported_types.get(column_type.lower())
supported_types = alteration.get_supported_alter_column_types(
engine, friendly_names=False,
)
sa_type = supported_types.get(column_type)
if sa_type is None:
logger.warning("Requested type not supported. falling back to String")
sa_type = supported_types[alteration.STRING]
logger.warning("Requested type not supported. falling back to VARCHAR")
sa_type = supported_types["VARCHAR"]
table = tables.reflect_table_from_oid(table_oid, engine)
column = MathesarColumn(
column_data[NAME], sa_type, nullable=column_nullable,
Expand Down Expand Up @@ -175,6 +177,7 @@ def retype_column(table_oid, column_index, new_type, engine):
table.columns[column_index].name,
new_type,
engine,
friendly_names=False,
)
return tables.reflect_table_from_oid(table_oid, engine).columns[column_index]

Expand Down
5 changes: 3 additions & 2 deletions db/tests/test_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,15 @@ def test_retype_column_correct_column(engine_with_schema):
table_name,
target_column_name,
"boolean",
engine
engine,
friendly_names=False,
)


def test_create_column(engine_with_schema):
engine, schema = engine_with_schema
table_name = "atableone"
target_type = "boolean"
target_type = "BOOLEAN"
initial_column_name = "original_column"
new_column_name = "added_column"
table = Table(
Expand Down
13 changes: 10 additions & 3 deletions db/types/alteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@ def get_supported_alter_column_types(engine, friendly_names=True):


def alter_column_type(
schema, table_name, column_name, target_type_str, engine
schema,
table_name,
column_name,
target_type_str,
engine,
friendly_names=True
):
_preparer = engine.dialect.identifier_preparer
supported_types = get_supported_alter_column_types(engine)
target_type = supported_types.get(target_type_str.lower())
supported_types = get_supported_alter_column_types(
engine, friendly_names=friendly_names
)
target_type = supported_types.get(target_type_str)

with engine.begin() as conn:
metadata = MetaData(bind=engine, schema=schema)
Expand Down
2 changes: 1 addition & 1 deletion mathesar/tests/views/api/test_column_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_column_update_type(column_test_table, client):

def test_column_update_type_invalid_cast(column_test_table, client):
cache.clear()
type_ = "email"
type_ = "mathesar_types.email"
data = {"type": type_}
response = client.patch(
f"/api/v0/tables/{column_test_table.id}/columns/1/", data=data
Expand Down
2 changes: 1 addition & 1 deletion mathesar/utils/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_table_column_types(table):
schema = table.schema
types = infer_table_column_types(schema.name, table.name, schema._sa_engine)
col_types = {
col.name: t.__name__
col.name: t().compile(dialect=schema._sa_engine.dialect)
for col, t in zip(table.sa_columns, types)
if not MathesarColumn.from_column(col).is_default
and not col.primary_key
Expand Down