Python sqlalchemy.Table() Examples

The following are code examples for showing how to use sqlalchemy.Table(). 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: flasky   Author: RoseOu   File: base.py    MIT License 6 votes vote down vote up
def _show_create_table(self, connection, table, charset=None,
                           full_name=None):
        """Run SHOW CREATE TABLE for a ``Table``."""

        if full_name is None:
            full_name = self.identifier_preparer.format_table(table)
        st = "SHOW CREATE TABLE %s" % full_name

        rp = None
        try:
            rp = connection.execute(st)
        except exc.DBAPIError as e:
            if self._extract_error_code(e.orig) == 1146:
                raise exc.NoSuchTableError(full_name)
            else:
                raise
        row = self._compat_first(rp, charset=charset)
        if not row:
            raise exc.NoSuchTableError(full_name)
        return row[1].strip()

        return sql 
Example 2
Project: flasky   Author: RoseOu   File: base.py    MIT License 6 votes vote down vote up
def _describe_table(self, connection, table, charset=None,
                        full_name=None):
        """Run DESCRIBE for a ``Table`` and return processed rows."""

        if full_name is None:
            full_name = self.identifier_preparer.format_table(table)
        st = "DESCRIBE %s" % full_name

        rp, rows = None, None
        try:
            try:
                rp = connection.execute(st)
            except exc.DBAPIError as e:
                if self._extract_error_code(e.orig) == 1146:
                    raise exc.NoSuchTableError(full_name)
                else:
                    raise
            rows = self._compat_fetchall(rp, charset=charset)
        finally:
            if rp:
                rp.close()
        return rows 
Example 3
Project: flasky   Author: RoseOu   File: migration.py    MIT License 6 votes vote down vote up
def bind(self):
        """Return the current "bind".

        In online mode, this is an instance of
        :class:`sqlalchemy.engine.Connection`, and is suitable
        for ad-hoc execution of any kind of usage described
        in :ref:`sqlexpression_toplevel` as well as
        for usage with the :meth:`sqlalchemy.schema.Table.create`
        and :meth:`sqlalchemy.schema.MetaData.create_all` methods
        of :class:`~sqlalchemy.schema.Table`, :class:`~sqlalchemy.schema.MetaData`.

        Note that when "standard output" mode is enabled,
        this bind will be a "mock" connection handler that cannot
        return results and is only appropriate for a very limited
        subset of commands.

        """
        return self.connection 
Example 4
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 001_cell_mapping.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    cell_mappings = Table('cell_mappings', meta,
        Column('created_at', DateTime),
        Column('updated_at', DateTime),
        Column('id', Integer, primary_key=True, nullable=False),
        Column('uuid', String(length=36), nullable=False),
        Column('name', String(length=255)),
        Column('transport_url', Text()),
        Column('database_connection', Text()),
        UniqueConstraint('uuid', name='uniq_cell_mappings0uuid'),
        mysql_engine='InnoDB',
        mysql_charset='utf8'
    )

    # NOTE(mriedem): DB2 creates an index when a unique constraint is created
    # so trying to add a second index on the uuid column will fail with
    # error SQL0605W, so omit the index in the case of DB2.
    if migrate_engine.name != 'ibm_db_sa':
        Index('uuid_idx', cell_mappings.c.uuid)

    cell_mappings.create(checkfirst=True) 
Example 5
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 216_havana.py    Apache License 2.0 6 votes vote down vote up
def _create_dump_tables(migrate_engine):
    meta = MetaData(migrate_engine)
    meta.reflect(migrate_engine)

    table_names = ['compute_node_stats', 'compute_nodes', 'instance_actions',
                   'instance_actions_events', 'instance_faults', 'migrations']
    for table_name in table_names:
        table = Table(table_name, meta, autoload=True)

        dump_table_name = 'dump_' + table.name
        columns = []
        for column in table.columns:
            # NOTE(dprince): The dump_ tables were originally created from an
            # earlier schema version so we don't want to add the pci_stats
            # column so that schema diffs are exactly the same.
            if column.name == 'pci_stats':
                continue
            else:
                columns.append(column.copy())
        table_dump = Table(dump_table_name, meta, *columns,
                           mysql_engine='InnoDB')
        table_dump.create() 
Example 6
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 234_add_expire_reservations_index.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    reservations = Table('reservations', meta, autoload=True)
    if _get_deleted_expire_index(reservations):
        LOG.info(_LI('Skipped adding reservations_deleted_expire_idx '
                     'because an equivalent index already exists.'))
        return

    # Based on expire_reservations query
    # from: nova/db/sqlalchemy/api.py
    index = Index('reservations_deleted_expire_idx',
                  reservations.c.deleted, reservations.c.expire)

    index.create(migrate_engine) 
Example 7
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 279_fix_unique_constraint_for_compute_node.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine
    compute_nodes = Table('compute_nodes', meta, autoload=True)

    # Drop the old UniqueConstraint
    ukey = UniqueConstraint('host', 'hypervisor_hostname', table=compute_nodes,
                            name="uniq_compute_nodes0host0hypervisor_hostname")
    ukey.drop()

    # Add new UniqueConstraint
    ukey = UniqueConstraint(
        'host', 'hypervisor_hostname', 'deleted',
        table=compute_nodes,
        name="uniq_compute_nodes0host0hypervisor_hostname0deleted")
    ukey.create() 
Example 8
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 280_add_nullable_false_to_keypairs_name.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    """Function enforces non-null value for keypairs name field."""
    meta = MetaData(bind=migrate_engine)
    key_pairs = Table('key_pairs', meta, autoload=True)

    # Note: Since we are altering name field, this constraint on name needs to
    # first be dropped before we can alter name. We then re-create the same
    # constraint. It was first added in 216_havana.py so no need to remove
    # constraint on downgrade.
    UniqueConstraint('user_id', 'name', 'deleted', table=key_pairs,
                     name='uniq_key_pairs0user_id0name0deleted').drop()

    key_pairs.c.name.alter(nullable=False)

    UniqueConstraint('user_id', 'name', 'deleted', table=key_pairs,
                     name='uniq_key_pairs0user_id0name0deleted').create() 
Example 9
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 275_add_keypair_type.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    """Function adds key_pairs type field."""
    meta = MetaData(bind=migrate_engine)
    key_pairs = Table('key_pairs', meta, autoload=True)
    shadow_key_pairs = Table('shadow_key_pairs', meta, autoload=True)

    enum = Enum('ssh', 'x509', metadata=meta, name='keypair_types')
    enum.create()

    keypair_type = Column('type', enum, nullable=False,
                          server_default=keypair.KEYPAIR_TYPE_SSH)

    if hasattr(key_pairs.c, 'type'):
        key_pairs.c.type.drop()

    if hasattr(shadow_key_pairs.c, 'type'):
        shadow_key_pairs.c.type.drop()

    key_pairs.create_column(keypair_type)
    shadow_key_pairs.create_column(keypair_type.copy()) 
Example 10
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 313_add_parent_id_column.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData(bind=migrate_engine)

    # Add a new column to store PCI device parent address
    pci_devices = Table('pci_devices', meta, autoload=True)
    shadow_pci_devices = Table('shadow_pci_devices', meta, autoload=True)

    parent_addr = Column('parent_addr', String(12), nullable=True)

    if not hasattr(pci_devices.c, 'parent_addr'):
        pci_devices.create_column(parent_addr)
    if not hasattr(shadow_pci_devices.c, 'parent_addr'):
        shadow_pci_devices.create_column(parent_addr.copy())

    # Create index
    parent_index = Index('ix_pci_devices_compute_node_id_parent_addr_deleted',
                         pci_devices.c.compute_node_id,
                         pci_devices.c.parent_addr,
                         pci_devices.c.deleted)
    parent_index.create(migrate_engine) 
