20-files.sql 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. -- Files
  7. --
  8. -- The files table contains all files announced by any device. Files present
  9. -- on this device are filed under the LocalDeviceID, not the actual current
  10. -- device ID, for simplicity, consistency and portability. One announced
  11. -- version of each file is considered the "global" version - the latest one,
  12. -- that all other devices strive to replicate. This instance gets the Global
  13. -- flag bit set. There may be other identical instances of this file
  14. -- announced by other devices, but only one onstance gets the Global flag;
  15. -- this simplifies accounting. If the current device has the Global version,
  16. -- the LocalDeviceID instance of the file is the one that has the Global
  17. -- bit.
  18. --
  19. -- If the current device does not have that version of the file it gets the
  20. -- Need bit set. Only Global files announced by another device can have the
  21. -- Need bit. This allows for very efficient lookup of files needing handling
  22. -- on this device, which is a common query.
  23. CREATE TABLE IF NOT EXISTS files (
  24. device_idx INTEGER NOT NULL, -- actual device ID or LocalDeviceID
  25. sequence INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, -- our local database sequence, for each and every entry
  26. remote_sequence INTEGER, -- remote device's sequence number, null for local or synthetic entries
  27. name TEXT NOT NULL COLLATE BINARY,
  28. type INTEGER NOT NULL, -- protocol.FileInfoType
  29. modified INTEGER NOT NULL, -- Unix nanos
  30. size INTEGER NOT NULL,
  31. version TEXT NOT NULL COLLATE BINARY,
  32. deleted INTEGER NOT NULL, -- boolean
  33. invalid INTEGER NOT NULL, -- boolean
  34. local_flags INTEGER NOT NULL,
  35. blocklist_hash BLOB, -- null when there are no blocks
  36. FOREIGN KEY(device_idx) REFERENCES devices(idx) ON DELETE CASCADE
  37. ) STRICT
  38. ;
  39. -- FileInfos store the actual protobuf object. We do this separately to keep
  40. -- the files rows smaller and more efficient.
  41. CREATE TABLE IF NOT EXISTS fileinfos (
  42. sequence INTEGER NOT NULL PRIMARY KEY, -- our local database sequence from the files table
  43. fiprotobuf BLOB NOT NULL,
  44. FOREIGN KEY(sequence) REFERENCES files(sequence) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  45. ) STRICT
  46. ;
  47. -- There can be only one file per folder, device, and remote sequence number
  48. CREATE UNIQUE INDEX IF NOT EXISTS files_remote_sequence ON files (device_idx, remote_sequence)
  49. WHERE remote_sequence IS NOT NULL
  50. ;
  51. -- There can be only one file per folder, device, and name
  52. CREATE UNIQUE INDEX IF NOT EXISTS files_device_name ON files (device_idx, name)
  53. ;
  54. -- We want to be able to look up & iterate files based on just folder and name
  55. CREATE INDEX IF NOT EXISTS files_name_only ON files (name)
  56. ;
  57. -- We want to be able to look up & iterate files based on blocks hash
  58. CREATE INDEX IF NOT EXISTS files_blocklist_hash_only ON files (blocklist_hash, device_idx) WHERE blocklist_hash IS NOT NULL
  59. ;