basedb.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright (C) 2025 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package sqlite
  7. import (
  8. "context"
  9. "database/sql"
  10. "embed"
  11. "io/fs"
  12. "log/slog"
  13. "net/url"
  14. "path/filepath"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "text/template"
  19. "time"
  20. "github.com/jmoiron/sqlx"
  21. "github.com/syncthing/syncthing/internal/slogutil"
  22. "github.com/syncthing/syncthing/lib/build"
  23. "github.com/syncthing/syncthing/lib/protocol"
  24. )
  25. const (
  26. currentSchemaVersion = 5
  27. applicationIDMain = 0x53546d6e // "STmn", Syncthing main database
  28. applicationIDFolder = 0x53546664 // "STfd", Syncthing folder database
  29. )
  30. //go:embed sql/**
  31. var embedded embed.FS
  32. type baseDB struct {
  33. path string
  34. baseName string
  35. sql *sqlx.DB
  36. updateLock sync.Mutex
  37. updatePoints int
  38. checkpointsCount int
  39. statementsMut sync.RWMutex
  40. statements map[string]*sqlx.Stmt
  41. tplInput map[string]any
  42. }
  43. func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScripts []string) (*baseDB, error) {
  44. // Open the database with options to enable foreign keys and recursive
  45. // triggers (needed for the delete+insert triggers on row replace).
  46. pathURL := url.URL{
  47. Scheme: "file",
  48. Path: fileToUriPath(path),
  49. RawQuery: commonOptions,
  50. }
  51. sqlDB, err := sqlx.Open(dbDriver, pathURL.String())
  52. if err != nil {
  53. return nil, wrap(err)
  54. }
  55. sqlDB.SetMaxOpenConns(maxConns)
  56. for _, pragma := range pragmas {
  57. if _, err := sqlDB.Exec("PRAGMA " + pragma); err != nil {
  58. return nil, wrap(err, "PRAGMA "+pragma)
  59. }
  60. }
  61. db := &baseDB{
  62. path: path,
  63. baseName: filepath.Base(path),
  64. sql: sqlDB,
  65. statements: make(map[string]*sqlx.Stmt),
  66. tplInput: map[string]any{
  67. "FlagLocalUnsupported": protocol.FlagLocalUnsupported,
  68. "FlagLocalIgnored": protocol.FlagLocalIgnored,
  69. "FlagLocalMustRescan": protocol.FlagLocalMustRescan,
  70. "FlagLocalReceiveOnly": protocol.FlagLocalReceiveOnly,
  71. "FlagLocalGlobal": protocol.FlagLocalGlobal,
  72. "FlagLocalNeeded": protocol.FlagLocalNeeded,
  73. "FlagLocalRemoteInvalid": protocol.FlagLocalRemoteInvalid,
  74. "LocalInvalidFlags": protocol.LocalInvalidFlags,
  75. "SyncthingVersion": build.LongVersion,
  76. },
  77. }
  78. // Create a specific connection for the schema setup and migration to
  79. // run in. We do this because we need to disable foreign keys for the
  80. // duration, which is a thing that needs to happen outside of a
  81. // transaction and affects the connection it's run on. So we need to a)
  82. // make sure all our commands run on this specific connection (which the
  83. // transaction accomplishes naturally) and b) make sure these pragmas
  84. // don't leak to anyone else afterwards.
  85. ctx := context.TODO()
  86. conn, err := db.sql.Connx(ctx)
  87. if err != nil {
  88. return nil, wrap(err)
  89. }
  90. defer func() {
  91. _, _ = conn.ExecContext(ctx, "PRAGMA foreign_keys = ON")
  92. _, _ = conn.ExecContext(ctx, "PRAGMA legacy_alter_table = OFF")
  93. conn.Close()
  94. }()
  95. if _, err := conn.ExecContext(ctx, "PRAGMA foreign_keys = OFF"); err != nil {
  96. return nil, wrap(err)
  97. }
  98. if _, err := conn.ExecContext(ctx, "PRAGMA legacy_alter_table = ON"); err != nil {
  99. return nil, wrap(err)
  100. }
  101. tx, err := conn.BeginTxx(ctx, nil)
  102. if err != nil {
  103. return nil, wrap(err)
  104. }
  105. defer tx.Rollback()
  106. for _, script := range schemaScripts {
  107. if err := db.runScripts(tx, script); err != nil {
  108. return nil, wrap(err)
  109. }
  110. }
  111. ver, _ := db.getAppliedSchemaVersion(tx)
  112. shouldVacuum := false
  113. if ver.SchemaVersion > 0 {
  114. filter := func(scr string) bool {
  115. scr = filepath.Base(scr)
  116. nstr, _, ok := strings.Cut(scr, "-")
  117. if !ok {
  118. return false
  119. }
  120. n, err := strconv.ParseInt(nstr, 10, 32)
  121. if err != nil {
  122. return false
  123. }
  124. if int(n) > ver.SchemaVersion {
  125. slog.Info("Applying database migration", slogutil.FilePath(db.baseName), slog.String("script", scr))
  126. shouldVacuum = true
  127. return true
  128. }
  129. return false
  130. }
  131. for _, script := range migrationScripts {
  132. if err := db.runScripts(tx, script, filter); err != nil {
  133. return nil, wrap(err)
  134. }
  135. }
  136. // Run the initial schema scripts once more. This is generally a
  137. // no-op. However, dropping a table removes associated triggers etc,
  138. // and that's a thing we sometimes do in migrations. To avoid having
  139. // to repeat the setup of associated triggers and indexes in the
  140. // migration, we re-run the initial schema scripts.
  141. for _, script := range schemaScripts {
  142. if err := db.runScripts(tx, script); err != nil {
  143. return nil, wrap(err)
  144. }
  145. }
  146. // Finally, ensure nothing we've done along the way has violated key integrity.
  147. if _, err := conn.ExecContext(ctx, "PRAGMA foreign_key_check"); err != nil {
  148. return nil, wrap(err)
  149. }
  150. }
  151. // Set the current schema version, if not already set
  152. if err := db.setAppliedSchemaVersion(tx, currentSchemaVersion); err != nil {
  153. return nil, wrap(err)
  154. }
  155. if err := tx.Commit(); err != nil {
  156. return nil, wrap(err)
  157. }
  158. if shouldVacuum {
  159. // We applied migrations and should take the opportunity to vaccuum
  160. // the database.
  161. if err := db.vacuumAndOptimize(); err != nil {
  162. return nil, wrap(err)
  163. }
  164. }
  165. return db, nil
  166. }
  167. func fileToUriPath(path string) string {
  168. path = filepath.ToSlash(path)
  169. if (build.IsWindows && len(path) >= 2 && path[1] == ':') ||
  170. (strings.HasPrefix(path, "//") && !strings.HasPrefix(path, "///")) {
  171. // Add an extra leading slash for Windows drive letter or UNC path
  172. path = "/" + path
  173. }
  174. return path
  175. }
  176. func (s *baseDB) Close() error {
  177. s.updateLock.Lock()
  178. s.statementsMut.Lock()
  179. defer s.updateLock.Unlock()
  180. defer s.statementsMut.Unlock()
  181. for _, stmt := range s.statements {
  182. stmt.Close()
  183. }
  184. return wrap(s.sql.Close())
  185. }
  186. var tplFuncs = template.FuncMap{
  187. "or": func(vs ...int) int {
  188. v := vs[0]
  189. for _, ov := range vs[1:] {
  190. v |= ov
  191. }
  192. return v
  193. },
  194. }
  195. // stmt returns a prepared statement for the given SQL string, after
  196. // applying local template expansions. The statement is cached.
  197. func (s *baseDB) stmt(tpl string) stmt {
  198. tpl = strings.TrimSpace(tpl)
  199. // Fast concurrent lookup of cached statement
  200. s.statementsMut.RLock()
  201. stmt, ok := s.statements[tpl]
  202. s.statementsMut.RUnlock()
  203. if ok {
  204. return stmt
  205. }
  206. // On miss, take the full lock, check again
  207. s.statementsMut.Lock()
  208. defer s.statementsMut.Unlock()
  209. stmt, ok = s.statements[tpl]
  210. if ok {
  211. return stmt
  212. }
  213. // Prepare and cache
  214. stmt, err := s.sql.Preparex(s.expandTemplateVars(tpl))
  215. if err != nil {
  216. return failedStmt{err}
  217. }
  218. s.statements[tpl] = stmt
  219. return stmt
  220. }
  221. // expandTemplateVars just applies template expansions to the template
  222. // string, or dies trying
  223. func (s *baseDB) expandTemplateVars(tpl string) string {
  224. var sb strings.Builder
  225. compTpl := template.Must(template.New("tpl").Funcs(tplFuncs).Parse(tpl))
  226. if err := compTpl.Execute(&sb, s.tplInput); err != nil {
  227. panic("bug: bad template: " + err.Error())
  228. }
  229. return sb.String()
  230. }
  231. func (s *baseDB) vacuumAndOptimize() error {
  232. stmts := []string{
  233. "VACUUM;",
  234. "PRAGMA optimize;",
  235. "PRAGMA wal_checkpoint(truncate);",
  236. }
  237. for _, stmt := range stmts {
  238. if _, err := s.sql.Exec(stmt); err != nil {
  239. return wrap(err, stmt)
  240. }
  241. }
  242. return nil
  243. }
  244. type stmt interface {
  245. Exec(args ...any) (sql.Result, error)
  246. Get(dest any, args ...any) error
  247. Queryx(args ...any) (*sqlx.Rows, error)
  248. Select(dest any, args ...any) error
  249. }
  250. type failedStmt struct {
  251. err error
  252. }
  253. func (f failedStmt) Exec(_ ...any) (sql.Result, error) { return nil, f.err }
  254. func (f failedStmt) Get(_ any, _ ...any) error { return f.err }
  255. func (f failedStmt) Queryx(_ ...any) (*sqlx.Rows, error) { return nil, f.err }
  256. func (f failedStmt) Select(_ any, _ ...any) error { return f.err }
  257. func (s *baseDB) runScripts(tx *sqlx.Tx, glob string, filter ...func(s string) bool) error {
  258. scripts, err := fs.Glob(embedded, glob)
  259. if err != nil {
  260. return wrap(err)
  261. }
  262. nextScript:
  263. for _, scr := range scripts {
  264. for _, fn := range filter {
  265. if !fn(scr) {
  266. continue nextScript
  267. }
  268. }
  269. bs, err := fs.ReadFile(embedded, scr)
  270. if err != nil {
  271. return wrap(err, scr)
  272. }
  273. // SQLite requires one statement per exec, so we split the init
  274. // files on lines containing only a semicolon and execute them
  275. // separately. We require it on a separate line because there are
  276. // also statement-internal semicolons in the triggers.
  277. for _, stmt := range strings.Split(string(bs), "\n;") {
  278. if _, err := tx.Exec(s.expandTemplateVars(stmt)); err != nil {
  279. if strings.Contains(stmt, "syncthing:ignore-failure") {
  280. // We're ok with this failing. Just note it.
  281. slog.Debug("Script failed, but with ignore-failure annotation", slog.String("script", scr), slogutil.Error(wrap(err, stmt)))
  282. } else {
  283. return wrap(err, stmt)
  284. }
  285. }
  286. }
  287. }
  288. return nil
  289. }
  290. type schemaVersion struct {
  291. SchemaVersion int
  292. AppliedAt int64
  293. SyncthingVersion string
  294. }
  295. func (s *schemaVersion) AppliedTime() time.Time {
  296. return time.Unix(0, s.AppliedAt)
  297. }
  298. func (s *baseDB) setAppliedSchemaVersion(tx *sqlx.Tx, ver int) error {
  299. _, err := tx.Exec(`
  300. INSERT OR IGNORE INTO schemamigrations (schema_version, applied_at, syncthing_version)
  301. VALUES (?, ?, ?)
  302. `, ver, time.Now().UnixNano(), build.LongVersion)
  303. return wrap(err)
  304. }
  305. func (s *baseDB) getAppliedSchemaVersion(tx *sqlx.Tx) (schemaVersion, error) {
  306. var v schemaVersion
  307. err := tx.Get(&v, `
  308. SELECT schema_version as schemaversion, applied_at as appliedat, syncthing_version as syncthingversion FROM schemamigrations
  309. ORDER BY schema_version DESC
  310. LIMIT 1
  311. `)
  312. return v, wrap(err)
  313. }