Skip to content

Commit

Permalink
Add entity population to migration command
Browse files Browse the repository at this point in the history
This way it runs every time minder updates

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX committed Aug 13, 2024
1 parent 8e45b42 commit abca3bd
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/server/app/migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/stacklok/minder/internal/authz"
"github.com/stacklok/minder/internal/config"
serverconfig "github.com/stacklok/minder/internal/config/server"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/logger"
)

Expand Down Expand Up @@ -111,6 +112,21 @@ var upCmd = &cobra.Command{
return fmt.Errorf("error preparing authz client: %w", err)
}

cmd.Println("Performing entity migrations...")
store := db.NewStore(dbConn)

if err := store.TemporaryPopulateRepositories(ctx); err != nil {
cmd.Printf("Error while populating entities table with repos: %v\n", err)
}

if err := store.TemporaryPopulateArtifacts(ctx); err != nil {
cmd.Printf("Error while populating entities table with artifacts: %v\n", err)
}

if err := store.TemporaryPopulatePullRequests(ctx); err != nil {
cmd.Printf("Error while populating entities table with pull requests: %v\n", err)
}

return nil
},
}
Expand Down
42 changes: 42 additions & 0 deletions database/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions database/query/entities.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,19 @@ LIMIT 1;
SELECT * FROM entity_instances
WHERE entity_instances.entity_type = $1 AND entity_instances.project_id = ANY(sqlc.arg(projects)::uuid[]);

-- name: TemporaryPopulateRepositories :exec
INSERT INTO entity_instances (id, entity_type, name, project_id, provider_id, created_at)
SELECT id, 'repository', repo_owner || '/' || repo_name, project_id, provider_id, created_at FROM repositories
WHERE NOT EXISTS (SELECT 1 FROM entity_instances WHERE entity_instances.id = repositories.id AND entity_instances.entity_type = 'repository');

-- name: TemporaryPopulateArtifacts :exec
INSERT INTO entity_instances (id, entity_type, name, project_id, provider_id, created_at, originated_from)
SELECT artifacts.id, 'artifact', LOWER(repositories.repo_owner) || '/' || repositories.repo_name || '/' || artifacts.name, repositories.project_id, repositories.provider_id, artifacts.created_at, artifacts.originated_from FROM artifacts
JOIN repositories ON repositories.id = artifacts.repository_id
WHERE NOT EXISTS (SELECT 1 FROM entity_instances WHERE entity_instances.id = artifacts.id AND entity_instances.entity_type = 'artifact');

-- name: TemporaryPopulatePullRequests :exec
INSERT INTO entity_instances (id, entity_type, name, project_id, provider_id, created_at, originated_from)
SELECT pull_requests.id, 'pull_request', repositories.repo_owner || '/' || repositories.repo_name || '/' || pull_requests.pr_number::TEXT, repositories.project_id, repositories.provider_id, pull_requests.created_at, pull_requests.repository_id FROM pull_requests
JOIN repositories ON repositories.id = pull_requests.repository_id
WHERE NOT EXISTS (SELECT 1 FROM entity_instances WHERE entity_instances.id = pull_requests.id AND entity_instances.entity_type = 'pull_request');
35 changes: 35 additions & 0 deletions internal/db/entities.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit abca3bd

Please sign in to comment.