Skip to content
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

[1.x] Perform clear-and-replace when applying entity Migration #1405

Merged
merged 5 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions license-report.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


# Dependencies of `io.spine:spine-client:1.7.7-SNAPSHOT.2`
# Dependencies of `io.spine:spine-client:1.7.7-SNAPSHOT.3`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -399,12 +399,12 @@
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Thu Oct 21 16:54:26 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Sat Oct 23 20:16:51 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-core:1.7.7-SNAPSHOT.2`
# Dependencies of `io.spine:spine-core:1.7.7-SNAPSHOT.3`

## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
Expand Down Expand Up @@ -763,12 +763,12 @@ This report was generated on **Thu Oct 21 16:54:26 EEST 2021** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Thu Oct 21 16:54:26 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Sat Oct 23 20:16:52 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.tools:spine-model-assembler:1.7.7-SNAPSHOT.2`
# Dependencies of `io.spine.tools:spine-model-assembler:1.7.7-SNAPSHOT.3`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -1162,12 +1162,12 @@ This report was generated on **Thu Oct 21 16:54:26 EEST 2021** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Thu Oct 21 16:54:27 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Sat Oct 23 20:16:52 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.tools:spine-model-verifier:1.7.7-SNAPSHOT.2`
# Dependencies of `io.spine.tools:spine-model-verifier:1.7.7-SNAPSHOT.3`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -1627,12 +1627,12 @@ This report was generated on **Thu Oct 21 16:54:27 EEST 2021** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Thu Oct 21 16:54:27 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Sat Oct 23 20:16:53 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-server:1.7.7-SNAPSHOT.2`
# Dependencies of `io.spine:spine-server:1.7.7-SNAPSHOT.3`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -2039,12 +2039,12 @@ This report was generated on **Thu Oct 21 16:54:27 EEST 2021** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Thu Oct 21 16:54:27 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Sat Oct 23 20:16:53 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-testutil-client:1.7.7-SNAPSHOT.2`
# Dependencies of `io.spine:spine-testutil-client:1.7.7-SNAPSHOT.3`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -2493,12 +2493,12 @@ This report was generated on **Thu Oct 21 16:54:27 EEST 2021** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Thu Oct 21 16:54:29 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Sat Oct 23 20:16:55 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-testutil-core:1.7.7-SNAPSHOT.2`
# Dependencies of `io.spine:spine-testutil-core:1.7.7-SNAPSHOT.3`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -2947,12 +2947,12 @@ This report was generated on **Thu Oct 21 16:54:29 EEST 2021** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Thu Oct 21 16:54:30 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Sat Oct 23 20:16:56 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-testutil-server:1.7.7-SNAPSHOT.2`
# Dependencies of `io.spine:spine-testutil-server:1.7.7-SNAPSHOT.3`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -3445,4 +3445,4 @@ This report was generated on **Thu Oct 21 16:54:30 EEST 2021** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Thu Oct 21 16:54:32 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Sat Oct 23 20:16:58 EEST 2021** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ all modules and does not describe the project structure per-subproject.

<groupId>io.spine</groupId>
<artifactId>spine-core-java</artifactId>
<version>1.7.7-SNAPSHOT.2</version>
<version>1.7.7-SNAPSHOT.3</version>

<inceptionYear>2015</inceptionYear>

