示例#1
0
    def update(item, **kwargs):
        """Update an existing %s item in the database.

        kwargs contains: no_commit

        If no_commit is present and True, no commit will be performed. It is
        assumed this is handled elsewhere.

        """ % str(obj)
        no_commit = False
        if "no_commit" in kwargs:
            no_commit = True
            kwargs.pop("no_commit")

        s = session()
        # TODO: check for instance, re-add to session?
        query = s.query(obj)
        key = getattr(item, id_attr, item)
        query = query.filter_by(**{id_attr: key})
        if not query.count():
            raise DBUpdateError("The %s '%s' was not found!" % (obj, item))
        db_item = query.first()
        [setattr(db_item, k, v) for k, v in kwargs.items()]

        if not no_commit:
            s.commit()
示例#2
0
def test_backup_and_restore_sqlite():
    logging.basicConfig(level=logging.DEBUG)
    backup_dir = tempfile.mkdtemp()
    try:
        dbfile = os.path.join(backup_dir, "test.db")
        dbsetup.setup(modules=[backup_test_db])
        dbsetup.init('sqlite:///' + dbfile)
        dbsetup.create()

        s = session()
        s.add(backup_test_db.TestTable(id="1", foo="bar"))
        transaction.commit()

        rows = s.query(backup_test_db.TestTable).filter(backup_test_db.TestTable.foo == "bar").all()
        assert rows

        backup.dump_database(s, backup_dir)

        now = datetime.datetime.now()
        dump_file = os.path.join(backup_dir, "test.db.dump.{:%Y%m%d-%H%M}.gz".format(now))

        assert os.path.isfile(dump_file)

        dbsetup.destroy()
        dbsetup.create()
        rows = s.query(backup_test_db.TestTable).all()
        assert not rows

        backup.load_database(s, dbsetup.Base.metadata, dump_file)

        rows = s.query(backup_test_db.TestTable).filter(backup_test_db.TestTable.foo == "bar").all()
        assert rows

    finally:
        shutil.rmtree(backup_dir)
示例#3
0
 def has(item):
     s = session()
     query = s.query(obj)
     key = getattr(item, id_attr, item)
     query = query.filter_by(**{id_attr: key})
     if query.count():
         return True
     return False
示例#4
0
    def get(item):
        """Recover and exiting %s item from the DB.

        """ % str(obj)
        s = session()
        query = s.query(obj)
        key = getattr(item, id_attr, item)
        query = query.filter_by(**{id_attr: key})
        if not query.count():
            raise DBGetError("The %s '%s' was not found!" % (obj, item))
        return query.first()
示例#5
0
    def setUp(self):
        """Set up the schema clean ready to load test data into for each test.
        """
        # This needs to be autogenerate or manage not to interfere with other
        # test runs that may occur simultaneously.
        #
        #dbsetup.init("sqlite:///testdata.db")
        #
        # Stick with in memory for the moment:
        dbsetup.init("sqlite:///:memory:", use_transaction=False)

        dbsetup.create()

        # Used so I can manipulate object returned from api,
        # binding them to my session. Otherwise the internal
        # session used is closed, and normally this would be
        # ok.
        self.session = session()
示例#6
0
    def setUp(self):
        """Set up the schema clean ready to load test data into for each test.
        """
        # This needs to be autogenerate or manage not to interfere with other
        # test runs that may occur simultaneously.
        #
        #dbsetup.init("sqlite:///testdata.db")
        #
        # Stick with in memory for the moment:
        dbsetup.init("sqlite:///:memory:", use_transaction=False)

        dbsetup.create()

        # Used so I can manipulate object returned from api,
        # binding them to my session. Otherwise the internal
        # session used is closed, and normally this would be
        # ok.
        self.session = session()
示例#7
0
def create():
    """Called to do the table schema creation.
    """
    get_log().info("create: begin.")

    from pp.db import session

    user_dict = dict(
        username="******",
        display_name=u'Andrés Plácido Bolívar',
        email=u'andrés.bolí[email protected]',
        phone="123",
        password="******",
    )

    s = session()
    admin_user = s.add(UserTable(**user_dict))

    # Call any custom creating hooks here
    get_log().info("create: Initial admin user <%s> created OK." % admin_user)
示例#8
0
    def remove(item, no_commit=False):
        """Remove an %s item from the database.

        :param no_commit: True | False

        If no_commit is present and True, no commit will be performed. It is
        assumed this is handled elsewhere.

        """ % str(obj)
        s = session()
        key = getattr(item, id_attr, item)
        query = s.query(obj)
        query = query.filter_by(**{id_attr: key})
        if not query.count():
            raise DBRemoveError("The %s '%s' was not found!" % (obj, item))

        db_item = query.first()
        s.delete(db_item)

        if not no_commit:
            s.commit()
示例#9
0
def test_backup_and_restore_sqlite():
    logging.basicConfig(level=logging.DEBUG)
    backup_dir = tempfile.mkdtemp()
    try:
        dbfile = os.path.join(backup_dir, "test.db")
        dbsetup.setup(modules=[backup_test_db])
        dbsetup.init('sqlite:///' + dbfile)
        dbsetup.create()

        s = session()
        s.add(backup_test_db.TestTable(id="1", foo="bar"))
        transaction.commit()

        rows = s.query(backup_test_db.TestTable).filter(
            backup_test_db.TestTable.foo == "bar").all()
        assert rows

        backup.dump_database(s, backup_dir)

        now = datetime.datetime.now()
        dump_file = os.path.join(backup_dir,
                                 "test.db.dump.{:%Y%m%d-%H%M}.gz".format(now))

        assert os.path.isfile(dump_file)

        dbsetup.destroy()
        dbsetup.create()
        rows = s.query(backup_test_db.TestTable).all()
        assert not rows

        backup.load_database(s, dbsetup.Base.metadata, dump_file)

        rows = s.query(backup_test_db.TestTable).filter(
            backup_test_db.TestTable.foo == "bar").all()
        assert rows

    finally:
        shutil.rmtree(backup_dir)
示例#10
0
    def add(**kwargs):
        """Add a new %s item to the database.

        kwargs contains: no_commit

        If no_commit is present and True, no commit will be performed. It is
        assumed this is handled elsewhere.

        """ % str(obj)
        no_commit = False
        if "no_commit" in kwargs:
            no_commit = True
            kwargs.pop("no_commit")

        s = session()
        item = obj(**kwargs)
        [setattr(item, k, v) for k, v in kwargs.items()]
        s.add(item)

        if not no_commit:
            s.commit()

        return item
示例#11
0
 def find(**kwargs):
     """Filter for %s by keyword arguments.""" % obj
     s = session()
     query = s.query(obj)
     query = query.filter_by(**kwargs)
     return query.all()
示例#12
0
def count():
    """Return the number of users on the system."""
    s = session()
    query = s.query(UserTable)
    return query.count()