-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
137 lines (110 loc) · 4.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using Azure;
using Azure.Data.Tables;
using System.Diagnostics;
switch (args[0])
{
case "delete":
{
string? connectionString = null;
string? tableName = null;
string? query = null;
var batch = false;
var pageSize = 100;
for (var i = 1; i < args.Length; i++)
{
switch (args[i])
{
case "-c":
case "--connection-string":
connectionString = args[++i];
break;
case "-t":
case "--table":
tableName = args[++i];
break;
case "-q":
case "--query":
query = args[++i];
break;
case "-b":
case "--batch":
batch = true;
break;
case "--page-size":
pageSize = int.Parse(args[++i]);
break;
case "--help":
Console.WriteLine("""
ato delete -c <connection-string> -t <table> -q <query> [-b] [--page-size <page-size>]
-c, --connection-string <connection-string> The connection string to the storage account.
-t, --table <table> The table name.
-q, --query <query> The OData query to filter the entities.
-b, --batch Delete entities in batches of <page-size>.
--page-size <page-size> The maximum number of entities that will be returned per page request. Default is 100.
--help Show help.
""");
break;
default:
Console.WriteLine($"Unknown argument '{args[i]}'.");
return;
}
}
if (string.IsNullOrEmpty(connectionString))
{
Console.WriteLine("Connection string is not set.");
return;
}
if (string.IsNullOrEmpty(tableName))
{
Console.WriteLine("Table name is not set.");
return;
}
if (string.IsNullOrEmpty(query))
{
Console.WriteLine("Query is not set.");
return;
}
await DeleteAsync(connectionString, tableName, query, batch, pageSize);
break;
}
case "--help":
Console.WriteLine("""
ato <command> [<args>]
delete Delete entities from a table.
--help Show help.
""");
break;
default:
Console.WriteLine("Unknown command.");
break;
}
async Task DeleteAsync(string connectionString, string tableName, string query, bool batch, int pageSize)
{
var tableClient = new TableClient(connectionString, tableName);
AsyncPageable<TableEntity>? entities =
tableClient.QueryAsync<TableEntity>(query, select: new[] { "PartitionKey", "RowKey" }, maxPerPage: pageSize);
Debug.WriteLine($"Deleting all entities with filter '{query}' from table '{tableName}'.");
var count = 0;
if (batch)
await foreach (Page<TableEntity> page in entities.AsPages())
foreach (TableEntity[] chunk in page.Values.Chunk(100))
{
await tableClient.SubmitTransactionAsync(chunk.Select(entity =>
new TableTransactionAction(TableTransactionActionType.Delete, entity)));
count += chunk.Length;
foreach (TableEntity entity in chunk)
{
Console.WriteLine(
$"Deleted entity with partition key '{entity.PartitionKey}' and row key '{entity.RowKey}'.");
}
}
else
await foreach (TableEntity entity in entities)
{
await tableClient.DeleteEntityAsync(entity.PartitionKey, entity.RowKey);
count++;
Console.WriteLine(
$"Deleted entity with partition key '{entity.PartitionKey}' and row key '{entity.RowKey}'.");
}
Console.WriteLine($"Deleted {count} entities.");
}