Expand Down
4 changes: 3 additions & 1 deletion server/src/main/java/io/spine/server/entity/Migration.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ private Operation(E entity, RecordBasedRepository<I, E, S> repository) {

private void updateState(S newState) {
if (!entity.state().equals(newState)) {
tx.builder().mergeFrom(newState);
tx.builder()
.clear()
.mergeFrom(newState);
Version version = increment(entity.version());
tx.setVersion(version);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import io.spine.server.procman.given.delivery.GivenMessage;
import io.spine.server.procman.given.repo.EventDiscardingProcManRepository;
import io.spine.server.procman.given.repo.ProjectCompletion;
import io.spine.server.procman.given.repo.RandomFillProcess;
import io.spine.server.procman.given.repo.RememberingSubscriber;
import io.spine.server.procman.given.repo.SensoryDeprivedPmRepository;
import io.spine.server.procman.given.repo.SetTestProcessId;
Expand Down Expand Up @@ -125,6 +126,7 @@
import static io.spine.server.procman.given.repo.GivenCommandMessage.projectStarted;
import static io.spine.server.procman.given.repo.GivenCommandMessage.startProject;
import static io.spine.server.procman.given.repo.GivenCommandMessage.taskAdded;
import static io.spine.server.procman.given.repo.SetTestProcessName.NEW_NAME;
import static io.spine.testing.TestValues.randomString;
import static io.spine.testing.server.Assertions.assertCommandClasses;
import static io.spine.testing.server.Assertions.assertEventClasses;
Expand Down Expand Up @@ -712,7 +714,7 @@ void performCustomMigrationForMultiple() {
repository.store(pm3);

// Init filters by the `name` column.
TargetFilters filters = targetFilters(Project.Column.name(), SetTestProcessName.NEW_NAME);
TargetFilters filters = targetFilters(Project.Column.name(), NEW_NAME);

// Check nothing is found as the entity states were not yet updated.
Iterator<TestProcessManager> found =
Expand All @@ -727,24 +729,41 @@ void performCustomMigrationForMultiple() {
repository.find(filters, ResponseFormat.getDefaultInstance());

ImmutableList<TestProcessManager> results = ImmutableList.copyOf(foundAfterMigration);
Project expectedState1 = pm1
.state()
.toBuilder()
.setName(SetTestProcessName.NEW_NAME)
.setIdString(pm1.getIdString())
.build();
Project expectedState2 = pm2
.state()
.toBuilder()
.setName(SetTestProcessName.NEW_NAME)
.setIdString(pm2.getIdString())
.build();
Project expectedState1 = expectedState(pm1, NEW_NAME);
Project expectedState2 = expectedState(pm2, NEW_NAME);
assertThat(results).hasSize(2);
assertThat(results)
.comparingElementsUsing(entityState())
.containsExactly(expectedState1, expectedState2);
}

@Test
@DisplayName("replace the state of the migrated process")
void replaceState() {
ProjectId id = createId(42);
TestProcessManager entity = new TestProcessManager(id);
TestProcessManagerRepository repository = repository();
repository.store(entity);
repository.applyMigration(id, new RandomFillProcess());
TargetFilters byNewName = filterByName(NEW_NAME);

// Ensure nothing found.
Iterator<TestProcessManager> expectedEmpty =
repository.find(byNewName, ResponseFormat.getDefaultInstance());
assertThat(expectedEmpty.hasNext()).isFalse();

repository.applyMigration(id, new SetTestProcessName());

// Now we should have found a single instance.
Iterator<TestProcessManager> shouldHaveOne =
repository.find(byNewName, ResponseFormat.getDefaultInstance());
Project expectedState = expectedState(entity, NEW_NAME);
ImmutableList<TestProcessManager> actualList = ImmutableList.copyOf(shouldHaveOne);
assertThat(actualList)
.comparingElementsUsing(entityState())
.containsExactly(expectedState);
}

@Test
@DisplayName("update columns via migration operation")
void updateColumns() {
Expand Down Expand Up @@ -882,6 +901,20 @@ private static TargetFilters targetFilters(QueryFilter first, QueryFilter... res
.build();
}

private static TargetFilters filterByName(String name) {
return targetFilters(Project.Column.name(), name);
}

private static Project expectedState(TestProcessManager entity, String newName) {
return entity
.state()
.toBuilder()
.setId(entity.id())
.setName(newName)
.setIdString(entity.getIdString())
.build();
}

private static Correspondence<TestProcessManager, Project> entityState() {
return Correspondence.from(ProcessManagerRepositoryTest::hasState, "has state");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2021, TeamDev. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.server.procman.given.repo;

import io.spine.server.procman.ProcessManagerMigration;
import io.spine.test.procman.Project;
import io.spine.test.procman.ProjectId;
import io.spine.test.procman.Task;
import io.spine.test.procman.TaskId;

import static io.spine.base.Identifier.newUuid;
import static io.spine.test.procman.Project.Status.DONE;

/**
* Fills the state of the migrated {@code ProcessManager} with some random values.
*/
public class RandomFillProcess
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class RandomFillProcess
public final class RandomFillProcess

extends ProcessManagerMigration<ProjectId, TestProcessManager, Project, Project.Builder> {

@Override
public Project apply(Project project) {
Project newState = project
.toBuilder()
.setId(project.getId())
.setName(newUuid())
.setStatus(DONE)
.addTask(task())
.vBuild();
return newState;
}

private static Task task() {
return Task.newBuilder()
.setTaskId(taskId())
.setTitle(newUuid())
.setDescription(newUuid())
.vBuild();
}

private static TaskId taskId() {
return TaskId.newBuilder()
.setId(newUuid().hashCode())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public final class SetTestProcessName
public Project apply(Project project) {
Project newState = project
.toBuilder()
.clear()
.setId(id())
.setName(NEW_NAME)
.build();
return newState;
Expand Down
Loading