Skip to content

Commit e159f5a

Browse files
committed
fix(ibis): added primary key management on postgres and updated mssql and mysql to manage composite keys
1 parent e55e8f5 commit e159f5a

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

ibis-server/app/model/metadata/dto.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Table(BaseModel):
7676
columns: list[Column]
7777
description: str | None = None
7878
properties: TableProperties = None
79-
primaryKey: str | None = None
79+
primaryKey: list[str] | None = None
8080

8181

8282
class ConstraintType(Enum):

ibis-server/app/model/metadata/mssql.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_table_list(self) -> list[Table]:
8181
catalog=row["catalog"],
8282
table=row["table_name"],
8383
),
84-
primaryKey="",
84+
primaryKey=[],
8585
)
8686

8787
# table exists, and add column to the table
@@ -96,7 +96,8 @@ def get_table_list(self) -> list[Table]:
9696
)
9797
# if column is primary key
9898
if row["is_pk"] == "YES":
99-
unique_tables[schema_table].primaryKey = row["column_name"]
99+
unique_tables[schema_table].primaryKey.append(row["column_name"])
100+
100101
return list(unique_tables.values())
101102

102103
def get_constraints(self) -> list[Constraint]:

ibis-server/app/model/metadata/mysql.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def get_table_list(self) -> list[Table]:
5656
catalog="",
5757
table=row["table_name"],
5858
),
59-
primaryKey="",
59+
primaryKey=[],
6060
)
6161

6262
# table exists, and add column to the table
@@ -71,7 +71,7 @@ def get_table_list(self) -> list[Table]:
7171
)
7272
# if column is primary key
7373
if row["column_key"] == "PRI":
74-
unique_tables[schema_table].primaryKey = row["column_name"]
74+
unique_tables[schema_table].primaryKey.append(row["column_name"])
7575
return list(unique_tables.values())
7676

7777
def get_constraints(self) -> list[Constraint]:

ibis-server/app/model/metadata/oracle.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def get_table_list(self) -> list[Table]:
8888
catalog="", # Oracle doesn't use catalogs.
8989
table=row["TABLE_NAME"],
9090
),
91-
primaryKey="",
91+
primaryKey=[],
9292
)
9393

9494
unique_tables[schema_table].columns.append(
@@ -100,6 +100,8 @@ def get_table_list(self) -> list[Table]:
100100
properties=None,
101101
)
102102
)
103+
104+
# TODO: manage primary key
103105

104106
return list(unique_tables.values())
105107

ibis-server/app/model/metadata/postgres.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def get_table_list(self) -> list[Table]:
2626
c.data_type,
2727
c.is_nullable,
2828
c.ordinal_position,
29+
tc.constraint_type,
2930
obj_description(cls.oid) AS table_comment,
3031
col_description(cls.oid, a.attnum) AS column_comment
3132
FROM
@@ -44,6 +45,12 @@ def get_table_list(self) -> list[Table]:
4445
pg_attribute a
4546
ON a.attrelid = cls.oid
4647
AND a.attname = c.column_name
48+
LEFT JOIN information_schema.key_column_usage kcu
49+
ON c.table_name = kcu.table_name
50+
AND c.column_name = kcu.column_name
51+
AND c.table_schema = kcu.table_schema
52+
LEFT JOIN information_schema.table_constraints tc
53+
ON kcu.constraint_name = tc.constraint_name
4754
WHERE
4855
t.table_type IN ('BASE TABLE', 'VIEW')
4956
AND t.table_schema NOT IN ('information_schema', 'pg_catalog');
@@ -67,7 +74,7 @@ def get_table_list(self) -> list[Table]:
6774
catalog=row["table_catalog"],
6875
table=row["table_name"],
6976
),
70-
primaryKey="",
77+
primaryKey=[],
7178
)
7279

7380
# table exists, and add column to the table
@@ -80,6 +87,10 @@ def get_table_list(self) -> list[Table]:
8087
properties=None,
8188
)
8289
)
90+
91+
if row["constraint_type"] == 'PRIMARY KEY':
92+
unique_tables[schema_table].primaryKey.append(row["column_name"])
93+
8394
return list(unique_tables.values())
8495

8596
def get_constraints(self) -> list[Constraint]:

0 commit comments

Comments
 (0)