diff --git a/alembic_db/alembic/versions/ef49f6644e0c_save_user_code.py b/alembic_db/alembic/versions/ef49f6644e0c_save_user_code.py index 7f818c9d9..e6d74ce99 100644 --- a/alembic_db/alembic/versions/ef49f6644e0c_save_user_code.py +++ b/alembic_db/alembic/versions/ef49f6644e0c_save_user_code.py @@ -13,7 +13,6 @@ from alembic_db.alembic_post_utils import write_revision_post_alembic -from mycodo.config import SQL_DATABASE_MYCODO from mycodo.config import ID_FILE from mycodo.config import STATS_CSV @@ -24,8 +23,6 @@ branch_labels = None depends_on = None -MYCODO_DB_PATH = f'sqlite:///{SQL_DATABASE_MYCODO}' - def upgrade(): write_revision_post_alembic(revision) diff --git a/mycodo/config.py b/mycodo/config.py index f6bc85edd..24aaa6410 100644 --- a/mycodo/config.py +++ b/mycodo/config.py @@ -3,21 +3,16 @@ # config.py - Global Mycodo settings # import binascii -import sys +import os import subprocess +import sys from datetime import timedelta -import os from flask_babel import lazy_gettext as lg # Append proper path for other software reading this config file sys.path.append(os.path.abspath(os.path.dirname(__file__))) - from config_translations import TRANSLATIONS as T -try: - import config_override -except: - pass MYCODO_VERSION = '8.16.0' ALEMBIC_VERSION = '5966b3569c89' @@ -40,25 +35,21 @@ # Install path (the parent directory of this file) INSTALL_DIRECTORY = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/..') -# Database +# Settings database DATABASE_PATH = os.path.join(INSTALL_DIRECTORY, 'databases') DATABASE_NAME = "mycodo.db" SQL_DATABASE_MYCODO = os.path.join(DATABASE_PATH, DATABASE_NAME) +ALEMBIC_PATH = os.path.join(INSTALL_DIRECTORY, 'alembic_db') +ALEMBIC_UPGRADE_POST = os.path.join(ALEMBIC_PATH, 'alembic_post_upgrade_versions') + try: + import config_override MYCODO_DB_PATH = config_override.MYCODO_DB_PATH except: MYCODO_DB_PATH = f'sqlite:///{SQL_DATABASE_MYCODO}' -# Alembic -ALEMBIC_PATH = os.path.join(INSTALL_DIRECTORY, 'alembic_db') -ALEMBIC_UPGRADE_POST = os.path.join(ALEMBIC_PATH, 'alembic_post_upgrade_versions') -try: # Ensure the correct alembic url is set - ALEMBIC_URL = config_override.ALEMBIC_URL - cmd = f'/opt/Mycodo/env/bin/crudini --set /opt/Mycodo/alembic_db/alembic.ini alembic sqlalchemy.url {ALEMBIC_URL}' - subprocess.Popen(cmd, shell=True) -except: - cmd = '/opt/Mycodo/env/bin/crudini --set /opt/Mycodo/alembic_db/alembic.ini alembic sqlalchemy.url sqlite:///../databases/mycodo.db' - subprocess.Popen(cmd, shell=True) +cmd = f'/opt/Mycodo/env/bin/crudini --set /opt/Mycodo/alembic_db/alembic.ini alembic sqlalchemy.url {MYCODO_DB_PATH}' +subprocess.Popen(cmd, shell=True) # Misc paths PATH_1WIRE = '/sys/bus/w1/devices/' diff --git a/mycodo/functions/example_function_all_options.py b/mycodo/functions/example_function_all_options.py index 84acb2270..137597f12 100644 --- a/mycodo/functions/example_function_all_options.py +++ b/mycodo/functions/example_function_all_options.py @@ -374,7 +374,7 @@ def loop(self): # Deactivate controller in the SQL database self.logger.debug( "Deactivating (SQL) Custom controller select_device_2 with ID {}".format(self.select_device_2_id)) - from mycodo.config import MYCODO_DB_PATH, SQL_DATABASE_MYCODO + from mycodo.config import MYCODO_DB_PATH from mycodo.databases.utils import session_scope with session_scope(MYCODO_DB_PATH) as new_session: mod_cont = new_session.query(CustomController).filter( diff --git a/mycodo/utils/tools.py b/mycodo/utils/tools.py index 049837ad0..6c3b86636 100644 --- a/mycodo/utils/tools.py +++ b/mycodo/utils/tools.py @@ -86,8 +86,12 @@ def create_settings_export(save_path=None): try: data = io.BytesIO() with zipfile.ZipFile(data, mode='w') as z: - z.write(SQL_DATABASE_MYCODO, - os.path.basename(SQL_DATABASE_MYCODO)) + try: + z.write(SQL_DATABASE_MYCODO, + os.path.basename(SQL_DATABASE_MYCODO)) + except: + logger.error(f"Could not find database file {SQL_DATABASE_MYCODO}") + export_directories = [ (PATH_FUNCTIONS_CUSTOM, "custom_functions"), (PATH_ACTIONS_CUSTOM, "custom_actions"),