Skip to content

Commit

Permalink
refactor(database): Refactor database migration to constructor CDI (#856
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MartinWitt authored Jul 16, 2023
1 parent e208bbe commit 05c9daa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,29 @@ public class DataBaseMigration {

private static final FluentLogger logger = FluentLogger.forEnclosingClass();

@Inject
ProjectConfigRepository projectConfigRepository;

@Inject
ProjectRepository projectRepository;

@Inject
BadSmellRepository badSmellRepository;
// we use this for faster mongodb access
@Inject
MongoBadSmellRepository badSmellRepositoryImpl;

@Inject
MongoProjectRepository projectRepositoryImpl;
Vertx vertx;

@Inject
Vertx vertx;
public DataBaseMigration(
ProjectConfigRepository projectConfigRepository,
ProjectRepository projectRepository,
BadSmellRepository badSmellRepository,
MongoBadSmellRepository badSmellRepositoryImpl,
MongoProjectRepository projectRepositoryImpl,
Vertx vertx) {
this.projectConfigRepository = projectConfigRepository;
this.projectRepository = projectRepository;
this.badSmellRepository = badSmellRepository;
this.badSmellRepositoryImpl = badSmellRepositoryImpl;
this.projectRepositoryImpl = projectRepositoryImpl;
this.vertx = vertx;
}

/**
* This method is called by the quarkus framework to migrate the database.
Expand All @@ -56,10 +62,9 @@ public void onStart(@Observes StartupEvent event) {
}

public void checkPeriodic() {
vertx.setPeriodic(
TimeUnit.MINUTES.toMillis(2),
TimeUnit.MINUTES.toMillis(60),
id -> vertx.executeBlocking(promise -> migrateDataBase(promise)));
vertx.setPeriodic(TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(60), id -> vertx.executeBlocking(
promise -> migrateDataBase(promise))
.onFailure(v -> logger.atSevere().withCause(v).log("Error while migrating database")));
}

private void migrateDataBase(Promise<Object> promise) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import spoon.reflect.code.CtLiteral;
import spoon.reflect.declaration.CtModifiable;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;

/**
* A utility class for creating matchers for Spoon elements.
Expand Down Expand Up @@ -68,6 +70,27 @@ public static Matcher<CtModifiable> isStatic() {
return v -> v.isStatic();
}

/**
* Returns a matcher that matches elements that are subtypes of the given class.
* @param fqClassname fully qualified classname of the class to match e.g. java.lang.String
* @param factory spoon factory to create a reference to the class
* @return a matcher that matches elements that are subtypes of the given class
*/
public static Matcher<CtTypedElement<?>> isSubtypeOf(String fqClassname, Factory factory) {
return v ->
v.getType() != null && v.getType().isSubtypeOf(factory.Type().createReference(fqClassname));
}

/**
* Returns a matcher that matches elements that are the type.
* @param fqClassname fully qualified classname of the class to match e.g. java.lang.String
* @param factory spoon factory to create a reference to the class
* @return a matcher that matches elements that are the same type as the matched element
*/
public static Matcher<CtTypedElement<?>> isType(String fqClassname, Factory factory) {
return v -> v.getType() != null && v.getType().getQualifiedName().equals(fqClassname);
}

/**
* Returns a matcher that matches elements that match all of the given matchers.
*
Expand Down

0 comments on commit 05c9daa

Please sign in to comment.