feat(ingest): Add option to change name of database for postgres (#2898)

This commit is contained in:
aseembansal-gogo 2021-07-20 19:31:42 +05:30 committed by GitHub
parent 267efe767f
commit 6e1b2cf4f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 0 deletions

View File

@ -313,6 +313,7 @@ Extracts:
- List of databases, schema, and tables
- Column types associated with each table
- Also supports PostGIS extensions
- database_alias (optional) can be used to change the name of database to be ingested
```yml
source:
@ -322,6 +323,7 @@ source:
password: pass
host_port: localhost:5432
database: DemoDatabase
database_alias: DatabaseNameToBeIngested
include_views: True # whether to include views, defaults to True
# table_pattern/schema_pattern is same as above
# options is same as above

View File

@ -151,6 +151,7 @@ base_dev_requirements = {
"looker",
"glue",
"oracle",
"postgres",
"sagemaker",
"datahub-kafka",
"datahub-rest",

View File

@ -29,6 +29,8 @@ class PostgresConfig(BasicSQLAlchemyConfig):
def get_identifier(self, schema: str, table: str) -> str:
regular = f"{schema}.{table}"
if self.database_alias:
return f"{self.database_alias}.{regular}"
if self.database:
return f"{self.database}.{regular}"
return regular

View File

@ -120,6 +120,7 @@ class BasicSQLAlchemyConfig(SQLAlchemyConfig):
password: Optional[pydantic.SecretStr] = None
host_port: str
database: Optional[str] = None
database_alias: Optional[str] = None
scheme: str
def get_sql_alchemy_url(self, uri_opts=None):

View File

@ -0,0 +1,21 @@
from datahub.ingestion.source.postgres import PostgresConfig
def _base_config():
return {"username": "user", "password": "password", "host_port": "host:1521"}
def test_database_alias_takes_precendence():
config = PostgresConfig.parse_obj(
{
**_base_config(),
"database_alias": "ops_database",
"database": "postgres",
}
)
assert config.get_identifier("superset", "logs") == "ops_database.superset.logs"
def test_database_in_identifier():
config = PostgresConfig.parse_obj({**_base_config(), "database": "postgres"})
assert config.get_identifier("superset", "logs") == "postgres.superset.logs"