Skip to content

Commit a442754

Browse files
authored
Merge pull request #40 from absynce/console-logging
Console logging
2 parents ce1fbbb + 779d599 commit a442754

File tree

14 files changed

+363
-117
lines changed

14 files changed

+363
-117
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ All notable changes will be documented in this file, particularly any breaking
33
changes. This project adheres to [Semantic Versioning](http://semver.org).
44

55
## [x.x.x]
6+
- Added - Option to configure a `RavenMigration.ILogger` or use the `ConsoleLogger`.
67

78
## [2.0.0]
89
- Changed (breaking) - The way the MigrationDocument's Id is determined. Multiple underscores

RavenMigrations.Tests/AlterTests.cs

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System.Collections.Generic;
2-
using FluentAssertions;
3-
using Raven.Abstractions.Commands;
1+
using System.Collections.Generic;
2+
using FluentAssertions;
3+
using Raven.Abstractions.Commands;
44
using Raven.Abstractions.Data;
55
using Raven.Client;
66
using Raven.Client.Indexes;
77
using Raven.Json.Linq;
88
using Raven.Tests.Helpers;
9+
using Moq;
910
using Xunit;
1011

1112
namespace RavenMigrations.Tests
@@ -20,7 +21,7 @@ public void Can_migrate_down_from_new_clr_type()
2021
InitialiseWithPerson(store, "Sean Kearon");
2122

2223
var migration = new AlterCollectionMigration();
23-
migration.Setup(store);
24+
migration.Setup(store, new NullLogger());
2425

2526
migration.Up();
2627
WaitForIndexing(store);
@@ -44,7 +45,7 @@ public void Can_migrate_up_to_new_clr_type()
4445
InitialiseWithPerson(store, "Sean Kearon");
4546

4647
var migration = new AlterCollectionMigration();
47-
migration.Setup(store);
48+
migration.Setup(store, new NullLogger());
4849

4950
migration.Up();
5051
using (var session = store.OpenSession())
@@ -54,28 +55,70 @@ public void Can_migrate_up_to_new_clr_type()
5455
customer.LastName.Should().Be("Kearon");
5556
}
5657
}
57-
}
58-
59-
[Fact]
60-
public void Can_add_additional_commands_as_part_of_migration()
61-
{
58+
}
59+
60+
[Fact]
61+
public void Can_add_additional_commands_as_part_of_migration()
62+
{
6263
using (var store = NewDocumentStore())
6364
{
6465
InitialiseWithPerson(store, "Sean Kearon");
6566

6667
var migration = new AlterCollectionMigration();
67-
migration.Setup(store);
68+
migration.Setup(store, new NullLogger());
6869

6970
migration.Up();
7071

7172
using (var session = store.OpenSession())
7273
{
73-
var foobaz = session.Load<FooBaz>(1);
74-
foobaz.Bar.Should().BeEquivalentTo("loaded");
74+
var foobaz = session.Load<FooBaz>(1);
75+
foobaz.Bar.Should().BeEquivalentTo("loaded");
7576
}
7677
}
7778
}
7879

80+
[Fact]
81+
public void Logger_WriteInformation_is_called_when_altering_collection()
82+
{
83+
var loggerMock = new Mock<ILogger>();
84+
85+
using (var store = NewDocumentStore())
86+
{
87+
InitialiseWithPerson(store, "Sean Kearon");
88+
89+
var migration = new AlterCollectionMigration();
90+
migration.Setup(store, loggerMock.Object);
91+
92+
migration.Up();
93+
}
94+
95+
loggerMock.Verify(logger => logger.WriteInformation("Updated {0} documents", 1), "Informational message should indicate how many documents were updated.");
96+
}
97+
98+
[Fact]
99+
public void Logger_WriteInformation_is_called_per_batch_when_altering_collection()
100+
{
101+
var loggerMock = new Mock<ILogger>();
102+
103+
using (var store = NewDocumentStore())
104+
{
105+
InitialiseWithPeople(store, new List<Person1>() {
106+
new Person1 {Name = "Sean Kearon"},
107+
new Person1 {Name = "Jared M. Smith"},
108+
new Person1 {Name = "Michael Owen"},
109+
new Person1 {Name = "Jonathan Skelton"},
110+
new Person1 {Name = "Matt King"}
111+
});
112+
var migration = new AlterCollectionMigration();
113+
migration.Setup(store, loggerMock.Object);
114+
115+
migration.Up();
116+
}
117+
118+
loggerMock.Verify(logger => logger.WriteInformation("Updated {0} documents", 2), Times.Exactly(2), "Informational message should indicate how many documents were updated.");
119+
loggerMock.Verify(logger => logger.WriteInformation("Updated {0} documents", 1), Times.Once, "Informational message should indicate how many documents were updated.");
120+
}
121+
79122
private void InitialiseWithPerson(IDocumentStore store, string name)
80123
{
81124
new RavenDocumentsByEntityName().Execute(store); //https://groups.google.com/forum/#!topic/ravendb/QqZPrRUwEkE
@@ -86,6 +129,17 @@ private void InitialiseWithPerson(IDocumentStore store, string name)
86129
}
87130
WaitForIndexing(store);
88131
}
132+
133+
private void InitialiseWithPeople(IDocumentStore store, List<Person1> people)
134+
{
135+
new RavenDocumentsByEntityName().Execute(store); //https://groups.google.com/forum/#!topic/ravendb/QqZPrRUwEkE
136+
using (var session = store.OpenSession())
137+
{
138+
people.ForEach(session.Store);
139+
session.SaveChanges();
140+
}
141+
WaitForIndexing(store);
142+
}
89143
}
90144

