diff --git a/server/auth.go b/server/auth.go index 88a8937f2bd14..ba67872e61b49 100644 --- a/server/auth.go +++ b/server/auth.go @@ -157,6 +157,9 @@ func (s *Server) createUserAuthSignInActivity(c echo.Context, user *api.User) er Level: api.ActivityInfo, Payload: string(payloadStr), }) + if err != nil || activity == nil { + return errors.Wrap(err, "failed to create activity") + } s.Collector.Collect(ctx, &metric.Metric{ Name: string(activity.Type), }) @@ -179,6 +182,9 @@ func (s *Server) createUserAuthSignUpActivity(c echo.Context, user *api.User) er Level: api.ActivityInfo, Payload: string(payloadStr), }) + if err != nil || activity == nil { + return errors.Wrap(err, "failed to create activity") + } s.Collector.Collect(ctx, &metric.Metric{ Name: string(activity.Type), }) diff --git a/server/memo.go b/server/memo.go index 63538992ff801..13d9b652ee5f9 100644 --- a/server/memo.go +++ b/server/memo.go @@ -583,6 +583,9 @@ func (s *Server) createMemoCreateActivity(c echo.Context, memo *api.Memo) error Level: api.ActivityInfo, Payload: string(payloadStr), }) + if err != nil || activity == nil { + return errors.Wrap(err, "failed to create activity") + } s.Collector.Collect(ctx, &metric.Metric{ Name: string(activity.Type), }) diff --git a/server/resource.go b/server/resource.go index f46d1ece753ac..ca067da38304f 100644 --- a/server/resource.go +++ b/server/resource.go @@ -299,6 +299,9 @@ func (s *Server) createResourceCreateActivity(c echo.Context, resource *api.Reso Level: api.ActivityInfo, Payload: string(payloadStr), }) + if err != nil || activity == nil { + return errors.Wrap(err, "failed to create activity") + } s.Collector.Collect(ctx, &metric.Metric{ Name: string(activity.Type), }) diff --git a/server/server.go b/server/server.go index 9df7ce5a3dd45..62baacf983cfc 100644 --- a/server/server.go +++ b/server/server.go @@ -146,6 +146,9 @@ func (s *Server) createServerStartActivity(ctx context.Context) error { Level: api.ActivityInfo, Payload: string(payloadStr), }) + if err != nil || activity == nil { + return errors.Wrap(err, "failed to create activity") + } s.Collector.Collect(ctx, &metric.Metric{ Name: string(activity.Type), }) diff --git a/server/shortcut.go b/server/shortcut.go index 73705886a88cb..3d5c585d0eb3e 100644 --- a/server/shortcut.go +++ b/server/shortcut.go @@ -175,11 +175,14 @@ func (s *Server) createShortcutCreateActivity(c echo.Context, shortcut *api.Shor if err != nil { return errors.Wrap(err, "failed to marshal activity payload") } - _, err = s.Store.CreateActivity(ctx, &api.ActivityCreate{ + activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{ CreatorID: shortcut.CreatorID, Type: api.ActivityShortcutCreate, Level: api.ActivityInfo, Payload: string(payloadStr), }) + if err != nil || activity == nil { + return errors.Wrap(err, "failed to create activity") + } return err } diff --git a/server/tag.go b/server/tag.go index f01ebe7a61d57..e0ca844e13201 100644 --- a/server/tag.go +++ b/server/tag.go @@ -165,11 +165,14 @@ func (s *Server) createTagCreateActivity(c echo.Context, tag *api.Tag) error { if err != nil { return errors.Wrap(err, "failed to marshal activity payload") } - _, err = s.Store.CreateActivity(ctx, &api.ActivityCreate{ + activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{ CreatorID: tag.CreatorID, Type: api.ActivityTagCreate, Level: api.ActivityInfo, Payload: string(payloadStr), }) + if err != nil || activity == nil { + return errors.Wrap(err, "failed to create activity") + } return err } diff --git a/server/user.go b/server/user.go index a1afd7695cd59..8a5fbb6555f98 100644 --- a/server/user.go +++ b/server/user.go @@ -296,6 +296,9 @@ func (s *Server) createUserCreateActivity(c echo.Context, user *api.User) error Level: api.ActivityInfo, Payload: string(payloadStr), }) + if err != nil || activity == nil { + return errors.Wrap(err, "failed to create activity") + } s.Collector.Collect(ctx, &metric.Metric{ Name: string(activity.Type), }) diff --git a/store/db/db.go b/store/db/db.go index e6c47593f2a2b..2694bcbd7f22d 100644 --- a/store/db/db.go +++ b/store/db/db.go @@ -68,11 +68,11 @@ func (db *DB) Open(ctx context.Context) (err error) { } currentVersion := version.GetCurrentVersion(db.profile.Mode) - migrationHistory, err := db.FindMigrationHistory(ctx, &MigrationHistoryFind{}) + migrationHistoryList, err := db.FindMigrationHistoryList(ctx, &MigrationHistoryFind{}) if err != nil { return fmt.Errorf("failed to find migration history, err: %w", err) } - if migrationHistory == nil { + if len(migrationHistoryList) == 0 { if _, err = db.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{ Version: currentVersion, }); err != nil { @@ -80,8 +80,14 @@ func (db *DB) Open(ctx context.Context) (err error) { } return nil } + migrationHistoryVersionList := []string{} + for _, migrationHistory := range migrationHistoryList { + migrationHistoryVersionList = append(migrationHistoryVersionList, migrationHistory.Version) + } + sort.Strings(migrationHistoryVersionList) + latestMigrationHistoryVersion := migrationHistoryVersionList[0] - if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), migrationHistory.Version) { + if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), latestMigrationHistoryVersion) { minorVersionList := getMinorVersionList() // backup the raw database file before migration @@ -98,7 +104,7 @@ func (db *DB) Open(ctx context.Context) (err error) { println("start migrate") for _, minorVersion := range minorVersionList { normalizedVersion := minorVersion + ".0" - if version.IsVersionGreaterThan(normalizedVersion, migrationHistory.Version) && version.IsVersionGreaterOrEqualThan(currentVersion, normalizedVersion) { + if version.IsVersionGreaterThan(normalizedVersion, latestMigrationHistoryVersion) && version.IsVersionGreaterOrEqualThan(currentVersion, normalizedVersion) { println("applying migration for", normalizedVersion) if err := db.applyMigrationForMinorVersion(ctx, minorVersion); err != nil { return fmt.Errorf("failed to apply minor version migration: %w", err) diff --git a/store/db/migration_history.go b/store/db/migration_history.go index 33e95c5ec2374..5906e52436208 100644 --- a/store/db/migration_history.go +++ b/store/db/migration_history.go @@ -19,7 +19,7 @@ type MigrationHistoryFind struct { Version *string } -func (db *DB) FindMigrationHistory(ctx context.Context, find *MigrationHistoryFind) (*MigrationHistory, error) { +func (db *DB) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) { tx, err := db.DBInstance.BeginTx(ctx, nil) if err != nil { return nil, err @@ -31,12 +31,7 @@ func (db *DB) FindMigrationHistory(ctx context.Context, find *MigrationHistoryFi return nil, err } - if len(list) == 0 { - return nil, nil - } - - migrationHistory := list[0] - return migrationHistory, nil + return list, nil } func (db *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {