mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
fix(activitylog): store version in stream metadata
This commit is contained in:
1 parent
0c4b8f4f89
commit
fef3fc6a6f
2 files changed
+59
-5
No files matched your search
@@ -479,6 +479,19 @@ var _ = Describe("migrations", func() {
|
||||
_, err := activitylog.New(kv, activitylog.JetStream(js), activitylog.Logger(log.NopLogger()))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = kv.Get(legacyKey)
|
||||
Expect(err).To(MatchError(nats.ErrKeyNotFound))
|
||||
info, err := js.StreamInfo("KV_" + db)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(info.Config.Metadata).To(HaveKeyWithValue(activitylog.ActivityLogVersionMetadataKey, "1"))
|
||||
_, err = kv.Get(activitylog.ActivityLogVersionKey)
|
||||
Expect(err).To(MatchError(nats.ErrKeyNotFound))
|
||||
})
|
||||
|
||||
It("stores the version key when no JetStream context is configured", func() {
|
||||
_, err := activitylog.New(kv, activitylog.Logger(log.NopLogger()))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = kv.Get(legacyKey)
|
||||
Expect(err).To(MatchError(nats.ErrKeyNotFound))
|
||||
entry, err := kv.Get(activitylog.ActivityLogVersionKey)
|
||||
@@ -486,6 +499,21 @@ var _ = Describe("migrations", func() {
|
||||
Expect(string(entry.Value())).To(Equal("1"))
|
||||
})
|
||||
|
||||
It("does not migrate again after the migration stored the version in the stream metadata", func() {
|
||||
_, err := activitylog.New(kv, activitylog.JetStream(js), activitylog.Logger(log.NopLogger()))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = kv.Put("second-legacy-key", []byte("not migrated"))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = activitylog.New(kv, activitylog.JetStream(js), activitylog.Logger(log.NopLogger()))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
entry, err := kv.Get("second-legacy-key")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(string(entry.Value())).To(Equal("not migrated"))
|
||||
})
|
||||
|
||||
It("fails when the stream metadata has an unexpected version", func() {
|
||||
setStreamVersion("2")
|
||||
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
// VersionKey is the KV key holding the activitylog data version.
|
||||
// ActivityLogVersionKey is the KV key holding the activitylog data version.
|
||||
const ActivityLogVersionKey = "activitylog.version"
|
||||
|
||||
// VersionMetadataKey is the stream metadata key holding a copy of the activitylog data version.
|
||||
// ActivityLogVersionMetadataKey is the stream metadata key holding the activitylog data version.
|
||||
const ActivityLogVersionMetadataKey = "opencloud.activitylog.version"
|
||||
const currentMigrationVersion = "1"
|
||||
|
||||
@@ -65,7 +65,8 @@ func (a *ActivityLog) storedVersion(kv nats.KeyValue) (string, error) {
|
||||
// It iterates over all keys, expecting their values to be JSON arrays of strings.
|
||||
// For each such key, it creates a new key in the format "originalKey.count.timestamp"
|
||||
// and stores the original list of strings (re-marshalled to messagepack) as its value.
|
||||
// Finally, it sets the activitylog.version key to "1".
|
||||
// Finally, it sets the activitylog version to "1" in the NATS stream metadata
|
||||
// (or in the activitylog.version key if no JetStream context is configured).
|
||||
func (a *ActivityLog) migrateToV1(_ context.Context, kv nats.KeyValue) error {
|
||||
lister, err := kv.ListKeys()
|
||||
if err != nil {
|
||||
@@ -146,10 +147,35 @@ func (a *ActivityLog) migrateToV1(_ context.Context, kv nats.KeyValue) error {
|
||||
}
|
||||
|
||||
// Set the activitylog version to "1" after migration
|
||||
if _, err := kv.PutString(ActivityLogVersionKey, currentMigrationVersion); err != nil {
|
||||
return fmt.Errorf("migrateToV1: failed to set activitylog version key to '%s' in NATS KV store: %w", currentMigrationVersion, err)
|
||||
if err := a.storeVersion(kv, currentMigrationVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.log.Info().Int("migrated", migratedCount).Int("skipped", skippedCount).Msg("Migration to V1 complete")
|
||||
return nil
|
||||
}
|
||||
|
||||
// storeVersion stores the activitylog data version in the stream metadata, which is not affected by the
|
||||
// stream MaxAge. Without JetStream it falls back to the version key of the KV store.
|
||||
func (a *ActivityLog) storeVersion(kv nats.KeyValue, version string) error {
|
||||
if a.js == nil {
|
||||
if _, err := kv.PutString(ActivityLogVersionKey, version); err != nil {
|
||||
return fmt.Errorf("migrateToV1: failed to set activitylog version key to '%s' in NATS KV store: %w", version, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
info, err := a.js.StreamInfo("KV_" + kv.Bucket())
|
||||
if err != nil {
|
||||
return fmt.Errorf("migrateToV1: failed to get NATS stream info: %w", err)
|
||||
}
|
||||
cfg := info.Config
|
||||
if cfg.Metadata == nil {
|
||||
cfg.Metadata = map[string]string{}
|
||||
}
|
||||
cfg.Metadata[ActivityLogVersionMetadataKey] = version
|
||||
if _, err := a.js.UpdateStream(&cfg); err != nil {
|
||||
return fmt.Errorf("migrateToV1: failed to set activitylog version to '%s' in the NATS stream metadata: %w", version, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user