Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

add migration to fix notifications from pre-1.0.0 upgrade #135

Merged
merged 2 commits into from
Oct 12, 2020
Merged
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
59 changes: 58 additions & 1 deletion src/deployment/data_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

import argparse
from uuid import UUID
import json
from typing import Callable, Dict, List

from azure.cosmosdb.table.tablebatch import TableBatch
from azure.cosmosdb.table.tableservice import TableService
from azure.mgmt.storage import StorageManagementClient
from azure.common.client_factory import get_client_from_cli_profile


def migrate_task_os(table_service: TableService) -> None:
Expand Down Expand Up @@ -38,8 +42,38 @@ def migrate_task_os(table_service: TableService) -> None:
print("migrated %s rows" % count)


def migrate_notification_keys(table_service: TableService) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe have much more explicit names and/or comments, including version info, for migration functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to think about for the future.

table_name = "Notification"
notifications = table_service.query_entities(
table_name, select="PartitionKey,RowKey,config"
)
partitionKey = None

count = 0
for entry in notifications:
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment about why this a non-exceptional UUID parse means we don't have to do anything?

UUID(entry.PartitionKey)
continue
except ValueError:
pass

table_service.insert_or_replace_entity(
table_name,
{
"PartitionKey": entry.RowKey,
"RowKey": entry.PartitionKey,
"config": entry.config,
},
)
table_service.delete_entity(table_name, entry.PartitionKey, entry.RowKey)
count += 1

print("migrated %s rows" % count)


migrations: Dict[str, Callable[[TableService], None]] = {
"migrate_task_os": migrate_task_os
"migrate_task_os": migrate_task_os,
"migrate_notification_keys": migrate_notification_keys,
}


Expand All @@ -48,3 +82,26 @@ def migrate(table_service: TableService, migration_names: List[str]) -> None:
print("applying migration '%s'" % name)
migrations[name](table_service)
print("migration '%s' applied" % name)


def main():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to have some user-facing INFO-level logging so it is clear what's going on.

formatter = argparse.ArgumentDefaultsHelpFormatter
parser = argparse.ArgumentParser(formatter_class=formatter)
parser.add_argument("resource_group")
parser.add_argument("storage_account")
parser.add_argument("migration", choices=migrations.keys(), nargs="+")
args = parser.parse_args()

client = get_client_from_cli_profile(StorageManagementClient)
storage_keys = client.storage_accounts.list_keys(
args.resource_group, args.storage_account
)
table_service = TableService(
account_name=args.storage_account, account_key=storage_keys.keys[0].value
)
print(args.migration)
migrate(table_service, args.migration)


if __name__ == "__main__":
main()