Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 1f71b47

Browse files
authored
chore/enterpriseportal: split database package (#63425)
As we set up to introduce more tables to Enterprise Portal, I think it'll be more sustainable for things to get their own subpackages. No real code changes, just moving things around. ## Test plan CI
1 parent cb3a1e4 commit 1f71b47

File tree

16 files changed

+220
-159
lines changed

16 files changed

+220
-159
lines changed
Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
load("//dev:go_defs.bzl", "go_test")
21
load("@io_bazel_rules_go//go:def.bzl", "go_library")
32

43
go_library(
54
name = "database",
65
srcs = [
76
"database.go",
87
"migrate.go",
9-
"subscriptions.go",
108
],
119
importpath = "github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database",
10+
tags = [TAG_INFRA_CORESERVICES],
1211
visibility = ["//cmd/enterprise-portal:__subpackages__"],
1312
deps = [
13+
"//cmd/enterprise-portal/internal/database/internal/tables",
14+
"//cmd/enterprise-portal/internal/database/subscriptions",
1415
"//lib/errors",
1516
"//lib/managedservicesplatform/runtime",
1617
"//lib/redislock",
@@ -21,35 +22,10 @@ go_library(
2122
"@io_gorm_driver_postgres//:postgres",
2223
"@io_gorm_gorm//:gorm",
2324
"@io_gorm_gorm//logger",
24-
"@io_gorm_gorm//schema",
2525
"@io_gorm_plugin_opentelemetry//tracing",
2626
"@io_opentelemetry_go_otel//:otel",
2727
"@io_opentelemetry_go_otel//attribute",
2828
"@io_opentelemetry_go_otel//codes",
2929
"@io_opentelemetry_go_otel_trace//:trace",
3030
],
3131
)
32-
33-
go_test(
34-
name = "database_test",
35-
srcs = [
36-
"main_test.go",
37-
"subscriptions_test.go",
38-
],
39-
embed = [":database"],
40-
tags = [
41-
# Test requires localhost database
42-
"requires-network",
43-
],
44-
deps = [
45-
"//internal/database/dbtest",
46-
"@com_github_google_uuid//:uuid",
47-
"@com_github_jackc_pgx_v5//:pgx",
48-
"@com_github_jackc_pgx_v5//pgxpool",
49-
"@com_github_stretchr_testify//assert",
50-
"@com_github_stretchr_testify//require",
51-
"@io_gorm_driver_postgres//:postgres",
52-
"@io_gorm_gorm//:gorm",
53-
"@io_gorm_gorm//schema",
54-
],
55-
)

cmd/enterprise-portal/internal/database/database.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
"github.com/redis/go-redis/v9"
1010
"github.com/sourcegraph/log"
1111
"go.opentelemetry.io/otel"
12-
"gorm.io/gorm/schema"
1312

1413
"github.com/sourcegraph/sourcegraph/lib/errors"
1514
"github.com/sourcegraph/sourcegraph/lib/managedservicesplatform/runtime"
15+
16+
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/subscriptions"
1617
)
1718

1819
var databaseTracer = otel.Tracer("enterprise-portal/internal/database")
@@ -22,13 +23,8 @@ type DB struct {
2223
db *pgxpool.Pool
2324
}
2425

25-
func (db *DB) Subscriptions() *SubscriptionsStore {
26-
return newSubscriptionsStore(db.db)
27-
}
28-
29-
// ⚠️ WARNING: This list is meant to be read-only.
30-
var allTables = []schema.Tabler{
31-
&Subscription{},
26+
func (db *DB) Subscriptions() *subscriptions.Store {
27+
return subscriptions.NewStore(db.db)
3228
}
3329

3430
func databaseName(msp bool) string {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "databasetest",
5+
srcs = ["databasetest.go"],
6+
importpath = "github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/databasetest",
7+
tags = [TAG_INFRA_CORESERVICES],
8+
visibility = ["//cmd/enterprise-portal:__subpackages__"],
9+
deps = [
10+
"//internal/database/dbtest",
11+
"@com_github_jackc_pgx_v5//pgxpool",
12+
"@com_github_stretchr_testify//require",
13+
"@io_gorm_driver_postgres//:postgres",
14+
"@io_gorm_gorm//:gorm",
15+
"@io_gorm_gorm//schema",
16+
],
17+
)

cmd/enterprise-portal/internal/database/main_test.go renamed to cmd/enterprise-portal/internal/database/databasetest/databasetest.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package database
1+
package databasetest
22

33
import (
44
"context"
@@ -17,12 +17,12 @@ import (
1717
"github.com/sourcegraph/sourcegraph/internal/database/dbtest"
1818
)
1919

20-
// newTestDB creates a new test database and initializes the given list of
20+
// NewTestDB creates a new test database and initializes the given list of
2121
// tables for the suite. The test database is dropped after testing is completed
2222
// unless failed.
2323
//
2424
// Future: Move to a shared package when more than Enterprise Portal uses it.
25-
func newTestDB(t testing.TB, system, suite string, tables ...schema.Tabler) *pgxpool.Pool {
25+
func NewTestDB(t testing.TB, system, suite string, tables ...schema.Tabler) *pgxpool.Pool {
2626
if testing.Short() {
2727
t.Skip("skipping DB test since -short specified")
2828
}
@@ -92,20 +92,25 @@ func newTestDB(t testing.TB, system, suite string, tables ...schema.Tabler) *pgx
9292
return testDB
9393
}
9494

95-
// clearTables removes all rows from the list of tables in the original order.
96-
// It uses soft-deletion when available and skips deletion when the test suite
97-
// failed.
95+
// ClearTablesAfterTest removes all rows from the list of tables in the original
96+
// order as a t.Cleanup hook. It uses soft-deletion when available and skips
97+
// deletion when the test suite failed.
9898
//
9999
// Future: Move to a shared package when more than Enterprise Portal uses it.
100-
func clearTables(t *testing.T, db *pgxpool.Pool, tables ...schema.Tabler) error {
101-
if t.Failed() {
102-
return nil
103-
}
100+
func ClearTablesAfterTest(t *testing.T, db *pgxpool.Pool, tables ...schema.Tabler) {
101+
t.Cleanup(func() {
102+
if t.Failed() {
103+
t.Log("Leaving table data intact after test failure")
104+
return
105+
}
104106

105-
tableNames := make([]string, 0, len(tables))
106-
for _, t := range tables {
107-
tableNames = append(tableNames, t.TableName())
108-
}
109-
_, err := db.Exec(context.Background(), "TRUNCATE TABLE "+strings.Join(tableNames, ", ")+" RESTART IDENTITY")
110-
return err
107+
tableNames := make([]string, 0, len(tables))
108+
for _, t := range tables {
109+
tableNames = append(tableNames, t.TableName())
110+
}
111+
_, err := db.Exec(context.Background(), "TRUNCATE TABLE "+strings.Join(tableNames, ", ")+" RESTART IDENTITY")
112+
if err != nil {
113+
t.Errorf("Failed to clear table data: %v", err)
114+
}
115+
})
111116
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "tables",
5+
srcs = ["tables.go"],
6+
importpath = "github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/tables",
7+
tags = [TAG_INFRA_CORESERVICES],
8+
visibility = ["//cmd/enterprise-portal:__subpackages__"],
9+
deps = [
10+
"//cmd/enterprise-portal/internal/database/subscriptions",
11+
"@io_gorm_gorm//schema",
12+
],
13+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package tables
2+
3+
import (
4+
"gorm.io/gorm/schema"
5+
6+
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/subscriptions"
7+
)
8+
9+
// All tables provisioned for the Enterprise Portal database are defined here.
10+
//
11+
// ⚠️ WARNING: This list is meant to be read-only.
12+
func All() []schema.Tabler {
13+
return []schema.Tabler{
14+
&subscriptions.Subscription{},
15+
}
16+
}

cmd/enterprise-portal/internal/database/migrate.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/sourcegraph/sourcegraph/lib/errors"
2020
"github.com/sourcegraph/sourcegraph/lib/managedservicesplatform/runtime"
2121
"github.com/sourcegraph/sourcegraph/lib/redislock"
22+
23+
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/tables"
2224
)
2325

2426
// maybeMigrate runs the auto-migration for the database when needed based on
@@ -96,7 +98,7 @@ func maybeMigrate(ctx context.Context, logger log.Logger, contract runtime.Contr
9698
Logger: gormlogger.Default.LogMode(gormlogger.Warn),
9799
})
98100
// Auto-migrate database table definitions.
99-
for _, table := range allTables {
101+
for _, table := range tables.All() {
100102
err := sess.AutoMigrate(table)
101103
if err != nil {
102104
return errors.Wrapf(err, "auto migrating table for %s", errors.Safe(fmt.Sprintf("%T", table)))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
load("//dev:go_defs.bzl", "go_test")
3+
4+
go_library(
5+
name = "subscriptions",
6+
srcs = ["subscriptions.go"],
7+
importpath = "github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/subscriptions",
8+
tags = [TAG_INFRA_CORESERVICES],
9+
visibility = ["//cmd/enterprise-portal:__subpackages__"],
10+
deps = [
11+
"//lib/errors",
12+
"@com_github_jackc_pgx_v5//:pgx",
13+
"@com_github_jackc_pgx_v5//pgxpool",
14+
],
15+
)
16+
17+
go_test(
18+
name = "subscriptions_test",
19+
srcs = ["subscriptions_test.go"],
20+
tags = [
21+
TAG_INFRA_CORESERVICES,
22+
"requires-network",
23+
],
24+
deps = [
25+
":subscriptions",
26+
"//cmd/enterprise-portal/internal/database/databasetest",
27+
"//cmd/enterprise-portal/internal/database/internal/tables",
28+
"@com_github_google_uuid//:uuid",
29+
"@com_github_jackc_pgx_v5//:pgx",
30+
"@com_github_stretchr_testify//assert",
31+
"@com_github_stretchr_testify//require",
32+
],
33+
)

cmd/enterprise-portal/internal/database/subscriptions.go renamed to cmd/enterprise-portal/internal/database/subscriptions/subscriptions.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package database
1+
package subscriptions
22

33
import (
44
"context"
@@ -11,7 +11,7 @@ import (
1111
"github.com/sourcegraph/sourcegraph/lib/errors"
1212
)
1313

14-
// Subscription is a product subscription record.
14+
// Subscription is an Enterprise subscription record.
1515
type Subscription struct {
1616
// ID is the prefixed UUID-format identifier for the subscription.
1717
ID string `gorm:"primaryKey"`
@@ -24,13 +24,13 @@ func (s *Subscription) TableName() string {
2424
return "enterprise_portal_subscriptions"
2525
}
2626

27-
// SubscriptionsStore is the storage layer for product subscriptions.
28-
type SubscriptionsStore struct {
27+
// Store is the storage layer for product subscriptions.
28+
type Store struct {
2929
db *pgxpool.Pool
3030
}
3131

32-
func newSubscriptionsStore(db *pgxpool.Pool) *SubscriptionsStore {
33-
return &SubscriptionsStore{
32+
func NewStore(db *pgxpool.Pool) *Store {
33+
return &Store{
3434
db: db,
3535
}
3636
}
@@ -74,7 +74,7 @@ func (opts ListEnterpriseSubscriptionsOptions) toQueryConditions() (where, limit
7474
}
7575

7676
// List returns a list of subscriptions based on the given options.
77-
func (s *SubscriptionsStore) List(ctx context.Context, opts ListEnterpriseSubscriptionsOptions) ([]*Subscription, error) {
77+
func (s *Store) List(ctx context.Context, opts ListEnterpriseSubscriptionsOptions) ([]*Subscription, error) {
7878
where, limit, namedArgs := opts.toQueryConditions()
7979
query := fmt.Sprintf(`
8080
SELECT
@@ -138,7 +138,7 @@ DO UPDATE SET
138138
}
139139

140140
// Upsert upserts a subscription record based on the given options.
141-
func (s *SubscriptionsStore) Upsert(ctx context.Context, subscriptionID string, opts UpsertSubscriptionOptions) (*Subscription, error) {
141+
func (s *Store) Upsert(ctx context.Context, subscriptionID string, opts UpsertSubscriptionOptions) (*Subscription, error) {
142142
query, namedArgs := opts.toQuery(subscriptionID)
143143
if query != "" {
144144
_, err := s.db.Exec(ctx, query, namedArgs)
@@ -151,7 +151,7 @@ func (s *SubscriptionsStore) Upsert(ctx context.Context, subscriptionID string,
151151

152152
// Get returns a subscription record with the given subscription ID. It returns
153153
// pgx.ErrNoRows if no such subscription exists.
154-
func (s *SubscriptionsStore) Get(ctx context.Context, subscriptionID string) (*Subscription, error) {
154+
func (s *Store) Get(ctx context.Context, subscriptionID string) (*Subscription, error) {
155155
var subscription Subscription
156156
query := `SELECT id, instance_domain FROM enterprise_portal_subscriptions WHERE id = @id`
157157
namedArgs := pgx.NamedArgs{"id": subscriptionID}

0 commit comments

Comments
 (0)