From ed9f3ff212c3ee8ef18541d536a18e39af98b382 Mon Sep 17 00:00:00 2001 From: Vitaly Markov Date: Fri, 11 Oct 2024 16:44:52 +0100 Subject: [PATCH] Copy blueprints during conversion in SingleDB mode, avoid issues with updating objects by reference --- snowddl/app/singledb.py | 8 +++++--- snowddl/config.py | 6 ++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/snowddl/app/singledb.py b/snowddl/app/singledb.py index fab46dc..9c2e448 100644 --- a/snowddl/app/singledb.py +++ b/snowddl/app/singledb.py @@ -250,10 +250,12 @@ def convert_config(self, original_config: SnowDDLConfig): return singledb_config def convert_blueprint(self, bp: AbstractBlueprint): - for field_name, field_value in bp: - self.convert_object_recursive(getattr(bp, field_name)) + converted_bp = bp.model_copy(deep=True) - return bp + for field_name, field_value in converted_bp: + self.convert_object_recursive(getattr(converted_bp, field_name)) + + return converted_bp def convert_object_recursive(self, obj): if isinstance(obj, BaseModel): diff --git a/snowddl/config.py b/snowddl/config.py index 2aaaf87..f7ed745 100644 --- a/snowddl/config.py +++ b/snowddl/config.py @@ -110,15 +110,13 @@ def remove_blueprint(self, bp: AbstractBlueprint): del self.blueprints[bp.__class__][str(bp.full_name)] def add_policy_reference(self, cls: Type[T_Blueprint], policy_name: AbstractIdentWithPrefix, ref: AbstractPolicyReference): - all_blueprints = self.blueprints.get(cls, {}) - if "references" not in cls.model_fields: raise ValueError(f"{cls.__name__} does not have field [references], probably not a policy") - if str(policy_name) not in all_blueprints: + if str(policy_name) not in self.blueprints[cls]: raise ValueError(f"{cls.__name__} with name [{policy_name}] does not exist or was not defined yet") - all_blueprints[str(policy_name)].references.append(ref) + self.blueprints[cls][str(policy_name)].references.append(ref) def add_error(self, path: Path, e: Exception): self.errors.append(