Example 11
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 291_enforce_flavors_migrated.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData(migrate_engine)
    instances = Table('instances', meta, autoload=True)
    sysmeta = Table('instance_system_metadata', meta, autoload=True)
    count = select([func.count()]).select_from(sysmeta).where(
        and_(instances.c.uuid == sysmeta.c.instance_uuid,
             sysmeta.c.key == 'instance_type_id',
             sysmeta.c.deleted != sysmeta.c.id,
             instances.c.deleted != instances.c.id)).execute().scalar()
    if count > 0:
        msg = _('There are still %(count)i unmigrated flavor records. '
                'Migration cannot continue until all instance flavor '
                'records have been migrated to the new format. Please run '
                '`nova-manage db migrate_flavor_data\' first.') % {
                    'count': count}
        raise exception.ValidationError(detail=msg) 
Example 12
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 330_enforce_mitaka_online_migrations.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData(migrate_engine)
    compute_nodes = Table('compute_nodes', meta, autoload=True)
    aggregates = Table('aggregates', meta, autoload=True)

    for table in (compute_nodes, aggregates):
        count = select([func.count()]).select_from(table).where(
            table.c.uuid == None).execute().scalar()  # NOQA
        if count > 0:
            msg = WARNING_MSG % {
                'count': count,
                'table': table.name,
            }
            raise exception.ValidationError(detail=msg)

    pci_devices = Table('pci_devices', meta, autoload=True)
    count = select([func.count()]).select_from(pci_devices).where(
        pci_devices.c.parent_addr == None).execute().scalar()  # NOQA
    if count > 0:
        msg = WARNING_MSG % {
            'count': count,
            'table': pci_devices.name,
        }
        raise exception.ValidationError(detail=msg) 
Example 13
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 268_add_host_in_compute_node.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    # Add a new column host
    compute_nodes = Table('compute_nodes', meta, autoload=True)
    shadow_compute_nodes = Table('shadow_compute_nodes', meta, autoload=True)

    # NOTE(sbauza) : Old compute nodes can report stats without this field, we
    # need to set it as nullable
    host = Column('host', String(255), nullable=True)
    if not hasattr(compute_nodes.c, 'host'):
        compute_nodes.create_column(host)
    if not hasattr(shadow_compute_nodes.c, 'host'):
        shadow_compute_nodes.create_column(host.copy())

    # NOTE(sbauza) : Populate the host field with the value from the services
    # table will be done at the ComputeNode object level when save()

    ukey = UniqueConstraint('host', 'hypervisor_hostname', table=compute_nodes,
                            name="uniq_compute_nodes0host0hypervisor_hostname")
    ukey.create() 
Example 14
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 278_remove_service_fk_in_compute_nodes.py    Apache License 2.0 6 votes vote down vote up
def _correct_sqlite_unique_constraints(migrate_engine, table):
    # NOTE(sbauza): SQLAlchemy<1.0 doesn't provide the unique keys in the
    # constraints field of the Table object, so it would drop them if we change
    # either the scheme or the constraints. Adding them back to the Table
    # object before changing the model makes sure that they are not dropped.
    if migrate_engine.name != 'sqlite':
        # other engines don't have this problem
        return
    inspector = reflection.Inspector.from_engine(migrate_engine)
    constraints = inspector.get_unique_constraints(table.name)
    constraint_names = [c.name for c in table.constraints]
    for constraint in constraints:
        if constraint['name'] in constraint_names:
            # the constraint is already in the table
            continue
        table.constraints.add(
            UniqueConstraint(*constraint['column_names'],
                             table=table, name=constraint['name'])) 
Example 15
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 248_add_expire_reservations_index.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    reservations = Table('reservations', meta, autoload=True)
    if _get_deleted_expire_index(reservations):
        LOG.info(_LI('Skipped adding reservations_deleted_expire_idx '
                     'because an equivalent index already exists.'))
        return

    # Based on expire_reservations query
    # from: nova/db/sqlalchemy/api.py
    index = Index('reservations_deleted_expire_idx',
                  reservations.c.deleted, reservations.c.expire)

    index.create(migrate_engine) 
Example 16
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 233_add_stats_in_compute_nodes.py    Apache License 2.0 6 votes vote down vote up
def upgrade(engine):
    meta = MetaData()
    meta.bind = engine

    # Drop the compute_node_stats table and add a 'stats' column to
    # compute_nodes directly.  The data itself is transient and doesn't
    # need to be copied over.
    table_names = ('compute_node_stats', 'shadow_compute_node_stats')
    for table_name in table_names:
        table = Table(table_name, meta, autoload=True)
        table.drop()

    # Add a new stats column to compute nodes
    table_names = ('compute_nodes', 'shadow_compute_nodes')
    for table_name in table_names:
        table = Table(table_name, meta, autoload=True)
        stats = Column('stats', Text, default='{}')
        table.create_column(stats) 
Example 17
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 274_update_instances_project_id_index.py    Apache License 2.0 6 votes vote down vote up
def upgrade(migrate_engine):
    """Change instances (project_id) index to cover (project_id, deleted)."""

    meta = MetaData(bind=migrate_engine)

    # Indexes can't be changed, we need to create the new one and delete
    # the old one

    instances = Table('instances', meta, autoload=True)

    for index in instances.indexes:
        if [c.name for c in index.columns] == ['project_id', 'deleted']:
            LOG.info(_LI('Skipped adding instances_project_id_deleted_idx '
                         'because an equivalent index already exists.'))
            break
    else:
        index = Index('instances_project_id_deleted_idx',
                      instances.c.project_id, instances.c.deleted)
        index.create()

    for index in instances.indexes:
        if [c.name for c in index.columns] == ['project_id']:
            index.drop() 
Example 18
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_fixtures.py    Apache License 2.0 6 votes vote down vote up
def test_fixture_schema_version(self):
        self.useFixture(conf_fixture.ConfFixture())

        # In/after 317 aggregates did have uuid
        self.useFixture(fixtures.DatabaseAtVersion(318))
        engine = session.get_engine()
        engine.connect()
        meta = sqlalchemy.MetaData(engine)
        aggregate = sqlalchemy.Table('aggregates', meta, autoload=True)
        self.assertTrue(hasattr(aggregate.c, 'uuid'))

        # Before 317, aggregates had no uuid
        self.useFixture(fixtures.DatabaseAtVersion(316))
        engine = session.get_engine()
        engine.connect()
        meta = sqlalchemy.MetaData(engine)
        aggregate = sqlalchemy.Table('aggregates', meta, autoload=True)
        self.assertFalse(hasattr(aggregate.c, 'uuid'))
        engine.dispose() 
Example 19
Project: flasky   Author: RoseOu   File: base.py    MIT License 5 votes vote down vote up
def get_column_specification(self, column, **kwargs):
        colspec = (self.preparer.format_column(column) + " "
                   + self.dialect.type_compiler.process(column.type))

        if column.nullable is not None:
            if not column.nullable or column.primary_key or \
                    isinstance(column.default, sa_schema.Sequence):
                colspec += " NOT NULL"
            else:
                colspec += " NULL"

        if column.table is None:
            raise exc.CompileError(
                "mssql requires Table-bound columns "
                "in order to generate DDL")

        # install an IDENTITY Sequence if we either a sequence or an implicit
        # IDENTITY column
        if isinstance(column.default, sa_schema.Sequence):
            if column.default.start == 0:
                start = 0
            else:
                start = column.default.start or 1

            colspec += " IDENTITY(%s,%s)" % (start,
                                             column.default.increment or 1)
        elif column is column.table._autoincrement_column:
            colspec += " IDENTITY(1,1)"
        else:
            default = self.get_column_default_string(column)
            if default is not None:
                colspec += " DEFAULT " + default

        return colspec 
Example 20
Project: flasky   Author: RoseOu   File: ddl.py    MIT License 5 votes vote down vote up
def execute_at(self, event_name, target):
        """Link execution of this DDL to the DDL lifecycle of a SchemaItem.

        Links this ``DDLElement`` to a ``Table`` or ``MetaData`` instance,
        executing it when that schema item is created or dropped. The DDL
        statement will be executed using the same Connection and transactional
        context as the Table create/drop itself. The ``.bind`` property of
        this statement is ignored.

        :param event:
          One of the events defined in the schema item's ``.ddl_events``;
          e.g. 'before-create', 'after-create', 'before-drop' or 'after-drop'

        :param target:
          The Table or MetaData instance for which this DDLElement will
          be associated with.

        A DDLElement instance can be linked to any number of schema items.

        ``execute_at`` builds on the ``append_ddl_listener`` interface of
        :class:`.MetaData` and :class:`.Table` objects.

        Caveat: Creating or dropping a Table in isolation will also trigger
        any DDL set to ``execute_at`` that Table's MetaData.  This may change
        in a future release.

        """

        def call_event(target, connection, **kw):
            if self._should_execute_deprecated(event_name,
                                               target, connection, **kw):
                return connection.execute(self.against(target))

        event.listen(target, "" + event_name.replace('-', '_'), call_event) 
Example 21
Project: flasky   Author: RoseOu   File: ddl.py    MIT License 5 votes vote down vote up
def __init__(self, element, on=None, bind=None):
        """Create a :class:`.CreateTable` construct.

        :param element: a :class:`.Table` that's the subject
         of the CREATE
        :param on: See the description for 'on' in :class:`.DDL`.
        :param bind: See the description for 'bind' in :class:`.DDL`.

        """
        super(CreateTable, self).__init__(element, on=on, bind=bind)
        self.columns = [CreateColumn(column)
                        for column in element.columns
                        ] 
Example 22
Project: flasky   Author: RoseOu   File: test_ddl.py    MIT License 5 votes vote down vote up
def _simple_fixture(self):
        return Table('test_table', self.metadata,
                     Column('id', Integer, primary_key=True,
                            autoincrement=False),
                     Column('data', String(50))
                     ) 
Example 23
Project: flasky   Author: RoseOu   File: test_ddl.py    MIT License 5 votes vote down vote up
def _underscore_fixture(self):
        return Table('_test_table', self.metadata,
                     Column('id', Integer, primary_key=True,
                            autoincrement=False),
                     Column('_data', String(50))
                     ) 
Example 24
Project: flasky   Author: RoseOu   File: events.py    MIT License 5 votes vote down vote up
def before_create(self, target, connection, **kw):
        """Called before CREATE statements are emitted.

        :param target: the :class:`.MetaData` or :class:`.Table`
         object which is the target of the event.
        :param connection: the :class:`.Connection` where the
         CREATE statement or statements will be emitted.
        :param \**kw: additional keyword arguments relevant
         to the event.  The contents of this dictionary
         may vary across releases, and include the
         list of tables being generated for a metadata-level
         event, the checkfirst flag, and other
         elements used by internal events.

        """ 
Example 25
Project: flasky   Author: RoseOu   File: events.py    MIT License 5 votes vote down vote up
def after_create(self, target, connection, **kw):
        """Called after CREATE statements are emitted.

        :param target: the :class:`.MetaData` or :class:`.Table`
         object which is the target of the event.
        :param connection: the :class:`.Connection` where the
         CREATE statement or statements have been emitted.
        :param \**kw: additional keyword arguments relevant
         to the event.  The contents of this dictionary
         may vary across releases, and include the
         list of tables being generated for a metadata-level
         event, the checkfirst flag, and other
         elements used by internal events.

        """ 
Example 26
Project: flasky   Author: RoseOu   File: events.py    MIT License 5 votes vote down vote up
def before_drop(self, target, connection, **kw):
        """Called before DROP statements are emitted.

        :param target: the :class:`.MetaData` or :class:`.Table`
         object which is the target of the event.
        :param connection: the :class:`.Connection` where the
         DROP statement or statements will be emitted.
        :param \**kw: additional keyword arguments relevant
         to the event.  The contents of this dictionary
         may vary across releases, and include the
         list of tables being generated for a metadata-level
         event, the checkfirst flag, and other
         elements used by internal events.

        """ 
Example 27
Project: flasky   Author: RoseOu   File: events.py    MIT License 5 votes vote down vote up
def before_parent_attach(self, target, parent):
        """Called before a :class:`.SchemaItem` is associated with
        a parent :class:`.SchemaItem`.

        :param target: the target object
        :param parent: the parent to which the target is being attached.

        :func:`.event.listen` also accepts a modifier for this event:

        :param propagate=False: When True, the listener function will
         be established for any copies made of the target object,
         i.e. those copies that are generated when
         :meth:`.Table.tometadata` is used.

        """ 
Example 28
Project: flasky   Author: RoseOu   File: events.py    MIT License 5 votes vote down vote up
def after_parent_attach(self, target, parent):
        """Called after a :class:`.SchemaItem` is associated with
        a parent :class:`.SchemaItem`.

        :param target: the target object
        :param parent: the parent to which the target is being attached.

        :func:`.event.listen` also accepts a modifier for this event:

        :param propagate=False: When True, the listener function will
         be established for any copies made of the target object,
         i.e. those copies that are generated when
         :meth:`.Table.tometadata` is used.

        """ 
Example 29
Project: flasky   Author: RoseOu   File: automap.py    MIT License 5 votes vote down vote up
def classname_for_table(base, tablename, table):
    """Return the class name that should be used, given the name
    of a table.

    The default implementation is::

        return str(tablename)

    Alternate implementations can be specified using the
    :paramref:`.AutomapBase.prepare.classname_for_table`
    parameter.

    :param base: the :class:`.AutomapBase` class doing the prepare.

    :param tablename: string name of the :class:`.Table`.

    :param table: the :class:`.Table` object itself.

    :return: a string class name.

     .. note::

        In Python 2, the string used for the class name **must** be a
        non-Unicode object, e.g. a ``str()`` object.  The ``.name`` attribute
        of :class:`.Table` is typically a Python unicode subclass, so the
        ``str()`` function should be applied to this name, after accounting for
        any non-ASCII characters.

    """
    return str(tablename) 
