Skip to content

Commit

Permalink
remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
LuiggiTenorioK committed Dec 5, 2024
1 parent 1d25aaf commit 4568c42
Showing 1 changed file with 11 additions and 287 deletions.
298 changes: 11 additions & 287 deletions autosubmit_api/database/db_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,165 +20,19 @@
"""
Module containing functions to manage autosubmit's database.
"""
import os
from sqlite3 import Connection, Cursor
import sqlite3

from bscearth.utils.log import Log
from autosubmit_api.config.basicConfig import APIBasicConfig
from autosubmit_api.builders.experiment_history_builder import ExperimentHistoryDirector, ExperimentHistoryBuilder
from autosubmit_api.builders.configuration_facade_builder import ConfigurationFacadeDirector, AutosubmitConfigurationFacadeBuilder
from typing import Tuple
from autosubmit_api.builders.experiment_history_builder import (
ExperimentHistoryDirector,
ExperimentHistoryBuilder,
)
from autosubmit_api.builders.configuration_facade_builder import (
ConfigurationFacadeDirector,
AutosubmitConfigurationFacadeBuilder,
)

from autosubmit_api.repositories.join.experiment_join import create_experiment_join_repository

CURRENT_DATABASE_VERSION = 1


def check_db():
"""
Checks if database file exist
:return: None if exists, terminates program if not
"""
APIBasicConfig.read()
if not os.path.exists(APIBasicConfig.DB_PATH):
Log.error('Some problem has happened...check the database file.' +
'DB file:' + APIBasicConfig.DB_PATH)
return False
return True


def open_conn(check_version=True) -> Tuple[Connection, Cursor]:
"""
Opens a connection to database
:param check_version: If true, check if the database is compatible with this autosubmit version
:type check_version: bool
:return: connection object, cursor object
:rtype: sqlite3.Connection, sqlite3.Cursor
"""
APIBasicConfig.read()
print((APIBasicConfig.DB_PATH))
conn = sqlite3.connect(APIBasicConfig.DB_PATH)
cursor = conn.cursor()

# Getting database version
if check_version:
try:
cursor.execute('SELECT version '
'FROM db_version;')
row = cursor.fetchone()
version = row[0]
except sqlite3.OperationalError:
# If this exception is thrown it's because db_version does not exist.
# Database is from 2.x or 3.0 beta releases
try:
cursor.execute('SELECT type '
'FROM experiment;')
# If type field exists, it's from 2.x
version = -1
except sqlite3.Error:
# If raises and error , it's from 3.0 beta releases
version = 0

# If database version is not the expected, update database....
if version < CURRENT_DATABASE_VERSION:
if not _update_database(version, cursor):
raise DbException('Database version could not be updated')

# ... or ask for autosubmit upgrade
elif version > CURRENT_DATABASE_VERSION:
Log.critical('Database version is not compatible with this autosubmit version. Please execute pip install '
'autosubmit --upgrade')
raise DbException('Database version not compatible')

return conn, cursor


def close_conn(conn: Connection, cursor):
"""
Commits changes and close connection to database
:param conn: connection to close
:type conn: sqlite3.Connection
:param cursor: cursor to close
:type cursor: sqlite3.Cursor
"""
conn.commit()
cursor.close()
conn.close()
return


def check_experiment_exists(name, error_on_inexistence=True):
"""
Checks if exist an experiment with the given name.
:param error_on_inexistence: if True, adds an error log if experiment does not exists
:type error_on_inexistence: bool
:param name: Experiment name
:type name: str
:return: If experiment exists returns true, if not returns false
:rtype: bool
"""
if not check_db():
return False
try:
(conn, cursor) = open_conn()
except DbException as e:
Log.error(
'Connection to database could not be established: {0}', e.message)
return False
conn.isolation_level = None

# SQLite always return a unicode object, but we can change this
# behaviour with the next sentence
conn.text_factory = str
cursor.execute(
'select name from experiment where name=:name', {'name': name})
row = cursor.fetchone()
close_conn(conn, cursor)
if row is None:
if error_on_inexistence:
Log.error('The experiment name "{0}" does not exist yet!!!', name)
return False
return True


def get_autosubmit_version(expid, log=None):
"""
Get the minimun autosubmit version needed for the experiment
:param expid: Experiment name
:type expid: str
:return: If experiment exists returns the autosubmit version for it, if not returns None
:rtype: str
"""
if not check_db():
return False

try:
(conn, cursor) = open_conn()
except DbException as e:
if log:
log.error(
'Connection to database could not be established: {0}', e.message)
return False
conn.isolation_level = None

