Skip to content

Commit c3ce0c1

Browse files
Fix to use gojq to run on mac (#77)
- Remove duplicated code for `if else softDelete` cases Fixes #64
1 parent af5b059 commit c3ce0c1

File tree

3 files changed

+24
-44
lines changed

3 files changed

+24
-44
lines changed

build/install_tools.sh

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ GOMRK_VERSION=${GOMRK_VERSION:=1.1.0}
1414
SHFMT_VERSION=${SHFMT_VERSION:=3.6.0}
1515
GOIMP_VERSION=${GOIMP_VERSION:=0.5.0}
1616
GOIMPORTS_REVISER_VERSION=${GOIMPORTS_REVISER_VERSION:=3.4.2}
17+
GOJQ_VERSION=${GOJQ_VERSION:=0.12.13}
1718
TYPOS_VERSION=${TYPOS_VERSION:=1.13.6}
1819
ADDLICENSE_VERSION=${ADDLICENSE_VERSION:=1.1.1}
1920
CODEOWNERS_VALIDATOR_VERSION=${CODEOWNERS_VALIDATOR_VERSION:=0.7.4}
@@ -34,6 +35,7 @@ go version | grep '1.19\|1.20\|1.21' || (
3435
exit 1
3536
)
3637
go_install "dlv version" ${GODLV_VERSION} github.com/go-delve/delve/cmd/dlv
38+
go_install "gojq --version" ${GOJQ_VERSION} github.com/itchyny/gojq/cmd/gojq
3739
go_install "golangci-lint version" ${GOLNT_VERSION} github.com/golangci/golangci-lint/cmd/golangci-lint
3840
go_install "gomarkdoc --version" ${GOMRK_VERSION} github.com/princjef/gomarkdoc/cmd/gomarkdoc
3941
go_install "shfmt -version" ${SHFMT_VERSION} mvdan.cc/sh/v3/cmd/shfmt

build/setup_local_postgres.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ elif [[ $(uname -s) == "Darwin" ]]; then
3333
brew install postgresql@14
3434
brew services restart postgresql@14
3535
sleep 5
36-
brew services info postgresql@14 --json | jq '.[0].running' | grep true
36+
brew services info postgresql@14 --json | gojq '.[0].running' | grep true
3737
echo "postgresql: Installation successful"
3838
fi
3939
dropdb tmp_db --if-exists

pkg/datastore/database.go

+21-43
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,14 @@ func (db *relationalDb) FindInTable(ctx context.Context, tableName string, recor
255255
}()
256256

257257
if softDelete {
258-
if err = tx.Unscoped().Table(tableName).Where(record).First(record).Error; err != nil {
259-
if errors.Is(err, gorm.ErrRecordNotFound) {
260-
return ErrRecordNotFound.Wrap(err).WithValue("record", fmt.Sprintf("%+v", record)).WithValue(DB_NAME, db.dbName)
261-
}
262-
db.logger.Debug(err)
263-
return ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
264-
}
265-
} else {
266-
if err = tx.Table(tableName).Where(record).First(record).Error; err != nil {
267-
if errors.Is(err, gorm.ErrRecordNotFound) {
268-
return ErrRecordNotFound.Wrap(err).WithValue("record", fmt.Sprintf("%+v", record)).WithValue(DB_NAME, db.dbName)
269-
}
270-
db.logger.Debug(err)
271-
return ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
258+
tx = tx.Unscoped()
259+
}
260+
if err = tx.Table(tableName).Where(record).First(record).Error; err != nil {
261+
if errors.Is(err, gorm.ErrRecordNotFound) {
262+
return ErrRecordNotFound.Wrap(err).WithValue("record", fmt.Sprintf("%+v", record)).WithValue(DB_NAME, db.dbName)
272263
}
264+
db.logger.Debug(err)
265+
return ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
273266
}
274267
return db.commitWhenTxNotInsideCtx(ctx, tx)
275268
}
@@ -320,15 +313,11 @@ func (db *relationalDb) FindWithFilterInTable(ctx context.Context, tableName str
320313
}
321314

322315
if softDelete {
323-
if err = tx.Unscoped().Table(tableName).Where(record).Error; err != nil {
324-
db.logger.Debug(err)
325-
return ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
326-
}
327-
} else {
328-
if err = tx.Table(tableName).Where(record).Error; err != nil {
329-
db.logger.Debug(err)
330-
return ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
331-
}
316+
tx = tx.Unscoped()
317+
}
318+
if err = tx.Table(tableName).Where(record).Error; err != nil {
319+
db.logger.Debug(err)
320+
return ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
332321
}
333322

334323
if pagination != nil {
@@ -338,16 +327,9 @@ func (db *relationalDb) FindWithFilterInTable(ctx context.Context, tableName str
338327
}
339328
}
340329

341-
if softDelete {
342-
if err = tx.Unscoped().Find(records).Error; err != nil {
343-
db.logger.Debug(err)
344-
return ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
345-
}
346-
} else {
347-
if err = tx.Find(records).Error; err != nil {
348-
db.logger.Debug(err)
349-
return ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
350-
}
330+
if err = tx.Find(records).Error; err != nil {
331+
db.logger.Debug(err)
332+
return ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
351333
}
352334

353335
return db.commitWhenTxNotInsideCtx(ctx, tx)
@@ -426,16 +408,12 @@ func (db *relationalDb) delete(ctx context.Context, tableName string, record Rec
426408
}
427409
}()
428410

429-
if softDelete {
430-
if err = tx.Delete(record).Error; err != nil {
431-
db.logger.Debug(err)
432-
return 0, ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
433-
}
434-
} else {
435-
if err = tx.Unscoped().Delete(record).Error; err != nil {
436-
db.logger.Debug(err)
437-
return 0, ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
438-
}
411+
if !softDelete {
412+
tx = tx.Unscoped()
413+
}
414+
if err = tx.Delete(record).Error; err != nil {
415+
db.logger.Debug(err)
416+
return 0, ErrExecutingSqlStmt.Wrap(err).WithValue(DB_NAME, db.dbName)
439417
}
440418
err = db.commitWhenTxNotInsideCtx(ctx, tx)
441419
if err != nil {

0 commit comments

Comments
 (0)