20-files.sql 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 instance 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. local_flags INTEGER NOT NULL,
  34. blocklist_hash BLOB, -- null when there are no blocks
  35. FOREIGN KEY(device_idx) REFERENCES devices(idx) ON DELETE CASCADE
  36. ) STRICT
  37. ;
  38. -- FileInfos store the actual protobuf object. We do this separately to keep
  39. -- the files rows smaller and more efficient.
  40. CREATE TABLE IF NOT EXISTS fileinfos (
  41. sequence INTEGER NOT NULL PRIMARY KEY, -- our local database sequence from the files table
  42. fiprotobuf BLOB NOT NULL,
  43. FOREIGN KEY(sequence) REFERENCES files(sequence) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  44. ) STRICT
  45. ;
  46. -- There can be only one file per folder, device, and remote sequence number
  47. CREATE UNIQUE INDEX IF NOT EXISTS files_remote_sequence ON files (device_idx, remote_sequence)
  48. WHERE remote_sequence IS NOT NULL
  49. ;
  50. -- There can be only one file per folder, device, and name
  51. CREATE UNIQUE INDEX IF NOT EXISTS files_device_name ON files (device_idx, name)
  52. ;
  53. -- We want to be able to look up & iterate files based on just folder and name
  54. CREATE INDEX IF NOT EXISTS files_name_only ON files (name)
  55. ;
  56. -- We want to be able to look up & iterate files based on blocks hash
  57. CREATE INDEX IF NOT EXISTS files_blocklist_hash_only ON files (blocklist_hash, device_idx) WHERE blocklist_hash IS NOT NULL
  58. ;