From 7389cfbbb9032dd0ac88f2ca68442a0cd67748c8 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Tue, 7 May 2024 18:46:42 +0800 Subject: [PATCH] fix: all previously published moments show as under review state after upgrade (#102) --- .../run/halo/moments/MomentMigration.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/run/halo/moments/MomentMigration.java diff --git a/src/main/java/run/halo/moments/MomentMigration.java b/src/main/java/run/halo/moments/MomentMigration.java new file mode 100644 index 0000000..06d7787 --- /dev/null +++ b/src/main/java/run/halo/moments/MomentMigration.java @@ -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; + +/** + *

Migration for 1.16.0, populate approved attribute for all moments before 1.16.0.

+ *

Note That: This migration is only for the moments that approved attribute is null.

+ */ +@Component +@RequiredArgsConstructor +public class MomentMigration implements Reconciler { + + 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(); + } +}