91145
[Migration(1, "alter")]
@@ -98,7 +152,7 @@ public override void Down()
98152

99153
public override void Up()
100154
{
101-
Alter.CollectionWithAdditionalCommands("Person1s", MigratePerson1ToPerson2);
155+
Alter.CollectionWithAdditionalCommands("Person1s", MigratePerson1ToPerson2, 2);
102156
}
103157

104158
private void MigratePerson2ToPerson1(RavenJObject doc, RavenJObject metadata)
@@ -110,7 +164,7 @@ private void MigratePerson2ToPerson1(RavenJObject doc, RavenJObject metadata)
110164
doc.Remove("FirstName");
111165
doc.Remove("LastName");
112166

113-
metadata[Constants.RavenClrType] = "RavenMigrations.Tests.Person1, RavenMigrations.Tests";
167+
metadata[Constants.RavenClrType] = "RavenMigrations.Tests.Person1, RavenMigrations.Tests";
114168
}
115169

116170
private IEnumerable<ICommandData> MigratePerson1ToPerson2(RavenJObject doc, RavenJObject metadata)
@@ -123,32 +177,32 @@ private IEnumerable<ICommandData> MigratePerson1ToPerson2(RavenJObject doc, Rave
123177
}
124178
doc.Remove("Name");
125179

126-
metadata[Constants.RavenClrType] = "RavenMigrations.Tests.Person2, RavenMigrations.Tests";
127-
128-
var foobaz = new FooBaz
129-
{
130-
Id = 1,
131-
Bar = "loaded"
132-
};
133-
134-
var foobazDoc = RavenJObject.FromObject(foobaz);
135-
var meta = new RavenJObject();
136-
meta[Constants.RavenEntityName] = "FooBazs";
137-
var cmd = new PutCommandData
138-
{
139-
Document = foobazDoc,
140-
Key = "foobazs/" + foobaz.Id,
141-
Metadata = meta
142-
};
143-
144-
return new[] {cmd};
180+
metadata[Constants.RavenClrType] = "RavenMigrations.Tests.Person2, RavenMigrations.Tests";
181+
182+
var foobaz = new FooBaz
183+
{
184+
Id = 1,
185+
Bar = "loaded"
186+
};
187+
188+
var foobazDoc = RavenJObject.FromObject(foobaz);
189+
var meta = new RavenJObject();
190+
meta[Constants.RavenEntityName] = "FooBazs";
191+
var cmd = new PutCommandData
192+
{
193+
Document = foobazDoc,
194+
Key = "foobazs/" + foobaz.Id,
195+
Metadata = meta
196+
};
197+
198+
return new[] {cmd};
145199
}
146-
}
147-
148-
public class FooBaz
149-
{
150-
public int Id { get; set; }
151-
public string Bar { get; set; }
200+
}
201+
202+
public class FooBaz
203+
{
204+
public int Id { get; set; }
205+
public string Bar { get; set; }
152206
}
153207

154208
public class Person1

RavenMigrations.Tests/RavenMigrations.Tests.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" />
34
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
45
<PropertyGroup>
56
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -32,6 +33,10 @@
3233
<WarningLevel>4</WarningLevel>
3334
</PropertyGroup>
3435
<ItemGroup>
36+
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
3540
<Reference Include="FluentAssertions">
3641
<HintPath>..\packages\FluentAssertions.2.1.0.0\lib\net45\FluentAssertions.dll</HintPath>
3742
</Reference>
@@ -49,6 +54,10 @@
4954
<Reference Include="Microsoft.WindowsAzure.Storage, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5055
<HintPath>..\packages\WindowsAzure.Storage.2.0.6.1\lib\net40\Microsoft.WindowsAzure.Storage.dll</HintPath>
5156
</Reference>
57+
<Reference Include="Moq, Version=4.5.9.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
58+
<HintPath>..\packages\Moq.4.5.9\lib\net45\Moq.dll</HintPath>
59+
<Private>True</Private>
60+
</Reference>
5261
<Reference Include="Raven.Abstractions">
5362
<HintPath>..\packages\RavenDB.Client.2.5.2700\lib\net45\Raven.Abstractions.dll</HintPath>
5463
</Reference>
@@ -101,6 +110,12 @@
101110
</ItemGroup>
102111
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
103112
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
113+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
114+
<PropertyGroup>
115+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
116+
</PropertyGroup>
117+
<Error Condition="!Exists('..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props'))" />
118+
</Target>
104119
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
105120
Other similar extension points exist, see Microsoft.Common.targets.
106121
<Target Name="BeforeBuild">

