From cb58721bd19cd838d64346ba8b866380b27d6e73 Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Wed, 18 Oct 2023 15:39:49 -0400 Subject: [PATCH 01/21] initial find and replace names --- .../migrations/000060_rename_tables.down.sql | 0 .../db/migrations/000060_rename_tables.up.sql | 0 .../deprecated_models/v2models/app_version.go | 2 +- sherlock/internal/models/app_version.go | 20 +++++++++---------- sherlock/internal/models/app_version_test.go | 2 +- sherlock/internal/models/changeset.go | 6 +++--- sherlock/internal/models/chart.go | 2 +- sherlock/internal/models/chart_release.go | 2 +- sherlock/internal/models/chart_version.go | 20 +++++++++---------- .../internal/models/chart_version_test.go | 2 +- sherlock/internal/models/ci_identifier.go | 4 ++-- sherlock/internal/models/ci_run.go | 4 ++-- sherlock/internal/models/cluster.go | 2 +- sherlock/internal/models/environment.go | 2 +- .../internal/models/pagerduty_integration.go | 2 +- sherlock/internal/models/user.go | 2 +- 16 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 sherlock/db/migrations/000060_rename_tables.down.sql create mode 100644 sherlock/db/migrations/000060_rename_tables.up.sql diff --git a/sherlock/db/migrations/000060_rename_tables.down.sql b/sherlock/db/migrations/000060_rename_tables.down.sql new file mode 100644 index 000000000..e69de29bb diff --git a/sherlock/db/migrations/000060_rename_tables.up.sql b/sherlock/db/migrations/000060_rename_tables.up.sql new file mode 100644 index 000000000..e69de29bb diff --git a/sherlock/internal/deprecated_models/v2models/app_version.go b/sherlock/internal/deprecated_models/v2models/app_version.go index 4ad46abde..1a8049ed7 100644 --- a/sherlock/internal/deprecated_models/v2models/app_version.go +++ b/sherlock/internal/deprecated_models/v2models/app_version.go @@ -23,7 +23,7 @@ type AppVersion struct { } func (a AppVersion) TableName() string { - return "v2_app_versions" + return "app_versions" } func (a AppVersion) getID() uint { diff --git a/sherlock/internal/models/app_version.go b/sherlock/internal/models/app_version.go index 8b92e7c6b..c658cf282 100644 --- a/sherlock/internal/models/app_version.go +++ b/sherlock/internal/models/app_version.go @@ -21,7 +21,7 @@ type AppVersion struct { } func (a *AppVersion) TableName() string { - return "v2_app_versions" + return "app_versions" } func (a *AppVersion) GetCiIdentifier() CiIdentifier { @@ -62,25 +62,25 @@ func GetAppVersionPathIDs(db *gorm.DB, inclusiveStartID uint, exclusiveEndID uin ` WITH RECURSIVE search_path(id, parent_app_version_id) AS ( -- Initially, get the ID and parent ID where: - SELECT v2_app_versions.id, v2_app_versions.parent_app_version_id - FROM v2_app_versions + SELECT app_versions.id, app_versions.parent_app_version_id + FROM app_versions WHERE -- The app version is the one we're starting at - v2_app_versions.id = ? AND + app_versions.id = ? AND -- The app version has a parent reference, so we don't start looking for a null parent (we'd rather bail with no results) - v2_app_versions.parent_app_version_id IS NOT NULL + app_versions.parent_app_version_id IS NOT NULL -- UNION, not UNION ALL, so that duplicate rows in search_path are dropped and we automatically avoid cycles UNION -- Recursively, get the ID and parent ID where: - SELECT v2_app_versions.id, v2_app_versions.parent_app_version_id - FROM v2_app_versions, search_path + SELECT app_versions.id, app_versions.parent_app_version_id + FROM app_versions, search_path WHERE -- The app version isn't the end of the path (if it is, bail because we want to stop searching then) - v2_app_versions.id != ? AND + app_versions.id != ? AND -- The app version is the parent we're currently looking for - v2_app_versions.id = search_path.parent_app_version_id AND + app_versions.id = search_path.parent_app_version_id AND -- The app version has a parent reference, so we don't start looking for a null parent (we'd rather bail) - v2_app_versions.parent_app_version_id IS NOT NULL + app_versions.parent_app_version_id IS NOT NULL ) SELECT * FROM search_path -- Postgres evaluates search_path lazily, so this LIMIT is an extra safeguard against lengthy recursion diff --git a/sherlock/internal/models/app_version_test.go b/sherlock/internal/models/app_version_test.go index 5326dc722..867316dda 100644 --- a/sherlock/internal/models/app_version_test.go +++ b/sherlock/internal/models/app_version_test.go @@ -8,7 +8,7 @@ import ( func (s *modelSuite) TestAppVersionChartIdValidationSqlMissing() { s.SetNonSuitableTestUserForDB() err := s.DB.Create(&AppVersion{AppVersion: "version"}).Error - s.ErrorContains(err, "fk_v2_app_versions_chart") + s.ErrorContains(err, "fk_app_versions_chart") } func (s *modelSuite) TestAppVersionVersionValidationSqlMissing() { diff --git a/sherlock/internal/models/changeset.go b/sherlock/internal/models/changeset.go index 0f142569a..078148ab3 100644 --- a/sherlock/internal/models/changeset.go +++ b/sherlock/internal/models/changeset.go @@ -15,8 +15,8 @@ type Changeset struct { To ChartReleaseVersion `gorm:"embedded;embeddedPrefix:to_"` AppliedAt *time.Time SupersededAt *time.Time - NewAppVersions []*AppVersion `gorm:"many2many:v2_changeset_new_app_versions;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` - NewChartVersions []*ChartVersion `gorm:"many2many:v2_changeset_new_chart_versions;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` + NewAppVersions []*AppVersion `gorm:"many2many:changeset_new_app_versions;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` + NewChartVersions []*ChartVersion `gorm:"many2many:changeset_new_chart_versions;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` PlannedBy *User PlannedByID *uint @@ -25,7 +25,7 @@ type Changeset struct { } func (c Changeset) TableName() string { - return "v2_changesets" + return "changesets" } func (c Changeset) GetCiIdentifier() CiIdentifier { diff --git a/sherlock/internal/models/chart.go b/sherlock/internal/models/chart.go index a03adb8f8..0737c064e 100644 --- a/sherlock/internal/models/chart.go +++ b/sherlock/internal/models/chart.go @@ -21,7 +21,7 @@ type Chart struct { } func (c Chart) TableName() string { - return "v2_charts" + return "charts" } func (c Chart) GetCiIdentifier() CiIdentifier { diff --git a/sherlock/internal/models/chart_release.go b/sherlock/internal/models/chart_release.go index 4f6b8454a..90cb3e6f4 100644 --- a/sherlock/internal/models/chart_release.go +++ b/sherlock/internal/models/chart_release.go @@ -24,7 +24,7 @@ type ChartRelease struct { } func (c ChartRelease) TableName() string { - return "v2_chart_releases" + return "chart_releases" } func (c ChartRelease) GetCiIdentifier() CiIdentifier { diff --git a/sherlock/internal/models/chart_version.go b/sherlock/internal/models/chart_version.go index afa141ce5..5d4131ebb 100644 --- a/sherlock/internal/models/chart_version.go +++ b/sherlock/internal/models/chart_version.go @@ -19,7 +19,7 @@ type ChartVersion struct { } func (c *ChartVersion) TableName() string { - return "v2_chart_versions" + return "chart_versions" } func (c *ChartVersion) GetCiIdentifier() CiIdentifier { @@ -60,25 +60,25 @@ func GetChartVersionPathIDs(db *gorm.DB, inclusiveStartID uint, exclusiveEndID u ` WITH RECURSIVE search_path(id, parent_chart_version_id) AS ( -- Initially, get the ID and parent ID where: - SELECT v2_chart_versions.id, v2_chart_versions.parent_chart_version_id - FROM v2_chart_versions + SELECT chart_versions.id, chart_versions.parent_chart_version_id + FROM chart_versions WHERE -- The chart version is the one we're starting at - v2_chart_versions.id = ? AND + chart_versions.id = ? AND -- The chart version has a parent reference, so we don't start looking for a null parent (we'd rather bail with no results) - v2_chart_versions.parent_chart_version_id IS NOT NULL + chart_versions.parent_chart_version_id IS NOT NULL -- UNION, not UNION ALL, so that duplicate rows in search_path are dropped and we automatically avoid cycles UNION -- Recursively, get the ID and parent ID where: - SELECT v2_chart_versions.id, v2_chart_versions.parent_chart_version_id - FROM v2_chart_versions, search_path + SELECT chart_versions.id, chart_versions.parent_chart_version_id + FROM chart_versions, search_path WHERE -- The chart version isn't the end of the path (if it is, bail because we want to stop searching then) - v2_chart_versions.id != ? AND + chart_versions.id != ? AND -- The chart version is the parent we're currently looking for - v2_chart_versions.id = search_path.parent_chart_version_id AND + chart_versions.id = search_path.parent_chart_version_id AND -- The chart version has a parent reference, so we don't start looking for a null parent (we'd rather bail) - v2_chart_versions.parent_chart_version_id IS NOT NULL + chart_versions.parent_chart_version_id IS NOT NULL ) SELECT * FROM search_path -- Postgres evaluates search_path lazily, so this LIMIT is an extra safeguard against lengthy recursion diff --git a/sherlock/internal/models/chart_version_test.go b/sherlock/internal/models/chart_version_test.go index c668d42a2..3afef8258 100644 --- a/sherlock/internal/models/chart_version_test.go +++ b/sherlock/internal/models/chart_version_test.go @@ -8,7 +8,7 @@ import ( func (s *modelSuite) TestChartVersionChartIdValidationSqlMissing() { s.SetNonSuitableTestUserForDB() err := s.DB.Create(&ChartVersion{ChartVersion: "version"}).Error - s.ErrorContains(err, "fk_v2_chart_versions_chart") + s.ErrorContains(err, "fk_chart_versions_chart") } func (s *modelSuite) TestChartVersionVersionValidationSqlMissing() { diff --git a/sherlock/internal/models/ci_identifier.go b/sherlock/internal/models/ci_identifier.go index 3dc45397f..eb41cbd05 100644 --- a/sherlock/internal/models/ci_identifier.go +++ b/sherlock/internal/models/ci_identifier.go @@ -9,9 +9,9 @@ type CiIdentifier struct { ResourceType string ResourceID uint // Mutable - CiRuns []CiRun `gorm:"many2many:v2_ci_runs_for_identifiers"` + CiRuns []CiRun `gorm:"many2many:ci_runs_for_identifiers"` } func (c *CiIdentifier) TableName() string { - return "v2_ci_identifiers" + return "ci_identifiers" } diff --git a/sherlock/internal/models/ci_run.go b/sherlock/internal/models/ci_run.go index d2134f5b5..02ae5a6d5 100644 --- a/sherlock/internal/models/ci_run.go +++ b/sherlock/internal/models/ci_run.go @@ -35,7 +35,7 @@ type CiRun struct { TerminationHooksDispatchedAt *string // Mutable - RelatedResources []CiIdentifier `gorm:"many2many:v2_ci_runs_for_identifiers"` + RelatedResources []CiIdentifier `gorm:"many2many:ci_runs_for_identifiers"` StartedAt *time.Time TerminalAt *time.Time Status *string @@ -44,7 +44,7 @@ type CiRun struct { } func (c *CiRun) TableName() string { - return "v2_ci_runs" + return "ci_runs" } func (c *CiRun) WebURL() string { diff --git a/sherlock/internal/models/cluster.go b/sherlock/internal/models/cluster.go index c9a6fc036..c383378e5 100644 --- a/sherlock/internal/models/cluster.go +++ b/sherlock/internal/models/cluster.go @@ -22,7 +22,7 @@ type Cluster struct { } func (c *Cluster) TableName() string { - return "v2_clusters" + return "clusters" } func (c *Cluster) GetCiIdentifier() CiIdentifier { diff --git a/sherlock/internal/models/environment.go b/sherlock/internal/models/environment.go index dcef1550d..faab49a89 100644 --- a/sherlock/internal/models/environment.go +++ b/sherlock/internal/models/environment.go @@ -43,7 +43,7 @@ type Environment struct { } func (e Environment) TableName() string { - return "v2_environments" + return "environments" } func (e Environment) GetCiIdentifier() CiIdentifier { diff --git a/sherlock/internal/models/pagerduty_integration.go b/sherlock/internal/models/pagerduty_integration.go index f6a4b372f..8957875e6 100644 --- a/sherlock/internal/models/pagerduty_integration.go +++ b/sherlock/internal/models/pagerduty_integration.go @@ -11,5 +11,5 @@ type PagerdutyIntegration struct { } func (p PagerdutyIntegration) TableName() string { - return "v2_pagerduty_integrations" + return "pagerduty_integrations" } diff --git a/sherlock/internal/models/user.go b/sherlock/internal/models/user.go index 0bff9bbf5..701cf1b88 100644 --- a/sherlock/internal/models/user.go +++ b/sherlock/internal/models/user.go @@ -72,7 +72,7 @@ type User struct { } func (u *User) TableName() string { - return "v2_users" + return "users" } func (u *User) BeforeUpdate(tx *gorm.DB) error { From 13679838b01b09a769511a1e3b2a1925924c1e5e Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Wed, 18 Oct 2023 16:29:53 -0400 Subject: [PATCH 02/21] sql migrations --- .../migrations/000060_rename_tables.down.sql | 44 +++++++++++++++++++ .../db/migrations/000060_rename_tables.up.sql | 44 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/sherlock/db/migrations/000060_rename_tables.down.sql b/sherlock/db/migrations/000060_rename_tables.down.sql index e69de29bb..db8c580f8 100644 --- a/sherlock/db/migrations/000060_rename_tables.down.sql +++ b/sherlock/db/migrations/000060_rename_tables.down.sql @@ -0,0 +1,44 @@ +alter table app_versions + rename to v2_app_versions; + +alter table changeset_new_app_versions + rename to v2_changeset_new_app_versions; + +alter table changeset_new_chart_versions + rename to v2_changeset_new_chart_versions; + +alter table changesets + rename to v2_changesets; + +alter table chart_releases + rename to v2_chart_releases; + +alter table chart_versions + rename to v2_chart_versions; + +alter table charts + rename to v2_charts; + +alter table ci_identifiers + rename to v2_ci_identifiers; + +alter table ci_runs + rename to v2_ci_runs; + +alter table ci_runs_for_identifiers + rename to v2_ci_runs_for_identifiers; + +alter table clusters + rename to v2_clusters; + +alter table database_instances + rename to v2_database_instances; + +alter table environments + rename to v2_environments; + +alter table pagerduty_integrations + rename to v2_pagerduty_integrations; + +alter table users + rename to v2_users; \ No newline at end of file diff --git a/sherlock/db/migrations/000060_rename_tables.up.sql b/sherlock/db/migrations/000060_rename_tables.up.sql index e69de29bb..1f6e84ec7 100644 --- a/sherlock/db/migrations/000060_rename_tables.up.sql +++ b/sherlock/db/migrations/000060_rename_tables.up.sql @@ -0,0 +1,44 @@ +alter table v2_app_versions + rename to app_versions; + +alter table v2_changeset_new_app_versions + rename to changeset_new_app_versions; + +alter table v2_changeset_new_chart_versions + rename to changeset_new_chart_versions; + +alter table v2_changesets + rename to changesets; + +alter table v2_chart_releases + rename to chart_releases; + +alter table v2_chart_versions + rename to chart_versions; + +alter table v2_charts + rename to charts; + +alter table v2_ci_identifiers + rename to ci_identifiers; + +alter table v2_ci_runs + rename to ci_runs; + +alter table v2_ci_runs_for_identifiers + rename to ci_runs_for_identifiers; + +alter table v2_clusters + rename to clusters; + +alter table v2_database_instances + rename to database_instances; + +alter table v2_environments + rename to environments; + +alter table v2_pagerduty_integrations + rename to pagerduty_integrations; + +alter table v2_users + rename to users; \ No newline at end of file From a4524eca38c1f30fbe7d7c73e7df922265c2915c Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Thu, 19 Oct 2023 11:40:35 -0400 Subject: [PATCH 03/21] revert fk_ changes --- sherlock/internal/models/app_version_test.go | 2 +- sherlock/internal/models/chart_version_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sherlock/internal/models/app_version_test.go b/sherlock/internal/models/app_version_test.go index 867316dda..96c89e9d2 100644 --- a/sherlock/internal/models/app_version_test.go +++ b/sherlock/internal/models/app_version_test.go @@ -8,7 +8,7 @@ import ( func (s *modelSuite) TestAppVersionChartIdValidationSqlMissing() { s.SetNonSuitableTestUserForDB() err := s.DB.Create(&AppVersion{AppVersion: "version"}).Error - s.ErrorContains(err, "fk_app_versions_chart") + s.ErrorContains(err, "fk_v2app_versions_chart") } func (s *modelSuite) TestAppVersionVersionValidationSqlMissing() { diff --git a/sherlock/internal/models/chart_version_test.go b/sherlock/internal/models/chart_version_test.go index 3afef8258..4f26dedf7 100644 --- a/sherlock/internal/models/chart_version_test.go +++ b/sherlock/internal/models/chart_version_test.go @@ -8,7 +8,7 @@ import ( func (s *modelSuite) TestChartVersionChartIdValidationSqlMissing() { s.SetNonSuitableTestUserForDB() err := s.DB.Create(&ChartVersion{ChartVersion: "version"}).Error - s.ErrorContains(err, "fk_chart_versions_chart") + s.ErrorContains(err, "fk_v2chart_versions_chart") } func (s *modelSuite) TestChartVersionVersionValidationSqlMissing() { From 863dda4285eb9796ca61706c993a290513676748 Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Fri, 20 Oct 2023 14:13:17 -0400 Subject: [PATCH 04/21] add _ --- sherlock/internal/models/app_version_test.go | 2 +- sherlock/internal/models/chart_version_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sherlock/internal/models/app_version_test.go b/sherlock/internal/models/app_version_test.go index 96c89e9d2..5326dc722 100644 --- a/sherlock/internal/models/app_version_test.go +++ b/sherlock/internal/models/app_version_test.go @@ -8,7 +8,7 @@ import ( func (s *modelSuite) TestAppVersionChartIdValidationSqlMissing() { s.SetNonSuitableTestUserForDB() err := s.DB.Create(&AppVersion{AppVersion: "version"}).Error - s.ErrorContains(err, "fk_v2app_versions_chart") + s.ErrorContains(err, "fk_v2_app_versions_chart") } func (s *modelSuite) TestAppVersionVersionValidationSqlMissing() { diff --git a/sherlock/internal/models/chart_version_test.go b/sherlock/internal/models/chart_version_test.go index 4f26dedf7..c668d42a2 100644 --- a/sherlock/internal/models/chart_version_test.go +++ b/sherlock/internal/models/chart_version_test.go @@ -8,7 +8,7 @@ import ( func (s *modelSuite) TestChartVersionChartIdValidationSqlMissing() { s.SetNonSuitableTestUserForDB() err := s.DB.Create(&ChartVersion{ChartVersion: "version"}).Error - s.ErrorContains(err, "fk_v2chart_versions_chart") + s.ErrorContains(err, "fk_v2_chart_versions_chart") } func (s *modelSuite) TestChartVersionVersionValidationSqlMissing() { From d5452b553fe148a5f25a563c37ec65280157188e Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Fri, 20 Oct 2023 16:24:14 -0400 Subject: [PATCH 05/21] soooo many renames --- .../migrations/000060_rename_tables.down.sql | 330 +++++++++++++++++- .../db/migrations/000060_rename_tables.up.sql | 330 +++++++++++++++++- 2 files changed, 658 insertions(+), 2 deletions(-) diff --git a/sherlock/db/migrations/000060_rename_tables.down.sql b/sherlock/db/migrations/000060_rename_tables.down.sql index db8c580f8..a1dafd3e2 100644 --- a/sherlock/db/migrations/000060_rename_tables.down.sql +++ b/sherlock/db/migrations/000060_rename_tables.down.sql @@ -1,44 +1,372 @@ +/* + * app_version + */ alter table app_versions rename to v2_app_versions; +-- keys +alter table v2_app_versions + rename constraint app_versions_pkey to v2_app_versions_pkey; + +-- foreign keys +alter table v2_app_versions + rename constraint fk_app_versions_chart to v2_fk_app_versions_chart; + +alter table v2_app_versions + rename constraint fk_app_versions_parent_app_version to v2_fk_app_versions_parent_app_version; + +-- indexes +alter index app_versions_pkey rename to v2_app_versions_pkey; + +alter index idx_app_versions_deleted_at rename to v2_idx_app_versions_deleted_at; + +/* + * changeset_new_app_versions + */ alter table changeset_new_app_versions rename to v2_changeset_new_app_versions; +-- keys +alter table v2_changeset_new_app_versions + rename constraint changeset_new_app_versions_pkey to v2_changeset_new_app_versions_pkey; + +-- foreign keys +alter table v2_changeset_new_app_versions + rename constraint fk_changeset_new_app_versions_app_version to v2_fk_changeset_new_app_versions_app_version; + +alter table v2_changeset_new_app_versions + rename constraint fk_changeset_new_app_versions_changeset to v2_fk_changeset_new_app_versions_changeset; + +-- indexes +alter index changeset_new_app_versions_pkey rename to v2_changeset_new_app_versions_pkey; + +/* + * changeset_new_chart_versions + */ alter table changeset_new_chart_versions rename to v2_changeset_new_chart_versions; +-- keys +alter table v2_changeset_new_chart_versions + rename constraint changeset_new_chart_versions_pkey to v2_changeset_new_chart_versions_pkey; + +-- foreign keys +alter table v2_changeset_new_chart_versions + rename constraint fk_changeset_new_chart_versions_changeset to v2_fk_changeset_new_chart_versions_changeset; + +alter table v2_changeset_new_chart_versions + rename constraint fk_changeset_new_chart_versions_chart_version to v2_fk_changeset_new_chart_versions_chart_version; + +-- indexes +alter index changeset_new_chart_versions_pkey rename to v2_changeset_new_chart_versions_pkey; + +/* + * changesets + */ alter table changesets rename to v2_changesets; +-- keys +alter table v2_changesets + rename constraint changesets_pkey to v2_changesets_pkey; + +-- foreign keys +alter table v2_changesets + rename constraint fk_changesets_chart_release to v2_fk_changesets_chart_release; + +alter table v2_changesets + rename constraint fk_changesets_from_app_version to v2_fk_changesets_from_app_version; + +alter table v2_changesets + rename constraint fk_changesets_from_chart_version to v2_fk_changesets_from_chart_version; + +alter table v2_changesets + rename constraint fk_changesets_from_follow_chart_release_app_version to v2_fk_changesets_from_follow_chart_release_app_version; + +alter table v2_changesets + rename constraint fk_changesets_from_follow_chart_release_chart_version to v2_fk_changesets_from_follow_chart_release_chart_version; + +alter table v2_changesets + rename constraint fk_changesets_to_app_version to v2_fk_changesets_to_app_version; + +alter table v2_changesets + rename constraint fk_changesets_to_chart_version to v2_fk_changesets_to_chart_version; + +alter table v2_changesets + rename constraint fk_changesets_to_follow_chart_release_app_version to v2_fk_changesets_to_follow_chart_release_app_version; + +alter table v2_changesets + rename constraint fk_changesets_to_follow_chart_release_chart_version to v2_fk_changesets_to_follow_chart_release_chart_version; + +-- indexes +alter index changesets_pkey rename to v2_changesets_pkey; + +alter index idx_changesets_deleted_at rename to v2_idx_changesets_deleted_at; + +/* + * chart_releases + */ alter table chart_releases rename to v2_chart_releases; +-- key +alter table v2_chart_releases + rename constraint chart_releases_pkey to v2_chart_releases_pkey; + +-- foreign keys +alter table v2_chart_releases + rename constraint fk_chart_releases_app_version to v2_fk_chart_releases_app_version; + +alter table v2_chart_releases + rename constraint fk_chart_releases_chart to v2_fk_chart_releases_chart; + +alter table v2_chart_releases + rename constraint fk_chart_releases_chart_version to v2_fk_chart_releases_chart_version; + +alter table v2_chart_releases + rename constraint fk_chart_releases_cluster to v2_fk_chart_releases_cluster; + +alter table v2_chart_releases + rename constraint fk_chart_releases_environment to v2_fk_chart_releases_environment; + +alter table v2_chart_releases + rename constraint fk_chart_releases_follow_chart_release_app_version to v2_fk_chart_releases_follow_chart_release_app_version; + +alter table v2_chart_releases + rename constraint fk_chart_releases_follow_chart_release_chart_version to v2_fk_chart_releases_follow_chart_release_chart_version; + +alter table v2_chart_releases + rename constraint fk_chart_releases_pagerduty_integration to v2_fk_chart_releases_pagerduty_integration; + +-- indexes +alter index chart_releases_pkey rename to v2_chart_releases_pkey; + +alter index idx_chart_releases_deleted_at rename to v2_idx_chart_releases_deleted_at; + +/* + * chart_versions + */ alter table chart_versions rename to v2_chart_versions; +-- keys +alter table v2_chart_versions + rename constraint chart_versions_pkey to v2_chart_versions_pkey; + +-- foreign keys +alter table v2_chart_versions + rename constraint fk_chart_versions_chart to v2_fk_chart_versions_chart; + +alter table v2_chart_versions + rename constraint fk_chart_versions_parent_chart_version to v2_fk_chart_versions_parent_chart_version; + +-- indexes +alter index chart_versions_pkey rename to v2_chart_versions_pkey; + +alter index idx_chart_versions_deleted_at rename to v2_idx_chart_versions_deleted_at; + +/* + * charts + */ alter table charts rename to v2_charts; +-- keys +alter table v2_charts + rename constraint charts_pkey to v2_charts_pkey; + +alter table v2_charts + rename constraint charts_name_key to v2_charts_name_key; + +-- indexes +alter index charts_name_key rename to v2_charts_name_key; + +alter index charts_pkey rename to v2_charts_pkey; + +alter index idx_charts_deleted_at rename to v2_idx_charts_deleted_at; + +/* + * ci_identifiers + */ alter table ci_identifiers rename to v2_ci_identifiers; +-- keys +alter table v2_ci_identifiers + rename constraint ci_identifiers_pkey to v2_ci_identifiers_pkey; + +-- indexes +alter index ci_identifiers_pkey rename to v2_ci_identifiers_pkey; + +alter index idx_ci_identifiers_deleted_at rename to v2_idx_ci_identifiers_deleted_at; + +alter index idx_ci_identifiers_polymorphic_index rename to v2_idx_ci_identifiers_polymorphic_index; + +/* + * ci_runs + */ alter table ci_runs rename to v2_ci_runs; +-- keys +alter table v2_ci_runs + rename constraint ci_runs_pkey to v2_ci_runs_pkey; + +-- indexes +alter index ci_runs_pkey rename to v2_ci_runs_pkey; + +alter index idx_ci_runs_deleted_at rename to v2_idx_ci_runs_deleted_at; + +/* + * ci_runs_for_identifiers + */ alter table ci_runs_for_identifiers rename to v2_ci_runs_for_identifiers; +-- keys +alter table v2_ci_runs_for_identifiers + rename constraint ci_runs_for_identifiers_pkey to v2_ci_runs_for_identifiers_pkey; + +-- foreign keys +alter table v2_ci_runs_for_identifiers + rename constraint fk_ci_runs_for_identifiers_ci_identifier to v2_fk_ci_runs_for_identifiers_ci_identifier; + +alter table v2_ci_runs_for_identifiers + rename constraint fk_ci_runs_for_identifiers_ci_run to v2_fk_ci_runs_for_identifiers_ci_run; + +-- indexes +alter index ci_runs_for_identifiers_pkey rename to v2_ci_runs_for_identifiers_pkey; + +/* + * clusters + */ alter table clusters rename to v2_clusters; +-- keys +alter table v2_clusters + rename constraint clusters_pkey to v2_clusters_pkey; + +alter table v2_clusters + rename constraint clusters_name_key to v2_clusters_name_key; + +-- indexes +alter index clusters_name_key rename to v2_clusters_name_key; + +alter index clusters_pkey rename to v2_clusters_pkey; + +alter index idx_clusters_deleted_at rename to v2_idx_clusters_deleted_at; + +/* + * database_instances + */ alter table database_instances rename to v2_database_instances; +-- keys +alter table v2_database_instances + rename constraint database_instances_pkey to v2_database_instances_pkey; + +-- foreign keys +alter table v2_database_instances + rename constraint fk_database_instances_chart_release to v2_fk_database_instances_chart_release; + +-- indexes +alter index database_instances_pkey rename to v2_database_instances_pkey; + +alter index idx_database_instances_deleted_at rename to v2_idx_database_instances_deleted_at; + +/* + * environments + */ alter table environments rename to v2_environments; +-- keys +alter table v2_environments + rename constraint environments_pkey to v2_environments_pkey; + +-- foreign keys +alter table v2_environments + rename constraint fk_environments_default_cluster to v2_fk_environments_default_cluster; + +alter table v2_environments + rename constraint fk_environments_owner to v2_fk_environments_owner; + +alter table v2_environments + rename constraint fk_environments_pagerduty_integration to v2_fk_environments_pagerduty_integration; + +alter table v2_environments + rename constraint fk_environments_template_environment to v2_fk_environments_template_environment; + +-- indexes +alter index environments_pkey rename to v2_environments_pkey; + +alter index idx_environments_deleted_at rename to v2_idx_environments_deleted_at; + +/* + * pagerduty_integration + */ alter table pagerduty_integrations rename to v2_pagerduty_integrations; +-- keys +alter table v2_pagerduty_integrations + rename constraint pagerduty_integrations_pkey to v2_pagerduty_integrations_pkey; + +-- indexes +alter index pagerduty_integrations_pkey rename to v2_pagerduty_integrations_pkey; + +alter index idx_pagerduty_integrations_deleted_at rename to v2_idx_pagerduty_integrations_deleted_at; + +/* + * users + */ alter table users - rename to v2_users; \ No newline at end of file + rename to v2_users; + +-- keys +alter table v2_users + rename constraint users_pkey to v2_users_pkey; + +alter table v2_users + rename constraint users_email_key to v2_users_email_key; + +alter table v2_users + rename constraint users_google_id_key to v2_users_google_id_key; + +-- indexes +alter index users_email_key rename to v2_users_email_key; + +alter index users_google_id_key rename to v2_users_google_id_key; + +alter index users_pkey rename to v2_users_pkey; + +alter index idx_users_deleted_at rename to v2_idx_users_deleted_at; + +/* + * sequences + */ +alter sequence app_versions_id_seq rename to v2_app_versions_id_seq; + +alter sequence changesets_id_seq rename to v2_changesets_id_seq; + +alter sequence chart_releases_id_seq rename to v2_chart_releases_id_seq; + +alter sequence chart_versions_id_seq rename to v2_chart_versions_id_seq; + +alter sequence charts_id_seq rename to v2_charts_id_seq; + +alter sequence ci_identifiers_id_seq rename to v2_ci_identifiers_id_seq; + +alter sequence ci_runs_id_seq rename to v2_ci_runs_id_seq; + +alter sequence clusters_id_seq rename to v2_clusters_id_seq; + +alter sequence database_instances_id_seq rename to v2_database_instances_id_seq; + +alter sequence environments_id_seq rename to v2_environments_id_seq; + +alter sequence pagerduty_integrations_id_seq rename to v2_pagerduty_integrations_id_seq; + +alter sequence users_id_seq rename to v2_users_id_seq; \ No newline at end of file diff --git a/sherlock/db/migrations/000060_rename_tables.up.sql b/sherlock/db/migrations/000060_rename_tables.up.sql index 1f6e84ec7..2e674bc76 100644 --- a/sherlock/db/migrations/000060_rename_tables.up.sql +++ b/sherlock/db/migrations/000060_rename_tables.up.sql @@ -1,44 +1,372 @@ +/* + * app_version + */ alter table v2_app_versions rename to app_versions; +-- keys +alter table app_versions + rename constraint v2_app_versions_pkey to app_versions_pkey; + +-- foreign keys +alter table app_versions + rename constraint fk_v2_app_versions_chart to fk_app_versions_chart; + +alter table app_versions + rename constraint fk_v2_app_versions_parent_app_version to fk_app_versions_parent_app_version; + +-- indexes +alter index v2_app_versions_pkey rename to app_versions_pkey; + +alter index idx_v2_app_versions_deleted_at rename to idx_app_versions_deleted_at; + +/* + * changeset_new_app_versions + */ alter table v2_changeset_new_app_versions rename to changeset_new_app_versions; +-- keys +alter table changeset_new_app_versions + rename constraint v2_changeset_new_app_versions_pkey to changeset_new_app_versions_pkey; + +-- foreign keys +alter table changeset_new_app_versions + rename constraint fk_v2_changeset_new_app_versions_app_version to fk_changeset_new_app_versions_app_version; + +alter table changeset_new_app_versions + rename constraint fk_v2_changeset_new_app_versions_changeset to fk_changeset_new_app_versions_changeset; + +-- indexes +alter index v2_changeset_new_app_versions_pkey rename to changeset_new_app_versions_pkey; + +/* + * changeset_new_chart_versions + */ alter table v2_changeset_new_chart_versions rename to changeset_new_chart_versions; +-- keys +alter table changeset_new_chart_versions + rename constraint v2_changeset_new_chart_versions_pkey to changeset_new_chart_versions_pkey; + +-- foreign keys +alter table changeset_new_chart_versions + rename constraint fk_v2_changeset_new_chart_versions_changeset to fk_changeset_new_chart_versions_changeset; + +alter table changeset_new_chart_versions + rename constraint fk_v2_changeset_new_chart_versions_chart_version to fk_changeset_new_chart_versions_chart_version; + +-- indexes +alter index v2_changeset_new_chart_versions_pkey rename to changeset_new_chart_versions_pkey; + +/* + * changesets + */ alter table v2_changesets rename to changesets; +-- keys +alter table changesets + rename constraint v2_changesets_pkey to changesets_pkey; + +-- foreign keys +alter table changesets + rename constraint fk_v2_changesets_chart_release to fk_changesets_chart_release; + +alter table changesets + rename constraint fk_v2_changesets_from_app_version to fk_changesets_from_app_version; + +alter table changesets + rename constraint fk_v2_changesets_from_chart_version to fk_changesets_from_chart_version; + +alter table changesets + rename constraint fk_v2_changesets_from_follow_chart_release_app_version to fk_changesets_from_follow_chart_release_app_version; + +alter table changesets + rename constraint fk_v2_changesets_from_follow_chart_release_chart_version to fk_changesets_from_follow_chart_release_chart_version; + +alter table changesets + rename constraint fk_v2_changesets_to_app_version to fk_changesets_to_app_version; + +alter table changesets + rename constraint fk_v2_changesets_to_chart_version to fk_changesets_to_chart_version; + +alter table changesets + rename constraint fk_v2_changesets_to_follow_chart_release_app_version to fk_changesets_to_follow_chart_release_app_version; + +alter table changesets + rename constraint fk_v2_changesets_to_follow_chart_release_chart_version to fk_changesets_to_follow_chart_release_chart_version; + +-- indexes +alter index v2_changesets_pkey rename to changesets_pkey; + +alter index idx_v2_changesets_deleted_at rename to idx_changesets_deleted_at; + +/* + * chart_releases + */ alter table v2_chart_releases rename to chart_releases; +-- key +alter table chart_releases + rename constraint v2_chart_releases_pkey to chart_releases_pkey; + +-- foreign keys +alter table chart_releases + rename constraint fk_v2_chart_releases_app_version to fk_chart_releases_app_version; + +alter table chart_releases + rename constraint fk_v2_chart_releases_chart to fk_chart_releases_chart; + +alter table chart_releases + rename constraint fk_v2_chart_releases_chart_version to fk_chart_releases_chart_version; + +alter table chart_releases + rename constraint fk_v2_chart_releases_cluster to fk_chart_releases_cluster; + +alter table chart_releases + rename constraint fk_v2_chart_releases_environment to fk_chart_releases_environment; + +alter table chart_releases + rename constraint fk_v2_chart_releases_follow_chart_release_app_version to fk_chart_releases_follow_chart_release_app_version; + +alter table chart_releases + rename constraint fk_v2_chart_releases_follow_chart_release_chart_version to fk_chart_releases_follow_chart_release_chart_version; + +alter table chart_releases + rename constraint fk_v2_chart_releases_pagerduty_integration to fk_chart_releases_pagerduty_integration; + +-- indexes +alter index v2_chart_releases_pkey rename to chart_releases_pkey; + +alter index idx_v2_chart_releases_deleted_at rename to idx_chart_releases_deleted_at; + +/* + * chart_versions + */ alter table v2_chart_versions rename to chart_versions; +-- keys +alter table chart_versions + rename constraint v2_chart_versions_pkey to chart_versions_pkey; + +-- foreign keys +alter table chart_versions + rename constraint fk_v2_chart_versions_chart to fk_chart_versions_chart; + +alter table chart_versions + rename constraint fk_v2_chart_versions_parent_chart_version to fk_chart_versions_parent_chart_version; + +-- indexes +alter index v2_chart_versions_pkey rename to chart_versions_pkey; + +alter index idx_v2_chart_versions_deleted_at rename to idx_chart_versions_deleted_at; + +/* + * charts + */ alter table v2_charts rename to charts; +-- keys +alter table charts + rename constraint v2_charts_pkey to charts_pkey; + +alter table charts + rename constraint v2_charts_name_key to charts_name_key; + +-- indexes +alter index v2_charts_name_key rename to charts_name_key; + +alter index v2_charts_pkey rename to charts_pkey; + +alter index idx_v2_charts_deleted_at rename to idx_charts_deleted_at; + +/* + * ci_identifiers + */ alter table v2_ci_identifiers rename to ci_identifiers; +-- keys +alter table ci_identifiers + rename constraint v2_ci_identifiers_pkey to ci_identifiers_pkey; + +-- indexes +alter index v2_ci_identifiers_pkey rename to ci_identifiers_pkey; + +alter index idx_v2_ci_identifiers_deleted_at rename to idx_ci_identifiers_deleted_at; + +alter index idx_v2_ci_identifiers_polymorphic_index rename to idx_ci_identifiers_polymorphic_index; + +/* + * ci_runs + */ alter table v2_ci_runs rename to ci_runs; +-- keys +alter table ci_runs + rename constraint v2_ci_runs_pkey to ci_runs_pkey; + +-- indexes +alter index v2_ci_runs_pkey rename to ci_runs_pkey; + +alter index idx_v2_ci_runs_deleted_at rename to idx_ci_runs_deleted_at; + +/* + * ci_runs_for_identifiers + */ alter table v2_ci_runs_for_identifiers rename to ci_runs_for_identifiers; +-- keys +alter table ci_runs_for_identifiers + rename constraint v2_ci_runs_for_identifiers_pkey to ci_runs_for_identifiers_pkey; + +-- foreign keys +alter table ci_runs_for_identifiers + rename constraint fk_v2_ci_runs_for_identifiers_ci_identifier to fk_ci_runs_for_identifiers_ci_identifier; + +alter table ci_runs_for_identifiers + rename constraint fk_v2_ci_runs_for_identifiers_ci_run to fk_ci_runs_for_identifiers_ci_run; + +-- indexes +alter index v2_ci_runs_for_identifiers_pkey rename to ci_runs_for_identifiers_pkey; + +/* + * clusters + */ alter table v2_clusters rename to clusters; +-- keys +alter table clusters + rename constraint v2_clusters_pkey to clusters_pkey; + +alter table clusters + rename constraint v2_clusters_name_key to clusters_name_key; + +-- indexes +alter index v2_clusters_name_key rename to clusters_name_key; + +alter index v2_clusters_pkey rename to clusters_pkey; + +alter index idx_v2_clusters_deleted_at rename to idx_clusters_deleted_at; + +/* + * database_instances + */ alter table v2_database_instances rename to database_instances; +-- keys +alter table database_instances + rename constraint v2_database_instances_pkey to database_instances_pkey; + +-- foreign keys +alter table database_instances + rename constraint fk_v2_database_instances_chart_release to fk_database_instances_chart_release; + +-- indexes +alter index v2_database_instances_pkey rename to database_instances_pkey; + +alter index idx_v2_database_instances_deleted_at rename to idx_database_instances_deleted_at; + +/* + * environments + */ alter table v2_environments rename to environments; +-- keys +alter table environments + rename constraint v2_environments_pkey to environments_pkey; + +-- foreign keys +alter table environments + rename constraint fk_v2_environments_default_cluster to fk_environments_default_cluster; + +alter table environments + rename constraint fk_v2_environments_owner to fk_environments_owner; + +alter table environments + rename constraint fk_v2_environments_pagerduty_integration to fk_environments_pagerduty_integration; + +alter table environments + rename constraint fk_v2_environments_template_environment to fk_environments_template_environment; + +-- indexes +alter index v2_environments_pkey rename to environments_pkey; + +alter index idx_v2_environments_deleted_at rename to idx_environments_deleted_at; + +/* + * pagerduty_integration + */ alter table v2_pagerduty_integrations rename to pagerduty_integrations; +-- keys +alter table pagerduty_integrations + rename constraint v2_pagerduty_integrations_pkey to pagerduty_integrations_pkey; + +-- indexes +alter index v2_pagerduty_integrations_pkey rename to pagerduty_integrations_pkey; + +alter index idx_v2_pagerduty_integrations_deleted_at rename to idx_pagerduty_integrations_deleted_at; + +/* + * users + */ alter table v2_users - rename to users; \ No newline at end of file + rename to users; + +-- keys +alter table users + rename constraint v2_users_pkey to users_pkey; + +alter table users + rename constraint v2_users_email_key to users_email_key; + +alter table users + rename constraint v2_users_google_id_key to users_google_id_key; + +-- indexes +alter index v2_users_email_key rename to users_email_key; + +alter index v2_users_google_id_key rename to users_google_id_key; + +alter index v2_users_pkey rename to users_pkey; + +alter index idx_v2_users_deleted_at rename to idx_users_deleted_at; + +/* + * sequences + */ +alter sequence v2_app_versions_id_seq rename to app_versions_id_seq; + +alter sequence v2_changesets_id_seq rename to changesets_id_seq; + +alter sequence v2_chart_releases_id_seq rename to chart_releases_id_seq; + +alter sequence v2_chart_versions_id_seq rename to chart_versions_id_seq; + +alter sequence v2_charts_id_seq rename to charts_id_seq; + +alter sequence v2_ci_identifiers_id_seq rename to ci_identifiers_id_seq; + +alter sequence v2_ci_runs_id_seq rename to ci_runs_id_seq; + +alter sequence v2_clusters_id_seq rename to clusters_id_seq; + +alter sequence v2_database_instances_id_seq rename to database_instances_id_seq; + +alter sequence v2_environments_id_seq rename to environments_id_seq; + +alter sequence v2_pagerduty_integrations_id_seq rename to pagerduty_integrations_id_seq; + +alter sequence v2_users_id_seq rename to users_id_seq; \ No newline at end of file From f8f75357a820e1fc066cc47e3dbfbcf29876cf3e Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Fri, 20 Oct 2023 16:34:42 -0400 Subject: [PATCH 06/21] final find and replace?? --- sherlock/internal/models/app_version_test.go | 2 +- sherlock/internal/models/chart_version_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sherlock/internal/models/app_version_test.go b/sherlock/internal/models/app_version_test.go index 5326dc722..867316dda 100644 --- a/sherlock/internal/models/app_version_test.go +++ b/sherlock/internal/models/app_version_test.go @@ -8,7 +8,7 @@ import ( func (s *modelSuite) TestAppVersionChartIdValidationSqlMissing() { s.SetNonSuitableTestUserForDB() err := s.DB.Create(&AppVersion{AppVersion: "version"}).Error - s.ErrorContains(err, "fk_v2_app_versions_chart") + s.ErrorContains(err, "fk_app_versions_chart") } func (s *modelSuite) TestAppVersionVersionValidationSqlMissing() { diff --git a/sherlock/internal/models/chart_version_test.go b/sherlock/internal/models/chart_version_test.go index c668d42a2..3afef8258 100644 --- a/sherlock/internal/models/chart_version_test.go +++ b/sherlock/internal/models/chart_version_test.go @@ -8,7 +8,7 @@ import ( func (s *modelSuite) TestChartVersionChartIdValidationSqlMissing() { s.SetNonSuitableTestUserForDB() err := s.DB.Create(&ChartVersion{ChartVersion: "version"}).Error - s.ErrorContains(err, "fk_v2_chart_versions_chart") + s.ErrorContains(err, "fk_chart_versions_chart") } func (s *modelSuite) TestChartVersionVersionValidationSqlMissing() { From cc0ad1c2a568cd00bf9eb2fb127c3d5b291899f2 Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Fri, 20 Oct 2023 17:07:52 -0400 Subject: [PATCH 07/21] dupes and typos --- .../migrations/000060_rename_tables.down.sql | 105 ++++++------------ .../db/migrations/000060_rename_tables.up.sql | 41 ------- 2 files changed, 32 insertions(+), 114 deletions(-) diff --git a/sherlock/db/migrations/000060_rename_tables.down.sql b/sherlock/db/migrations/000060_rename_tables.down.sql index a1dafd3e2..206a1172e 100644 --- a/sherlock/db/migrations/000060_rename_tables.down.sql +++ b/sherlock/db/migrations/000060_rename_tables.down.sql @@ -10,14 +10,12 @@ alter table v2_app_versions -- foreign keys alter table v2_app_versions - rename constraint fk_app_versions_chart to v2_fk_app_versions_chart; + rename constraint fk_app_versions_chart to fk_v2_app_versions_chart; alter table v2_app_versions - rename constraint fk_app_versions_parent_app_version to v2_fk_app_versions_parent_app_version; + rename constraint fk_app_versions_parent_app_version to fk_v2_app_versions_parent_app_version; -- indexes -alter index app_versions_pkey rename to v2_app_versions_pkey; - alter index idx_app_versions_deleted_at rename to v2_idx_app_versions_deleted_at; /* @@ -32,13 +30,10 @@ alter table v2_changeset_new_app_versions -- foreign keys alter table v2_changeset_new_app_versions - rename constraint fk_changeset_new_app_versions_app_version to v2_fk_changeset_new_app_versions_app_version; + rename constraint fk_changeset_new_app_versions_app_version to fk_v2_changeset_new_app_versions_app_version; alter table v2_changeset_new_app_versions - rename constraint fk_changeset_new_app_versions_changeset to v2_fk_changeset_new_app_versions_changeset; - --- indexes -alter index changeset_new_app_versions_pkey rename to v2_changeset_new_app_versions_pkey; + rename constraint fk_changeset_new_app_versions_changeset to fk_v2_changeset_new_app_versions_changeset; /* * changeset_new_chart_versions @@ -52,13 +47,10 @@ alter table v2_changeset_new_chart_versions -- foreign keys alter table v2_changeset_new_chart_versions - rename constraint fk_changeset_new_chart_versions_changeset to v2_fk_changeset_new_chart_versions_changeset; + rename constraint fk_changeset_new_chart_versions_changeset to fk_v2_changeset_new_chart_versions_changeset; alter table v2_changeset_new_chart_versions - rename constraint fk_changeset_new_chart_versions_chart_version to v2_fk_changeset_new_chart_versions_chart_version; - --- indexes -alter index changeset_new_chart_versions_pkey rename to v2_changeset_new_chart_versions_pkey; + rename constraint fk_changeset_new_chart_versions_chart_version to fk_v2_changeset_new_chart_versions_chart_version; /* * changesets @@ -72,35 +64,33 @@ alter table v2_changesets -- foreign keys alter table v2_changesets - rename constraint fk_changesets_chart_release to v2_fk_changesets_chart_release; + rename constraint fk_changesets_chart_release to fk_v2_changesets_chart_release; alter table v2_changesets - rename constraint fk_changesets_from_app_version to v2_fk_changesets_from_app_version; + rename constraint fk_changesets_from_app_version to fk_v2_changesets_from_app_version; alter table v2_changesets - rename constraint fk_changesets_from_chart_version to v2_fk_changesets_from_chart_version; + rename constraint fk_changesets_from_chart_version to fk_v2_changesets_from_chart_version; alter table v2_changesets - rename constraint fk_changesets_from_follow_chart_release_app_version to v2_fk_changesets_from_follow_chart_release_app_version; + rename constraint fk_changesets_from_follow_chart_release_app_version to fk_v2_changesets_from_follow_chart_release_app_version; alter table v2_changesets - rename constraint fk_changesets_from_follow_chart_release_chart_version to v2_fk_changesets_from_follow_chart_release_chart_version; + rename constraint fk_changesets_from_follow_chart_release_chart_version to fk_v2_changesets_from_follow_chart_release_chart_version; alter table v2_changesets - rename constraint fk_changesets_to_app_version to v2_fk_changesets_to_app_version; + rename constraint fk_changesets_to_app_version to fk_v2_changesets_to_app_version; alter table v2_changesets - rename constraint fk_changesets_to_chart_version to v2_fk_changesets_to_chart_version; + rename constraint fk_changesets_to_chart_version to fk_v2_changesets_to_chart_version; alter table v2_changesets - rename constraint fk_changesets_to_follow_chart_release_app_version to v2_fk_changesets_to_follow_chart_release_app_version; + rename constraint fk_changesets_to_follow_chart_release_app_version to fk_v2_changesets_to_follow_chart_release_app_version; alter table v2_changesets - rename constraint fk_changesets_to_follow_chart_release_chart_version to v2_fk_changesets_to_follow_chart_release_chart_version; + rename constraint fk_changesets_to_follow_chart_release_chart_version to fk_v2_changesets_to_follow_chart_release_chart_version; -- indexes -alter index changesets_pkey rename to v2_changesets_pkey; - alter index idx_changesets_deleted_at rename to v2_idx_changesets_deleted_at; /* @@ -115,32 +105,30 @@ alter table v2_chart_releases -- foreign keys alter table v2_chart_releases - rename constraint fk_chart_releases_app_version to v2_fk_chart_releases_app_version; + rename constraint fk_chart_releases_app_version to fk_v2_chart_releases_app_version; alter table v2_chart_releases - rename constraint fk_chart_releases_chart to v2_fk_chart_releases_chart; + rename constraint fk_chart_releases_chart to fk_v2_chart_releases_chart; alter table v2_chart_releases - rename constraint fk_chart_releases_chart_version to v2_fk_chart_releases_chart_version; + rename constraint fk_chart_releases_chart_version to fk_v2_chart_releases_chart_version; alter table v2_chart_releases - rename constraint fk_chart_releases_cluster to v2_fk_chart_releases_cluster; + rename constraint fk_chart_releases_cluster to fk_v2_chart_releases_cluster; alter table v2_chart_releases - rename constraint fk_chart_releases_environment to v2_fk_chart_releases_environment; + rename constraint fk_chart_releases_environment to fk_v2_chart_releases_environment; alter table v2_chart_releases - rename constraint fk_chart_releases_follow_chart_release_app_version to v2_fk_chart_releases_follow_chart_release_app_version; + rename constraint fk_chart_releases_follow_chart_release_app_version to fk_v2_chart_releases_follow_chart_release_app_version; alter table v2_chart_releases - rename constraint fk_chart_releases_follow_chart_release_chart_version to v2_fk_chart_releases_follow_chart_release_chart_version; + rename constraint fk_chart_releases_follow_chart_release_chart_version to fk_v2_chart_releases_follow_chart_release_chart_version; alter table v2_chart_releases - rename constraint fk_chart_releases_pagerduty_integration to v2_fk_chart_releases_pagerduty_integration; + rename constraint fk_chart_releases_pagerduty_integration to fk_v2_chart_releases_pagerduty_integration; -- indexes -alter index chart_releases_pkey rename to v2_chart_releases_pkey; - alter index idx_chart_releases_deleted_at rename to v2_idx_chart_releases_deleted_at; /* @@ -155,14 +143,12 @@ alter table v2_chart_versions -- foreign keys alter table v2_chart_versions - rename constraint fk_chart_versions_chart to v2_fk_chart_versions_chart; + rename constraint fk_chart_versions_chart to fk_v2_chart_versions_chart; alter table v2_chart_versions - rename constraint fk_chart_versions_parent_chart_version to v2_fk_chart_versions_parent_chart_version; + rename constraint fk_chart_versions_parent_chart_version to fk_v2_chart_versions_parent_chart_version; -- indexes -alter index chart_versions_pkey rename to v2_chart_versions_pkey; - alter index idx_chart_versions_deleted_at rename to v2_idx_chart_versions_deleted_at; /* @@ -179,10 +165,6 @@ alter table v2_charts rename constraint charts_name_key to v2_charts_name_key; -- indexes -alter index charts_name_key rename to v2_charts_name_key; - -alter index charts_pkey rename to v2_charts_pkey; - alter index idx_charts_deleted_at rename to v2_idx_charts_deleted_at; /* @@ -196,8 +178,6 @@ alter table v2_ci_identifiers rename constraint ci_identifiers_pkey to v2_ci_identifiers_pkey; -- indexes -alter index ci_identifiers_pkey rename to v2_ci_identifiers_pkey; - alter index idx_ci_identifiers_deleted_at rename to v2_idx_ci_identifiers_deleted_at; alter index idx_ci_identifiers_polymorphic_index rename to v2_idx_ci_identifiers_polymorphic_index; @@ -213,8 +193,6 @@ alter table v2_ci_runs rename constraint ci_runs_pkey to v2_ci_runs_pkey; -- indexes -alter index ci_runs_pkey rename to v2_ci_runs_pkey; - alter index idx_ci_runs_deleted_at rename to v2_idx_ci_runs_deleted_at; /* @@ -229,13 +207,10 @@ alter table v2_ci_runs_for_identifiers -- foreign keys alter table v2_ci_runs_for_identifiers - rename constraint fk_ci_runs_for_identifiers_ci_identifier to v2_fk_ci_runs_for_identifiers_ci_identifier; + rename constraint fk_ci_runs_for_identifiers_ci_identifier to fk_v2_ci_runs_for_identifiers_ci_identifier; alter table v2_ci_runs_for_identifiers - rename constraint fk_ci_runs_for_identifiers_ci_run to v2_fk_ci_runs_for_identifiers_ci_run; - --- indexes -alter index ci_runs_for_identifiers_pkey rename to v2_ci_runs_for_identifiers_pkey; + rename constraint fk_ci_runs_for_identifiers_ci_run to fk_v2_ci_runs_for_identifiers_ci_run; /* * clusters @@ -251,10 +226,6 @@ alter table v2_clusters rename constraint clusters_name_key to v2_clusters_name_key; -- indexes -alter index clusters_name_key rename to v2_clusters_name_key; - -alter index clusters_pkey rename to v2_clusters_pkey; - alter index idx_clusters_deleted_at rename to v2_idx_clusters_deleted_at; /* @@ -269,11 +240,9 @@ alter table v2_database_instances -- foreign keys alter table v2_database_instances - rename constraint fk_database_instances_chart_release to v2_fk_database_instances_chart_release; + rename constraint fk_database_instances_chart_release to fk_v2_database_instances_chart_release; -- indexes -alter index database_instances_pkey rename to v2_database_instances_pkey; - alter index idx_database_instances_deleted_at rename to v2_idx_database_instances_deleted_at; /* @@ -288,20 +257,18 @@ alter table v2_environments -- foreign keys alter table v2_environments - rename constraint fk_environments_default_cluster to v2_fk_environments_default_cluster; + rename constraint fk_environments_default_cluster to fk_v2_environments_default_cluster; alter table v2_environments - rename constraint fk_environments_owner to v2_fk_environments_owner; + rename constraint fk_environments_owner to fk_v2_environments_owner; alter table v2_environments - rename constraint fk_environments_pagerduty_integration to v2_fk_environments_pagerduty_integration; + rename constraint fk_environments_pagerduty_integration to fk_v2_environments_pagerduty_integration; alter table v2_environments - rename constraint fk_environments_template_environment to v2_fk_environments_template_environment; + rename constraint fk_environments_template_environment to fk_v2_environments_template_environment; -- indexes -alter index environments_pkey rename to v2_environments_pkey; - alter index idx_environments_deleted_at rename to v2_idx_environments_deleted_at; /* @@ -315,8 +282,6 @@ alter table v2_pagerduty_integrations rename constraint pagerduty_integrations_pkey to v2_pagerduty_integrations_pkey; -- indexes -alter index pagerduty_integrations_pkey rename to v2_pagerduty_integrations_pkey; - alter index idx_pagerduty_integrations_deleted_at rename to v2_idx_pagerduty_integrations_deleted_at; /* @@ -336,12 +301,6 @@ alter table v2_users rename constraint users_google_id_key to v2_users_google_id_key; -- indexes -alter index users_email_key rename to v2_users_email_key; - -alter index users_google_id_key rename to v2_users_google_id_key; - -alter index users_pkey rename to v2_users_pkey; - alter index idx_users_deleted_at rename to v2_idx_users_deleted_at; /* diff --git a/sherlock/db/migrations/000060_rename_tables.up.sql b/sherlock/db/migrations/000060_rename_tables.up.sql index 2e674bc76..b3278838a 100644 --- a/sherlock/db/migrations/000060_rename_tables.up.sql +++ b/sherlock/db/migrations/000060_rename_tables.up.sql @@ -16,8 +16,6 @@ alter table app_versions rename constraint fk_v2_app_versions_parent_app_version to fk_app_versions_parent_app_version; -- indexes -alter index v2_app_versions_pkey rename to app_versions_pkey; - alter index idx_v2_app_versions_deleted_at rename to idx_app_versions_deleted_at; /* @@ -37,9 +35,6 @@ alter table changeset_new_app_versions alter table changeset_new_app_versions rename constraint fk_v2_changeset_new_app_versions_changeset to fk_changeset_new_app_versions_changeset; --- indexes -alter index v2_changeset_new_app_versions_pkey rename to changeset_new_app_versions_pkey; - /* * changeset_new_chart_versions */ @@ -57,9 +52,6 @@ alter table changeset_new_chart_versions alter table changeset_new_chart_versions rename constraint fk_v2_changeset_new_chart_versions_chart_version to fk_changeset_new_chart_versions_chart_version; --- indexes -alter index v2_changeset_new_chart_versions_pkey rename to changeset_new_chart_versions_pkey; - /* * changesets */ @@ -99,8 +91,6 @@ alter table changesets rename constraint fk_v2_changesets_to_follow_chart_release_chart_version to fk_changesets_to_follow_chart_release_chart_version; -- indexes -alter index v2_changesets_pkey rename to changesets_pkey; - alter index idx_v2_changesets_deleted_at rename to idx_changesets_deleted_at; /* @@ -139,8 +129,6 @@ alter table chart_releases rename constraint fk_v2_chart_releases_pagerduty_integration to fk_chart_releases_pagerduty_integration; -- indexes -alter index v2_chart_releases_pkey rename to chart_releases_pkey; - alter index idx_v2_chart_releases_deleted_at rename to idx_chart_releases_deleted_at; /* @@ -161,8 +149,6 @@ alter table chart_versions rename constraint fk_v2_chart_versions_parent_chart_version to fk_chart_versions_parent_chart_version; -- indexes -alter index v2_chart_versions_pkey rename to chart_versions_pkey; - alter index idx_v2_chart_versions_deleted_at rename to idx_chart_versions_deleted_at; /* @@ -179,10 +165,6 @@ alter table charts rename constraint v2_charts_name_key to charts_name_key; -- indexes -alter index v2_charts_name_key rename to charts_name_key; - -alter index v2_charts_pkey rename to charts_pkey; - alter index idx_v2_charts_deleted_at rename to idx_charts_deleted_at; /* @@ -196,8 +178,6 @@ alter table ci_identifiers rename constraint v2_ci_identifiers_pkey to ci_identifiers_pkey; -- indexes -alter index v2_ci_identifiers_pkey rename to ci_identifiers_pkey; - alter index idx_v2_ci_identifiers_deleted_at rename to idx_ci_identifiers_deleted_at; alter index idx_v2_ci_identifiers_polymorphic_index rename to idx_ci_identifiers_polymorphic_index; @@ -213,8 +193,6 @@ alter table ci_runs rename constraint v2_ci_runs_pkey to ci_runs_pkey; -- indexes -alter index v2_ci_runs_pkey rename to ci_runs_pkey; - alter index idx_v2_ci_runs_deleted_at rename to idx_ci_runs_deleted_at; /* @@ -234,9 +212,6 @@ alter table ci_runs_for_identifiers alter table ci_runs_for_identifiers rename constraint fk_v2_ci_runs_for_identifiers_ci_run to fk_ci_runs_for_identifiers_ci_run; --- indexes -alter index v2_ci_runs_for_identifiers_pkey rename to ci_runs_for_identifiers_pkey; - /* * clusters */ @@ -251,10 +226,6 @@ alter table clusters rename constraint v2_clusters_name_key to clusters_name_key; -- indexes -alter index v2_clusters_name_key rename to clusters_name_key; - -alter index v2_clusters_pkey rename to clusters_pkey; - alter index idx_v2_clusters_deleted_at rename to idx_clusters_deleted_at; /* @@ -272,8 +243,6 @@ alter table database_instances rename constraint fk_v2_database_instances_chart_release to fk_database_instances_chart_release; -- indexes -alter index v2_database_instances_pkey rename to database_instances_pkey; - alter index idx_v2_database_instances_deleted_at rename to idx_database_instances_deleted_at; /* @@ -300,8 +269,6 @@ alter table environments rename constraint fk_v2_environments_template_environment to fk_environments_template_environment; -- indexes -alter index v2_environments_pkey rename to environments_pkey; - alter index idx_v2_environments_deleted_at rename to idx_environments_deleted_at; /* @@ -315,8 +282,6 @@ alter table pagerduty_integrations rename constraint v2_pagerduty_integrations_pkey to pagerduty_integrations_pkey; -- indexes -alter index v2_pagerduty_integrations_pkey rename to pagerduty_integrations_pkey; - alter index idx_v2_pagerduty_integrations_deleted_at rename to idx_pagerduty_integrations_deleted_at; /* @@ -336,12 +301,6 @@ alter table users rename constraint v2_users_google_id_key to users_google_id_key; -- indexes -alter index v2_users_email_key rename to users_email_key; - -alter index v2_users_google_id_key rename to users_google_id_key; - -alter index v2_users_pkey rename to users_pkey; - alter index idx_v2_users_deleted_at rename to idx_users_deleted_at; /* From 5cd97ac5001b9d02549945c94983d8d55b996741 Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Tue, 24 Oct 2023 11:54:22 -0400 Subject: [PATCH 08/21] replace in depracated files and fix typo in down --- .../migrations/000060_rename_tables.down.sql | 26 ++--- .../deprecated_models/v2models/changeset.go | 86 +++++++-------- .../deprecated_models/v2models/chart.go | 2 +- .../v2models/chart_release.go | 2 +- .../v2models/chart_version.go | 2 +- .../v2models/ci_identifier.go | 8 +- .../deprecated_models/v2models/ci_run.go | 4 +- .../deprecated_models/v2models/cluster.go | 2 +- .../v2models/database_instance.go | 2 +- .../deprecated_models/v2models/environment.go | 2 +- .../v2models/metrics_updater.go | 102 +++++++++--------- .../v2models/pagerduty_integration.go | 2 +- .../deprecated_models/v2models/user.go | 2 +- sherlock/internal/metrics/metrics.go | 52 ++++----- .../models/deploy_hook_trigger_config.go | 2 +- .../models/github_actions_deploy_hook.go | 2 +- sherlock/internal/models/slack_deploy_hook.go | 2 +- 17 files changed, 150 insertions(+), 150 deletions(-) diff --git a/sherlock/db/migrations/000060_rename_tables.down.sql b/sherlock/db/migrations/000060_rename_tables.down.sql index 206a1172e..8b7dcb73d 100644 --- a/sherlock/db/migrations/000060_rename_tables.down.sql +++ b/sherlock/db/migrations/000060_rename_tables.down.sql @@ -16,7 +16,7 @@ alter table v2_app_versions rename constraint fk_app_versions_parent_app_version to fk_v2_app_versions_parent_app_version; -- indexes -alter index idx_app_versions_deleted_at rename to v2_idx_app_versions_deleted_at; +alter index idx_app_versions_deleted_at rename to idx_v2_app_versions_deleted_at; /* * changeset_new_app_versions @@ -91,7 +91,7 @@ alter table v2_changesets rename constraint fk_changesets_to_follow_chart_release_chart_version to fk_v2_changesets_to_follow_chart_release_chart_version; -- indexes -alter index idx_changesets_deleted_at rename to v2_idx_changesets_deleted_at; +alter index idx_changesets_deleted_at rename to idx_v2_changesets_deleted_at; /* * chart_releases @@ -129,7 +129,7 @@ alter table v2_chart_releases rename constraint fk_chart_releases_pagerduty_integration to fk_v2_chart_releases_pagerduty_integration; -- indexes -alter index idx_chart_releases_deleted_at rename to v2_idx_chart_releases_deleted_at; +alter index idx_chart_releases_deleted_at rename to idx_v2_chart_releases_deleted_at; /* * chart_versions @@ -149,7 +149,7 @@ alter table v2_chart_versions rename constraint fk_chart_versions_parent_chart_version to fk_v2_chart_versions_parent_chart_version; -- indexes -alter index idx_chart_versions_deleted_at rename to v2_idx_chart_versions_deleted_at; +alter index idx_chart_versions_deleted_at rename to idx_v2_chart_versions_deleted_at; /* * charts @@ -165,7 +165,7 @@ alter table v2_charts rename constraint charts_name_key to v2_charts_name_key; -- indexes -alter index idx_charts_deleted_at rename to v2_idx_charts_deleted_at; +alter index idx_charts_deleted_at rename to idx_v2_charts_deleted_at; /* * ci_identifiers @@ -178,9 +178,9 @@ alter table v2_ci_identifiers rename constraint ci_identifiers_pkey to v2_ci_identifiers_pkey; -- indexes -alter index idx_ci_identifiers_deleted_at rename to v2_idx_ci_identifiers_deleted_at; +alter index idx_ci_identifiers_deleted_at rename to idx_v2_ci_identifiers_deleted_at; -alter index idx_ci_identifiers_polymorphic_index rename to v2_idx_ci_identifiers_polymorphic_index; +alter index idx_ci_identifiers_polymorphic_index rename to idx_v2_ci_identifiers_polymorphic_index; /* * ci_runs @@ -193,7 +193,7 @@ alter table v2_ci_runs rename constraint ci_runs_pkey to v2_ci_runs_pkey; -- indexes -alter index idx_ci_runs_deleted_at rename to v2_idx_ci_runs_deleted_at; +alter index idx_ci_runs_deleted_at rename to idx_v2_ci_runs_deleted_at; /* * ci_runs_for_identifiers @@ -226,7 +226,7 @@ alter table v2_clusters rename constraint clusters_name_key to v2_clusters_name_key; -- indexes -alter index idx_clusters_deleted_at rename to v2_idx_clusters_deleted_at; +alter index idx_clusters_deleted_at rename to idx_v2_clusters_deleted_at; /* * database_instances @@ -243,7 +243,7 @@ alter table v2_database_instances rename constraint fk_database_instances_chart_release to fk_v2_database_instances_chart_release; -- indexes -alter index idx_database_instances_deleted_at rename to v2_idx_database_instances_deleted_at; +alter index idx_database_instances_deleted_at rename to idx_v2_database_instances_deleted_at; /* * environments @@ -269,7 +269,7 @@ alter table v2_environments rename constraint fk_environments_template_environment to fk_v2_environments_template_environment; -- indexes -alter index idx_environments_deleted_at rename to v2_idx_environments_deleted_at; +alter index idx_environments_deleted_at rename to idx_v2_environments_deleted_at; /* * pagerduty_integration @@ -282,7 +282,7 @@ alter table v2_pagerduty_integrations rename constraint pagerduty_integrations_pkey to v2_pagerduty_integrations_pkey; -- indexes -alter index idx_pagerduty_integrations_deleted_at rename to v2_idx_pagerduty_integrations_deleted_at; +alter index idx_pagerduty_integrations_deleted_at rename to idx_v2_pagerduty_integrations_deleted_at; /* * users @@ -301,7 +301,7 @@ alter table v2_users rename constraint users_google_id_key to v2_users_google_id_key; -- indexes -alter index idx_users_deleted_at rename to v2_idx_users_deleted_at; +alter index idx_users_deleted_at rename to idx_v2_users_deleted_at; /* * sequences diff --git a/sherlock/internal/deprecated_models/v2models/changeset.go b/sherlock/internal/deprecated_models/v2models/changeset.go index b811ca4d0..f7148c4f2 100644 --- a/sherlock/internal/deprecated_models/v2models/changeset.go +++ b/sherlock/internal/deprecated_models/v2models/changeset.go @@ -27,8 +27,8 @@ type Changeset struct { To ChartReleaseVersion `gorm:"embedded;embeddedPrefix:to_"` AppliedAt *time.Time SupersededAt *time.Time - NewAppVersions []*AppVersion `gorm:"many2many:v2_changeset_new_app_versions;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` - NewChartVersions []*ChartVersion `gorm:"many2many:v2_changeset_new_chart_versions;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` + NewAppVersions []*AppVersion `gorm:"many2many:changeset_new_app_versions;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` + NewChartVersions []*ChartVersion `gorm:"many2many:changeset_new_chart_versions;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` PlannedBy *User PlannedByID *uint @@ -37,7 +37,7 @@ type Changeset struct { } func (c Changeset) TableName() string { - return "v2_changesets" + return "changesets" } func (c Changeset) getID() uint { @@ -346,42 +346,42 @@ func (s *internalChangesetStore) queryAppliedForVersion(db *gorm.DB, chartID uin // If we have record of the version, it would show up in the "new versions" introduced by a changeset. // Use that to match so we have a shot at catching deployments where this version was an intermediary. if err = db.Raw(` -select v2_changesets.id -from v2_changesets +select changesets.id +from changesets -- Join through changelog table to the app version we care about - inner join v2_changeset_new_app_versions - on v2_changeset_new_app_versions.changeset_id = v2_changesets.id - and v2_changeset_new_app_versions.app_version_id = ? + inner join changeset_new_app_versions + on changeset_new_app_versions.changeset_id = changesets.id + and changeset_new_app_versions.app_version_id = ? -- Join through to chart releases to filter out deleted/irrelevant ones - inner join v2_chart_releases - on v2_chart_releases.id = v2_changesets.chart_release_id - and v2_chart_releases.deleted_at is null - and v2_chart_releases.chart_id = ? + inner join chart_releases + on chart_releases.id = changesets.chart_release_id + and chart_releases.deleted_at is null + and chart_releases.chart_id = ? -- Filter to changes that actually got applied -where v2_changesets.applied_at is not null +where changesets.applied_at is not null -order by v2_changesets.applied_at desc`, appVersionIfExists.ID, chartID).Scan(&changesetIDs).Error; err != nil { +order by changesets.applied_at desc`, appVersionIfExists.ID, chartID).Scan(&changesetIDs).Error; err != nil { return ret, err } } else { // If we have no record of the version at all, it might just be a custom string that was entered // into the API. All we can do is match based on changesets that specifically deployed that version. if err = db.Raw(` -select v2_changesets.id -from v2_changesets +select changesets.id +from changesets -- Join through to chart releases to filter out deleted/irrelevant ones - inner join v2_chart_releases - on v2_chart_releases.id = v2_changesets.chart_release_id - and v2_chart_releases.deleted_at is null - and v2_chart_releases.chart_id = ? + inner join chart_releases + on chart_releases.id = changesets.chart_release_id + and chart_releases.deleted_at is null + and chart_releases.chart_id = ? -- Filter to changes that actually got applied and target our version -where v2_changesets.applied_at is not null - and v2_changesets.to_app_version_exact = ? +where changesets.applied_at is not null + and changesets.to_app_version_exact = ? -order by v2_changesets.applied_at desc +order by changesets.applied_at desc `, chartID, version).Error; err != nil { return ret, err } @@ -394,42 +394,42 @@ order by v2_changesets.applied_at desc // If we have record of the version, it would show up in the "new versions" introduced by a changeset. // Use that to match so we have a shot at catching deployments where this version was an intermediary. if err = db.Raw(` -select v2_changesets.id -from v2_changesets +select changesets.id +from changesets -- Join through changelog table to the chart version we care about - inner join v2_changeset_new_chart_versions - on v2_changeset_new_chart_versions.changeset_id = v2_changesets.id - and v2_changeset_new_chart_versions.chart_version_id = ? + inner join changeset_new_chart_versions + on changeset_new_chart_versions.changeset_id = changesets.id + and changeset_new_chart_versions.chart_version_id = ? -- Join through to chart releases to filter out deleted/irrelevant ones - inner join v2_chart_releases - on v2_chart_releases.id = v2_changesets.chart_release_id - and v2_chart_releases.deleted_at is null - and v2_chart_releases.chart_id = ? + inner join chart_releases + on chart_releases.id = changesets.chart_release_id + and chart_releases.deleted_at is null + and chart_releases.chart_id = ? -- Filter to changes that actually got applied -where v2_changesets.applied_at is not null +where changesets.applied_at is not null -order by v2_changesets.applied_at desc`, chartVersionIfExists.ID, chartID).Scan(&changesetIDs).Error; err != nil { +order by changesets.applied_at desc`, chartVersionIfExists.ID, chartID).Scan(&changesetIDs).Error; err != nil { return ret, err } } else { // If we have no record of the version at all, it might just be a custom string that was entered // into the API. All we can do is match based on changesets that specifically deployed that version. if err = db.Raw(` -select v2_changesets.id -from v2_changesets +select changesets.id +from changesets -- Join through to chart releases to filter out deleted/irrelevant ones - inner join v2_chart_releases - on v2_chart_releases.id = v2_changesets.chart_release_id - and v2_chart_releases.deleted_at is null - and v2_chart_releases.chart_id = ? + inner join chart_releases + on chart_releases.id = changesets.chart_release_id + and chart_releases.deleted_at is null + and chart_releases.chart_id = ? -- Filter to changes that actually got applied and target our version -where v2_changesets.applied_at is not null - and v2_changesets.to_chart_version_exact = ? +where changesets.applied_at is not null + and changesets.to_chart_version_exact = ? -order by v2_changesets.applied_at desc +order by changesets.applied_at desc `, chartID, version).Error; err != nil { return ret, err } diff --git a/sherlock/internal/deprecated_models/v2models/chart.go b/sherlock/internal/deprecated_models/v2models/chart.go index a62698cb4..d2c8739c0 100644 --- a/sherlock/internal/deprecated_models/v2models/chart.go +++ b/sherlock/internal/deprecated_models/v2models/chart.go @@ -28,7 +28,7 @@ type Chart struct { } func (c Chart) TableName() string { - return "v2_charts" + return "charts" } func (c Chart) getID() uint { diff --git a/sherlock/internal/deprecated_models/v2models/chart_release.go b/sherlock/internal/deprecated_models/v2models/chart_release.go index 0ce5ce8fd..427955487 100644 --- a/sherlock/internal/deprecated_models/v2models/chart_release.go +++ b/sherlock/internal/deprecated_models/v2models/chart_release.go @@ -33,7 +33,7 @@ type ChartRelease struct { } func (c ChartRelease) TableName() string { - return "v2_chart_releases" + return "chart_releases" } func (c ChartRelease) getID() uint { diff --git a/sherlock/internal/deprecated_models/v2models/chart_version.go b/sherlock/internal/deprecated_models/v2models/chart_version.go index 9c659e91f..39af3d9b0 100644 --- a/sherlock/internal/deprecated_models/v2models/chart_version.go +++ b/sherlock/internal/deprecated_models/v2models/chart_version.go @@ -21,7 +21,7 @@ type ChartVersion struct { } func (c ChartVersion) TableName() string { - return "v2_chart_versions" + return "chart_versions" } func (c ChartVersion) getID() uint { diff --git a/sherlock/internal/deprecated_models/v2models/ci_identifier.go b/sherlock/internal/deprecated_models/v2models/ci_identifier.go index bd2aa3771..74a1f20dd 100644 --- a/sherlock/internal/deprecated_models/v2models/ci_identifier.go +++ b/sherlock/internal/deprecated_models/v2models/ci_identifier.go @@ -27,14 +27,14 @@ type CiIdentifiable interface { type CiIdentifier struct { gorm.Model - ResourceType string `gorm:"index:idx_v2_ci_identifiers_polymorphic_index,priority:1"` - ResourceID uint `gorm:"index:idx_v2_ci_identifiers_polymorphic_index,priority:2"` + ResourceType string `gorm:"index:idx_ci_identifiers_polymorphic_index,priority:1"` + ResourceID uint `gorm:"index:idx_ci_identifiers_polymorphic_index,priority:2"` // Mutable - CiRuns []*CiRun `gorm:"many2many:v2_ci_runs_for_identifiers; constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` + CiRuns []*CiRun `gorm:"many2many:ci_runs_for_identifiers; constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` } func (c CiIdentifier) TableName() string { - return "v2_ci_identifiers" + return "ci_identifiers" } func (c CiIdentifier) getID() uint { diff --git a/sherlock/internal/deprecated_models/v2models/ci_run.go b/sherlock/internal/deprecated_models/v2models/ci_run.go index 277427bd5..18ce629e0 100644 --- a/sherlock/internal/deprecated_models/v2models/ci_run.go +++ b/sherlock/internal/deprecated_models/v2models/ci_run.go @@ -24,14 +24,14 @@ type CiRun struct { ArgoWorkflowsName string ArgoWorkflowsTemplate string // Mutable - RelatedResources []*CiIdentifier `gorm:"many2many:v2_ci_runs_for_identifiers; constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` + RelatedResources []*CiIdentifier `gorm:"many2many:ci_runs_for_identifiers; constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` StartedAt *time.Time TerminalAt *time.Time Status *string } func (c CiRun) TableName() string { - return "v2_ci_runs" + return "ci_runs" } func (c CiRun) getID() uint { diff --git a/sherlock/internal/deprecated_models/v2models/cluster.go b/sherlock/internal/deprecated_models/v2models/cluster.go index 5441768e2..1c5837c2a 100644 --- a/sherlock/internal/deprecated_models/v2models/cluster.go +++ b/sherlock/internal/deprecated_models/v2models/cluster.go @@ -27,7 +27,7 @@ type Cluster struct { } func (c Cluster) TableName() string { - return "v2_clusters" + return "clusters" } func (c Cluster) getID() uint { diff --git a/sherlock/internal/deprecated_models/v2models/database_instance.go b/sherlock/internal/deprecated_models/v2models/database_instance.go index aa331b0c2..4184aa86f 100644 --- a/sherlock/internal/deprecated_models/v2models/database_instance.go +++ b/sherlock/internal/deprecated_models/v2models/database_instance.go @@ -24,7 +24,7 @@ type DatabaseInstance struct { } func (d DatabaseInstance) TableName() string { - return "v2_database_instances" + return "database_instances" } func (d DatabaseInstance) getID() uint { diff --git a/sherlock/internal/deprecated_models/v2models/environment.go b/sherlock/internal/deprecated_models/v2models/environment.go index 5833ff2a6..0905dcd5b 100644 --- a/sherlock/internal/deprecated_models/v2models/environment.go +++ b/sherlock/internal/deprecated_models/v2models/environment.go @@ -58,7 +58,7 @@ type Environment struct { } func (e Environment) TableName() string { - return "v2_environments" + return "environments" } func (e Environment) getID() uint { diff --git a/sherlock/internal/deprecated_models/v2models/metrics_updater.go b/sherlock/internal/deprecated_models/v2models/metrics_updater.go index 4c7097cd8..2aec47c79 100644 --- a/sherlock/internal/deprecated_models/v2models/metrics_updater.go +++ b/sherlock/internal/deprecated_models/v2models/metrics_updater.go @@ -122,9 +122,9 @@ func calculateVersionLeadTimes(db *gorm.DB) (map[uint]int64, map[uint]int64, err // version diff is known to Sherlock--it ignores custom/unreported versions that we wouldn't have timestamps for // anyway. // - // This means that the underlying many2many table, v2_changeset_new_app_versions, is a treasure trove of "what - // app versions were deployed by what changesets". We use this many2many relation to associate v2_changesets's - // chart_release_id and applied_at time to v2_app_versions's created_at time (filtering for Changesets that + // This means that the underlying many2many table, changeset_new_app_versions, is a treasure trove of "what + // app versions were deployed by what changesets". We use this many2many relation to associate changesets's + // chart_release_id and applied_at time to app_versions's created_at time (filtering for Changesets that // were actually applied and AppVersions from the past 30 days). Those two times are the interval that we care about // for lead time--we can subtract them to get the duration between an app version being created and being deployed // to that particular chart release. Knowing that interval associated to a given ChartRelease ID is enough for us @@ -147,14 +147,14 @@ func calculateVersionLeadTimes(db *gorm.DB) (map[uint]int64, map[uint]int64, err // final lead time--the average lead time to deploy to it over the last 30 days. if err := db.Raw(` select result_per_version.chart_release_id, round(avg(result_per_version.lead_time_seconds))::bigint as lead_time_seconds -from (select v2_changesets.chart_release_id, - extract(epoch from (min(v2_changesets.applied_at) - min(v2_app_versions.created_at))) as lead_time_seconds - from v2_changesets - inner join v2_changeset_new_app_versions on v2_changesets.id = v2_changeset_new_app_versions.changeset_id - inner join v2_app_versions on v2_changeset_new_app_versions.app_version_id = v2_app_versions.id - where v2_changesets.applied_at is not null - and v2_app_versions.created_at >= current_timestamp - '30 days'::interval - group by v2_changesets.chart_release_id, v2_app_versions.id) as result_per_version +from (select changesets.chart_release_id, + extract(epoch from (min(changesets.applied_at) - min(app_versions.created_at))) as lead_time_seconds + from changesets + inner join changeset_new_app_versions on changesets.id = changeset_new_app_versions.changeset_id + inner join app_versions on changeset_new_app_versions.app_version_id = app_versions.id + where changesets.applied_at is not null + and app_versions.created_at >= current_timestamp - '30 days'::interval + group by changesets.chart_release_id, app_versions.id) as result_per_version group by result_per_version.chart_release_id `).Scan(&appVersionResults).Error; err != nil { return nil, nil, err @@ -169,14 +169,14 @@ group by result_per_version.chart_release_id chartVersionLeadTimes := make(map[uint]int64) if err := db.Raw(` select result_per_version.chart_release_id, round(avg(result_per_version.lead_time_seconds))::bigint as lead_time_seconds -from (select v2_changesets.chart_release_id, - extract(epoch from (min(v2_changesets.applied_at) - min(v2_chart_versions.created_at))) as lead_time_seconds - from v2_changesets - inner join v2_changeset_new_chart_versions on v2_changesets.id = v2_changeset_new_chart_versions.changeset_id - inner join v2_chart_versions on v2_changeset_new_chart_versions.chart_version_id = v2_chart_versions.id - where v2_changesets.applied_at is not null - and v2_chart_versions.created_at >= current_timestamp - '30 days'::interval - group by v2_changesets.chart_release_id, v2_chart_versions.id) as result_per_version +from (select changesets.chart_release_id, + extract(epoch from (min(changesets.applied_at) - min(chart_versions.created_at))) as lead_time_seconds + from changesets + inner join changeset_new_chart_versions on changesets.id = changeset_new_chart_versions.changeset_id + inner join chart_versions on changeset_new_chart_versions.chart_version_id = chart_versions.id + where changesets.applied_at is not null + and chart_versions.created_at >= current_timestamp - '30 days'::interval + group by changesets.chart_release_id, chart_versions.id) as result_per_version group by result_per_version.chart_release_id `).Scan(&chartVersionResults).Error; err != nil { return nil, nil, err @@ -257,61 +257,61 @@ func reportGitHubActionMetrics(ctx context.Context, db *gorm.DB) error { } var results []CompletionResult if err := db.Raw(` -select v2_ci_runs.github_actions_owner, - v2_ci_runs.github_actions_repo, - v2_ci_runs.github_actions_workflow_path, - v2_ci_runs.status, +select ci_runs.github_actions_owner, + ci_runs.github_actions_repo, + ci_runs.github_actions_workflow_path, + ci_runs.status, count(1) - filter (where v2_ci_runs.github_actions_attempt_number = 1 - and v2_ci_runs.terminal_at >= current_timestamp - '1 hour'::interval) + filter (where ci_runs.github_actions_attempt_number = 1 + and ci_runs.terminal_at >= current_timestamp - '1 hour'::interval) as hourly_first_attempts, - coalesce(round(sum(extract(epoch from v2_ci_runs.terminal_at - v2_ci_runs.started_at)) - filter (where v2_ci_runs.github_actions_attempt_number = 1 - and v2_ci_runs.terminal_at >= current_timestamp - '1 hour'::interval))::bigint, 0) + coalesce(round(sum(extract(epoch from ci_runs.terminal_at - ci_runs.started_at)) + filter (where ci_runs.github_actions_attempt_number = 1 + and ci_runs.terminal_at >= current_timestamp - '1 hour'::interval))::bigint, 0) as hourly_first_attempts_duration, count(1) - filter (where v2_ci_runs.github_actions_attempt_number > 1 - and v2_ci_runs.terminal_at >= current_timestamp - '1 hour'::interval) + filter (where ci_runs.github_actions_attempt_number > 1 + and ci_runs.terminal_at >= current_timestamp - '1 hour'::interval) as hourly_retries, - coalesce(round(sum(extract(epoch from v2_ci_runs.terminal_at - v2_ci_runs.started_at)) - filter (where v2_ci_runs.github_actions_attempt_number > 1 - and v2_ci_runs.terminal_at >= current_timestamp - '1 hour'::interval))::bigint, 0) + coalesce(round(sum(extract(epoch from ci_runs.terminal_at - ci_runs.started_at)) + filter (where ci_runs.github_actions_attempt_number > 1 + and ci_runs.terminal_at >= current_timestamp - '1 hour'::interval))::bigint, 0) as hourly_retries_duration, count(1) - filter (where v2_ci_runs.github_actions_attempt_number = 1 - and v2_ci_runs.terminal_at >= current_timestamp - '7 days'::interval) + filter (where ci_runs.github_actions_attempt_number = 1 + and ci_runs.terminal_at >= current_timestamp - '7 days'::interval) as weekly_first_attempts, - coalesce(round(sum(extract(epoch from v2_ci_runs.terminal_at - v2_ci_runs.started_at)) - filter (where v2_ci_runs.github_actions_attempt_number = 1 - and v2_ci_runs.terminal_at >= current_timestamp - '7 days'::interval))::bigint, 0) + coalesce(round(sum(extract(epoch from ci_runs.terminal_at - ci_runs.started_at)) + filter (where ci_runs.github_actions_attempt_number = 1 + and ci_runs.terminal_at >= current_timestamp - '7 days'::interval))::bigint, 0) as weekly_first_attempts_duration, count(1) - filter (where v2_ci_runs.github_actions_attempt_number > 1 - and v2_ci_runs.terminal_at >= current_timestamp - '7 days'::interval) + filter (where ci_runs.github_actions_attempt_number > 1 + and ci_runs.terminal_at >= current_timestamp - '7 days'::interval) as weekly_retries, - coalesce(round(sum(extract(epoch from v2_ci_runs.terminal_at - v2_ci_runs.started_at)) - filter (where v2_ci_runs.github_actions_attempt_number > 1 - and v2_ci_runs.terminal_at >= current_timestamp - '7 days'::interval))::bigint, 0) + coalesce(round(sum(extract(epoch from ci_runs.terminal_at - ci_runs.started_at)) + filter (where ci_runs.github_actions_attempt_number > 1 + and ci_runs.terminal_at >= current_timestamp - '7 days'::interval))::bigint, 0) as weekly_retries_duration -from v2_ci_runs -where v2_ci_runs.platform = 'github-actions' +from ci_runs +where ci_runs.platform = 'github-actions' -- After two weeks, let metrics drop off to null. -- This strikes a balance between tracking seldom-run actions and cleaning up after a workflow file is renamed. - and v2_ci_runs.terminal_at >= current_timestamp - '14 days'::interval - and v2_ci_runs.started_at is not null -group by v2_ci_runs.github_actions_owner, - v2_ci_runs.github_actions_repo, - v2_ci_runs.github_actions_workflow_path, - v2_ci_runs.status + and ci_runs.terminal_at >= current_timestamp - '14 days'::interval + and ci_runs.started_at is not null +group by ci_runs.github_actions_owner, + ci_runs.github_actions_repo, + ci_runs.github_actions_workflow_path, + ci_runs.status `).Scan(&results).Error; err != nil { return err } diff --git a/sherlock/internal/deprecated_models/v2models/pagerduty_integration.go b/sherlock/internal/deprecated_models/v2models/pagerduty_integration.go index 6693c9a19..01a5cae13 100644 --- a/sherlock/internal/deprecated_models/v2models/pagerduty_integration.go +++ b/sherlock/internal/deprecated_models/v2models/pagerduty_integration.go @@ -20,7 +20,7 @@ type PagerdutyIntegration struct { } func (p PagerdutyIntegration) TableName() string { - return "v2_pagerduty_integrations" + return "pagerduty_integrations" } func (p PagerdutyIntegration) getID() uint { diff --git a/sherlock/internal/deprecated_models/v2models/user.go b/sherlock/internal/deprecated_models/v2models/user.go index a63e23900..d86fd89c3 100644 --- a/sherlock/internal/deprecated_models/v2models/user.go +++ b/sherlock/internal/deprecated_models/v2models/user.go @@ -21,7 +21,7 @@ type User struct { } func (u User) TableName() string { - return "v2_users" + return "users" } func (u User) getID() uint { diff --git a/sherlock/internal/metrics/metrics.go b/sherlock/internal/metrics/metrics.go index 5dbd7c20f..0ac5fa31f 100644 --- a/sherlock/internal/metrics/metrics.go +++ b/sherlock/internal/metrics/metrics.go @@ -9,52 +9,52 @@ import ( // Synced across replicas var ( ChangesetCountMeasure = stats.Int64( - "sherlock/v2_changeset_count", + "sherlock/changeset_count", "count of changesets (version changes)", "changes", ) AppVersionCountMeasure = stats.Int64( - "sherlock/v2_app_version_count", + "sherlock/app_version_count", "count of recorded app versions", "versions") ChartVersionCountMeasure = stats.Int64( - "sherlock/v2_chart_version_count", + "sherlock/chart_version_count", "count of recorded chart versions", "versions") AppVersionLeadTimeMeasure = stats.Int64( - "sherlock/v2_app_version_30_day_lead_time", + "sherlock/app_version_30_day_lead_time", "average seconds between app version reporting and deployment for versions reported in last 30 days", "seconds") ChartVersionLeadTimeMeasure = stats.Int64( - "sherlock/v2_chart_version_30_day_lead_time", + "sherlock/chart_version_30_day_lead_time", "average seconds between chart version reporting and deployment for versions reported in last 30 days", "seconds") ChartFirecloudDevelopUsageMeasure = stats.Int64( - "sherlock/v2_chart_firecloud_develop_usage", + "sherlock/chart_firecloud_develop_usage", "boolean value of if a given chart currently is flagged as using firecloud-develop for config", "true") DataTypeCountMeasure = stats.Int64( - "sherlock/v2_data_type_count", + "sherlock/data_type_count", "count of records per data type", "records") EnvironmentStateCountMeasure = stats.Int64( - "sherlock/v2_environment_state_count", + "sherlock/environment_state_count", "count of environments", "environments") GithubActions1HourCompletionCountMeasure = stats.Int64( - "sherlock/v2_github_actions_1_hour_completion_count", + "sherlock/github_actions_1_hour_completion_count", "count of GitHub Actions completions in the past hour", "completions") GithubActions7DayCompletionCountMeasure = stats.Int64( - "sherlock/v2_github_actions_7_day_completion_count", + "sherlock/github_actions_7_day_completion_count", "count of GitHub Actions completions in the past seven days", "completions") GithubActions1HourTotalDurationMeasure = stats.Int64( - "sherlock/v2_github_actions_1_hour_total_duration", + "sherlock/github_actions_1_hour_total_duration", "total duration of GitHub Actions completed in the past hour", "seconds") GithubActions7DayTotalDurationMeasure = stats.Int64( - "sherlock/v2_github_actions_7_day_total_duration", + "sherlock/github_actions_7_day_total_duration", "total duration of GitHub Actions completed in the past seven days", "seconds") ) @@ -62,7 +62,7 @@ var ( // Unique per replica var ( PagerdutyRequestCount = stats.Int64( - "sherlock/v2_pagerduty_request_count", + "sherlock/pagerduty_request_count", "count of outgoing requests to pagerduty", "requests") ) @@ -85,91 +85,91 @@ var ( GithubActionsRetryKey = tag.MustNewKey("gha_retry") ChangesetCountView = &view.View{ - Name: "v2_changeset_count", + Name: "changeset_count", Measure: ChangesetCountMeasure, TagKeys: []tag.Key{ChartKey, EnvironmentKey, EnvironmentLifecycleKey, ChartReleaseKey, ChangesetStateKey}, Description: ChangesetCountMeasure.Description(), Aggregation: view.LastValue(), } AppVersionCountView = &view.View{ - Name: "v2_app_version_count", + Name: "app_version_count", Measure: AppVersionCountMeasure, TagKeys: []tag.Key{ChartKey, AppVersionTypeKey}, Description: AppVersionCountMeasure.Description(), Aggregation: view.LastValue(), } ChartVersionCountView = &view.View{ - Name: "v2_chart_version_count", + Name: "chart_version_count", Measure: ChartVersionCountMeasure, TagKeys: []tag.Key{ChartKey}, Description: ChangesetCountMeasure.Description(), Aggregation: view.LastValue(), } AppVersionLeadTimeView = &view.View{ - Name: "v2_app_version_30_day_lead_time", + Name: "app_version_30_day_lead_time", Measure: AppVersionLeadTimeMeasure, TagKeys: []tag.Key{ChartKey, EnvironmentKey, EnvironmentLifecycleKey, ChartReleaseKey}, Description: AppVersionLeadTimeMeasure.Description(), Aggregation: view.LastValue(), } ChartVersionLeadTimeView = &view.View{ - Name: "v2_chart_version_30_day_lead_time", + Name: "chart_version_30_day_lead_time", Measure: ChartVersionLeadTimeMeasure, TagKeys: []tag.Key{ChartKey, EnvironmentKey, EnvironmentLifecycleKey, ChartReleaseKey}, Description: ChartVersionLeadTimeMeasure.Description(), Aggregation: view.LastValue(), } ChartFirecloudDevelopUsageView = &view.View{ - Name: "v2_chart_firecloud_develop_usage", + Name: "chart_firecloud_develop_usage", Measure: ChartFirecloudDevelopUsageMeasure, TagKeys: []tag.Key{ChartKey}, Description: ChartFirecloudDevelopUsageMeasure.Description(), Aggregation: view.LastValue(), } DataTypeCountView = &view.View{ - Name: "v2_data_type_count", + Name: "data_type_count", Measure: DataTypeCountMeasure, TagKeys: []tag.Key{DataTypeKey}, Description: DataTypeCountMeasure.Description(), Aggregation: view.LastValue(), } PagerdutyRequestCountView = &view.View{ - Name: "v2_pagerduty_request_count", + Name: "pagerduty_request_count", Measure: PagerdutyRequestCount, TagKeys: []tag.Key{PagerdutyRequestTypeKey, PagerdutyResponseCodeKey}, Description: PagerdutyRequestCount.Description(), Aggregation: view.Count(), } EnvironmentStateCountView = &view.View{ - Name: "v2_environment_state_count", + Name: "environment_state_count", Measure: EnvironmentStateCountMeasure, TagKeys: []tag.Key{EnvironmentLifecycleKey, EnvironmentOfflineKey, EnvironmentPreventDeletionKey}, Description: EnvironmentStateCountMeasure.Description(), Aggregation: view.LastValue(), } GithubActions1HourCompletionCountView = &view.View{ - Name: "v2_github_actions_1_hour_completion_count", + Name: "github_actions_1_hour_completion_count", Measure: GithubActions1HourCompletionCountMeasure, TagKeys: []tag.Key{GithubActionsRepoKey, GithubActionsWorkflowFileKey, GithubActionsOutcomeKey, GithubActionsRetryKey}, Description: GithubActions1HourCompletionCountMeasure.Description(), Aggregation: view.LastValue(), } GithubActions7DayCompletionCountView = &view.View{ - Name: "v2_github_actions_7_day_completion_count", + Name: "github_actions_7_day_completion_count", Measure: GithubActions7DayCompletionCountMeasure, TagKeys: []tag.Key{GithubActionsRepoKey, GithubActionsWorkflowFileKey, GithubActionsOutcomeKey, GithubActionsRetryKey}, Description: GithubActions7DayCompletionCountMeasure.Description(), Aggregation: view.LastValue(), } GithubActions1HourTotalDurationView = &view.View{ - Name: "v2_github_actions_1_hour_total_duration", + Name: "github_actions_1_hour_total_duration", Measure: GithubActions1HourTotalDurationMeasure, TagKeys: []tag.Key{GithubActionsRepoKey, GithubActionsWorkflowFileKey, GithubActionsOutcomeKey, GithubActionsRetryKey}, Description: GithubActions1HourTotalDurationMeasure.Description(), Aggregation: view.LastValue(), } GithubActions7DayTotalDurationView = &view.View{ - Name: "v2_github_actions_7_day_total_duration", + Name: "github_actions_7_day_total_duration", Measure: GithubActions7DayTotalDurationMeasure, TagKeys: []tag.Key{GithubActionsRepoKey, GithubActionsWorkflowFileKey, GithubActionsOutcomeKey, GithubActionsRetryKey}, Description: GithubActions7DayTotalDurationMeasure.Description(), diff --git a/sherlock/internal/models/deploy_hook_trigger_config.go b/sherlock/internal/models/deploy_hook_trigger_config.go index bf5f5617e..aad9c2641 100644 --- a/sherlock/internal/models/deploy_hook_trigger_config.go +++ b/sherlock/internal/models/deploy_hook_trigger_config.go @@ -19,7 +19,7 @@ type DeployHookTriggerConfig struct { } func (d *DeployHookTriggerConfig) TableName() string { - return "v2_deploy_hook_trigger_configs" + return "deploy_hook_trigger_configs" } // ErrorIfForbidden looks at the environment or chart release's environment/cluster to determine diff --git a/sherlock/internal/models/github_actions_deploy_hook.go b/sherlock/internal/models/github_actions_deploy_hook.go index 8d055ecce..fbfa8fa4b 100644 --- a/sherlock/internal/models/github_actions_deploy_hook.go +++ b/sherlock/internal/models/github_actions_deploy_hook.go @@ -17,7 +17,7 @@ type GithubActionsDeployHook struct { } func (g *GithubActionsDeployHook) TableName() string { - return "v2_github_actions_deploy_hooks" + return "github_actions_deploy_hooks" } func (g *GithubActionsDeployHook) AfterSave(tx *gorm.DB) error { diff --git a/sherlock/internal/models/slack_deploy_hook.go b/sherlock/internal/models/slack_deploy_hook.go index 8e03f8ea3..c5c986beb 100644 --- a/sherlock/internal/models/slack_deploy_hook.go +++ b/sherlock/internal/models/slack_deploy_hook.go @@ -11,7 +11,7 @@ type SlackDeployHook struct { } func (s *SlackDeployHook) TableName() string { - return "v2_slack_deploy_hooks" + return "slack_deploy_hooks" } func (s *SlackDeployHook) AfterSave(tx *gorm.DB) error { From 3fb926af36b1deb911afd35d3b69ec810a2bef06 Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Tue, 24 Oct 2023 16:54:22 -0400 Subject: [PATCH 09/21] deploy hooks --- .../migrations/000060_rename_tables.down.sql | 46 +++++++++++++++++++ .../db/migrations/000060_rename_tables.up.sql | 46 +++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/sherlock/db/migrations/000060_rename_tables.down.sql b/sherlock/db/migrations/000060_rename_tables.down.sql index 8b7dcb73d..790e8e7e5 100644 --- a/sherlock/db/migrations/000060_rename_tables.down.sql +++ b/sherlock/db/migrations/000060_rename_tables.down.sql @@ -245,6 +245,26 @@ alter table v2_database_instances -- indexes alter index idx_database_instances_deleted_at rename to idx_v2_database_instances_deleted_at; +/* + * deploy_hook_trigger_configs + */ +alter table deploy_hook_trigger_configs + rename to v2_deploy_hook_trigger_configs; + +-- keys +alter table v2_deploy_hook_trigger_configs + rename constraint deploy_hook_trigger_configs_pkey to v2_deploy_hook_trigger_configs_pkey; + +-- foreign keys +alter table v2_deploy_hook_trigger_configs + rename constraint fk_deploy_hook_trigger_configs_on_chart_release to fk_v2_deploy_hook_trigger_configs_on_chart_release; + +alter table v2_deploy_hook_trigger_configs + rename constraint fk_deploy_hook_trigger_configs_on_environment to fk_v2_deploy_hook_trigger_configs_on_environment; + +--indexes +alter index idx_deploy_hook_trigger_configs_deleted_at rename to idx_v2_deploy_hook_trigger_configs_deleted_at; + /* * environments */ @@ -271,6 +291,19 @@ alter table v2_environments -- indexes alter index idx_environments_deleted_at rename to idx_v2_environments_deleted_at; +/* + * github_actions_deploy_hooks + */ +alter table github_actions_deploy_hooks + rename to v2_github_actions_deploy_hooks; + +-- keys +alter table v2_github_actions_deploy_hooks + rename constraint github_actions_deploy_hooks_pkey to v2_github_actions_deploy_hooks_pkey; + +-- indexes +alter index idx_github_actions_deploy_hooks_deleted_at rename to idx_v2_github_actions_deploy_hooks_deleted_at; + /* * pagerduty_integration */ @@ -284,6 +317,19 @@ alter table v2_pagerduty_integrations -- indexes alter index idx_pagerduty_integrations_deleted_at rename to idx_v2_pagerduty_integrations_deleted_at; +/* + * slack_deploy_hooks + */ +alter table slack_deploy_hooks + rename to v2_slack_deploy_hooks; + +-- keys +alter table v2_slack_deploy_hooks + rename constraint slack_deploy_hooks_pkey to v2_slack_deploy_hooks_pkey; + +-- indexes +alter index idx_slack_deploy_hooks_deleted_at rename to idx_v2_slack_deploy_hooks_deleted_at; + /* * users */ diff --git a/sherlock/db/migrations/000060_rename_tables.up.sql b/sherlock/db/migrations/000060_rename_tables.up.sql index b3278838a..8de239ec9 100644 --- a/sherlock/db/migrations/000060_rename_tables.up.sql +++ b/sherlock/db/migrations/000060_rename_tables.up.sql @@ -245,6 +245,26 @@ alter table database_instances -- indexes alter index idx_v2_database_instances_deleted_at rename to idx_database_instances_deleted_at; +/* + * deploy_hook_trigger_configs + */ +alter table v2_deploy_hook_trigger_configs + rename to deploy_hook_trigger_configs; + +-- keys +alter table deploy_hook_trigger_configs + rename constraint v2_deploy_hook_trigger_configs_pkey to deploy_hook_trigger_configs_pkey; + +-- foreign keys +alter table deploy_hook_trigger_configs + rename constraint fk_v2_deploy_hook_trigger_configs_on_chart_release to fk_deploy_hook_trigger_configs_on_chart_release; + +alter table deploy_hook_trigger_configs + rename constraint fk_v2_deploy_hook_trigger_configs_on_environment to fk_deploy_hook_trigger_configs_on_environment; + +--indexes +alter index idx_v2_deploy_hook_trigger_configs_deleted_at rename to idx_deploy_hook_trigger_configs_deleted_at; + /* * environments */ @@ -271,6 +291,19 @@ alter table environments -- indexes alter index idx_v2_environments_deleted_at rename to idx_environments_deleted_at; +/* + * github_actions_deploy_hooks + */ +alter table v2_github_actions_deploy_hooks + rename to github_actions_deploy_hooks; + +-- keys +alter table github_actions_deploy_hooks + rename constraint v2_github_actions_deploy_hooks_pkey to github_actions_deploy_hooks_pkey; + +-- indexes +alter index idx_v2_github_actions_deploy_hooks_deleted_at rename to idx_github_actions_deploy_hooks_deleted_at; + /* * pagerduty_integration */ @@ -284,6 +317,19 @@ alter table pagerduty_integrations -- indexes alter index idx_v2_pagerduty_integrations_deleted_at rename to idx_pagerduty_integrations_deleted_at; +/* + * slack_deploy_hooks + */ +alter table v2_slack_deploy_hooks + rename to slack_deploy_hooks; + +-- keys +alter table slack_deploy_hooks + rename constraint v2_slack_deploy_hooks_pkey to slack_deploy_hooks_pkey; + +-- indexes +alter index idx_v2_slack_deploy_hooks_deleted_at rename to idx_slack_deploy_hooks_deleted_at; + /* * users */ From f2cc24cf783bf236b0a732e5a664d742cc2814f3 Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Tue, 24 Oct 2023 17:09:35 -0400 Subject: [PATCH 10/21] new lines :/ --- sherlock/db/migrations/000060_rename_tables.down.sql | 2 +- sherlock/db/migrations/000060_rename_tables.up.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sherlock/db/migrations/000060_rename_tables.down.sql b/sherlock/db/migrations/000060_rename_tables.down.sql index 790e8e7e5..815c54ffd 100644 --- a/sherlock/db/migrations/000060_rename_tables.down.sql +++ b/sherlock/db/migrations/000060_rename_tables.down.sql @@ -374,4 +374,4 @@ alter sequence environments_id_seq rename to v2_environments_id_seq; alter sequence pagerduty_integrations_id_seq rename to v2_pagerduty_integrations_id_seq; -alter sequence users_id_seq rename to v2_users_id_seq; \ No newline at end of file +alter sequence users_id_seq rename to v2_users_id_seq; diff --git a/sherlock/db/migrations/000060_rename_tables.up.sql b/sherlock/db/migrations/000060_rename_tables.up.sql index 8de239ec9..4b84d4e0f 100644 --- a/sherlock/db/migrations/000060_rename_tables.up.sql +++ b/sherlock/db/migrations/000060_rename_tables.up.sql @@ -374,4 +374,4 @@ alter sequence v2_environments_id_seq rename to environments_id_seq; alter sequence v2_pagerduty_integrations_id_seq rename to pagerduty_integrations_id_seq; -alter sequence v2_users_id_seq rename to users_id_seq; \ No newline at end of file +alter sequence v2_users_id_seq rename to users_id_seq; From 0bae89ba32e0355ff206ed9a1442c459f734fe2c Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Wed, 25 Oct 2023 11:18:25 -0400 Subject: [PATCH 11/21] revert metrics.go --- sherlock/internal/metrics/metrics.go | 52 ++++++++++++++-------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/sherlock/internal/metrics/metrics.go b/sherlock/internal/metrics/metrics.go index 0ac5fa31f..5dbd7c20f 100644 --- a/sherlock/internal/metrics/metrics.go +++ b/sherlock/internal/metrics/metrics.go @@ -9,52 +9,52 @@ import ( // Synced across replicas var ( ChangesetCountMeasure = stats.Int64( - "sherlock/changeset_count", + "sherlock/v2_changeset_count", "count of changesets (version changes)", "changes", ) AppVersionCountMeasure = stats.Int64( - "sherlock/app_version_count", + "sherlock/v2_app_version_count", "count of recorded app versions", "versions") ChartVersionCountMeasure = stats.Int64( - "sherlock/chart_version_count", + "sherlock/v2_chart_version_count", "count of recorded chart versions", "versions") AppVersionLeadTimeMeasure = stats.Int64( - "sherlock/app_version_30_day_lead_time", + "sherlock/v2_app_version_30_day_lead_time", "average seconds between app version reporting and deployment for versions reported in last 30 days", "seconds") ChartVersionLeadTimeMeasure = stats.Int64( - "sherlock/chart_version_30_day_lead_time", + "sherlock/v2_chart_version_30_day_lead_time", "average seconds between chart version reporting and deployment for versions reported in last 30 days", "seconds") ChartFirecloudDevelopUsageMeasure = stats.Int64( - "sherlock/chart_firecloud_develop_usage", + "sherlock/v2_chart_firecloud_develop_usage", "boolean value of if a given chart currently is flagged as using firecloud-develop for config", "true") DataTypeCountMeasure = stats.Int64( - "sherlock/data_type_count", + "sherlock/v2_data_type_count", "count of records per data type", "records") EnvironmentStateCountMeasure = stats.Int64( - "sherlock/environment_state_count", + "sherlock/v2_environment_state_count", "count of environments", "environments") GithubActions1HourCompletionCountMeasure = stats.Int64( - "sherlock/github_actions_1_hour_completion_count", + "sherlock/v2_github_actions_1_hour_completion_count", "count of GitHub Actions completions in the past hour", "completions") GithubActions7DayCompletionCountMeasure = stats.Int64( - "sherlock/github_actions_7_day_completion_count", + "sherlock/v2_github_actions_7_day_completion_count", "count of GitHub Actions completions in the past seven days", "completions") GithubActions1HourTotalDurationMeasure = stats.Int64( - "sherlock/github_actions_1_hour_total_duration", + "sherlock/v2_github_actions_1_hour_total_duration", "total duration of GitHub Actions completed in the past hour", "seconds") GithubActions7DayTotalDurationMeasure = stats.Int64( - "sherlock/github_actions_7_day_total_duration", + "sherlock/v2_github_actions_7_day_total_duration", "total duration of GitHub Actions completed in the past seven days", "seconds") ) @@ -62,7 +62,7 @@ var ( // Unique per replica var ( PagerdutyRequestCount = stats.Int64( - "sherlock/pagerduty_request_count", + "sherlock/v2_pagerduty_request_count", "count of outgoing requests to pagerduty", "requests") ) @@ -85,91 +85,91 @@ var ( GithubActionsRetryKey = tag.MustNewKey("gha_retry") ChangesetCountView = &view.View{ - Name: "changeset_count", + Name: "v2_changeset_count", Measure: ChangesetCountMeasure, TagKeys: []tag.Key{ChartKey, EnvironmentKey, EnvironmentLifecycleKey, ChartReleaseKey, ChangesetStateKey}, Description: ChangesetCountMeasure.Description(), Aggregation: view.LastValue(), } AppVersionCountView = &view.View{ - Name: "app_version_count", + Name: "v2_app_version_count", Measure: AppVersionCountMeasure, TagKeys: []tag.Key{ChartKey, AppVersionTypeKey}, Description: AppVersionCountMeasure.Description(), Aggregation: view.LastValue(), } ChartVersionCountView = &view.View{ - Name: "chart_version_count", + Name: "v2_chart_version_count", Measure: ChartVersionCountMeasure, TagKeys: []tag.Key{ChartKey}, Description: ChangesetCountMeasure.Description(), Aggregation: view.LastValue(), } AppVersionLeadTimeView = &view.View{ - Name: "app_version_30_day_lead_time", + Name: "v2_app_version_30_day_lead_time", Measure: AppVersionLeadTimeMeasure, TagKeys: []tag.Key{ChartKey, EnvironmentKey, EnvironmentLifecycleKey, ChartReleaseKey}, Description: AppVersionLeadTimeMeasure.Description(), Aggregation: view.LastValue(), } ChartVersionLeadTimeView = &view.View{ - Name: "chart_version_30_day_lead_time", + Name: "v2_chart_version_30_day_lead_time", Measure: ChartVersionLeadTimeMeasure, TagKeys: []tag.Key{ChartKey, EnvironmentKey, EnvironmentLifecycleKey, ChartReleaseKey}, Description: ChartVersionLeadTimeMeasure.Description(), Aggregation: view.LastValue(), } ChartFirecloudDevelopUsageView = &view.View{ - Name: "chart_firecloud_develop_usage", + Name: "v2_chart_firecloud_develop_usage", Measure: ChartFirecloudDevelopUsageMeasure, TagKeys: []tag.Key{ChartKey}, Description: ChartFirecloudDevelopUsageMeasure.Description(), Aggregation: view.LastValue(), } DataTypeCountView = &view.View{ - Name: "data_type_count", + Name: "v2_data_type_count", Measure: DataTypeCountMeasure, TagKeys: []tag.Key{DataTypeKey}, Description: DataTypeCountMeasure.Description(), Aggregation: view.LastValue(), } PagerdutyRequestCountView = &view.View{ - Name: "pagerduty_request_count", + Name: "v2_pagerduty_request_count", Measure: PagerdutyRequestCount, TagKeys: []tag.Key{PagerdutyRequestTypeKey, PagerdutyResponseCodeKey}, Description: PagerdutyRequestCount.Description(), Aggregation: view.Count(), } EnvironmentStateCountView = &view.View{ - Name: "environment_state_count", + Name: "v2_environment_state_count", Measure: EnvironmentStateCountMeasure, TagKeys: []tag.Key{EnvironmentLifecycleKey, EnvironmentOfflineKey, EnvironmentPreventDeletionKey}, Description: EnvironmentStateCountMeasure.Description(), Aggregation: view.LastValue(), } GithubActions1HourCompletionCountView = &view.View{ - Name: "github_actions_1_hour_completion_count", + Name: "v2_github_actions_1_hour_completion_count", Measure: GithubActions1HourCompletionCountMeasure, TagKeys: []tag.Key{GithubActionsRepoKey, GithubActionsWorkflowFileKey, GithubActionsOutcomeKey, GithubActionsRetryKey}, Description: GithubActions1HourCompletionCountMeasure.Description(), Aggregation: view.LastValue(), } GithubActions7DayCompletionCountView = &view.View{ - Name: "github_actions_7_day_completion_count", + Name: "v2_github_actions_7_day_completion_count", Measure: GithubActions7DayCompletionCountMeasure, TagKeys: []tag.Key{GithubActionsRepoKey, GithubActionsWorkflowFileKey, GithubActionsOutcomeKey, GithubActionsRetryKey}, Description: GithubActions7DayCompletionCountMeasure.Description(), Aggregation: view.LastValue(), } GithubActions1HourTotalDurationView = &view.View{ - Name: "github_actions_1_hour_total_duration", + Name: "v2_github_actions_1_hour_total_duration", Measure: GithubActions1HourTotalDurationMeasure, TagKeys: []tag.Key{GithubActionsRepoKey, GithubActionsWorkflowFileKey, GithubActionsOutcomeKey, GithubActionsRetryKey}, Description: GithubActions1HourTotalDurationMeasure.Description(), Aggregation: view.LastValue(), } GithubActions7DayTotalDurationView = &view.View{ - Name: "github_actions_7_day_total_duration", + Name: "v2_github_actions_7_day_total_duration", Measure: GithubActions7DayTotalDurationMeasure, TagKeys: []tag.Key{GithubActionsRepoKey, GithubActionsWorkflowFileKey, GithubActionsOutcomeKey, GithubActionsRetryKey}, Description: GithubActions7DayTotalDurationMeasure.Description(), From e27d297cacf95c2fff9fdfc95ee7cd3fa4cf8b65 Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Thu, 26 Oct 2023 13:04:41 -0400 Subject: [PATCH 12/21] sequences --- sherlock/db/migrations/000060_rename_tables.down.sql | 6 ++++++ sherlock/db/migrations/000060_rename_tables.up.sql | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/sherlock/db/migrations/000060_rename_tables.down.sql b/sherlock/db/migrations/000060_rename_tables.down.sql index 815c54ffd..6ac3187f5 100644 --- a/sherlock/db/migrations/000060_rename_tables.down.sql +++ b/sherlock/db/migrations/000060_rename_tables.down.sql @@ -375,3 +375,9 @@ alter sequence environments_id_seq rename to v2_environments_id_seq; alter sequence pagerduty_integrations_id_seq rename to v2_pagerduty_integrations_id_seq; alter sequence users_id_seq rename to v2_users_id_seq; + +alter sequence deploy_hook_trigger_configs_id_seq rename to v2_deploy_hook_trigger_configs_id_seq; + +alter sequence github_actions_deploy_hooks_id_seq rename to v2_github_actions_deploy_hooks_id_seq; + +alter sequence slack_deploy_hooks_id_seq rename to v2_slack_deploy_hooks_id_seq; diff --git a/sherlock/db/migrations/000060_rename_tables.up.sql b/sherlock/db/migrations/000060_rename_tables.up.sql index 4b84d4e0f..0cdf79194 100644 --- a/sherlock/db/migrations/000060_rename_tables.up.sql +++ b/sherlock/db/migrations/000060_rename_tables.up.sql @@ -375,3 +375,9 @@ alter sequence v2_environments_id_seq rename to environments_id_seq; alter sequence v2_pagerduty_integrations_id_seq rename to pagerduty_integrations_id_seq; alter sequence v2_users_id_seq rename to users_id_seq; + +alter sequence v2_deploy_hook_trigger_configs_id_seq rename to deploy_hook_trigger_configs_id_seq; + +alter sequence v2_github_actions_deploy_hooks_id_seq rename to github_actions_deploy_hooks_id_seq; + +alter sequence v2_slack_deploy_hooks_id_seq rename to slack_deploy_hooks_id_seq; From a8b9935a0a787100386bfb9cbc60ca21279e411f Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Thu, 26 Oct 2023 15:04:37 -0400 Subject: [PATCH 13/21] database instance database migrations --- .../000061_database_instance_v3.down.sql | 8 ++++++++ .../migrations/000061_database_instance_v3.up.sql | 14 ++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 sherlock/db/migrations/000061_database_instance_v3.down.sql create mode 100644 sherlock/db/migrations/000061_database_instance_v3.up.sql diff --git a/sherlock/db/migrations/000061_database_instance_v3.down.sql b/sherlock/db/migrations/000061_database_instance_v3.down.sql new file mode 100644 index 000000000..6279da2ad --- /dev/null +++ b/sherlock/db/migrations/000061_database_instance_v3.down.sql @@ -0,0 +1,8 @@ +alter table database_instances + drop constraint if exists chart_release_id_present; + +alter table database_instances + drop constraint if exists platform_present; + +alter table database_instances + drop constraint if exists default_database_present; diff --git a/sherlock/db/migrations/000061_database_instance_v3.up.sql b/sherlock/db/migrations/000061_database_instance_v3.up.sql new file mode 100644 index 000000000..b96472b6b --- /dev/null +++ b/sherlock/db/migrations/000061_database_instance_v3.up.sql @@ -0,0 +1,14 @@ +alter table database_instances + add constraint chart_release_id_present + check (chart_release_id != 0); + +alter table database_instances + add constraint platform_present + check (platform is not null and + ((platform = 'google' and google_project is not null and google_project != '') or + (platform = 'azure' and instance_name is not null and instance_name != '') or + (platform = 'kubernetes'))); + +alter table database_instances + add constraint default_database_present + check (default_database is not null and default_database != ''); From 55fa2e7d680ad2d6c8f6325a93ecd9c183d46949 Mon Sep 17 00:00:00 2001 From: Katie Welch Date: Thu, 26 Oct 2023 15:27:31 -0400 Subject: [PATCH 14/21] null check --- sherlock/db/migrations/000061_database_instance_v3.up.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sherlock/db/migrations/000061_database_instance_v3.up.sql b/sherlock/db/migrations/000061_database_instance_v3.up.sql index b96472b6b..95207a5ef 100644 --- a/sherlock/db/migrations/000061_database_instance_v3.up.sql +++ b/sherlock/db/migrations/000061_database_instance_v3.up.sql @@ -1,6 +1,6 @@ alter table database_instances add constraint chart_release_id_present - check (chart_release_id != 0); + check (chart_release_id is not null and chart_release_id != 0); alter table database_instances add constraint platform_present From d91a7c33d90cda35e8157615b69d0c3ba1e14237 Mon Sep 17 00:00:00 2001 From: Jack Warren Date: Mon, 30 Oct 2023 12:20:36 -0400 Subject: [PATCH 15/21] move metrics code to models package --- sherlock/internal/boot/application.go | 12 ++--- .../v2controllers/metrics_updater_test.go | 52 ------------------- .../metrics_cron.go} | 18 ++++--- sherlock/internal/models/metrics_cron_test.go | 11 ++++ 4 files changed, 29 insertions(+), 64 deletions(-) delete mode 100644 sherlock/internal/deprecated_controllers/v2controllers/metrics_updater_test.go rename sherlock/internal/{deprecated_models/v2models/metrics_updater.go => models/metrics_cron.go} (97%) create mode 100644 sherlock/internal/models/metrics_cron_test.go diff --git a/sherlock/internal/boot/application.go b/sherlock/internal/boot/application.go index 02eaa1edf..949f354ab 100644 --- a/sherlock/internal/boot/application.go +++ b/sherlock/internal/boot/application.go @@ -10,9 +10,9 @@ import ( "github.com/broadinstitute/sherlock/sherlock/internal/config" "github.com/broadinstitute/sherlock/sherlock/internal/db" "github.com/broadinstitute/sherlock/sherlock/internal/deployhooks" - "github.com/broadinstitute/sherlock/sherlock/internal/deprecated_models/v2models" "github.com/broadinstitute/sherlock/sherlock/internal/github" "github.com/broadinstitute/sherlock/sherlock/internal/metrics" + "github.com/broadinstitute/sherlock/sherlock/internal/models" "github.com/broadinstitute/sherlock/sherlock/internal/slack" "github.com/rs/zerolog/log" "gorm.io/gorm" @@ -94,15 +94,15 @@ func (a *Application) Start() { if config.Config.Bool("metrics.v2.enable") { log.Info().Msgf("BOOT | registering metric views...") if err = metrics.RegisterViews(); err != nil { - log.Fatal().Msgf("v2metrics.RegisterViews() err: %v", err) + log.Fatal().Msgf("metrics.RegisterViews() err: %v", err) } log.Info().Msgf("BOOT | calculating metric values...") - if err = v2models.UpdateMetrics(ctx, a.gormDB); err != nil { - log.Fatal().Msgf("v2models.UpdateMetrics() err: %v", err) + if err = models.UpdateMetrics(ctx, a.gormDB); err != nil { + log.Fatal().Msgf("models.UpdateMetrics() err: %v", err) } - go v2models.KeepMetricsUpdated(ctx, a.gormDB) + go models.KeepMetricsUpdated(ctx, a.gormDB) } if config.Config.Bool("slack.enable") { @@ -116,7 +116,7 @@ func (a *Application) Start() { if config.Config.Bool("github.enable") { log.Info().Msgf("BOOT | initializing GitHub client...") if err = github.Init(ctx); err != nil { - log.Fatal().Msgf("githubz.Init() err: %v", err) + log.Fatal().Msgf("github.Init() err: %v", err) } } diff --git a/sherlock/internal/deprecated_controllers/v2controllers/metrics_updater_test.go b/sherlock/internal/deprecated_controllers/v2controllers/metrics_updater_test.go deleted file mode 100644 index 634936828..000000000 --- a/sherlock/internal/deprecated_controllers/v2controllers/metrics_updater_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package v2controllers - -import ( - "context" - "github.com/broadinstitute/sherlock/sherlock/internal/config" - "github.com/broadinstitute/sherlock/sherlock/internal/deprecated_db" - "github.com/broadinstitute/sherlock/sherlock/internal/deprecated_models/v2models" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" - "gorm.io/gorm" - "testing" -) - -// v2's metrics code is entirely in the model, but we aren't able to run functional tests from the model because -// we get circular imports trying to connect to the database from there (database connection code is in another package -// that itself requires the model for migrations). That's not a problem anywhere else because we run functional tests -// via controller methods anyway. There isn't a controller component to v2's metrics, but our best option to run -// a basic "does the SQL validate" test is to run it from here similar to other functional tests. -// -// In other words, I (Jack) thinks having this rudimentary test not co-located is okay for now because literally all the -// other testing is on the happy path and I don't feel like fighting Go imports here. Maybe we'll add higher-level -// metrics code down the line and this will make sense as is, or once v1 is gone maybe the database connection code -// can live in the model itself and then we can move this there. - -func TestMetricsUpdaterSuite(t *testing.T) { - if testing.Short() { - t.Skip("skipping functional test") - } - suite.Run(t, new(metricsUpdaterSuite)) -} - -type metricsUpdaterSuite struct { - suite.Suite - db *gorm.DB -} - -func (suite *metricsUpdaterSuite) SetupSuite() { - config.LoadTestConfig() - suite.db = deprecated_db.ConnectAndConfigureFromTest(suite.T()) -} - -func (suite *metricsUpdaterSuite) TearDownSuite() { - deprecated_db.Truncate(suite.T(), suite.db) -} - -func (suite *metricsUpdaterSuite) TestUpdateMetrics() { - suite.Run("doesn't error when running custom SQL", func() { - deprecated_db.Truncate(suite.T(), suite.db) - err := v2models.UpdateMetrics(context.Background(), suite.db) - assert.NoError(suite.T(), err) - }) -} diff --git a/sherlock/internal/deprecated_models/v2models/metrics_updater.go b/sherlock/internal/models/metrics_cron.go similarity index 97% rename from sherlock/internal/deprecated_models/v2models/metrics_updater.go rename to sherlock/internal/models/metrics_cron.go index 2aec47c79..715c77677 100644 --- a/sherlock/internal/deprecated_models/v2models/metrics_updater.go +++ b/sherlock/internal/models/metrics_cron.go @@ -1,4 +1,4 @@ -package v2models +package models import ( "context" @@ -189,7 +189,7 @@ group by result_per_version.chart_release_id } func reportDataTypeCounts(ctx context.Context, db *gorm.DB) error { - for dataType, model := range map[string]Model{ + for dataType, model := range map[string]any{ "chart": Chart{}, "environment": Environment{}, "cluster": Cluster{}, @@ -359,7 +359,10 @@ func UpdateMetrics(ctx context.Context, db *gorm.DB) error { return err } - charts, err := InternalChartStore.ListAllMatchingByUpdated(db, 0, &Chart{}) + var charts []Chart + if err = db.Model(&Chart{}).Order("updated_at desc").Find(&charts).Error; err != nil { + return err + } if err != nil { return err } @@ -392,7 +395,10 @@ func UpdateMetrics(ctx context.Context, db *gorm.DB) error { stats.Record(ctx, metrics.AppVersionCountMeasure.M(count)) } - chartReleases, err := InternalChartReleaseStore.ListAllMatchingByUpdated(db, 0, &ChartRelease{ChartID: chart.ID}) + var chartReleases []ChartRelease + if err = db.Model(&ChartRelease{}).Where(&ChartRelease{ChartID: chart.ID}).Order("updated_at desc").Find(&chartReleases).Error; err != nil { + return err + } if err != nil { return err } @@ -462,7 +468,7 @@ func UpdateMetrics(ctx context.Context, db *gorm.DB) error { } lastUpdateTime = time.Now() - log.Info().Msgf("MTRC | v2 metrics updated, took %s", lastUpdateTime.Sub(updateStartTime).String()) + log.Info().Msgf("MTRC | metrics updated, took %s", lastUpdateTime.Sub(updateStartTime).String()) return nil } @@ -471,7 +477,7 @@ func KeepMetricsUpdated(ctx context.Context, db *gorm.DB) { for { time.Sleep(interval) if err := UpdateMetrics(ctx, db); err != nil { - log.Warn().Err(err).Msgf("failed to update v2 metrics, now %s stale", time.Since(lastUpdateTime).String()) + log.Warn().Err(err).Msgf("MTRC | failed to update metrics, now %s stale", time.Since(lastUpdateTime).String()) } } } diff --git a/sherlock/internal/models/metrics_cron_test.go b/sherlock/internal/models/metrics_cron_test.go new file mode 100644 index 000000000..42af2ad94 --- /dev/null +++ b/sherlock/internal/models/metrics_cron_test.go @@ -0,0 +1,11 @@ +package models + +import ( + "context" +) + +func (s *modelSuite) TestUpdateMetrics() { + s.Run("doesn't error when running custom SQL", func() { + s.NoError(UpdateMetrics(context.Background(), s.DB)) + }) +} From f95dc46cb128f40eefe5778104b797fbba95da16 Mon Sep 17 00:00:00 2001 From: Jack Warren Date: Mon, 30 Oct 2023 12:29:46 -0400 Subject: [PATCH 16/21] pagerduty --- .../internal/models/pagerduty_integration.go | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/sherlock/internal/models/pagerduty_integration.go b/sherlock/internal/models/pagerduty_integration.go index 8957875e6..bb2186a46 100644 --- a/sherlock/internal/models/pagerduty_integration.go +++ b/sherlock/internal/models/pagerduty_integration.go @@ -10,6 +10,31 @@ type PagerdutyIntegration struct { Type *string } -func (p PagerdutyIntegration) TableName() string { +func (p *PagerdutyIntegration) TableName() string { return "pagerduty_integrations" } + +func (p *PagerdutyIntegration) errorIfForbidden(tx *gorm.DB) error { + if user, err := GetCurrentUserForDB(tx); err != nil { + return err + } else if err = user.Suitability().SuitableOrError(); err != nil { + return err + } else { + return nil + } +} + +// BeforeCreate checks permissions +func (p *PagerdutyIntegration) BeforeCreate(tx *gorm.DB) error { + return p.errorIfForbidden(tx) +} + +// BeforeUpdate checks permissions +func (p *PagerdutyIntegration) BeforeUpdate(tx *gorm.DB) error { + return p.errorIfForbidden(tx) +} + +// BeforeDelete checks permissions +func (p *PagerdutyIntegration) BeforeDelete(tx *gorm.DB) error { + return p.errorIfForbidden(tx) +} From 85f2c6f564c0d3f2d19a2ab39cbdafc88d7bd69d Mon Sep 17 00:00:00 2001 From: Jack Warren Date: Mon, 30 Oct 2023 12:43:32 -0400 Subject: [PATCH 17/21] move resource prefix code --- .../generate_resource_prefix.go | 52 +++++++++++++++++++ .../generate_resource_prefix_test.go | 33 ++++++++++++ .../deprecated_models/v2models/environment.go | 34 +----------- .../v2models/environment_test.go | 27 ---------- 4 files changed, 87 insertions(+), 59 deletions(-) create mode 100644 go-shared/pkg/resource_prefix/generate_resource_prefix.go create mode 100644 go-shared/pkg/resource_prefix/generate_resource_prefix_test.go diff --git a/go-shared/pkg/resource_prefix/generate_resource_prefix.go b/go-shared/pkg/resource_prefix/generate_resource_prefix.go new file mode 100644 index 000000000..91825a385 --- /dev/null +++ b/go-shared/pkg/resource_prefix/generate_resource_prefix.go @@ -0,0 +1,52 @@ +package resource_prefix + +import ( + "math/bits" + "strings" +) + +// Go strings are UTF-8, and these characters all map to single bytes, so this is like a `const` +// slice of bytes for the possible characters (a normal slice can't be a constant). +// +//goland:noinspection SpellCheckingInspection +const characterBytes = "abcdefghijklmnopqrstuvwxyz0123456789" + +// The domain of what can be represented by GenerateResourcePrefix +const possibleCombinations = uint64(26 * 36 * 36 * 36) + +// GenerateResourcePrefix converts the input number to four characters, written into the string builder. +// +// The string is like `[r3][r2][r1][r0]`. +// r0 through r2 are in base 36, for 26 letters plus 10 digits. +// r3 is in base 26, for 26 letters, so the string always starts with a letter. +// r0 is the "lowest" digit, and the string is a bit similar to a hexadecimal number with letters taking +// on numeral values. The result is a string representation of the input number, modulo the domain of +// the resulting string, achieving full coverage of that domain to minimize conflicts. +// +// Why does this function exist? For ephemeral environments it is sometimes required to have a short +// differentiating string that can be a prefix onto cloud resource names to try to avoid conflicts. +// Four characters is the largest this string can be, so we just do our best. The idea is that +// Sherlock can generate a prefix from the count of all environments, check if it already exists, +// and keep incrementing the number until it finds an opening. Since ephemeral environments are +// deleted with some regularity, we can reasonably assume this won't grow out of control. +// +// For more historical context, environments imported to Sherlock actually included resource prefixes +// already, so the algorithm needs to be able to handle surprise conflicts (we can't just go based on +// environment ID or something). +// +// Examples (remember that input is always modulo possibleCombinations): +// possibleCombinations-2 => z998 +// possibleCombinations-1 => z999 +// possibleCombinations => aaaa +// possibleCombinations+1 => aaab +// possibleCombinations+2 => aaac +func GenerateResourcePrefix(sb *strings.Builder, number uint64) { + number, r0 := bits.Div64(0, number%possibleCombinations, 36) + number, r1 := bits.Div64(0, number, 36) + number, r2 := bits.Div64(0, number, 36) + _, r3 := bits.Div64(0, number, 26) + sb.WriteByte(characterBytes[r3]) + sb.WriteByte(characterBytes[r2]) + sb.WriteByte(characterBytes[r1]) + sb.WriteByte(characterBytes[r0]) +} diff --git a/go-shared/pkg/resource_prefix/generate_resource_prefix_test.go b/go-shared/pkg/resource_prefix/generate_resource_prefix_test.go new file mode 100644 index 000000000..40d4f6fed --- /dev/null +++ b/go-shared/pkg/resource_prefix/generate_resource_prefix_test.go @@ -0,0 +1,33 @@ +package resource_prefix + +import ( + "fmt" + "github.com/broadinstitute/sherlock/go-shared/pkg/testutils" + "strings" + "testing" +) + +func TestGenerateResourcePrefix(t *testing.T) { + sb := strings.Builder{} + sb.Grow(4) + tests := []struct { + input uint64 + output string + }{ + {0, "aaaa"}, + {1, "aaab"}, + {2, "aaac"}, + {possibleCombinations - 2, "z998"}, + {possibleCombinations - 1, "z999"}, + {possibleCombinations, "aaaa"}, + {possibleCombinations + 1, "aaab"}, + {possibleCombinations + 2, "aaac"}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%d to %s", tt.input, tt.output), func(t *testing.T) { + GenerateResourcePrefix(&sb, tt.input) + testutils.AssertNoDiff(t, tt.output, sb.String()) + sb.Reset() + }) + } +} diff --git a/sherlock/internal/deprecated_models/v2models/environment.go b/sherlock/internal/deprecated_models/v2models/environment.go index 0905dcd5b..f45ceb751 100644 --- a/sherlock/internal/deprecated_models/v2models/environment.go +++ b/sherlock/internal/deprecated_models/v2models/environment.go @@ -2,6 +2,7 @@ package v2models import ( "fmt" + "github.com/broadinstitute/sherlock/go-shared/pkg/resource_prefix" "github.com/broadinstitute/sherlock/go-shared/pkg/utils" "github.com/broadinstitute/sherlock/sherlock/internal/config" "github.com/broadinstitute/sherlock/sherlock/internal/deprecated_models/model_actions" @@ -9,7 +10,6 @@ import ( "github.com/broadinstitute/sherlock/sherlock/internal/models" "github.com/google/uuid" "github.com/rs/zerolog/log" - "math/bits" "regexp" "strconv" "strings" @@ -299,7 +299,7 @@ func preCreateEnvironment(db *gorm.DB, environment *Environment, _ *models.User) // Write the letter bytes into the strings.Builder, from our starting count plus // however many iterations we've already used. We modulo that down inside the // function to encapsulate the part that cares about the string. - generateUniqueResourcePrefix(&sb, unsignedCountOfAllEnvironmentsEver+iterations) + resource_prefix.GenerateResourcePrefix(&sb, unsignedCountOfAllEnvironmentsEver+iterations) candidate.UniqueResourcePrefix = sb.String() // Check the database for this candidate prefix existing. Note that we do not use // Unscoped here like we did above, because now we don't care if there is a @@ -335,36 +335,6 @@ func preCreateEnvironment(db *gorm.DB, environment *Environment, _ *models.User) return nil } -// Go strings are UTF-8, and these characters all map to single bytes, so this is like a `const` -// slice of bytes for the possible characters (a normal slice can't be a constant). -const ( - characterBytes = "abcdefghijklmnopqrstuvwxyz0123456789" - possibleCombinations = uint64(26 * 36 * 36 * 36) -) - -func generateUniqueResourcePrefix(sb *strings.Builder, number uint64) { - // We're assembling a string like `[r3][r2][r1][r0]`. r0 through r2 are in - // base 36, while r3 is in base 26 so the string always starts with a letter. - // r0 is the "lowest" digit, and the string is a bit similar to a hexadecimal - // number with letters taking on numeral values. The result is a string-y - // modulo representation of the input number that achieves full coverage of - // the domain to minimize conflicts. - // Example (remember that input is always modulo possibleCombinations): - // possibleCombinations-2 => z998 - // possibleCombinations-1 => z999 - // possibleCombinations => aaaa - // possibleCombinations+1 => aaab - // possibleCombinations+2 => aaac - number, r0 := bits.Div64(0, number%possibleCombinations, 36) - number, r1 := bits.Div64(0, number, 36) - number, r2 := bits.Div64(0, number, 36) - _, r3 := bits.Div64(0, number, 26) - sb.WriteByte(characterBytes[r3]) - sb.WriteByte(characterBytes[r2]) - sb.WriteByte(characterBytes[r1]) - sb.WriteByte(characterBytes[r0]) -} - func postCreateEnvironment(db *gorm.DB, environment *Environment, user *models.User) error { if environment.AutoPopulateChartReleases != nil && *environment.AutoPopulateChartReleases { if environment.Lifecycle == "dynamic" && environment.TemplateEnvironmentID != nil { diff --git a/sherlock/internal/deprecated_models/v2models/environment_test.go b/sherlock/internal/deprecated_models/v2models/environment_test.go index 3226407f4..9bea257f0 100644 --- a/sherlock/internal/deprecated_models/v2models/environment_test.go +++ b/sherlock/internal/deprecated_models/v2models/environment_test.go @@ -1,12 +1,10 @@ package v2models import ( - "fmt" "github.com/broadinstitute/sherlock/go-shared/pkg/testutils" "github.com/broadinstitute/sherlock/go-shared/pkg/utils" "github.com/broadinstitute/sherlock/sherlock/internal/deprecated_models/v2models/environment" "reflect" - "strings" "testing" "time" @@ -808,31 +806,6 @@ func Test_validateEnvironment(t *testing.T) { } } -func Test_generateUniqueResourcePrefix(t *testing.T) { - sb := strings.Builder{} - sb.Grow(4) - tests := []struct { - input uint64 - output string - }{ - {0, "aaaa"}, - {1, "aaab"}, - {2, "aaac"}, - {possibleCombinations - 2, "z998"}, - {possibleCombinations - 1, "z999"}, - {possibleCombinations, "aaaa"}, - {possibleCombinations + 1, "aaab"}, - {possibleCombinations + 2, "aaac"}, - } - for _, tt := range tests { - t.Run(fmt.Sprintf("%d to %s", tt.input, tt.output), func(t *testing.T) { - generateUniqueResourcePrefix(&sb, tt.input) - testutils.AssertNoDiff(t, tt.output, sb.String()) - sb.Reset() - }) - } -} - func TestEnvironment_GetCiIdentifier(t *testing.T) { type fields struct { Model gorm.Model From 266eb3aeefd2970d2fde9a699fffd8b35c0cf064 Mon Sep 17 00:00:00 2001 From: Jack Warren Date: Mon, 30 Oct 2023 12:49:32 -0400 Subject: [PATCH 18/21] improve cluster permissions clarity --- sherlock/internal/models/cluster.go | 21 ++++--- sherlock/internal/models/cluster_test.go | 74 ++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/sherlock/internal/models/cluster.go b/sherlock/internal/models/cluster.go index c383378e5..6f3cfe1d8 100644 --- a/sherlock/internal/models/cluster.go +++ b/sherlock/internal/models/cluster.go @@ -33,7 +33,7 @@ func (c *Cluster) GetCiIdentifier() CiIdentifier { } } -func (c *Cluster) ErrorIfForbidden(tx *gorm.DB) error { +func (c *Cluster) errorIfForbidden(tx *gorm.DB) error { user, err := GetCurrentUserForDB(tx) if err != nil { return err @@ -46,15 +46,22 @@ func (c *Cluster) ErrorIfForbidden(tx *gorm.DB) error { return nil } +// BeforeCreate checks permissions +func (c *Cluster) BeforeCreate(tx *gorm.DB) error { + return c.errorIfForbidden(tx) +} + +// BeforeUpdate checks permissions func (c *Cluster) BeforeUpdate(tx *gorm.DB) error { - // Updates could potentially set suitability lower, so we check before updates too - return c.ErrorIfForbidden(tx) + return c.errorIfForbidden(tx) } -func (c *Cluster) AfterSave(tx *gorm.DB) error { - return c.ErrorIfForbidden(tx) +// AfterUpdate checks permissions. This is necessary because mutations can change permissions. +func (c *Cluster) AfterUpdate(tx *gorm.DB) error { + return c.errorIfForbidden(tx) } -func (c *Cluster) AfterDelete(tx *gorm.DB) error { - return c.ErrorIfForbidden(tx) +// BeforeDelete checks permissions +func (c *Cluster) BeforeDelete(tx *gorm.DB) error { + return c.errorIfForbidden(tx) } diff --git a/sherlock/internal/models/cluster_test.go b/sherlock/internal/models/cluster_test.go index 28cef3785..0c0ab3041 100644 --- a/sherlock/internal/models/cluster_test.go +++ b/sherlock/internal/models/cluster_test.go @@ -2,6 +2,7 @@ package models import ( "github.com/broadinstitute/sherlock/go-shared/pkg/utils" + "github.com/broadinstitute/sherlock/sherlock/internal/errors" ) func (s *modelSuite) TestClusterNameValidationSqlMissing() { @@ -242,3 +243,76 @@ func (s *modelSuite) TestClusterCiIdentifiers() { s.Equal("cluster", result.CiIdentifier.ResourceType) }) } + +func (s *modelSuite) TestClusterCreationForbidden() { + s.SetNonSuitableTestUserForDB() + cluster := Cluster{ + Name: "some-name", + Provider: "google", + GoogleProject: "some-project", + Location: "some-location", + Base: utils.PointerTo("some base"), + Address: utils.PointerTo("0.0.0.0"), + RequiresSuitability: utils.PointerTo(true), + HelmfileRef: utils.PointerTo("some-ref"), + } + s.ErrorContains(s.DB.Create(&cluster).Error, errors.Forbidden) +} + +func (s *modelSuite) TestClusterEditEscalateForbidden() { + s.SetNonSuitableTestUserForDB() + cluster := Cluster{ + Name: "some-name", + Provider: "google", + GoogleProject: "some-project", + Location: "some-location", + Base: utils.PointerTo("some base"), + Address: utils.PointerTo("0.0.0.0"), + RequiresSuitability: utils.PointerTo(false), + HelmfileRef: utils.PointerTo("some-ref"), + } + s.NoError(s.DB.Create(&cluster).Error) + s.ErrorContains(s.DB. + Model(&cluster). + Updates(&Cluster{RequiresSuitability: utils.PointerTo(true)}). + Error, errors.Forbidden) +} + +func (s *modelSuite) TestClusterEditDeescalateForbidden() { + s.SetSuitableTestUserForDB() + cluster := Cluster{ + Name: "some-name", + Provider: "google", + GoogleProject: "some-project", + Location: "some-location", + Base: utils.PointerTo("some base"), + Address: utils.PointerTo("0.0.0.0"), + RequiresSuitability: utils.PointerTo(true), + HelmfileRef: utils.PointerTo("some-ref"), + } + s.NoError(s.DB.Create(&cluster).Error) + s.SetNonSuitableTestUserForDB() + s.ErrorContains(s.DB. + Model(&cluster). + Updates(&Cluster{RequiresSuitability: utils.PointerTo(false)}). + Error, errors.Forbidden) +} + +func (s *modelSuite) TestClusterDeleteForbidden() { + s.SetSuitableTestUserForDB() + cluster := Cluster{ + Name: "some-name", + Provider: "google", + GoogleProject: "some-project", + Location: "some-location", + Base: utils.PointerTo("some base"), + Address: utils.PointerTo("0.0.0.0"), + RequiresSuitability: utils.PointerTo(true), + HelmfileRef: utils.PointerTo("some-ref"), + } + s.NoError(s.DB.Create(&cluster).Error) + s.SetNonSuitableTestUserForDB() + s.ErrorContains(s.DB. + Delete(&cluster). + Error, errors.Forbidden) +} From 1fcfe71b8a994d42b2de5ffaddd5182a1b16293f Mon Sep 17 00:00:00 2001 From: Jack Warren Date: Tue, 31 Oct 2023 10:20:50 -0400 Subject: [PATCH 19/21] TestData --- sherlock/internal/models/chart_test.go | 3 +- .../internal/models/chart_version_test.go | 6 +- sherlock/internal/models/test_data.go | 121 ++++++++++++++++++ sherlock/internal/models/test_helper.go | 43 +++---- 4 files changed, 140 insertions(+), 33 deletions(-) create mode 100644 sherlock/internal/models/test_data.go diff --git a/sherlock/internal/models/chart_test.go b/sherlock/internal/models/chart_test.go index e1f55f584..23cf2428e 100644 --- a/sherlock/internal/models/chart_test.go +++ b/sherlock/internal/models/chart_test.go @@ -32,8 +32,7 @@ func (s *modelSuite) TestChartValidationSqlValid() { } func (s *modelSuite) TestChartCiIdentifiers() { - chart := Chart{Name: "name", ChartRepo: utils.PointerTo("repo")} - s.NoError(s.DB.Create(&chart).Error) + chart := s.TestData.Chart_Leonardo() ciIdentifier := chart.GetCiIdentifier() s.NoError(s.DB.Create(&ciIdentifier).Error) s.NotZero(ciIdentifier.ID) diff --git a/sherlock/internal/models/chart_version_test.go b/sherlock/internal/models/chart_version_test.go index 3afef8258..7a159eab3 100644 --- a/sherlock/internal/models/chart_version_test.go +++ b/sherlock/internal/models/chart_version_test.go @@ -50,11 +50,7 @@ func (s *modelSuite) TestChartVersionValid() { } func (s *modelSuite) TestChartVersionCiIdentifiers() { - s.SetNonSuitableTestUserForDB() - chart := Chart{Name: "name", ChartRepo: utils.PointerTo("repo")} - s.NoError(s.DB.Create(&chart).Error) - chartVersion := ChartVersion{ChartID: chart.ID, ChartVersion: "version"} - s.NoError(s.DB.Create(&chartVersion).Error) + chartVersion := s.TestData.ChartVersion_Leonardo_V1() ciIdentifier := chartVersion.GetCiIdentifier() s.NoError(s.DB.Create(&ciIdentifier).Error) s.NotZero(ciIdentifier.ID) diff --git a/sherlock/internal/models/test_data.go b/sherlock/internal/models/test_data.go new file mode 100644 index 000000000..ec6a16550 --- /dev/null +++ b/sherlock/internal/models/test_data.go @@ -0,0 +1,121 @@ +package models + +import ( + "fmt" + "github.com/broadinstitute/sherlock/go-shared/pkg/utils" + "github.com/broadinstitute/sherlock/sherlock/internal/authentication/test_users" + "github.com/rs/zerolog/log" +) + +// TestData offers convenience methods for example data for usage in testing. +// 1. The data returned from these methods will exist in the database, along +// with any necessary dependencies, at the time of the first return. +// 2. These methods cache within the context of a test function. Subsequent +// calls to a method will not contact the database. +type TestData interface { + User_Suitable() User + User_NonSuitable() User + + Chart_Leonardo() Chart + + ChartVersion_Leonardo_V1() ChartVersion + ChartVersion_Leonardo_V2() ChartVersion +} + +// testDataImpl contains the caching for TestData and a (back-)reference to +// TestSuiteHelper to actually interact with the database. TestSuiteHelper +// uses testDataImpl to provide TestData in the context of a test function. +type testDataImpl struct { + h *TestSuiteHelper + + user_suitable User + user_nonSuitable User + + chart_leonardo Chart + + chartVersion_leonardo_v1 ChartVersion + chartVersion_leonardo_v2 ChartVersion +} + +// User_Suitable essentially defers to the authentication and +// authorization packages: it returns a User based on the +// authentication package's test_users.SuitableTestUserEmail, +// which the authorization package will recognize when appropriate. +// +// The benefit of this approach is the identity of the test suitable +// user is kept consistent, regardless of whether it comes from here +// or from mock authentication middleware +func (td *testDataImpl) User_Suitable() User { + if td.user_suitable.ID == 0 { + td.user_suitable = User{ + Email: test_users.SuitableTestUserEmail, + GoogleID: test_users.SuitableTestUserGoogleID, + } + td.create(&td.user_suitable) + } + return td.user_suitable +} + +// User_NonSuitable is like User_Suitable but for a non-suitable User +func (td *testDataImpl) User_NonSuitable() User { + if td.user_nonSuitable.ID == 0 { + td.user_nonSuitable = User{ + Email: test_users.NonSuitableTestUserEmail, + GoogleID: test_users.NonSuitableTestUserGoogleID, + } + td.create(&td.user_nonSuitable) + } + return td.user_nonSuitable +} + +func (td *testDataImpl) Chart_Leonardo() Chart { + if td.chart_leonardo.ID == 0 { + td.chart_leonardo = Chart{ + Name: "leonardo", + ChartRepo: utils.PointerTo("terra-helm"), + AppImageGitRepo: utils.PointerTo("DataBiosphere/leonardo"), + AppImageGitMainBranch: utils.PointerTo("main"), + ChartExposesEndpoint: utils.PointerTo(true), + DefaultSubdomain: utils.PointerTo("leonardo"), + DefaultProtocol: utils.PointerTo("https"), + DefaultPort: utils.PointerTo[uint](443), + } + td.create(&td.chart_leonardo) + } + return td.chart_leonardo +} + +func (td *testDataImpl) ChartVersion_Leonardo_V1() ChartVersion { + if td.chartVersion_leonardo_v1.ID == 0 { + td.chartVersion_leonardo_v1 = ChartVersion{ + ChartID: td.Chart_Leonardo().ID, + ChartVersion: "0.1.0", + } + td.h.SetSuitableTestUserForDB() + td.create(&td.chartVersion_leonardo_v1) + } + return td.chartVersion_leonardo_v1 +} + +func (td *testDataImpl) ChartVersion_Leonardo_V2() ChartVersion { + if td.chartVersion_leonardo_v2.ID == 0 { + td.chartVersion_leonardo_v2 = ChartVersion{ + ChartID: td.Chart_Leonardo().ID, + ChartVersion: "0.2.0", + ParentChartVersionID: utils.PointerTo(td.ChartVersion_Leonardo_V1().ID), + } + td.h.SetSuitableTestUserForDB() + td.create(&td.chartVersion_leonardo_v2) + } + return td.chartVersion_leonardo_v2 +} + +// create is a helper function for creating TestData entries in the database. +// It will forcibly exit if it encounters an error. +func (td *testDataImpl) create(pointer any) { + if err := td.h.DB.Create(pointer).Error; err != nil { + err = fmt.Errorf("error creating %T in TestData: %w", pointer, err) + log.Fatal().Err(err).Caller(2).Send() + panic(err) + } +} diff --git a/sherlock/internal/models/test_helper.go b/sherlock/internal/models/test_helper.go index e501ae78f..f11b0f6d3 100644 --- a/sherlock/internal/models/test_helper.go +++ b/sherlock/internal/models/test_helper.go @@ -1,8 +1,8 @@ package models import ( + "github.com/broadinstitute/sherlock/go-shared/pkg/utils" "github.com/broadinstitute/sherlock/sherlock/internal/authentication/authentication_method" - "github.com/broadinstitute/sherlock/sherlock/internal/authentication/test_users" "github.com/broadinstitute/sherlock/sherlock/internal/config" "github.com/broadinstitute/sherlock/sherlock/internal/db" "gorm.io/gorm" @@ -14,6 +14,7 @@ import ( type TestSuiteHelper struct { DB *gorm.DB internalDB *gorm.DB + TestData TestData } // SetupSuite runs once before all tests. It connects to the @@ -64,39 +65,28 @@ func (h *TestSuiteHelper) SetupSuite() { // changes. func (h *TestSuiteHelper) SetupTest() { h.DB = h.internalDB.Begin() + h.TestData = &testDataImpl{h: h} } -// SetUserForDB is intended to be called from within a test function. -// It will upsert a User with the given email and googleID and -// set it as the DB's current user. -func (h *TestSuiteHelper) SetUserForDB(email, googleID string) *User { - var user User - if err := h.DB. - Where(&User{Email: email, GoogleID: googleID}). - FirstOrCreate(&user).Error; err != nil { - panic(err) - } else { - user.AuthenticationMethod = authentication_method.TEST - h.DB = SetCurrentUserForDB(h.DB, &user) - return &user - } +// SetUserForDB is a low-level helper function, setting with given user as +// the current principal for the database. You'll usually want to call +// SetSuitableTestUserForDB or SetNonSuitableTestUserForDB instead. +func (h *TestSuiteHelper) SetUserForDB(user *User) *User { + user.AuthenticationMethod = authentication_method.TEST + h.DB = SetCurrentUserForDB(h.DB, user) + return user } -// SetSuitableTestUserForDB is intended to be called from within -// a test function. It calls UseUser with the suitable test user -// info from the test_users package, which will be recognized -// as suitable by the authorization package. +// SetSuitableTestUserForDB is a helper function, calling SetUserForDB with +// TestData.User_Suitable func (h *TestSuiteHelper) SetSuitableTestUserForDB() *User { - return h.SetUserForDB(test_users.SuitableTestUserEmail, test_users.SuitableTestUserGoogleID) + return h.SetUserForDB(utils.PointerTo(h.TestData.User_Suitable())) } -// SetNonSuitableTestUserForDB is intended to be called from -// within a test function. It calls UseUser with the -// non-suitable test user from the test_users package, which -// will be recognized but considered non-suitable by the -// authorization package. +// SetNonSuitableTestUserForDB is a helper function, calling SetUserForDB with +// TestData.User_NonSuitable func (h *TestSuiteHelper) SetNonSuitableTestUserForDB() *User { - return h.SetUserForDB(test_users.NonSuitableTestUserEmail, test_users.NonSuitableTestUserGoogleID) + return h.SetUserForDB(utils.PointerTo(h.TestData.User_NonSuitable())) } // TearDownTest takes advantage of SetupTest having begun a @@ -104,6 +94,7 @@ func (h *TestSuiteHelper) SetNonSuitableTestUserForDB() *User { // sets the modelSuite's main database reference to nil to // help surface any concurrency issues. func (h *TestSuiteHelper) TearDownTest() { + h.TestData = nil h.DB.Rollback() h.DB = nil } From 09871d08293eaf09171b9358eef72fb3f63b7af2 Mon Sep 17 00:00:00 2001 From: Jack Warren Date: Tue, 31 Oct 2023 12:13:36 -0400 Subject: [PATCH 20/21] FirstOrCreate --- sherlock/internal/models/test_data.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sherlock/internal/models/test_data.go b/sherlock/internal/models/test_data.go index ec6a16550..a86a2a1f4 100644 --- a/sherlock/internal/models/test_data.go +++ b/sherlock/internal/models/test_data.go @@ -113,9 +113,12 @@ func (td *testDataImpl) ChartVersion_Leonardo_V2() ChartVersion { // create is a helper function for creating TestData entries in the database. // It will forcibly exit if it encounters an error. func (td *testDataImpl) create(pointer any) { - if err := td.h.DB.Create(pointer).Error; err != nil { + // We do FirstOrCreate on the off-chance that what we're inserting already exists. + // That'll basically never happen... except for when Sherlock is trying to be helpful. + // Middleware will upsert users, the database layer will auto-populate resources, etc. + if err := td.h.DB.FirstOrCreate(pointer).Error; err != nil { err = fmt.Errorf("error creating %T in TestData: %w", pointer, err) - log.Fatal().Err(err).Caller(2).Send() + log.Error().Err(err).Caller(2).Send() panic(err) } } From 3c3a54a1e95128e5b8e8f6d7630c70d826666d25 Mon Sep 17 00:00:00 2001 From: Jack Warren Date: Tue, 31 Oct 2023 12:28:24 -0400 Subject: [PATCH 21/21] proper FirstOrCreate --- sherlock/internal/models/test_data.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sherlock/internal/models/test_data.go b/sherlock/internal/models/test_data.go index a86a2a1f4..b06b627ab 100644 --- a/sherlock/internal/models/test_data.go +++ b/sherlock/internal/models/test_data.go @@ -116,7 +116,7 @@ func (td *testDataImpl) create(pointer any) { // We do FirstOrCreate on the off-chance that what we're inserting already exists. // That'll basically never happen... except for when Sherlock is trying to be helpful. // Middleware will upsert users, the database layer will auto-populate resources, etc. - if err := td.h.DB.FirstOrCreate(pointer).Error; err != nil { + if err := td.h.DB.Where(pointer).FirstOrCreate(pointer).Error; err != nil { err = fmt.Errorf("error creating %T in TestData: %w", pointer, err) log.Error().Err(err).Caller(2).Send() panic(err)