コード例 #1
0
 def delete(session: Session, sensor_type: str):
     try:
         sensor = session.query(SensorTypes).filter_by(
             sensor_type=sensor_type).first()
         session.delete(sensor)
     except NoResultFound as ex:
         """Sensor type does not exists. No need to nothing"""
コード例 #2
0
def handle_user_audio_tracks(
    session: Session,
    user_id: int,
    music_choices: Optional[List[str]],
    sfx_choice: Optional[str],
):
    """[summary]

    Args:
        session (Session): the database.
        user_id (int): current users' id.
        music_choices (Optional[List[str]]):
        a list of music tracks if music is enabled, None otherwise.
        sfx_choice (Optional[str]): choice for sound effect.
    """
    user_audio_tracks = session.query(UserAudioTracks).filter_by(
        user_id=user_id, )
    if user_audio_tracks:
        for record in user_audio_tracks:
            session.delete(record)
            session.commit()

    if music_choices:
        for track in music_choices:
            create_new_user_audio_record(session, track, user_id)
    if sfx_choice:
        create_new_user_audio_record(session, sfx_choice, user_id)
コード例 #3
0
    def deleteParameter(self, session: Session, name: str) -> None:
        """
        Delete parameter from the db.

            Returns:
                None
            Throws:
                ParameterNotFound
                DbError
        """

        try:
            p = self._globalParametersDbHandler.getParameter(session, name)

            self._logger.debug('Deleting parameter [%s]' % (name))

            session.delete(p)

            session.commit()

            self._logger.info('Deleted parameter [%s]' % name)
        except TortugaException:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self._logger.exception(str(ex))
            raise
コード例 #4
0
def delete_items(session: Session, url_hashes: List[str]) -> List[str]:
    deleted_items = []
    for obj in (session.query(ScraperItemModel).filter(
            ScraperItemModel.url_hash.in_(url_hashes)).all()):
        deleted_items.append(obj.url_hash)
        session.delete(obj)
    return deleted_items
コード例 #5
0
ファイル: storage.py プロジェクト: creviera/pyqt-sandbox
def update_sources(remote_sources: List[SDKSource],
                   local_sources: List[Source], session: Session,
                   data_dir: str) -> None:
    """
    Given collections of remote sources, the current local sources and a
    session to the local database, ensure the state of the local database
    matches that of the remote sources:

    * Existing items are updated in the local database.
    * New items are created in the local database.
    * Local items not returned in the remote sources are deleted from the
      local database.
    """
    local_sources_by_uuid = {s.uuid: s for s in local_sources}
    for source in remote_sources:
        if source.uuid in local_sources_by_uuid:
            # Update an existing record.
            local_source = local_sources_by_uuid[source.uuid]
            lazy_setattr(local_source, "journalist_designation",
                         source.journalist_designation)
            lazy_setattr(local_source, "is_flagged", source.is_flagged)
            lazy_setattr(local_source, "interaction_count",
                         source.interaction_count)
            lazy_setattr(local_source, "document_count",
                         source.number_of_documents)
            lazy_setattr(local_source, "is_starred", source.is_starred)
            lazy_setattr(local_source, "last_updated",
                         parse(source.last_updated))
            lazy_setattr(local_source, "public_key", source.key['public'])

            # Removing the UUID from local_sources_by_uuid ensures
            # this record won't be deleted at the end of this
            # function.
            del local_sources_by_uuid[source.uuid]
            logger.debug('Updated source {}'.format(source.uuid))
        else:
            # A new source to be added to the database.
            ns = Source(
                uuid=source.uuid,
                journalist_designation=source.journalist_designation,
                is_flagged=source.is_flagged,
                interaction_count=source.interaction_count,
                is_starred=source.is_starred,
                last_updated=parse(source.last_updated),
                document_count=source.number_of_documents,
                public_key=source.key['public'],
                fingerprint=source.key['fingerprint'],
            )
            session.add(ns)

            logger.debug('Added new source {}'.format(source.uuid))

    # The uuids remaining in local_uuids do not exist on the remote server, so
    # delete the related records.
    for deleted_source in local_sources_by_uuid.values():
        delete_source_collection(deleted_source.journalist_filename, data_dir)
        session.delete(deleted_source)
        logger.debug('Deleted source {}'.format(deleted_source.uuid))

    session.commit()
