This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
add migration to fix notifications from pre-1.0.0 upgrade #135
Merged
bmc-msft
merged 2 commits into
microsoft:main
from
bmc-msft:add-notification-key-migration
Oct 12, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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: | ||
table_name = "Notification" | ||
notifications = table_service.query_entities( | ||
table_name, select="PartitionKey,RowKey,config" | ||
) | ||
partitionKey = None | ||
|
||
count = 0 | ||
for entry in notifications: | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
|
||
|
||
|
@@ -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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.