cinder/cinder/db/migrations/versions/b8660621f1b9_update_reservations_resource.py
Gorka Eguileor 3a968212d6 DB: Set quota resource property length to 300
On change I6c30a6be750f6b9ecff7399dbb0aea66cdc097da we increased the
`resource` column of the quota_usages table from 255 to 300, because its
value is constructed from (prefix + volume_type_name), but the length of
`volume_type_name` can be up to 255 characters, so if we add a prefix
such as 'volumes_' or 'gigabytes_' to it we'll exceed the db length
limit of the `resource` column.

There are other 3 quota related tables (quotas, quota_classes,
reservations) that have a `resource` column, and they are all
referencing the same kind of thing, but they still have a maximum size
of 255 characters, so there will be things that we won't be able to do
when using a volume type with a 255 characters name. Some of the
operations we won't be able to do are setting a default quota limit for
it or migrate volumes using that volume type.

Related-Bug: #1798327
Related-Bug: #1608849
Closes-Bug: #1948962
Change-Id: I40546b20322443dc34556de4aababf33a230db78
2024-01-12 14:25:43 +01:00

68 lines
2.7 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Update reservations resource
Revision ID: b8660621f1b9
Revises: 89aa6f9639f9
Create Date: 2021-10-27 17:25:16.790525
"""
from alembic import op
from oslo_log import log as logging
import sqlalchemy as sa
LOG = logging.getLogger(__name__)
# revision identifiers, used by Alembic.
revision = 'b8660621f1b9'
down_revision = '89aa6f9639f9'
branch_labels = None
depends_on = None
def upgrade():
connection = op.get_bind()
for table_name in ('quotas', 'quota_classes', 'reservations'):
table = sa.Table(table_name, sa.MetaData(), autoload_with=connection)
col = table.c.resource
# SQLite doesn't support altering tables, so we use a workaround
if connection.engine.name == 'sqlite':
with op.batch_alter_table(table_name) as batch_op:
batch_op.alter_column('resource',
existing_type=col.type,
type_=sa.String(length=300))
else:
# MySQL ALTER needs to have existing_type, existing_server_default,
# and existing_nullable or it will do who-knows-what
try:
op.alter_column(table_name, 'resource',
existing_type=col.type,
existing_nullable=col.nullable,
existing_server_default=col.server_default,
type_=sa.String(length=300))
except Exception:
# On MariaDB, max length varies depending on the version and
# the InnoDB page size [1], so it is possible to have error
# 1071 ('Specified key was too long; max key length is 767
# bytes"). Since this migration is to resolve a corner case,
# deployments with those DB versions won't be covered.
# [1]: https://mariadb.com/kb/en/library/innodb-limitations/#page-sizes # noqa
if not connection.engine.name == 'mysql':
raise
LOG.warning('Error in migration %s, Cinder still affected by '
'bug #1948962', revision)