Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dex
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
dex
Commits
52e2a166
Commit
52e2a166
authored
Nov 01, 2016
by
Eric Chiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
storage/sql: use isolation level "serializable" for transactions
parent
651b406c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
7 deletions
+45
-7
config_test.go
storage/sql/config_test.go
+21
-6
sql.go
storage/sql/sql.go
+24
-1
No files found.
storage/sql/config_test.go
View file @
52e2a166
...
...
@@ -36,6 +36,7 @@ func cleanDB(c *conn) error {
delete from auth_code;
delete from refresh_token;
delete from keys;
delete from password;
`
)
return
err
}
...
...
@@ -48,6 +49,7 @@ func TestSQLite3(t *testing.T) {
s
:=
&
SQLite3
{
":memory:"
}
conn
,
err
:=
s
.
open
()
if
err
!=
nil
{
fmt
.
Fprintln
(
os
.
Stdout
,
err
)
t
.
Fatal
(
err
)
}
return
conn
...
...
@@ -58,15 +60,25 @@ func TestSQLite3(t *testing.T) {
})
}
func
getenv
(
key
,
defaultVal
string
)
string
{
if
val
:=
os
.
Getenv
(
key
);
val
!=
""
{
return
val
}
return
defaultVal
}
const
testPostgresEnv
=
"DEX_POSTGRES_HOST"
func
TestPostgres
(
t
*
testing
.
T
)
{
if
os
.
Getenv
(
"DEX_POSTGRES_HOST"
)
==
""
{
t
.
Skip
(
"postgres envs not set, skipping tests"
)
host
:=
os
.
Getenv
(
testPostgresEnv
)
if
host
==
""
{
t
.
Skipf
(
"test environment variable %q not set, skipping"
,
testPostgresEnv
)
}
p
:=
Postgres
{
Database
:
os
.
Getenv
(
"DEX_POSTGRES_DATABASE
"
),
User
:
os
.
Getenv
(
"DEX_POSTGRES_USER
"
),
Password
:
os
.
Getenv
(
"DEX_POSTGRES_PASSWORD
"
),
Host
:
os
.
Getenv
(
"DEX_POSTGRES_HOST"
)
,
Database
:
getenv
(
"DEX_POSTGRES_DATABASE"
,
"postgres
"
),
User
:
getenv
(
"DEX_POSTGRES_USER"
,
"postgres
"
),
Password
:
getenv
(
"DEX_POSTGRES_PASSWORD"
,
"postgres
"
),
Host
:
host
,
SSL
:
PostgresSSL
{
Mode
:
sslDisable
,
// Postgres container doesn't support SSL.
},
...
...
@@ -92,4 +104,7 @@ func TestPostgres(t *testing.T) {
withTimeout
(
time
.
Minute
*
1
,
func
()
{
conformance
.
RunTests
(
t
,
newStorage
)
})
withTimeout
(
time
.
Minute
*
1
,
func
()
{
conformance
.
RunTransactionTests
(
t
,
newStorage
)
})
}
storage/sql/sql.go
View file @
52e2a166
...
...
@@ -45,7 +45,30 @@ func matchLiteral(s string) *regexp.Regexp {
var
(
// The "github.com/lib/pq" driver is the default flavor. All others are
// translations of this.
flavorPostgres
=
flavor
{}
flavorPostgres
=
flavor
{
// The default behavior for Postgres transactions is consistent reads, not consistent writes.
// For each transaction opened, ensure it has the correct isolation level.
//
// See: https://www.postgresql.org/docs/9.3/static/sql-set-transaction.html
//
// NOTE(ericchiang): For some reason using `SET SESSION CHARACTERISTICS AS TRANSACTION` at a
// session level didn't work for some edge cases. Might be something worth exploring.
executeTx
:
func
(
db
*
sql
.
DB
,
fn
func
(
sqlTx
*
sql
.
Tx
)
error
)
error
{
tx
,
err
:=
db
.
Begin
()
if
err
!=
nil
{
return
err
}
defer
tx
.
Rollback
()
if
_
,
err
:=
tx
.
Exec
(
`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;`
);
err
!=
nil
{
return
err
}
if
err
:=
fn
(
tx
);
err
!=
nil
{
return
err
}
return
tx
.
Commit
()
},
}
flavorSQLite3
=
flavor
{
queryReplacers
:
[]
replacer
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment