فهرست منبع

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

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

7b319111d3c07f092760fd781bf16daa192d46b6

---------

Co-authored-by: Jakob Borg <[email protected]>
Simon Frei 4 ماه پیش
والد
کامیت
8b978d4712

+ 25 - 21
internal/db/sqlite/basedb.go

@@ -22,7 +22,7 @@ import (
 	"github.com/syncthing/syncthing/lib/protocol"
 )
 
-const currentSchemaVersion = 1
+const currentSchemaVersion = 2
 
 //go:embed sql/**
 var embedded embed.FS
@@ -62,6 +62,17 @@ func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScript
 		baseName:   filepath.Base(path),
 		sql:        sqlDB,
 		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 {
@@ -96,17 +107,6 @@ func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScript
 		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
 }
 
@@ -152,15 +152,8 @@ func (s *baseDB) stmt(tpl string) 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
-	stmt, err := s.sql.Preparex(sb.String())
+	stmt, err := s.sql.Preparex(s.expandTemplateVars(tpl))
 	if err != nil {
 		return failedStmt{err}
 	}
@@ -168,6 +161,17 @@ func (s *baseDB) stmt(tpl string) 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 {
 	Exec(args ...any) (sql.Result, error)
 	Get(dest any, args ...any) error
@@ -212,7 +216,7 @@ nextScript:
 		// separately. We require it on a separate line because there are
 		// also statement-internal semicolons in the triggers.
 		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)
 			}
 		}

+ 0 - 7
internal/db/sqlite/sql/migrations/01-placeholder.sql

@@ -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.

+ 20 - 0
internal/db/sqlite/sql/migrations/folder/02-remove-invalid.sql

@@ -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
+;