@@ -18,33 +18,54 @@ import (
18
18
// New returns an in-memory fake of the database.
19
19
func New () database.Store {
20
20
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 () {}
43
61
44
62
// fakeQuerier replicates database functionality to enable quick testing.
45
63
type fakeQuerier struct {
46
- mutex sync.RWMutex
64
+ mutex rwMutex
65
+ * data
66
+ }
47
67
68
+ type data struct {
48
69
// Legacy tables
49
70
apiKeys []database.APIKey
50
71
organizations []database.Organization
@@ -73,7 +94,9 @@ type fakeQuerier struct {
73
94
74
95
// InTx doesn't rollback data properly for in-memory yet.
75
96
func (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 })
77
100
}
78
101
79
102
func (q * fakeQuerier ) AcquireProvisionerJob (_ context.Context , arg database.AcquireProvisionerJobParams ) (database.ProvisionerJob , error ) {
0 commit comments