Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
1. Updating Go version
Browse files Browse the repository at this point in the history
2. Updating dependency versions
3. Fixed lint comments
4. Refactoring GitHub Actions
  • Loading branch information
BorzdeG committed Oct 18, 2023
1 parent a999fa8 commit 10dc19c
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 204 deletions.
38 changes: 0 additions & 38 deletions .github/workflows/test-matrix.yml

This file was deleted.

40 changes: 27 additions & 13 deletions .github/workflows/test-asdf.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: test Go using asdf
name: test

on:
push:
pull_request:

jobs:
test-mod:
test-gomod:
runs-on: ubuntu-latest

steps:
Expand All @@ -22,17 +22,7 @@ jobs:
- name: Install asdf & tools
uses: asdf-vm/actions/install@v3

- run: go get -u golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest && go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
- run: go vet -vettool=$(which shadow) ./...

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
skip-pkg-cache: true

- run: go get github.com/onsi/ginkgo/v2@latest && go install github.com/onsi/ginkgo/v2/ginkgo
- run: $(go env GOPATH)/bin/ginkgo -r -race --cover --coverprofile=.coverage.out --junit-report=junit-report.xml ./...
- run: make go-all-tests

- name: Test Summary
uses: test-summary/action@v2
Expand All @@ -45,3 +35,27 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./.coverage.out

test-matrix:
runs-on: ubuntu-latest

strategy:
matrix:
go: ["1.20.x", "1.21.x"]

steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
check-latest: true

- run: make go-all-tests

- name: Test Summary
uses: test-summary/action@v2
with:
paths: "junit-report.xml"
if: always()
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
golang 1.20.8
golang 1.20.10
36 changes: 22 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
go-dependencies:
# https://asdf-vm.com/
asdf install golang
#
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint@latest && go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go get -u github.com/onsi/ginkgo/v2@latest && go install github.com/onsi/ginkgo/v2/ginkgo@latest
asdf install golang || :

# https://github.com/securego/gosec
go get -u github.com/securego/gosec/v2/cmd/gosec@latest && go install github.com/securego/gosec/v2/cmd/gosec@latest
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
#
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/nunnatsa/ginkgolinter/cmd/ginkgolinter@latest
#
go get -u golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest && go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
go install github.com/onsi/ginkgo/v2/ginkgo@latest
#
go install github.com/vektra/mockery/v2@latest
#
asdf reshim golang || :
#
go get -u -t -v ./... || :
asdf reshim golang

go-generate:
go-generate: go-dependencies
mockery
go generate ./...

go-test:
golangci-lint run ./...
go vet -vettool=$(which shadow) ./...
go-lint: go-dependencies
golangci-lint run
ginkgolinter ./...
go vet -vettool=$$(go env GOPATH)/bin/shadow ./...

go-test: go-lint
gosec ./...
ginkgo -r -race --cover --coverprofile=.coverage-details.out ./...
go tool cover -func=.coverage-details.out -o=.coverage.out
ginkgo -r -race --cover --coverprofile=.coverage-ginkgo.out --junit-report=junit-report.xml ./...
go tool cover -func=.coverage-ginkgo.out -o=.coverage.out
cat .coverage.out

go-all: go-dependencies go-generate go-test
go-all-tests: go-dependencies go-generate go-lint go-test

go-all: go-all-tests
go mod tidy || :
84 changes: 84 additions & 0 deletions db/db-migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package db

import (
"database/sql"
"fmt"
"os"

"github.com/juju/zaputil/zapctx"
"github.com/pkg/errors"
"github.com/pressly/goose/v3"
)

