mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-09-25 17:04:54 +00:00
fixed mssql comments (#12546)
This commit is contained in:
parent
8feada22de
commit
94166aeb84
@ -55,23 +55,6 @@ WHERE ep.name = 'MS_Description'
|
|||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
MSSQL_GET_COLUMN_COMMENTS = textwrap.dedent(
|
|
||||||
"""
|
|
||||||
SELECT obj.name AS TableName,
|
|
||||||
col.name AS ColumnName,
|
|
||||||
ep.value AS ColumnComment,
|
|
||||||
s.name AS schema_name
|
|
||||||
FROM sys.tables AS obj
|
|
||||||
INNER JOIN sys.columns AS col ON obj.object_id = col.object_id
|
|
||||||
INNER JOIN sys.extended_properties AS ep ON col.object_id = ep.major_id AND col.column_id = ep.minor_id
|
|
||||||
JOIN sys.schemas AS s
|
|
||||||
ON obj.schema_id = s.schema_id
|
|
||||||
WHERE ep.name = 'MS_Description'
|
|
||||||
AND obj.name = '{table_name}'
|
|
||||||
And s.name = '{schema_name}';
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
MSSQL_ALL_VIEW_DEFINITIONS = textwrap.dedent(
|
MSSQL_ALL_VIEW_DEFINITIONS = textwrap.dedent(
|
||||||
"""
|
"""
|
||||||
select
|
select
|
||||||
|
@ -11,9 +11,8 @@
|
|||||||
"""
|
"""
|
||||||
MSSQL SQLAlchemy Helper Methods
|
MSSQL SQLAlchemy Helper Methods
|
||||||
"""
|
"""
|
||||||
import traceback
|
|
||||||
|
|
||||||
from sqlalchemy import sql
|
from sqlalchemy import Column, Integer, MetaData, String, Table, alias, sql
|
||||||
from sqlalchemy import types as sqltypes
|
from sqlalchemy import types as sqltypes
|
||||||
from sqlalchemy import util
|
from sqlalchemy import util
|
||||||
from sqlalchemy.dialects.mssql import information_schema as ischema
|
from sqlalchemy.dialects.mssql import information_schema as ischema
|
||||||
@ -35,7 +34,6 @@ from sqlalchemy.util import compat
|
|||||||
|
|
||||||
from metadata.ingestion.source.database.mssql.queries import (
|
from metadata.ingestion.source.database.mssql.queries import (
|
||||||
MSSQL_ALL_VIEW_DEFINITIONS,
|
MSSQL_ALL_VIEW_DEFINITIONS,
|
||||||
MSSQL_GET_COLUMN_COMMENTS,
|
|
||||||
MSSQL_GET_TABLE_COMMENTS,
|
MSSQL_GET_TABLE_COMMENTS,
|
||||||
)
|
)
|
||||||
from metadata.utils.logger import ingestion_logger
|
from metadata.utils.logger import ingestion_logger
|
||||||
@ -84,6 +82,26 @@ def get_columns(
|
|||||||
|
|
||||||
computed_cols = ischema.computed_columns
|
computed_cols = ischema.computed_columns
|
||||||
identity_cols = ischema.identity_columns
|
identity_cols = ischema.identity_columns
|
||||||
|
sqlalchemy_metadata = MetaData()
|
||||||
|
extended_properties = Table(
|
||||||
|
"extended_properties",
|
||||||
|
sqlalchemy_metadata,
|
||||||
|
Column("major_id", Integer, primary_key=True),
|
||||||
|
Column("minor_id", Integer, primary_key=True),
|
||||||
|
Column("name", String, primary_key=True),
|
||||||
|
Column("value", String),
|
||||||
|
schema="sys",
|
||||||
|
)
|
||||||
|
sys_columns = alias(
|
||||||
|
Table(
|
||||||
|
"columns",
|
||||||
|
sqlalchemy_metadata,
|
||||||
|
Column("object_id", Integer, primary_key=True),
|
||||||
|
Column("name", String, primary_key=True),
|
||||||
|
Column("column_id", Integer, primary_key=True),
|
||||||
|
schema="sys",
|
||||||
|
)
|
||||||
|
)
|
||||||
if owner:
|
if owner:
|
||||||
whereclause = sql.and_(
|
whereclause = sql.and_(
|
||||||
columns.c.table_name == tablename,
|
columns.c.table_name == tablename,
|
||||||
@ -94,21 +112,45 @@ def get_columns(
|
|||||||
whereclause = columns.c.table_name == tablename
|
whereclause = columns.c.table_name == tablename
|
||||||
full_name = columns.c.table_name
|
full_name = columns.c.table_name
|
||||||
|
|
||||||
join = columns.join(
|
# adding the condition for fetching column comments
|
||||||
|
whereclause.and_(extended_properties.c.name == "MS_Description")
|
||||||
|
|
||||||
|
join = (
|
||||||
|
columns.join(
|
||||||
computed_cols,
|
computed_cols,
|
||||||
onclause=sql.and_(
|
onclause=sql.and_(
|
||||||
computed_cols.c.object_id == func.object_id(full_name),
|
computed_cols.c.object_id == func.object_id(full_name),
|
||||||
computed_cols.c.name == columns.c.column_name.collate("DATABASE_DEFAULT"),
|
computed_cols.c.name
|
||||||
|
== columns.c.column_name.collate("DATABASE_DEFAULT"),
|
||||||
),
|
),
|
||||||
isouter=True,
|
isouter=True,
|
||||||
).join(
|
)
|
||||||
|
.join(
|
||||||
identity_cols,
|
identity_cols,
|
||||||
onclause=sql.and_(
|
onclause=sql.and_(
|
||||||
identity_cols.c.object_id == func.object_id(full_name),
|
identity_cols.c.object_id == func.object_id(full_name),
|
||||||
identity_cols.c.name == columns.c.column_name.collate("DATABASE_DEFAULT"),
|
identity_cols.c.name
|
||||||
|
== columns.c.column_name.collate("DATABASE_DEFAULT"),
|
||||||
),
|
),
|
||||||
isouter=True,
|
isouter=True,
|
||||||
)
|
)
|
||||||
|
.join(
|
||||||
|
sys_columns,
|
||||||
|
onclause=sql.and_(
|
||||||
|
sys_columns.c.object_id == func.object_id(full_name),
|
||||||
|
sys_columns.c.name == columns.c.column_name.collate("DATABASE_DEFAULT"),
|
||||||
|
),
|
||||||
|
isouter=True,
|
||||||
|
)
|
||||||
|
.join(
|
||||||
|
extended_properties,
|
||||||
|
onclause=sql.and_(
|
||||||
|
extended_properties.c.major_id == sys_columns.c.object_id,
|
||||||
|
extended_properties.c.minor_id == sys_columns.c.column_id,
|
||||||
|
),
|
||||||
|
isouter=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
if self._supports_nvarchar_max: # pylint: disable=protected-access
|
if self._supports_nvarchar_max: # pylint: disable=protected-access
|
||||||
computed_definition = computed_cols.c.definition
|
computed_definition = computed_cols.c.definition
|
||||||
@ -124,6 +166,7 @@ def get_columns(
|
|||||||
identity_cols.c.is_identity,
|
identity_cols.c.is_identity,
|
||||||
identity_cols.c.seed_value,
|
identity_cols.c.seed_value,
|
||||||
identity_cols.c.increment_value,
|
identity_cols.c.increment_value,
|
||||||
|
extended_properties.c.value,
|
||||||
)
|
)
|
||||||
.where(whereclause)
|
.where(whereclause)
|
||||||
.select_from(join)
|
.select_from(join)
|
||||||
@ -149,9 +192,10 @@ def get_columns(
|
|||||||
is_identity = row[identity_cols.c.is_identity]
|
is_identity = row[identity_cols.c.is_identity]
|
||||||
identity_start = row[identity_cols.c.seed_value]
|
identity_start = row[identity_cols.c.seed_value]
|
||||||
identity_increment = row[identity_cols.c.increment_value]
|
identity_increment = row[identity_cols.c.increment_value]
|
||||||
|
comment = row[extended_properties.c.value]
|
||||||
|
|
||||||
coltype = self.ischema_names.get(type_, None)
|
coltype = self.ischema_names.get(type_, None)
|
||||||
comment = None
|
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
if coltype in (
|
if coltype in (
|
||||||
MSString,
|
MSString,
|
||||||
@ -225,15 +269,6 @@ def get_columns(
|
|||||||
}
|
}
|
||||||
|
|
||||||
cols.append(cdict)
|
cols.append(cdict)
|
||||||
cursor = connection.execute(
|
|
||||||
MSSQL_GET_COLUMN_COMMENTS.format(schema_name=schema, table_name=tablename)
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
for index, result in enumerate(cursor):
|
|
||||||
if result[2]:
|
|
||||||
cols[index]["comment"] = result[2]
|
|
||||||
except Exception:
|
|
||||||
logger.debug(traceback.format_exc())
|
|
||||||
return cols
|
return cols
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user