コード例 #6
0
ファイル: db_helper.py プロジェクト: Fathui/fsqlfly
 def clean(cls, model: str, pk: int, *args, session: Session,
           base: Type[DBT], **kwargs) -> DBRes:
     obj = session.query(base).get(pk)
     if isinstance(obj, Connector):
         back = obj.as_dict()
         source, target = obj.source, obj.target
         session.delete(obj)
         session.commit()
         cls._clean(source, session, Connection)
         cls._clean(target, session, Connection)
         session.add(Connector(**back))
     else:
         if isinstance(obj, Connection):
             s_names = [
                 x[0] for x in session.query(Connector.name).join(
                     Connector.source).filter(Connection.id == pk).all()
             ]
             t_names = [
                 x[0] for x in session.query(Connector.name).join(
                     Connector.target).filter(Connection.id == pk).all()
             ]
             msg = "please clean connector source: {} target: {}".format(
                 ','.join(s_names), ','.join(t_names))
             if s_names or t_names:
                 return DBRes.api_error(msg)
         cls._clean(obj, session, base)
     return DBRes()
コード例 #7
0
ファイル: Statification.py プロジェクト: ANSSI-FR/cornetto
 def delete(self, session: Session) -> None:
     """
     Delete the Statification
     :param session: the database session
     """
     session.delete(self)
     session.commit()
コード例 #8
0
def test_cascade_delete_table(app_context: None, session: Session) -> None:
    """
    Test that deleting ``Table`` also deletes its columns.
    """
    from superset.columns.models import Column
    from superset.models.core import Database
    from superset.tables.models import Table

    engine = session.get_bind()
    Table.metadata.create_all(engine)  # pylint: disable=no-member

    table = Table(
        name="my_table",
        schema="my_schema",
        catalog="my_catalog",
        database=Database(database_name="my_database",
                          sqlalchemy_uri="sqlite://"),
        columns=[
            Column(name="longitude", expression="longitude"),
            Column(name="latitude", expression="latitude"),
        ],
    )
    session.add(table)
    session.flush()

    columns = session.query(Column).all()
    assert len(columns) == 2

    session.delete(table)
    session.flush()

    # test that columns were deleted
    columns = session.query(Column).all()
    assert len(columns) == 0
コード例 #9
0
def test_delete_sqlatable(app_context: None, session: Session) -> None:
    """
    Test that deleting a ``SqlaTable`` also deletes the corresponding ``Dataset``.
    """
    from superset.columns.models import Column
    from superset.connectors.sqla.models import SqlaTable, TableColumn
    from superset.datasets.models import Dataset
    from superset.models.core import Database
    from superset.tables.models import Table

    engine = session.get_bind()
    Dataset.metadata.create_all(engine)  # pylint: disable=no-member

    columns = [
        TableColumn(column_name="ds", is_dttm=1, type="TIMESTAMP"),
    ]
    sqla_table = SqlaTable(
        table_name="old_dataset",
        columns=columns,
        metrics=[],
        database=Database(database_name="my_database",
                          sqlalchemy_uri="sqlite://"),
    )
    session.add(sqla_table)
    session.flush()

    datasets = session.query(Dataset).all()
    assert len(datasets) == 1

    session.delete(sqla_table)
    session.flush()

    # test that dataset was also deleted
    datasets = session.query(Dataset).all()
    assert len(datasets) == 0
コード例 #10
0
def upgrade():
    from sqlalchemy.orm.session import Session
    session = Session(bind=op.get_bind())

    # Find duplicates
    for vcs in session.query(Vcs).group_by(Vcs.repository_id, Vcs.revision).having(func.count(Vcs.id) > 1).all():
        print(vcs)
        # Find all vcs entries with this duplication
        dupes = session.query(Vcs).filter(Vcs.repository_id == vcs.repository_id).filter(Vcs.revision == vcs.revision).all()
        # Keep the first and remove the others - thus we need to update references to others to the first
        for update in dupes[1:]:
            for af in session.query(Artifakt).filter(Artifakt.vcs_id == update.id).all():
                print("Updating artifakt {} to point to vcs {}".format(af.sha1, dupes[0].id))
                af.vcs_id = dupes[0].id
            print("Deleting vcs  {}".format(update.id))
            session.delete(update)
    session.commit()

    if session.bind.dialect.name == "sqlite":
        session.execute("PRAGMA foreign_keys = OFF")
    elif session.bind.dialect.name == "mysql":
        session.execute("SET foreign_key_checks = 0")
    else:
        raise NotImplemented

    with op.batch_alter_table('vcs', schema=None) as batch_op:
        batch_op.create_unique_constraint('rr', ['repository_id', 'revision'])

    if session.bind.dialect.name == "sqlite":
        session.execute("PRAGMA foreign_keys = ON")
    elif session.bind.dialect.name == "mysql":
        session.execute("SET foreign_key_checks = 1")
