Skip to content

Commit 2c4ee2a

Browse files
committed
created method for retrieving column type from postgres
1 parent 5eb512c commit 2c4ee2a

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

tests/base.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,28 @@ def run_sync(self, query):
5050
_Table = type("_Table", (Table,), {})
5151
return _Table.raw(query).run_sync()
5252

53-
def table_exists(self, tablename: str):
53+
def table_exists(self, tablename: str) -> bool:
5454
_Table: t.Type[Table] = type(tablename.upper(), (Table,), {})
5555
_Table._meta.tablename = tablename
5656
return _Table.table_exists().run_sync()
5757

58+
def get_postgres_column_type_str(
59+
self, tablename: str, column_name: str
60+
) -> str:
61+
"""
62+
Fetches the column type as a string, from the database.
63+
"""
64+
query = """
65+
SELECT data_type FROM information_schema.columns
66+
WHERE table_name = '{tablename}'
67+
AND table_catalog = 'piccolo'
68+
AND column_name = '{column_name}'
69+
""".format(
70+
tablename=tablename, column_name=column_name
71+
)
72+
response = self.run_sync(query)
73+
return response[0]["data_type"].upper()
74+
5875
def create_tables(self):
5976
if ENGINE.engine_type == "postgres":
6077
self.run_sync(

tests/table/test_alter.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,12 @@ def test_integer_to_bigint(self):
151151
)
152152
alter_query.run_sync()
153153

154-
query = """
155-
SELECT data_type FROM information_schema.columns
156-
WHERE table_name = 'band'
157-
AND table_catalog = 'piccolo'
158-
AND column_name = 'popularity'
159-
"""
160-
161-
response = Band.raw(query).run_sync()
162-
self.assertEqual(response[0]["data_type"].upper(), "BIGINT")
154+
self.assertEqual(
155+
self.get_postgres_column_type_str(
156+
tablename="band", column_name="popularity"
157+
),
158+
"BIGINT",
159+
)
163160

164161
popularity = (
165162
Band.select(Band.popularity).first().run_sync()["popularity"]
@@ -177,15 +174,12 @@ def test_integer_to_varchar(self):
177174
)
178175
alter_query.run_sync()
179176

180-
query = """
181-
SELECT data_type FROM information_schema.columns
182-
WHERE table_name = 'band'
183-
AND table_catalog = 'piccolo'
184-
AND column_name = 'popularity'
185-
"""
186-
187-
response = Band.raw(query).run_sync()
188-
self.assertEqual(response[0]["data_type"].upper(), "CHARACTER VARYING")
177+
self.assertEqual(
178+
self.get_postgres_column_type_str(
179+
tablename="band", column_name="popularity"
180+
),
181+
"CHARACTER VARYING",
182+
)
189183

190184
popularity = (
191185
Band.select(Band.popularity).first().run_sync()["popularity"]

0 commit comments

Comments
 (0)