Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[utils-go][sqldb] Add new sqldb package #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
646 changes: 638 additions & 8 deletions bazel/go/deps.bzl

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions fastlinks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
"//fastlinks/services",
"//fastlinks/stores/postgresql",
"//utils-go/logging",
"//utils-go/sqldb",
"@com_github_labstack_echo_v4//:echo",
"@com_github_labstack_echo_v4//middleware",
"@org_uber_go_zap//:zap",
Expand Down
8 changes: 6 additions & 2 deletions fastlinks/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/TerrenceHo/monorepo/fastlinks/services"
"github.com/TerrenceHo/monorepo/fastlinks/stores/postgresql"
"github.com/TerrenceHo/monorepo/utils-go/logging"
"github.com/TerrenceHo/monorepo/utils-go/sqldb"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"go.uber.org/zap"
Expand All @@ -26,6 +27,7 @@ type Config struct {
}

type DBConfig struct {
Engine string
User string
Password string
DBName string
Expand All @@ -44,7 +46,8 @@ func Start(conf Config) {
logging.SetGlobalLogger(logger)

// Instantiate database connections
db, err := postgresql.NewConnection(
db, err := sqldb.NewConnection(
conf.DB.Engine,
conf.DB.User,
conf.DB.Password,
conf.DB.DBName,
Expand All @@ -59,9 +62,10 @@ func Start(conf Config) {
zap.Error(err),
)
}
sql_db := sqldb.NewSQLDB(db)

// create stores
routesStore := postgresql.NewRoutesStore(db)
routesStore := postgresql.NewRoutesStore(sql_db)

// create services
healthService := services.NewHealthService(routesStore)
Expand Down
1 change: 1 addition & 0 deletions fastlinks/cmd/fastlinks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func rootCmd(run func(cmd *cobra.Command, args []string)) *cobra.Command {
flags.String("host", "localhost", "hostserver on this hostname")
flags.StringP("port", "p", "12345", "host server on localhost:<port>")

flags.String("db.engine", "postgres", "database engine")
flags.String("db.user", "fastlinks", "database user")
flags.String("db.password", "password", "database password")
flags.String("db.dbname", "fastlinks", "database name")
Expand Down
3 changes: 3 additions & 0 deletions fastlinks/cmd/fastlinks/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestConfig(t *testing.T) {
Host: "localhost",
Port: "12345",
DB: fastlinks.DBConfig{
Engine: "postgres",
User: "fastlinks",
Password: "password",
DBName: "fastlinks",
Expand All @@ -57,6 +58,7 @@ func TestConfig(t *testing.T) {
"--hidebanner",
"--host=google.com",
"--port=5555",
"--db.engine=mysql",
"--db.user=user",
"--db.password=newpassword",
"--db.dbname=newdb",
Expand All @@ -70,6 +72,7 @@ func TestConfig(t *testing.T) {
Host: "google.com",
Port: "5555",
DB: fastlinks.DBConfig{
Engine: "mysql",
User: "user",
Password: "newpassword",
DBName: "newdb",
Expand Down
15 changes: 2 additions & 13 deletions fastlinks/stores/postgresql/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//bazel/go:default.bzl", "go_library", "go_test")
load("//bazel/go:default.bzl", "go_library")

go_library(
name = "postgresql",
Expand All @@ -8,16 +8,5 @@ go_library(
],
importpath = "github.com/TerrenceHo/monorepo/fastlinks/stores/postgresql",
visibility = ["//visibility:public"],
deps = [
"//utils-go/stackerrors",
"@com_github_jmoiron_sqlx//:sqlx",
"@com_github_lib_pq//:pq",
],
)

go_test(
name = "postgresql_test",
srcs = ["stores_test.go"],
embed = [":postgresql"],
deps = ["@com_github_stretchr_testify//assert"],
deps = ["//utils-go/sqldb"],
)
8 changes: 5 additions & 3 deletions fastlinks/stores/postgresql/routes.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package postgresql

import "github.com/jmoiron/sqlx"
import (
"github.com/TerrenceHo/monorepo/utils-go/sqldb"
)

type RoutesStore struct {
db *sqlx.DB
db *sqldb.DB
}

func NewRoutesStore(db *sqlx.DB) *RoutesStore {
func NewRoutesStore(db *sqldb.DB) *RoutesStore {
return &RoutesStore{
db: db,
}
Expand Down
27 changes: 0 additions & 27 deletions fastlinks/stores/postgresql/stores.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
package postgresql

import (
"fmt"

"github.com/TerrenceHo/monorepo/utils-go/stackerrors"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)

type Store interface{}

func NewConnection(
user, password, dbname, port, host, sslmode string,
) (*sqlx.DB, error) {
dbConnection := connectPostgresql(user, password, dbname, port, host, sslmode)

db, err := sqlx.Connect("postgres", dbConnection)
if err != nil {
return nil, stackerrors.Wrap(err, "postgresql connection failed to connect")
}

return db, nil
}

func connectPostgresql(user, password, dbname, port, host, sslmode string) string {
dbConnection := fmt.Sprintf("user=%s password=%s dbname=%s port=%s host=%s sslmode=%s",
user, password, dbname, port, host, sslmode)
return dbConnection
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/golang-migrate/migrate/v4 v4.14.1 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/labstack/echo/v4 v4.5.0 // indirect
Expand Down
Loading