Removing transactions from sqlite backend (#2361)

* Removing transactions from sqlite backend

* Skipping tests in sqlite because the lack of transactions

* Generating the mocks

* Fixing golangci-lint
This commit is contained in:
Jesús Espino 2022-02-22 22:30:47 +01:00 committed by GitHub
parent 8d94422d5f
commit 1d5583ac7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 50 additions and 0 deletions

View file

@ -82,6 +82,7 @@ type storeMetadata struct {
var blacklistedStoreMethodNames = map[string]bool{
"Shutdown": true,
"IsErrNotFound": true,
"DBType": true,
}
func extractMethodMetadata(method *ast.Field, src []byte) methodData {

View file

@ -25,6 +25,9 @@ import (
{{range $index, $element := .Methods}}
func (s *SQLStore) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
{{- if $element.WithTransaction}}
if s.dbType == sqliteDBType {
return s.{{$index | renameStoreMethod}}(s.db, {{$element.Params | joinParams}})
}
tx, txErr := s.db.BeginTx(context.Background(), nil)
if txErr != nil {
return {{ genErrorResultsVars $element.Results "txErr"}}

View file

@ -93,6 +93,20 @@ func (mr *MockStoreMockRecorder) CreateUser(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateUser", reflect.TypeOf((*MockStore)(nil).CreateUser), arg0)
}
// DBType mocks base method.
func (m *MockStore) DBType() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "DBType")
ret0, _ := ret[0].(string)
return ret0
}
// DBType indicates an expected call of DBType.
func (mr *MockStoreMockRecorder) DBType() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DBType", reflect.TypeOf((*MockStore)(nil).DBType))
}
// DeleteBlock mocks base method.
func (m *MockStore) DeleteBlock(arg0 store.Container, arg1, arg2 string) error {
m.ctrl.T.Helper()

View file

@ -444,6 +444,11 @@ func (s *SQLStore) patchBlocks(db sq.BaseRunner, c store.Container, blockPatches
}
func (s *SQLStore) insertBlocks(db sq.BaseRunner, c store.Container, blocks []model.Block, userID string) error {
for _, block := range blocks {
if block.RootID == "" {
return RootIDNilError{}
}
}
for i := range blocks {
err := s.insertBlock(db, c, &blocks[i], userID)
if err != nil {

View file

@ -43,6 +43,9 @@ func (s *SQLStore) CreateUser(user *model.User) error {
}
func (s *SQLStore) DeleteBlock(c store.Container, blockID string, modifiedBy string) error {
if s.dbType == sqliteDBType {
return s.deleteBlock(s.db, c, blockID, modifiedBy)
}
tx, txErr := s.db.BeginTx(context.Background(), nil)
if txErr != nil {
return txErr
@ -254,6 +257,9 @@ func (s *SQLStore) HasWorkspaceAccess(userID string, workspaceID string) (bool,
}
func (s *SQLStore) InsertBlock(c store.Container, block *model.Block, userID string) error {
if s.dbType == sqliteDBType {
return s.insertBlock(s.db, c, block, userID)
}
tx, txErr := s.db.BeginTx(context.Background(), nil)
if txErr != nil {
return txErr
@ -275,6 +281,9 @@ func (s *SQLStore) InsertBlock(c store.Container, block *model.Block, userID str
}
func (s *SQLStore) InsertBlocks(c store.Container, blocks []model.Block, userID string) error {
if s.dbType == sqliteDBType {
return s.insertBlocks(s.db, c, blocks, userID)
}
tx, txErr := s.db.BeginTx(context.Background(), nil)
if txErr != nil {
return txErr
@ -296,6 +305,9 @@ func (s *SQLStore) InsertBlocks(c store.Container, blocks []model.Block, userID
}
func (s *SQLStore) PatchBlock(c store.Container, blockID string, blockPatch *model.BlockPatch, userID string) error {
if s.dbType == sqliteDBType {
return s.patchBlock(s.db, c, blockID, blockPatch, userID)
}
tx, txErr := s.db.BeginTx(context.Background(), nil)
if txErr != nil {
return txErr
@ -317,6 +329,9 @@ func (s *SQLStore) PatchBlock(c store.Container, blockID string, blockPatch *mod
}
func (s *SQLStore) PatchBlocks(c store.Container, blockPatches *model.BlockPatchBatch, userID string) error {
if s.dbType == sqliteDBType {
return s.patchBlocks(s.db, c, blockPatches, userID)
}
tx, txErr := s.db.BeginTx(context.Background(), nil)
if txErr != nil {
return txErr

View file

@ -70,6 +70,11 @@ func (s *SQLStore) DBHandle() *sql.DB {
return s.db
}
// DBType returns the DB driver used for the store.
func (s *SQLStore) DBType() string {
return s.dbType
}
func (s *SQLStore) getQueryBuilder(db sq.BaseRunner) sq.StatementBuilderType {
builder := sq.StatementBuilder
if s.dbType == postgresDBType || s.dbType == sqliteDBType {

View file

@ -95,6 +95,8 @@ type Store interface {
RemoveDefaultTemplates(blocks []model.Block) error
GetDefaultTemplateBlocks() ([]model.Block, error)
DBType() string
IsErrNotFound(err error) bool
}

View file

@ -226,6 +226,7 @@ func testInsertBlocks(t *testing.T, store store.Store, container store.Container
newBlocks := []model.Block{validBlock, invalidBlock}
time.Sleep(1 * time.Millisecond)
err := store.InsertBlocks(container, newBlocks, "user-id-1")
require.Error(t, err)
@ -400,6 +401,10 @@ func testPatchBlocks(t *testing.T, store store.Store, container store.Container)
})
t.Run("invalid block id, nothing updated existing blocks", func(t *testing.T) {
if store.DBType() == "sqlite3" {
t.Skip("No transactions support int sqlite")
}
title := "Another Title"
blockPatch := model.BlockPatch{
Title: &title,