datus-postgresql 0.1.6__tar.gz → 0.1.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/PKG-INFO +3 -3
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/datus_postgresql/connector.py +98 -24
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/pyproject.toml +3 -3
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/integration/test_tpch.py +12 -8
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/unit/test_connector_unit.py +107 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/.gitignore +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/README.md +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/datus_postgresql/__init__.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/datus_postgresql/config.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/datus_postgresql/handlers.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/docker-compose.yml +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/scripts/init_tpch_data.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/__init__.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/conftest.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/integration/__init__.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/integration/conftest.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/integration/test_contract.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/integration/test_integration.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/unit/__init__.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/unit/test_config.py +0 -0
- {datus_postgresql-0.1.6 → datus_postgresql-0.1.8}/tests/unit/test_migration_mixin.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: datus-postgresql
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.8
|
|
4
4
|
Summary: PostgreSQL database adapter for Datus
|
|
5
5
|
Project-URL: Homepage, https://github.com/Datus-ai/datus-db-adapters
|
|
6
6
|
Project-URL: Repository, https://github.com/Datus-ai/datus-db-adapters
|
|
@@ -14,8 +14,8 @@ Classifier: License :: OSI Approved :: Apache Software License
|
|
|
14
14
|
Classifier: Programming Language :: Python :: 3
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Requires-Python: >=3.12
|
|
17
|
-
Requires-Dist: datus-db-core>=0.1.
|
|
18
|
-
Requires-Dist: datus-sqlalchemy>=0.1.
|
|
17
|
+
Requires-Dist: datus-db-core>=0.1.5
|
|
18
|
+
Requires-Dist: datus-sqlalchemy>=0.1.7
|
|
19
19
|
Requires-Dist: psycopg2-binary>=2.9.11
|
|
20
20
|
Requires-Dist: pydantic>=2.0.0
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
@@ -144,7 +144,7 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
144
144
|
Args:
|
|
145
145
|
table_type: Type of object (table, view, mv)
|
|
146
146
|
catalog_name: Catalog name (unused in PostgreSQL)
|
|
147
|
-
database_name: Database
|
|
147
|
+
database_name: Database in which to inspect the objects
|
|
148
148
|
schema_name: Schema name to query
|
|
149
149
|
|
|
150
150
|
Returns:
|
|
@@ -211,7 +211,11 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
211
211
|
tb_name = query_result["table_name"][i]
|
|
212
212
|
result.append(
|
|
213
213
|
{
|
|
214
|
-
"identifier": self.identifier(
|
|
214
|
+
"identifier": self.identifier(
|
|
215
|
+
database_name=database_name,
|
|
216
|
+
schema_name=schema,
|
|
217
|
+
table_name=tb_name,
|
|
218
|
+
),
|
|
215
219
|
"catalog_name": "",
|
|
216
220
|
"database_name": database_name,
|
|
217
221
|
"schema_name": schema,
|
|
@@ -221,7 +225,13 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
221
225
|
)
|
|
222
226
|
return result
|
|
223
227
|
|
|
224
|
-
def _get_ddl(
|
|
228
|
+
def _get_ddl(
|
|
229
|
+
self,
|
|
230
|
+
schema_name: str,
|
|
231
|
+
table_name: str,
|
|
232
|
+
object_type: str = "TABLE",
|
|
233
|
+
database_name: str = "",
|
|
234
|
+
) -> str:
|
|
225
235
|
"""
|
|
226
236
|
Get DDL for a table/view using pg_get_tabledef or reconstructing from metadata.
|
|
227
237
|
|
|
@@ -229,11 +239,17 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
229
239
|
schema_name: Schema name
|
|
230
240
|
table_name: Table name
|
|
231
241
|
object_type: Object type (TABLE, VIEW, MATERIALIZED VIEW)
|
|
242
|
+
database_name: Database in which to inspect the object
|
|
232
243
|
|
|
233
244
|
Returns:
|
|
234
245
|
DDL statement as string
|
|
235
246
|
"""
|
|
236
|
-
|
|
247
|
+
database_name = database_name or self.database_name
|
|
248
|
+
full_name = self.full_name(
|
|
249
|
+
database_name=database_name,
|
|
250
|
+
schema_name=schema_name,
|
|
251
|
+
table_name=table_name,
|
|
252
|
+
)
|
|
237
253
|
|
|
238
254
|
safe_schema = schema_name.replace("'", "''") if schema_name else ""
|
|
239
255
|
safe_table = table_name.replace("'", "''") if table_name else ""
|
|
@@ -243,7 +259,7 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
243
259
|
sql = f"""
|
|
244
260
|
SELECT pg_get_viewdef('{safe_schema}.{safe_table}'::regclass, true) as definition
|
|
245
261
|
"""
|
|
246
|
-
result = self._execute_pandas(sql)
|
|
262
|
+
result = self._execute_pandas(sql, database_name=database_name)
|
|
247
263
|
if not result.empty and result["definition"][0]:
|
|
248
264
|
return f"CREATE VIEW {full_name} AS\n{result['definition'][0]}"
|
|
249
265
|
return f"-- DDL not available for {full_name}"
|
|
@@ -255,14 +271,18 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
255
271
|
FROM pg_matviews
|
|
256
272
|
WHERE schemaname = '{safe_schema}' AND matviewname = '{safe_table}'
|
|
257
273
|
"""
|
|
258
|
-
result = self._execute_pandas(sql)
|
|
274
|
+
result = self._execute_pandas(sql, database_name=database_name)
|
|
259
275
|
if not result.empty and result["definition"][0]:
|
|
260
276
|
return f"CREATE MATERIALIZED VIEW {full_name} AS\n{result['definition'][0]}"
|
|
261
277
|
return f"-- DDL not available for {full_name}"
|
|
262
278
|
|
|
263
279
|
else:
|
|
264
280
|
# For tables, reconstruct DDL from column info
|
|
265
|
-
columns = self.get_schema(
|
|
281
|
+
columns = self.get_schema(
|
|
282
|
+
database_name=database_name,
|
|
283
|
+
schema_name=schema_name,
|
|
284
|
+
table_name=table_name,
|
|
285
|
+
)
|
|
266
286
|
if not columns:
|
|
267
287
|
return f"-- DDL not available for {full_name}"
|
|
268
288
|
|
|
@@ -301,7 +321,7 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
301
321
|
table_type: Type of object
|
|
302
322
|
tables: Optional list of specific tables to retrieve
|
|
303
323
|
catalog_name: Catalog name (unused)
|
|
304
|
-
database_name: Database name
|
|
324
|
+
database_name: Database name
|
|
305
325
|
schema_name: Schema name
|
|
306
326
|
|
|
307
327
|
Returns:
|
|
@@ -318,7 +338,12 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
318
338
|
object_type = object_type_map.get(table_type, "TABLE")
|
|
319
339
|
|
|
320
340
|
for meta in self._get_metadata(table_type, catalog_name, database_name, schema_name):
|
|
321
|
-
|
|
341
|
+
metadata_database = meta["database_name"]
|
|
342
|
+
full_name = self.full_name(
|
|
343
|
+
database_name=metadata_database,
|
|
344
|
+
schema_name=meta["schema_name"],
|
|
345
|
+
table_name=meta["table_name"],
|
|
346
|
+
)
|
|
322
347
|
|
|
323
348
|
# Skip if not in filter list
|
|
324
349
|
if filter_tables and full_name not in filter_tables:
|
|
@@ -326,7 +351,19 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
326
351
|
|
|
327
352
|
# Get DDL
|
|
328
353
|
try:
|
|
329
|
-
|
|
354
|
+
# Keep the legacy three-argument _get_ddl dispatch compatible
|
|
355
|
+
# with independently released PostgreSQL subclasses. The
|
|
356
|
+
# ContextVar is task-local and makes the target database visible
|
|
357
|
+
# to both the base implementation and subclass metadata queries.
|
|
358
|
+
token = self._database_var.set(metadata_database)
|
|
359
|
+
try:
|
|
360
|
+
ddl = self._get_ddl(
|
|
361
|
+
meta["schema_name"],
|
|
362
|
+
meta["table_name"],
|
|
363
|
+
object_type,
|
|
364
|
+
)
|
|
365
|
+
finally:
|
|
366
|
+
self._database_var.reset(token)
|
|
330
367
|
except Exception as e:
|
|
331
368
|
logger.warning(f"Could not get DDL for {full_name}: {e}")
|
|
332
369
|
ddl = f"-- DDL not available for {meta['table_name']}"
|
|
@@ -337,21 +374,45 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
337
374
|
return result
|
|
338
375
|
|
|
339
376
|
@override
|
|
377
|
+
@staticmethod
|
|
378
|
+
def _qualify_name(meta, arg_db, arg_schema):
|
|
379
|
+
"""Prefix the table with the db/schema levels the caller left blank.
|
|
380
|
+
|
|
381
|
+
Yields ``[db.][schema.]table`` so an unscoped listing stays addressable; a level is
|
|
382
|
+
prepended only when the caller passed it empty and the row carries that coordinate.
|
|
383
|
+
"""
|
|
384
|
+
parts = []
|
|
385
|
+
if not arg_db and meta.get("database_name"):
|
|
386
|
+
parts.append(meta["database_name"])
|
|
387
|
+
if not arg_schema and meta.get("schema_name"):
|
|
388
|
+
parts.append(meta["schema_name"])
|
|
389
|
+
parts.append(meta["table_name"])
|
|
390
|
+
return ".".join(parts)
|
|
391
|
+
|
|
340
392
|
def get_tables(self, catalog_name: str = "", database_name: str = "", schema_name: str = "") -> List[str]:
|
|
341
393
|
"""Get list of table names."""
|
|
342
|
-
return [
|
|
394
|
+
return [
|
|
395
|
+
self._qualify_name(meta, database_name, schema_name)
|
|
396
|
+
for meta in self._get_metadata("table", catalog_name, database_name, schema_name)
|
|
397
|
+
]
|
|
343
398
|
|
|
344
399
|
@override
|
|
345
400
|
def get_views(self, catalog_name: str = "", database_name: str = "", schema_name: str = "") -> List[str]:
|
|
346
401
|
"""Get list of view names."""
|
|
347
|
-
return [
|
|
402
|
+
return [
|
|
403
|
+
self._qualify_name(meta, database_name, schema_name)
|
|
404
|
+
for meta in self._get_metadata("view", catalog_name, database_name, schema_name)
|
|
405
|
+
]
|
|
348
406
|
|
|
349
407
|
@override
|
|
350
408
|
def get_materialized_views(
|
|
351
409
|
self, catalog_name: str = "", database_name: str = "", schema_name: str = ""
|
|
352
410
|
) -> List[str]:
|
|
353
411
|
"""Get list of materialized view names."""
|
|
354
|
-
return [
|
|
412
|
+
return [
|
|
413
|
+
self._qualify_name(meta, database_name, schema_name)
|
|
414
|
+
for meta in self._get_metadata("mv", catalog_name, database_name, schema_name)
|
|
415
|
+
]
|
|
355
416
|
|
|
356
417
|
@override
|
|
357
418
|
def get_tables_with_ddl(
|
|
@@ -384,7 +445,7 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
384
445
|
|
|
385
446
|
Args:
|
|
386
447
|
catalog_name: Catalog name (unused)
|
|
387
|
-
database_name: Database name
|
|
448
|
+
database_name: Database name
|
|
388
449
|
schema_name: Schema name
|
|
389
450
|
table_name: Table name
|
|
390
451
|
|
|
@@ -445,9 +506,9 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
445
506
|
ORDER BY c.table_schema, c.table_name, c.ordinal_position
|
|
446
507
|
"""
|
|
447
508
|
|
|
448
|
-
query_result = self._execute_pandas(_build_sql(False))
|
|
509
|
+
query_result = self._execute_pandas(_build_sql(False), database_name=database_name)
|
|
449
510
|
if len(query_result) == 0:
|
|
450
|
-
query_result = self._execute_pandas(_build_sql(True))
|
|
511
|
+
query_result = self._execute_pandas(_build_sql(True), database_name=database_name)
|
|
451
512
|
self._raise_if_ambiguous_table(query_result, schema_name, table_name)
|
|
452
513
|
|
|
453
514
|
result = []
|
|
@@ -519,7 +580,7 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
519
580
|
database_name = database_name or self.database_name
|
|
520
581
|
safe_db = database_name.replace("'", "''") if database_name else ""
|
|
521
582
|
sql = f"SELECT schema_name FROM information_schema.schemata WHERE catalog_name = '{safe_db}'"
|
|
522
|
-
result = self._execute_pandas(sql)
|
|
583
|
+
result = self._execute_pandas(sql, database_name=database_name)
|
|
523
584
|
schemas = result["schema_name"].tolist()
|
|
524
585
|
|
|
525
586
|
if not include_sys:
|
|
@@ -648,15 +709,23 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
648
709
|
# If specific tables provided, query those
|
|
649
710
|
if tables:
|
|
650
711
|
for table_name in tables:
|
|
651
|
-
full_name = self.full_name(
|
|
712
|
+
full_name = self.full_name(
|
|
713
|
+
database_name=database_name,
|
|
714
|
+
schema_name=schema_name,
|
|
715
|
+
table_name=table_name,
|
|
716
|
+
)
|
|
652
717
|
sql = f"SELECT * FROM {full_name} LIMIT {top_n}"
|
|
653
|
-
df = self._execute_pandas(sql)
|
|
718
|
+
df = self._execute_pandas(sql, database_name=database_name)
|
|
654
719
|
if not df.empty:
|
|
655
720
|
result.append(
|
|
656
721
|
{
|
|
657
|
-
"identifier": self.identifier(
|
|
722
|
+
"identifier": self.identifier(
|
|
723
|
+
database_name=database_name,
|
|
724
|
+
schema_name=schema_name,
|
|
725
|
+
table_name=table_name,
|
|
726
|
+
),
|
|
658
727
|
"catalog_name": "",
|
|
659
|
-
"database_name": self.database_name,
|
|
728
|
+
"database_name": database_name or self.database_name,
|
|
660
729
|
"schema_name": schema_name,
|
|
661
730
|
"table_name": table_name,
|
|
662
731
|
"sample_rows": df.to_csv(index=False),
|
|
@@ -667,15 +736,20 @@ class PostgreSQLConnector(SQLAlchemyConnector, MigrationTargetMixin):
|
|
|
667
736
|
# Otherwise get metadata and query all tables
|
|
668
737
|
metadata = self._get_metadata(table_type, "", database_name, schema_name)
|
|
669
738
|
for meta in metadata:
|
|
670
|
-
|
|
739
|
+
metadata_database = meta["database_name"]
|
|
740
|
+
full_name = self.full_name(
|
|
741
|
+
database_name=metadata_database,
|
|
742
|
+
schema_name=meta["schema_name"],
|
|
743
|
+
table_name=meta["table_name"],
|
|
744
|
+
)
|
|
671
745
|
sql = f"SELECT * FROM {full_name} LIMIT {top_n}"
|
|
672
|
-
df = self._execute_pandas(sql)
|
|
746
|
+
df = self._execute_pandas(sql, database_name=metadata_database)
|
|
673
747
|
if not df.empty:
|
|
674
748
|
result.append(
|
|
675
749
|
{
|
|
676
750
|
"identifier": meta["identifier"],
|
|
677
751
|
"catalog_name": "",
|
|
678
|
-
"database_name":
|
|
752
|
+
"database_name": metadata_database,
|
|
679
753
|
"schema_name": meta["schema_name"],
|
|
680
754
|
"table_name": meta["table_name"],
|
|
681
755
|
"sample_rows": df.to_csv(index=False),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "datus-postgresql"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.8"
|
|
4
4
|
description = "PostgreSQL database adapter for Datus"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.12"
|
|
@@ -18,8 +18,8 @@ classifiers = [
|
|
|
18
18
|
]
|
|
19
19
|
|
|
20
20
|
dependencies = [
|
|
21
|
-
"datus-db-core>=0.1.
|
|
22
|
-
"datus-sqlalchemy>=0.1.
|
|
21
|
+
"datus-db-core>=0.1.5",
|
|
22
|
+
"datus-sqlalchemy>=0.1.7",
|
|
23
23
|
"psycopg2-binary>=2.9.11",
|
|
24
24
|
"pydantic>=2.0.0",
|
|
25
25
|
]
|
|
@@ -128,17 +128,21 @@ class TestTpchMetadata:
|
|
|
128
128
|
"""Validate metadata retrieval for TPC-H tables."""
|
|
129
129
|
|
|
130
130
|
def test_get_tables_includes_tpch(self, tpch_setup):
|
|
131
|
-
"""get_tables() should return TPC-H tables.
|
|
131
|
+
"""get_tables() should return TPC-H tables qualified with the database name.
|
|
132
|
+
|
|
133
|
+
Only ``schema_name`` is passed, so the listing prefixes the unscoped database
|
|
134
|
+
level, yielding ``<database>.<table>``.
|
|
135
|
+
"""
|
|
132
136
|
tables = tpch_setup.get_tables(schema_name="public")
|
|
133
|
-
|
|
137
|
+
db = tpch_setup.database_name
|
|
134
138
|
expected = {
|
|
135
|
-
"tpch_region",
|
|
136
|
-
"tpch_nation",
|
|
137
|
-
"tpch_supplier",
|
|
138
|
-
"tpch_customer",
|
|
139
|
-
"tpch_orders",
|
|
139
|
+
f"{db}.tpch_region",
|
|
140
|
+
f"{db}.tpch_nation",
|
|
141
|
+
f"{db}.tpch_supplier",
|
|
142
|
+
f"{db}.tpch_customer",
|
|
143
|
+
f"{db}.tpch_orders",
|
|
140
144
|
}
|
|
141
|
-
assert expected.issubset(
|
|
145
|
+
assert expected.issubset(set(tables))
|
|
142
146
|
|
|
143
147
|
def test_get_schema_columns(self, tpch_setup):
|
|
144
148
|
"""get_schema() should return correct columns for tpch_region."""
|
|
@@ -544,6 +544,17 @@ def _df(rows, columns):
|
|
|
544
544
|
return pd.DataFrame(rows, columns=columns)
|
|
545
545
|
|
|
546
546
|
|
|
547
|
+
def test_get_schemas_executes_in_requested_database():
|
|
548
|
+
connector = _make_pg_connector_for_metadata(database_name="default_db")
|
|
549
|
+
connector._execute_pandas = MagicMock(return_value=_df([("reporting",)], ["schema_name"]))
|
|
550
|
+
|
|
551
|
+
result = connector.get_schemas(database_name="other_db")
|
|
552
|
+
|
|
553
|
+
assert result == ["reporting"]
|
|
554
|
+
connector._execute_pandas.assert_called_once()
|
|
555
|
+
assert connector._execute_pandas.call_args.kwargs["database_name"] == "other_db"
|
|
556
|
+
|
|
557
|
+
|
|
547
558
|
def test_get_schema_strict_match_hits_no_fallback():
|
|
548
559
|
"""Exact case match returns rows from first query and never executes the lower() fallback."""
|
|
549
560
|
connector = _make_pg_connector_for_metadata()
|
|
@@ -563,6 +574,17 @@ def test_get_schema_strict_match_hits_no_fallback():
|
|
|
563
574
|
assert result[0]["pk"] is True
|
|
564
575
|
|
|
565
576
|
|
|
577
|
+
def test_get_schema_executes_in_requested_database():
|
|
578
|
+
connector = _make_pg_connector_for_metadata(database_name="default_db")
|
|
579
|
+
empty = _df([], _SCHEMA_COLS)
|
|
580
|
+
connector._execute_pandas = MagicMock(side_effect=[empty, empty])
|
|
581
|
+
|
|
582
|
+
connector.get_schema(database_name="other_db", schema_name="public", table_name="users")
|
|
583
|
+
|
|
584
|
+
assert connector._execute_pandas.call_count == 2
|
|
585
|
+
assert all(call.kwargs["database_name"] == "other_db" for call in connector._execute_pandas.call_args_list)
|
|
586
|
+
|
|
587
|
+
|
|
566
588
|
def test_get_schema_falls_back_to_lower_when_strict_empty():
|
|
567
589
|
"""If strict match returns empty, fallback to lower() and use those rows."""
|
|
568
590
|
connector = _make_pg_connector_for_metadata()
|
|
@@ -621,6 +643,82 @@ def test_get_schema_empty_table_name_short_circuit():
|
|
|
621
643
|
connector._execute_pandas.assert_not_called()
|
|
622
644
|
|
|
623
645
|
|
|
646
|
+
def test_get_ddl_uses_requested_database_for_table_schema():
|
|
647
|
+
connector = _make_pg_connector_for_metadata(database_name="default_db")
|
|
648
|
+
connector.get_schema = MagicMock(return_value=[])
|
|
649
|
+
|
|
650
|
+
ddl = connector._get_ddl("public", "orders", database_name="other_db")
|
|
651
|
+
|
|
652
|
+
connector.get_schema.assert_called_once_with(
|
|
653
|
+
database_name="other_db",
|
|
654
|
+
schema_name="public",
|
|
655
|
+
table_name="orders",
|
|
656
|
+
)
|
|
657
|
+
assert '"other_db"."public"."orders"' in ddl
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
@pytest.mark.parametrize("object_type", ["VIEW", "MATERIALIZED VIEW"])
|
|
661
|
+
def test_get_ddl_uses_requested_database_for_view_queries(object_type):
|
|
662
|
+
connector = _make_pg_connector_for_metadata(database_name="default_db")
|
|
663
|
+
connector._execute_pandas = MagicMock(return_value=_df([], ["definition"]))
|
|
664
|
+
|
|
665
|
+
connector._get_ddl("public", "orders", object_type, database_name="other_db")
|
|
666
|
+
|
|
667
|
+
connector._execute_pandas.assert_called_once()
|
|
668
|
+
assert connector._execute_pandas.call_args.kwargs["database_name"] == "other_db"
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
def test_get_objects_with_ddl_propagates_metadata_database():
|
|
672
|
+
connector = _make_pg_connector_for_metadata(database_name="default_db")
|
|
673
|
+
metadata = [
|
|
674
|
+
{
|
|
675
|
+
"identifier": "other_db.public.orders",
|
|
676
|
+
"catalog_name": "",
|
|
677
|
+
"database_name": "other_db",
|
|
678
|
+
"schema_name": "public",
|
|
679
|
+
"table_name": "orders",
|
|
680
|
+
"table_type": "table",
|
|
681
|
+
}
|
|
682
|
+
]
|
|
683
|
+
connector._get_metadata = MagicMock(return_value=metadata)
|
|
684
|
+
observed_databases = []
|
|
685
|
+
|
|
686
|
+
def get_ddl(*_args):
|
|
687
|
+
observed_databases.append(connector.database_name)
|
|
688
|
+
return "CREATE TABLE orders (id INT);"
|
|
689
|
+
|
|
690
|
+
connector._get_ddl = MagicMock(side_effect=get_ddl)
|
|
691
|
+
|
|
692
|
+
result = connector._get_objects_with_ddl("table", database_name="other_db", schema_name="public")
|
|
693
|
+
|
|
694
|
+
connector._get_ddl.assert_called_once_with(
|
|
695
|
+
"public",
|
|
696
|
+
"orders",
|
|
697
|
+
"TABLE",
|
|
698
|
+
)
|
|
699
|
+
assert observed_databases == ["other_db"]
|
|
700
|
+
assert connector.database_name == "default_db"
|
|
701
|
+
assert result[0]["definition"] == "CREATE TABLE orders (id INT);"
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
def test_get_sample_rows_executes_in_requested_database():
|
|
705
|
+
connector = _make_pg_connector_for_metadata(database_name="default_db")
|
|
706
|
+
connector._execute_pandas = MagicMock(return_value=_df([(1,)], ["id"]))
|
|
707
|
+
|
|
708
|
+
rows = connector.get_sample_rows(
|
|
709
|
+
database_name="other_db",
|
|
710
|
+
schema_name="public",
|
|
711
|
+
tables=["orders"],
|
|
712
|
+
top_n=1,
|
|
713
|
+
)
|
|
714
|
+
|
|
715
|
+
connector._execute_pandas.assert_called_once()
|
|
716
|
+
assert connector._execute_pandas.call_args.kwargs["database_name"] == "other_db"
|
|
717
|
+
assert '"other_db"."public"."orders"' in connector._execute_pandas.call_args.args[0]
|
|
718
|
+
assert rows[0]["database_name"] == "other_db"
|
|
719
|
+
assert rows[0]["identifier"] == "other_db.public.orders"
|
|
720
|
+
|
|
721
|
+
|
|
624
722
|
def test_get_metadata_strict_hit_no_fallback():
|
|
625
723
|
"""_get_metadata uses only the strict query when it returns rows."""
|
|
626
724
|
connector = _make_pg_connector_for_metadata(schema_name="public")
|
|
@@ -633,6 +731,15 @@ def test_get_metadata_strict_hit_no_fallback():
|
|
|
633
731
|
assert [m["table_name"] for m in result] == ["t1", "t2"]
|
|
634
732
|
|
|
635
733
|
|
|
734
|
+
def test_get_metadata_identifier_uses_requested_database():
|
|
735
|
+
connector = _make_pg_connector_for_metadata(database_name="default_db", schema_name="public")
|
|
736
|
+
connector._execute_pandas = MagicMock(return_value=_df([("public", "orders")], ["table_schema", "table_name"]))
|
|
737
|
+
|
|
738
|
+
result = connector._get_metadata("table", database_name="other_db", schema_name="public")
|
|
739
|
+
|
|
740
|
+
assert result[0]["identifier"] == "other_db.public.orders"
|
|
741
|
+
|
|
742
|
+
|
|
636
743
|
def test_get_metadata_falls_back_to_lower_when_strict_empty():
|
|
637
744
|
"""_get_metadata falls back to lower() when strict returns empty and schema_name is provided."""
|
|
638
745
|
connector = _make_pg_connector_for_metadata(schema_name="public")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|