Repository: reata/sqllineage
Pull request #555 (2024-01-16 03:05:27)

Set default schema
fix  https://github.com/reata/sqllineage/issues/536

please check . thanks 

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/reata/sqllineage/pull/555?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`06a474b`)](https://app.codecov.io/gh/reata/sqllineage/commit/06a474b5e7d6a26994db18f97cf9058246cd24bd?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) 99.50% compared to head [(`b226751`)](https://app.codecov.io/gh/reata/sqllineage/pull/555?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) 99.50%.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master     #555   +/-   ##
=======================================
  Coverage   99.50%   99.50%           
=======================================
  Files          41       41           
  Lines        2211     2214    +3     
=======================================
+ Hits         2200     2203    +3     
  Misses         11       11           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/reata/sqllineage/pull/555?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).

reata: Hold until we get #553 merged

Patch:
diff --git a/docs/gear_up/configuration.rst b/docs/gear_up/configuration.rst
index 8653358..a0dbd02 100644
--- a/docs/gear_up/configuration.rst
+++ b/docs/gear_up/configuration.rst
@@ -7,6 +7,24 @@ The SQLLineage configuration allows user to customize the behaviour of sqllineag
 We adopt environment variable approach for global key-value mapping. The keys listed in this section should start with
 `"SQLLINEAGE_"` to be a valid config. For example, to use DEFAULT_SCHEMA, use ``SQLLINEAGE_DEFAULT_SCHEMA=default``.
 
+.. note::
+     Starting v1.5.2, we also support changing config at runtime. A local config is kept for each thread that will mask
+     global config. Local configuration must be set using context manager:
+
+     .. code-block:: python
+
+        >>> from sqllineage.config import SQLLineageConfig
+        >>> from sqllineage.runner import LineageRunner
+
+        >>> with SQLLineageConfig(DEFAULT_SCHEMA="ods"):
+        >>>     print(LineageRunner("select * from test").source_tables)
+        [Table: ods.test]
+        >>> with SQLLineageConfig(DEFAULT_SCHEMA="dwd"):
+        >>>     print(LineageRunner("select * from test").source_tables)
+        [Table: dwd.test]
+
+     Note when setting local config, the key does not start with `"SQLLINEAGE_"`.
+
 DEFAULT_SCHEMA
 ==============
 Default schema, or interchangeably called database. Tables without schema qualifier parsed from SQL is set with a schema
diff --git a/sqllineage/core/models.py b/sqllineage/core/models.py
index 01e6985..49ce17a 100644
--- a/sqllineage/core/models.py
+++ b/sqllineage/core/models.py
@@ -12,13 +12,17 @@ class Schema:
     """
 
     unknown = "<default>"
-    default = SQLLineageConfig.DEFAULT_SCHEMA or unknown
 
-    def __init__(self, name: str = default):
+    def __init__(self, name: Optional[str] = None):
         """
         :param name: schema name
         """
-        self.raw_name = escape_identifier_name(name)
+        if name:
+            self.raw_name = escape_identifier_name(name)
+        elif SQLLineageConfig.DEFAULT_SCHEMA:
+            self.raw_name = escape_identifier_name(SQLLineageConfig.DEFAULT_SCHEMA)
+        else:
+            self.raw_name = escape_identifier_name(Schema.unknown)
 
     def __str__(self):
         return self.raw_name
@@ -33,7 +37,7 @@ class Schema:
         return hash(str(self))
 
     def __bool__(self):
-        return str(self) != self.default
+        return str(self) != self.unknown
 
 
 class Table:


Test patch:
diff --git a/tests/core/test_config.py b/tests/core/test_config.py
index 9d7ed46..e3b96e6 100644
--- a/tests/core/test_config.py
+++ b/tests/core/test_config.py
@@ -8,6 +8,7 @@ import pytest
 
 from sqllineage.config import SQLLineageConfig
 from sqllineage.exceptions import ConfigException
+from sqllineage.runner import LineageRunner
 
 
 @patch(
@@ -64,9 +65,10 @@ def test_disable_update_unknown_config():
 def _check_schema(schema: str):
     # used by test_config_parallel, must be a global function so that it can be pickled between processes
     with SQLLineageConfig(DEFAULT_SCHEMA=schema):
+        table = LineageRunner("select * from test").source_tables.pop()
         # randomly sleep [0, 0.1) second to simulate real parsing scenario
         time.sleep(random.random() * 0.1)
-        return SQLLineageConfig.DEFAULT_SCHEMA
+        return table.schema.raw_name
 
 
 @pytest.mark.parametrize("pool", ["ThreadPoolExecutor", "ProcessPoolExecutor"])