
jbe@45: BEGIN;
jbe@45: 
jbe@45: DROP VIEW "liquid_feedback_version";
jbe@45: CREATE VIEW "liquid_feedback_version" AS
jbe@45:   SELECT * FROM (VALUES ('1.0.0', 1, 0, 0))
jbe@45:   AS "subquery"("string", "major", "minor", "revision");
jbe@45:  
jbe@45: ALTER TABLE "member" ALTER COLUMN "login" DROP NOT NULL;
jbe@45: 
jbe@45: ALTER TABLE "member_history" ALTER COLUMN "login" DROP NOT NULL;
jbe@45: 
jbe@45: CREATE INDEX "member_history_member_id_idx" ON "member_history" ("member_id");
jbe@45: 
jbe@45: ALTER TABLE "direct_population_snapshot" DROP
jbe@45:   CONSTRAINT "direct_population_snapshot_member_id_fkey";
jbe@45: ALTER TABLE "direct_population_snapshot" ADD
jbe@45:   CONSTRAINT "direct_population_snapshot_member_id_fkey"
jbe@45:   FOREIGN KEY ("member_id")
jbe@45:   REFERENCES "member"("id") ON DELETE RESTRICT ON UPDATE RESTRICT;
jbe@45: 
jbe@45: ALTER TABLE "delegating_population_snapshot" DROP
jbe@45:   CONSTRAINT "delegating_population_snapshot_member_id_fkey";
jbe@45: ALTER TABLE "delegating_population_snapshot" ADD
jbe@45:   CONSTRAINT "delegating_population_snapshot_member_id_fkey"
jbe@45:   FOREIGN KEY ("member_id")
jbe@45:   REFERENCES "member"("id") ON DELETE RESTRICT ON UPDATE RESTRICT;
jbe@45: 
jbe@45: ALTER TABLE "direct_interest_snapshot" DROP
jbe@45:   CONSTRAINT "direct_interest_snapshot_member_id_fkey";
jbe@45: ALTER TABLE "direct_interest_snapshot" ADD
jbe@45:   CONSTRAINT "direct_interest_snapshot_member_id_fkey"
jbe@45:   FOREIGN KEY ("member_id")
jbe@45:   REFERENCES "member"("id") ON DELETE RESTRICT ON UPDATE RESTRICT;
jbe@45: 
jbe@45: ALTER TABLE "delegating_interest_snapshot" DROP
jbe@45:   CONSTRAINT "delegating_interest_snapshot_member_id_fkey";
jbe@45: ALTER TABLE "delegating_interest_snapshot" ADD
jbe@45:   CONSTRAINT "delegating_interest_snapshot_member_id_fkey"
jbe@45:   FOREIGN KEY ("member_id")
jbe@45:   REFERENCES "member"("id") ON DELETE RESTRICT ON UPDATE RESTRICT;
jbe@45: 
jbe@45: ALTER TABLE "direct_supporter_snapshot" DROP
jbe@45:   CONSTRAINT "direct_supporter_snapshot_member_id_fkey";
jbe@45: ALTER TABLE "direct_supporter_snapshot" ADD
jbe@45:   CONSTRAINT "direct_supporter_snapshot_member_id_fkey"
jbe@45:   FOREIGN KEY ("member_id")
jbe@45:   REFERENCES "member"("id") ON DELETE RESTRICT ON UPDATE RESTRICT;
jbe@45: 
jbe@45: ALTER TABLE "direct_voter" DROP
jbe@45:   CONSTRAINT "direct_voter_member_id_fkey";
jbe@45: ALTER TABLE "direct_voter" ADD
jbe@45:   CONSTRAINT "direct_voter_member_id_fkey"
jbe@45:   FOREIGN KEY ("member_id")
jbe@45:   REFERENCES "member"("id") ON DELETE RESTRICT ON UPDATE RESTRICT;
jbe@45: 
jbe@45: ALTER TABLE "delegating_voter" DROP
jbe@45:   CONSTRAINT "delegating_voter_member_id_fkey";
jbe@45: ALTER TABLE "delegating_voter" ADD
jbe@45:   CONSTRAINT "delegating_voter_member_id_fkey"
jbe@45:   FOREIGN KEY ("member_id")
jbe@45:   REFERENCES "member"("id") ON DELETE RESTRICT ON UPDATE RESTRICT;
jbe@45: 
jbe@45: CREATE OR REPLACE FUNCTION "write_member_history_trigger"()
jbe@45:   RETURNS TRIGGER
jbe@45:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@45:     BEGIN
jbe@45:       IF
jbe@45:         ( NEW."login" NOTNULL AND OLD."login" NOTNULL AND
jbe@45:           NEW."login" != OLD."login" ) OR
jbe@45:         ( NEW."login" NOTNULL AND OLD."login" ISNULL ) OR
jbe@45:         ( NEW."login" ISNULL AND OLD."login" NOTNULL ) OR
jbe@45:         NEW."active" != OLD."active" OR
jbe@45:         NEW."name"   != OLD."name"
jbe@45:       THEN
jbe@45:         INSERT INTO "member_history"
jbe@45:           ("member_id", "login", "active", "name")
jbe@45:           VALUES (NEW."id", OLD."login", OLD."active", OLD."name");
jbe@45:       END IF;
jbe@45:       RETURN NULL;
jbe@45:     END;
jbe@45:   $$;
jbe@45: 
jbe@45: CREATE FUNCTION "delete_member_data"("member_id_p" "member"."id"%TYPE)
jbe@45:   RETURNS VOID
jbe@45:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@45:     BEGIN
jbe@45:       UPDATE "member" SET
jbe@45:         "login"                        = NULL,
jbe@45:         "password"                     = NULL,
jbe@45:         "notify_email"                 = NULL,
jbe@45:         "notify_email_unconfirmed"     = NULL,
jbe@45:         "notify_email_secret"          = NULL,
jbe@45:         "notify_email_secret_expiry"   = NULL,
jbe@45:         "password_reset_secret"        = NULL,
jbe@45:         "password_reset_secret_expiry" = NULL,
jbe@45:         "organizational_unit"          = NULL,
jbe@45:         "internal_posts"               = NULL,
jbe@45:         "realname"                     = NULL,
jbe@45:         "birthday"                     = NULL,
jbe@45:         "address"                      = NULL,
jbe@45:         "email"                        = NULL,
jbe@45:         "xmpp_address"                 = NULL,
jbe@45:         "website"                      = NULL,
jbe@45:         "phone"                        = NULL,
jbe@45:         "mobile_phone"                 = NULL,
jbe@45:         "profession"                   = NULL,
jbe@45:         "external_memberships"         = NULL,
jbe@45:         "external_posts"               = NULL,
jbe@45:         "statement"                    = NULL
jbe@45:         WHERE "id" = "member_id_p";
jbe@45:       -- "text_search_data" is updated by triggers
jbe@45:       UPDATE "member_history" SET "login" = NULL
jbe@45:         WHERE "member_id" = "member_id_p";
jbe@45:       DELETE FROM "setting"            WHERE "member_id" = "member_id_p";
jbe@45:       DELETE FROM "setting_map"        WHERE "member_id" = "member_id_p";
jbe@45:       DELETE FROM "member_relation_setting" WHERE "member_id" = "member_id_p";
jbe@45:       DELETE FROM "member_image"       WHERE "member_id" = "member_id_p";
jbe@45:       DELETE FROM "contact"            WHERE "member_id" = "member_id_p";
jbe@45:       DELETE FROM "area_setting"       WHERE "member_id" = "member_id_p";
jbe@45:       DELETE FROM "issue_setting"      WHERE "member_id" = "member_id_p";
jbe@45:       DELETE FROM "initiative_setting" WHERE "member_id" = "member_id_p";
jbe@45:       DELETE FROM "suggestion_setting" WHERE "member_id" = "member_id_p";
jbe@45:       RETURN;
jbe@45:     END;
jbe@45:   $$;
jbe@45: COMMENT ON FUNCTION "delete_member_data"("member_id_p" "member"."id"%TYPE) IS 'Clear certain settings and data of a particular member (data protection)';
jbe@45: 
jbe@45: CREATE OR REPLACE FUNCTION "delete_private_data"()
jbe@45:   RETURNS VOID
jbe@45:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@45:     BEGIN
jbe@45:       PERFORM "delete_member_data"("id") FROM "member";
jbe@45:       DELETE FROM "invite_code";
jbe@45:       DELETE FROM "session";
jbe@45:       DELETE FROM "direct_voter" USING "issue"
jbe@45:         WHERE "direct_voter"."issue_id" = "issue"."id"
jbe@45:         AND "issue"."closed" ISNULL;
jbe@45:       RETURN;
jbe@45:     END;
jbe@45:   $$;
jbe@45: 
jbe@45: COMMIT;




