Skip to content

Commit

Permalink
feat: add xorm closeState and sqlite's randomName
Browse files Browse the repository at this point in the history
  • Loading branch information
Malyue committed Mar 14, 2024
1 parent aba9d7b commit 79a8fde
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 12 deletions.
3 changes: 2 additions & 1 deletion providers/mysqlxorm/sqlite3/examples/examples.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sqlite3-xorm:
dbSourceName: "${SQLITE3_DB_SOURCE_NAME:test.sqlite3}"
journalMode: "${SQLITE3_JOURNAL_MODE:memory}"
randomName: "true"

example:
example:
1 change: 1 addition & 0 deletions providers/mysqlxorm/sqlite3/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type provider struct {

func (p *provider) Init(ctx servicehub.Context) error {
fmt.Println(p.Sqlite3)
fmt.Println(p.Sqlite3.DB().DataSourceName())

// get journal_mode
results, _ := p.Sqlite3.DB().Query("PRAGMA journal_mode;")
Expand Down
44 changes: 42 additions & 2 deletions providers/mysqlxorm/sqlite3/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ package sqlite3
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync/atomic"

"github.com/google/uuid"
_ "github.com/mattn/go-sqlite3"
"xorm.io/xorm"
"xorm.io/xorm/names"
Expand All @@ -26,13 +31,22 @@ import (
)

type Sqlite3 struct {
db *xorm.Engine
db *xorm.Engine
closeState int32
}

func (s *Sqlite3) DB() *xorm.Engine {
return s.db
}

func (s *Sqlite3) DataSourceName() string {
return s.DB().DataSourceName()
}

func (s *Sqlite3) GetCloseState() bool {
return atomic.LoadInt32(&s.closeState) == 1

Check warning on line 47 in providers/mysqlxorm/sqlite3/interface.go

View check run for this annotation

Codecov / codecov/patch

providers/mysqlxorm/sqlite3/interface.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

func (s *Sqlite3) NewSession(ops ...mysqlxorm.SessionOption) *mysqlxorm.Session {
tx := &mysqlxorm.Session{}
for _, opt := range ops {
Expand All @@ -58,6 +72,10 @@ func NewSqlite3(dbSourceName string, opts ...OptionFunc) (*Sqlite3, error) {
opt(o)
}

if o.RandomName {
dbSourceName = randomName(dbSourceName)
}

engine, err := xorm.NewEngine("sqlite3", dbSourceName)
if err != nil {
return nil, err
Expand All @@ -74,7 +92,29 @@ func NewSqlite3(dbSourceName string, opts ...OptionFunc) (*Sqlite3, error) {

engine.SetMapper(names.GonicMapper{})

sqlite3Engine := &Sqlite3{db: engine}
sqlite3Engine := &Sqlite3{
db: engine,
}

return sqlite3Engine, nil
}

func (s *Sqlite3) Close() error {
if atomic.CompareAndSwapInt32(&s.closeState, 0, 1) {
err := s.DB().Close()
if err != nil {
return err

Check warning on line 106 in providers/mysqlxorm/sqlite3/interface.go

View check run for this annotation

Codecov / codecov/patch

providers/mysqlxorm/sqlite3/interface.go#L106

Added line #L106 was not covered by tests
}

err = os.Remove(s.DataSourceName())
return err
}
return nil
}

func randomName(path string) string {
dir, file := filepath.Split(path)
name := strings.TrimSuffix(file, filepath.Ext(file))
random := fmt.Sprintf("%s-%s%s", name, strings.ReplaceAll(uuid.New().String(), "-", ""), filepath.Ext(file))
return filepath.Join(dir, random)
}
42 changes: 37 additions & 5 deletions providers/mysqlxorm/sqlite3/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -26,7 +27,7 @@ import (
"github.com/erda-project/erda-infra/providers/mysqlxorm"
)

const dbSourceName = "test1.sqlite3"
const dbSourceName = "test1.db"

type Server struct {
mysql mysqlxorm.Interface
Expand Down Expand Up @@ -67,14 +68,13 @@ func (s *Server) TestTx(err error, ops ...mysqlxorm.SessionOption) error {

func TestNewSqlite3(t *testing.T) {
dbname := filepath.Join(os.TempDir(), dbSourceName)
defer func() {
os.Remove(dbname)
}()
engine, err := NewSqlite3(dbname)
if err != nil {
t.Fatalf("new sqlite3 err : %s", err)
}

defer engine.Close()

server := Server{
mysql: engine,
}
Expand Down Expand Up @@ -181,11 +181,43 @@ func TestJournalMode(t *testing.T) {
if err != nil {
t.Fatalf("new sqlite3 err : %s", err)
}
defer engine.Close()

// get journal in sqlite3
results, _ := engine.DB().Query("PRAGMA journal_mode;")
assert.Equal(t, string(w), string(results[0]["journal_mode"]))
os.Remove(dbname)
engine.Close()
}

}

func TestRandomName(t *testing.T) {
path := "/test/dir/sample.txt"
name1 := randomName(path)
name2 := randomName(path)

assert.True(t, strings.HasPrefix(name1, "/test/dir/sample-"), "Random name does not start with original name")

assert.Equal(t, filepath.Ext(name1), ".txt", "Random name does not have original extension")

assert.NotEqual(t, name1, name2, "Random name generator produced the same result twice")
}

func TestWithRandomName(t *testing.T) {
//dbname := filepath.Join(os.TempDir(), dbSourceName)
dbname := dbSourceName
engine, err := NewSqlite3(dbname, WithRandomName(false))
if err != nil {
panic(err)
}
defer engine.Close()
assert.Nil(t, err)
assert.Equal(t, dbname, engine.DataSourceName())
engine.Close()

engineRandom, err := NewSqlite3(dbname, WithRandomName(true))
assert.Nil(t, err)
assert.NotEqual(t, dbname, engineRandom, "Random name is not take effect")
assert.Equal(t, filepath.Ext(engineRandom.DataSourceName()), filepath.Ext(dbname), "Random names does not have original extension")
defer engineRandom.Close()
}
9 changes: 9 additions & 0 deletions providers/mysqlxorm/sqlite3/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package sqlite3

type Options struct {
JournalMode JournalMode
RandomName bool
}

type OptionFunc func(options *Options)
Expand All @@ -36,3 +37,11 @@ func WithJournalMode(mode JournalMode) OptionFunc {
o.JournalMode = mode
}
}

// WithRandomName use to set the uuid in the given filename
// such as `test.db => test-550e8400e29b41d4a716446655440000.db`
func WithRandomName(isRandomName bool) OptionFunc {
return func(o *Options) {
o.RandomName = isRandomName
}
}
8 changes: 7 additions & 1 deletion providers/mysqlxorm/sqlite3/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
type config struct {
DbSourceName string `file:"dbSourceName" env:"SQLITE3_DB_SOURCE_NAME" default:"test.sqlite3"`
JournalMode string `file:"journalMode" env:"SQLITE3_JOURNAL_MODE" default:""`
RandomName bool `file:"randomName" env:"SQLITE3_RANDOM_NAME" default:"false"`
}

type provider struct {
Expand All @@ -41,7 +42,12 @@ var _ servicehub.ProviderInitializer = (*provider)(nil)

// Init .
func (p *provider) Init(ctx servicehub.Context) error {
engine, err := xorm.NewEngine("sqlite3", p.Cfg.DbSourceName)
var path = p.Cfg.DbSourceName
if p.Cfg.RandomName {
path = randomName(p.Cfg.DbSourceName)

Check warning on line 47 in providers/mysqlxorm/sqlite3/provider.go

View check run for this annotation

Codecov / codecov/patch

providers/mysqlxorm/sqlite3/provider.go#L45-L47

Added lines #L45 - L47 were not covered by tests
}

engine, err := xorm.NewEngine("sqlite3", path)

Check warning on line 50 in providers/mysqlxorm/sqlite3/provider.go

View check run for this annotation

Codecov / codecov/patch

providers/mysqlxorm/sqlite3/provider.go#L50

Added line #L50 was not covered by tests
if err != nil {
return fmt.Errorf("failed to connect to sqlite3 engine,err : %s", err)
}
Expand Down
26 changes: 23 additions & 3 deletions providers/mysqlxorm/xorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"reflect"
"sync/atomic"
"time"

_ "github.com/go-sql-driver/mysql" // mysql client driver package
Expand All @@ -34,6 +35,9 @@ import (
type Interface interface {
DB() *xorm.Engine
NewSession(ops ...SessionOption) *Session
Close() error
GetCloseState() bool
DataSourceName() string
}

var (
Expand Down Expand Up @@ -77,9 +81,10 @@ func (c *config) url() string {

// provider .
type provider struct {
Cfg *config
Log logs.Logger
db *xorm.Engine
Cfg *config
Log logs.Logger
db *xorm.Engine
closeState int32
}

// Init .
Expand Down Expand Up @@ -128,6 +133,21 @@ func (p *provider) Provide(ctx servicehub.DependencyContext, args ...interface{}
return p
}

func (p *provider) DataSourceName() string {
return p.DB().DataSourceName()

Check warning on line 137 in providers/mysqlxorm/xorm.go

View check run for this annotation

Codecov / codecov/patch

providers/mysqlxorm/xorm.go#L136-L137

Added lines #L136 - L137 were not covered by tests
}

func (p *provider) Close() error {
if atomic.CompareAndSwapInt32(&p.closeState, 0, 1) {
return p.db.Close()

Check warning on line 142 in providers/mysqlxorm/xorm.go

View check run for this annotation

Codecov / codecov/patch

providers/mysqlxorm/xorm.go#L140-L142

Added lines #L140 - L142 were not covered by tests
}
return nil

Check warning on line 144 in providers/mysqlxorm/xorm.go

View check run for this annotation

Codecov / codecov/patch

providers/mysqlxorm/xorm.go#L144

Added line #L144 was not covered by tests
}

func (p *provider) GetCloseState() bool {
return atomic.LoadInt32(&p.closeState) == 1

Check warning on line 148 in providers/mysqlxorm/xorm.go

View check run for this annotation

Codecov / codecov/patch

providers/mysqlxorm/xorm.go#L147-L148

Added lines #L147 - L148 were not covered by tests
}

func init() {
servicehub.Register("mysql-xorm", &servicehub.Spec{
Services: []string{"mysql-xorm", "mysql-xorm-client"},
Expand Down

0 comments on commit 79a8fde

Please sign in to comment.