Example #1
0
    def run_fn(f):
        """Run the function f and return a representation of its db."""

        # We can't just do db.drop_all(), since db's metadata won't
        # necessarily reflect the contents of the database. If there are
        # tables it doesn't know about, it may raise an exception.
        with app.app_context():
            drop_tables()
            f()

        return get_db_state()
Example #2
0
    def run_fn(f):
        """Run the function f and return a representation of its db."""

        # We can't just do db.drop_all(), since db's metadata won't
        # necessarily reflect the contents of the database. If there are
        # tables it doesn't know about, it may raise an exception.
        with app.app_context():
            drop_tables()
            f()

        return get_db_state()
Example #3
0
def create_db():
    """Create and populate the initial database.

    The database connection must have been previously initialzed via `haas.model.init_db`.
    """
    with app.app_context():
        db.create_all()
        for head in paths.keys():
            # Record the version of each branch. Each extension which uses the
            # database will have its own branch.
            stamp(revision=head)
        get_network_allocator().populate()
        db.session.commit()
Example #4
0
def load_dump(filename):
    """Load a database dump and upgrades it to the latest schema.

    `filename` is path to the SQL dump to load, relative to `pg_dump_dir`.

    The SQL in that file will be executed, and then migration scripts will
    be run to bring it up to date.
    """
    with open(path.join(pg_dump_dir, filename)) as f:
        sql = f.read()
    with app.app_context():
        db.session.execute(sql)
        db.session.commit()
    upgrade(revision='heads')
Example #5
0
def load_dump(filename):
    """Load a database dump and upgrades it to the latest schema.

    `filename` is path to the SQL dump to load, relative to `pg_dump_dir`.

    The SQL in that file will be executed, and then migration scripts will
    be run to bring it up to date.
    """
    with open(path.join(pg_dump_dir, filename)) as f:
        sql = f.read()
    with app.app_context():
        db.session.execute(sql)
        db.session.commit()
    upgrade(revision='heads')
Example #6
0
def create_db():
    """Create and populate the initial database.

    The database connection must have been previously initialzed via
    `haas.model.init_db`.
    """
    with app.app_context():
        db.create_all()
        for head in _expected_heads():
            # Record the version of each branch. Each extension which uses the
            # database will have its own branch.
            db.session.execute(
                AlembicVersion.insert().values(version_num=head))
        get_network_allocator().populate()
        db.session.commit()
Example #7
0
def test_populate_dirty_db():
    """running the allocator's populate() on an existing db should be ok.

    This includes the case where modifications have been made to the vlans
    in the database.

    Note that we only check that this doesn't raise an exception.
    """
    # The fresh_database fixture will have created a clean database for us. We
    # just tweak it and then re-run create_db
    with app.app_context():
        # flag vlan 100 as in-use, just so the db isn't quite pristine.
        vlan100 = Vlan.query.filter_by(vlan_no=100)
        vlan100.available = False
        db.session.commit()
    # Okay, now try re-initializing:
    create_db()
Example #8
0
def initial_db(request):
    fresh_database(request)
    with app.app_context():
        alice = User(label='alice',
                     password='******',
                     is_admin=True)
        bob = User(label='bob',
                   password='******',
                   is_admin=False)

        db.session.add(alice)
        db.session.add(bob)

        runway = model.Project('runway')
        runway.users.append(alice)
        db.session.add(runway)
        db.session.commit()
Example #9
0
def initial_db(request):
    fresh_database(request)
    with app.app_context():
        alice = User(label='alice',
                    password='******',
                    is_admin=True)
        bob = User(label='bob',
                password='******',
                is_admin=False)

        db.session.add(alice)
        db.session.add(bob)

        runway = model.Project('runway')
        runway.users.append(alice)
        db.session.add(runway)
        db.session.commit()
Example #10
0
def test_populate_dirty_db():
    """running the allocator's populate() on an existing db should be ok.

    This includes the case where modifications have been made to the vlans
    in the database.

    Note that we only check that this doesn't raise an exception.
    """
    # The fresh_database fixture will have created a clean database for us. We
    # just tweak it and then re-run create_db
    with app.app_context():
        # flag vlan 100 as in-use, just so the db isn't quite pristine.
        vlan100 = Vlan.query.filter_by(vlan_no=100)
        vlan100.available = False
        db.session.commit()
    # Okay, now try re-initializing:
    create_db()