diff --git a/bw2data/__init__.py b/bw2data/__init__.py index 98446b46..5c95a673 100644 --- a/bw2data/__init__.py +++ b/bw2data/__init__.py @@ -33,7 +33,7 @@ "weightings", ] -__version__ = "4.6.2" +__version__ = "4.7" from bw2data.configuration import config, labels from bw2data.project import projects diff --git a/bw2data/backends/base.py b/bw2data/backends/base.py index 138a5beb..07d3cf2f 100644 --- a/bw2data/backends/base.py +++ b/bw2data/backends/base.py @@ -1103,6 +1103,7 @@ def process(self, csv=False): name=clean_datapackage_name(self.name + " activity metadata"), ) + dp.metadata["database_dependencies"] = sorted(dependents) dp.finalize_serialization() self.metadata["depends"] = sorted(dependents) diff --git a/bw2data/updates.py b/bw2data/updates.py index 0f3ae9d3..4569fd91 100644 --- a/bw2data/updates.py +++ b/bw2data/updates.py @@ -120,6 +120,11 @@ class Updates: "automatic": True, "explanation": "bw2data 4.0 release switched to a new database search implementation", }, + "4.7 database dependencies in datapackage": { + "method": "expire_all_processed_data_47", + "automatic": True, + "explanation": "bw2data 4.7 adds database_dependencies to datapackage metadata; all databases must be reprocessed", + }, } @classmethod @@ -226,6 +231,10 @@ def processed_data_format_change_23(cls): def expire_all_processed_data_40(cls): cls._reprocess_all() + @classmethod + def expire_all_processed_data_47(cls): + cls._reprocess_all() + @classmethod def fix_migrations_filename(cls): """ "Fix migration data filenames to use shorter hash. diff --git a/tests/database.py b/tests/database.py index 5defff80..7097a81f 100644 --- a/tests/database.py +++ b/tests/database.py @@ -505,6 +505,63 @@ def test_processed_array(): assert array[0]["uncertainty_type"] == 7 +@bw2test +def test_process_writes_database_dependencies_to_datapackage(): + Database("other database").write( + { + ("other database", "x"): { + "type": "process", + "exchanges": [ + {"input": ("other database", "x"), "amount": 1, "type": "production"} + ], + } + } + ) + db_a = Database("a database") + db_a.write( + { + ("a database", "1"): { + "type": "process", + "exchanges": [ + { + "input": ("a database", "1"), + "amount": 1, + "type": "production", + }, + { + "input": ("other database", "x"), + "amount": 0.5, + "type": "technosphere", + }, + ], + } + } + ) + package = db_a.datapackage() + assert package.metadata["database_dependencies"] == ["other database"] + + +@bw2test +def test_process_writes_empty_database_dependencies_when_no_external_inputs(): + db = Database("a database") + db.write( + { + ("a database", "1"): { + "type": "process", + "exchanges": [ + { + "input": ("a database", "1"), + "amount": 1, + "type": "production", + } + ], + } + } + ) + package = db.datapackage() + assert package.metadata["database_dependencies"] == [] + + @bw2test def test_processed_array_with_metadata(): database = Database("a database")