func (receiver *DB) migrationDB() error {
logger := zapctx.Default.Sugar()

migrationDir, err := receiver.prepareMigrationDB()
if err != nil {
e := errors.Wrap(err, "database migration error")
logger.Error(e)

return e
} else if len(migrationDir) == 0 {
logger.Warn("Source for database migration not found - migration skipped")

return nil
}

logger.Infof("Directory with database migrations: %s", migrationDir)

// We connect to the database with the rights to edit the database schema
sqlDb, err := sql.Open(receiver.Config.Dialect, receiver.getDSN(*receiver.connSchemaCredential))
if err != nil {
logger.Error(fmt.Errorf("failed to connect to database to run migrations: %w", err))

return ErrDbMigration
}

defer func() {
if e := sqlDb.Close(); e != nil {
logger.Error(fmt.Errorf("failed to disconnect from database: %w", e))
}
}()

if e := goose.Up(sqlDb, migrationDir, goose.WithAllowMissing()); e != nil {
logger.Error(e)

return ErrDbMigration
}

logger.Info("Database migration completed")

return nil
}

func (receiver *DB) prepareMigrationDB() (string, error) {
if err := goose.SetDialect(receiver.Config.Dialect); err != nil {
return "", err //nolint:wrapcheck
}

logger := zapctx.Default.Sugar()

if receiver.dbEmbedMigrations != nil {
logger.Debug("Configure Goose using embedded FS for migrations")

goose.SetBaseFS(receiver.dbEmbedMigrations)

return "migrations", nil
}

migrationDir := receiver.Config.GooseMigrationDir
if len(migrationDir) != 0 {
// Checking the availability of the directory with database migrations
_, err := os.Stat(migrationDir)
if errors.Is(err, os.ErrNotExist) {
logger.Error(fmt.Errorf("directory '%s' with database migrations not found: %w", migrationDir, err))

return "", err //nolint:wrapcheck
}

logger.Infof("Directory with database migrations: %s", migrationDir)
}

return migrationDir, nil
}
75 changes: 0 additions & 75 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package db

import (
"context"
"database/sql"
"embed"
"errors"
"fmt"
"os"
"time"

"github.com/caarlos0/env/v9"
coreUtils "github.com/itbasis/go-core-utils/v2"
"github.com/juju/zaputil/zapctx"
"github.com/pressly/goose/v3"
"go.uber.org/zap/zapcore"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand Down Expand Up @@ -169,75 +166,3 @@ func (receiver *DB) connectDB() error {

return nil
}

func (receiver *DB) migrationDB() error {
logger := zapctx.Default.Sugar()

migrationDir, err := receiver.prepareMigrationDB()
if err != nil {
logger.Error(fmt.Errorf("database migration error: %w", err))

return err
} else if len(migrationDir) == 0 {
logger.Warn("Source for database migration not found - migration skipped")

return nil
}

logger.Infof("Directory with database migrations: %s", migrationDir)

// We connect to the database with the rights to edit the database schema
sqlDb, err := sql.Open(receiver.Config.Dialect, receiver.getDSN(*receiver.connSchemaCredential))
if err != nil {
logger.Error(fmt.Errorf("failed to connect to database to run migrations: %w", err))

return ErrDbMigration
}

defer func() {
if err := sqlDb.Close(); err != nil {
logger.Error(fmt.Errorf("failed to disconnect from database: %w", err))
}
}()

if err = goose.Up(sqlDb, migrationDir, goose.WithAllowMissing()); err != nil {
logger.Error(err)

return ErrDbMigration
}

logger.Info("Database migration completed")

return nil
}

func (receiver *DB) prepareMigrationDB() (string, error) {
if err := goose.SetDialect(receiver.Config.Dialect); err != nil {
return "", err //nolint:wrapcheck
}

logger := zapctx.Default.Sugar()

if receiver.dbEmbedMigrations != nil {
logger.Debug("Configure Goose using embedded FS for migrations")

goose.SetBaseFS(receiver.dbEmbedMigrations)

return "migrations", nil
}

migrationDir := receiver.Config.GooseMigrationDir
if len(migrationDir) != 0 {
// Checking the availability of the directory with database migrations
_, err := os.Stat(migrationDir)
if errors.Is(err, os.ErrNotExist) {
logger.Error(fmt.Errorf("directory '%s' with database migrations not found: %w", migrationDir, err))

return "", err //nolint:wrapcheck
}

logger.Infof("Directory with database migrations: %s", migrationDir)
}

return migrationDir, nil
}
Loading

0 comments on commit 10dc19c

Please sign in to comment.