• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

georgia-tech-db / eva / #754

04 Sep 2023 09:54PM UTC coverage: 74.807% (-5.5%) from 80.336%
#754

push

circle-ci

jiashenC
update case

8727 of 11666 relevant lines covered (74.81%)

0.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.74
/evadb/catalog/models/base_model.py
1
# coding=utf-8
2
# Copyright 2018-2023 EvaDB
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
import contextlib
1✔
16

17
import sqlalchemy
1✔
18
from sqlalchemy import Column, Integer
1✔
19
from sqlalchemy.engine import Engine
1✔
20
from sqlalchemy.exc import SQLAlchemyError
1✔
21
from sqlalchemy.ext.declarative import declarative_base
1✔
22
from sqlalchemy_utils import database_exists
1✔
23

24
from evadb.catalog.sql_config import CATALOG_TABLES
1✔
25
from evadb.utils.logging_manager import logger
1✔
26

27

28
class CustomModel:
1✔
29
    """This overrides the default `_declarative_constructor` constructor.
30

31
    It skips the attributes that are not present for the model, thus if a
32
    dict is passed with some unknown attributes for the model on creation,
33
    it won't complain for `unknown field`s.
34
    Declares and int `_row_id` field for all tables
35
    """
36

37
    _row_id = Column("_row_id", Integer, primary_key=True)
1✔
38

39
    def __init__(self, **kwargs):
1✔
40
        cls_ = type(self)
1✔
41
        for k in kwargs:
1✔
42
            if hasattr(cls_, k):
×
43
                setattr(self, k, kwargs[k])
×
44
            else:
45
                continue
×
46

47
    def save(self, db_session):
1✔
48
        """Add and commit
49

50
        Returns: saved object
51

52
        """
53
        try:
1✔
54
            db_session.add(self)
1✔
55
            self._commit(db_session)
1✔
56
        except Exception as e:
57
            db_session.rollback()
58
            logger.error(f"Database save failed : {str(e)}")
59
            raise e
60
        return self
1✔
61

62
    def update(self, db_session, **kwargs):
1✔
63
        """Update and commit
64

65
        Args:
66
            **kwargs: attributes to update
67

68
        Returns: updated object
69

70
        """
71
        try:
1✔
72
            for attr, value in kwargs.items():
1✔
73
                if hasattr(self, attr):
1✔
74
                    setattr(self, attr, value)
×
75
            return self.save(db_session)
1✔
76
        except Exception as e:
77
            db_session.rollback()
78
            logger.error(f"Database update failed : {str(e)}")
79
            raise e
80

81
    def delete(self, db_session):
1✔
82
        """Delete and commit"""
83
        try:
1✔
84
            db_session.delete(self)
1✔
85
            self._commit(db_session)
1✔
86
        except Exception as e:
87
            db_session.rollback()
88
            logger.error(f"Database delete failed : {str(e)}")
89
            raise e
90

91
    def _commit(self, db_session):
1✔
92
        """Try to commit. If an error is raised, the session is rollbacked."""
93
        try:
1✔
94
            db_session.commit()
1✔
95
        except SQLAlchemyError as e:
96
            db_session.rollback()
97
            logger.error(f"Database commit failed : {str(e)}")
98
            raise e
99

100

101
# Custom Base Model to be inherited by all models
102
BaseModel = declarative_base(cls=CustomModel, constructor=None)
1✔
103

104

105
def truncate_catalog_tables(engine: Engine):
1✔
106
    """Truncate all the catalog tables"""
107
    # https://stackoverflow.com/questions/4763472/sqlalchemy-clear-database-content-but-dont-drop-the-schema/5003705#5003705 #noqa
108
    # reflect to refresh the metadata
109
    BaseModel.metadata.reflect(bind=engine)
×
110
    insp = sqlalchemy.inspect(engine)
×
111
    if database_exists(engine.url):
×
112
        with contextlib.closing(engine.connect()) as con:
×
113
            trans = con.begin()
×
114
            for table in reversed(BaseModel.metadata.sorted_tables):
×
115
                if insp.has_table(table.name):
×
116
                    con.execute(table.delete())
×
117
            trans.commit()
×
118

119

120
def drop_all_tables_except_catalog(engine: Engine):
121
    """drop all the tables except the catalog"""
122
    # reflect to refresh the metadata
123
    BaseModel.metadata.reflect(bind=engine)
124
    insp = sqlalchemy.inspect(engine)
125
    if database_exists(engine.url):
126
        with contextlib.closing(engine.connect()) as con:
127
            trans = con.begin()
128
            for table in reversed(BaseModel.metadata.sorted_tables):
129
                if table.name not in CATALOG_TABLES:
130
                    if insp.has_table(table.name):
131
                        table.drop(con)
132
            trans.commit()
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc