|
@@ -22,7 +22,7 @@ import (
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
)
|
|
)
|
|
|
|
|
|
-const currentSchemaVersion = 1
|
|
|
|
|
|
+const currentSchemaVersion = 2
|
|
|
|
|
|
//go:embed sql/**
|
|
//go:embed sql/**
|
|
var embedded embed.FS
|
|
var embedded embed.FS
|
|
@@ -62,6 +62,17 @@ func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScript
|
|
baseName: filepath.Base(path),
|
|
baseName: filepath.Base(path),
|
|
sql: sqlDB,
|
|
sql: sqlDB,
|
|
statements: make(map[string]*sqlx.Stmt),
|
|
statements: make(map[string]*sqlx.Stmt),
|
|
|
|
+ tplInput: map[string]any{
|
|
|
|
+ "FlagLocalUnsupported": protocol.FlagLocalUnsupported,
|
|
|
|
+ "FlagLocalIgnored": protocol.FlagLocalIgnored,
|
|
|
|
+ "FlagLocalMustRescan": protocol.FlagLocalMustRescan,
|
|
|
|
+ "FlagLocalReceiveOnly": protocol.FlagLocalReceiveOnly,
|
|
|
|
+ "FlagLocalGlobal": protocol.FlagLocalGlobal,
|
|
|
|
+ "FlagLocalNeeded": protocol.FlagLocalNeeded,
|
|
|
|
+ "FlagLocalRemoteInvalid": protocol.FlagLocalRemoteInvalid,
|
|
|
|
+ "LocalInvalidFlags": protocol.LocalInvalidFlags,
|
|
|
|
+ "SyncthingVersion": build.LongVersion,
|
|
|
|
+ },
|
|
}
|
|
}
|
|
|
|
|
|
for _, script := range schemaScripts {
|
|
for _, script := range schemaScripts {
|
|
@@ -96,17 +107,6 @@ func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScript
|
|
return nil, wrap(err)
|
|
return nil, wrap(err)
|
|
}
|
|
}
|
|
|
|
|
|
- db.tplInput = map[string]any{
|
|
|
|
- "FlagLocalUnsupported": protocol.FlagLocalUnsupported,
|
|
|
|
- "FlagLocalIgnored": protocol.FlagLocalIgnored,
|
|
|
|
- "FlagLocalMustRescan": protocol.FlagLocalMustRescan,
|
|
|
|
- "FlagLocalReceiveOnly": protocol.FlagLocalReceiveOnly,
|
|
|
|
- "FlagLocalGlobal": protocol.FlagLocalGlobal,
|
|
|
|
- "FlagLocalNeeded": protocol.FlagLocalNeeded,
|
|
|
|
- "LocalInvalidFlags": protocol.LocalInvalidFlags,
|
|
|
|
- "SyncthingVersion": build.LongVersion,
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
return db, nil
|
|
return db, nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -152,15 +152,8 @@ func (s *baseDB) stmt(tpl string) stmt {
|
|
return stmt
|
|
return stmt
|
|
}
|
|
}
|
|
|
|
|
|
- // Apply template expansions
|
|
|
|
- var sb strings.Builder
|
|
|
|
- compTpl := template.Must(template.New("tpl").Funcs(tplFuncs).Parse(tpl))
|
|
|
|
- if err := compTpl.Execute(&sb, s.tplInput); err != nil {
|
|
|
|
- panic("bug: bad template: " + err.Error())
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// Prepare and cache
|
|
// Prepare and cache
|
|
- stmt, err := s.sql.Preparex(sb.String())
|
|
|
|
|
|
+ stmt, err := s.sql.Preparex(s.expandTemplateVars(tpl))
|
|
if err != nil {
|
|
if err != nil {
|
|
return failedStmt{err}
|
|
return failedStmt{err}
|
|
}
|
|
}
|
|
@@ -168,6 +161,17 @@ func (s *baseDB) stmt(tpl string) stmt {
|
|
return stmt
|
|
return stmt
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// expandTemplateVars just applies template expansions to the template
|
|
|
|
+// string, or dies trying
|
|
|
|
+func (s *baseDB) expandTemplateVars(tpl string) string {
|
|
|
|
+ var sb strings.Builder
|
|
|
|
+ compTpl := template.Must(template.New("tpl").Funcs(tplFuncs).Parse(tpl))
|
|
|
|
+ if err := compTpl.Execute(&sb, s.tplInput); err != nil {
|
|
|
|
+ panic("bug: bad template: " + err.Error())
|
|
|
|
+ }
|
|
|
|
+ return sb.String()
|
|
|
|
+}
|
|
|
|
+
|
|
type stmt interface {
|
|
type stmt interface {
|
|
Exec(args ...any) (sql.Result, error)
|
|
Exec(args ...any) (sql.Result, error)
|
|
Get(dest any, args ...any) error
|
|
Get(dest any, args ...any) error
|
|
@@ -212,7 +216,7 @@ nextScript:
|
|
// separately. We require it on a separate line because there are
|
|
// separately. We require it on a separate line because there are
|
|
// also statement-internal semicolons in the triggers.
|
|
// also statement-internal semicolons in the triggers.
|
|
for _, stmt := range strings.Split(string(bs), "\n;") {
|
|
for _, stmt := range strings.Split(string(bs), "\n;") {
|
|
- if _, err := tx.Exec(stmt); err != nil {
|
|
|
|
|
|
+ if _, err := tx.Exec(s.expandTemplateVars(stmt)); err != nil {
|
|
return wrap(err, stmt)
|
|
return wrap(err, stmt)
|
|
}
|
|
}
|
|
}
|
|
}
|