Python sqlalchemy.engine() Examples

The following are code examples for showing how to use sqlalchemy.engine(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 6 votes vote down vote up
def _check_002(self, engine, data):
        for column in ['created_at', 'updated_at', 'id', 'instance_uuid',
                'cell_id', 'project_id']:
            self.assertColumnExists(engine, 'instance_mappings', column)

        for index in ['instance_uuid_idx', 'project_id_idx']:
            self.assertIndexExists(engine, 'instance_mappings', index)

        self.assertUniqueConstraintExists(engine, 'instance_mappings',
                ['instance_uuid'])

        inspector = reflection.Inspector.from_engine(engine)
        # There should only be one foreign key here
        fk = inspector.get_foreign_keys('instance_mappings')[0]
        self.assertEqual('cell_mappings', fk['referred_table'])
        self.assertEqual(['id'], fk['referred_columns'])
        self.assertEqual(['cell_id'], fk['constrained_columns']) 
Example 2
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 6 votes vote down vote up
def _check_006(self, engine, data):
        for column in ['id', 'request_spec_id', 'project_id', 'user_id',
                'display_name', 'instance_metadata', 'progress', 'vm_state',
                'image_ref', 'access_ip_v4', 'access_ip_v6', 'info_cache',
                'security_groups', 'config_drive', 'key_name', 'locked_by']:
            self.assertColumnExists(engine, 'build_requests', column)

        self.assertIndexExists(engine, 'build_requests',
            'build_requests_project_id_idx')
        self.assertUniqueConstraintExists(engine, 'build_requests',
                ['request_spec_id'])

        inspector = reflection.Inspector.from_engine(engine)
        # There should only be one foreign key here
        fk = inspector.get_foreign_keys('build_requests')[0]
        self.assertEqual('request_specs', fk['referred_table'])
        self.assertEqual(['id'], fk['referred_columns'])
        self.assertEqual(['request_spec_id'], fk['constrained_columns']) 
Example 3
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 6 votes vote down vote up
def _check_275(self, engine, data):
        self.assertColumnExists(engine, 'key_pairs', 'type')
        self.assertColumnExists(engine, 'shadow_key_pairs', 'type')

        key_pairs = oslodbutils.get_table(engine, 'key_pairs')
        shadow_key_pairs = oslodbutils.get_table(engine, 'shadow_key_pairs')
        self.assertIsInstance(key_pairs.c.type.type,
                              sqlalchemy.types.String)
        self.assertIsInstance(shadow_key_pairs.c.type.type,
                              sqlalchemy.types.String)

        # Make sure the keypair entry will have the type 'ssh'
        key_pairs = oslodbutils.get_table(engine, 'key_pairs')
        keypair = key_pairs.select(
            key_pairs.c.name == 'test-migr').execute().first()
        self.assertEqual('ssh', keypair.type) 
Example 4
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 6 votes vote down vote up
def _check_313(self, engine, data):

        self.assertColumnExists(engine, 'pci_devices', 'parent_addr')
        self.assertColumnExists(engine, 'shadow_pci_devices', 'parent_addr')
        pci_devices = oslodbutils.get_table(engine, 'pci_devices')
        shadow_pci_devices = oslodbutils.get_table(
            engine, 'shadow_pci_devices')
        self.assertIsInstance(pci_devices.c.parent_addr.type,
                              sqlalchemy.types.String)
        self.assertTrue(pci_devices.c.parent_addr.nullable)
        self.assertIsInstance(shadow_pci_devices.c.parent_addr.type,
                              sqlalchemy.types.String)
        self.assertTrue(shadow_pci_devices.c.parent_addr.nullable)
        self.assertIndexMembers(engine, 'pci_devices',
                        'ix_pci_devices_compute_node_id_parent_addr_deleted',
                        ['compute_node_id', 'parent_addr', 'deleted']) 
Example 5
Project: gnocchi   Author: gnocchixyz   File: sqlalchemy.py    Apache License 2.0 6 votes vote down vote up
def build_filter(cls, engine, table, tree):
        try:
            operator, nodes = list(tree.items())[0]
        except Exception:
            raise indexer.QueryError()

        try:
            op = cls.multiple_operators[operator]
        except KeyError:
            try:
                op = cls.binary_operators[operator]
            except KeyError:
                try:
                    op = cls.unary_operators[operator]
                except KeyError:
                    raise indexer.QueryInvalidOperator(operator)
                return cls._handle_unary_op(engine, table, op, nodes)
            return cls._handle_binary_op(engine, table, op, nodes)
        return cls._handle_multiple_op(engine, table, op, nodes) 
Example 6
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: test_sql.py    MIT License 6 votes vote down vote up
def setup_method(self, load_iris_data):
        super().load_test_data_and_sql()
        engine = self.conn
        conn = engine.connect()
        self.__tx = conn.begin()
        self.pandasSQL = sql.SQLDatabase(conn)
        self.__engine = engine
        self.conn = conn

        yield

        self.__tx.rollback()
        self.conn.close()
        self.conn = self.__engine
        self.pandasSQL = sql.SQLDatabase(self.__engine)
        # XXX:
        # super().teardown_method(method) 
Example 7
Project: recruit   Author: Frank-qlu   File: test_sql.py    Apache License 2.0 6 votes vote down vote up
def setup_method(self, load_iris_data):
        super(_EngineToConnMixin, self).load_test_data_and_sql()
        engine = self.conn
        conn = engine.connect()
        self.__tx = conn.begin()
        self.pandasSQL = sql.SQLDatabase(conn)
        self.__engine = engine
        self.conn = conn

        yield

        self.__tx.rollback()
        self.conn.close()
        self.conn = self.__engine
        self.pandasSQL = sql.SQLDatabase(self.__engine)
        # XXX:
        # super(_EngineToConnMixin, self).teardown_method(method) 
Example 8
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def db_sync(self, engine):
        with mock.patch.object(sa_migration, 'get_engine',
                               return_value=engine):
            sa_migration.db_sync(database='api') 
Example 9
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertColumnExists(self, engine, table_name, column):
        self.assertTrue(db_utils.column_exists(engine, table_name, column),
                'Column %s.%s does not exist' % (table_name, column)) 
Example 10
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertIndexExists(self, engine, table_name, index):
        self.assertTrue(db_utils.index_exists(engine, table_name, index),
                        'Index %s on table %s does not exist' %
                        (index, table_name)) 
Example 11
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertUniqueConstraintExists(self, engine, table_name, columns):
        inspector = reflection.Inspector.from_engine(engine)
        constrs = inspector.get_unique_constraints(table_name)
        constr_columns = [constr['column_names'] for constr in constrs]
        self.assertIn(columns, constr_columns) 
Example 12
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertTableNotExists(self, engine, table_name):
        self.assertRaises(sqlalchemy.exc.NoSuchTableError,
                db_utils.get_table, engine, table_name) 
Example 13
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_003(self, engine, data):
        for column in ['created_at', 'updated_at', 'id',
                'cell_id', 'host']:
            self.assertColumnExists(engine, 'host_mappings', column)

        self.assertIndexExists(engine, 'host_mappings', 'host_idx')
        self.assertUniqueConstraintExists(engine, 'host_mappings',
                ['host'])

        inspector = reflection.Inspector.from_engine(engine)
        # There should only be one foreign key here
        fk = inspector.get_foreign_keys('host_mappings')[0]
        self.assertEqual('cell_mappings', fk['referred_table'])
        self.assertEqual(['id'], fk['referred_columns'])
        self.assertEqual(['cell_id'], fk['constrained_columns']) 
Example 14
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_004(self, engine, data):
        columns = ['created_at', 'updated_at', 'id', 'instance_uuid', 'spec']
        for column in columns:
            self.assertColumnExists(engine, 'request_specs', column)

        self.assertUniqueConstraintExists(engine, 'request_specs',
                ['instance_uuid'])
        if engine.name != 'ibm_db_sa':
            self.assertIndexExists(engine, 'request_specs',
                    'request_spec_instance_uuid_idx') 
Example 15
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertColumnExists(self, engine, table_name, column):
        self.assertTrue(oslodbutils.column_exists(engine, table_name, column),
                        'Column %s.%s does not exist' % (table_name, column)) 
Example 16
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertColumnNotExists(self, engine, table_name, column):
        self.assertFalse(oslodbutils.column_exists(engine, table_name, column),
                        'Column %s.%s should not exist' % (table_name, column)) 
Example 17
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertTableNotExists(self, engine, table):
        self.assertRaises(sqlalchemy.exc.NoSuchTableError,
                          oslodbutils.get_table, engine, table) 
Example 18
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertIndexExists(self, engine, table_name, index):
        self.assertTrue(oslodbutils.index_exists(engine, table_name, index),
                        'Index %s on table %s does not exist' %
                        (index, table_name)) 
Example 19
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertIndexNotExists(self, engine, table_name, index):
        self.assertFalse(oslodbutils.index_exists(engine, table_name, index),
                         'Index %s on table %s should not exist' %
                         (index, table_name)) 
Example 20
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def db_sync(self, engine):
        with mock.patch.object(sa_migration, 'get_engine',
                               return_value=engine):
            sa_migration.db_sync() 
Example 21
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_227(self, engine, data):
        table = oslodbutils.get_table(engine, 'project_user_quotas')

        # Insert fake_quotas with the longest resource name.
        fake_quotas = {'id': 5,
                       'project_id': 'fake_project',
                       'user_id': 'fake_user',
                       'resource': 'injected_file_content_bytes',
                       'hard_limit': 10}
        table.insert().execute(fake_quotas)

        # Check we can get the longest resource name.
        quota = table.select(table.c.id == 5).execute().first()
        self.assertEqual(quota['resource'], 'injected_file_content_bytes') 
Example 22
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_228(self, engine, data):
        self.assertColumnExists(engine, 'compute_nodes', 'metrics')

        compute_nodes = oslodbutils.get_table(engine, 'compute_nodes')
        self.assertIsInstance(compute_nodes.c.metrics.type,
                              sqlalchemy.types.Text) 
Example 23
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_229(self, engine, data):
        self.assertColumnExists(engine, 'compute_nodes', 'extra_resources')

        compute_nodes = oslodbutils.get_table(engine, 'compute_nodes')
        self.assertIsInstance(compute_nodes.c.extra_resources.type,
                              sqlalchemy.types.Text) 
Example 24
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_230(self, engine, data):
        for table_name in ['instance_actions_events',
                           'shadow_instance_actions_events']:
            self.assertColumnExists(engine, table_name, 'host')
            self.assertColumnExists(engine, table_name, 'details')

        action_events = oslodbutils.get_table(engine,
                                              'instance_actions_events')
        self.assertIsInstance(action_events.c.host.type,
                              sqlalchemy.types.String)
        self.assertIsInstance(action_events.c.details.type,
                              sqlalchemy.types.Text) 
Example 25
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_232(self, engine, data):
        table_names = ['compute_node_stats', 'compute_nodes',
                       'instance_actions', 'instance_actions_events',
                       'instance_faults', 'migrations']
        for table_name in table_names:
            self.assertTableNotExists(engine, 'dump_' + table_name) 
Example 26
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_233(self, engine, data):
        self.assertColumnExists(engine, 'compute_nodes', 'stats')

        compute_nodes = oslodbutils.get_table(engine, 'compute_nodes')
        self.assertIsInstance(compute_nodes.c.stats.type,
                              sqlalchemy.types.Text)

        self.assertRaises(sqlalchemy.exc.NoSuchTableError,
                          oslodbutils.get_table, engine, 'compute_node_stats') 
Example 27
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_234(self, engine, data):
        self.assertIndexMembers(engine, 'reservations',
                                'reservations_deleted_expire_idx',
                                ['deleted', 'expire']) 
Example 28
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_244(self, engine, data):
        volume_usage_cache = oslodbutils.get_table(
            engine, 'volume_usage_cache')
        self.assertEqual(64, volume_usage_cache.c.user_id.type.length) 
Example 29
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _pre_upgrade_245(self, engine):
        # create a fake network
        networks = oslodbutils.get_table(engine, 'networks')
        fake_network = {'id': 1}
        networks.insert().execute(fake_network) 
Example 30
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_246(self, engine, data):
        pci_devices = oslodbutils.get_table(engine, 'pci_devices')
        self.assertEqual(1, len([fk for fk in pci_devices.foreign_keys
                                 if fk.parent.name == 'compute_node_id'])) 
Example 31
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_247(self, engine, data):
        quota_usages = oslodbutils.get_table(engine, 'quota_usages')
        self.assertFalse(quota_usages.c.resource.nullable)

        pci_devices = oslodbutils.get_table(engine, 'pci_devices')
        if engine.name == 'ibm_db_sa':
            self.assertFalse(pci_devices.c.deleted.nullable)
        else:
            self.assertTrue(pci_devices.c.deleted.nullable)
        self.assertFalse(pci_devices.c.product_id.nullable)
        self.assertFalse(pci_devices.c.vendor_id.nullable)
        self.assertFalse(pci_devices.c.dev_type.nullable) 
Example 32
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_248(self, engine, data):
        self.assertIndexMembers(engine, 'reservations',
                                'reservations_deleted_expire_idx',
                                ['deleted', 'expire']) 
Example 33
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_249(self, engine, data):
        # Assert that only one index exists that covers columns
        # instance_uuid and device_name
        bdm = oslodbutils.get_table(engine, 'block_device_mapping')
        self.assertEqual(1, len([i for i in bdm.indexes
                                 if [c.name for c in i.columns] ==
                                    ['instance_uuid', 'device_name']])) 
Example 34
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_250(self, engine, data):
        self.assertTableNotExists(engine, 'instance_group_metadata')
        self.assertTableNotExists(engine, 'shadow_instance_group_metadata') 
Example 35
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_252(self, engine, data):
        oslodbutils.get_table(engine, 'instance_extra')
        oslodbutils.get_table(engine, 'shadow_instance_extra')
        self.assertIndexMembers(engine, 'instance_extra',
                                'instance_extra_idx',
                                ['instance_uuid']) 
Example 36
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_253(self, engine, data):
        self.assertColumnExists(engine, 'instance_extra', 'pci_requests')
        self.assertColumnExists(
            engine, 'shadow_instance_extra', 'pci_requests')
        instance_extra = oslodbutils.get_table(engine, 'instance_extra')
        shadow_instance_extra = oslodbutils.get_table(engine,
                                                      'shadow_instance_extra')
        self.assertIsInstance(instance_extra.c.pci_requests.type,
                              sqlalchemy.types.Text)
        self.assertIsInstance(shadow_instance_extra.c.pci_requests.type,
                              sqlalchemy.types.Text) 
Example 37
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_254(self, engine, data):
        self.assertColumnExists(engine, 'pci_devices', 'request_id')
        self.assertColumnExists(
            engine, 'shadow_pci_devices', 'request_id')

        pci_devices = oslodbutils.get_table(engine, 'pci_devices')
        shadow_pci_devices = oslodbutils.get_table(
            engine, 'shadow_pci_devices')
        self.assertIsInstance(pci_devices.c.request_id.type,
                              sqlalchemy.types.String)
        self.assertIsInstance(shadow_pci_devices.c.request_id.type,
                              sqlalchemy.types.String) 
Example 38
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_265(self, engine, data):
        # Assert that only one index exists that covers columns
        # host and deleted
        instances = oslodbutils.get_table(engine, 'instances')
        self.assertEqual(1, len([i for i in instances.indexes
                                 if [c.name for c in i.columns][:2] ==
                                    ['host', 'deleted']]))
        # and only one index covers host column
        iscsi_targets = oslodbutils.get_table(engine, 'iscsi_targets')
        self.assertEqual(1, len([i for i in iscsi_targets.indexes
                                 if [c.name for c in i.columns][:1] ==
                                    ['host']])) 
Example 39
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_266(self, engine, data):
        self.assertColumnExists(engine, 'tags', 'resource_id')
        self.assertColumnExists(engine, 'tags', 'tag')

        table = oslodbutils.get_table(engine, 'tags')

        self.assertIsInstance(table.c.resource_id.type,
                              sqlalchemy.types.String)
        self.assertIsInstance(table.c.tag.type,
                              sqlalchemy.types.String) 
Example 40
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_267(self, engine, data):
        # Make sure the column is non-nullable and the UC exists.
        fixed_ips = oslodbutils.get_table(engine, 'fixed_ips')
        self.assertTrue(fixed_ips.c.instance_uuid.nullable)
        fixed_ip = fixed_ips.select(fixed_ips.c.id == 1).execute().first()
        self.assertIsNone(fixed_ip.instance_uuid)

        instances = oslodbutils.get_table(engine, 'instances')
        self.assertFalse(instances.c.uuid.nullable)

        inspector = reflection.Inspector.from_engine(engine)
        constraints = inspector.get_unique_constraints('instances')
        constraint_names = [constraint['name'] for constraint in constraints]
        self.assertIn('uniq_instances0uuid', constraint_names)

        # Make sure the instances record with the valid uuid is still there.
        instance = instances.select(instances.c.id == 1).execute().first()
        self.assertIsNotNone(instance)

        # Check that the null entry in the volumes table is still there since
        # we skipped tables that don't have FK's back to the instances table.
        volumes = oslodbutils.get_table(engine, 'volumes')
        self.assertTrue(volumes.c.instance_uuid.nullable)
        volume = fixed_ips.select(
            volumes.c.id == '9c3c317e-24db-4d57-9a6f-96e6d477c1da'
        ).execute().first()
        self.assertIsNone(volume.instance_uuid) 
Example 41
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def test_migration_267(self):
        # This is separate from test_walk_versions so we can test the case
        # where there are non-null instance_uuid entries in the database which
        # cause the 267 migration to fail.
        engine = self.migrate_engine
        self.migration_api.version_control(
            engine, self.REPOSITORY, self.INIT_VERSION)
        self.migration_api.upgrade(engine, self.REPOSITORY, 266)
        # Create a consoles record with a null instance_uuid so
        # we can test that the upgrade fails if that entry is found.
        # NOTE(mriedem): We use the consoles table since that's the only table
        # created in the 216 migration with a ForeignKey created on the
        # instance_uuid table for sqlite.
        consoles = oslodbutils.get_table(engine, 'consoles')
        fake_console = {'id': 1}
        consoles.insert().execute(fake_console)

        # NOTE(mriedem): We handle the 267 migration where we expect to
        # hit a ValidationError on the consoles table to have
        # a null instance_uuid entry
        ex = self.assertRaises(exception.ValidationError,
                               self.migration_api.upgrade,
                               engine, self.REPOSITORY, 267)

        self.assertIn("There are 1 records in the "
                      "'consoles' table where the uuid or "
                      "instance_uuid column is NULL.",
                      ex.kwargs['detail'])

        # Remove the consoles entry with the null instance_uuid column.
        rows = consoles.delete().where(
            consoles.c['instance_uuid'] == null()).execute().rowcount
        self.assertEqual(1, rows)
        # Now run the 267 upgrade again.
        self.migration_api.upgrade(engine, self.REPOSITORY, 267)

        # Make sure the consoles entry with the null instance_uuid
        # was deleted.
        console = consoles.select(consoles.c.id == 1).execute().first()
        self.assertIsNone(console) 
Example 42
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_268(self, engine, data):
        # We can only assert that the col exists, not the unique constraint
        # as the engine is running sqlite
        self.assertColumnExists(engine, 'compute_nodes', 'host')
        self.assertColumnExists(engine, 'shadow_compute_nodes', 'host')
        compute_nodes = oslodbutils.get_table(engine, 'compute_nodes')
        shadow_compute_nodes = oslodbutils.get_table(
            engine, 'shadow_compute_nodes')
        self.assertIsInstance(compute_nodes.c.host.type,
                              sqlalchemy.types.String)
        self.assertIsInstance(shadow_compute_nodes.c.host.type,
                              sqlalchemy.types.String) 
Example 43
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_269(self, engine, data):

        self.assertColumnExists(engine, 'pci_devices', 'numa_node')
        self.assertColumnExists(engine, 'shadow_pci_devices', 'numa_node')
        pci_devices = oslodbutils.get_table(engine, 'pci_devices')
        shadow_pci_devices = oslodbutils.get_table(
            engine, 'shadow_pci_devices')
        self.assertIsInstance(pci_devices.c.numa_node.type,
                              sqlalchemy.types.Integer)
        self.assertTrue(pci_devices.c.numa_node.nullable)
        self.assertIsInstance(shadow_pci_devices.c.numa_node.type,
                              sqlalchemy.types.Integer)
        self.assertTrue(shadow_pci_devices.c.numa_node.nullable) 
Example 44
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_270(self, engine, data):
        self.assertColumnExists(engine, 'instance_extra', 'flavor')
        self.assertColumnExists(engine, 'shadow_instance_extra', 'flavor')

        instance_extra = oslodbutils.get_table(engine, 'instance_extra')
        shadow_instance_extra = oslodbutils.get_table(
                engine, 'shadow_instance_extra')
        self.assertIsInstance(instance_extra.c.flavor.type,
                              sqlalchemy.types.Text)
        self.assertIsInstance(shadow_instance_extra.c.flavor.type,
                              sqlalchemy.types.Text) 
Example 45
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _pre_upgrade_273(self, engine):
        if engine.name != 'sqlite':
            return

        # Drop a variety of unique constraints to ensure that the script
        # properly readds them back
        for table_name, constraint_name in [
                ('compute_nodes', 'uniq_compute_nodes0'
                                  'host0hypervisor_hostname'),
                ('fixed_ips', 'uniq_fixed_ips0address0deleted'),
                ('instance_info_caches', 'uniq_instance_info_caches0'
                                         'instance_uuid'),
                ('instance_type_projects', 'uniq_instance_type_projects0'
                                           'instance_type_id0project_id0'
                                           'deleted'),
                ('pci_devices', 'uniq_pci_devices0compute_node_id0'
                                'address0deleted'),
                ('virtual_interfaces', 'uniq_virtual_interfaces0'
                                       'address0deleted')]:
            table = oslodbutils.get_table(engine, table_name)
            constraints = [c for c in table.constraints
                           if c.name == constraint_name]
            for cons in constraints:
                # Need to use sqlalchemy-migrate UniqueConstraint
                cons = UniqueConstraint(*[c.name for c in cons.columns],
                                        name=cons.name,
                                        table=table)
                cons.drop() 
Example 46
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_273(self, engine, data):
        for src_table, src_column, dst_table, dst_column in [
                ('fixed_ips', 'instance_uuid', 'instances', 'uuid'),
                ('block_device_mapping', 'instance_uuid', 'instances', 'uuid'),
                ('instance_info_caches', 'instance_uuid', 'instances', 'uuid'),
                ('instance_metadata', 'instance_uuid', 'instances', 'uuid'),
                ('instance_system_metadata', 'instance_uuid',
                 'instances', 'uuid'),
                ('instance_type_projects', 'instance_type_id',
                 'instance_types', 'id'),
                ('iscsi_targets', 'volume_id', 'volumes', 'id'),
                ('reservations', 'usage_id', 'quota_usages', 'id'),
                ('security_group_instance_association', 'instance_uuid',
                 'instances', 'uuid'),
                ('security_group_instance_association', 'security_group_id',
                 'security_groups', 'id'),
                ('virtual_interfaces', 'instance_uuid', 'instances', 'uuid'),
                ('compute_nodes', 'service_id', 'services', 'id'),
                ('instance_actions', 'instance_uuid', 'instances', 'uuid'),
                ('instance_faults', 'instance_uuid', 'instances', 'uuid'),
                ('migrations', 'instance_uuid', 'instances', 'uuid')]:
            src_table = oslodbutils.get_table(engine, src_table)
            fkeys = {fk.parent.name: fk.column
                     for fk in src_table.foreign_keys}
            self.assertIn(src_column, fkeys)
            self.assertEqual(fkeys[src_column].table.name, dst_table)
            self.assertEqual(fkeys[src_column].name, dst_column) 
Example 47
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_274(self, engine, data):
        self.assertIndexMembers(engine, 'instances',
                                'instances_project_id_deleted_idx',
                                ['project_id', 'deleted'])
        self.assertIndexNotExists(engine, 'instances', 'project_id') 
Example 48
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _pre_upgrade_275(self, engine):
        # Create a keypair record so we can test that the upgrade will set
        # 'ssh' as default value in the new column for the previous keypair
        # entries.
        key_pairs = oslodbutils.get_table(engine, 'key_pairs')
        fake_keypair = {'name': 'test-migr'}
        key_pairs.insert().execute(fake_keypair) 
Example 49
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_277(self, engine, data):
        self.assertIndexMembers(engine, 'fixed_ips',
                                'fixed_ips_deleted_allocated_updated_at_idx',
                                ['deleted', 'allocated', 'updated_at']) 
Example 50
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_278(self, engine, data):
        compute_nodes = oslodbutils.get_table(engine, 'compute_nodes')
        self.assertEqual(0, len([fk for fk in compute_nodes.foreign_keys
                                 if fk.parent.name == 'service_id']))
        self.assertTrue(compute_nodes.c.service_id.nullable) 
Example 51
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_279(self, engine, data):
        inspector = reflection.Inspector.from_engine(engine)
        constraints = inspector.get_unique_constraints('compute_nodes')
        constraint_names = [constraint['name'] for constraint in constraints]
        self.assertNotIn('uniq_compute_nodes0host0hypervisor_hostname',
                         constraint_names)
        self.assertIn('uniq_compute_nodes0host0hypervisor_hostname0deleted',
                      constraint_names) 
Example 52
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_280(self, engine, data):
        key_pairs = oslodbutils.get_table(engine, 'key_pairs')
        self.assertFalse(key_pairs.c.name.nullable) 
Example 53
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_291(self, engine, data):
        # NOTE(danms): This is a dummy migration that just does a consistency
        # check
        pass 
Example 54
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _pre_upgrade_293(self, engine):
        migrations = oslodbutils.get_table(engine, 'migrations')
        fake_migration = {}
        migrations.insert().execute(fake_migration) 
Example 55
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_293(self, engine, data):
        self.assertColumnExists(engine, 'migrations', 'migration_type')
        self.assertColumnExists(engine, 'shadow_migrations', 'migration_type')
        migrations = oslodbutils.get_table(engine, 'migrations')
        fake_migration = migrations.select().execute().first()
        self.assertIsNone(fake_migration.migration_type)
        self.assertFalse(fake_migration.hidden) 
Example 56
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_294(self, engine, data):
        self.assertColumnExists(engine, 'services', 'last_seen_up')
        self.assertColumnExists(engine, 'shadow_services', 'last_seen_up')

        services = oslodbutils.get_table(engine, 'services')
        shadow_services = oslodbutils.get_table(
                engine, 'shadow_services')
        self.assertIsInstance(services.c.last_seen_up.type,
                              sqlalchemy.types.DateTime)
        self.assertIsInstance(shadow_services.c.last_seen_up.type,
                              sqlalchemy.types.DateTime) 
Example 57
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _pre_upgrade_295(self, engine):
        self.assertIndexNotExists(engine, 'virtual_interfaces',
                                  'virtual_interfaces_uuid_idx') 
Example 58
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_295(self, engine, data):
        self.assertIndexMembers(engine, 'virtual_interfaces',
                                'virtual_interfaces_uuid_idx', ['uuid']) 
Example 59
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_297(self, engine, data):
        self.assertColumnExists(engine, 'services', 'forced_down') 
Example 60
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_298(self, engine, data):
        # NOTE(nic): This is a MySQL-specific migration, and is a no-op from
        # the point-of-view of unit tests, since they use SQLite
        pass 
Example 61
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_299(self, engine, data):
        self.assertColumnExists(engine, 'services', 'version') 
Example 62
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_300(self, engine, data):
        self.assertColumnExists(engine, 'instance_extra', 'migration_context') 
Example 63
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_301(self, engine, data):
        self.assertColumnExists(engine, 'compute_nodes',
                                'cpu_allocation_ratio')
        self.assertColumnExists(engine, 'compute_nodes',
                                'ram_allocation_ratio') 
Example 64
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_314(self, engine, data):
        self.assertColumnExists(engine, 'inventories', 'resource_class_id')
        self.assertColumnExists(engine, 'allocations', 'resource_class_id')

        self.assertColumnExists(engine, 'resource_providers', 'id')
        self.assertColumnExists(engine, 'resource_providers', 'uuid')

        self.assertColumnExists(engine, 'compute_nodes', 'uuid')
        self.assertColumnExists(engine, 'shadow_compute_nodes', 'uuid')

        self.assertIndexMembers(engine, 'allocations',
                        'allocations_resource_provider_class_id_idx',
                        ['resource_provider_id', 'resource_class_id']) 
Example 65
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_315(self, engine, data):
        self.assertColumnExists(engine, 'migrations',
                                'memory_total')
        self.assertColumnExists(engine, 'migrations',
                                'memory_processed')
        self.assertColumnExists(engine, 'migrations',
                                'memory_remaining')
        self.assertColumnExists(engine, 'migrations',
                                'disk_total')
        self.assertColumnExists(engine, 'migrations',
                                'disk_processed')
        self.assertColumnExists(engine, 'migrations',
                                'disk_remaining') 
Example 66
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_316(self, engine, data):
        self.assertColumnExists(engine, 'compute_nodes',
                                'disk_allocation_ratio') 
Example 67
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_317(self, engine, data):
        self.assertColumnExists(engine, 'aggregates', 'uuid')
        self.assertColumnExists(engine, 'shadow_aggregates', 'uuid') 
Example 68
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_319(self, engine, data):
        self.assertIndexMembers(engine, 'instances',
                                'instances_deleted_created_at_idx',
                                ['deleted', 'created_at']) 
Example 69
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def _check_330(self, engine, data):
        # Just a sanity-check migration
        pass 
Example 70
Project: gnocchi   Author: gnocchixyz   File: sqlalchemy.py    Apache License 2.0 5 votes vote down vote up
def delete_resources(self, resource_type='generic',
                         attribute_filter=None):
        if not attribute_filter:
            raise ValueError("attribute_filter must be set")

        with self.facade.writer() as session:
            target_cls = self._resource_type_to_mappers(
                session, resource_type)["resource"]

            q = session.query(target_cls.id)

            engine = session.connection()
            try:
                f = QueryTransformer.build_filter(engine.dialect.name,
                                                  target_cls,
                                                  attribute_filter)
            except indexer.QueryAttributeError as e:
                # NOTE(jd) The QueryAttributeError does not know about
                # resource_type, so convert it
                raise indexer.ResourceAttributeError(resource_type,
                                                     e.attribute)

            q = q.filter(f)

            session.query(Metric).filter(
                Metric.resource_id.in_(q)
            ).update({"status": "delete"},
                     synchronize_session=False)
            return q.delete(synchronize_session=False) 
Example 71
Project: gnocchi   Author: gnocchixyz   File: sqlalchemy.py    Apache License 2.0 5 votes vote down vote up
def _handle_multiple_op(cls, engine, table, op, nodes):
        return op(*[
            cls.build_filter(engine, table, node)
            for node in nodes
        ]) 
Example 72
Project: gnocchi   Author: gnocchixyz   File: sqlalchemy.py    Apache License 2.0 5 votes vote down vote up
def _handle_unary_op(cls, engine, table, op, node):
        return op(cls.build_filter(engine, table, node)) 
Example 73
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: test_sql.py    MIT License 5 votes vote down vote up
def _get_index_columns(self, tbl_name):
        from sqlalchemy.engine import reflection

        insp = reflection.Inspector.from_engine(self.conn)
        ixs = insp.get_indexes("test_index_saved")
        ixs = [i["column_names"] for i in ixs]
        return ixs 
Example 74
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: test_sql.py    MIT License 5 votes vote down vote up
def _get_index_columns(self, tbl_name):
        from sqlalchemy.engine import reflection

        insp = reflection.Inspector.from_engine(self.conn)
        ixs = insp.get_indexes(tbl_name)
        ixs = [i["column_names"] for i in ixs]
        return ixs 
Example 75
Project: recruit   Author: Frank-qlu   File: test_sql.py    Apache License 2.0 5 votes vote down vote up
def _get_index_columns(self, tbl_name):
        from sqlalchemy.engine import reflection
        insp = reflection.Inspector.from_engine(self.conn)
        ixs = insp.get_indexes('test_index_saved')
        ixs = [i['column_names'] for i in ixs]
        return ixs 
Example 76
Project: recruit   Author: Frank-qlu   File: test_sql.py    Apache License 2.0 5 votes vote down vote up
def _get_index_columns(self, tbl_name):
        from sqlalchemy.engine import reflection
        insp = reflection.Inspector.from_engine(self.conn)
        ixs = insp.get_indexes(tbl_name)
        ixs = [i['column_names'] for i in ixs]
        return ixs 
Example 77
Project: nova   Author: ZhanHan   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def db_sync(self, engine):
        with mock.patch.object(sa_migration, 'get_engine',
                               return_value=engine):
            sa_migration.db_sync(database='api') 
Example 78
Project: nova   Author: ZhanHan   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertColumnExists(self, engine, table_name, column):
        self.assertTrue(db_utils.column_exists(engine, table_name, column),
                'Column %s.%s does not exist' % (table_name, column)) 
Example 79
Project: nova   Author: ZhanHan   File: test_migrations.py    Apache License 2.0 5 votes vote down vote up
def assertIndexExists(self, engine, table_name, index):
        self.assertTrue(db_utils.index_exists(engine, table_name, index),
                        'Index %s on table %s does not exist' %
                        (index, table_name)) 
Example 80
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migrations.py    Apache License 2.0 4 votes vote down vote up
def _check_005(self, engine, data):
        # flavors
        for column in ['created_at', 'updated_at', 'name', 'id', 'memory_mb',
            'vcpus', 'swap', 'vcpu_weight', 'flavorid', 'rxtx_factor',
            'root_gb', 'ephemeral_gb', 'disabled', 'is_public']:
            self.assertColumnExists(engine, 'flavors', column)
        self.assertUniqueConstraintExists(engine, 'flavors',
                ['flavorid'])
        self.assertUniqueConstraintExists(engine, 'flavors',
                ['name'])

        # flavor_extra_specs
        for column in ['created_at', 'updated_at', 'id', 'flavor_id', 'key',
            'value']:
            self.assertColumnExists(engine, 'flavor_extra_specs', column)

        if engine.name != 'ibm_db_sa':
            self.assertIndexExists(engine, 'flavor_extra_specs',
                'flavor_extra_specs_flavor_id_key_idx')
        self.assertUniqueConstraintExists(engine, 'flavor_extra_specs',
            ['flavor_id', 'key'])

        inspector = reflection.Inspector.from_engine(engine)
        # There should only be one foreign key here
        fk = inspector.get_foreign_keys('flavor_extra_specs')[0]
        self.assertEqual('flavors', fk['referred_table'])
        self.assertEqual(['id'], fk['referred_columns'])
        self.assertEqual(['flavor_id'], fk['constrained_columns'])

        # flavor_projects
        for column in ['created_at', 'updated_at', 'id', 'flavor_id',
            'project_id']:
            self.assertColumnExists(engine, 'flavor_projects', column)

        self.assertUniqueConstraintExists(engine, 'flavor_projects',
            ['flavor_id', 'project_id'])

        inspector = reflection.Inspector.from_engine(engine)
        # There should only be one foreign key here
        fk = inspector.get_foreign_keys('flavor_projects')[0]
        self.assertEqual('flavors', fk['referred_table'])
        self.assertEqual(['id'], fk['referred_columns'])
        self.assertEqual(['flavor_id'], fk['constrained_columns'])