-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[#14312] Add more developer friendly schema validation error #14313
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
TobyCyan
wants to merge
26
commits into
TEAMMATES:master
Choose a base branch
from
TobyCyan:more-friendly-schema-validation-error
base: master
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.
Open
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
24eb254
Throw start up exception with custom message and stop server
TobyCyan 460f1d1
Create dev server error handling system
TobyCyan ac3cff9
Rename package
TobyCyan 8f9b533
Rename error handler list and make private
TobyCyan 5e4037d
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan e4f4b8a
Handle exception instead of throwable
TobyCyan 793df49
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan 5f36b11
Commit suggestions
TobyCyan e0a02dc
Simplify error handling
TobyCyan 10a0583
Remove package from testng
TobyCyan addee80
Fail fast based on liquibase status
TobyCyan e7718f1
Move packages into main
TobyCyan ce35882
Use liquibase API
TobyCyan f3b2c98
Move status checker to liquibase package
TobyCyan 229170d
Use instanceof to determine exception
TobyCyan 908a88f
Fix pmd
TobyCyan 0f78125
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan 231501d
Revert package comment
TobyCyan 885715e
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan d3bf228
Upgrade liquibase version
TobyCyan 553090d
Normalize changeset file paths
TobyCyan fafd51e
Remove liquibase core testImplementation dependency
TobyCyan fc63779
Extract transform start up exception
TobyCyan 8d7190e
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan 947dabf
Revert liquibase core version
TobyCyan 717f6e6
Force commons-lang3 version
TobyCyan 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
19 changes: 19 additions & 0 deletions
19
src/main/java/teammates/common/exception/PendingDatabaseMigrationsException.java
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,19 @@ | ||
| package teammates.common.exception; | ||
|
|
||
| /** | ||
| * Exception thrown when there are unapplied database migrations. | ||
| */ | ||
| public class PendingDatabaseMigrationsException extends Exception { | ||
|
|
||
| private final String migrationStatus; | ||
|
|
||
| public PendingDatabaseMigrationsException(String message, String migrationStatus) { | ||
| super(message); | ||
| this.migrationStatus = migrationStatus; | ||
| } | ||
|
|
||
| public String getMigrationStatus() { | ||
| return migrationStatus; | ||
| } | ||
|
|
||
| } |
94 changes: 94 additions & 0 deletions
94
src/main/java/teammates/liquibase/LiquibaseStatusChecker.java
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,94 @@ | ||
| package teammates.liquibase; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import teammates.common.exception.PendingDatabaseMigrationsException; | ||
| import teammates.common.util.Config; | ||
|
|
||
| import liquibase.Contexts; | ||
| import liquibase.LabelExpression; | ||
| import liquibase.Liquibase; | ||
| import liquibase.changelog.ChangeSet; | ||
| import liquibase.database.Database; | ||
| import liquibase.database.DatabaseFactory; | ||
| import liquibase.exception.DatabaseException; | ||
| import liquibase.exception.LiquibaseException; | ||
| import liquibase.resource.ClassLoaderResourceAccessor; | ||
| import liquibase.resource.ResourceAccessor; | ||
|
|
||
| /** | ||
| * Checks whether the liquibase status is successful. | ||
| */ | ||
| public final class LiquibaseStatusChecker { | ||
|
|
||
| private static final String CHANGELOG_FILE = "db/changelog/db.changelog-root.xml"; | ||
| private static final String GRADLE_CHANGELOG_FILE_PREFIX = "src/main/resources/"; | ||
|
|
||
| private LiquibaseStatusChecker() { | ||
| // utility class | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that liquibase reports successful status. | ||
| */ | ||
| public static void assertSuccessStatus() throws LiquibaseException, PendingDatabaseMigrationsException { | ||
| assertDatabaseUpToDate(); | ||
| } | ||
|
|
||
| /** | ||
| * Fails fast when Liquibase reports unapplied changesets. | ||
| */ | ||
| private static void assertDatabaseUpToDate() throws LiquibaseException, PendingDatabaseMigrationsException { | ||
| Optional<String> pendingMigrationStatus = getPendingMigrationStatus(); | ||
| if (pendingMigrationStatus.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| throw new PendingDatabaseMigrationsException( | ||
| "Database schema is out of date. Apply pending Liquibase migrations before starting the server.", | ||
| pendingMigrationStatus.get()); | ||
| } | ||
|
|
||
| private static Optional<String> getPendingMigrationStatus() throws LiquibaseException { | ||
| List<ChangeSet> unrunChangeSets = listUnrunChangeSets(); | ||
| return unrunChangeSets.isEmpty() | ||
| ? Optional.empty() | ||
| : Optional.of(formatPendingMigrationStatus(unrunChangeSets)); | ||
| } | ||
|
|
||
| private static List<ChangeSet> listUnrunChangeSets() throws LiquibaseException { | ||
| try (ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(); | ||
| Liquibase liquibase = createLiquibase(resourceAccessor)) { | ||
| normalizeChangeSetFilePaths(liquibase); | ||
| return liquibase.listUnrunChangeSets(new Contexts(), new LabelExpression()); | ||
| } catch (Exception e) { | ||
| throw new LiquibaseException("Failed to check Liquibase status", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Normalizes file paths to match the paths used in the Gradle build, | ||
| * so that the unrun changesets can be correctly identified. | ||
| */ | ||
| private static void normalizeChangeSetFilePaths(Liquibase liquibase) throws LiquibaseException { | ||
| for (ChangeSet changeSet : liquibase.getDatabaseChangeLog().getChangeSets()) { | ||
| changeSet.setFilePath(GRADLE_CHANGELOG_FILE_PREFIX + changeSet.getFilePath()); | ||
| } | ||
| } | ||
|
|
||
| private static Liquibase createLiquibase(ResourceAccessor resourceAccessor) throws DatabaseException { | ||
| Database database = DatabaseFactory.getInstance().openDatabase( | ||
| Config.getDbConnectionUrl(), | ||
| Config.POSTGRES_USERNAME, | ||
| Config.POSTGRES_PASSWORD, | ||
| null, | ||
| resourceAccessor); | ||
| return new Liquibase(CHANGELOG_FILE, resourceAccessor, database); | ||
| } | ||
|
|
||
| private static String formatPendingMigrationStatus(List<ChangeSet> unrunChangeSets) { | ||
| return unrunChangeSets.size() + " pending Liquibase changeset(s)."; | ||
| } | ||
|
|
||
| } |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/teammates/main/exception/DevServerStartupException.java
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,13 @@ | ||
| package teammates.main.exception; | ||
|
|
||
| /** | ||
| * Exception thrown when an error occurs during dev server startup. | ||
| */ | ||
| public class DevServerStartupException extends RuntimeException { | ||
|
|
||
| public DevServerStartupException(String message) { | ||
| // Null cause to avoid printing the stack trace twice. | ||
| super(message, null, true, false); | ||
| } | ||
|
|
||
| } |
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,5 @@ | ||
|
|
||
| /** | ||
| * Contains exception classes for the main package. | ||
| */ | ||
| package teammates.main.exception; |
63 changes: 63 additions & 0 deletions
63
src/main/java/teammates/main/util/DevServerStartupErrorHandler.java
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,63 @@ | ||
| package teammates.main.util; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
|
|
||
| import teammates.common.exception.PendingDatabaseMigrationsException; | ||
| import teammates.main.exception.DevServerStartupException; | ||
|
|
||
| /** | ||
| * Handles startup errors with dev-server-friendly messages where possible. | ||
| */ | ||
| public final class DevServerStartupErrorHandler { | ||
|
|
||
| private static final String SCHEMA_OUT_OF_DATE_MESSAGE = String.join(System.lineSeparator(), | ||
| "", | ||
| "============================================================", | ||
| "Database schema is not up to date:", | ||
| "%s", | ||
| "", | ||
| "Your local database schema is out of date.", | ||
| "Run: ./gradlew liquibaseUpdate", | ||
| "Then restart the local server.", | ||
| "", | ||
| "For more details, see docs/how-to/schema-migration.md", | ||
| "============================================================", | ||
| ""); | ||
|
|
||
| /** | ||
| * A list of builders that can recognize startup errors and provide a dev-server-friendly message. | ||
| */ | ||
| private static final List<Function<Throwable, Optional<String>>> ERROR_MESSAGE_BUILDERS = List.of( | ||
| DevServerStartupErrorHandler::buildPendingDatabaseMigrationsMessage); | ||
|
|
||
| private DevServerStartupErrorHandler() { | ||
| // Utility class | ||
| } | ||
|
|
||
| /** | ||
| * Transforms a recognized startup error into a dev-server-friendly exception. | ||
| * Returns the original exception when no builder recognizes it. | ||
| */ | ||
| public static Exception transform(Exception e) { | ||
| return ERROR_MESSAGE_BUILDERS.stream() | ||
| .map(builder -> builder.apply(e)) | ||
| .flatMap(optional -> optional.stream()) | ||
| .findFirst() | ||
| .<Exception>map(message -> new DevServerStartupException(message)) | ||
| .orElse(e); | ||
| } | ||
|
|
||
| /** | ||
| * Builds a dev-server-friendly message for a pending database migrations error. | ||
| */ | ||
| private static Optional<String> buildPendingDatabaseMigrationsMessage(Throwable error) { | ||
| if (!(error instanceof PendingDatabaseMigrationsException)) { | ||
| return Optional.empty(); | ||
| } | ||
| PendingDatabaseMigrationsException pendingMigrationsFailure = (PendingDatabaseMigrationsException) error; | ||
| return Optional.of(String.format(SCHEMA_OUT_OF_DATE_MESSAGE, pendingMigrationsFailure.getMigrationStatus())); | ||
| } | ||
|
|
||
| } |
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,4 @@ | ||
| /** | ||
| * Contains utility classes for the main package. | ||
| */ | ||
| package teammates.main.util; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.