schema_sentinel.metadata_manager.model.comparison

  1import json
  2import logging as log
  3from datetime import datetime
  4
  5import pandas as pd
  6import sqlalchemy as db
  7from sqlalchemy import ForeignKey, select
  8
  9from schema_sentinel.metadata_manager.enums import DbObjectType
 10from schema_sentinel.metadata_manager.model.function import Function
 11from schema_sentinel.metadata_manager.model.procedure import Procedure
 12from schema_sentinel.metadata_manager.model.task import Task
 13from schema_sentinel.metadata_manager.model.view import View
 14
 15from . import CommonBase
 16
 17
 18class Comparison(CommonBase):
 19    __tablename__ = "comparisons"
 20    object_type = db.Column(db.String, primary_key=True)
 21    comparison_key = db.Column(db.String, primary_key=True)
 22    source_database_id = db.Column(db.String, ForeignKey("databases.database_id"))
 23    target_database_id = db.Column(db.String, ForeignKey("databases.database_id"))
 24    comparison_value = db.Column(db.String)
 25    comparison_performed_by = db.Column(db.String)
 26    created = db.Column(db.String)
 27
 28    def save(self, session) -> None:
 29        if not session.execute(self.exists()).first():
 30            session.add(self)
 31            session.commit()
 32
 33    def exists(self) -> str:
 34        return select(Comparison).filter_by(object_type=self.object_type, comparison_key=self.comparison_key)
 35
 36    @staticmethod
 37    def is_empty(comparison_value) -> bool:
 38        return not (
 39            "differences" in comparison_value.keys()
 40            and comparison_value["differences"] != {}
 41            or comparison_value["right"] is None
 42        )
 43
 44    @staticmethod
 45    def save_comparison(
 46        comparison_dict: dict, src_database_id: str, trg_database_id: str, session, db_timestamp_to_string, user: str
 47    ):
 48        for db_object_type in comparison_dict.keys():
 49            for comparison_key in comparison_dict[db_object_type].keys():
 50                if Comparison.is_empty(comparison_dict[db_object_type][comparison_key]):
 51                    continue
 52                comp_val = json.dumps(
 53                    {"key": comparison_key, "comparison": comparison_dict[db_object_type][comparison_key]}
 54                )
 55                comparison = Comparison(
 56                    object_type=db_object_type,
 57                    comparison_key=comparison_key,
 58                    source_database_id=src_database_id,
 59                    target_database_id=trg_database_id,
 60                    comparison_value=comp_val,
 61                    created=db_timestamp_to_string(datetime.now()),
 62                    comparison_performed_by=user,
 63                )
 64                comparison.save(session=session)
 65                log.debug(f"saved comparison={comparison.diffs}")
 66
 67    @property
 68    def one_diffs(self) -> pd.DataFrame:
 69        if not self.comparison_value:
 70            return pd.DataFrame(columns=["Attribute", "Left", "Right"])
 71        diffs = json.loads(self.comparison_value)
 72
 73        comparison_key = diffs["key"] if "key" in diffs.keys() else self.comparison_key
 74        object_name = comparison_key.split(" ")[0]
 75        comparison_database_key = (
 76            comparison_key.split(" ")[1].replace("[", "").replace("]", "") if " " in comparison_key else comparison_key
 77        )
 78        data = {}
 79        if "comparison" in diffs.keys():
 80            comparison = diffs["comparison"]
 81
 82            left_object_type = comparison["left"] if "left" in comparison.keys() else self.object_type
 83            right_object_type = comparison["right"] if "right" in comparison.keys() and comparison["right"] else None
 84            data = comparison["differences"] if "differences" in comparison.keys() else {}
 85
 86        data_array = []
 87        if data:
 88            for attribute in data.keys():
 89                left = data[attribute][0] if not attribute.endswith("definition") else "code is different"
 90                right = data[attribute][1] if not attribute.endswith("definition") else ""
 91                data_array.append([attribute, left, right])
 92        else:
 93            data_array.append([""] * 3)
 94
 95        diff_df = pd.DataFrame(data=data_array, columns=["Attribute", "Left", "Right"])
 96        diff_df.insert(
 97            loc=0,
 98            column="DB Key",
 99            value=almost_empty_array_of(comparison_database_key, len(data_array)),
100            allow_duplicates=True,
101        )
102        diff_df.insert(
103            loc=1,
104            column="Left Object",
105            value=almost_empty_array_of(left_object_type, len(data_array)),
106            allow_duplicates=True,
107        )
108        diff_df.insert(
109            loc=2,
110            column="Right Object",
111            value=almost_empty_array_of(right_object_type if right_object_type else "Not present", len(data_array)),
112            allow_duplicates=True,
113        )
114        diff_df.insert(
115            loc=3,
116            column="Object Name",
117            value=almost_empty_array_of([object_name], len(data_array)),
118            allow_duplicates=True,
119        )
120        return diff_df
121
122    def both_diffs(self, session) -> pd.DataFrame:
123        if not self.comparison_value:
124            return pd.DataFrame(columns=["Attribute", "Left", "Right"])
125        diffs = json.loads(self.comparison_value)
126
127        comparison_key = diffs["key"]
128        object_name = self.object_type
129        comparison_database_key = comparison_key.split(" ")[1].replace("[", "").replace("]", "")
130        comparison_object_name = comparison_key.split(" ")[0]
131
132        data = {}
133        if "comparison" in diffs.keys():
134            comparison = diffs["comparison"]
135
136            left_object_type = comparison["left"]
137            right_object_type = comparison["right"]
138            data = comparison["differences"]
139
140        data_array = []
141        if data:
142            for attribute in data.keys():
143                if not attribute.endswith("definition"):
144                    left = data[attribute][0]
145                    right = data[attribute][1]
146                    data_array.append([attribute, left, right])
147                else:
148                    left_object_id = f"{self.source_database_id}.{comparison_object_name}"
149                    right_object_id = f"{self.target_database_id}.{comparison_object_name}"
150                    left_code, right_code = ""
151
152                    if left_object_type == DbObjectType.PROCEDURE.value:
153                        left_object: Procedure = (
154                            session.query(Procedure).filter(Procedure.procedure_id == left_object_id).one()
155                        )
156                        left_code = get_code(comparison_object_name, left_object, Procedure)
157                        right_object: Procedure = (
158                            session.query(Procedure).filter(Procedure.procedure_id == left_object_id).one()
159                        )
160                        right_code = get_code(comparison_object_name, right_object, Procedure)
161
162                    elif left_object_type == DbObjectType.FUNCTION.value:
163                        left_object: Function = (
164                            session.query(Function).filter(Function.function_id == left_object_id).one()
165                        )
166                        left_code = get_code(comparison_object_name, left_object, Function)
167                        right_object: Function = (
168                            session.query(Function).filter(Function.function_id == right_object_id).one()
169                        )
170                        right_code = get_code(comparison_object_name, right_object, Function)
171
172                    elif left_object_type == DbObjectType.VIEW.value:
173                        left_object: View = session.query(View).filter(View.view_id == left_object_id).one()
174                        left_code = get_code(comparison_object_name, left_object, View)
175                        right_object: View = session.query(View).filter(View.view_id == right_object_id).one()
176                        right_code = get_code(comparison_object_name, right_object, View)
177                    elif left_object_type == DbObjectType.TASK.value:
178                        left_object: Task = session.query(Task).filter(Task.task_id == left_object_id).one()
179                        left_code = get_code(comparison_object_name, left_object, Task)
180                        right_object: Task = session.query(Task).filter(Task.task_id == right_object_id).one()
181                        right_code = get_code(comparison_object_name, right_object, Task)
182                    data_array.append([attribute, left_code, right_code])
183
184        else:
185            data_array.append([""] * 3)
186
187        diff_df = pd.DataFrame(data=data_array, columns=["Attribute", "Left", "Right"])
188        diff_df.insert(
189            loc=0,
190            column="DB Key",
191            value=almost_empty_array_of(comparison_database_key, len(data_array)),
192            allow_duplicates=True,
193        )
194        diff_df.insert(
195            loc=1,
196            column="Left Object",
197            value=almost_empty_array_of(left_object_type, len(data_array)),
198            allow_duplicates=True,
199        )
200        diff_df.insert(
201            loc=2,
202            column="Right Object",
203            value=almost_empty_array_of(right_object_type if right_object_type else "Not present", len(data_array)),
204            allow_duplicates=True,
205        )
206        diff_df.insert(
207            loc=3,
208            column="Object Name",
209            value=almost_empty_array_of([object_name], len(data_array)),
210            allow_duplicates=True,
211        )
212        return diff_df
213
214
215def almost_empty_array_of(value: str, elements_number: int = 1) -> [str]:
216    array = [""] * elements_number
217    array[0] = value
218    return array
219
220
221def get_code(comparison_object_name, db_object, klass) -> str:
222    if klass == Procedure:
223        argument_signature = {"\t\n".join(db_object.argument_signature.split(","))}
224        return f"""CREATE OR REPLACE PROCEDURE {comparison_object_name} ({argument_signature})
225    RETURNS {db_object.__data_type__()}
226    LANGUAGE {db_object.procedure_language}
227    COMMENT {db_object.comment}
228AS
229{db_object.procedure_definition}
230"""
231    elif klass == Function:
232        argument_signature = {"\t\n".join(db_object.argument_signature.split(","))}
233        return f"""CREATE OR REPLACE FUNCTION {comparison_object_name} ({argument_signature})
234    RETURNS {db_object.__data_type__()}
235    COMMENT {db_object.comment}
236AS
237{db_object.function_definition}
238"""
239    elif klass == Task:
240        return f"""CREATE OR REPLACE TASK {comparison_object_name}
241    ERROR_INTEGRATION {db_object.error_integration}
242    WAREHOUSE {db_object.warehouse}
243    COMMENT {db_object.comment}
244    SCHEDULE='{db_object.schedule}'
245AS
246    {db_object.definition}
247"""
248    elif klass == View:
249        return f"""CREATE OR REPLACE VIEW {comparison_object_name}
250AS
251    {db_object.view_definition}
252"""
class Comparison(schema_sentinel.metadata_manager.model.CommonBase):
 19class Comparison(CommonBase):
 20    __tablename__ = "comparisons"
 21    object_type = db.Column(db.String, primary_key=True)
 22    comparison_key = db.Column(db.String, primary_key=True)
 23    source_database_id = db.Column(db.String, ForeignKey("databases.database_id"))
 24    target_database_id = db.Column(db.String, ForeignKey("databases.database_id"))
 25    comparison_value = db.Column(db.String)
 26    comparison_performed_by = db.Column(db.String)
 27    created = db.Column(db.String)
 28
 29    def save(self, session) -> None:
 30        if not session.execute(self.exists()).first():
 31            session.add(self)
 32            session.commit()
 33
 34    def exists(self) -> str:
 35        return select(Comparison).filter_by(object_type=self.object_type, comparison_key=self.comparison_key)
 36
 37    @staticmethod
 38    def is_empty(comparison_value) -> bool:
 39        return not (
 40            "differences" in comparison_value.keys()
 41            and comparison_value["differences"] != {}
 42            or comparison_value["right"] is None
 43        )
 44
 45    @staticmethod
 46    def save_comparison(
 47        comparison_dict: dict, src_database_id: str, trg_database_id: str, session, db_timestamp_to_string, user: str
 48    ):
 49        for db_object_type in comparison_dict.keys():
 50            for comparison_key in comparison_dict[db_object_type].keys():
 51                if Comparison.is_empty(comparison_dict[db_object_type][comparison_key]):
 52                    continue
 53                comp_val = json.dumps(
 54                    {"key": comparison_key, "comparison": comparison_dict[db_object_type][comparison_key]}
 55                )
 56                comparison = Comparison(
 57                    object_type=db_object_type,
 58                    comparison_key=comparison_key,
 59                    source_database_id=src_database_id,
 60                    target_database_id=trg_database_id,
 61                    comparison_value=comp_val,
 62                    created=db_timestamp_to_string(datetime.now()),
 63                    comparison_performed_by=user,
 64                )
 65                comparison.save(session=session)
 66                log.debug(f"saved comparison={comparison.diffs}")
 67
 68    @property
 69    def one_diffs(self) -> pd.DataFrame:
 70        if not self.comparison_value:
 71            return pd.DataFrame(columns=["Attribute", "Left", "Right"])
 72        diffs = json.loads(self.comparison_value)
 73
 74        comparison_key = diffs["key"] if "key" in diffs.keys() else self.comparison_key
 75        object_name = comparison_key.split(" ")[0]
 76        comparison_database_key = (
 77            comparison_key.split(" ")[1].replace("[", "").replace("]", "") if " " in comparison_key else comparison_key
 78        )
 79        data = {}
 80        if "comparison" in diffs.keys():
 81            comparison = diffs["comparison"]
 82
 83            left_object_type = comparison["left"] if "left" in comparison.keys() else self.object_type
 84            right_object_type = comparison["right"] if "right" in comparison.keys() and comparison["right"] else None
 85            data = comparison["differences"] if "differences" in comparison.keys() else {}
 86
 87        data_array = []
 88        if data:
 89            for attribute in data.keys():
 90                left = data[attribute][0] if not attribute.endswith("definition") else "code is different"
 91                right = data[attribute][1] if not attribute.endswith("definition") else ""
 92                data_array.append([attribute, left, right])
 93        else:
 94            data_array.append([""] * 3)
 95
 96        diff_df = pd.DataFrame(data=data_array, columns=["Attribute", "Left", "Right"])
 97        diff_df.insert(
 98            loc=0,
 99            column="DB Key",
100            value=almost_empty_array_of(comparison_database_key, len(data_array)),
101            allow_duplicates=True,
102        )
103        diff_df.insert(
104            loc=1,
105            column="Left Object",
106            value=almost_empty_array_of(left_object_type, len(data_array)),
107            allow_duplicates=True,
108        )
109        diff_df.insert(
110            loc=2,
111            column="Right Object",
112            value=almost_empty_array_of(right_object_type if right_object_type else "Not present", len(data_array)),
113            allow_duplicates=True,
114        )
115        diff_df.insert(
116            loc=3,
117            column="Object Name",
118            value=almost_empty_array_of([object_name], len(data_array)),
119            allow_duplicates=True,
120        )
121        return diff_df
122
123    def both_diffs(self, session) -> pd.DataFrame:
124        if not self.comparison_value:
125            return pd.DataFrame(columns=["Attribute", "Left", "Right"])
126        diffs = json.loads(self.comparison_value)
127
128        comparison_key = diffs["key"]
129        object_name = self.object_type
130        comparison_database_key = comparison_key.split(" ")[1].replace("[", "").replace("]", "")
131        comparison_object_name = comparison_key.split(" ")[0]
132
133        data = {}
134        if "comparison" in diffs.keys():
135            comparison = diffs["comparison"]
136
137            left_object_type = comparison["left"]
138            right_object_type = comparison["right"]
139            data = comparison["differences"]
140
141        data_array = []
142        if data:
143            for attribute in data.keys():
144                if not attribute.endswith("definition"):
145                    left = data[attribute][0]
146                    right = data[attribute][1]
147                    data_array.append([attribute, left, right])
148                else:
149                    left_object_id = f"{self.source_database_id}.{comparison_object_name}"
150                    right_object_id = f"{self.target_database_id}.{comparison_object_name}"
151                    left_code, right_code = ""
152
153                    if left_object_type == DbObjectType.PROCEDURE.value:
154                        left_object: Procedure = (
155                            session.query(Procedure).filter(Procedure.procedure_id == left_object_id).one()
156                        )
157                        left_code = get_code(comparison_object_name, left_object, Procedure)
158                        right_object: Procedure = (
159                            session.query(Procedure).filter(Procedure.procedure_id == left_object_id).one()
160                        )
161                        right_code = get_code(comparison_object_name, right_object, Procedure)
162
163                    elif left_object_type == DbObjectType.FUNCTION.value:
164                        left_object: Function = (
165                            session.query(Function).filter(Function.function_id == left_object_id).one()
166                        )
167                        left_code = get_code(comparison_object_name, left_object, Function)
168                        right_object: Function = (
169                            session.query(Function).filter(Function.function_id == right_object_id).one()
170                        )
171                        right_code = get_code(comparison_object_name, right_object, Function)
172
173                    elif left_object_type == DbObjectType.VIEW.value:
174                        left_object: View = session.query(View).filter(View.view_id == left_object_id).one()
175                        left_code = get_code(comparison_object_name, left_object, View)
176                        right_object: View = session.query(View).filter(View.view_id == right_object_id).one()
177                        right_code = get_code(comparison_object_name, right_object, View)
178                    elif left_object_type == DbObjectType.TASK.value:
179                        left_object: Task = session.query(Task).filter(Task.task_id == left_object_id).one()
180                        left_code = get_code(comparison_object_name, left_object, Task)
181                        right_object: Task = session.query(Task).filter(Task.task_id == right_object_id).one()
182                        right_code = get_code(comparison_object_name, right_object, Task)
183                    data_array.append([attribute, left_code, right_code])
184
185        else:
186            data_array.append([""] * 3)
187
188        diff_df = pd.DataFrame(data=data_array, columns=["Attribute", "Left", "Right"])
189        diff_df.insert(
190            loc=0,
191            column="DB Key",
192            value=almost_empty_array_of(comparison_database_key, len(data_array)),
193            allow_duplicates=True,
194        )
195        diff_df.insert(
196            loc=1,
197            column="Left Object",
198            value=almost_empty_array_of(left_object_type, len(data_array)),
199            allow_duplicates=True,
200        )
201        diff_df.insert(
202            loc=2,
203            column="Right Object",
204            value=almost_empty_array_of(right_object_type if right_object_type else "Not present", len(data_array)),
205            allow_duplicates=True,
206        )
207        diff_df.insert(
208            loc=3,
209            column="Object Name",
210            value=almost_empty_array_of([object_name], len(data_array)),
211            allow_duplicates=True,
212        )
213        return diff_df

