-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d3a29a
commit 922b865
Showing
25 changed files
with
1,029 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
56 changes: 56 additions & 0 deletions
56
neo4j-migrations-cli/src/main/java/ac/simons/neo4j/migrations/cli/ValidateCommand.java
This file contains 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,56 @@ | ||
/* | ||
* Copyright 2020-2021 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ac.simons.neo4j.migrations.cli; | ||
|
||
import ac.simons.neo4j.migrations.core.Migrations; | ||
import ac.simons.neo4j.migrations.core.ValidationResult; | ||
import ac.simons.neo4j.migrations.core.utils.Messages; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.ParentCommand; | ||
|
||
/** | ||
* The validate command. | ||
* | ||
* @author Michael J. Simons | ||
* @soundtrack Sugarcubes - Life's Too Good | ||
* @since 1.2.0 | ||
*/ | ||
@Command(name = "validate", description = "Resolves all local migrations and validates the state of the configured database with them.") | ||
final class ValidateCommand extends ConnectedCommand { | ||
|
||
@ParentCommand | ||
private MigrationsCli parent; | ||
|
||
@Override | ||
public MigrationsCli getParent() { | ||
return parent; | ||
} | ||
|
||
@Override | ||
Integer withMigrations(Migrations migrations) { | ||
|
||
ValidationResult validationResult = migrations.validate(); | ||
MigrationsCli.LOGGER.info(validationResult::prettyPrint); | ||
boolean isValid = validationResult.isValid(); | ||
if (!isValid) { | ||
validationResult.getWarnings().forEach(MigrationsCli.LOGGER::info); | ||
MigrationsCli.LOGGER.info(Messages.INSTANCE.get(validationResult.needsRepair() ? | ||
"validation.database_needs_repair" : | ||
"validation.database_is_invalid")); | ||
} | ||
return isValid ? 0 : 1; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
neo4j-migrations-cli/src/test/java/ac/simons/neo4j/migrations/cli/ValidateCommandTest.java
This file contains 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,72 @@ | ||
/* | ||
* Copyright 2020-2021 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ac.simons.neo4j.migrations.cli; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import ac.simons.neo4j.migrations.core.Migrations; | ||
import ac.simons.neo4j.migrations.core.ValidationResult; | ||
|
||
import java.util.Collections; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
/** | ||
* @soundtrack Sugarcubes - Life's Too Good | ||
*/ | ||
class ValidateCommandTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = { 0, 1, 2 }) | ||
void shouldInvokeValidate(int levelOfFailure) { | ||
|
||
boolean makeItFail = levelOfFailure > 0; | ||
|
||
Migrations migrations = mock(Migrations.class); | ||
ValidationResult result = mock(ValidationResult.class); | ||
when(result.prettyPrint()).thenReturn("things have been validated"); | ||
if (makeItFail) { | ||
when(result.getWarnings()).thenReturn(Collections.singletonList("a warning")); | ||
when(result.isValid()).thenReturn(false); | ||
if (levelOfFailure == 2) { | ||
when(result.needsRepair()).thenReturn(true); | ||
} | ||
} else { | ||
when(result.isValid()).thenReturn(true); | ||
} | ||
when(migrations.validate()).thenReturn(result); | ||
|
||
ValidateCommand cmd = new ValidateCommand(); | ||
|
||
int exitCode = cmd.withMigrations(migrations); | ||
assertThat(exitCode).isEqualTo(makeItFail ? 1 : 0); | ||
|
||
verify(migrations).validate(); | ||
verify(result).isValid(); | ||
verify(result).prettyPrint(); | ||
if (makeItFail) { | ||
verify(result).needsRepair(); | ||
verify(result).getWarnings(); | ||
} | ||
|
||
verifyNoMoreInteractions(migrations, result); | ||
} | ||
} |
This file contains 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
This file contains 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
34 changes: 34 additions & 0 deletions
34
...igrations-core/src/main/java/ac/simons/neo4j/migrations/core/DatabaseOperationResult.java
This file contains 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,34 @@ | ||
/* | ||
* Copyright 2020-2021 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ac.simons.neo4j.migrations.core; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* A specialization of the {@link OperationResult} that always affects a database, either the default one | ||
* or a named other. | ||
* | ||
* @author Michael J. Simons | ||
* @soundtrack Die Krupps - Paradise Now | ||
* @since 1.2.0 | ||
*/ | ||
public interface DatabaseOperationResult extends OperationResult { | ||
|
||
/** | ||
* @return the optional name of the database clean, an empty optional indicates the default database | ||
*/ | ||
Optional<String> getAffectedDatabase(); | ||
} |
This file contains 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
Oops, something went wrong.