Example 30
Project: flasky   Author: RoseOu   File: __init__.py    MIT License 5 votes vote down vote up
def _include_sqlalchemy(obj):
    for module in sqlalchemy, sqlalchemy.orm:
        for key in module.__all__:
            if not hasattr(obj, key):
                setattr(obj, key, getattr(module, key))
    # Note: obj.Table does not attempt to be a SQLAlchemy Table class.
    obj.Table = _make_table(obj)
    obj.relationship = _wrap_with_default_query_class(obj.relationship)
    obj.relation = _wrap_with_default_query_class(obj.relation)
    obj.dynamic_loader = _wrap_with_default_query_class(obj.dynamic_loader)
    obj.event = event 
Example 31
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: migration.py    Apache License 2.0 5 votes vote down vote up
def _process_null_records(table, col_name, check_fkeys, delete=False):
    """Queries the database and optionally deletes the NULL records.

    :param table: sqlalchemy.Table object.
    :param col_name: The name of the column to check in the table.
    :param check_fkeys: If True, check the table for foreign keys back to the
        instances table and if not found, return.
    :param delete: If true, run a delete operation on the table, else just
        query for number of records that match the NULL column.
    :returns: The number of records processed for the table and column.
    """
    records = 0
    if col_name in table.columns:
        # NOTE(mriedem): filter out tables that don't have a foreign key back
        # to the instances table since they could have stale data even if
        # instances.uuid wasn't NULL.
        if check_fkeys:
            fkey_found = False
            fkeys = table.c[col_name].foreign_keys or []
            for fkey in fkeys:
                if fkey.column.table.name == 'instances':
                    fkey_found = True

            if not fkey_found:
                return 0

        if delete:
            records = table.delete().where(
                table.c[col_name] == null()
            ).execute().rowcount
        else:
            records = len(list(
                table.select().where(table.c[col_name] == null()).execute()
            ))
    return records 
Example 32
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 007_instance_mapping_nullable_cellid.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    instance_mapping = Table('instance_mappings', meta, autoload=True)
    instance_mapping.c.cell_id.alter(nullable=True) 
Example 33
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 003_host_mapping.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    cell_mappings = Table('cell_mappings', meta, autoload=True)
    host_mappings = Table('host_mappings', meta,
        Column('created_at', DateTime),
        Column('updated_at', DateTime),
        Column('id', Integer, primary_key=True, nullable=False),
        Column('cell_id', Integer, nullable=False),
        Column('host', String(length=255), nullable=False),
        UniqueConstraint('host',
            name='uniq_host_mappings0host'),
        ForeignKeyConstraint(columns=['cell_id'],
            refcolumns=[cell_mappings.c.id]),
        mysql_engine='InnoDB',
        mysql_charset='utf8'
    )

    # NOTE(mriedem): DB2 creates an index when a unique constraint is created
    # so trying to add a second index on the host column will fail with error
    # SQL0605W, so omit the index in the case of DB2.
    if migrate_engine.name != 'ibm_db_sa':
        Index('host_idx', host_mappings.c.host)

    host_mappings.create(checkfirst=True) 
Example 34
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 002_instance_mapping.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    cell_mappings = Table('cell_mappings', meta, autoload=True)
    instance_mappings = Table('instance_mappings', meta,
        Column('created_at', DateTime),
        Column('updated_at', DateTime),
        Column('id', Integer, primary_key=True, nullable=False),
        Column('instance_uuid', String(length=36), nullable=False),
        Column('cell_id', Integer, nullable=False),
        Column('project_id', String(length=255), nullable=False),
        UniqueConstraint('instance_uuid',
            name='uniq_instance_mappings0instance_uuid'),
        Index('project_id_idx', 'project_id'),
        ForeignKeyConstraint(columns=['cell_id'],
            refcolumns=[cell_mappings.c.id]),
        mysql_engine='InnoDB',
        mysql_charset='utf8'
    )

    # NOTE(mriedem): DB2 creates an index when a unique constraint is created
    # so trying to add a second index on the instance_uuid column will fail
    # with error SQL0605W, so omit the index in the case of DB2.
    if migrate_engine.name != 'ibm_db_sa':
        Index('instance_uuid_idx', instance_mappings.c.instance_uuid)

    instance_mappings.create(checkfirst=True) 
Example 35
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 006_build_request.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    request_specs = Table('request_specs', meta, autoload=True)
    build_requests = Table('build_requests', meta,
        Column('created_at', DateTime),
        Column('updated_at', DateTime),
        Column('id', Integer, primary_key=True, nullable=False),
        Column('request_spec_id', Integer, nullable=False),
        Column('project_id', String(length=255), nullable=False),
        Column('user_id', String(length=255), nullable=False),
        Column('display_name', String(length=255)),
        Column('instance_metadata', Text),
        Column('progress', Integer),
        Column('vm_state', String(length=255)),
        Column('task_state', String(length=255)),
        Column('image_ref', String(length=255)),
        Column('access_ip_v4', InetSmall()),
        Column('access_ip_v6', InetSmall()),
        Column('info_cache', Text),
        Column('security_groups', Text, nullable=False),
        Column('config_drive', Boolean, default=False, nullable=False),
        Column('key_name', String(length=255)),
        Column('locked_by', Enum('owner', 'admin',
            name='build_requests0locked_by')),
        UniqueConstraint('request_spec_id',
            name='uniq_build_requests0request_spec_id'),
        Index('build_requests_project_id_idx', 'project_id'),
        ForeignKeyConstraint(columns=['request_spec_id'],
            refcolumns=[request_specs.c.id]),
        mysql_engine='InnoDB',
        mysql_charset='utf8'
    )

    build_requests.create(checkfirst=True) 
Example 36
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 231_add_ephemeral_key_uuid.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    """Function adds ephemeral storage encryption key uuid field."""
    meta = MetaData(bind=migrate_engine)

    instances = Table('instances', meta, autoload=True)
    shadow_instances = Table('shadow_instances', meta, autoload=True)

    ephemeral_key_uuid = Column('ephemeral_key_uuid', String(36))
    instances.create_column(ephemeral_key_uuid)
    shadow_instances.create_column(ephemeral_key_uuid.copy())

    migrate_engine.execute(instances.update().
                           values(ephemeral_key_uuid=None))
    migrate_engine.execute(shadow_instances.update().
                           values(ephemeral_key_uuid=None)) 
Example 37
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 245_add_mtu_and_dhcp_server.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    """Function adds network mtu, dhcp_server, and share_dhcp fields."""
    meta = MetaData(bind=migrate_engine)

    networks = Table('networks', meta, autoload=True)
    shadow_networks = Table('shadow_networks', meta, autoload=True)

    # NOTE(vish): ignore duplicate runs of upgrade so this can
    #             be backported
    mtu = Column('mtu', Integer)
    dhcp_server = Column('dhcp_server', types.IPAddress)
    enable_dhcp = Column('enable_dhcp', Boolean, default=True)
    share_address = Column('share_address', Boolean, default=False)

    if not hasattr(networks.c, 'mtu'):
        networks.create_column(mtu)
    if not hasattr(networks.c, 'dhcp_server'):
        networks.create_column(dhcp_server)
    if not hasattr(networks.c, 'enable_dhcp'):
        networks.create_column(enable_dhcp)
    if not hasattr(networks.c, 'share_address'):
        networks.create_column(share_address)

    if not hasattr(shadow_networks.c, 'mtu'):
        shadow_networks.create_column(mtu.copy())
    if not hasattr(shadow_networks.c, 'dhcp_server'):
        shadow_networks.create_column(dhcp_server.copy())
    if not hasattr(shadow_networks.c, 'enable_dhcp'):
        shadow_networks.create_column(enable_dhcp.copy())
    if not hasattr(shadow_networks.c, 'share_address'):
        shadow_networks.create_column(share_address.copy()) 
