Skip to content

Commit

Permalink
working implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
feyruzb committed Aug 29, 2024
1 parent 5824ea7 commit dfb9f3a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
18 changes: 14 additions & 4 deletions web/server/codechecker_server/api/product_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,15 @@ def getProductConfiguration(self, product_id):
return prod

@timeit
def add_product_support(self, product_info):
def add_product_support(self, product):
"""
Creates a database for the given product,
to assist add product function that connects to
to assist addProduct() function that connects to
an already existing database.
"""
self.__require_permission([permissions.SUPERUSER])

product_info = product.connection
if product_info.engine == 'sqlite':
LOG.info("Using SQLite engine, skipping database creation")
return True
Expand All @@ -338,7 +339,6 @@ def add_product_support(self, product_info):
database='postgres'
)
engine = create_engine(engine_url)

try:
with engine.connect() as conn:
conn.execute("commit")
Expand All @@ -356,6 +356,8 @@ def add_product_support(self, product_info):
except exc.SQLAlchemyError as e:
LOG.error("SQLAlchemyError occurred: %s", str(e))
return False
finally:
engine.dispose()

return True

Expand Down Expand Up @@ -393,7 +395,15 @@ def addProduct(self, product):
codechecker_api_shared.ttypes.ErrorCode.GENERAL,
msg)

if self.add_product_support(product.connection):
db_in_use = self.__server.get_if_database_in_use(product.connection)
if db_in_use:
LOG.error("Database '%s' is already in use by another product!",
product.connection.database)
raise codechecker_api_shared.ttypes.RequestFailed(
codechecker_api_shared.ttypes.ErrorCode.DATABASE,
"Database is already in use by another product!")

if self.add_product_support(product):
LOG.info("Database support added successfully.")

# Some values come encoded as Base64, decode these.
Expand Down
31 changes: 31 additions & 0 deletions web/server/codechecker_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,9 @@ def __init__(self,
permissions.initialise_defaults('SYSTEM', {
'config_db_session': cfg_sess
})

self.cfg_sess_private = cfg_sess

products = cfg_sess.query(ORMProduct).all()
for product in products:
self.add_product(product)
Expand Down Expand Up @@ -912,6 +915,34 @@ def add_product(self, orm_product, init_db=False):

self.__products[prod.endpoint] = prod

def get_if_database_in_use(self, product_connection_string):
"""
Returns the product endpoint if the given database is in use
"""
# get the database name from the product connection string
to_add = product_connection_string.database

if str(product_connection_string.engine) == "sqlite":
to_add = str(to_add).split("/", maxsplit=1)[-1]
if to_add.endswith('.sqlite'):
to_add = to_add[:-7]
LOG.info("to add: %s", to_add)

# get the current state of connected databases from products
dynamic_list = [
a.connection.split("/")[-1].split('?')[0]
for a in self.cfg_sess_private.query(ORMProduct).all()
]
# log dynamic list
LOG.info("dynamic list: %s", dynamic_list)
dynamic_list = [
d[:-7] if d.endswith('.sqlite') else d
for d in dynamic_list
]
LOG.info("dynamic list: %s", dynamic_list)

return to_add in dynamic_list # True if found, False otherwise

@property
def num_products(self):
"""
Expand Down

0 comments on commit dfb9f3a

Please sign in to comment.