Skip to content

Commit

Permalink
Add option to change name of database
Browse files Browse the repository at this point in the history
  • Loading branch information
aseembansal-gogo committed Jul 16, 2021
1 parent 44ed2f3 commit ce1e6e0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions metadata-ingestion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ Extracts:
- List of databases, schema, and tables
- Column types associated with each table
- Also supports PostGIS extensions
- database_identifer (optional) can be used to change the name of database to be ingested

```yml
source:
Expand All @@ -322,6 +323,7 @@ source:
password: pass
host_port: localhost:5432
database: DemoDatabase
database_identifer: DatabaseNameToBeIngested
include_views: True # whether to include views, defaults to True
# table_pattern/schema_pattern is same as above
# options is same as above
Expand Down
2 changes: 2 additions & 0 deletions metadata-ingestion/src/datahub/ingestion/source/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class PostgresConfig(BasicSQLAlchemyConfig):

def get_identifier(self, schema: str, table: str) -> str:
regular = f"{schema}.{table}"
if self.database_identifier:
return f"{self.database_identifier}.{regular}"
if self.database:
return f"{self.database}.{regular}"
return regular
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class BasicSQLAlchemyConfig(SQLAlchemyConfig):
password: Optional[pydantic.SecretStr] = None
host_port: str
database: Optional[str] = None
database_identifier: Optional[str] = None
scheme: str

def get_sql_alchemy_url(self, uri_opts=None):
Expand Down
21 changes: 21 additions & 0 deletions metadata-ingestion/tests/unit/test_postgres_source.py
Original file line number Diff line number Diff line change
@@ -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_identifier_takes_precendence():
config = PostgresConfig.parse_obj(
{
**_base_config(),
"database_identifier": "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"

0 comments on commit ce1e6e0

Please sign in to comment.