Skip to content

Commit 8d1bcf2

Browse files
* This should fix piccolo-orm#242 * add test for unknown column types Co-authored-by: Daniel Townsend <[email protected]>
1 parent d861bb5 commit 8d1bcf2

File tree

2 files changed

+68
-41
lines changed

2 files changed

+68
-41
lines changed

piccolo/apps/schema/commands/generate.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -309,52 +309,52 @@ class Schema(Table, db=engine):
309309
data_type = pg_row_meta.data_type
310310
column_type = COLUMN_TYPE_MAP.get(data_type, None)
311311
column_name = pg_row_meta.column_name
312-
313-
if column_type:
314-
kwargs: t.Dict[str, t.Any] = {
315-
"null": pg_row_meta.is_nullable == "YES",
316-
"unique": constraints.is_unique(column_name=column_name),
317-
}
318-
319-
if constraints.is_primary_key(column_name=column_name):
320-
kwargs["primary_key"] = True
321-
if column_type == Integer:
322-
column_type = Serial
323-
324-
if constraints.is_foreign_key(column_name=column_name):
325-
fk_constraint_name = (
326-
constraints.get_foreign_key_constraint_name(
327-
column_name=column_name
328-
)
329-
)
330-
column_type = ForeignKey
331-
referenced_tablename = await get_foreign_key_reference(
332-
table_class=Schema, constraint_name=fk_constraint_name
312+
if not column_type:
313+
warnings.append(f"{tablename}.{column_name} ['{data_type}']")
314+
column_type = Column
315+
316+
kwargs: t.Dict[str, t.Any] = {
317+
"null": pg_row_meta.is_nullable == "YES",
318+
"unique": constraints.is_unique(column_name=column_name),
319+
}
320+
321+
if constraints.is_primary_key(column_name=column_name):
322+
kwargs["primary_key"] = True
323+
if column_type == Integer:
324+
column_type = Serial
325+
326+
if constraints.is_foreign_key(column_name=column_name):
327+
fk_constraint_name = (
328+
constraints.get_foreign_key_constraint_name(
329+
column_name=column_name
333330
)
334-
if referenced_tablename:
335-
kwargs["references"] = create_table_class(
336-
_snake_to_camel(referenced_tablename)
337-
)
338-
else:
339-
kwargs["references"] = ForeignKeyPlaceholder
340-
341-
imports.add(
342-
"from piccolo.columns.column_types import "
343-
+ column_type.__name__
344331
)
332+
column_type = ForeignKey
333+
referenced_tablename = await get_foreign_key_reference(
334+
table_class=Schema, constraint_name=fk_constraint_name
335+
)
336+
if referenced_tablename:
337+
kwargs["references"] = create_table_class(
338+
_snake_to_camel(referenced_tablename)
339+
)
340+
else:
341+
kwargs["references"] = ForeignKeyPlaceholder
342+
343+
imports.add(
344+
"from piccolo.columns.column_types import "
345+
+ column_type.__name__
346+
)
345347

346-
if column_type is Varchar:
347-
kwargs["length"] = pg_row_meta.character_maximum_length
348+
if column_type is Varchar:
349+
kwargs["length"] = pg_row_meta.character_maximum_length
348350

349-
column = column_type(**kwargs)
351+
column = column_type(**kwargs)
350352

351-
serialised_params = serialise_params(column._meta.params)
352-
for extra_import in serialised_params.extra_imports:
353-
imports.add(extra_import.__repr__())
353+
serialised_params = serialise_params(column._meta.params)
354+
for extra_import in serialised_params.extra_imports:
355+
imports.add(extra_import.__repr__())
354356

355-
columns[column_name] = column
356-
else:
357-
warnings.append(f"{tablename}.{column_name} ['{data_type}']")
357+
columns[column_name] = column
358358

359359
table = create_table_class(
360360
class_name=_snake_to_camel(tablename),

tests/apps/schema/commands/test_generate.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
generate,
1111
get_output_schema,
1212
)
13+
from piccolo.columns.base import Column
1314
from piccolo.columns.column_types import Varchar
1415
from piccolo.table import Table
1516
from piccolo.utils.sync import run_sync
@@ -71,7 +72,7 @@ def test_get_output_schema(self):
7172
self._compare_table_columns(SmallTable, SmallTable_)
7273

7374
@patch("piccolo.apps.schema.commands.generate.print")
74-
def test_generate(self, print_: MagicMock):
75+
def test_generate_command(self, print_: MagicMock):
7576
"""
7677
Test the main generate command runs without errors.
7778
"""
@@ -81,3 +82,29 @@ def test_generate(self, print_: MagicMock):
8182
# Make sure the output is valid Python code (will raise a SyntaxError
8283
# exception otherwise).
8384
ast.parse(file_contents)
85+
86+
def test_unknown_column_type(self):
87+
"""
88+
Make sure unknown column types are handled gracefully.
89+
"""
90+
91+
class Box(Column):
92+
"""
93+
A column type which isn't supported by Piccolo officially yet.
94+
"""
95+
96+
pass
97+
98+
MegaTable.alter().add_column("box", Box()).run_sync()
99+
100+
output_schema: OutputSchema = run_sync(get_output_schema())
101+
102+
# Make sure there's a warning.
103+
self.assertEqual(output_schema.warnings, ["mega_table.box ['box']"])
104+
105+
# Make sure the column type of the generated table is just ``Column``.
106+
for table in output_schema.tables:
107+
if table.__name__ == "MegaTable":
108+
self.assertEqual(
109+
output_schema.tables[1].box.__class__.__name__, "Column"
110+
)

0 commit comments

Comments
 (0)