Migrate Approval entities to Vote entities (#2279)

* migrate approvals to votes

* cron boilerplate

* refactor using generic function
This commit is contained in:
Daniel Smith 2022-10-03 10:54:32 -07:00 коммит произвёл GitHub
Родитель 7b08f2a6b8
Коммит c422b05881
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 58 добавлений и 1 удалений

Просмотреть файл

@ -29,6 +29,9 @@ cron:
- description: Copy over Comment entities to new Activity entities
url: /cron/schema_migration_comment_activity
schedule: 1 of jan 00:00
- description: Copy over Approval entities to new Vote entities
url: /cron/schema_migration_approval_vote
schedule: 1 of jan 00:00
- description: Copy over Feature entities to new FeatureEntry entities
url: /cron/schema_migration_feature_featureentry
schedule: 1 of jan 00:00

Просмотреть файл

@ -17,7 +17,7 @@ from google.cloud import ndb
from framework.basehandlers import FlaskHandler
from internals.core_models import Feature, FeatureEntry
from internals.review_models import Activity, Comment
from internals.review_models import Activity, Approval, Comment, Vote
def handle_migration(original_cls, new_cls, kwarg_mapping,
special_handler=None):
@ -87,6 +87,20 @@ class MigrateCommentsToActivities(FlaskHandler):
return (f'{old_migrations_deleted} Activities deleted '
'from previous migration.')
class MigrateApprovalsToVotes(FlaskHandler):
def get_template_data(self):
"""Writes a Vote entity for each unmigrated Approval entity."""
self.require_cron_header()
kwarg_mapping = [
('feature_id', 'feature_id'),
('gate_id', 'field_id'),
('state', 'state'),
('set_on', 'set_on'),
('set_by', 'set_by')]
return handle_migration(Approval, Vote, kwarg_mapping)
class MigrateFeaturesToFeatureEntries(FlaskHandler):
def get_template_data(self):

Просмотреть файл

@ -74,6 +74,45 @@ class MigrateCommentsToActivitiesTest(testing_config.CustomTestCase):
expected = '0 Comment entities migrated to Activity entities.'
self.assertEqual(result_2, expected)
class MigrateApprovalsToVotesTest(testing_config.CustomTestCase):
def setUp(self):
approval_1 = review_models.Approval(id=1, feature_id=1, field_id=1,
state=1, set_on=datetime(2020, 1, 1), set_by='user1@example.com')
approval_1.put()
approval_2 = review_models.Approval(id=2, feature_id=1, field_id=2,
state=2, set_on=datetime(2020, 3, 1), set_by='user2@example.com')
approval_2.put()
approval_3 = review_models.Approval(id=3, feature_id=2, field_id=2,
state=1, set_on=datetime(2022, 7, 1), set_by='user1@example.com')
approval_3.put()
vote_3 = review_models.Vote(id=3, feature_id=2, gate_id=2,
state=1, set_on=datetime(2022, 7, 1), set_by='user1@example.com')
vote_3.put()
def tearDown(self):
for comm in review_models.Approval.query().fetch():
comm.key.delete()
for activity in review_models.Vote.query().fetch():
activity.key.delete()
def test_migration(self):
migration_handler = schema_migration.MigrateApprovalsToVotes()
result = migration_handler.get_template_data()
# One approval is already migrated, so only 2 need migration.
expected = '2 Approval entities migrated to Vote entities.'
self.assertEqual(result, expected)
approvals = review_models.Approval.query().fetch()
self.assertEqual(len(approvals), 3)
self.assertEqual(2020, approvals[0].set_on.year)
# The migration should be idempotent, so nothing should be migrated twice.
result_2 = migration_handler.get_template_data()
expected = '0 Approval entities migrated to Vote entities.'
self.assertEqual(result_2, expected)
class MigrateFeaturesToFeatureEntriesTest(testing_config.CustomTestCase):

Просмотреть файл

@ -195,6 +195,7 @@ internals_routes = [
('/cron/warn_inactive_users', notifier.NotifyInactiveUsersHandler),
('/cron/remove_inactive_users', inactive_users.RemoveInactiveUsersHandler),
('/cron/schema_migration_comment_activity', schema_migration.MigrateCommentsToActivities),
('/cron/schema_migration_approval_vote', schema_migration.MigrateApprovalsToVotes),
('/cron/schema_migration_feature_featureentry', schema_migration.MigrateFeaturesToFeatureEntries),
('/cron/write_standard_maturity', deprecate_field.WriteStandardMaturityHandler),