Skip to content

Commit

Permalink
fix: sort version (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack authored Jan 7, 2023
1 parent 46c13a4 commit e2e8130
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 13 deletions.
6 changes: 6 additions & 0 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand All @@ -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),
})
Expand Down
3 changes: 3 additions & 0 deletions server/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
3 changes: 3 additions & 0 deletions server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
5 changes: 4 additions & 1 deletion server/shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion server/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
14 changes: 10 additions & 4 deletions store/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,26 @@ 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 {
return fmt.Errorf("failed to upsert migration history, err: %w", err)
}
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
Expand All @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions store/db/migration_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit e2e8130

Please sign in to comment.