def test_fixture_reset(self): # because this sets up reasonable db connection strings self.useFixture(fixtures.ConfFixture()) db_fixture = fixtures.Database() self.useFixture(db_fixture) engine = main_db_api.get_engine() conn = engine.connect() result = conn.execute(sa.text("SELECT * FROM instance_types")) rows = result.fetchall() self.assertEqual(0, len(rows), "Rows %s" % rows) # insert a 6th instance type, column 5 below is an int id # which has a constraint on it, so if new standard instance # types are added you have to bump it. conn.execute(sa.text( "INSERT INTO instance_types VALUES " "(NULL, NULL, NULL, 't1.test', 6, 4096, 2, 0, NULL, '87'" ", 1.0, 40, 0, 0, 1, 0)" )) result = conn.execute(sa.text("SELECT * FROM instance_types")) rows = result.fetchall() self.assertEqual(1, len(rows), "Rows %s" % rows) # NOTE(sdague): it's important to reestablish the db # connection because otherwise we have a reference to the old # in mem db. db_fixture.reset() engine = main_db_api.get_engine() conn = engine.connect() result = conn.execute(sa.text("SELECT * FROM instance_types")) rows = result.fetchall() self.assertEqual(0, len(rows), "Rows %s" % rows)
def _count_compute_nodes(self, context=None): """Returns the number of compute nodes in the cell database.""" # NOTE(mriedem): This does not filter based on the service status # because a disabled nova-compute service could still be reporting # inventory info to the placement service. There could be an outside # chance that there are compute node records in the database for # disabled nova-compute services that aren't yet upgraded to Ocata or # the nova-compute service was deleted and the service isn't actually # running on the compute host but the operator hasn't cleaned up the # compute_nodes entry in the database yet. We consider those edge cases # here and the worst case scenario is we give a warning that there are # more compute nodes than resource providers. We can tighten this up # later if needed, for example by not including compute nodes that # don't have a corresponding nova-compute service in the services # table, or by only counting compute nodes with a service version of at # least 15 which was the highest service version when Newton was # released. meta = sa.MetaData() engine = main_db_api.get_engine(context=context) compute_nodes = sa.Table('compute_nodes', meta, autoload_with=engine) with engine.connect() as conn: return conn.execute( sa.select(sqlfunc.count()).select_from(compute_nodes).where( compute_nodes.c.deleted == 0 ) ).scalars().first()
def _get_table_counts(self): engine = db.get_engine() conn = engine.connect() meta = sa.MetaData(engine) meta.reflect() shadow_tables = db._purgeable_tables(meta) results = {} for table in shadow_tables: r = conn.execute(sa.select( func.count()).select_from(table)).fetchone() results[table.name] = r[0] return results
def test_fixture_cleanup(self): # because this sets up reasonable db connection strings self.useFixture(fixtures.ConfFixture()) fix = fixtures.Database() self.useFixture(fix) # manually do the cleanup that addCleanup will do fix.cleanup() # ensure the db contains nothing engine = main_db_api.get_engine() conn = engine.connect() schema = "".join(line for line in conn.connection.iterdump()) self.assertEqual(schema, "BEGIN TRANSACTION;COMMIT;")
def enforce_fk_constraints(self, engine=None): if engine is None: engine = db_api.get_engine() dialect = engine.url.get_dialect() if dialect == sqlite.dialect: engine.connect().execute("PRAGMA foreign_keys = ON")
def _get_engine(database='main', context=None): if database == 'main': return main_db_api.get_engine(context=context) if database == 'api': return api_db_api.get_engine()