Skip to content

Commit 638b82a

Browse files
committed
dbmem-panic-2
1 parent bf0271f commit 638b82a

File tree

2 files changed

+41
-166
lines changed

2 files changed

+41
-166
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 3 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"slices"
1515
"sort"
1616
"strings"
17-
"sync"
1817
"time"
1918

2019
"github.com/google/uuid"
@@ -23,9 +22,10 @@ import (
2322
"golang.org/x/exp/maps"
2423
"golang.org/x/xerrors"
2524

25+
"github.com/coder/coder/v2/coderd/notifications/types"
26+
2627
"github.com/coder/coder/v2/coderd/database"
2728
"github.com/coder/coder/v2/coderd/database/dbtime"
28-
"github.com/coder/coder/v2/coderd/notifications/types"
2929
"github.com/coder/coder/v2/coderd/rbac"
3030
"github.com/coder/coder/v2/coderd/rbac/regosql"
3131
"github.com/coder/coder/v2/coderd/util/slice"
@@ -51,127 +51,7 @@ var (
5151

5252
// New returns an in-memory fake of the database.
5353
func New() database.Store {
54-
q := &FakeQuerier{
55-
mutex: &sync.RWMutex{},
56-
data: &data{
57-
apiKeys: make([]database.APIKey, 0),
58-
auditLogs: make([]database.AuditLog, 0),
59-
customRoles: make([]database.CustomRole, 0),
60-
dbcryptKeys: make([]database.DBCryptKey, 0),
61-
externalAuthLinks: make([]database.ExternalAuthLink, 0),
62-
files: make([]database.File, 0),
63-
gitSSHKey: make([]database.GitSSHKey, 0),
64-
groups: make([]database.Group, 0),
65-
groupMembers: make([]database.GroupMemberTable, 0),
66-
licenses: make([]database.License, 0),
67-
locks: map[int64]struct{}{},
68-
notificationMessages: make([]database.NotificationMessage, 0),
69-
notificationPreferences: make([]database.NotificationPreference, 0),
70-
organizationMembers: make([]database.OrganizationMember, 0),
71-
organizations: make([]database.Organization, 0),
72-
inboxNotifications: make([]database.InboxNotification, 0),
73-
parameterSchemas: make([]database.ParameterSchema, 0),
74-
presets: make([]database.TemplateVersionPreset, 0),
75-
presetParameters: make([]database.TemplateVersionPresetParameter, 0),
76-
presetPrebuildSchedules: make([]database.TemplateVersionPresetPrebuildSchedule, 0),
77-
provisionerDaemons: make([]database.ProvisionerDaemon, 0),
78-
provisionerJobs: make([]database.ProvisionerJob, 0),
79-
provisionerJobLogs: make([]database.ProvisionerJobLog, 0),
80-
provisionerKeys: make([]database.ProvisionerKey, 0),
81-
runtimeConfig: map[string]string{},
82-
telemetryItems: make([]database.TelemetryItem, 0),
83-
templateVersions: make([]database.TemplateVersionTable, 0),
84-
templateVersionTerraformValues: make([]database.TemplateVersionTerraformValue, 0),
85-
templates: make([]database.TemplateTable, 0),
86-
users: make([]database.User, 0),
87-
userConfigs: make([]database.UserConfig, 0),
88-
userStatusChanges: make([]database.UserStatusChange, 0),
89-
workspaceAgents: make([]database.WorkspaceAgent, 0),
90-
workspaceResources: make([]database.WorkspaceResource, 0),
91-
workspaceModules: make([]database.WorkspaceModule, 0),
92-
workspaceResourceMetadata: make([]database.WorkspaceResourceMetadatum, 0),
93-
workspaceAgentStats: make([]database.WorkspaceAgentStat, 0),
94-
workspaceAgentLogs: make([]database.WorkspaceAgentLog, 0),
95-
workspaceBuilds: make([]database.WorkspaceBuild, 0),
96-
workspaceApps: make([]database.WorkspaceApp, 0),
97-
workspaceAppAuditSessions: make([]database.WorkspaceAppAuditSession, 0),
98-
workspaces: make([]database.WorkspaceTable, 0),
99-
workspaceProxies: make([]database.WorkspaceProxy, 0),
100-
},
101-
}
102-
// Always start with a default org. Matching migration 198.
103-
defaultOrg, err := q.InsertOrganization(context.Background(), database.InsertOrganizationParams{
104-
ID: uuid.New(),
105-
Name: "coder",
106-
DisplayName: "Coder",
107-
Description: "Builtin default organization.",
108-
Icon: "",
109-
CreatedAt: dbtime.Now(),
110-
UpdatedAt: dbtime.Now(),
111-
})
112-
if err != nil {
113-
panic(xerrors.Errorf("failed to create default organization: %w", err))
114-
}
115-
116-
_, err = q.InsertAllUsersGroup(context.Background(), defaultOrg.ID)
117-
if err != nil {
118-
panic(xerrors.Errorf("failed to create default group: %w", err))
119-
}
120-
121-
q.defaultProxyDisplayName = "Default"
122-
q.defaultProxyIconURL = "/emojis/1f3e1.png"
123-
124-
_, err = q.InsertProvisionerKey(context.Background(), database.InsertProvisionerKeyParams{
125-
ID: codersdk.ProvisionerKeyUUIDBuiltIn,
126-
OrganizationID: defaultOrg.ID,
127-
CreatedAt: dbtime.Now(),
128-
HashedSecret: []byte{},
129-
Name: codersdk.ProvisionerKeyNameBuiltIn,
130-
Tags: map[string]string{},
131-
})
132-
if err != nil {
133-
panic(xerrors.Errorf("failed to create built-in provisioner key: %w", err))
134-
}
135-
_, err = q.InsertProvisionerKey(context.Background(), database.InsertProvisionerKeyParams{
136-
ID: codersdk.ProvisionerKeyUUIDUserAuth,
137-
OrganizationID: defaultOrg.ID,
138-
CreatedAt: dbtime.Now(),
139-
HashedSecret: []byte{},
140-
Name: codersdk.ProvisionerKeyNameUserAuth,
141-
Tags: map[string]string{},
142-
})
143-
if err != nil {
144-
panic(xerrors.Errorf("failed to create user-auth provisioner key: %w", err))
145-
}
146-
_, err = q.InsertProvisionerKey(context.Background(), database.InsertProvisionerKeyParams{
147-
ID: codersdk.ProvisionerKeyUUIDPSK,
148-
OrganizationID: defaultOrg.ID,
149-
CreatedAt: dbtime.Now(),
150-
HashedSecret: []byte{},
151-
Name: codersdk.ProvisionerKeyNamePSK,
152-
Tags: map[string]string{},
153-
})
154-
if err != nil {
155-
panic(xerrors.Errorf("failed to create psk provisioner key: %w", err))
156-
}
157-
158-
q.mutex.Lock()
159-
// We can't insert this user using the interface, because it's a system user.
160-
q.data.users = append(q.data.users, database.User{
161-
ID: database.PrebuildsSystemUserID,
162-
Email: "prebuilds@coder.com",
163-
Username: "prebuilds",
164-
CreatedAt: dbtime.Now(),
165-
UpdatedAt: dbtime.Now(),
166-
Status: "active",
167-
LoginType: "none",
168-
HashedPassword: []byte{},
169-
IsSystem: true,
170-
Deleted: false,
171-
})
172-
q.mutex.Unlock()
173-
174-
return q
54+
panic("dbmem created")
17555
}
17656

17757
type rwMutex interface {

coderd/database/dbtestutil/db.go

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020

2121
"cdr.dev/slog"
2222
"github.com/coder/coder/v2/coderd/database"
23-
"github.com/coder/coder/v2/coderd/database/dbmem"
2423
"github.com/coder/coder/v2/coderd/database/pubsub"
2524
"github.com/coder/coder/v2/testutil"
2625
)
@@ -109,52 +108,48 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
109108

110109
var db database.Store
111110
var ps pubsub.Pubsub
112-
if WillUsePostgres() {
113-
connectionURL := os.Getenv("CODER_PG_CONNECTION_URL")
114-
if connectionURL == "" && o.url != "" {
115-
connectionURL = o.url
116-
}
117-
if connectionURL == "" {
118-
var err error
119-
connectionURL, err = Open(t)
120-
require.NoError(t, err)
121-
}
122-
123-
if o.fixedTimezone == "" {
124-
// To make sure we find timezone-related issues, we set the timezone
125-
// of the database to a non-UTC one.
126-
// The below was picked due to the following properties:
127-
// - It has a non-UTC offset
128-
// - It has a fractional hour UTC offset
129-
// - It includes a daylight savings time component
130-
o.fixedTimezone = DefaultTimezone
131-
}
132-
dbName := dbNameFromConnectionURL(t, connectionURL)
133-
setDBTimezone(t, connectionURL, dbName, o.fixedTimezone)
134111

135-
sqlDB, err := sql.Open("postgres", connectionURL)
112+
connectionURL := os.Getenv("CODER_PG_CONNECTION_URL")
113+
if connectionURL == "" && o.url != "" {
114+
connectionURL = o.url
115+
}
116+
if connectionURL == "" {
117+
var err error
118+
connectionURL, err = Open(t)
136119
require.NoError(t, err)
137-
t.Cleanup(func() {
138-
_ = sqlDB.Close()
139-
})
140-
if o.returnSQLDB != nil {
141-
o.returnSQLDB(sqlDB)
142-
}
143-
if o.dumpOnFailure {
144-
t.Cleanup(func() { DumpOnFailure(t, connectionURL) })
145-
}
146-
// Unit tests should not retry serial transaction failures.
147-
db = database.New(sqlDB, database.WithSerialRetryCount(1))
120+
}
148121

149-
ps, err = pubsub.New(context.Background(), o.logger, sqlDB, connectionURL)
150-
require.NoError(t, err)
151-
t.Cleanup(func() {
152-
_ = ps.Close()
153-
})
154-
} else {
155-
db = dbmem.New()
156-
ps = pubsub.NewInMemory()
122+
if o.fixedTimezone == "" {
123+
// To make sure we find timezone-related issues, we set the timezone
124+
// of the database to a non-UTC one.
125+
// The below was picked due to the following properties:
126+
// - It has a non-UTC offset
127+
// - It has a fractional hour UTC offset
128+
// - It includes a daylight savings time component
129+
o.fixedTimezone = DefaultTimezone
130+
}
131+
dbName := dbNameFromConnectionURL(t, connectionURL)
132+
setDBTimezone(t, connectionURL, dbName, o.fixedTimezone)
133+
134+
sqlDB, err := sql.Open("postgres", connectionURL)
135+
require.NoError(t, err)
136+
t.Cleanup(func() {
137+
_ = sqlDB.Close()
138+
})
139+
if o.returnSQLDB != nil {
140+
o.returnSQLDB(sqlDB)
141+
}
142+
if o.dumpOnFailure {
143+
t.Cleanup(func() { DumpOnFailure(t, connectionURL) })
157144
}
145+
// Unit tests should not retry serial transaction failures.
146+
db = database.New(sqlDB, database.WithSerialRetryCount(1))
147+
148+
ps, err = pubsub.New(context.Background(), o.logger, sqlDB, connectionURL)
149+
require.NoError(t, err)
150+
t.Cleanup(func() {
151+
_ = ps.Close()
152+
})
158153

159154
return db, ps
160155
}

0 commit comments

Comments
 (0)