
poelzi@79: BEGIN;
poelzi@79:  
poelzi@79: CREATE OR REPLACE VIEW "liquid_feedback_version" AS
poelzi@79:   SELECT * FROM (VALUES ('1.2.9', 1, 2, 9))
poelzi@79:   AS "subquery"("string", "major", "minor", "revision");
poelzi@79: 
poelzi@79: 
poelzi@79: ALTER TABLE "supporter" ADD "auto_support" BOOLEAN NOT NULL DEFAULT 'f';
poelzi@79: 
poelzi@80: CREATE FUNCTION VOLATILE update_supporter_drafts()
poelzi@79:   RETURNS trigger
poelzi@79:   LANGUAGE 'plpgsql' AS $$
poelzi@79:   BEGIN
poelzi@79:     UPDATE supporter SET draft_id = NEW.id 
poelzi@79:     WHERE initiative_id = NEW.initiative_id AND
poelzi@79:           (auto_support = 't' OR member_id = NEW.author_id);
poelzi@79:     RETURN new;
poelzi@79:   END
poelzi@79: $$;
poelzi@79: 
poelzi@79: COMMENT ON FUNCTION "update_supporter_drafts"() IS 'Automaticly update the supported draft_id to the latest version when auto_support is enabled';
poelzi@79: 
poelzi@79: CREATE TRIGGER "update_draft_supporter"
poelzi@79:   AFTER INSERT ON "draft"
poelzi@79:   FOR EACH ROW EXECUTE PROCEDURE
poelzi@79:   update_supporter_drafts();
poelzi@79: 
poelzi@79: CREATE FUNCTION "check_delegation"()
poelzi@79:   RETURNS TRIGGER
poelzi@79:   LANGUAGE 'plpgsql' VOLATILE AS $$
poelzi@79:   BEGIN
poelzi@79:     IF EXISTS (
poelzi@79:       SELECT NULL FROM "member" WHERE 
poelzi@79:         "id" = NEW."trustee_id" AND active = 'n'
poelzi@79:     ) THEN
poelzi@79:       RAISE EXCEPTION 'Cannot delegate to an inactive member';
poelzi@79:     END IF;
poelzi@79:     RETURN NEW;
poelzi@79:   END;
poelzi@79: $$;
poelzi@79: 
poelzi@79: CREATE TRIGGER "update_delegation"
poelzi@79:   BEFORE INSERT OR UPDATE ON "delegation"
poelzi@79:   FOR EACH ROW EXECUTE PROCEDURE
poelzi@79:   check_delegation();
poelzi@79: 
poelzi@79: CREATE OR REPLACE FUNCTION "delete_member"("member_id_p" "member"."id"%TYPE)
poelzi@79:   RETURNS VOID
poelzi@79:   LANGUAGE 'plpgsql' VOLATILE AS $$
poelzi@79:     BEGIN
poelzi@79:       UPDATE "member" SET
poelzi@79:         "last_login"                   = NULL,
poelzi@79:         "login"                        = NULL,
poelzi@79:         "password"                     = NULL,
poelzi@79:         "active"                       = FALSE,
poelzi@79:         "notify_email"                 = NULL,
poelzi@79:         "notify_email_unconfirmed"     = NULL,
poelzi@79:         "notify_email_secret"          = NULL,
poelzi@79:         "notify_email_secret_expiry"   = NULL,
poelzi@79:         "notify_email_lock_expiry"     = NULL,
poelzi@79:         "password_reset_secret"        = NULL,
poelzi@79:         "password_reset_secret_expiry" = NULL,
poelzi@79:         "organizational_unit"          = NULL,
poelzi@79:         "internal_posts"               = NULL,
poelzi@79:         "realname"                     = NULL,
poelzi@79:         "birthday"                     = NULL,
poelzi@79:         "address"                      = NULL,
poelzi@79:         "email"                        = NULL,
poelzi@79:         "xmpp_address"                 = NULL,
poelzi@79:         "website"                      = NULL,
poelzi@79:         "phone"                        = NULL,
poelzi@79:         "mobile_phone"                 = NULL,
poelzi@79:         "profession"                   = NULL,
poelzi@79:         "external_memberships"         = NULL,
poelzi@79:         "external_posts"               = NULL,
poelzi@79:         "statement"                    = NULL
poelzi@79:         WHERE "id" = "member_id_p";
poelzi@79:       -- "text_search_data" is updated by triggers
poelzi@79:       DELETE FROM "setting"            WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "setting_map"        WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "member_relation_setting" WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "member_image"       WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "contact"            WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "area_setting"       WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "issue_setting"      WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "initiative_setting" WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "suggestion_setting" WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "membership"         WHERE "member_id" = "member_id_p";
poelzi@79:       DELETE FROM "delegation"         WHERE "truster_id" = "member_id_p";
poelzi@79:       DELETE FROM "delegation"         WHERE "trustee_id" = "member_id_p";
poelzi@79:       DELETE FROM "direct_voter" USING "issue"
poelzi@79:         WHERE "direct_voter"."issue_id" = "issue"."id"
poelzi@79:         AND "issue"."closed" ISNULL
poelzi@79:         AND "member_id" = "member_id_p";
poelzi@79:       RETURN;
poelzi@79:     END;
poelzi@79:   $$;
poelzi@79: 
poelzi@79: COMMENT ON FUNCTION "delete_member"("member_id_p" "member"."id"%TYPE) IS 'Deactivate member and clear certain settings and data of this member (data protection)';
poelzi@79: 
poelzi@81: CREATE INDEX "initiative_issue_id_idx" ON "initiative" ("issue_id");
poelzi@81: 
poelzi@79: COMMIT;