Example 38
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 294_add_service_heartbeat.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    for prefix in ('', 'shadow_'):
        table = Table(prefix + BASE_TABLE_NAME, meta, autoload=True)
        new_column = Column(NEW_COLUMN_NAME, DateTime, nullable=True)
        if not hasattr(table.c, NEW_COLUMN_NAME):
            table.create_column(new_column) 
Example 39
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 316_add_disk_ratio_for_compute_nodes.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    cn = Table('compute_nodes', meta, autoload=True)
    shadow_cn = Table('shadow_compute_nodes', meta, autoload=True)
    cn.create_column(Column('disk_allocation_ratio', Float, nullable=True))
    shadow_cn.create_column(Column('disk_allocation_ratio', Float)) 
Example 40
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 253_add_pci_requests_to_instance_extra_table.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    for prefix in ('', 'shadow_'):
        table = Table(prefix + BASE_TABLE_NAME, meta, autoload=True)
        new_column = Column(NEW_COLUMN_NAME, Text, nullable=True)
        if not hasattr(table.c, NEW_COLUMN_NAME):
            table.create_column(new_column) 
Example 41
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 216_havana.py    Apache License 2.0 5 votes vote down vote up
def _create_shadow_tables(migrate_engine):
    meta = MetaData(migrate_engine)
    meta.reflect(migrate_engine)
    table_names = list(meta.tables.keys())

    meta.bind = migrate_engine

    for table_name in table_names:
        table = Table(table_name, meta, autoload=True)

        columns = []
        for column in table.columns:
            column_copy = None
            # NOTE(boris-42): BigInteger is not supported by sqlite, so
            #                 after copy it will have NullType, other
            #                 types that are used in Nova are supported by
            #                 sqlite.
            if isinstance(column.type, NullType):
                column_copy = Column(column.name, BigInteger(), default=0)
            if table_name == 'instances' and column.name == 'locked_by':
                enum = Enum('owner', 'admin',
                            name='shadow_instances0locked_by')
                column_copy = Column(column.name, enum)
            else:
                column_copy = column.copy()
            columns.append(column_copy)

        shadow_table_name = 'shadow_' + table_name
        shadow_table = Table(shadow_table_name, meta, *columns,
                             mysql_engine='InnoDB')
        try:
            shadow_table.create()
        except Exception:
            LOG.info(repr(shadow_table))
            LOG.exception(_LE('Exception while creating table.'))
            raise 
Example 42
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 244_increase_user_id_length_volume_usage_cache.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData(bind=migrate_engine)
    table = Table('volume_usage_cache', meta, autoload=True)
    col_resource = getattr(table.c, 'user_id')
    # Match the maximum length of user_id in Keystone here instead of
    # assuming explicitly a single UUID length.
    col_resource.alter(type=String(64)) 