コード例 #11
0
 def delete_cookie(session: Session, token) -> bool:
     c = session.query(Cookie).filter(Cookie.token == token).first()
     if c is None:
         return False
     session.delete(c)
     session.commit()
     return True
コード例 #12
0
ファイル: main.py プロジェクト: hstefan/fotc
def remove_quote_handler(db_session: DbSession, bot: telegram.Bot,
                         update: telegram.Update):
    """Adds a message as a user quote"""
    _, group, group_user = _record_presence(db_session, bot, update)
    if not update.message.reply_to_message:
        update.message.reply_text(
            "Command must be sent as a reply to a message", quote=True)

    user_repo = ChatUserRepository(db_session)
    group_repo = ChatGroupRepository(db_session)
    quote_repo = QuoteRepository(db_session)

    reply = update.message.reply_to_message
    quoted_user = user_repo.find_or_create_by_id(reply.from_user.id)
    quoted_group_user = group_repo.record_membership(group, quoted_user)

    quote = quote_repo.find_quote(quoted_group_user, str(reply.message_id))

    if not quote:
        update.message.reply_text("Message is not a registered quote",
                                  quote=True)
        return

    if group_user.id != quoted_group_user.id:
        update.message.reply_text(
            "Only the quoted user can remove his own quote", quote=True)
        return

    db_session.delete(quote)
    update.message.reply_text("Quote removed from database", quote=True)
コード例 #13
0
ファイル: nodeManager.py プロジェクト: cameronbrunner/tortuga
    def __delete_nodes(self, session: Session, kitmgr: KitActionsManager,
                       nodes: List[NodeModel]) -> None:
        """
        :raises DeleteNodeFailed:
        """

        hwprofile_nodes = self.__pre_delete_nodes(kitmgr, nodes)

        # commit node state changes to database
        session.commit()

        for hwprofile, node_data_dicts in hwprofile_nodes.items():
            # build list of NodeModels
            node_objs: List[NodeModel] = [
                node_data_dict['node'] for node_data_dict in node_data_dicts
            ]

            # Call resource adapter deleteNode() entry point
            self.__get_resource_adapter(session,
                                        hwprofile).deleteNode(node_objs)

            # Perform delete node action for each node in hwprofile
            for node_data_dict in node_data_dicts:
                # get JSON object for node record
                node_dict = NodeSchema(
                    only=('hardwareprofile', 'softwareprofile', 'name',
                          'state'),
                    exclude=('softwareprofile.metadata', )).dump(
                        node_data_dict['node']).data

                # Delete the Node
                self._logger.debug('Deleting node [%s]', node_dict['name'])

                #
                # Fire node state change events
                #
                NodeStateChanged.fire(
                    node=node_dict,
                    previous_state=node_data_dict['previous_state'])

                session.delete(node_data_dict['node'])

                # Commit the actual deletion of this node to the DB.  This is required
                # as the post_delete hooks may use a different DB session and we have
                # already commited some changes for this node.
                session.commit()

                self.__post_delete(kitmgr, node_dict)

                self._logger.info('Node [%s] deleted', node_dict['name'])

            # clean up add host session cache
            self._addHostManager.delete_sessions(
                set([
                    node.addHostSession for node in node_objs
                    if node.addHostSession
                ]))

        self.__scheduleUpdate()
コード例 #14
0
 def test_delete_none_sceamer_chance_default(self):
     session = Session(connection)
     webm = session.query(WEBM).get('q')
     assert webm
     session.delete(webm)
     session.commit()
     webm = session.query(WEBM).get('q')
     assert webm is None
コード例 #15
0
ファイル: Statification.py プロジェクト: ANSSI-FR/cornetto
 def delete_statification(session: Session, i_id: int) -> None:
     """
     Delete the statification that have the given id
     :param session: the database session
     :param i_id: the id of the statification to be deleted
     """
     session.delete(Statification.get_from_statification_id(session, i_id))
     session.commit()
コード例 #16
0
ファイル: query.py プロジェクト: badock/rome
 def delete(self, synchronize_session='evaluate'):
     from rome.core.session.session import Session
     temporary_session = Session()
     objects = self.matching_objects(filter_deleted=False)
     for obj in objects:
         temporary_session.delete(obj)
     temporary_session.flush()
     return len(objects)
