Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
d0932442
Unverified
Commit
d0932442
authored
Sep 27, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sqlstore: add support for checking if error is constraint validation error
parent
c5278af6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
76 additions
and
19 deletions
+76
-19
Gopkg.lock
+7
-1
Gopkg.toml
+4
-0
pkg/services/sqlstore/alert_notification.go
+8
-18
pkg/services/sqlstore/migrator/dialect.go
+2
-0
pkg/services/sqlstore/migrator/mysql_dialect.go
+12
-0
pkg/services/sqlstore/migrator/postgres_dialect.go
+11
-0
pkg/services/sqlstore/migrator/sqlite_dialect.go
+11
-0
vendor/github.com/VividCortex/mysqlerr/LICENSE
+21
-0
vendor/github.com/VividCortex/mysqlerr/mysqlerr.go
+0
-0
No files found.
Gopkg.lock
View file @
d0932442
...
@@ -20,6 +20,12 @@
...
@@ -20,6 +20,12 @@
revision = "7677a1d7c1137cd3dd5ba7a076d0c898a1ef4520"
revision = "7677a1d7c1137cd3dd5ba7a076d0c898a1ef4520"
[[projects]]
[[projects]]
branch = "master"
name = "github.com/VividCortex/mysqlerr"
packages = ["."]
revision = "6c6b55f8796f578c870b7e19bafb16103bc40095"
[[projects]]
name = "github.com/aws/aws-sdk-go"
name = "github.com/aws/aws-sdk-go"
packages = [
packages = [
"aws",
"aws",
...
@@ -673,6 +679,6 @@
...
@@ -673,6 +679,6 @@
[solve-meta]
[solve-meta]
analyzer-name = "dep"
analyzer-name = "dep"
analyzer-version = 1
analyzer-version = 1
inputs-digest = "
81a37e747b875cf870c1b9486fa3147e704dea7db8ba86f7cb942d3ddc01d3e3
"
inputs-digest = "
6e9458f912a5f0eb3430b968f1b4dbc4e3b7671b282cf4fe1573419a6d9ba0d4
"
solver-name = "gps-cdcl"
solver-name = "gps-cdcl"
solver-version = 1
solver-version = 1
Gopkg.toml
View file @
d0932442
...
@@ -203,3 +203,7 @@ ignored = [
...
@@ -203,3 +203,7 @@ ignored = [
[[constraint]]
[[constraint]]
name
=
"github.com/denisenkom/go-mssqldb"
name
=
"github.com/denisenkom/go-mssqldb"
revision
=
"270bc3860bb94dd3a3ffd047377d746c5e276726"
revision
=
"270bc3860bb94dd3a3ffd047377d746c5e276726"
[[constraint]]
name
=
"github.com/VividCortex/mysqlerr"
branch
=
"master"
pkg/services/sqlstore/alert_notification.go
View file @
d0932442
...
@@ -240,31 +240,21 @@ func InsertAlertNotificationState(ctx context.Context, cmd *m.InsertAlertNotific
...
@@ -240,31 +240,21 @@ func InsertAlertNotificationState(ctx context.Context, cmd *m.InsertAlertNotific
State
:
cmd
.
State
,
State
:
cmd
.
State
,
}
}
_
,
err
:=
sess
.
Insert
(
notificationState
)
if
_
,
err
:=
sess
.
Insert
(
notificationState
);
err
!=
nil
{
if
dialect
.
IsUniqueConstraintViolation
(
err
)
{
if
err
==
nil
{
return
nil
}
uniqenessIndexFailureCodes
:=
[]
string
{
"UNIQUE constraint failed"
,
"pq: duplicate key value violates unique constraint"
,
"Error 1062: Duplicate entry "
,
}
for
_
,
code
:=
range
uniqenessIndexFailureCodes
{
if
strings
.
HasPrefix
(
err
.
Error
(),
code
)
{
return
m
.
ErrAlertNotificationStateAlreadyExist
return
m
.
ErrAlertNotificationStateAlreadyExist
}
}
return
err
}
}
return
err
return
nil
})
})
}
}
func
SetAlertNotificationStateToCompleteCommand
(
ctx
context
.
Context
,
cmd
*
m
.
SetAlertNotificationStateToCompleteCommand
)
error
{
func
SetAlertNotificationStateToCompleteCommand
(
ctx
context
.
Context
,
cmd
*
m
.
SetAlertNotificationStateToCompleteCommand
)
error
{
return
withDbSession
(
ctx
,
func
(
sess
*
DBSession
)
error
{
return
withDbSession
(
ctx
,
func
(
sess
*
DBSession
)
error
{
sql
:=
`UPDATE alert_notification_state SET
sql
:=
`UPDATE alert_notification_state SET
state= ?
state= ?
WHERE
WHERE
id = ?`
id = ?`
...
@@ -286,8 +276,8 @@ func SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *m.SetA
...
@@ -286,8 +276,8 @@ func SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *m.SetA
func
SetAlertNotificationStateToPendingCommand
(
ctx
context
.
Context
,
cmd
*
m
.
SetAlertNotificationStateToPendingCommand
)
error
{
func
SetAlertNotificationStateToPendingCommand
(
ctx
context
.
Context
,
cmd
*
m
.
SetAlertNotificationStateToPendingCommand
)
error
{
return
withDbSession
(
ctx
,
func
(
sess
*
DBSession
)
error
{
return
withDbSession
(
ctx
,
func
(
sess
*
DBSession
)
error
{
sql
:=
`UPDATE alert_notification_state SET
sql
:=
`UPDATE alert_notification_state SET
state= ?,
state= ?,
version = ?
version = ?
WHERE
WHERE
id = ? AND
id = ? AND
...
...
pkg/services/sqlstore/migrator/dialect.go
View file @
d0932442
...
@@ -44,6 +44,8 @@ type Dialect interface {
...
@@ -44,6 +44,8 @@ type Dialect interface {
CleanDB
()
error
CleanDB
()
error
NoOpSql
()
string
NoOpSql
()
string
IsUniqueConstraintViolation
(
err
error
)
bool
}
}
func
NewDialect
(
engine
*
xorm
.
Engine
)
Dialect
{
func
NewDialect
(
engine
*
xorm
.
Engine
)
Dialect
{
...
...
pkg/services/sqlstore/migrator/mysql_dialect.go
View file @
d0932442
...
@@ -5,6 +5,8 @@ import (
...
@@ -5,6 +5,8 @@ import (
"strconv"
"strconv"
"strings"
"strings"
"github.com/VividCortex/mysqlerr"
"github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"github.com/go-xorm/xorm"
)
)
...
@@ -125,3 +127,13 @@ func (db *Mysql) CleanDB() error {
...
@@ -125,3 +127,13 @@ func (db *Mysql) CleanDB() error {
return
nil
return
nil
}
}
func
(
db
*
Mysql
)
IsUniqueConstraintViolation
(
err
error
)
bool
{
if
driverErr
,
ok
:=
err
.
(
*
mysql
.
MySQLError
);
ok
{
if
driverErr
.
Number
==
mysqlerr
.
ER_DUP_ENTRY
{
return
true
}
}
return
false
}
pkg/services/sqlstore/migrator/postgres_dialect.go
View file @
d0932442
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"strings"
"strings"
"github.com/go-xorm/xorm"
"github.com/go-xorm/xorm"
"github.com/lib/pq"
)
)
type
Postgres
struct
{
type
Postgres
struct
{
...
@@ -136,3 +137,13 @@ func (db *Postgres) CleanDB() error {
...
@@ -136,3 +137,13 @@ func (db *Postgres) CleanDB() error {
return
nil
return
nil
}
}
func
(
db
*
Postgres
)
IsUniqueConstraintViolation
(
err
error
)
bool
{
if
driverErr
,
ok
:=
err
.
(
*
pq
.
Error
);
ok
{
if
driverErr
.
Code
==
"23505"
{
return
true
}
}
return
false
}
pkg/services/sqlstore/migrator/sqlite_dialect.go
View file @
d0932442
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"fmt"
"fmt"
"github.com/go-xorm/xorm"
"github.com/go-xorm/xorm"
sqlite3
"github.com/mattn/go-sqlite3"
)
)
type
Sqlite3
struct
{
type
Sqlite3
struct
{
...
@@ -82,3 +83,13 @@ func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
...
@@ -82,3 +83,13 @@ func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
func
(
db
*
Sqlite3
)
CleanDB
()
error
{
func
(
db
*
Sqlite3
)
CleanDB
()
error
{
return
nil
return
nil
}
}
func
(
db
*
Sqlite3
)
IsUniqueConstraintViolation
(
err
error
)
bool
{
if
driverErr
,
ok
:=
err
.
(
sqlite3
.
Error
);
ok
{
if
driverErr
.
ExtendedCode
==
sqlite3
.
ErrConstraintUnique
{
return
true
}
}
return
false
}
vendor/github.com/VividCortex/mysqlerr/LICENSE
0 → 100644
View file @
d0932442
MIT License
Copyright (c) 2017 VividCortex
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
vendor/github.com/VividCortex/mysqlerr/mysqlerr.go
0 → 100644
View file @
d0932442
This diff is collapsed.
Click to expand it.
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