1
0

05-normalize-files.sql 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. -- Grab all unique names into the names table
  7. INSERT INTO file_names (idx, name) SELECT DISTINCT null, name FROM files
  8. ;
  9. -- Grab all unique versions into the versions table
  10. INSERT INTO file_versions (idx, version) SELECT DISTINCT null, version FROM files
  11. ;
  12. -- Create the new files table
  13. DROP TABLE IF EXISTS files_v5
  14. ;
  15. CREATE TABLE files_v5 (
  16. device_idx INTEGER NOT NULL,
  17. sequence INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  18. remote_sequence INTEGER,
  19. name_idx INTEGER NOT NULL, -- changed
  20. type INTEGER NOT NULL,
  21. modified INTEGER NOT NULL,
  22. size INTEGER NOT NULL,
  23. version_idx INTEGER NOT NULL, -- changed
  24. deleted INTEGER NOT NULL,
  25. local_flags INTEGER NOT NULL,
  26. blocklist_hash BLOB,
  27. FOREIGN KEY(device_idx) REFERENCES devices(idx) ON DELETE CASCADE,
  28. FOREIGN KEY(name_idx) REFERENCES file_names(idx), -- added
  29. FOREIGN KEY(version_idx) REFERENCES file_versions(idx) -- added
  30. ) STRICT
  31. ;
  32. -- Populate the new files table and move it in place
  33. INSERT INTO files_v5
  34. SELECT f.device_idx, f.sequence, f.remote_sequence, n.idx as name_idx, f.type, f.modified, f.size, v.idx as version_idx, f.deleted, f.local_flags, f.blocklist_hash
  35. FROM files f
  36. INNER JOIN file_names n ON n.name = f.name
  37. INNER JOIN file_versions v ON v.version = f.version
  38. ;
  39. DROP TABLE files
  40. ;
  41. ALTER TABLE files_v5 RENAME TO files
  42. ;