コード例 #17
0
    def delete(self):
        if not self.id:
            session = Session()
        else:
            session = Session.object_session(self)

        session.delete(self)
        session.commit()
コード例 #18
0
ファイル: db_base.py プロジェクト: jeefberkey/mealie
    def delete(self, session: Session, primary_key_value) -> dict:
        result = session.query(
            self.sql_model).filter_by(**{
                self.primary_key: primary_key_value
            }).one()

        session.delete(result)
        session.commit()
コード例 #19
0
ファイル: db_helper.py プロジェクト: Fathui/fsqlfly
 def delete(cls, model: str, pk: int, *args, session: Session,
            base: Type[Base], **kwargs) -> DBRes:
     if settings.FSQLFLY_SAVE_MODE_DISABLE:
         obj = session.query(base).get(pk)
         session.delete(obj)
         return DBRes(data=obj.id)
     else:
         return DBRes.sever_error(
             'Not Support Delete when FSQLFLY_SAVE_MODE_DISABLE not set')
コード例 #20
0
def upgrade():
    session = Session(bind=op.get_bind())
    session.add(Exchange(name=BitkubExchange.name, is_active=True, weight=15))

    bx_in_th_name = "[bx.in.th](https://bx.in.th/ref/s9c3HU/)"
    bx_in_th = session.query(Exchange).filter_by(name=bx_in_th_name).one()
    session.query(Rate).filter_by(exchange_id=bx_in_th.id).delete()
    session.delete(bx_in_th)
    session.flush()
コード例 #21
0
def __update_submissions(model: Union[Type[File], Type[Message]],
                         remote_submissions: List[SDKSubmission],
                         local_submissions: Union[List[Message], List[File]],
                         session: Session, data_dir: str) -> None:
    """
    The logic for updating files and messages is effectively the same, so this function is somewhat
    overloaded to allow us to do both in a DRY way.

    * Existing submissions are updated in the local database.
    * New submissions have an entry created in the local database.
    * Local submissions not returned in the remote submissions are deleted
      from the local database.
    """
    local_uuids = {submission.uuid for submission in local_submissions}
    for submission in remote_submissions:
        if submission.uuid in local_uuids:
            local_submission = [
                s for s in local_submissions if s.uuid == submission.uuid
            ][0]

            # Update files on disk to match new filename.
            if (local_submission.filename != submission.filename):
                rename_file(data_dir, local_submission.filename,
                            submission.filename)

            # Update an existing record.
            local_submission.filename = submission.filename
            local_submission.size = submission.size
            local_submission.is_read = submission.is_read
            local_submission.download_url = submission.download_url

            # Removing the UUID from local_uuids ensures this record won't be
            # deleted at the end of this function.
            local_uuids.remove(submission.uuid)
            logger.debug('Updated submission {}'.format(submission.uuid))
        else:
            # A new submission to be added to the database.
            _, source_uuid = submission.source_url.rsplit('/', 1)
            source = session.query(Source).filter_by(uuid=source_uuid)[0]
            ns = model(source_id=source.id,
                       uuid=submission.uuid,
                       size=submission.size,
                       filename=submission.filename,
                       download_url=submission.download_url)
            session.add(ns)
            logger.debug('Added new submission {}'.format(submission.uuid))

    # The uuids remaining in local_uuids do not exist on the remote server, so
    # delete the related records.
    for deleted_submission in [
            s for s in local_submissions if s.uuid in local_uuids
    ]:
        delete_single_submission_or_reply_on_disk(deleted_submission, data_dir)
        session.delete(deleted_submission)
        logger.debug('Deleted submission {}'.format(deleted_submission.uuid))

    session.commit()
コード例 #22
0
def delete_local_source_by_uuid(session: Session, uuid: str, data_dir: str) -> None:
    """
    Delete the source with the referenced UUID.
    """
    source = session.query(Source).filter_by(uuid=uuid).one_or_none()
    if source:
        delete_source_collection(source.journalist_filename, data_dir)
        session.delete(source)
        session.commit()
        logger.info("Deleted source with UUID {} from local database.".format(uuid))
コード例 #23
0
def deleteGene(geneId):
    session = Session()
    try:
        gene = session.query(Gene).filter(Gene.id == geneId).one()
    except:
        print "gene does not exist in database"
        raise
    session.delete(gene)
    session.commit()
    session.close()
