mirror of
https://github.com/grafana/grafana.git
synced 2025-08-06 02:29:33 +08:00
AlertingNG: Save alert instances (#30223)
* AlertingNG: Save alert instances Co-authored-by: Kyle Brandt <kyle@grafana.com> * Rename alert instance fields/columns * Include definition title in listing alert instances * Delete instances when deleting defintion Co-authored-by: Kyle Brandt <kyle@grafana.com>
This commit is contained in:

committed by
GitHub

parent
93a59561ba
commit
8c31e25926
@ -3,6 +3,7 @@ package migrator
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
@ -147,3 +148,40 @@ func (db *SQLite3) IsUniqueConstraintViolation(err error) bool {
|
||||
func (db *SQLite3) IsDeadlock(err error) bool {
|
||||
return false // No deadlock
|
||||
}
|
||||
|
||||
// UpsertSQL returns the upsert sql statement for SQLite dialect
|
||||
func (db *SQLite3) UpsertSQL(tableName string, keyCols, updateCols []string) string {
|
||||
columnsStr := strings.Builder{}
|
||||
onConflictStr := strings.Builder{}
|
||||
colPlaceHoldersStr := strings.Builder{}
|
||||
setStr := strings.Builder{}
|
||||
|
||||
const separator = ", "
|
||||
separatorVar := separator
|
||||
for i, c := range updateCols {
|
||||
if i == len(updateCols)-1 {
|
||||
separatorVar = ""
|
||||
}
|
||||
|
||||
columnsStr.WriteString(fmt.Sprintf("%s%s", db.Quote(c), separatorVar))
|
||||
colPlaceHoldersStr.WriteString(fmt.Sprintf("?%s", separatorVar))
|
||||
setStr.WriteString(fmt.Sprintf("%s=excluded.%s%s", db.Quote(c), db.Quote(c), separatorVar))
|
||||
}
|
||||
|
||||
separatorVar = separator
|
||||
for i, c := range keyCols {
|
||||
if i == len(keyCols)-1 {
|
||||
separatorVar = ""
|
||||
}
|
||||
onConflictStr.WriteString(fmt.Sprintf("%s%s", db.Quote(c), separatorVar))
|
||||
}
|
||||
|
||||
s := fmt.Sprintf(`INSERT INTO %s (%s) VALUES (%s) ON CONFLICT(%s) DO UPDATE SET %s`,
|
||||
tableName,
|
||||
columnsStr.String(),
|
||||
colPlaceHoldersStr.String(),
|
||||
onConflictStr.String(),
|
||||
setStr.String(),
|
||||
)
|
||||
return s
|
||||
}
|
||||
|
Reference in New Issue
Block a user