Skip to content

Commit

Permalink
Merge pull request #185 from gravitational/justinas/verify-tag-migrat…
Browse files Browse the repository at this point in the history
…ions

 Add verification of DB migrations for access-graph
  • Loading branch information
justinas authored Dec 5, 2023
2 parents 4009e21 + 29464f5 commit 7ade89f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
40 changes: 27 additions & 13 deletions bot/internal/bot/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,40 @@ import (

// Verify is a catch-all for verifying the PR doesn't have any issues.
func (b *Bot) Verify(ctx context.Context) error {
var err error
switch b.c.Environment.Repository {
case env.AccessGraphRepo:
err = b.verifyAccessGraph(ctx)
case env.CloudRepo:
err := b.verifyCloud(ctx)
if err != nil {
return trace.Wrap(err)
}
err = b.verifyCloud(ctx)
}
return nil
return trace.Wrap(err)
}

// verifyAccessGraph runs verification checks for the access-graph repo.
func (b *Bot) verifyAccessGraph(ctx context.Context) error {
// exec DB migration verification
return trace.Wrap(b.verifyDBMigrations(ctx))
}

// verifyCloud runs verification checks for cloud repos.
// E.g. it is used to verify DB migration files are ordered properly in the Cloud repo.
func (b *Bot) verifyCloud(ctx context.Context) error {
// exec DB migration verification
return trace.Wrap(b.verifyDBMigrations(ctx))
}

// migrationConfig enables the DB migration verification for a repo/path.
//
// map[repo]: [...path]
var migrationConfig = map[string][]string{
env.AccessGraphRepo: {"migrations/public", "migrations/tenant"},
env.CloudRepo: {"db/salescenter/migrations"},
}

// verifyDBMigrations runs verifyDBMigration for each
// of the migration paths defined for the current repository
func (b *Bot) verifyDBMigrations(ctx context.Context) error {
for _, path := range migrationConfig[b.c.Environment.Repository] {
err := b.verifyDBMigration(ctx, path)
if err != nil {
Expand All @@ -39,12 +59,6 @@ func (b *Bot) verifyCloud(ctx context.Context) error {
return nil
}

// migrationConfig enables the DB migration verification for a repo/path.
// map[repo]: [...path]
var migrationConfig = map[string][]string{
env.CloudRepo: []string{"db/salescenter/migrations"},
}

// verifyDBMigration ensures the DB migration files in a PR have a timestamp
// that is more recent than the migration files in the base branch.
func (b *Bot) verifyDBMigration(ctx context.Context, pathPrefix string) error {
Expand Down Expand Up @@ -77,7 +91,7 @@ func (b *Bot) verifyDBMigration(ctx context.Context, pathPrefix string) error {

// no PR migration files
if len(prIDs) == 0 {
log.Print("Verify:cloudDBMigration: no migration files in this PR")
log.Printf("Verify:cloudDBMigration: no migration files in %s in this PR", pathPrefix)
return nil
}

Expand Down Expand Up @@ -132,7 +146,7 @@ func (b *Bot) verifyDBMigration(ctx context.Context, pathPrefix string) error {
// the prefix ID of each file or returns an error if the file does not have an
// integer prefix. The returned IDs are sorted in ascending order.
//
// 202301031500_subscription-alter.up.sql => 202301031500
// 202301031500_subscription-alter.up.sql => 202301031500
func parseMigrationFileIDs(pathPrefix string, files []string) ([]int, error) {
var ids []int
for _, file := range files {
Expand Down
7 changes: 4 additions & 3 deletions bot/internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (

const (
// Repo slugs
CloudRepo = "cloud"
TeleportRepo = "teleport"
TeleportERepo = "teleport.e"
AccessGraphRepo = "access-graph"
CloudRepo = "cloud"
TeleportRepo = "teleport"
TeleportERepo = "teleport.e"

// Teams
CoreTeam = "Core"
Expand Down
4 changes: 2 additions & 2 deletions bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
func main() {
flags, err := parseFlags()
if err != nil {
log.Fatalf("Failed to parse flags: %v.", err)
log.Fatalf("Failed to parse flags: %#v.", err)
}

// Cancel run if it takes longer than 5 minutes.
Expand All @@ -48,7 +48,7 @@ func main() {

b, err := createBot(ctx, flags)
if err != nil {
log.Fatalf("Failed to create bot: %v.", err)
log.Fatalf("Failed to create bot: %#v.", err)
}

log.Printf("Running %v.", flags.workflow)
Expand Down

0 comments on commit 7ade89f

Please sign in to comment.