コード例 #24
0
def deleteModel(modelId):
    session = Session()
    try:
        model = session.query(Model).filter(Model.id == modelId).one()
    except:
        print "model does not exist in database"
        raise
    session.delete(model)
    session.commit()
    session.close()
コード例 #25
0
def test_get_all_notifications(db: Session) -> None:
    provider = utils.create_random_provider(db)
    document = utils.create_random_notification(provider)
    provider_notifications = crud_notification.get_all_notifications(str(provider.id))
    assert provider_notifications
    assert provider_notifications[0]['id'] == document.id
    assert provider_notifications[0]['recipient_id'] == str(provider.id)

    db.delete(provider)
    db.commit()
コード例 #26
0
def test_update_notification(db: Session) -> None:
    provider = utils.create_random_provider(db)
    notification = utils.create_random_notification(provider)
    crud_notification.update(notification.id)
    update_notification = Notification.objects(id=notification.id).first()
    assert update_notification

    db.delete(provider)
    db.commit()
    Notification.delete(notification)
コード例 #27
0
    def deleteSoftwareProfile(self, session: Session, name: str) -> None:
        """
        Delete softwareProfile from the db.

            Returns:
                None
            Throws:
                SoftwareProfileNotFound
                DbError
        """

        try:
            # This role of this lookup is twofold.  One, it validates
            # the existence of the software profile to be deleted and
            # second, gets the id for looking up any associated nodes
            dbSwProfile = self._softwareProfilesDbHandler.\
                getSoftwareProfile(session, name)

            if dbSwProfile.nodes:
                # The software profile cannot be removed while
                # associated
                # nodes exist
                raise TortugaException('Unable to delete software profile with'
                                       ' associated nodes')

            if dbSwProfile.isIdle:
                # Ensure this software profile is not associated with
                # a hardware profile

                if dbSwProfile.hwprofileswithidle:
                    # This software profile is associated with one
                    # or more hardware profiles
                    raise TortugaException(
                        'Unable to delete software profile.'
                        '  This software profile is associated'
                        ' with one or more hardware profiles: [%s]' %
                        (' '.join([
                            hwprofile.name
                            for hwprofile in dbSwProfile.hwprofileswithidle
                        ])))

            # Proceed with software profile deletion
            self.getLogger().debug(
                'Marking software profile [%s] for deletion' % (name))

            session.delete(dbSwProfile)

            session.commit()
        except TortugaException:
            session.rollback()
            raise
        except Exception as ex:
            session.rollback()
            self.getLogger().exception('%s' % ex)
            raise
コード例 #28
0
def convert_sdb(
    env: Environment,
    sess: Session,
    sdb: StoredDataBlockMetadata,
    conversion_path: ConversionPath,
    target_storage: Storage,
    storages: Optional[List[Storage]] = None,
) -> StoredDataBlockMetadata:
    if not conversion_path.conversions:
        return sdb
    if storages is None:
        storages = env.storages
    prev_sdb = sdb
    next_sdb: Optional[StoredDataBlockMetadata] = None
    prev_storage = sdb.storage
    next_storage: Optional[Storage] = None
    realized_schema = sdb.realized_schema(env, sess)
    for conversion_edge in conversion_path.conversions:
        conversion = conversion_edge.conversion
        target_storage_format = conversion.to_storage_format
        next_storage = select_storage(target_storage, storages,
                                      target_storage_format)
        logger.debug(
            f"CONVERSION: {conversion.from_storage_format} -> {conversion.to_storage_format}"
        )
        next_sdb = StoredDataBlockMetadata(  # type: ignore
            id=get_datablock_id(),
            data_block_id=prev_sdb.data_block_id,
            data_block=prev_sdb.data_block,
            data_format=target_storage_format.data_format,
            storage_url=next_storage.url,
        )
        sess.add(next_sdb)
        conversion_edge.copier.copy(
            from_name=prev_sdb.get_name(),
            to_name=next_sdb.get_name(),
            conversion=conversion,
            from_storage_api=prev_storage.get_api(),
            to_storage_api=next_storage.get_api(),
            schema=realized_schema,
        )
        if (prev_sdb.data_format.is_python_format()
                and not prev_sdb.data_format.is_storable()):
            # If the records obj is in python and not storable, and we just used it, then it can be reused
            # TODO: Bit of a hack. Is there a central place we can do this?
            #       also is reusable a better name than storable?
            prev_storage.get_api().remove(prev_sdb.get_name())
            prev_sdb.data_block.stored_data_blocks.remove(prev_sdb)
            if prev_sdb in sess.new:
                sess.expunge(prev_sdb)
            else:
                sess.delete(prev_sdb)
        prev_sdb = next_sdb
        prev_storage = next_storage
    return next_sdb
コード例 #29
0
ファイル: vmdatabase.py プロジェクト: bhuvan/devstack-gate
class VMDatabase(object):
    def __init__(self, path=os.path.expanduser("~/vm.db")):
        engine = create_engine('sqlite:///%s' % path, echo=False)
        metadata.create_all(engine)
        Session = sessionmaker(bind=engine, autoflush=True, autocommit=False)
        self.session = Session()

    def print_state(self):
        for provider in self.getProviders():
            print 'Provider:', provider.name
            for base_image in provider.base_images:
                print '  Base image:', base_image.name
                for snapshot_image in base_image.snapshot_images:
                    print '    Snapshot:', snapshot_image.name, \
                          snapshot_image.state
                for machine in base_image.machines:
                    print '    Machine:', machine.id, machine.name, \
                          machine.state, machine.state_time, machine.ip

    def abort(self):
        self.session.rollback()

    def commit(self):
        self.session.commit()

    def delete(self, obj):
        self.session.delete(obj)

    def getProviders(self):
        return self.session.query(Provider).all()

    def getProvider(self, name):
        return self.session.query(Provider).filter_by(name=name)[0]

    def getResult(self, id):
        return self.session.query(Result).filter_by(id=id)[0]

    def getMachine(self, id):
        return self.session.query(Machine).filter_by(id=id)[0]

    def getMachineByJenkinsName(self, name):
        return self.session.query(Machine).filter_by(jenkins_name=name)[0]

    def getMachineForUse(self, image_name):
        """Atomically find a machine that is ready for use, and update
        its state."""
        image = None
        for machine in self.session.query(Machine).filter(
            machine_table.c.state == READY).order_by(
            machine_table.c.state_time):
            if machine.base_image.name == image_name:
                machine.state = USED
                self.commit()
                return machine
        raise Exception("No machine found for image %s" % image_name)
コード例 #30
0
ファイル: vmdatabase.py プロジェクト: bhuvan/devstack-gate
class VMDatabase(object):
    def __init__(self, path=os.path.expanduser("~/vm.db")):
        engine = create_engine('sqlite:///%s' % path, echo=False)
        metadata.create_all(engine)
        Session = sessionmaker(bind=engine, autoflush=True, autocommit=False)
        self.session = Session()

    def print_state(self):
        for provider in self.getProviders():
            print 'Provider:', provider.name
            for base_image in provider.base_images:
                print '  Base image:', base_image.name
                for snapshot_image in base_image.snapshot_images:
                    print '    Snapshot:', snapshot_image.name, \
                          snapshot_image.state
                for machine in base_image.machines:
                    print '    Machine:', machine.id, machine.name, \
                          machine.state, machine.state_time, machine.ip

    def abort(self):
        self.session.rollback()

    def commit(self):
        self.session.commit()

    def delete(self, obj):
        self.session.delete(obj)

    def getProviders(self):
        return self.session.query(Provider).all()

    def getProvider(self, name):
        return self.session.query(Provider).filter_by(name=name)[0]

    def getResult(self, id):
        return self.session.query(Result).filter_by(id=id)[0]

    def getMachine(self, id):
        return self.session.query(Machine).filter_by(id=id)[0]

    def getMachineByJenkinsName(self, name):
        return self.session.query(Machine).filter_by(jenkins_name=name)[0]

    def getMachineForUse(self, image_name):
        """Atomically find a machine that is ready for use, and update
        its state."""
        image = None
        for machine in self.session.query(Machine).filter(
                machine_table.c.state == READY).order_by(
                    machine_table.c.state_time):
            if machine.base_image.name == image_name:
                machine.state = USED
                self.commit()
                return machine
        raise Exception("No machine found for image %s" % image_name)
