@@ -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