20-files.sql 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. folder_idx INTEGER NOT NULL,
  25. device_idx INTEGER NOT NULL, -- actual device ID or LocalDeviceID
  26. sequence INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, -- our local database sequence, for each and every entry
  27. remote_sequence INTEGER, -- remote device's sequence number, null for local or synthetic entries
  28. name TEXT NOT NULL COLLATE BINARY,
  29. type INTEGER NOT NULL, -- protocol.FileInfoType
  30. modified INTEGER NOT NULL, -- Unix nanos
  31. size INTEGER NOT NULL,
  32. version TEXT NOT NULL COLLATE BINARY,
  33. deleted INTEGER NOT NULL, -- boolean
  34. invalid INTEGER NOT NULL, -- boolean
  35. local_flags INTEGER NOT NULL,
  36. blocklist_hash BLOB, -- null when there are no blocks
  37. FOREIGN KEY(device_idx) REFERENCES devices(idx) ON DELETE CASCADE,
  38. FOREIGN KEY(folder_idx) REFERENCES folders(idx) ON DELETE CASCADE
  39. ) STRICT
  40. ;
  41. -- FileInfos store the actual protobuf object. We do this separately to keep
  42. -- the files rows smaller and more efficient.
  43. CREATE TABLE IF NOT EXISTS fileinfos (
  44. sequence INTEGER NOT NULL PRIMARY KEY, -- our local database sequence from the files table
  45. fiprotobuf BLOB NOT NULL,
  46. FOREIGN KEY(sequence) REFERENCES files(sequence) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  47. ) STRICT
  48. ;
  49. -- There can be only one file per folder, device, and remote sequence number
  50. CREATE UNIQUE INDEX IF NOT EXISTS files_remote_sequence ON files (folder_idx, device_idx, remote_sequence)
  51. WHERE remote_sequence IS NOT NULL
  52. ;
  53. -- There can be only one file per folder, device, and name
  54. CREATE UNIQUE INDEX IF NOT EXISTS files_device_name ON files (folder_idx, device_idx, name)
  55. ;
  56. -- We want to be able to look up & iterate files based on just folder and name
  57. CREATE INDEX IF NOT EXISTS files_name_only ON files (folder_idx, name)
  58. ;
  59. -- We want to be able to look up & iterate files based on blocks hash
  60. CREATE INDEX IF NOT EXISTS files_blocklist_hash_only ON files (blocklist_hash, device_idx, folder_idx) WHERE blocklist_hash IS NOT NULL
  61. ;