コード例 #31
0
def process_delete_host_request(session: Session,
                                transaction_id: str,
                                nodespec: str,
                                force: bool = False):
    try:
        req = NodeRequestsDbHandler().get_by_addHostSession(
            session, transaction_id)
        if req is None:
            # Session was deleted prior to being processed. Nothing to do...

            logger.warning(
                'Delete host request [%s] not found; nothing to do...',
                transaction_id)

            return

        #
        # Save this data so that we have it for firing the event below
        #
        evt_req_id = req.id
        evt_req_request = {'name': nodespec}

        ahm.update_session(transaction_id, running=True)

        logger.debug('process_delete_host_request(): transaction_id=[{0}],'
                     ' nodespec=[{1}]'.format(transaction_id, nodespec))

        try:
            NodeApi().deleteNode(session, nodespec, force=force)

            ahm.delete_session(transaction_id)

            session.delete(req)

            DeleteNodeRequestComplete.fire(request_id=evt_req_id,
                                           request=evt_req_request)
        except (OperationFailed, NodeNotFound):
            ahm.delete_session(transaction_id)

            session.delete(req)

            raise
        except TortugaException as exc:
            logger.exception('Exception while deleting nodes')

            req.message = str(exc)

            req.state = 'error'

            req.last_update = datetime.datetime.utcnow()
        finally:
            ahm.update_session(transaction_id, running=False)
    finally:
        session.commit()
コード例 #32
0
async def delete_todo(id: int, db: Session, current_user: schemas.User):
    todo = db.query(models.TODO).filter(models.TODO.id == id).first()
    if not todo:
        detail = f"Todo {id} not found in db, thus cannot deleted"
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=detail)
    if user_right(todo.owner_id, current_user.id):
        detail = "Unauthorized to delete, kindly login into your Account"
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=detail)
    db.delete(todo)
    db.commit()
    return Response(status_code=status.HTTP_204_NO_CONTENT)
コード例 #33
0
def update_sources(remote_sources: List[SDKSource],
                   local_sources: List[Source], session: Session,
                   data_dir: str) -> None:
    """
    Given collections of remote sources, the current local sources and a
    session to the local database, ensure the state of the local database
    matches that of the remote sources:

    * Existing items are updated in the local database.
    * New items are created in the local database.
    * Local items not returned in the remote sources are deleted from the
      local database.
    """
    local_uuids = {source.uuid for source in local_sources}
    for source in remote_sources:
        if source.uuid in local_uuids:
            # Update an existing record.
            local_source = [s for s in local_sources
                            if s.uuid == source.uuid][0]
            local_source.journalist_designation = source.journalist_designation
            local_source.is_flagged = source.is_flagged
            local_source.public_key = source.key['public']
            local_source.interaction_count = source.interaction_count
            local_source.document_count = source.number_of_documents
            local_source.is_starred = source.is_starred
            local_source.last_updated = parse(source.last_updated)

            # Removing the UUID from local_uuids ensures this record won't be
            # deleted at the end of this function.
            local_uuids.remove(source.uuid)
            logger.debug('Updated source {}'.format(source.uuid))
        else:
            # A new source to be added to the database.
            ns = Source(uuid=source.uuid,
                        journalist_designation=source.journalist_designation,
                        is_flagged=source.is_flagged,
                        public_key=source.key['public'],
                        interaction_count=source.interaction_count,
                        is_starred=source.is_starred,
                        last_updated=parse(source.last_updated),
                        document_count=source.number_of_documents)
            session.add(ns)
            logger.debug('Added new source {}'.format(source.uuid))

    # The uuids remaining in local_uuids do not exist on the remote server, so
    # delete the related records.
    for deleted_source in [s for s in local_sources if s.uuid in local_uuids]:
        for document in deleted_source.collection:
            delete_single_submission_or_reply_on_disk(document, data_dir)

        session.delete(deleted_source)
        logger.debug('Deleted source {}'.format(deleted_source.uuid))

    session.commit()
コード例 #34
0
ファイル: manager.py プロジェクト: BtbN/esaupload
def delete_video(session: Session, vid_id: int):
    vid = session.query(Video).get(vid_id)

    if not vid:
        print("Video ID not found")
        return -1

    print("Deleting video %s: %s" % (vid.id, vid.title))

    session.delete(vid)
    session.commit()

    print("Done")

    return 0
コード例 #35
0
    def test_delete_existing_object(self):
        """
            Test the method _delete_object(self, obj) when the object exists
            Test scenario:
            When the data is in the database, the delete operation should
            complete successfully
        """

        self.aMox.StubOutWithMock(session, 'query')
        session.query(model.PowerVCMapping).AndReturn(query)

        self.aMox.StubOutWithMock(query, 'filter_by')
        query.filter_by(id=self.powerVCMapping['id']).AndReturn(query)

        self.aMox.StubOutWithMock(query, 'one')
        query.one().AndReturn(self.powerVCMapping)

        self.aMox.StubOutWithMock(session, 'begin')
        session.begin(subtransactions=True).AndReturn(transaction(None, None))

        self.aMox.StubOutWithMock(session, 'delete')
        returnValue = session.delete(self.powerVCMapping).AndReturn(True)

        self.aMox.ReplayAll()

        self.powervcagentdb._delete_object(self.powerVCMapping)

        self.aMox.VerifyAll()

        self.assertEqual(returnValue, True)

        self.aMox.UnsetStubs()
