Generate table metadata
(table_name)
| 246 | |
| 247 | |
| 248 | def generate_table_metadata(table_name): |
| 249 | """ |
| 250 | Generate table metadata |
| 251 | """ |
| 252 | try: |
| 253 | with ENGINE.connect() as connection: |
| 254 | connection = connection.execution_options( |
| 255 | postgresql_readonly=True |
| 256 | ) |
| 257 | with connection.begin(): |
| 258 | sql_text = text(f""" |
| 259 | SELECT column_name, data_type, udt_name |
| 260 | FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '{table_name}'; |
| 261 | """) |
| 262 | result = connection.execute(sql_text) |
| 263 | |
| 264 | rows = [list(r) for r in result.all()] |
| 265 | |
| 266 | columns_metadata = [] |
| 267 | for row in rows: |
| 268 | if row[1] == "USER-DEFINED": |
| 269 | column_type = row[2] |
| 270 | else: |
| 271 | column_type = row[1] |
| 272 | columns_metadata.append({ |
| 273 | "name": row[0], |
| 274 | "type": column_type |
| 275 | }) |
| 276 | |
| 277 | |
| 278 | # TODO: generate table description |
| 279 | # TODO: generate column description (FK, PK, etc.) |
| 280 | table_description = "" |
| 281 | return { |
| 282 | "name": table_name, |
| 283 | "description": table_description, |
| 284 | "columns": columns_metadata |
| 285 | } |
| 286 | |
| 287 | except Exception as e: |
| 288 | print(e) |
| 289 | return None |
no outgoing calls
no test coverage detected