Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: gobuffalo/pop
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ce05da9328cd04ac1c9c5fb62ed98e65a9596f65
Choose a base ref
..
head repository: gobuffalo/pop
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9b946de9c71b279d8c5861b3283452799ea2f453
Choose a head ref
Showing with 108 additions and 23 deletions.
  1. +1 −0 .gitignore
  2. +2 −7 connection.go
  3. +45 −0 connection_test.go
  4. +17 −7 dialect_sqlite.go
  5. +43 −9 dialect_sqlite_test.go
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ testdata/migrations/schema.sql
vendor/
.env
.release-env
.vscode

# test data
cockroach-data/
9 changes: 2 additions & 7 deletions connection.go
Original file line number Diff line number Diff line change
@@ -186,7 +186,7 @@ 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(context.Background(), nil)
return c.NewTransactionContextOptions(c.Context(), nil)
}

// NewTransactionContext starts a new transaction on the connection using the provided context
@@ -202,15 +202,10 @@ func (c *Connection) NewTransactionContextOptions(ctx context.Context, 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,
}
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"
@@ -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
@@ -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
}

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
@@ -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) {