30-indexids.sql 1.0 KB

123456789101112131415161718192021222324
  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. -- indexids holds the index ID and maximum sequence for a given device and folder
  7. CREATE TABLE IF NOT EXISTS indexids (
  8. device_idx INTEGER NOT NULL,
  9. folder_idx INTEGER NOT NULL,
  10. index_id TEXT NOT NULL COLLATE BINARY,
  11. sequence INTEGER NOT NULL DEFAULT 0,
  12. PRIMARY KEY(device_idx, folder_idx),
  13. FOREIGN KEY(folder_idx) REFERENCES folders(idx) ON DELETE CASCADE,
  14. FOREIGN KEY(device_idx) REFERENCES devices(idx) ON DELETE CASCADE
  15. ) STRICT, WITHOUT ROWID
  16. ;
  17. CREATE TRIGGER IF NOT EXISTS indexids_seq AFTER INSERT ON files
  18. BEGIN
  19. INSERT INTO indexids (folder_idx, device_idx, index_id, sequence)
  20. VALUES (NEW.folder_idx, NEW.device_idx, "", COALESCE(NEW.remote_sequence, NEW.sequence))
  21. ON CONFLICT DO UPDATE SET sequence = COALESCE(NEW.remote_sequence, NEW.sequence);
  22. END
  23. ;