Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/gobuffalo/pop into d…
Browse files Browse the repository at this point in the history
…evelopment
  • Loading branch information
paganotoni committed May 24, 2022
2 parents 57fe58c + 012ea29 commit 65fd526
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 23 deletions.
20 changes: 13 additions & 7 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pop

import (
"context"
"database/sql"
"errors"
"fmt"
"sync/atomic"
Expand Down Expand Up @@ -185,21 +186,26 @@ func (c *Connection) Rollback(fn func(tx *Connection)) error {

// NewTransaction starts a new transaction on the connection
func (c *Connection) NewTransaction() (*Connection, error) {
return c.NewTransactionContextOptions(c.Context(), nil)
}

// NewTransactionContext starts a new transaction on the connection using the provided context
func (c *Connection) NewTransactionContext(ctx context.Context) (*Connection, error) {
return c.NewTransactionContextOptions(ctx, nil)
}

// NewTransactionContextOptions starts a new transaction on the connection using the provided context and transaction options
func (c *Connection) NewTransactionContextOptions(ctx context.Context, options *sql.TxOptions) (*Connection, error) {
var cn *Connection
if c.TX == nil {
tx, err := c.Store.Transaction()
tx, err := c.Store.TransactionContextOptions(ctx, options)
if err != nil {
return cn, fmt.Errorf("couldn't start a new transaction: %w", err)
}
var store store = tx

// Rewrap the store if it was a context store
if cs, ok := c.Store.(contextStore); ok {
store = contextStore{store: store, ctx: cs.ctx}
}
cn = &Connection{
ID: randx.String(30),
Store: store,
Store: contextStore{store: tx, ctx: ctx},
Dialect: c.Dialect,
TX: tx,
}
Expand Down
45 changes: 45 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//go:build sqlite
// +build sqlite

package pop

import (
"context"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -52,3 +54,46 @@ func Test_Connection_Open_BadDriver(t *testing.T) {
err = c.Open()
r.Error(err)
}

func Test_Connection_Transaction(t *testing.T) {
r := require.New(t)
ctx := context.WithValue(context.Background(), "test", "test")

c, err := NewConnection(&ConnectionDetails{
URL: "sqlite://file::memory:?_fk=true",
})
r.NoError(err)
r.NoError(c.Open())
c = c.WithContext(ctx)

t.Run("func=NewTransaction", func(t *testing.T) {
r := require.New(t)
tx, err := c.NewTransaction()
r.NoError(err)

// has transaction and context
r.NotNil(tx.TX)
r.Nil(c.TX)
r.Equal(ctx, tx.Context())

// does not start a new transaction
ntx, err := tx.NewTransaction()
r.Equal(tx, ntx)

r.NoError(tx.TX.Rollback())
})

t.Run("func=NewTransactionContext", func(t *testing.T) {
r := require.New(t)
nctx := context.WithValue(ctx, "nested", "test")
tx, err := c.NewTransactionContext(nctx)
r.NoError(err)

// has transaction and context
r.NotNil(tx.TX)
r.Nil(c.TX)
r.Equal(nctx, tx.Context())

r.NoError(tx.TX.Rollback())
})
}
24 changes: 17 additions & 7 deletions dialect_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,32 @@ func (m *sqlite) locker(l *sync.Mutex, fn func() error) error {
}

func (m *sqlite) CreateDB() error {
_, err := os.Stat(m.ConnectionDetails.Database)
durl := m.ConnectionDetails.Database

// Checking whether the url specifies in-memory mode
// as specified in https://github.com/mattn/go-sqlite3#faq
if strings.Contains(durl, ":memory:") || strings.Contains(durl, "mode=memory") {
log(logging.Info, "in memory db selected, no database file created.")

return nil
}

_, err := os.Stat(durl)
if err == nil {
return fmt.Errorf("could not create SQLite database '%s'; database exists", m.ConnectionDetails.Database)
return fmt.Errorf("could not create SQLite database '%s'; database exists", durl)
}
dir := filepath.Dir(m.ConnectionDetails.Database)
dir := filepath.Dir(durl)
err = os.MkdirAll(dir, 0766)
if err != nil {
return fmt.Errorf("could not create SQLite database '%s': %w", m.ConnectionDetails.Database, err)
return fmt.Errorf("could not create SQLite database '%s': %w", durl, err)
}
f, err := os.Create(m.ConnectionDetails.Database)
f, err := os.Create(durl)
if err != nil {
return fmt.Errorf("could not create SQLite database '%s': %w", m.ConnectionDetails.Database, err)
return fmt.Errorf("could not create SQLite database '%s': %w", durl, err)
}
_ = f.Close()

log(logging.Info, "created database '%s'", m.ConnectionDetails.Database)
log(logging.Info, "created database '%s'", durl)
return nil
}

Expand Down
52 changes: 43 additions & 9 deletions dialect_sqlite_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build sqlite
// +build sqlite

package pop
Expand Down Expand Up @@ -144,18 +145,51 @@ func Test_ConnectionDetails_FinalizeOSPath(t *testing.T) {

func TestSqlite_CreateDB(t *testing.T) {
r := require.New(t)
dir := t.TempDir()
p := filepath.Join(dir, "testdb.sqlite")
cd := &ConnectionDetails{
Dialect: "sqlite",
Database: p,
}

cd := &ConnectionDetails{Dialect: "sqlite"}
dialect, err := newSQLite(cd)
r.NoError(err)
r.NoError(dialect.CreateDB())

// Creating DB twice should produce an error
r.EqualError(dialect.CreateDB(), fmt.Sprintf("could not create SQLite database '%s'; database exists", p))
t.Run("CreateFile", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, "testdb.sqlite")

r.NoError(dialect.CreateDB())
r.FileExists(cd.Database)
})

t.Run("MemoryDB_tag", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, "file::memory:?cache=shared")

r.NoError(dialect.CreateDB())
r.NoFileExists(cd.Database)
})

t.Run("MemoryDB_only", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, ":memory:")

r.NoError(dialect.CreateDB())
r.NoFileExists(cd.Database)
})

t.Run("MemoryDB_param", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, "file:foobar?mode=memory&cache=shared")

r.NoError(dialect.CreateDB())
r.NoFileExists(cd.Database)
})

t.Run("CreateFile_ExistingDB", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, "testdb.sqlite")

r.NoError(dialect.CreateDB())
r.EqualError(dialect.CreateDB(), fmt.Sprintf("could not create SQLite database '%s'; database exists", cd.Database))
})

}

func TestSqlite_NewDriver(t *testing.T) {
Expand Down

0 comments on commit 65fd526

Please sign in to comment.