database.sql 931 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. -- weatherdata database setup
  2. CREATE TABLE IF NOT EXISTS data (
  3. timestamp timestamp with time zone PRIMARY KEY,
  4. temp double precision,
  5. humidity double precision,
  6. pressure double precision,
  7. battery double precision
  8. );
  9. -- update data table from previous versions
  10. DO $$
  11. BEGIN
  12. BEGIN
  13. ALTER TABLE "data" ADD COLUMN "pressure"
  14. double precision;
  15. EXCEPTION WHEN duplicate_column THEN
  16. RAISE NOTICE
  17. 'column "pressure" already exists in "data".';
  18. END;
  19. END;
  20. $$;
  21. DO $$
  22. BEGIN
  23. BEGIN
  24. ALTER TABLE "data" ADD COLUMN "battery"
  25. double precision;
  26. EXCEPTION WHEN duplicate_column THEN
  27. RAISE NOTICE
  28. 'column "battery" already exists in "data".';
  29. END;
  30. END;
  31. $$;
  32. -- aggregate error messages for some time in the db
  33. CREATE TABLE IF NOT EXISTS errors (
  34. device varchar(36),
  35. timestamp timestamp with time zone DEFAULT NOW() NOT NULL,
  36. error text NOT NULL,
  37. PRIMARY KEY (device, timestamp)
  38. );