@@ -18,33 +18,54 @@ import (
1818// New returns an in-memory fake of the database.
1919func New () database.Store {
2020 return & fakeQuerier {
21- apiKeys : make ([]database.APIKey , 0 ),
22- organizationMembers : make ([]database.OrganizationMember , 0 ),
23- organizations : make ([]database.Organization , 0 ),
24- users : make ([]database.User , 0 ),
25-
26- auditLogs : make ([]database.AuditLog , 0 ),
27- files : make ([]database.File , 0 ),
28- gitSSHKey : make ([]database.GitSSHKey , 0 ),
29- parameterSchemas : make ([]database.ParameterSchema , 0 ),
30- parameterValues : make ([]database.ParameterValue , 0 ),
31- provisionerDaemons : make ([]database.ProvisionerDaemon , 0 ),
32- provisionerJobAgents : make ([]database.WorkspaceAgent , 0 ),
33- provisionerJobLogs : make ([]database.ProvisionerJobLog , 0 ),
34- provisionerJobResources : make ([]database.WorkspaceResource , 0 ),
35- provisionerJobs : make ([]database.ProvisionerJob , 0 ),
36- templateVersions : make ([]database.TemplateVersion , 0 ),
37- templates : make ([]database.Template , 0 ),
38- workspaceBuilds : make ([]database.WorkspaceBuild , 0 ),
39- workspaceApps : make ([]database.WorkspaceApp , 0 ),
40- workspaces : make ([]database.Workspace , 0 ),
41- }
42- }
21+ mutex : & sync.RWMutex {},
22+ data : & data {
23+ apiKeys : make ([]database.APIKey , 0 ),
24+ organizationMembers : make ([]database.OrganizationMember , 0 ),
25+ organizations : make ([]database.Organization , 0 ),
26+ users : make ([]database.User , 0 ),
27+
28+ auditLogs : make ([]database.AuditLog , 0 ),
29+ files : make ([]database.File , 0 ),
30+ gitSSHKey : make ([]database.GitSSHKey , 0 ),
31+ parameterSchemas : make ([]database.ParameterSchema , 0 ),
32+ parameterValues : make ([]database.ParameterValue , 0 ),
33+ provisionerDaemons : make ([]database.ProvisionerDaemon , 0 ),
34+ provisionerJobAgents : make ([]database.WorkspaceAgent , 0 ),
35+ provisionerJobLogs : make ([]database.ProvisionerJobLog , 0 ),
36+ provisionerJobResources : make ([]database.WorkspaceResource , 0 ),
37+ provisionerJobs : make ([]database.ProvisionerJob , 0 ),
38+ templateVersions : make ([]database.TemplateVersion , 0 ),
39+ templates : make ([]database.Template , 0 ),
40+ workspaceBuilds : make ([]database.WorkspaceBuild , 0 ),
41+ workspaceApps : make ([]database.WorkspaceApp , 0 ),
42+ workspaces : make ([]database.Workspace , 0 ),
43+ },
44+ }
45+ }
46+
47+ type rwMutex interface {
48+ Lock ()
49+ RLock ()
50+ Unlock ()
51+ RUnlock ()
52+ }
53+
54+ // inTxMutex is a no op, since inside a transaction we are already locked.
55+ type inTxMutex struct {}
56+
57+ func (inTxMutex ) Lock () {}
58+ func (inTxMutex ) RLock () {}
59+ func (inTxMutex ) Unlock () {}
60+ func (inTxMutex ) RUnlock () {}
4361
4462// fakeQuerier replicates database functionality to enable quick testing.
4563type fakeQuerier struct {
46- mutex sync.RWMutex
64+ mutex rwMutex
65+ * data
66+ }
4767
68+ type data struct {
4869 // Legacy tables
4970 apiKeys []database.APIKey
5071 organizations []database.Organization
@@ -73,7 +94,9 @@ type fakeQuerier struct {
7394
7495// InTx doesn't rollback data properly for in-memory yet.
7596func (q * fakeQuerier ) InTx (fn func (database.Store ) error ) error {
76- return fn (q )
97+ q .mutex .Lock ()
98+ defer q .mutex .Unlock ()
99+ return fn (& fakeQuerier {mutex : inTxMutex {}, data : q .data })
77100}
78101
79102func (q * fakeQuerier ) AcquireProvisionerJob (_ context.Context , arg database.AcquireProvisionerJobParams ) (database.ProvisionerJob , error ) {
0 commit comments