-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Fix Uninstall & UninstallAsync support in DataMigrationManager #18608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hishamco
wants to merge
7
commits into
main
Choose a base branch
from
hishamco/data-migration-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+263
−13
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eef15f4
Fix Uninstall & UninstallAsync support in DataMigrationManager
hishamco f271e3e
Use compiled expressions
hishamco 769fc40
Remove GetMethod()
hishamco 5bacb6b
Remove unnecessary comments
hishamco 777bbbf
Add unit tests
hishamco e1f224e
Sealed classes
hishamco 5553c5e
Remove expressions
hishamco 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 hidden or 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
221 changes: 221 additions & 0 deletions
221
test/OrchardCore.Tests/Data/Migration/DataMigrationManagerTests.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| using System.Data.Common; | ||
| using OrchardCore.Environment.Extensions; | ||
| using OrchardCore.Environment.Extensions.Features; | ||
| using ISession = YesSql.ISession; | ||
|
|
||
| namespace OrchardCore.Data.Migration.Tests; | ||
|
|
||
| public class DataMigrationManagerTests | ||
| { | ||
| [Fact] | ||
| public async Task UpdateAsync_ShouldExecuteDataMigration_CreateMethod_OnFreshMigration() | ||
| { | ||
| // Arrange | ||
| var migration1 = new Migration1(); | ||
| var migration2 = new Migration2(); | ||
| var migrationManager = GetDataMigrationManager([migration1, migration2]); | ||
|
|
||
| // Act | ||
| await migrationManager.UpdateAsync("TestFeature"); | ||
|
|
||
| // Assert | ||
| Assert.True(migration1.CreateCalled); | ||
| Assert.True(migration2.CreateCalled); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task UpdateAsync_ShouldExecuteDataMigration_UpdateFromMethods() | ||
| { | ||
| // Arrange | ||
| var migration1 = new Migration1(); | ||
| var migration2 = new Migration2(); | ||
| var migrationManager = GetDataMigrationManager([migration1, migration2]); | ||
|
|
||
| // Act | ||
| await migrationManager.UpdateAsync("TestFeature"); | ||
|
|
||
| // Assert | ||
| Assert.Equal(2, migration1.UpdateFromCalls); | ||
| Assert.Equal(0, migration2.UpdateFromCalls); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Uninstall_ShouldExecuteDataMigration_UninstallMethod() | ||
| { | ||
| // Arrange | ||
| var migration1 = new Migration1(); | ||
| var migration2 = new Migration2(); | ||
| var migrationManager = GetDataMigrationManager([migration1, migration2]); | ||
|
|
||
| // Act | ||
| await migrationManager.Uninstall("TestFeature"); | ||
|
|
||
| // Assert | ||
| Assert.True(migration1.UninstallCalled); | ||
| Assert.True(migration2.UninstallCalled); | ||
| } | ||
|
|
||
| private static DataMigrationManager GetDataMigrationManager(IEnumerable<DataMigration> dataMigrations) | ||
| { | ||
| var featureInfo = new Mock<IFeatureInfo>(); | ||
| featureInfo.Setup(f => f.Id).Returns("TestFeature"); | ||
|
|
||
| var typeFeatureProviderMock = new Mock<ITypeFeatureProvider>(); | ||
| typeFeatureProviderMock.Setup(m => m.GetFeatureForDependency(It.IsAny<Type>())) | ||
| .Returns(featureInfo.Object); | ||
|
|
||
| var extensionManagerMock = new Mock<IExtensionManager>(); | ||
| extensionManagerMock.Setup(m => m.GetFeatureDependencies(It.IsAny<string>())) | ||
| .Returns(Enumerable.Empty<IFeatureInfo>()); | ||
|
|
||
| var sessionMock = new Mock<ISession>(); | ||
| sessionMock.Setup(s => s.BeginTransactionAsync()) | ||
| .ReturnsAsync(Mock.Of<DbTransaction>()); | ||
|
|
||
| sessionMock.Setup(s => s.Query()) | ||
| .Returns(new FakeQuery()); | ||
|
|
||
| sessionMock.Setup(s => s.SaveAsync(It.IsAny<object>())) | ||
| .Returns(Task.CompletedTask); | ||
|
|
||
| var storeMock = new Mock<IStore>(); | ||
| storeMock.Setup(s => s.Configuration).Returns(new Configuration()); | ||
|
|
||
| return new DataMigrationManager( | ||
| typeFeatureProviderMock.Object, | ||
| dataMigrations, | ||
| sessionMock.Object, | ||
| storeMock.Object, | ||
| extensionManagerMock.Object, | ||
| NullLogger<DataMigrationManager>.Instance); | ||
| } | ||
|
|
||
| private sealed class Migration1 : DataMigration | ||
| { | ||
| public bool CreateCalled { get; private set; } | ||
|
|
||
| public bool UninstallCalled { get; private set; } | ||
|
|
||
| public int UpdateFromCalls { get; private set; } | ||
|
|
||
| public int Create() | ||
| { | ||
| CreateCalled = true; | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| public int UpdateFrom1() | ||
| { | ||
| ++UpdateFromCalls; | ||
|
|
||
| return 2; | ||
| } | ||
|
|
||
| public Task<int> UpdateFrom2Async() | ||
| { | ||
| ++UpdateFromCalls; | ||
|
|
||
| return Task.FromResult(3); | ||
| } | ||
|
|
||
| #pragma warning disable CA1822 // Mark members as static | ||
| public int UpdateFromInvalid() => 0; | ||
| #pragma warning restore CA1822 // Mark members as static | ||
|
|
||
| public void Uninstall() => UninstallCalled = true; | ||
| } | ||
|
|
||
| private sealed class Migration2 : DataMigration | ||
| { | ||
| public bool CreateCalled { get; private set; } | ||
|
|
||
| public bool UninstallCalled { get; private set; } | ||
|
|
||
| public int UpdateFromCalls { get; private set; } | ||
|
|
||
| public Task<int> CreateAsync() | ||
| { | ||
| CreateCalled = true; | ||
|
|
||
| return Task.FromResult(1); | ||
| } | ||
|
|
||
| public Task UninstallAsync() | ||
| { | ||
| UninstallCalled = true; | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
|
|
||
| private sealed class FakeQuery : IQuery | ||
| { | ||
| public IQuery<object> Any() | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public IQuery<T> For<T>(bool filterType = true) where T : class => new FakeQuery<T>(); | ||
|
|
||
| IQueryIndex<T> IQuery.ForIndex<T>() | ||
| => throw new NotImplementedException(); | ||
| } | ||
|
|
||
| private sealed class FakeQuery<T> : IQuery<T> where T : class | ||
| { | ||
| public IQuery<T> All(params Func<IQuery<T>, IQuery<T>>[] predicates) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public ValueTask<IQuery<T>> AllAsync(params Func<IQuery<T>, ValueTask<IQuery<T>>>[] predicates) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public IQuery<T> Any(params Func<IQuery<T>, IQuery<T>>[] predicates) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public ValueTask<IQuery<T>> AnyAsync(params Func<IQuery<T>, ValueTask<IQuery<T>>>[] predicates) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public Task<int> CountAsync(CancellationToken cancellationToken = default) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public Task<int> CountAsync() | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public Task<T> FirstOrDefaultAsync(CancellationToken cancellationToken = default) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public Task<T> FirstOrDefaultAsync() => Task.FromResult((T)null); | ||
|
|
||
| public string GetTypeAlias(Type t) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public Task<IEnumerable<T>> ListAsync(CancellationToken cancellationToken = default) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public Task<IEnumerable<T>> ListAsync() | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public IQuery<T> NoDuplicates() | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public IQuery<T> Skip(int count) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public IQuery<T> Take(int count) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public IAsyncEnumerable<T> ToAsyncEnumerable(CancellationToken cancellationToken = default) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public IAsyncEnumerable<T> ToAsyncEnumerable() | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public IQuery<T> With(Type indexType) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| IQuery<T, TIndex> IQuery<T>.With<TIndex>() | ||
| => throw new NotImplementedException(); | ||
|
|
||
| IQuery<T, TIndex> IQuery<T>.With<TIndex>(System.Linq.Expressions.Expression<Func<TIndex, bool>> predicate) | ||
| => throw new NotImplementedException(); | ||
| } | ||
| } |
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 could merge both methods instead