
jbe@57: BEGIN;
jbe@57: 
jbe@57: CREATE OR REPLACE VIEW "liquid_feedback_version" AS
jbe@57:   SELECT * FROM (VALUES ('1.2.0', 1, 2, 0))
jbe@57:   AS "subquery"("string", "major", "minor", "revision");
jbe@57: 
jbe@57: ALTER TABLE "member_history" DROP COLUMN "login";
jbe@57: 
jbe@57: COMMENT ON TABLE "member_history" IS 'Filled by trigger; keeps information about old names and active flag of members';
jbe@57: COMMENT ON COLUMN "member_history"."until" IS 'Timestamp until the data was valid';
jbe@57: 
jbe@57: CREATE OR REPLACE FUNCTION "write_member_history_trigger"()
jbe@57:   RETURNS TRIGGER
jbe@57:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@57:     BEGIN
jbe@57:       IF
jbe@57:         NEW."active" != OLD."active" OR
jbe@57:         NEW."name"   != OLD."name"
jbe@57:       THEN
jbe@57:         INSERT INTO "member_history"
jbe@57:           ("member_id", "active", "name")
jbe@57:           VALUES (NEW."id", OLD."active", OLD."name");
jbe@57:       END IF;
jbe@57:       RETURN NULL;
jbe@57:     END;
jbe@57:   $$;
jbe@57: 
jbe@57: COMMENT ON TRIGGER "write_member_history" ON "member" IS 'When changing certain fields of a member, create a history entry in "member_history" table';
jbe@57: 
jbe@57: CREATE OR REPLACE FUNCTION "delete_member"("member_id_p" "member"."id"%TYPE)
jbe@57:   RETURNS VOID
jbe@57:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@57:     BEGIN
jbe@57:       UPDATE "member" SET
jbe@57:         "last_login"                   = NULL,
jbe@57:         "login"                        = NULL,
jbe@57:         "password"                     = NULL,
jbe@57:         "active"                       = FALSE,
jbe@57:         "notify_email"                 = NULL,
jbe@57:         "notify_email_unconfirmed"     = NULL,
jbe@57:         "notify_email_secret"          = NULL,
jbe@57:         "notify_email_secret_expiry"   = NULL,
jbe@57:         "notify_email_lock_expiry"     = NULL,
jbe@57:         "password_reset_secret"        = NULL,
jbe@57:         "password_reset_secret_expiry" = NULL,
jbe@57:         "organizational_unit"          = NULL,
jbe@57:         "internal_posts"               = NULL,
jbe@57:         "realname"                     = NULL,
jbe@57:         "birthday"                     = NULL,
jbe@57:         "address"                      = NULL,
jbe@57:         "email"                        = NULL,
jbe@57:         "xmpp_address"                 = NULL,
jbe@57:         "website"                      = NULL,
jbe@57:         "phone"                        = NULL,
jbe@57:         "mobile_phone"                 = NULL,
jbe@57:         "profession"                   = NULL,
jbe@57:         "external_memberships"         = NULL,
jbe@57:         "external_posts"               = NULL,
jbe@57:         "statement"                    = NULL
jbe@57:         WHERE "id" = "member_id_p";
jbe@57:       -- "text_search_data" is updated by triggers
jbe@57:       DELETE FROM "setting"            WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "setting_map"        WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "member_relation_setting" WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "member_image"       WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "contact"            WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "area_setting"       WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "issue_setting"      WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "initiative_setting" WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "suggestion_setting" WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "membership"         WHERE "member_id" = "member_id_p";
jbe@57:       DELETE FROM "delegation"         WHERE "truster_id" = "member_id_p";
jbe@57:       DELETE FROM "direct_voter" USING "issue"
jbe@57:         WHERE "direct_voter"."issue_id" = "issue"."id"
jbe@57:         AND "issue"."closed" ISNULL
jbe@57:         AND "member_id" = "member_id_p";
jbe@57:       RETURN;
jbe@57:     END;
jbe@57:   $$;
jbe@57: 
jbe@57: 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)';
jbe@57: 
jbe@57: CREATE OR REPLACE FUNCTION "delete_private_data"()
jbe@57:   RETURNS VOID
jbe@57:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@57:     BEGIN
jbe@57:       UPDATE "member" SET
jbe@57:         "last_login"                   = NULL,
jbe@57:         "login"                        = NULL,
jbe@57:         "password"                     = NULL,
jbe@57:         "notify_email"                 = NULL,
jbe@57:         "notify_email_unconfirmed"     = NULL,
jbe@57:         "notify_email_secret"          = NULL,
jbe@57:         "notify_email_secret_expiry"   = NULL,
jbe@57:         "notify_email_lock_expiry"     = NULL,
jbe@57:         "password_reset_secret"        = NULL,
jbe@57:         "password_reset_secret_expiry" = NULL,
jbe@57:         "organizational_unit"          = NULL,
jbe@57:         "internal_posts"               = NULL,
jbe@57:         "realname"                     = NULL,
jbe@57:         "birthday"                     = NULL,
jbe@57:         "address"                      = NULL,
jbe@57:         "email"                        = NULL,
jbe@57:         "xmpp_address"                 = NULL,
jbe@57:         "website"                      = NULL,
jbe@57:         "phone"                        = NULL,
jbe@57:         "mobile_phone"                 = NULL,
jbe@57:         "profession"                   = NULL,
jbe@57:         "external_memberships"         = NULL,
jbe@57:         "external_posts"               = NULL,
jbe@57:         "statement"                    = NULL;
jbe@57:       -- "text_search_data" is updated by triggers
jbe@57:       DELETE FROM "invite_code";
jbe@57:       DELETE FROM "setting";
jbe@57:       DELETE FROM "setting_map";
jbe@57:       DELETE FROM "member_relation_setting";
jbe@57:       DELETE FROM "member_image";
jbe@57:       DELETE FROM "contact";
jbe@57:       DELETE FROM "session";
jbe@57:       DELETE FROM "area_setting";
jbe@57:       DELETE FROM "issue_setting";
jbe@57:       DELETE FROM "initiative_setting";
jbe@57:       DELETE FROM "suggestion_setting";
jbe@57:       DELETE FROM "direct_voter" USING "issue"
jbe@57:         WHERE "direct_voter"."issue_id" = "issue"."id"
jbe@57:         AND "issue"."closed" ISNULL;
jbe@57:       RETURN;
jbe@57:     END;
jbe@57:   $$;
jbe@57: 
jbe@57: COMMIT;




