Skip to content

Commit

Permalink
refactor generate_postgres_schema to better support both with-schema …
Browse files Browse the repository at this point in the history
…and schemaless postgres metadata generation
  • Loading branch information
rishsriv committed Sep 16, 2024
1 parent 168ac0a commit 297ad85
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
43 changes: 22 additions & 21 deletions defog/generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,28 @@ def generate_postgres_schema(
table_columns = {}

# get the columns for each table
for schema in schemas:
for table_name in tables:
if "." in table_name:
_, table_name = table_name.split(".", 1)
cur.execute(
"SELECT CAST(column_name AS TEXT), CAST(data_type AS TEXT) FROM information_schema.columns WHERE table_name::text = %s AND table_schema = %s;",
(
table_name,
schema,
),
)
rows = cur.fetchall()
rows = [row for row in rows]
rows = [{"column_name": i[0], "data_type": i[1]} for i in rows]
if len(rows) > 0:
if scan:
rows = identify_categorical_columns(cur, table_name, rows)
if schema == "public":
table_columns[table_name] = rows
else:
table_columns[schema + "." + table_name] = rows
for table_name in tables:
if "." in table_name:
schema, table_name = table_name.split(".", 1)
else:
schema = "public"
cur.execute(
"SELECT CAST(column_name AS TEXT), CAST(data_type AS TEXT) FROM information_schema.columns WHERE table_name::text = %s AND table_schema = %s;",
(
table_name,
schema,
),
)
rows = cur.fetchall()
rows = [row for row in rows]
rows = [{"column_name": i[0], "data_type": i[1]} for i in rows]
if len(rows) > 0:
if scan:
rows = identify_categorical_columns(cur, table_name, rows)
if schema == "public":
table_columns[table_name] = rows
else:
table_columns[schema + "." + table_name] = rows
conn.close()

print(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def package_files(directory):
name="defog",
packages=find_packages(),
package_data={"defog": ["gcp/*", "aws/*"] + next_static_files},
version="0.65.14",
version="0.65.16",
description="Defog is a Python library that helps you generate data queries from natural language questions.",
author="Full Stack Data Pte. Ltd.",
license="MIT",
Expand Down

0 comments on commit 297ad85

Please sign in to comment.