The base class of the class hierarchy.

When called, it accepts no arguments and returns a new featureless instance that has no instance attributes and cannot be given any.

Comparison(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance's class are allowed. These could be, for example, any mapped columns or relationships.

object_type
comparison_key
source_database_id
target_database_id
comparison_value
comparison_performed_by
created
def save(self, session) -> None:
29    def save(self, session) -> None:
30        if not session.execute(self.exists()).first():
31            session.add(self)
32            session.commit()
def exists(self) -> str:
34    def exists(self) -> str:
35        return select(Comparison).filter_by(object_type=self.object_type, comparison_key=self.comparison_key)
@staticmethod
def is_empty(comparison_value) -> bool:
37    @staticmethod
38    def is_empty(comparison_value) -> bool:
39        return not (
40            "differences" in comparison_value.keys()
41            and comparison_value["differences"] != {}
42            or comparison_value["right"] is None
43        )
@staticmethod
def save_comparison( comparison_dict: dict, src_database_id: str, trg_database_id: str, session, db_timestamp_to_string, user: str):
45    @staticmethod
46    def save_comparison(
47        comparison_dict: dict, src_database_id: str, trg_database_id: str, session, db_timestamp_to_string, user: str
48    ):
49        for db_object_type in comparison_dict.keys():
50            for comparison_key in comparison_dict[db_object_type].keys():
51                if Comparison.is_empty(comparison_dict[db_object_type][comparison_key]):
52                    continue
53                comp_val = json.dumps(
54                    {"key": comparison_key, "comparison": comparison_dict[db_object_type][comparison_key]}
55                )
56                comparison = Comparison(
57                    object_type=db_object_type,
58                    comparison_key=comparison_key,
59                    source_database_id=src_database_id,
60                    target_database_id=trg_database_id,
61                    comparison_value=comp_val,
62                    created=db_timestamp_to_string(datetime.now()),
63                    comparison_performed_by=user,
64                )
65                comparison.save(session=session)
66                log.debug(f"saved comparison={comparison.diffs}")
one_diffs: pandas.core.frame.DataFrame
 68    @property
 69    def one_diffs(self) -> pd.DataFrame:
 70        if not self.comparison_value:
 71            return pd.DataFrame(columns=["Attribute", "Left", "Right"])
 72        diffs = json.loads(self.comparison_value)
 73
 74        comparison_key = diffs["key"] if "key" in diffs.keys() else self.comparison_key
 75        object_name = comparison_key.split(" ")[0]
 76        comparison_database_key = (
 77            comparison_key.split(" ")[1].replace("[", "").replace("]", "") if " " in comparison_key else comparison_key
 78        )
 79        data = {}
 80        if "comparison" in diffs.keys():
 81            comparison = diffs["comparison"]
 82
 83            left_object_type = comparison["left"] if "left" in comparison.keys() else self.object_type
 84            right_object_type = comparison["right"] if "right" in comparison.keys() and comparison["right"] else None
 85            data = comparison["differences"] if "differences" in comparison.keys() else {}
 86
 87        data_array = []
 88        if data:
 89            for attribute in data.keys():
 90                left = data[attribute][0] if not attribute.endswith("definition") else "code is different"
 91                right = data[attribute][1] if not attribute.endswith("definition") else ""
 92                data_array.append([attribute, left, right])
 93        else:
 94            data_array.append([""] * 3)
 95
 96        diff_df = pd.DataFrame(data=data_array, columns=["Attribute", "Left", "Right"])
 97        diff_df.insert(
 98            loc=0,
 99            column="DB Key",
100            value=almost_empty_array_of(comparison_database_key, len(data_array)),
101            allow_duplicates=True,
102        )
103        diff_df.insert(
104            loc=1,
105            column="Left Object",
106            value=almost_empty_array_of(left_object_type, len(data_array)),
107            allow_duplicates=True,
108        )
109        diff_df.insert(
110            loc=2,
111            column="Right Object",
112            value=almost_empty_array_of(right_object_type if right_object_type else "Not present", len(data_array)),
113            allow_duplicates=True,
114        )
115        diff_df.insert(
116            loc=3,
117            column="Object Name",
118            value=almost_empty_array_of([object_name], len(data_array)),
119            allow_duplicates=True,
120        )
121        return diff_df
def both_diffs(self, session) -> pandas.core.frame.DataFrame:
123    def both_diffs(self, session) -> pd.DataFrame:
124        if not self.comparison_value:
125            return pd.DataFrame(columns=["Attribute", "Left", "Right"])
126        diffs = json.loads(self.comparison_value)
127
128        comparison_key = diffs["key"]
129        object_name = self.object_type
130        comparison_database_key = comparison_key.split(" ")[1].replace("[", "").replace("]", "")
131        comparison_object_name = comparison_key.split(" ")[0]
132
133        data = {}
134        if "comparison" in diffs.keys():
135            comparison = diffs["comparison"]
136
137            left_object_type = comparison["left"]
138            right_object_type = comparison["right"]
139            data = comparison["differences"]
140
141        data_array = []
142        if data:
143            for attribute in data.keys():
144                if not attribute.endswith("definition"):
145                    left = data[attribute][0]
146                    right = data[attribute][1]
147                    data_array.append([attribute, left, right])
148                else:
149                    left_object_id = f"{self.source_database_id}.{comparison_object_name}"
150                    right_object_id = f"{self.target_database_id}.{comparison_object_name}"
151                    left_code, right_code = ""
152
153                    if left_object_type == DbObjectType.PROCEDURE.value:
154                        left_object: Procedure = (
155                            session.query(Procedure).filter(Procedure.procedure_id == left_object_id).one()
156                        )
157                        left_code = get_code(comparison_object_name, left_object, Procedure)
158                        right_object: Procedure = (
159                            session.query(Procedure).filter(Procedure.procedure_id == left_object_id).one()
160                        )
161                        right_code = get_code(comparison_object_name, right_object, Procedure)
162
163                    elif left_object_type == DbObjectType.FUNCTION.value:
164                        left_object: Function = (
165                            session.query(Function).filter(Function.function_id == left_object_id).one()
166                        )
167                        left_code = get_code(comparison_object_name, left_object, Function)
168                        right_object: Function = (
169                            session.query(Function).filter(Function.function_id == right_object_id).one()
170                        )
171                        right_code = get_code(comparison_object_name, right_object, Function)
172
173                    elif left_object_type == DbObjectType.VIEW.value:
174                        left_object: View = session.query(View).filter(View.view_id == left_object_id).one()
175                        left_code = get_code(comparison_object_name, left_object, View)
176                        right_object: View = session.query(View).filter(View.view_id == right_object_id).one()
177                        right_code = get_code(comparison_object_name, right_object, View)
178                    elif left_object_type == DbObjectType.TASK.value:
179                        left_object: Task = session.query(Task).filter(Task.task_id == left_object_id).one()
180                        left_code = get_code(comparison_object_name, left_object, Task)
181                        right_object: Task = session.query(Task).filter(Task.task_id == right_object_id).one()
182                        right_code = get_code(comparison_object_name, right_object, Task)
183                    data_array.append([attribute, left_code, right_code])
184
185        else:
186            data_array.append([""] * 3)
187
188        diff_df = pd.DataFrame(data=data_array, columns=["Attribute", "Left", "Right"])
189        diff_df.insert(
190            loc=0,
191            column="DB Key",
192            value=almost_empty_array_of(comparison_database_key, len(data_array)),
193            allow_duplicates=True,
194        )
195        diff_df.insert(
196            loc=1,
197            column="Left Object",
198            value=almost_empty_array_of(left_object_type, len(data_array)),
199            allow_duplicates=True,
200        )
201        diff_df.insert(
202            loc=2,
203            column="Right Object",
204            value=almost_empty_array_of(right_object_type if right_object_type else "Not present", len(data_array)),
205            allow_duplicates=True,
206        )
207        diff_df.insert(
208            loc=3,
209            column="Object Name",
210            value=almost_empty_array_of([object_name], len(data_array)),
211            allow_duplicates=True,
212        )
213        return diff_df
def almost_empty_array_of(value: str, elements_number: int = 1) -> [<class 'str'>]:
216def almost_empty_array_of(value: str, elements_number: int = 1) -> [str]:
217    array = [""] * elements_number
218    array[0] = value
219    return array
def get_code(comparison_object_name, db_object, klass) -> str:
222def get_code(comparison_object_name, db_object, klass) -> str:
223    if klass == Procedure:
224        argument_signature = {"\t\n".join(db_object.argument_signature.split(","))}
225        return f"""CREATE OR REPLACE PROCEDURE {comparison_object_name} ({argument_signature})
226    RETURNS {db_object.__data_type__()}
227    LANGUAGE {db_object.procedure_language}
228    COMMENT {db_object.comment}
229AS
230{db_object.procedure_definition}
231"""
232    elif klass == Function:
233        argument_signature = {"\t\n".join(db_object.argument_signature.split(","))}
234        return f"""CREATE OR REPLACE FUNCTION {comparison_object_name} ({argument_signature})
235    RETURNS {db_object.__data_type__()}
236    COMMENT {db_object.comment}
237AS
238{db_object.function_definition}
239"""
240    elif klass == Task:
241        return f"""CREATE OR REPLACE TASK {comparison_object_name}
242    ERROR_INTEGRATION {db_object.error_integration}
243    WAREHOUSE {db_object.warehouse}
244    COMMENT {db_object.comment}
245    SCHEDULE='{db_object.schedule}'
246AS
247    {db_object.definition}
248"""
249    elif klass == View:
250        return f"""CREATE OR REPLACE VIEW {comparison_object_name}
251AS
252    {db_object.view_definition}
253"""