You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

10_websem-project.sql 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. CREATE OR REPLACE FUNCTION now_utc() RETURNS timestamp without time zone
  2. LANGUAGE sql
  3. AS $$
  4. SELECT now() at time zone 'utc'
  5. $$;
  6. CREATE OR REPLACE FUNCTION lu_on_row_update() RETURNS trigger
  7. LANGUAGE plpgsql
  8. AS $$
  9. BEGIN
  10. NEW.updated_at := now_utc();
  11. INSERT INTO lu_entities_history ("table", "data") VALUES(TG_TABLE_NAME, to_json(OLD));
  12. return NEW;
  13. END
  14. $$;
  15. CREATE OR REPLACE FUNCTION lu_on_row_update_no_history() RETURNS trigger
  16. LANGUAGE plpgsql
  17. AS $$
  18. BEGIN
  19. NEW.updated_at := now_utc();
  20. return NEW;
  21. END
  22. $$;
  23. CREATE TABLE articles (
  24. id character varying(255) NOT NULL,
  25. text text NOT NULL,
  26. picture_url text,
  27. picture_caption text,
  28. type character varying(255) NOT NULL,
  29. created_at timestamp without time zone DEFAULT now_utc() NOT NULL,
  30. updated_at timestamp without time zone
  31. );
  32. CREATE TABLE articles_fields (
  33. article_id character varying(255) NOT NULL,
  34. property character varying(255) NOT NULL,
  35. "value" text NOT NULL,
  36. type character varying(255) NOT NULL
  37. );
  38. ALTER TABLE articles
  39. ADD CONSTRAINT articles_pkey PRIMARY KEY (id);
  40. ALTER TABLE articles_fields
  41. ADD CONSTRAINT articles_fields_pkey PRIMARY KEY (article_id, property);
  42. ALTER TABLE articles_fields
  43. ADD CONSTRAINT articles_fields_article_id_fkey FOREIGN KEY (article_id) REFERENCES articles(id) ON UPDATE CASCADE ON DELETE CASCADE;
  44. CREATE TRIGGER articles_lu_on_row_update_no_history
  45. BEFORE UPDATE ON articles
  46. FOR EACH ROW
  47. EXECUTE PROCEDURE lu_on_row_update_no_history();