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

Make DB connection use PingContext #24

Merged
merged 7 commits into from
Sep 16, 2024
Merged
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
9 changes: 7 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocommerce

import (
"context"
"database/sql"
"os"

Expand All @@ -13,7 +14,7 @@ var defaultSockets = []string{
}

// NB copy StoreConfig, as we may modify it
func ConnectDB(cfg DBConfig) (*sql.DB, error) {
func ConnectDB(ctx context.Context, cfg DBConfig) (*sql.DB, error) {
// Mimic libmysql behavior, where "localhost" is overridden with
// system specific unix socket.
if cfg.Host == "localhost" || cfg.Host == "" {
Expand All @@ -29,7 +30,11 @@ func ConnectDB(cfg DBConfig) (*sql.DB, error) {
if err != nil {
return nil, err
}
err = db.Ping()

if ctx == nil {
ctx = context.Background()
}
err = db.PingContext(ctx)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions magento1.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocommerce

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -62,15 +63,15 @@ func (m1 *Magento1) ParseConfig(cfgPath string) (*StoreConfig, error) {
}, nil
}

func (m1 *Magento1) BaseURLs(docroot string) ([]string, error) {
func (m1 *Magento1) BaseURLs(ctx context.Context, docroot string) ([]string, error) {
cfgPath := filepath.Join(docroot, m1.ConfigPath())

cfg, err := m1.ParseConfig(cfgPath)
if err != nil {
return nil, err
}

db, err := ConnectDB(*cfg.DB)
db, err := ConnectDB(ctx, *cfg.DB)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion magento1_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package gocommerce

import (
"testing"
"context"

"github.com/stretchr/testify/assert"
)

func TestGetMagento1BaseURLsFromDatabase(t *testing.T) {
baseURLs, err := m1store.BaseURLs(fixtureBase + "magento1_integration")
baseURLs, err := m1store.BaseURLs(context.TODO(), fixtureBase+"magento1_integration")
assert.Nil(t, err)
assert.ElementsMatch(t, []string{"https://app.magento1.test/", "https://second.magento1.test/"}, baseURLs)
}
9 changes: 5 additions & 4 deletions magento2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocommerce

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -69,10 +70,10 @@ func (m2 *Magento2) ParseConfig(cfgPath string) (*StoreConfig, error) {
}, nil
}

func (m2 *Magento2) BaseURLs(docroot string) ([]string, error) {
func (m2 *Magento2) BaseURLs(ctx context.Context, docroot string) ([]string, error) {
cfgPath := filepath.Join(docroot, m2.ConfigPath())
urls := []string{}
if ud, err := m2.getBaseURLsFromDatabase(cfgPath); err == nil {
if ud, err := m2.getBaseURLsFromDatabase(ctx, cfgPath); err == nil {
urls = append(urls, ud...)
}
if uc, err := m2.getBaseURLsFromConfig(cfgPath); err == nil {
Expand Down Expand Up @@ -120,13 +121,13 @@ func (m2 *Magento2) getBaseURLsFromConfig(cfgPath string) ([]string, error) {
return nil, errors.New("base url(s) not found in config")
}

func (m2 *Magento2) getBaseURLsFromDatabase(cfgPath string) ([]string, error) {
func (m2 *Magento2) getBaseURLsFromDatabase(ctx context.Context, cfgPath string) ([]string, error) {
cfg, err := m2.ParseConfig(cfgPath)
if err != nil {
return nil, err
}

db, err := ConnectDB(*cfg.DB)
db, err := ConnectDB(ctx, *cfg.DB)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion magento2_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package gocommerce

import (
"testing"
"context"

"github.com/stretchr/testify/assert"
)

func TestGetMagento2BaseURLsFromDatabase(t *testing.T) {
baseURLs, err := m2store.BaseURLs(fixtureBase + "magento2_integration")
baseURLs, err := m2store.BaseURLs(context.TODO(), fixtureBase+"magento2_integration")
assert.Nil(t, err)
assert.ElementsMatch(t, []string{"https://sansec.io/", "https://api.sansec.io/"}, baseURLs)
}
5 changes: 3 additions & 2 deletions magento2_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocommerce

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -62,8 +63,8 @@ func TestGetMagentoVersionWithoutSystemPackages(t *testing.T) {
assert.Equal(t, "2.4.2-p2", version)
}

func TestGetMagentoBaseURLsFromConfig(t *testing.T) {
baseURLs, err := m2store.BaseURLs(fixtureBase + "magento2")
func TestGetMagentoBaseURLsFromConfigNilCtx(t *testing.T) {
baseURLs, err := m2store.BaseURLs(context.TODO(), fixtureBase+"magento2")
assert.Nil(t, err)
assert.ElementsMatch(t, []string{"https://sansec.io/", "https://api.sansec.io/"}, baseURLs)
}
3 changes: 2 additions & 1 deletion opencart4.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocommerce

import (
"context"
"errors"
"os"
"path/filepath"
Expand Down Expand Up @@ -57,7 +58,7 @@ func (oc4 *OpenCart4) ParseConfig(cfgPath string) (*StoreConfig, error) {
}, nil
}

func (oc4 *OpenCart4) BaseURLs(docroot string) ([]string, error) {
func (oc4 *OpenCart4) BaseURLs(ctx context.Context, docroot string) ([]string, error) {
cfgPath := filepath.Join(docroot, "config.php")
cfg, err := os.ReadFile(cfgPath)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion opencart4_config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocommerce

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -14,7 +15,7 @@ func TestOpenCartConfig(t *testing.T) {

func TestOpenCartURL(t *testing.T) {
oc4 := OpenCart4{}
urls, err := oc4.BaseURLs(fixtureBase + "opencart4")
urls, err := oc4.BaseURLs(context.TODO(), fixtureBase+"opencart4")
assert.NoError(t, err)
assert.NotEmpty(t, urls)
assert.Equal(t, "http://sansec.io/", urls[0])
Expand Down
3 changes: 2 additions & 1 deletion phpcfg/phpcfg_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package phpcfg

import (
"github.com/google/go-cmp/cmp"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestParseShortArray(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions platform.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocommerce

import (
"context"
"errors"
"fmt"
"regexp"
Expand Down Expand Up @@ -38,7 +39,7 @@ type (
Name() string
ParseConfig(cfgPath string) (*StoreConfig, error)
Version(docroot string) (string, error)
BaseURLs(docroot string) ([]string, error)
BaseURLs(ctx context.Context, docroot string) ([]string, error)
ConfigPath() string
UniquePath() string
}
Expand Down Expand Up @@ -121,7 +122,7 @@ func (b *basePlatform) ParseConfig(cfgPath string) (*StoreConfig, error) {
return nil, errors.New("not implemented")
}

func (b *basePlatform) BaseURLs(docroot string) ([]string, error) {
func (b *basePlatform) BaseURLs(ctx context.Context, docroot string) ([]string, error) {
return nil, errors.New("not implemented")
}

Expand Down
10 changes: 5 additions & 5 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ var commonDocRoots = []string{
"/var/www/*",
"/var/www/*/public_html",
"/var/www/vhosts/*/htdocs",
"/vhosts/*/httpdocs", // plesk
"$HOME/public_html/..", // nexcess
"$HOME/public/..", // hypernode
"$HOME/public", // hypernode, nimbus hosting
"$HOME/??*.*", // generic domain pattern
"/vhosts/*/httpdocs", // plesk
"$HOME/public_html/..", // nexcess
"$HOME/public/..", // hypernode
"$HOME/public", // hypernode, nimbus hosting
"$HOME/??*.*", // generic domain pattern
}

func (s *Store) ConfigPath() string {
Expand Down
2 changes: 1 addition & 1 deletion tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

const fixtureBase = "fixture/"

func dbConfigFromSource(t *testing.T, src string, pl PlatformInterface) *DBConfig {
func dbConfigFromSource(_ *testing.T, src string, pl PlatformInterface) *DBConfig {
cfg, e := pl.ParseConfig(src)
if e != nil {
fmt.Println(e)
Expand Down
Loading