Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations: fix the rename of name to label for DbComputer #4926

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '535039300e4a'
Expand All @@ -19,15 +18,13 @@

def upgrade():
"""Migrations for the upgrade."""
op.add_column('db_dbcomputer', sa.Column('label', sa.String(length=255), nullable=False))
op.drop_constraint('db_dbcomputer_name_key', 'db_dbcomputer', type_='unique')
op.drop_constraint('db_dbcomputer_name_key', 'db_dbcomputer')
op.alter_column('db_dbcomputer', 'name', new_column_name='label') # pylint: disable=no-member
op.create_unique_constraint('db_dbcomputer_label_key', 'db_dbcomputer', ['label'])
op.drop_column('db_dbcomputer', 'name')


def downgrade():
"""Migrations for the downgrade."""
op.add_column('db_dbcomputer', sa.Column('name', sa.VARCHAR(length=255), autoincrement=False, nullable=False))
op.drop_constraint('db_dbcomputer_label_key', 'db_dbcomputer', type_='unique')
op.drop_constraint('db_dbcomputer_label_key', 'db_dbcomputer')
op.alter_column('db_dbcomputer', 'label', new_column_name='name') # pylint: disable=no-member
op.create_unique_constraint('db_dbcomputer_name_key', 'db_dbcomputer', ['name'])
op.drop_column('db_dbcomputer', 'label')
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
# pylint: disable=import-error,no-name-in-module,invalid-name
"""Test migration that renames the ``name`` column of the ``Computer`` entity to ``label``."""
from .test_migrations_common import TestMigrations


class TestMigration(TestMigrations):
"""Test migration that renames the ``name`` column of the ``Computer`` entity to ``label``."""

migrate_from = '0047_migrate_repository'
migrate_to = '0048_computer_name_to_label'

def setUpBeforeMigration(self):
DbComputer = self.apps.get_model('db', 'DbComputer')

computer = DbComputer(name='testing')
computer.save()
self.computer_pk = computer.pk

def test_migration(self):
"""Test that the migration was performed correctly."""
DbComputer = self.apps.get_model('db', 'DbComputer')

computer = DbComputer.objects.get(pk=self.computer_pk)
assert computer.label == 'testing'
44 changes: 44 additions & 0 deletions tests/backends/aiida_sqlalchemy/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,7 @@ def test_data_node_type_string(self):
finally:
session.close()


class TestParameterDataToDictMigration(TestMigrationsSQLA):
"""Test the data migration after `ParameterData` was renamed to `Dict`."""

Expand Down Expand Up @@ -1898,3 +1899,46 @@ def test_migration(self):
assert isinstance(repository_uuid.val, str)
finally:
session.close()


class TestComputerNameToLabelMigration(TestMigrationsSQLA):
"""Test the renaming of `name` to `label` for `DbComputer."""

migrate_from = '1feaea71bd5a' # 1feaea71bd5a_migrate_repository
migrate_to = '535039300e4a' # 5ddd24e52864_dbnode_type_to_dbnode_node_type

def setUpBeforeMigration(self):
from sqlalchemy.orm import Session # pylint: disable=import-error,no-name-in-module

DbComputer = self.get_auto_base().classes.db_dbcomputer # pylint: disable=invalid-name

with sa.ENGINE.begin() as connection:
try:
session = Session(connection.engine)

computer = DbComputer(name='testing')

session.add(computer)
session.commit()

self.computer_id = computer.id
except Exception:
session.rollback()
raise
finally:
session.close()

def test_migration(self):
"""Verify that the column was successfully renamed."""
from sqlalchemy.orm import Session # pylint: disable=import-error,no-name-in-module

DbComputer = self.get_auto_base().classes.db_dbcomputer # pylint: disable=invalid-name

with sa.ENGINE.begin() as connection:
try:
session = Session(connection.engine)

computer = session.query(DbComputer).filter(DbComputer.id == self.computer_id).one()
self.assertEqual(computer.label, 'testing')
finally:
session.close()