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

georgia-tech-db / eva / #758

04 Sep 2023 08:37PM UTC coverage: 0.0% (-78.3%) from 78.333%
#758

push

circle-ci

hershd23
Increased underline length in at line 75 in text_summarization.rst
	modified:   docs/source/benchmarks/text_summarization.rst

0 of 11303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
×
16

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

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

27

28
class CustomModel:
×
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)
×
38

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

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

50
        Returns: saved object
51

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

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

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

68
        Returns: updated object
69

70
        """
71
        try:
×
72
            for attr, value in kwargs.items():
×
73
                if hasattr(self, attr):
×
74
                    setattr(self, attr, value)
×
75
            return self.save(db_session)
×
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):
×
82
        """Delete and commit"""
83
        try:
×
84
            db_session.delete(self)
×
85
            self._commit(db_session)
×
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):
×
92
        """Try to commit. If an error is raised, the session is rollbacked."""
93
        try:
×
94
            db_session.commit()
×
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)
×
103

104

105
def truncate_catalog_tables(engine: Engine):
×
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