2015-01-20 21:15:48 +08:00
|
|
|
package migrator
|
2015-01-19 17:44:16 +08:00
|
|
|
|
2018-05-10 22:54:21 +08:00
|
|
|
import (
|
2020-11-19 21:47:17 +08:00
|
|
|
"errors"
|
2018-05-10 22:54:21 +08:00
|
|
|
"fmt"
|
2021-01-19 02:57:17 +08:00
|
|
|
"strings"
|
2020-06-29 20:08:32 +08:00
|
|
|
|
2020-02-05 16:00:40 +08:00
|
|
|
"github.com/mattn/go-sqlite3"
|
2025-03-12 22:40:11 +08:00
|
|
|
|
2025-05-02 23:13:01 +08:00
|
|
|
"github.com/grafana/grafana/pkg/util/xorm"
|
2018-05-10 22:54:21 +08:00
|
|
|
)
|
2015-02-24 18:46:34 +08:00
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
type SQLite3 struct {
|
2015-01-19 17:44:16 +08:00
|
|
|
BaseDialect
|
|
|
|
}
|
|
|
|
|
2023-06-15 04:13:36 +08:00
|
|
|
func NewSQLite3Dialect() Dialect {
|
2020-11-11 13:21:08 +08:00
|
|
|
d := SQLite3{}
|
2025-04-10 20:42:23 +08:00
|
|
|
d.dialect = &d
|
|
|
|
d.driverName = SQLite
|
2015-01-19 17:44:16 +08:00
|
|
|
return &d
|
|
|
|
}
|
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) SupportEngine() bool {
|
2015-01-20 21:44:37 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) Quote(name string) string {
|
2015-01-19 17:44:16 +08:00
|
|
|
return "`" + name + "`"
|
|
|
|
}
|
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) AutoIncrStr() string {
|
2015-01-19 17:44:16 +08:00
|
|
|
return "AUTOINCREMENT"
|
|
|
|
}
|
|
|
|
|
2025-03-12 22:40:11 +08:00
|
|
|
func (db *SQLite3) BooleanValue(value bool) any {
|
|
|
|
if value {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) BooleanStr(value bool) string {
|
2016-09-23 14:07:14 +08:00
|
|
|
if value {
|
|
|
|
return "1"
|
|
|
|
}
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
2022-11-02 02:24:32 +08:00
|
|
|
func (db *SQLite3) BatchSize() int {
|
|
|
|
// SQLite has a maximum parameter count per statement of 100.
|
|
|
|
// So, we use a small batch size to support write operations.
|
|
|
|
return 10
|
|
|
|
}
|
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) DateTimeFunc(value string) string {
|
2018-01-26 17:41:41 +08:00
|
|
|
return "datetime(" + value + ")"
|
|
|
|
}
|
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) SQLType(c *Column) string {
|
2015-01-19 17:44:16 +08:00
|
|
|
switch c.Type {
|
|
|
|
case DB_Date, DB_DateTime, DB_TimeStamp, DB_Time:
|
|
|
|
return DB_DateTime
|
|
|
|
case DB_TimeStampz:
|
|
|
|
return DB_Text
|
|
|
|
case DB_Char, DB_Varchar, DB_NVarchar, DB_TinyText, DB_Text, DB_MediumText, DB_LongText:
|
2015-01-20 21:15:48 +08:00
|
|
|
return DB_Text
|
2015-01-19 17:44:16 +08:00
|
|
|
case DB_Bit, DB_TinyInt, DB_SmallInt, DB_MediumInt, DB_Int, DB_Integer, DB_BigInt, DB_Bool:
|
|
|
|
return DB_Integer
|
|
|
|
case DB_Float, DB_Double, DB_Real:
|
|
|
|
return DB_Real
|
|
|
|
case DB_Decimal, DB_Numeric:
|
|
|
|
return DB_Numeric
|
|
|
|
case DB_TinyBlob, DB_Blob, DB_MediumBlob, DB_LongBlob, DB_Bytea, DB_Binary, DB_VarBinary:
|
|
|
|
return DB_Blob
|
|
|
|
case DB_Serial, DB_BigSerial:
|
|
|
|
c.IsPrimaryKey = true
|
|
|
|
c.IsAutoIncrement = true
|
|
|
|
c.Nullable = false
|
2015-01-20 21:15:48 +08:00
|
|
|
return DB_Integer
|
2015-01-19 17:44:16 +08:00
|
|
|
default:
|
|
|
|
return c.Type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 23:46:47 +08:00
|
|
|
func (db *SQLite3) IndexCheckSQL(tableName, indexName string) (string, []any) {
|
|
|
|
args := []any{tableName, indexName}
|
2018-12-19 04:47:45 +08:00
|
|
|
sql := "SELECT 1 FROM " + db.Quote("sqlite_master") + " WHERE " + db.Quote("type") + "='index' AND " + db.Quote("tbl_name") + "=? AND " + db.Quote("name") + "=?"
|
|
|
|
return sql, args
|
2015-01-19 17:44:16 +08:00
|
|
|
}
|
2015-02-24 18:46:34 +08:00
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) DropIndexSQL(tableName string, index *Index) string {
|
2015-02-24 18:46:34 +08:00
|
|
|
quote := db.Quote
|
2020-09-22 22:22:19 +08:00
|
|
|
// var unique string
|
2015-02-24 18:46:34 +08:00
|
|
|
idxName := index.XName(tableName)
|
|
|
|
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
|
|
|
}
|
2018-05-10 22:54:21 +08:00
|
|
|
|
2023-06-15 04:13:36 +08:00
|
|
|
func (db *SQLite3) CleanDB(engine *xorm.Engine) error {
|
2018-05-10 22:54:21 +08:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-27 18:07:43 +08:00
|
|
|
|
2020-10-16 15:46:14 +08:00
|
|
|
// TruncateDBTables deletes all data from all the tables and resets the sequences.
|
|
|
|
// A special case is the dashboard_acl table where we keep the default permissions.
|
2023-06-15 04:13:36 +08:00
|
|
|
func (db *SQLite3) TruncateDBTables(engine *xorm.Engine) error {
|
2025-03-19 21:16:20 +08:00
|
|
|
tables, err := engine.Dialect().GetTables()
|
2020-10-16 15:46:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-15 04:13:36 +08:00
|
|
|
sess := engine.NewSession()
|
2020-10-16 15:46:14 +08:00
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
switch table.Name {
|
2021-01-07 18:36:13 +08:00
|
|
|
case "migration_log":
|
2021-08-25 21:11:22 +08:00
|
|
|
continue
|
2020-10-16 15:46:14 +08:00
|
|
|
case "dashboard_acl":
|
|
|
|
// keep default dashboard permissions
|
|
|
|
if _, err := sess.Exec(fmt.Sprintf("DELETE FROM %q WHERE dashboard_id != -1 AND org_id != -1;", table.Name)); err != nil {
|
2022-06-07 04:30:31 +08:00
|
|
|
return fmt.Errorf("failed to truncate table %q: %w", table.Name, err)
|
2020-10-16 15:46:14 +08:00
|
|
|
}
|
|
|
|
if _, err := sess.Exec("UPDATE sqlite_sequence SET seq = 2 WHERE name = '%s';", table.Name); err != nil {
|
2022-06-07 04:30:31 +08:00
|
|
|
return fmt.Errorf("failed to cleanup sqlite_sequence: %w", err)
|
2020-10-16 15:46:14 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if _, err := sess.Exec(fmt.Sprintf("DELETE FROM %s;", table.Name)); err != nil {
|
2022-06-07 04:30:31 +08:00
|
|
|
return fmt.Errorf("failed to truncate table %q: %w", table.Name, err)
|
2020-10-16 15:46:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := sess.Exec("UPDATE sqlite_sequence SET seq = 0 WHERE name != 'dashboard_acl';"); err != nil {
|
2024-04-08 23:42:12 +08:00
|
|
|
// if we have not created any autoincrement columns in the database this will fail, the error is expected and we can ignore it
|
|
|
|
// we can't discriminate based on code because sqlite returns a generic error code
|
|
|
|
if err.Error() != "no such table: sqlite_sequence" {
|
|
|
|
return fmt.Errorf("failed to cleanup sqlite_sequence: %w", err)
|
|
|
|
}
|
2020-10-16 15:46:14 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) isThisError(err error, errcode int) bool {
|
2020-11-19 21:47:17 +08:00
|
|
|
var driverErr sqlite3.Error
|
|
|
|
if errors.As(err, &driverErr) {
|
2019-06-13 21:36:09 +08:00
|
|
|
if int(driverErr.ExtendedCode) == errcode {
|
2018-09-27 18:07:43 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2019-06-13 21:36:09 +08:00
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) ErrorMessage(err error) string {
|
2020-11-19 21:47:17 +08:00
|
|
|
var driverErr sqlite3.Error
|
|
|
|
if errors.As(err, &driverErr) {
|
2020-04-20 21:48:38 +08:00
|
|
|
return driverErr.Error()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) IsUniqueConstraintViolation(err error) bool {
|
2023-05-29 22:45:08 +08:00
|
|
|
return db.isThisError(err, int(sqlite3.ErrConstraintUnique)) || db.isThisError(err, int(sqlite3.ErrConstraintPrimaryKey))
|
2019-06-13 21:36:09 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 13:21:08 +08:00
|
|
|
func (db *SQLite3) IsDeadlock(err error) bool {
|
2019-06-13 21:36:09 +08:00
|
|
|
return false // No deadlock
|
|
|
|
}
|
2021-01-19 02:57:17 +08:00
|
|
|
|
|
|
|
// UpsertSQL returns the upsert sql statement for SQLite dialect
|
|
|
|
func (db *SQLite3) UpsertSQL(tableName string, keyCols, updateCols []string) string {
|
2022-07-19 16:42:48 +08:00
|
|
|
str, _ := db.UpsertMultipleSQL(tableName, keyCols, updateCols, 1)
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpsertMultipleSQL returns the upsert sql statement for PostgreSQL dialect
|
|
|
|
func (db *SQLite3) UpsertMultipleSQL(tableName string, keyCols, updateCols []string, count int) (string, error) {
|
|
|
|
if count < 1 {
|
|
|
|
return "", fmt.Errorf("upsert statement must have count >= 1. Got %v", count)
|
|
|
|
}
|
2021-01-19 02:57:17 +08:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-07-19 16:42:48 +08:00
|
|
|
valuesStr := strings.Builder{}
|
|
|
|
separatorVar = separator
|
|
|
|
colPlaceHolders := colPlaceHoldersStr.String()
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
if i == count-1 {
|
|
|
|
separatorVar = ""
|
|
|
|
}
|
|
|
|
valuesStr.WriteString(fmt.Sprintf("(%s)%s", colPlaceHolders, separatorVar))
|
|
|
|
}
|
|
|
|
|
|
|
|
s := fmt.Sprintf(`INSERT INTO %s (%s) VALUES %s ON CONFLICT(%s) DO UPDATE SET %s`,
|
2021-01-19 02:57:17 +08:00
|
|
|
tableName,
|
|
|
|
columnsStr.String(),
|
2022-07-19 16:42:48 +08:00
|
|
|
valuesStr.String(),
|
2021-01-19 02:57:17 +08:00
|
|
|
onConflictStr.String(),
|
|
|
|
setStr.String(),
|
|
|
|
)
|
2022-07-19 16:42:48 +08:00
|
|
|
return s, nil
|
2021-01-19 02:57:17 +08:00
|
|
|
}
|
2024-01-25 15:27:13 +08:00
|
|
|
|
|
|
|
func (db *SQLite3) Concat(strs ...string) string {
|
|
|
|
return strings.Join(strs, " || ")
|
|
|
|
}
|