Skip to content

Commit 77c0fa1

Browse files
committed
added a test for setting a column as nullable via a MigrationManager
1 parent 06985e3 commit 77c0fa1

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

tests/apps/migrations/auto/test_migration_manager.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,36 @@ def test_alter_column_unique(self):
310310
response = self.run_sync("SELECT name FROM manager;")
311311
self.assertEqual(response, [{"name": "Dave"}, {"name": "Dave"}])
312312

313+
@postgres_only
314+
def test_alter_column_set_null(self):
315+
"""
316+
Test altering whether a column is nullable with MigrationManager.
317+
"""
318+
manager = MigrationManager()
319+
320+
manager.alter_column(
321+
table_class_name="Manager",
322+
tablename="manager",
323+
column_name="name",
324+
params={"null": True},
325+
old_params={"null": False},
326+
)
327+
328+
asyncio.run(manager.run())
329+
self.assertTrue(
330+
self.get_postgres_is_nullable(
331+
tablename="manager", column_name="name"
332+
)
333+
)
334+
335+
# Reverse
336+
asyncio.run(manager.run_backwards())
337+
self.assertFalse(
338+
self.get_postgres_is_nullable(
339+
tablename="manager", column_name="name"
340+
)
341+
)
342+
313343
def _get_column_precision_and_scale(
314344
self, tablename="ticket", column_name="price"
315345
):

tests/base.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,46 @@ def table_exists(self, tablename: str) -> bool:
5555
_Table._meta.tablename = tablename
5656
return _Table.table_exists().run_sync()
5757

58-
def get_postgres_column_type_str(
58+
###########################################################################
59+
60+
# Postgres specific utils
61+
62+
def get_postgres_column_definition(
5963
self, tablename: str, column_name: str
60-
) -> str:
61-
"""
62-
Fetches the column type as a string, from the database.
63-
"""
64+
) -> t.Dict[str, t.Any]:
6465
query = """
65-
SELECT data_type FROM information_schema.columns
66+
SELECT * FROM information_schema.columns
6667
WHERE table_name = '{tablename}'
6768
AND table_catalog = 'piccolo'
6869
AND column_name = '{column_name}'
6970
""".format(
7071
tablename=tablename, column_name=column_name
7172
)
7273
response = self.run_sync(query)
73-
return response[0]["data_type"].upper()
74+
return response[0]
75+
76+
def get_postgres_column_type_str(
77+
self, tablename: str, column_name: str
78+
) -> str:
79+
"""
80+
Fetches the column type as a string, from the database.
81+
"""
82+
return self.get_postgres_column_definition(
83+
tablename=tablename, column_name=column_name
84+
)["data_type"].upper()
85+
86+
def get_postgres_is_nullable(self, tablename, column_name: str) -> bool:
87+
"""
88+
Fetches whether the column is defined as nullable, from the database.
89+
"""
90+
return (
91+
self.get_postgres_column_definition(
92+
tablename=tablename, column_name=column_name
93+
)["is_nullable"].upper()
94+
== "YES"
95+
)
96+
97+
###########################################################################
7498

7599
def create_tables(self):
76100
if ENGINE.engine_type == "postgres":

0 commit comments

Comments
 (0)