コード例 #36
0
ファイル: session.py プロジェクト: GEverding/inbox
class InboxSession(object):
    """ Inbox custom ORM (with SQLAlchemy compatible API).

    Parameters
    ----------
    engine : <sqlalchemy.engine.Engine>
        A configured database engine to use for this session
    versioned : bool
        Do you want to enable the transaction log?
    ignore_soft_deletes : bool
        Whether or not to ignore soft-deleted objects in query results.
    namespace_id : int
        Namespace to limit query results with.
    """
    def __init__(self, engine, versioned=True, ignore_soft_deletes=True,
                 namespace_id=None):
        # TODO: support limiting on namespaces
        assert engine, "Must set the database engine"

        args = dict(bind=engine, autoflush=True, autocommit=False)
        self.ignore_soft_deletes = ignore_soft_deletes
        if ignore_soft_deletes:
            args['query_cls'] = InboxQuery
        self._session = Session(**args)

        if versioned:
            from inbox.models.transaction import create_revisions

            @event.listens_for(self._session, 'after_flush')
            def after_flush(session, flush_context):
                """
                Hook to log revision snapshots. Must be post-flush in order to
                grab object IDs on new objects.
                """
                create_revisions(session)

    def query(self, *args, **kwargs):
        q = self._session.query(*args, **kwargs)
        if self.ignore_soft_deletes:
            return q.options(IgnoreSoftDeletesOption())
        else:
            return q

    def add(self, instance):
        if not self.ignore_soft_deletes or not instance.is_deleted:
            self._session.add(instance)
        else:
            raise Exception("Why are you adding a deleted object?")

    def add_all(self, instances):
        if True not in [i.is_deleted for i in instances] or \
                not self.ignore_soft_deletes:
            self._session.add_all(instances)
        else:
            raise Exception("Why are you adding a deleted object?")

    def delete(self, instance):
        if self.ignore_soft_deletes:
            instance.mark_deleted()
            # just to make sure
            self._session.add(instance)
        else:
            self._session.delete(instance)

    def begin(self):
        self._session.begin()

    def commit(self):
        self._session.commit()

    def rollback(self):
        self._session.rollback()

    def flush(self):
        self._session.flush()

    def close(self):
        self._session.close()

    def expunge(self, obj):
        self._session.expunge(obj)

    def merge(self, obj):
        return self._session.merge(obj)

    @property
    def no_autoflush(self):
        return self._session.no_autoflush
コード例 #37
0
ファイル: session.py プロジェクト: helixyte/everest
 def __remove(self, entity, path): # pylint: disable=W0613
     if len(path) == 0:
         SaSession.delete(self, entity)
コード例 #38
0
ファイル: session.py プロジェクト: helixyte/everest
 def remove(self, entity_class, data):
     if not IEntity.providedBy(data): # pylint: disable=E1101
         self.__run_traversal(entity_class, None, data,
                              RELATION_OPERATIONS.REMOVE)
     else:
         SaSession.delete(self, data)
コード例 #39
0
ファイル: session.py プロジェクト: BigData-Tools/everest
 def delete(self, entity):
     self.begin()
     SaSession.delete(self, entity)
     self.commit()
コード例 #40
0
session.expire_all() 


################################################################################
#                             manage relationship
#     emit a query and load relationship objects during the first qualification
# if no eager load is applied.
################################################################################

# load ########################################################
# may emit a quer
addresses = user.addresses

# eager load through left join
session.query(User).options('addresses')

# eager load with join. cause the inner join followed by left outer join.
session.query(User).options(joinedload('addresses')).join(User.addresses)


# delete ######################################################
# delete from secondary table automatically
user.addresses.remove(addresses[0])

# whether delete from secondary table depends on the configuration
session.delete(addresses[0])
 



コード例 #41
0
ファイル: db_1_1_0.py プロジェクト: adamscieszko/rhodecode
 def delete(cls, id_):
     obj = cls.query().get(id_)
     Session.delete(obj)
     Session.commit()