def inject_in_db(cls, enable_player=False): from mediacore.model import DBSession from mediacore.model.players import players as players_table, PlayerPrefs prefs = PlayerPrefs() prefs.name = cls.name prefs.enabled = enable_player # didn't get direct SQL expression to work with SQLAlchemy # player_table = sql.func.max(player_table.c.priority) query = sql.select([sql.func.max(players_table.c.priority)]) max_priority = DBSession.execute(query).first()[0] if max_priority is None: max_priority = -1 prefs.priority = max_priority + 1 prefs.created_on = datetime.now() prefs.modified_on = datetime.now() prefs.data = cls.default_data DBSession.add(prefs) DBSession.commit()
def map_migrate_version(self): migrate_version_query = migrate_table.select( migrate_table.c.repository_id == u'MediaCore Migrations' ) result = DBSession.execute(migrate_version_query).fetchone() db_migrate_version = result.version if db_migrate_version in migrate_to_alembic_mapping: return migrate_to_alembic_mapping[db_migrate_version] earliest_upgradable_version = sorted(migrate_to_alembic_mapping)[0] if db_migrate_version < earliest_upgradable_version: error_msg = ('Upgrading from such an old version of MediaCore is not ' 'supported. Your database is at version %d but upgrades are only ' 'supported from MediaCore CE 0.9.0 (DB version %d). Please upgrade ' '0.9.0 first.') self.log.error(error_msg % (db_migrate_version, earliest_upgradable_version)) else: self.log.error('Unknown DB version %s. Can not upgrade to alembic' % db_migrate_version) raise AssertionError('unsupported DB migration version.')
def map_migrate_version(self): migrate_version_query = migrate_table.select( migrate_table.c.repository_id == u'MediaCore Migrations') result = DBSession.execute(migrate_version_query).fetchone() db_migrate_version = result.version if db_migrate_version in migrate_to_alembic_mapping: return migrate_to_alembic_mapping[db_migrate_version] earliest_upgradable_version = sorted(migrate_to_alembic_mapping)[0] if db_migrate_version < earliest_upgradable_version: error_msg = ( 'Upgrading from such an old version of MediaCore is not ' 'supported. Your database is at version %d but upgrades are only ' 'supported from MediaCore CE 0.9.0 (DB version %d). Please upgrade ' '0.9.0 first.') self.log.error(error_msg % (db_migrate_version, earliest_upgradable_version)) else: self.log.error( 'Unknown DB version %s. Can not upgrade to alembic' % db_migrate_version) raise AssertionError('unsupported DB migration version.')
def reposition_file(self, file, budge_infront=None): """Position the first file after the second or last file. If only one file is specified, we move it to the last position (the end). If two files are specified, the first takes the seconds position, and the positions of the second file and those that follow it are incremented. This increments MediaFile.position such that gaps in the sequence occur. When the primary_file is changed by this operation, we ensure that the Media.type matches the type of the new primary_file. Depending on the situation, we manipulate the DBSession, rather than our usual practice of simply modifying the object and leaving DBSession work to the controller. This is necessary because we run a query on the DB without using the ORM in some cases. :param file: The file to move :type file: :class:`MediaFile` or int :param budge_infront: The file to position after. :type budge_infront: :class:`MediaFile` or int or None """ if not isinstance(file, MediaFile): file = [f for f in self.files if f.id == file][0] if budge_infront: # The first file is going to move in front of the second if not isinstance(budge_infront, MediaFile): budge_infront = [f for f in self.files if f.id == budge_infront][0] pos = budge_infront.position is_first = budge_infront is self.primary_file # Update the moved row, the budge_infront file, and those after it. # When we reach the moved row, the new position is simply set, # otherwise the position is incremented 1. update = media_files.update()\ .where(sql.and_( media_files.c.media_id == self.id, sql.or_( media_files.c.position >= pos, media_files.c.id == file.id ) ))\ .values({ media_files.c.position: sql.case( [(media_files.c.id == file.id, pos)], else_=media_files.c.position + 1 ) }) DBSession.execute(update) datamanager.mark_changed(DBSession()) else: # No budging, so if there any other files we'll have to go after them... pos = 0 is_first = True if self.files: pos += self.files[-1].position is_first = False file.position = pos DBSession.add(file) # Making an audio file primary over a video file changes the media type if is_first and file.medium is not None: self.type = file.medium return pos