def get_db_connection() -> sqlite3.Connection: if Helper.__db_connection is None: Helper.__db_connection = sqlite3.connect( mc_global.get_database_filename()) # Upgrading the database # Very good upgrade explanation: # http://stackoverflow.com/questions/19331550/database-change-with-software-update # More info here: https://www.sqlite.org/pragma.html#pragma_schema_version current_db_ver_it = get_schema_version(Helper.__db_connection) target_db_ver_it = max(upgrade_steps) database_tables_dropped_bool = False if current_db_ver_it < min( upgrade_steps ) and mc_global.db_file_exists_at_application_startup_bl: database_tables_dropped_bool = True backup_db_file() mc_global.db_upgrade_message_str = ( "Database upgraded, all user entries have been removed. " "The old db has been backed up, and is available in the user_files directory. " ) try: model.export_all() # -please note that there is a high likelihood that this fails, the reason is that the tables # have now been changed mc_global.db_upgrade_message_str += ( "Some of the old db contents has also been exported to a text file " "that you can find here: /user_files/exported.csv") except: pass Helper.drop_all_db_tables(Helper.__db_connection) for upgrade_step_nr_int in range(current_db_ver_it + 1, target_db_ver_it + 1): if upgrade_step_nr_int in upgrade_steps: upgrade_steps[upgrade_step_nr_int](Helper.__db_connection) set_schema_version(Helper.__db_connection, upgrade_step_nr_int) if mc_global.testing_bool: model.populate_db_with_test_data() else: if database_tables_dropped_bool: model.populate_db_with_setup_data() elif not mc_global.db_file_exists_at_application_startup_bl: model.populate_db_with_setup_data() else: pass # -default, the user has started the application before and is not testing # TODO: Where do we close the db connection? (Do we need to close it?) # http://stackoverflow.com/questions/3850261/doing-something-before-program-exit return Helper.__db_connection
def get_db_connection() -> sqlite3.Connection: if Helper.__db_connection is None: Helper.__db_connection = sqlite3.connect(mc_global.get_database_filename()) # Upgrading the database # Very good upgrade explanation: # http://stackoverflow.com/questions/19331550/database-change-with-software-update # More info here: https://www.sqlite.org/pragma.html#pragma_schema_version current_db_ver_it = get_schema_version(Helper.__db_connection) target_db_ver_it = max(upgrade_steps) database_tables_dropped_bool = False if current_db_ver_it < min(upgrade_steps) and mc_global.db_file_exists_at_application_startup_bl: database_tables_dropped_bool = True backup_db_file() mc_global.db_upgrade_message_str = ( "Database upgraded, all user entries have been removed. " "The old db has been backed up, and is available in the user_files directory. " ) try: model.export_all() # -please note that there is a high likelihood that this fails, the reason is that the tables # have now been changed mc_global.db_upgrade_message_str += ( "Some of the old db contents has also been exported to a text file " "that you can find here: /user_files/exported.csv" ) except Exception: pass Helper.drop_all_db_tables(Helper.__db_connection) for upgrade_step_nr_int in range(current_db_ver_it + 1, target_db_ver_it + 1): if upgrade_step_nr_int in upgrade_steps: upgrade_steps[upgrade_step_nr_int](Helper.__db_connection) set_schema_version(Helper.__db_connection, upgrade_step_nr_int) if mc_global.testing_bool: model.populate_db_with_test_data() else: if database_tables_dropped_bool: model.populate_db_with_setup_data() elif not mc_global.db_file_exists_at_application_startup_bl: model.populate_db_with_setup_data() else: pass # -default, the user has started the application before and is not testing # TODO: Where do we close the db connection? (Do we need to close it?) # http://stackoverflow.com/questions/3850261/doing-something-before-program-exit return Helper.__db_connection
def get_db_connection(): if Helper.__db_connection is None: Helper.__db_connection = sqlite3.connect(mc_global.get_database_filename()) # Upgrading the database # Very good upgrade explanation: # http://stackoverflow.com/questions/19331550/database-change-with-software-update # More info here: https://www.sqlite.org/pragma.html#pragma_schema_version current_db_ver_it = get_schema_version(Helper.__db_connection) target_db_ver_it = max(upgrade_steps) for upgrade_step_it in range(current_db_ver_it + 1, target_db_ver_it + 1): if upgrade_step_it in upgrade_steps: upgrade_steps[upgrade_step_it](Helper.__db_connection) set_schema_version(Helper.__db_connection, upgrade_step_it) Helper.__db_connection.commit() # TODO: Where do we close the db connection? (Do we need to close it?) # http://stackoverflow.com/questions/3850261/doing-something-before-program-exit return Helper.__db_connection
def get_db_connection(): if Helper.__db_connection is None: Helper.__db_connection = sqlite3.connect( mc_global.get_database_filename()) # Upgrading the database # Very good upgrade explanation: # http://stackoverflow.com/questions/19331550/database-change-with-software-update # More info here: https://www.sqlite.org/pragma.html#pragma_schema_version current_db_ver_it = get_schema_version(Helper.__db_connection) target_db_ver_it = max(upgrade_steps) database_tables_dropped_bool = False if current_db_ver_it < min( upgrade_steps ) and mc_global.db_file_exists_at_application_startup_bl: database_tables_dropped_bool = True backup_db_file() model.export_all() Helper.drop_all_db_tables(Helper.__db_connection) mc_global.db_upgrade_message_str = ( "Database upgraded, all user entries have been removed," "you can find them in this file: /user_files/exported.csv") for upgrade_step_nr_int in range(current_db_ver_it + 1, target_db_ver_it + 1): if upgrade_step_nr_int in upgrade_steps: upgrade_steps[upgrade_step_nr_int](Helper.__db_connection) set_schema_version(Helper.__db_connection, upgrade_step_nr_int) if database_tables_dropped_bool: model.populate_db_with_setup_data() Helper.__db_connection.commit() # TODO: Where do we close the db connection? (Do we need to close it?) # http://stackoverflow.com/questions/3850261/doing-something-before-program-exit return Helper.__db_connection
#!/usr/bin/env python3 import logging import os import sqlite3 import sys import PyQt5.Qt from PyQt5 import QtCore from PyQt5 import QtWidgets import mc.gui.main_win from mc import mc_global import mc.db if __name__ == "__main__": mc_global.db_file_exists_at_application_startup_bl = os.path.isfile( mc_global.get_database_filename()) # -settings this variable before the file has been created logging.basicConfig( level=logging.DEBUG) # -by default only warnings and higher are shown # Application information logging.info("===== Starting " + mc_global.APPLICATION_TITLE_STR + " - " + mc_global.APPLICATION_VERSION_STR + " =====") logging.info("Python version: " + str(sys.version)) logging.info("SQLite version: " + str(sqlite3.sqlite_version)) logging.info("PySQLite (Python module) version: " + str(sqlite3.version)) logging.info("Qt version: " + str(QtCore.qVersion())) # noinspection PyUnresolvedReferences logging.info("PyQt (Python module) version: " + str(PyQt5.Qt.PYQT_VERSION_STR)) logging.info(mc_global.APPLICATION_TITLE_STR + " - Application version: " +
def backup_db_file() -> None: if mc_global.testing_bool: return date_sg = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") new_file_name_sg = mc_global.get_database_filename(date_sg) shutil.copyfile(mc_global.get_database_filename(), new_file_name_sg)
def backup_db_file(): date_sg = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") new_file_name_sg = mc_global.get_database_filename() + "_" + date_sg shutil.copyfile(mc_global.get_database_filename(), new_file_name_sg)
#!/usr/bin/env python3 import logging import os import sqlite3 import sys import PyQt5.Qt from PyQt5 import QtCore from PyQt5 import QtWidgets import mc.gui.main_win from mc import mc_global import mc.db if __name__ == "__main__": mc_global.db_file_exists_at_application_startup_bl = os.path.isfile(mc_global.get_database_filename()) # -settings this variable before the file has been created logging.basicConfig(level=logging.DEBUG) # -by default only warnings and higher are shown # Application information mc.mc_global.sys_info_telist.append(("Application name", mc.mc_global.APPLICATION_VERSION_STR)) mc.mc_global.sys_info_telist.append(("Application version", mc.mc_global.APPLICATION_VERSION_STR)) db_conn = mc.db.Helper.get_db_connection() mc.mc_global.sys_info_telist.append(("Application database schema version", mc.db.get_schema_version(db_conn))) mc.mc_global.sys_info_telist.append(("Python version", sys.version)) mc.mc_global.sys_info_telist.append(("SQLite version", sqlite3.sqlite_version)) mc.mc_global.sys_info_telist.append(("PySQLite (Python module) version", sqlite3.version)) mc.mc_global.sys_info_telist.append(("Qt version", QtCore.qVersion())) # noinspection PyUnresolvedReferences mc.mc_global.sys_info_telist.append(("PyQt (Python module) version", PyQt5.Qt.PYQT_VERSION_STR))