Skip to content

Commit

Permalink
fix: sqlite dsn check (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benehiko authored Mar 22, 2021
1 parent d80f3e1 commit cc927dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
10 changes: 3 additions & 7 deletions dbal/dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ const (
SQLiteSharedInMemory = "sqlite://file::memory:?_fk=true&cache=shared"
)

var dsnRegex = regexp.MustCompile(`^(sqlite://file:(?:.+)\?((\w+=\w+)(&\w+=\w+)*)?(&?mode=memory)(&\w+=\w+)*)$|(?:sqlite://(file:)?:memory:(?:\?\w+=\w+)?(?:&\w+=\w+)*)|^(?:(?::memory:)|(?:memory))$`)

// SQLite can be written in different styles depending on the use case
// - just in memory
// - shared connection
// - shared but unique in the same process
// see: https://sqlite.org/inmemorydb.html
func IsMemorySQLite(dsn string) bool {
r := regexp.MustCompile(`sqlite://file::?\w+:\?_fk=true&*(cache=shared)?(mode=memory)?`)

if r.MatchString(dsn) {
return true
}

return false
return dsnRegex.MatchString(dsn)
}
45 changes: 29 additions & 16 deletions dbal/dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,36 @@ package dbal
import (
"testing"

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

func TestIsMemorySQLite(t *testing.T) {
require.True(t, IsMemorySQLite(SQLiteInMemory))
require.True(t, IsMemorySQLite(SQLiteSharedInMemory))
require.True(t, IsMemorySQLite("sqlite://file:uniquedb:?_fk=true&mode=memory"))
require.True(t, IsMemorySQLite("sqlite://file:uniquedb:?_fk=true&cache=shared"))
require.True(t, IsMemorySQLite("sqlite://file:uniquedb:?_fk=true&mode=memory&cache=shared"))
require.True(t, IsMemorySQLite("sqlite://file:uniquedb:?_fk=true&cache=shared&mode=memory"))
require.False(t, IsMemorySQLite("sqlite://file:::uniquedb:?_fk=true&mode=memory"))
require.False(t, IsMemorySQLite("sqlite://"))
require.False(t, IsMemorySQLite("sqlite://file"))
require.False(t, IsMemorySQLite("sqlite://file:::"))
require.False(t, IsMemorySQLite("sqlite://?_fk=true&mode=memory"))
require.False(t, IsMemorySQLite("sqlite://?_fk=true&cache=shared"))
require.False(t, IsMemorySQLite("sqlite://file::?_fk=true"))
require.False(t, IsMemorySQLite("sqlite://file:::?_fk=true"))
require.False(t, IsMemorySQLite("postgresql://username:secret@localhost:5432/database"))
testCases := map[string]bool{
SQLiteInMemory: true,
SQLiteSharedInMemory: true,
"memory": true,
":memory:": true,
"sqlite://:memory:?_fk=true": true,
"sqlite://file:uniquedb:?_fk=true&mode=memory": true,
"sqlite://file:uniquedb:?_fk=true&mode=memory&cache=shared": true,
"sqlite://file:uniquedb:?_fk=true&cache=shared&mode=memory": true,
"sqlite://file:uniquedb:?mode=memory": true,
"sqlite://file:::uniquedb:?_fk=true&mode=memory": true,
"sqlite://file:memdb1?mode=memory&cache=shared": true,
"sqlite://file:uniquedb:?_fk=true&cache=shared": false,
"sqlite://": false,
"sqlite://file": false,
"sqlite://file:::": false,
"sqlite://?_fk=true&mode=memory": false,
"sqlite://?_fk=true&cache=shared": false,
"sqlite://file::?_fk=true": false,
"sqlite://file:::?_fk=true": false,
"postgresql://username:secret@localhost:5432/database": false,
}

for dsn, expected := range testCases {
t.Run("dsn="+dsn, func(t *testing.T) {
assert.Equal(t, expected, IsMemorySQLite(dsn))
})
}
}

0 comments on commit cc927dd

Please sign in to comment.