diff --git a/database/query/repositories.sql b/database/query/repositories.sql index 91f28dc1a9..67daa13ef8 100644 --- a/database/query/repositories.sql +++ b/database/query/repositories.sql @@ -22,7 +22,7 @@ SELECT * FROM repositories WHERE repo_id = $1; -- name: GetRepositoryByRepoName :one SELECT * FROM repositories WHERE repo_owner = $1 AND repo_name = $2 AND project_id = $3 - AND lower(provider) = lower(sqlc.narg('provider')::text) OR sqlc.narg('provider')::text IS NULL; + AND (lower(provider) = lower(sqlc.narg('provider')::text) OR sqlc.narg('provider')::text IS NULL); -- avoid using this, where possible use GetRepositoryByIDAndProject instead -- name: GetRepositoryByID :one @@ -42,7 +42,7 @@ LIMIT sqlc.narg('limit')::bigint; -- name: ListRegisteredRepositoriesByProjectIDAndProvider :many SELECT * FROM repositories WHERE project_id = $1 AND webhook_id IS NOT NULL - AND lower(provider) = lower(sqlc.narg('provider')::text) OR sqlc.narg('provider')::text IS NULL + AND (lower(provider) = lower(sqlc.narg('provider')::text) OR sqlc.narg('provider')::text IS NULL) ORDER BY repo_name; -- name: DeleteRepository :exec diff --git a/internal/db/repositories.sql.go b/internal/db/repositories.sql.go index 703f2f2c71..f55c7caf62 100644 --- a/internal/db/repositories.sql.go +++ b/internal/db/repositories.sql.go @@ -205,7 +205,7 @@ func (q *Queries) GetRepositoryByRepoID(ctx context.Context, repoID int64) (Repo const getRepositoryByRepoName = `-- name: GetRepositoryByRepoName :one SELECT id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id FROM repositories WHERE repo_owner = $1 AND repo_name = $2 AND project_id = $3 - AND lower(provider) = lower($4::text) OR $4::text IS NULL + AND (lower(provider) = lower($4::text) OR $4::text IS NULL) ` type GetRepositoryByRepoNameParams struct { @@ -248,7 +248,7 @@ func (q *Queries) GetRepositoryByRepoName(ctx context.Context, arg GetRepository const listRegisteredRepositoriesByProjectIDAndProvider = `-- name: ListRegisteredRepositoriesByProjectIDAndProvider :many SELECT id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id FROM repositories WHERE project_id = $1 AND webhook_id IS NOT NULL - AND lower(provider) = lower($2::text) OR $2::text IS NULL + AND (lower(provider) = lower($2::text) OR $2::text IS NULL) ORDER BY repo_name ` diff --git a/internal/db/repositories_test.go b/internal/db/repositories_test.go index c5c8464f4a..147d0157e1 100644 --- a/internal/db/repositories_test.go +++ b/internal/db/repositories_test.go @@ -156,6 +156,38 @@ func TestGetRepositoryByRepoName(t *testing.T) { require.Equal(t, repo1.UpdatedAt, repo2.UpdatedAt) } +func TestGetRepositoryByRepoNameNoProvider(t *testing.T) { + t.Parallel() + + org := createRandomOrganization(t) + project := createRandomProject(t, org.ID) + prov := createRandomProvider(t, project.ID) + createRandomRepository(t, project.ID, prov) + repo1 := createRandomRepository(t, project.ID, prov) + + repo2, err := testQueries.GetRepositoryByRepoName(context.Background(), GetRepositoryByRepoNameParams{ + RepoOwner: repo1.RepoOwner, + RepoName: repo1.RepoName, + ProjectID: project.ID, + }) + require.NoError(t, err) + require.NotEmpty(t, repo2) + + require.Equal(t, repo1.ID, repo2.ID) + require.Equal(t, repo1.Provider, repo2.Provider) + require.Equal(t, repo1.ProjectID, repo2.ProjectID) + require.Equal(t, repo1.RepoOwner, repo2.RepoOwner) + require.Equal(t, repo1.RepoName, repo2.RepoName) + require.Equal(t, repo1.RepoID, repo2.RepoID) + require.Equal(t, repo1.IsPrivate, repo2.IsPrivate) + require.Equal(t, repo1.IsFork, repo2.IsFork) + require.Equal(t, repo1.WebhookID, repo2.WebhookID) + require.Equal(t, repo1.WebhookUrl, repo2.WebhookUrl) + require.Equal(t, repo1.DeployUrl, repo2.DeployUrl) + require.Equal(t, repo1.CreatedAt, repo2.CreatedAt) + require.Equal(t, repo1.UpdatedAt, repo2.UpdatedAt) +} + func TestListRepositoriesByProjectID(t *testing.T) { t.Parallel() @@ -188,6 +220,37 @@ func TestListRepositoriesByProjectID(t *testing.T) { } } +func TestListRepositoriesByProjectIDAndProvider(t *testing.T) { + t.Parallel() + + org := createRandomOrganization(t) + otherProject := createRandomProject(t, org.ID) + project := createRandomProject(t, org.ID) + otherProv := createRandomProvider(t, otherProject.ID) + prov := createRandomProvider(t, project.ID) + createRandomRepository(t, project.ID, otherProv) + createRandomRepository(t, otherProject.ID, prov) + + for i := 1001; i < 1020; i++ { + createRandomRepository(t, project.ID, prov, func(r *CreateRepositoryParams) { + r.RepoID = int64(i) + }) + } + + arg := ListRegisteredRepositoriesByProjectIDAndProviderParams{ + ProjectID: project.ID, + } + + repos, err := testQueries.ListRegisteredRepositoriesByProjectIDAndProvider(context.Background(), arg) + require.NoError(t, err) + require.NotEmpty(t, repos) + + for _, repo := range repos { + require.NotEmpty(t, repo) + require.Equal(t, arg.ProjectID, repo.ProjectID) + } +} + func TestDeleteRepository(t *testing.T) { t.Parallel()