From 8b978d471225c8d7b835285f6599dc67e64a62ff Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Fri, 13 Jun 2025 21:28:07 +0000 Subject: [PATCH] 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 --- internal/db/sqlite/basedb.go | 46 ++++++++++--------- .../sqlite/sql/migrations/01-placeholder.sql | 7 --- .../migrations/folder/02-remove-invalid.sql | 20 ++++++++ 3 files changed, 45 insertions(+), 28 deletions(-) delete mode 100644 internal/db/sqlite/sql/migrations/01-placeholder.sql create mode 100644 internal/db/sqlite/sql/migrations/folder/02-remove-invalid.sql diff --git a/internal/db/sqlite/basedb.go b/internal/db/sqlite/basedb.go index 1c69d9548..9343e28b8 100644 --- a/internal/db/sqlite/basedb.go +++ b/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) } } diff --git a/internal/db/sqlite/sql/migrations/01-placeholder.sql b/internal/db/sqlite/sql/migrations/01-placeholder.sql deleted file mode 100644 index 30ec3e8c3..000000000 --- a/internal/db/sqlite/sql/migrations/01-placeholder.sql +++ /dev/null @@ -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. \ No newline at end of file diff --git a/internal/db/sqlite/sql/migrations/folder/02-remove-invalid.sql b/internal/db/sqlite/sql/migrations/folder/02-remove-invalid.sql new file mode 100644 index 000000000..08f6036c3 --- /dev/null +++ b/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 +;