Example 43
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 319_add_instances_deleted_created_at_index.py    Apache License 2.0 5 votes vote down vote up
def _get_table_index(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine
    table = Table('instances', meta, autoload=True)
    for idx in table.indexes:
        if idx.columns.keys() == INDEX_COLUMNS:
            break
    else:
        idx = None
    return meta, table, idx 
Example 44
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 297_add_forced_down_for_services.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    services = Table('services', meta, autoload=True)
    shadow_services = Table('shadow_services', meta, autoload=True)
    services.create_column(Column('forced_down', Boolean, default=False))
    shadow_services.create_column(Column('forced_down', Boolean,
                                         default=False)) 
Example 45
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 251_add_numa_topology_to_comput_nodes.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    compute_nodes = Table('compute_nodes', meta, autoload=True)
    shadow_compute_nodes = Table('shadow_compute_nodes', meta, autoload=True)

    numa_topology = Column('numa_topology', Text, nullable=True)
    shadow_numa_topology = Column('numa_topology', Text, nullable=True)
    compute_nodes.create_column(numa_topology)
    shadow_compute_nodes.create_column(shadow_numa_topology) 
Example 46
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 265_remove_duplicated_index.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    """Remove index that are subsets of other indexes."""

    meta = MetaData(bind=migrate_engine)

    for table_name, index_name in INDEXES:
        table = Table(table_name, meta, autoload=True)
        for index in table.indexes:
            if index.name == index_name:
                index.drop() 
Example 47
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 299_service_version_number.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    services = Table('services', meta, autoload=True)
    shadow_services = Table('shadow_services', meta, autoload=True)
    services.create_column(Column('version', Integer, default=0))
    shadow_services.create_column(Column('version', Integer,
                                         default=0)) 
Example 48
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 315_add_migration_progresss_detail.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData(bind=migrate_engine)
    migrations = Table('migrations', meta, autoload=True)
    shadow_migrations = Table('shadow_migrations', meta, autoload=True)

    columns = ['memory_total', 'memory_processed', 'memory_remaining',
               'disk_total', 'disk_processed', 'disk_remaining']
    for column_name in columns:
        column = Column(column_name, BigInteger, nullable=True)
        migrations.create_column(column)
        shadow_migrations.create_column(column.copy()) 
Example 49
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 300_migration_context.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    for prefix in ('', 'shadow_'):
        table = Table(prefix + BASE_TABLE_NAME, meta, autoload=True)
        new_column = Column(NEW_COLUMN_NAME, Text, nullable=True)
        if not hasattr(table.c, NEW_COLUMN_NAME):
            table.create_column(new_column) 
Example 50
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 232_drop_dump_tables.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData(migrate_engine)
    meta.reflect(migrate_engine)

    table_names = ['compute_node_stats', 'compute_nodes', 'instance_actions',
                   'instance_actions_events', 'instance_faults', 'migrations']
    for table_name in table_names:
        table = Table('dump_' + table_name, meta)
        table.drop(checkfirst=True) 
Example 51
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 273_sqlite_foreign_keys.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    if migrate_engine.name == 'sqlite':
        # SQLite is also missing this one index
        if not utils.index_exists(migrate_engine, 'fixed_ips', 'address'):
            utils.add_index(migrate_engine, 'fixed_ips', 'address',
                            ['address'])

        for src_table, src_column, dst_table, dst_column, name in FKEYS:
            src_table = Table(src_table, meta, autoload=True)
            if name in set(fk.name for fk in src_table.foreign_keys):
                continue

            src_column = src_table.c[src_column]

            dst_table = Table(dst_table, meta, autoload=True)
            dst_column = dst_table.c[dst_column]

            fkey = ForeignKeyConstraint(columns=[src_column],
                                        refcolumns=[dst_column],
                                        name=name)
            fkey.create()

        # SQLAlchemy versions < 1.0.0 don't reflect unique constraints
        # for SQLite correctly causing sqlalchemy-migrate to recreate
        # some tables with missing unique constraints. Re-add some
        # potentially missing unique constraints as a workaround.
        for table_name, name, column_names in UNIQUES:
            table = Table(table_name, meta, autoload=True)
            if name in set(c.name for c in table.constraints
                           if isinstance(table, schema.UniqueConstraint)):
                continue

            uc = UniqueConstraint(*column_names, table=table, name=name)
            uc.create() 
Example 52
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 252_add_instance_extra_table.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    columns = [
        (('created_at', DateTime), {}),
        (('updated_at', DateTime), {}),
        (('deleted_at', DateTime), {}),
        (('deleted', Integer), {}),
        (('id', Integer), dict(primary_key=True, nullable=False)),
        (('instance_uuid', String(length=36)), dict(nullable=False)),
        (('numa_topology', Text), dict(nullable=True)),
     ]
    for prefix in ('', 'shadow_'):
        instances = Table(prefix + 'instances', meta, autoload=True)
        basename = prefix + 'instance_extra'
        if migrate_engine.has_table(basename):
            continue
        _columns = tuple([Column(*args, **kwargs)
                          for args, kwargs in columns])
        table = Table(basename, meta, *_columns, mysql_engine='InnoDB',
                      mysql_charset='utf8')
        table.create()

        # Index
        instance_uuid_index = Index(basename + '_idx',
                                    table.c.instance_uuid)
        instance_uuid_index.create(migrate_engine)

        # Foreign key
        # NOTE(mriedem): DB2 won't create the ForeignKey over the
        # instances.uuid column since it doesn't have a UniqueConstraint (added
        # later in the 267 migration). The ForeignKey will be created for DB2
        # in the 296 migration.
        if not prefix and migrate_engine.name != 'ibm_db_sa':
            fkey_columns = [table.c.instance_uuid]
            fkey_refcolumns = [instances.c.uuid]
            instance_fkey = ForeignKeyConstraint(
                columns=fkey_columns, refcolumns=fkey_refcolumns)
            instance_fkey.create() 
Example 53
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 296_add_missing_db2_fkeys.py    Apache License 2.0 5 votes vote down vote up
def _get_refcolumns(metadata, refcolumns):
    refcolumn_objects = []
    for refcol in refcolumns:
        table, column = refcol.split('.')
        table = Table(table, metadata, autoload=True)
        refcolumn_objects.append(table.c[column])
    return refcolumn_objects 
Example 54
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 276_vcpu_model.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    for prefix in ('', 'shadow_'):
        table = Table(prefix + BASE_TABLE_NAME, meta, autoload=True)
        new_column = Column(NEW_COLUMN_NAME, Text, nullable=True)
        if not hasattr(table.c, NEW_COLUMN_NAME):
            table.create_column(new_column) 
Example 55
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 250_remove_instance_groups_metadata.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    """Remove the instance_group_metadata table."""
    meta = MetaData(bind=migrate_engine)

    if migrate_engine.has_table('instance_group_metadata'):
        group_metadata = Table('instance_group_metadata', meta, autoload=True)
        group_metadata.drop()

    if migrate_engine.has_table('shadow_instance_group_metadata'):
        shadow_group_metadata = Table('shadow_instance_group_metadata', meta,
                                      autoload=True)
        shadow_group_metadata.drop() 
Example 56
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 254_add_request_id_in_pci_devices.py    Apache License 2.0 5 votes vote down vote up
def upgrade(engine):
    """Function adds request_id field."""
    meta = MetaData(bind=engine)

    pci_devices = Table('pci_devices', meta, autoload=True)
    shadow_pci_devices = Table('shadow_pci_devices', meta, autoload=True)
    request_id = Column('request_id', String(36), nullable=True)

    if not hasattr(pci_devices.c, 'request_id'):
        pci_devices.create_column(request_id)

    if not hasattr(shadow_pci_devices.c, 'request_id'):
        shadow_pci_devices.create_column(request_id.copy()) 
Example 57
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 266_add_instance_tags.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = sa.MetaData(bind=migrate_engine)

    tags = sa.Table('tags', meta,
                    sa.Column('resource_id', sa.String(36), primary_key=True,
                              nullable=False),
                    sa.Column('tag', sa.Unicode(80), primary_key=True,
                              nullable=False),
                    sa.Index('tags_tag_idx', 'tag'),
                    mysql_engine='InnoDB',
                    mysql_charset='utf8')
    tags.create() 
Example 58
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 228_add_metrics_in_compute_nodes.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    # Add a new column metrics to save metrics info for compute nodes
    compute_nodes = Table('compute_nodes', meta, autoload=True)
    shadow_compute_nodes = Table('shadow_compute_nodes', meta, autoload=True)

    metrics = Column('metrics', Text, nullable=True)
    shadow_metrics = Column('metrics', Text, nullable=True)
    compute_nodes.create_column(metrics)
    shadow_compute_nodes.create_column(shadow_metrics) 
Example 59
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 229_add_extra_resources_in_compute_nodes.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    # Add a new column extra_resources to save extra_resources info for
    # compute nodes
    compute_nodes = Table('compute_nodes', meta, autoload=True)
    shadow_compute_nodes = Table('shadow_compute_nodes', meta, autoload=True)

    extra_resources = Column('extra_resources', Text, nullable=True)
    shadow_extra_resources = Column('extra_resources', Text, nullable=True)
    compute_nodes.create_column(extra_resources)
    shadow_compute_nodes.create_column(shadow_extra_resources) 
Example 60
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 227_fix_project_user_quotas_resource_length.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData(bind=migrate_engine)
    table = Table('project_user_quotas', meta, autoload=True)
    col_resource = getattr(table.c, 'resource')

    if col_resource.type.length == 25:
        # The resource of project_user_quotas table had been changed to
        # invalid length(25) since I56ad98d3702f53fe8cfa94093fea89074f7a5e90.
        # The following code fixes the length for the environments which are
        # deployed after I56ad98d3702f53fe8cfa94093fea89074f7a5e90.
        col_resource.alter(type=String(255))
        table.update().where(table.c.resource == 'injected_file_content_byt')\
            .values(resource='injected_file_content_bytes').execute() 
Example 61
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 317_add_aggregate_uuid.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    for table_prefix in ('', 'shadow_'):
        uuid_column = Column('uuid', String(36))
        aggregates = Table('%saggregates' % table_prefix, meta)
        if not hasattr(aggregates.c, 'uuid'):
            aggregates.create_column(uuid_column)
            if not table_prefix:
                index = Index('aggregate_uuid_idx', aggregates.c.uuid)
                index.create() 
Example 62
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 246_add_compute_node_id_fk.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    """Add missing foreign key constraint on pci_devices.compute_node_id."""
    meta = MetaData(bind=migrate_engine)

    pci_devices = Table('pci_devices', meta, autoload=True)
    compute_nodes = Table('compute_nodes', meta, autoload=True)

    fkey = ForeignKeyConstraint(columns=[pci_devices.c.compute_node_id],
                                refcolumns=[compute_nodes.c.id])
    fkey.create() 
Example 63
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 278_remove_service_fk_in_compute_nodes.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    compute_nodes = Table('compute_nodes', meta, autoload=True)
    shadow_compute_nodes = Table('shadow_compute_nodes', meta, autoload=True)
    services = Table('services', meta, autoload=True)

    _correct_sqlite_unique_constraints(migrate_engine, compute_nodes)

    # Make the service_id column nullable
    compute_nodes.c.service_id.alter(nullable=True)
    shadow_compute_nodes.c.service_id.alter(nullable=True)

    for fk in compute_nodes.foreign_keys:
        if fk.column == services.c.id:
            # Delete the FK
            fkey = ForeignKeyConstraint(columns=[compute_nodes.c.service_id],
                                        refcolumns=[services.c.id],
                                        name=fk.name)
            fkey.drop()
            break
    for index in compute_nodes.indexes:
        if 'service_id' in index.columns:
            # Delete the nested index which was created by the FK
            index.drop()
            break 
Example 64
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 269_add_numa_node_column.py    Apache License 2.0 5 votes vote down vote up
def upgrade(migrate_engine):
    meta = MetaData(bind=migrate_engine)

    # Add a new column to store PCI device numa node
    pci_devices = Table('pci_devices', meta, autoload=True)
    shadow_pci_devices = Table('shadow_pci_devices', meta, autoload=True)

    numa_node = Column('numa_node', Integer, default=None)
    if not hasattr(pci_devices.c, 'numa_node'):
        pci_devices.create_column(numa_node)
    if not hasattr(shadow_pci_devices.c, 'numa_node'):
        shadow_pci_devices.create_column(numa_node.copy()) 
Example 65
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 292_drop_nova_volumes_tables.py    Apache License 2.0 5 votes vote down vote up
def upgrade(engine):
    meta = MetaData()
    meta.bind = engine

    def _get_columns(source_table, params):
        columns = set()
        for column in params:
            columns.add(source_table.c[column])
        return columns

    def _remove_foreign_key_constraints(engine, meta, table_name):
        inspector = reflection.Inspector.from_engine(engine)

        for fk in inspector.get_foreign_keys(table_name):
            source_table = Table(table_name, meta, autoload=True)
            target_table = Table(fk['referred_table'], meta, autoload=True)

            fkey = ForeignKeyConstraint(
                columns=_get_columns(source_table, fk['constrained_columns']),
                refcolumns=_get_columns(target_table, fk['referred_columns']),
                name=fk['name'])
            fkey.drop()

    def _drop_table_and_indexes(meta, table_name):
        table = Table(table_name, meta, autoload=True)
        for index in table.indexes:
            index.drop()
        table.drop()

    # Drop the `iscsi_targets` and `volumes` tables They were used with
    # nova-volumes, which is deprecated and removed, but the related
    # tables are still created.
    table_names = ('iscsi_targets', 'shadow_iscsi_targets',
                   'volumes', 'shadow_volumes')

    for table_name in table_names:
        _remove_foreign_key_constraints(engine, meta, table_name)
        _drop_table_and_indexes(meta, table_name) 
Example 66
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: 295_add_virtual_interfaces_uuid_index.py    Apache License 2.0 5 votes vote down vote up
def _get_table_index(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine
    table = Table('virtual_interfaces', meta, autoload=True)
    for idx in table.indexes:
        if idx.columns.keys() == INDEX_COLUMNS:
            break
    else:
        idx = None
    return meta, table, idx 
Example 67
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_fixtures.py    Apache License 2.0 5 votes vote down vote up
def test_table(self):
        table = sqlalchemy.Table()
        with fixtures.BannedDBSchemaOperations(['Table']):
            self.assertRaises(exception.DBNotAllowed,
                              table.drop)
            self.assertRaises(exception.DBNotAllowed,
                              table.alter) 
Example 68
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_db_api.py    Apache License 2.0 5 votes vote down vote up
def _assert_shadow_tables_empty_except(self, *exceptions):
        """Ensure shadow tables are empty

        This method ensures that all the shadow tables in the schema,
        except for specificially named exceptions, are empty. This
        makes sure that archiving isn't moving unexpected content.
        """
        metadata = MetaData(bind=self.engine)
        metadata.reflect()
        for table in metadata.tables:
            if table.startswith("shadow_") and table not in exceptions:
                rows = self.conn.execute("select * from %s" % table).fetchall()
                self.assertEqual(rows, [], "Table %s not empty" % table) 
Example 69
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_db_api.py    Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(TestSqlalchemyTypesRepr, self).setUp()
        meta = MetaData(bind=self.engine)
        self.table = Table(
            'cidr_tbl',
            meta,
            Column('id', Integer, primary_key=True),
            Column('addr', col_types.CIDR())
        )
        self.table.create()
        self.addCleanup(meta.drop_all) 
Example 70
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migration_utils.py    Apache License 2.0 5 votes vote down vote up
def test_check_shadow_table(self):
        table_name = 'test_check_shadow_table'

        table = Table(table_name, self.meta,
                      Column('id', Integer, primary_key=True),
                      Column('a', Integer),
                      Column('c', String(256)))
        table.create()

        # check missing shadow table
        self.assertRaises(NoSuchTableError,
                          utils.check_shadow_table, self.engine, table_name)

        shadow_table = Table(db._SHADOW_TABLE_PREFIX + table_name, self.meta,
                             Column('id', Integer),
                             Column('a', Integer))
        shadow_table.create()

        # check missing column
        self.assertRaises(exception.NovaException,
                          utils.check_shadow_table, self.engine, table_name)

        # check when all is ok
        c = Column('c', String(256))
        shadow_table.create_column(c)
        self.assertTrue(utils.check_shadow_table(self.engine, table_name))

        # check extra column
        d = Column('d', Integer)
        shadow_table.create_column(d)
        self.assertRaises(exception.NovaException,
                          utils.check_shadow_table, self.engine, table_name) 
Example 71
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migration_utils.py    Apache License 2.0 5 votes vote down vote up
def test_check_shadow_table_different_types(self):
        table_name = 'test_check_shadow_table_different_types'

        table = Table(table_name, self.meta,
                      Column('id', Integer, primary_key=True),
                      Column('a', Integer))
        table.create()

        shadow_table = Table(db._SHADOW_TABLE_PREFIX + table_name, self.meta,
                             Column('id', Integer, primary_key=True),
                             Column('a', String(256)))
        shadow_table.create()
        self.assertRaises(exception.NovaException,
                          utils.check_shadow_table, self.engine, table_name) 
Example 72
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migration_utils.py    Apache License 2.0 5 votes vote down vote up
def test_check_shadow_table_with_unsupported_sqlite_type(self):
        table_name = 'test_check_shadow_table_with_unsupported_sqlite_type'

        table = Table(table_name, self.meta,
                      Column('id', Integer, primary_key=True),
                      Column('a', Integer),
                      Column('c', CustomType))
        table.create()

        shadow_table = Table(db._SHADOW_TABLE_PREFIX + table_name, self.meta,
                             Column('id', Integer, primary_key=True),
                             Column('a', Integer),
                             Column('c', CustomType))
        shadow_table.create()
        self.assertTrue(utils.check_shadow_table(self.engine, table_name)) 
Example 73
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migration_utils.py    Apache License 2.0 5 votes vote down vote up
def test_create_shadow_table_by_table_instance(self):
        table_name = 'test_create_shadow_table_by_table_instance'
        table = Table(table_name, self.meta,
                      Column('id', Integer, primary_key=True),
                      Column('a', Integer),
                      Column('b', String(256)))
        table.create()
        utils.create_shadow_table(self.engine, table=table)
        self.assertTrue(utils.check_shadow_table(self.engine, table_name)) 
Example 74
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migration_utils.py    Apache License 2.0 5 votes vote down vote up
def test_create_shadow_table_by_name(self):
        table_name = 'test_create_shadow_table_by_name'

        table = Table(table_name, self.meta,
                      Column('id', Integer, primary_key=True),
                      Column('a', Integer),
                      Column('b', String(256)))
        table.create()
        utils.create_shadow_table(self.engine, table_name=table_name)
        self.assertTrue(utils.check_shadow_table(self.engine, table_name)) 
Example 75
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migration_utils.py    Apache License 2.0 5 votes vote down vote up
def test_create_shadow_both_table_and_table_name_are_specified(self):
        table_name = ('test_create_shadow_both_table_and_table_name_are_'
                      'specified')
        table = Table(table_name, self.meta,
                      Column('id', Integer, primary_key=True),
                      Column('a', Integer))
        table.create()
        self.assertRaises(exception.NovaException,
                          utils.create_shadow_table,
                          self.engine, table=table, table_name=table_name) 
Example 76
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_migration_utils.py    Apache License 2.0 5 votes vote down vote up
def test_create_duplicate_shadow_table(self):
        table_name = 'test_create_duplicate_shadow_table'
        table = Table(table_name, self.meta,
                      Column('id', Integer, primary_key=True),
                      Column('a', Integer))
        table.create()
        utils.create_shadow_table(self.engine, table_name=table_name)
        self.assertRaises(exception.ShadowTableExists,
                          utils.create_shadow_table,
                          self.engine, table_name=table_name) 
Example 77
Project: flasky   Author: RoseOu   File: events.py    MIT License 4 votes vote down vote up
def column_reflect(self, inspector, table, column_info):
        """Called for each unit of 'column info' retrieved when
        a :class:`.Table` is being reflected.

        The dictionary of column information as returned by the
        dialect is passed, and can be modified.  The dictionary
        is that returned in each element of the list returned
        by :meth:`.reflection.Inspector.get_columns`.

        The event is called before any action is taken against
        this dictionary, and the contents can be modified.
        The :class:`.Column` specific arguments ``info``, ``key``,
        and ``quote`` can also be added to the dictionary and
        will be passed to the constructor of :class:`.Column`.

        Note that this event is only meaningful if either
        associated with the :class:`.Table` class across the
        board, e.g.::

            from sqlalchemy.schema import Table
            from sqlalchemy import event

            def listen_for_reflect(inspector, table, column_info):
                "receive a column_reflect event"
                # ...

            event.listen(
                    Table,
                    'column_reflect',
                    listen_for_reflect)

        ...or with a specific :class:`.Table` instance using
        the ``listeners`` argument::

            def listen_for_reflect(inspector, table, column_info):
                "receive a column_reflect event"
                # ...

            t = Table(
                'sometable',
                autoload=True,
                listeners=[
                    ('column_reflect', listen_for_reflect)
                ])

        This because the reflection process initiated by ``autoload=True``
        completes within the scope of the constructor for :class:`.Table`.

        """ 
Example 78
Project: flasky   Author: RoseOu   File: mutable.py    MIT License 4 votes vote down vote up
def as_mutable(cls, sqltype):
        """Associate a SQL type with this mutable Python type.

        This establishes listeners that will detect ORM mappings against
        the given type, adding mutation event trackers to those mappings.

        The type is returned, unconditionally as an instance, so that
        :meth:`.as_mutable` can be used inline::

            Table('mytable', metadata,
                Column('id', Integer, primary_key=True),
                Column('data', MyMutableType.as_mutable(PickleType))
            )

        Note that the returned type is always an instance, even if a class
        is given, and that only columns which are declared specifically with
        that type instance receive additional instrumentation.

        To associate a particular mutable type with all occurrences of a
        particular type, use the :meth:`.Mutable.associate_with` classmethod
        of the particular :class:`.Mutable` subclass to establish a global
        association.

        .. warning::

           The listeners established by this method are *global*
           to all mappers, and are *not* garbage collected.   Only use
           :meth:`.as_mutable` for types that are permanent to an application,
           not with ad-hoc types else this will cause unbounded growth
           in memory usage.

        """
        sqltype = types.to_instance(sqltype)

        def listen_for_type(mapper, class_):
            for prop in mapper.column_attrs:
                if prop.columns[0].type is sqltype:
                    cls.associate_with_attribute(getattr(class_, prop.key))

        event.listen(mapper, 'mapper_configured', listen_for_type)

        return sqltype 
Example 79
Project: flasky   Author: RoseOu   File: migration.py    MIT License 4 votes vote down vote up
def __init__(self, dialect, connection, opts):
        self.opts = opts
        self.dialect = dialect
        self.script = opts.get('script')

        as_sql = opts.get('as_sql', False)
        transactional_ddl = opts.get("transactional_ddl")

        if as_sql:
            self.connection = self._stdout_connection(connection)
            assert self.connection is not None
        else:
            self.connection = connection
        self._migrations_fn = opts.get('fn')
        self.as_sql = as_sql

        if "output_encoding" in opts:
            self.output_buffer = EncodedIO(
                opts.get("output_buffer") or sys.stdout,
                opts['output_encoding']
            )
        else:
            self.output_buffer = opts.get("output_buffer", sys.stdout)

        self._user_compare_type = opts.get('compare_type', False)
        self._user_compare_server_default = opts.get(
                                            'compare_server_default',
                                            False)
        version_table = opts.get('version_table', 'alembic_version')
        version_table_schema = opts.get('version_table_schema', None)
        self._version = Table(
            version_table, MetaData(),
            Column('version_num', String(32), nullable=False),
            schema=version_table_schema)

        self._start_from_rev = opts.get("starting_rev")
        self.impl = ddl.DefaultImpl.get_by_dialect(dialect)(
                            dialect, self.connection, self.as_sql,
                            transactional_ddl,
                            self.output_buffer,
                            opts
                            )
        log.info("Context impl %s.", self.impl.__class__.__name__)
        if self.as_sql:
            log.info("Generating static SQL")
        log.info("Will assume %s DDL.",
                        "transactional" if self.impl.transactional_ddl
                        else "non-transactional") 
Example 80
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: utils.py    Apache License 2.0 4 votes vote down vote up
def create_shadow_table(migrate_engine, table_name=None, table=None,
                        **col_name_col_instance):
    """This method create shadow table for table with name ``table_name``
    or table instance ``table``.
    :param table_name: Autoload table with this name and create shadow table
    :param table: Autoloaded table, so just create corresponding shadow table.
    :param col_name_col_instance:   contains pair column_name=column_instance.
    column_instance is instance of Column. These params are required only for
    columns that have unsupported types by sqlite. For example BigInteger.
    :returns: The created shadow_table object.
    """
    meta = MetaData(bind=migrate_engine)

    if table_name is None and table is None:
        raise exception.NovaException(_("Specify `table_name` or `table` "
                                        "param"))
    if not (table_name is None or table is None):
        raise exception.NovaException(_("Specify only one param `table_name` "
                                        "`table`"))

    if table is None:
        table = Table(table_name, meta, autoload=True)

    columns = []
    for column in table.columns:
        if isinstance(column.type, NullType):
            new_column = oslodbutils._get_not_supported_column(
                col_name_col_instance, column.name)
            columns.append(new_column)
        else:
            columns.append(column.copy())

    shadow_table_name = db._SHADOW_TABLE_PREFIX + table.name
    shadow_table = Table(shadow_table_name, meta, *columns,
                         mysql_engine='InnoDB')
    try:
        shadow_table.create()
        return shadow_table
    except (db_exc.DBError, OperationalError):
        # NOTE(ekudryashova): At the moment there is a case in oslo.db code,
        # which raises unwrapped OperationalError, so we should catch it until
        # oslo.db would wraps all such exceptions
        LOG.info(repr(shadow_table))
        LOG.exception(_LE('Exception while creating table.'))
        raise exception.ShadowTableExists(name=shadow_table_name)
    except Exception:
        LOG.info(repr(shadow_table))
        LOG.exception(_LE('Exception while creating table.'))