Skip to content

Commit

Permalink
Add sample of using cosmos db migration for console (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanilsz committed Apr 2, 2024
1 parent 77d317f commit e4be46a
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The MSA.BuildingBlocks library includes the following packages:

* [x] [**MSA.BuildingBlocks.ServiceClient**](src/MSA.BuildingBlocks.ServiceClient/README.md): Provides infrastructure for internal HTTP communication between services.
* [x] [**MSA.BuildingBlocks.Mapping**](src/MSA.BuildingBlocks.Mapping/README.md): Contains extensions and interfaces IMapTo<> and IMapFrom<> for AutoMapper, facilitating object-object mapping.
* [x] [**MSA.BuildingBlocks.CosmosDbMigrations**](src/MSA.BuildingBlocks.CosmosDbMigrations/README.md): Implements a migration mechanism for CosmosDB, ensuring your database schema is always up-to-date.
* [x] [**MSA.BuildingBlocks.CosmosDbMigration**](src/MSA.BuildingBlocks.CosmosDbMigration/README.md): Implements a migration mechanism for CosmosDB, ensuring your database schema is always up-to-date.
* [ ] **MSA.BuildingBlocks.EventBus**: Offers infrastructure for working with messaging-broker technologies like Azure Service Bus, RabbitMQ, and Kafka.
* [ ] **MSA.BuildingBlocks.RequestReplyBus**: Provides infrastructure for working with messaging-broker technologies like Azure Service Bus and RabbitMQ, specifically tailored for request-reply messaging patterns.
* [ ] **MSA.BuildingBlocks.FileStorage**: Facilitates work with file storage systems such as Azure Blob storage, Azure DataLake, and Amazon S3.
Expand Down
18 changes: 18 additions & 0 deletions samples/CosmosDbMigrationConsole/CosmosDbMigrationConsole.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\MSA.BuildingBlocks.CosmosDbMigration\MSA.BuildingBlocks.CosmosDbMigration.csproj" />
</ItemGroup>

</Project>
135 changes: 135 additions & 0 deletions samples/CosmosDbMigrationConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Logging;
using MSA.BuildingBlocks.CosmosDbMigration;
using Newtonsoft.Json;

Console.WriteLine("Testing migrations");

using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());

//BaseDatabaseMigration databaseMigration = new DatabaseMigration(
// new CosmosClient("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="),
// "TestDb",
// "TestContainer1",
// factory.CreateLogger<DatabaseMigration>());

////var response = await migrationOperation.GetItems();

////await migrationOperation.RecreateContainerWithNewPartitionKey("CountryCode");

////await databaseMigration.CloneContainer("TestContainer3", "id");

//Collection<IncludedPath> includedPathes = new()
//{
// new IncludedPath
// {
// Path = "/FirstName/?"
// }
//};

//Collection<ExcludedPath> excludedPathes = new()
//{
// new ExcludedPath()
// {
// Path = "/*"
// }
//};

//var compositePathes = new Collection<Collection<CompositePath>>()
//{
// new Collection<CompositePath>()
// {
// new CompositePath()
// {
// Path = "/FirstName",
// Order = CompositePathSortOrder.Ascending
// },
// new CompositePath()
// {
// Path = "/id",
// Order = CompositePathSortOrder.Ascending
// }
// }
//};

//await databaseMigration.AddIndexingPolicy(includedPathes, excludedPathes, compositePathes);

//await databaseMigration.SwitchToContainer("Analysis", "BankPropositionsPredictor");
//await databaseMigration.CloneContainer("Analysis2", "Suggestion");

BaseContainerMigration containerMigration = new ContainerMigration(
new CosmosClient("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="),
"TestDb",
"TestContainer1",
factory.CreateLogger<ContainerMigration>());

//var items = await containerMigration.GetItems();

//await containerMigration.UpsertItems<TestClass>(new List<TestClass>
//{
// new TestClass
// {
// id = Guid.NewGuid().ToString(),
// MyProperty = 10002,
// MyProperty2 = 4,
// MyProperty3 = 3,
// CountryCode = "UA"
// },
// new TestClass
// {
// id = Guid.NewGuid().ToString(),
// MyProperty = 1,
// MyProperty2 = 2,
// MyProperty3 = 3,
// CountryCode = "FR"
// },
// new TestClass
// {
// id = Guid.NewGuid().ToString(),
// MyProperty = 1,
// MyProperty2 = 2,
// MyProperty3 = 3,
// CountryCode = "TR"
// }
//});

//await containerMigration.UpsertItem<TestClass>(new TestClass
//{
// id = Guid.NewGuid().ToString(),
// MyProperty = 10002,
// MyProperty2 = 1234,
// MyProperty3 = 3,
// CountryCode = "UA"
//});

//await containerMigration.AddPropertyToItems(items, "InnerClass.SomeProp1", "SomeProp3", new { InterProp = "Some" });
//await containerMigration.AddPropertyToItems(items, "MyProperty6", 10);
//await containerMigration.RemovePropertyFromItems(items, "InnerClass.SomeProp1", "SomeProp3");
//await containerMigration.RemovePropertyFromItems(items, "MyProperty6");

await containerMigration.RemoveItemsByQuery("SELECT * FROM c WHERE c.CountryCode = \"AR\" and c.City = \"Necochea\"");

Console.ReadLine();

public class TestClass
{
[JsonProperty(PropertyName = "id")]
public string id { get; set; }
public string someField { get; set; }
public int MyProperty { get; set; }
public int MyProperty2 { get; set; }
public int MyProperty3 { get; set; }
public string CountryCode { get; set; }

public TestInnerClass InnerClass { get; set; }
}

public class TestInnerClass
{
public TestInner2Class SomeProp1 { get; set; }

public class TestInner2Class
{
public int SomeProp { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/MSA.BuildingBlocks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MSA.BuildingBlocks.ServiceC
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MSA.BuildingBlocks.Mapping", "MSA.BuildingBlocks.Mapping\MSA.BuildingBlocks.Mapping.csproj", "{CDA38D23-951C-4777-871F-7C5D609326C6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{BF9EDB6F-070A-4935-A13A-7FC531BDF85F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CosmosDbMigrationConsole", "..\samples\CosmosDbMigrationConsole\CosmosDbMigrationConsole.csproj", "{13F92C58-EB81-4634-BA88-F1D4D10422E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,6 +33,10 @@ Global
{CDA38D23-951C-4777-871F-7C5D609326C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDA38D23-951C-4777-871F-7C5D609326C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDA38D23-951C-4777-871F-7C5D609326C6}.Release|Any CPU.Build.0 = Release|Any CPU
{13F92C58-EB81-4634-BA88-F1D4D10422E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13F92C58-EB81-4634-BA88-F1D4D10422E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13F92C58-EB81-4634-BA88-F1D4D10422E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13F92C58-EB81-4634-BA88-F1D4D10422E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -37,6 +45,7 @@ Global
{233184F9-A005-40F5-AF06-D23A43E981A7} = {70C4DDFE-91CE-4A5B-A0FD-BF91A9F990A9}
{C602F21D-843B-4597-BB22-D55D24C9B4C6} = {70C4DDFE-91CE-4A5B-A0FD-BF91A9F990A9}
{CDA38D23-951C-4777-871F-7C5D609326C6} = {70C4DDFE-91CE-4A5B-A0FD-BF91A9F990A9}
{13F92C58-EB81-4634-BA88-F1D4D10422E1} = {BF9EDB6F-070A-4935-A13A-7FC531BDF85F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A2E6DDAD-43F6-416D-A5BE-CC867C00654E}
Expand Down

0 comments on commit e4be46a

Please sign in to comment.