From 8a1947f8bd6971619a886b0fe9151c3b02069736 Mon Sep 17 00:00:00 2001 From: wass3r Date: Fri, 27 May 2022 15:05:01 -0500 Subject: [PATCH] fix(db): add source index to builds --- database/postgres/ddl/build.go | 9 +++++++++ database/postgres/postgres.go | 6 ++++++ database/postgres/postgres_test.go | 2 ++ database/sqlite/ddl/build.go | 9 +++++++++ database/sqlite/sqlite.go | 6 ++++++ 5 files changed, 32 insertions(+) diff --git a/database/postgres/ddl/build.go b/database/postgres/ddl/build.go index fd4d77db7..58eacf15b 100644 --- a/database/postgres/ddl/build.go +++ b/database/postgres/ddl/build.go @@ -72,5 +72,14 @@ CREATE INDEX CONCURRENTLY IF NOT EXISTS builds_created ON builds (created); +` + + // CreateBuildSourceIndex represents a query to create an + // index on the builds table for the source column. + CreateBuildSourceIndex = ` +CREATE INDEX CONCURRENTLY +IF NOT EXISTS +builds_source +ON builds (source); ` ) diff --git a/database/postgres/postgres.go b/database/postgres/postgres.go index c25048f1b..c45b954a2 100644 --- a/database/postgres/postgres.go +++ b/database/postgres/postgres.go @@ -300,6 +300,12 @@ func createIndexes(c *client) error { return fmt.Errorf("unable to create builds_created index for the %s table: %w", constants.TableBuild, err) } + // create the builds_source index for the builds table + err = c.Postgres.Exec(ddl.CreateBuildSourceIndex).Error + if err != nil { + return fmt.Errorf("unable to create builds_source index for the %s table: %w", constants.TableBuild, err) + } + // create the hooks_repo_id index for the hooks table err = c.Postgres.Exec(ddl.CreateHookRepoIDIndex).Error if err != nil { diff --git a/database/postgres/postgres_test.go b/database/postgres/postgres_test.go index 5188b5c93..fd614af9a 100644 --- a/database/postgres/postgres_test.go +++ b/database/postgres/postgres_test.go @@ -87,6 +87,7 @@ func TestPostgres_setupDatabase(t *testing.T) { _mock.ExpectExec(ddl.CreateBuildRepoIDIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateBuildStatusIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateBuildCreatedIndex).WillReturnResult(sqlmock.NewResult(1, 1)) + _mock.ExpectExec(ddl.CreateBuildSourceIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateHookRepoIDIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateLogBuildIDIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateRepoOrgNameIndex).WillReturnResult(sqlmock.NewResult(1, 1)) @@ -207,6 +208,7 @@ func TestPostgres_createIndexes(t *testing.T) { _mock.ExpectExec(ddl.CreateBuildRepoIDIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateBuildStatusIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateBuildCreatedIndex).WillReturnResult(sqlmock.NewResult(1, 1)) + _mock.ExpectExec(ddl.CreateBuildSourceIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateHookRepoIDIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateLogBuildIDIndex).WillReturnResult(sqlmock.NewResult(1, 1)) _mock.ExpectExec(ddl.CreateRepoOrgNameIndex).WillReturnResult(sqlmock.NewResult(1, 1)) diff --git a/database/sqlite/ddl/build.go b/database/sqlite/ddl/build.go index db9f5d5cc..e4a713fdd 100644 --- a/database/sqlite/ddl/build.go +++ b/database/sqlite/ddl/build.go @@ -72,5 +72,14 @@ CREATE INDEX IF NOT EXISTS builds_created ON builds (created); +` + + // CreateBuildSourceIndex represents a query to create an + // index on the builds table for the source column. + CreateBuildSourceIndex = ` +CREATE INDEX +IF NOT EXISTS +builds_source +ON builds (source); ` ) diff --git a/database/sqlite/sqlite.go b/database/sqlite/sqlite.go index 5702c76cd..61935ff56 100644 --- a/database/sqlite/sqlite.go +++ b/database/sqlite/sqlite.go @@ -296,6 +296,12 @@ func createIndexes(c *client) error { return fmt.Errorf("unable to create builds_created index for the %s table: %w", constants.TableBuild, err) } + // create the builds_source index for the builds table + err = c.Sqlite.Exec(ddl.CreateBuildSourceIndex).Error + if err != nil { + return fmt.Errorf("unable to create builds_source index for the %s table: %w", constants.TableBuild, err) + } + // create the hooks_repo_id index for the hooks table err = c.Sqlite.Exec(ddl.CreateHookRepoIDIndex).Error if err != nil {