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

fix: all previously published moments show as under review state after upgrade #102

Merged
merged 1 commit into from
May 7, 2024
Merged
Changes from all 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
49 changes: 49 additions & 0 deletions src/main/java/run/halo/moments/MomentMigration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package run.halo.moments;

import static run.halo.app.extension.index.query.QueryFactory.and;
import static run.halo.app.extension.index.query.QueryFactory.isNull;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import run.halo.app.extension.DefaultExtensionMatcher;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.controller.Controller;
import run.halo.app.extension.controller.ControllerBuilder;
import run.halo.app.extension.controller.Reconciler;
import run.halo.app.extension.router.selector.FieldSelector;

/**
* <p>Migration for 1.16.0, populate approved attribute for all moments before 1.16.0.</p>
* <p>Note That: This migration is only for the moments that approved attribute is null.</p>
*/
@Component
@RequiredArgsConstructor
public class MomentMigration implements Reconciler<Reconciler.Request> {

private final ExtensionClient client;

@Override
public Result reconcile(Request request) {
client.fetch(Moment.class, request.name()).ifPresent(moment -> {
moment.getSpec().setApproved(true);
moment.getSpec().setApprovedTime(moment.getMetadata().getCreationTimestamp());
client.update(moment);
});
return Result.doNotRetry();
}

@Override
public Controller setupWith(ControllerBuilder builder) {
final var moment = new Moment();
return builder
.extension(moment)
.onAddMatcher(DefaultExtensionMatcher.builder(client, moment.groupVersionKind())
.fieldSelector(FieldSelector.of(
and(isNull("spec.approved"),
isNull("metadata.deletionTimestamp"))
))
.build()
)
.build();
}
}