20-files.sql 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_idx INTEGER NOT NULL,
  28. type INTEGER NOT NULL, -- protocol.FileInfoType
  29. modified INTEGER NOT NULL, -- Unix nanos
  30. size INTEGER NOT NULL,
  31. version_idx INTEGER NOT NULL,
  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. FOREIGN KEY(name_idx) REFERENCES file_names(idx),
  37. FOREIGN KEY(version_idx) REFERENCES file_versions(idx)
  38. ) STRICT
  39. ;
  40. CREATE TABLE IF NOT EXISTS file_names (
  41. idx INTEGER NOT NULL PRIMARY KEY,
  42. name TEXT NOT NULL UNIQUE COLLATE BINARY
  43. ) STRICT
  44. ;
  45. CREATE TABLE IF NOT EXISTS file_versions (
  46. idx INTEGER NOT NULL PRIMARY KEY,
  47. version TEXT NOT NULL UNIQUE COLLATE BINARY
  48. ) STRICT
  49. ;
  50. -- FileInfos store the actual protobuf object. We do this separately to keep
  51. -- the files rows smaller and more efficient.
  52. CREATE TABLE IF NOT EXISTS fileinfos (
  53. sequence INTEGER NOT NULL PRIMARY KEY, -- our local database sequence from the files table
  54. fiprotobuf BLOB NOT NULL,
  55. FOREIGN KEY(sequence) REFERENCES files(sequence) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  56. ) STRICT
  57. ;
  58. -- There can be only one file per folder, device, and remote sequence number
  59. CREATE UNIQUE INDEX IF NOT EXISTS files_remote_sequence ON files (device_idx, remote_sequence)
  60. WHERE remote_sequence IS NOT NULL
  61. ;
  62. -- There can be only one file per folder, device, and name
  63. CREATE UNIQUE INDEX IF NOT EXISTS files_device_name ON files (device_idx, name_idx)
  64. ;
  65. -- We want to be able to look up & iterate files based on blocks hash
  66. CREATE INDEX IF NOT EXISTS files_blocklist_hash_only ON files (blocklist_hash, device_idx) WHERE blocklist_hash IS NOT NULL
  67. ;
  68. -- We need to look by name_idx or version_idx for garbage collection.
  69. -- This will fail pre-migration for v4 schemas, which is fine.
  70. -- syncthing:ignore-failure
  71. CREATE INDEX IF NOT EXISTS files_name_idx_only ON files (name_idx)
  72. ;
  73. -- This will fail pre-migration for v4 schemas, which is fine.
  74. -- syncthing:ignore-failure
  75. CREATE INDEX IF NOT EXISTS files_version_idx_only ON files (version_idx)
  76. ;