def remove_old_backups(backup_dir, ext, max_keep): path_list = sorted(ut.glob(backup_dir, '*%s' % ext)) if len(path_list) > max_keep: path_delete_list = path_list[:-1 * max_keep] for path_delete in path_delete_list: print('[ensure_daily_database_backup] Deleting old backup %r' % path_delete) ut.remove_file(path_delete, verbose=False)
def _make_empty_controller(): print('make_empty_controller') sqldb_fname = 'temp_test_sql_control.sqlite3' sqldb_dpath = utool.util_cplat.get_app_resource_dir('ibeis', 'testfiles') utool.ensuredir(sqldb_dpath) utool.remove_file(join(sqldb_dpath, sqldb_fname), dryrun=False) db = SQLDatabaseControl.SQLDatabaseController(sqldb_dpath=sqldb_dpath, sqldb_fname=sqldb_fname) return db
def update_schema_version(ibs, db, schema_spec, version, version_target, dobackup=True, clearbackup=False): """ version_target = version_expected clearbackup = False FIXME: AN SQL HELPER FUNCTION SHOULD BE AGNOSTIC TO CONTROLER OBJECTS """ def _check_superkeys(): all_tablename_list = db.get_table_names() # always ignore the metadata table. ignore_tables_ = ['metadata'] tablename_list = [tablename for tablename in all_tablename_list if tablename not in ignore_tables_] for tablename in tablename_list: superkey_colnames_list = db.get_table_superkey_colnames(tablename) # some tables seem to only have old constraints and aren't # properly updated to superkeys... weird. old_constraints = db.get_table_constraints(tablename) assert len(superkey_colnames_list) > 0 or len(old_constraints) > 0, ( 'ERROR UPDATING DATABASE, SUPERKEYS of %s DROPPED!' % (tablename,)) print('[_SQL] update_schema_version') db_fpath = db.fpath if dobackup: db_dpath, db_fname = split(db_fpath) db_fname_noext, ext = splitext(db_fname) db_backup_fname = ''.join((db_fname_noext, '_backup', '_v', version, ext)) db_backup_fpath = join(db_dpath, db_backup_fname) count = 0 # TODO MAKE UTOOL THAT DOES THIS (there might be one in util_logging) while ut.checkpath(db_backup_fpath, verbose=True): db_backup_fname = ''.join((db_fname_noext, '_backup', '_v', version, '_copy', str(count), ext)) db_backup_fpath = join(db_dpath, db_backup_fname) count += 1 ut.copy(db_fpath, db_backup_fpath) legacy_update_funcs = schema_spec.LEGACY_UPDATE_FUNCTIONS for legacy_version, func in legacy_update_funcs: if compare_string_versions(version, legacy_version) == -1: func(db) db_versions = schema_spec.VALID_VERSIONS valid_versions = sorted(db_versions.keys(), compare_string_versions) try: start_index = valid_versions.index(version) + 1 except IndexError: raise AssertionError('[!update_schema_version]' ' The current database version is unknown') try: end_index = valid_versions.index(version_target) + 1 except IndexError: raise AssertionError('[!update_schema_version]' ' The target database version is unknown') try: print('Update path: %r ' % (valid_versions[start_index:end_index])) for index in range(start_index, end_index): next_version = valid_versions[index] print('Updating database to version: %r' % (next_version)) pre, update, post = db_versions[next_version] if pre is not None: pre(db, ibs=ibs) if update is not None: update(db, ibs=ibs) if post is not None: post(db, ibs=ibs) _check_superkeys() except Exception as ex: if dobackup: msg = 'The database update failed, rolled back to the original version.' ut.printex(ex, msg, iswarning=True) ut.remove_file(db_fpath) ut.copy(db_backup_fpath, db_fpath) if clearbackup: ut.remove_file(db_backup_fpath) raise else: ut.printex(ex, ( 'The database update failed, and no backup was made.'), iswarning=False) raise if dobackup and clearbackup: ut.remove_file(db_backup_fpath)