Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verification of DB migrations for access-graph #185

Merged
merged 8 commits into from
Dec 5, 2023
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
Loading