RavenMigrations.Tests/RunnerTests.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Linq;
22
using FluentAssertions;
3+
using Moq;
34
using Raven.Abstractions.Data;
5+
using Raven.Client;
46
using Raven.Client.Indexes;
57
using Raven.Tests.Helpers;
68
using Xunit;
@@ -284,6 +286,52 @@ public void Can_call_migrations_with_custom_attributes()
284286
}
285287
}
286288
}
289+
290+
public class Logger
291+
{
292+
public class WhenMigrationRunUp : RavenTestBase
293+
{
294+
[Fact]
295+
public void Logger_WriteInformation_should_be_called_before_and_after_migration()
296+
{
297+
var mockLogger = new Mock<ILogger>();
298+
299+
using (var store = NewDocumentStore())
300+
{
301+
Runner.Run(store, new MigrationOptions { Logger = mockLogger.Object });
302+
}
303+
304+
mockLogger.Verify(logger => logger.WriteInformation("{0}: Up migration started", typeof(First_Migration).Name), Times.AtLeastOnce, "Informational message should indicate Up migration started.");
305+
mockLogger.Verify(logger => logger.WriteInformation("{0}: Up migration completed", typeof(First_Migration).Name), Times.AtLeastOnce, "Informational message should indicate Up migration started.");
306+
}
307+
}
308+
309+
public class WhenMigrationRunDown : RavenTestBase
310+
{
311+
[Fact]
312+
public void Logger_WriteInformation_should_be_called_before_and_after_migration()
313+
{
314+
var mockLogger = new Mock<ILogger>();
315+
316+
using (var store = NewDocumentStore())
317+
{
318+
new TestDocumentIndex().Execute(store);
319+
320+
Runner.Run(store);
321+
WaitForIndexing(store);
322+
323+
Runner.Run(store, new MigrationOptions
324+
{
325+
Direction = Directions.Down,
326+
Logger = mockLogger.Object
327+
});
328+
}
329+
330+
mockLogger.Verify(logger => logger.WriteInformation("{0}: Down migration started", typeof(First_Migration).Name), Times.AtLeastOnce, "Informational message should indicate Up migration started.");
331+
mockLogger.Verify(logger => logger.WriteInformation("{0}: Down migration completed", typeof(First_Migration).Name), Times.AtLeastOnce, "Informational message should indicate Up migration started.");
332+
}
333+
}
334+
}
287335
}
288336

289337
public class TestDocument
@@ -388,7 +436,9 @@ public override void Up()
388436
session.SaveChanges();
389437
}
390438
}
391-
}
439+
}
440+
441+
392442

393443
public abstract class BaseMigration : Migration
394444
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Castle.Core" version="3.3.3" targetFramework="net45" />
34
<package id="FluentAssertions" version="2.1.0.0" targetFramework="net45" />
45
<package id="Microsoft.Data.Edm" version="5.2.0" targetFramework="net45" />
56
<package id="Microsoft.Data.OData" version="5.2.0" targetFramework="net45" />
67
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="1.8.0.0" targetFramework="net45" />
8+
<package id="Moq" version="4.5.9" targetFramework="net45" />
79
<package id="RavenDB.Client" version="2.5.2700" targetFramework="net45" />
810
<package id="RavenDB.Database" version="2.5.2700" targetFramework="net45" />
911
<package id="RavenDB.Embedded" version="2.5.2700" targetFramework="net45" />
1012
<package id="RavenDB.Tests.Helpers" version="2.5.2700" targetFramework="net45" />
1113
<package id="System.Spatial" version="5.2.0" targetFramework="net45" />
1214
<package id="WindowsAzure.Storage" version="2.0.6.1" targetFramework="net45" />
1315
<package id="xunit" version="1.9.2" targetFramework="net45" />
16+
<package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="net45" />
1417
</packages>

RavenMigrations/ConsoleLogger.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
3+
namespace RavenMigrations
4+
{
5+
public class ConsoleLogger : ILogger
6+
{
7+
/// <summary>
8+
/// Writes an informational message to the log.
9+
/// </summary>
10+
/// <param name="format">The format.</param>
11+
/// <param name="args">The args.</param>
12+
public void WriteInformation(string format, params object[] args)
13+
{
14+
Write(ConsoleColor.White, format, args);
15+
}
16+
17+
/// <summary>
18+
/// Writes an error message to the log.
19+
/// </summary>
20+
/// <param name="format">The format.</param>
21+
/// <param name="args">The args.</param>
22+
public void WriteError(string format, params object[] args)
23+
{
24+
Write(ConsoleColor.Red, format, args);
25+
}
26+
27+
/// <summary>
28+
/// Writes a warning message to the log.
29+
/// </summary>
30+
/// <param name="format">The format.</param>
31+
/// <param name="args">The args.</param>
32+
public void WriteWarning(string format, params object[] args)
33+
{
34+
Write(ConsoleColor.Yellow, format, args);
35+
}
36+
37+
private static void Write(ConsoleColor color, string format, object[] args)
38+
{
39+
Console.ForegroundColor = color;
40+
Console.WriteLine(format, args);
41+
Console.ResetColor();
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)