chore: add migration for remote invalid local flag (#10174)

Follow to resp. migration for the change in this commit:

7b319111d3

---------

Co-authored-by: Jakob Borg <jakob@kastelo.net>
This commit is contained in:
Simon Frei
2025-06-13 21:28:07 +00:00
committed by GitHub
co-authored by Jakob Borg
parent 7b319111d3
commit 8b978d4712
3 changed files with 45 additions and 28 deletions
+25 -21
View File
@@ -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)
} }
} }
@@ -1,7 +0,0 @@
-- Copyright (C) 2025 The Syncthing Authors.
--
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this file,
-- You can obtain one at https://mozilla.org/MPL/2.0/.
-- The next migration should be number two.
@@ -0,0 +1,20 @@
-- Copyright (C) 2025 The Syncthing Authors.
--
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this file,
-- You can obtain one at https://mozilla.org/MPL/2.0/.
-- Remote files with the invalid bit instead gain the RemoteInvalid local
-- flag.
UPDATE files
SET local_flags = local_flags | {{.FlagLocalRemoteInvalid}}
FROM (
SELECT idx FROM devices
WHERE device_id = '7777777-777777N-7777777-777777N-7777777-777777N-7777777-77777Q4'
) AS local_device
WHERE invalid AND device_idx != local_device.idx
;
-- The invalid column goes away.
ALTER TABLE files DROP COLUMN invalid
;