def save(self, commit=True): if not hasattr(self, 'slug') or not self.slug: self.slug = slugify(self.name) session = Session() log.debug(self) session.add(self) if commit: session.commit()
def get_unmigrated_resources(): # type: () -> Generator[Resource, None, None] """Generator of un-migrated resource This works by fetching one resource at a time using SELECT FOR UPDATE SKIP LOCKED. Once the resource has been migrated to the new storage, it will be unlocked. This allows running multiple migrator scripts in parallel, without any conflicts and with small chance of re-doing any work. While a specific resource is being migrated, it will be locked for modification on the DB level. Users can still read the resource without any effect. """ session = Session() session.revisioning_disabled = True # Start from inspecting all uploaded, undeleted resources all_resources = session.query(Resource).filter( Resource.url_type == 'upload', Resource.state != 'deleted', ).order_by(Resource.created).options( load_only("id", "extras", "package_id")) for resource in all_resources: if not _needs_migration(resource): _log().debug("Skipping resource %s as it was already migrated", resource.id) continue with db_transaction(session): locked_resource = session.query(Resource).filter(Resource.id == resource.id).\ with_for_update(skip_locked=True).one_or_none() if locked_resource is None: _log().debug( "Skipping resource %s as it is locked (being migrated?)", resource.id) continue # let's double check as the resource might have been migrated by another process by now if not _needs_migration(locked_resource): continue yield locked_resource
def init_db(): """ Create board, app, mark tables in the database. Prepopulate category table with default data. """ if not model.package_table.exists(): # during tests? return session = Session() for table in [board_table, app_table, mark_table]: if not table.exists(): table.create(checkfirst=True) log.debug("Apps {} have been created".format(table.name)) for board_name, board_desc in DEFAULT_BOARDS.iteritems(): if not Board.get_by_slug(slugify(board_name)): board = Board() board.name = board_name board.slug = slugify(board_name) board.description = board_desc session.add(board) log.debug("Add {0} to {1} table".format(board_name, board_table.name)) session.commit() if not migration_table.exists(): migration_table.create(checkfirst=True) session.commit() migration_number = session.query(migration_table).count() log.debug('Migration number: %s', migration_number) migration_sql_list = [] for counter, sql in enumerate(migration_sql_list, start=1): if migration_number < counter: try: session.execute(sql) except ProgrammingError: session.rollback() finally: session.execute(migration_table.insert()) session.commit() session.close()
def save(self, commit=True): session = Session() log.debug(self) session.add(self) if commit: session.commit()
def delete(self, commit=True): session = Session() session.delete(self) if commit: session.commit()
def get_rate(self): session = Session() marks = session.query(Mark).filter(Mark.app_id == self.id) if not marks.count(): # If no marks then return 0 return 0 return int(sum([mark.mark for mark in marks]) / marks.count())
def unhide(self): self.active = True session = Session() session.add(self) session.commit()
def hide(self): self.active = False session = Session() session.add(self) session.commit()
def add(cls, user_id, thread_id): session = Session() unsubscr = cls(user_id, thread_id) session.add(unsubscr) session.commit()
def unban(cls, user_id): session = Session() session.query(cls).filter(cls.author_id == user_id).delete() session.commit()
def filter_by_thread_id(cls, thread_id): session = Session() return session.query(cls).filter(cls.thread_id == thread_id)
def ban(cls, user_id): session = Session() banned_user = cls(user_id) if not session.query(cls).filter(cls.author_id == user_id).first(): session.add(banned_user) session.commit()
def check_by_id(cls, user): session = Session() return session.query( session.query(cls).filter( cls.author_id == user.id).exists()).scalar()
def get_banned_users(cls): session = Session() query = session.query(cls, model.User).join( User, cls.author_id == User.id).order_by(cls.id.desc()) return query
def ban(self): session = Session() session.query( self.__class__).filter(self.__class__.id == self.id).update( {self.__class__.active: False}) session.commit()
def init_db(): """ Create boards, threads and posts tables in the database. Prepopulate board table with default data. """ if not model.package_table.exists(): # during tests? return session = Session() if not board_table.exists(): board_table.create(checkfirst=True) thread_table.create(checkfirst=True) post_table.create(checkfirst=True) log.debug("Forum tables have been created") for board_name, board_desc in DEFAULT_BOARDS.iteritems(): board = Board() board.name = board_name board.slug = slugify(board_name) board.description = board_desc session.add(board) if session.new: log.debug('Default boards created') session.commit() if not migration_table.exists(): migration_table.create(checkfirst=True) session.commit() if not banned_table.exists(): banned_table.create(checkfirst=True) session.commit() if not unsubscription_table.exists(): unsubscription_table.create(checkfirst=True) session.commit() migration_number = session.query(migration_table).count() log.debug('Migration number: %s', migration_number) migration_sql_list = [ "ALTER TABLE forum_post ADD COLUMN active boolean DEFAULT TRUE", "ALTER TABLE forum_thread ADD COLUMN active boolean DEFAULT TRUE", "ALTER TABLE forum_board ADD COLUMN active boolean DEFAULT TRUE", "ALTER TABLE forum_thread DROP COLUMN slug", "ALTER TABLE forum_thread ADD COLUMN can_post boolean DEFAULT TRUE", "ALTER TABLE forum_board ADD COLUMN can_post boolean DEFAULT TRUE", u"INSERT INTO forum_board(\"id\", \"name\", \"slug\", \"description\", \"active\", \"can_post\") " + u"VALUES(DEFAULT, 'Запропонувати набір', 'zaproponuvati-nabir', '', true, false)" ] for counter, sql in enumerate(migration_sql_list, start=1): if migration_number < counter: try: log.debug(sql) session.execute(sql) except ProgrammingError as e: print(e) log.debug('Migration have been rolled back.') session.rollback() finally: session.execute(migration_table.insert()) session.commit() session.close()