def update_db(): db = get_sqlite_database() # First test for version 1 try: next(c for c in db.get_columns("settings") if c.name == "version") except Exception as e: if len(db.get_tables()) == 0: data_dir = get_data_dir() if os.path.exists(os.path.join(data_dir, "cozy.db")): os.remove(os.path.join(data_dir, "cozy.db")) os.remove(os.path.join(data_dir, "cozy.db-shm")) os.remove(os.path.join(data_dir, "cozy.db-wal")) __update_db_1(db) version = Settings.get().version # then for version 2 and so on if version < 2: __update_db_2(db) if version < 3: __update_db_3(db) if version < 4: __update_db_4(db) if version < 5: __update_db_5(db) if version < 6: __update_db_6(db) if version < 7: __update_db_7(db) if version < 8: __update_db_8(db)
from cozy.report import reporter log = logging.getLogger("db") from peewee import __version__ as PeeweeVersion from apsw import apswversion if PeeweeVersion[0] == '2': log.info("Using peewee 2 backend") from peewee import BaseModel ModelBase = BaseModel else: log.info("Using peewee 3 backend") from gi.repository import GLib, Gdk _db = get_sqlite_database() def init_db(): tmp_db = None _connect_db(_db) sqlite_version = ".".join([str(num) for num in _db.server_version]) log.info("SQLite version: {}, APSW version: {}".format( sqlite_version, apswversion())) if Settings.table_exists(): update_db() else: tmp_db = PooledSqliteDatabase(os.path.join(get_data_dir(), "cozy.db"))
def update_db(): db = get_sqlite_database() # First test for version 1 try: next(c for c in db.get_columns("settings") if c.name == "version") except Exception as e: if len(db.get_tables()) == 0: data_dir = get_data_dir() if os.path.exists(os.path.join(data_dir, "cozy.db")): os.remove(os.path.join(data_dir, "cozy.db")) os.remove(os.path.join(data_dir, "cozy.db-shm")) os.remove(os.path.join(data_dir, "cozy.db-wal")) __update_db_1(db) version = Settings.get().version # then for version 2 and so on if version < 2: __update_db_2(db) if version < 3: __update_db_3(db) if version < 4: __update_db_4(db) if version < 5: __update_db_5(db) if version < 6: __update_db_6(db) if version < 7: __update_db_7(db) if version < 8: __update_db_8(db) if version < 9: backup_dir_name = _backup_db(db) try: _update_db_9(db) except Exception as e: log.error(e) reporter.exception("db_updator", e) db.stop() _restore_db(backup_dir_name) from cozy.ui.db_migration_failed_view import DBMigrationFailedView dialog = DBMigrationFailedView() dialog.show() exit(1) if version < 10: backup_dir_name = _backup_db(db) try: _update_db_10(db) except Exception as e: log.error(e) reporter.exception("db_updator", e) db.stop() _restore_db(backup_dir_name) from cozy.ui.db_migration_failed_view import DBMigrationFailedView dialog = DBMigrationFailedView() dialog.show() exit(1)