20251102000000_create_keyset_amounts.sql 796 B

123456789101112131415161718192021222324
  1. -- Create keyset_amounts table
  2. CREATE TABLE IF NOT EXISTS keyset_amounts (
  3. keyset_id TEXT NOT NULL,
  4. type TEXT NOT NULL,
  5. amount INTEGER NOT NULL DEFAULT 0,
  6. PRIMARY KEY (keyset_id, type)
  7. );
  8. -- Create index for faster lookups
  9. CREATE INDEX IF NOT EXISTS idx_keyset_amounts_type ON keyset_amounts(type);
  10. -- Prefill with issued amounts (sum from blind_signature where c IS NOT NULL)
  11. INSERT INTO keyset_amounts (keyset_id, type, amount)
  12. SELECT keyset_id, 'issued', COALESCE(SUM(amount), 0)
  13. FROM blind_signature
  14. WHERE c IS NOT NULL
  15. GROUP BY keyset_id;
  16. -- Prefill with redeemed amounts (sum from proof where state = 'SPENT')
  17. INSERT INTO keyset_amounts (keyset_id, type, amount)
  18. SELECT keyset_id, 'redeemed', COALESCE(SUM(amount), 0)
  19. FROM proof
  20. WHERE state = 'SPENT'
  21. GROUP BY keyset_id;