# SQLite always return a unicode object, but we can change this
# behaviour with the next sentence
conn.text_factory = str
cursor.execute('SELECT autosubmit_version FROM experiment WHERE name=:expid', {
'expid': expid})
row = cursor.fetchone()
close_conn(conn, cursor)
if row is None:
if log:
log.error('The experiment "{0}" does not exist yet!!!', expid)
return None
return row[0]
from autosubmit_api.repositories.join.experiment_join import (
create_experiment_join_repository,
)


def search_experiment_by_id(query, exp_type=None, only_active=None, owner=None):
Expand Down Expand Up @@ -357,133 +211,3 @@ def get_current_running_exp():
}
)
return {"experiment": result}


def _update_database(version, cursor):
Log.info("Autosubmit's database version is {0}. Current version is {1}. Updating...",
version, CURRENT_DATABASE_VERSION)
try:
# For databases from Autosubmit 2
if version <= -1:
cursor.executescript('CREATE TABLE experiment_backup(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '
'name VARCHAR NOT NULL, type VARCHAR, autosubmit_version VARCHAR, '
'description VARCHAR NOT NULL, model_branch VARCHAR, template_name VARCHAR, '
'template_branch VARCHAR, ocean_diagnostics_branch VARCHAR);'
'INSERT INTO experiment_backup (name,type,description,model_branch,template_name,'
'template_branch,ocean_diagnostics_branch) SELECT name,type,description,model_branch,'
'template_name,template_branch,ocean_diagnostics_branch FROM experiment;'
'UPDATE experiment_backup SET autosubmit_version = "2";'
'DROP TABLE experiment;'
'ALTER TABLE experiment_backup RENAME TO experiment;')
if version <= 0:
# Autosubmit beta version. Create db_version table
cursor.executescript('CREATE TABLE db_version(version INTEGER NOT NULL);'
'INSERT INTO db_version (version) VALUES (1);'
'ALTER TABLE experiment ADD COLUMN autosubmit_version VARCHAR;'
'UPDATE experiment SET autosubmit_version = "3.0.0b" '
'WHERE autosubmit_version NOT NULL;')
cursor.execute('UPDATE db_version SET version={0};'.format(
CURRENT_DATABASE_VERSION))
except sqlite3.Error as e:
Log.critical('Can not update database: {0}', e)
return False
Log.info("Update completed")
return True


def update_experiment_description_owner(name, new_description=None, owner=None):
"""
We are suppossing that the front-end is making the owner validation.
:param expid:
:type expid:
:param new_description:
:type new_description:
:param owner:
:type owner:
"""
error = False
auth = False
description = None
message = None
try:
if new_description and owner:
result = _update_experiment_descrip_version(name, new_description)
if result:
auth = True
description = new_description
message = "Description Updated."
else:
error = True
if not new_description and not owner:
auth = False
message = "Not a valid user and no description provided"
elif new_description and not owner:
# Description provided by no valid user
auth = False
message = "It seems that your session has expired, please log in again."
else:
message = "No description provided."
except Exception as exp:
error = True
message = str(exp)
return {
'error': error,
'auth': auth,
'description': description,
'message': message
}


def _update_experiment_descrip_version(name, description=None, version=None):
"""
Updates the experiment's description and/or version
:param name: experiment name (expid)
:rtype name: str
:param description: experiment new description
:rtype description: str
:param version: experiment autosubmit version
:rtype version: str
:return: If description has been update, True; otherwise, False.
:rtype: bool
"""
if not check_db():
return False
try:
(conn, cursor) = open_conn()
except DbException:
raise Exception(
"Could not establish a connection to the database.")
conn.isolation_level = None

# Changing default unicode
conn.text_factory = str
# Conditional update
if description is not None and version is not None:
cursor.execute('update experiment set description=:description, autosubmit_version=:version where name=:name', {
'description': description, 'version': version, 'name': name})
elif description is not None and version is None:
cursor.execute('update experiment set description=:description where name=:name', {
'description': description, 'name': name})
elif version is not None and description is None:
cursor.execute('update experiment set autosubmit_version=:version where name=:name', {
'version': version, 'name': name})
else:
raise Exception(
"Not enough data to update {}.".format(name))
row = cursor.rowcount
close_conn(conn, cursor)
if row == 0:
raise Exception(
"Update on experiment {} failed.".format(name))
return False
return True


class DbException(Exception):
"""
Exception class for database errors
"""

def __init__(self, message):
self.message = message

0 comments on commit 4568c42

Please sign in to comment.