Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Listing Crate Owner Invitations #959

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions migrations/20170812041508_crate_owner_invitations/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TRIGGER trigger_ensure_single_crate_owner_invitation ON crate_owner_invitations;
DROP FUNCTION ensure_single_crate_owner_invitation();
DROP TABLE crate_owner_invitations;
36 changes: 36 additions & 0 deletions migrations/20170812041508_crate_owner_invitations/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
CREATE TABLE crate_owner_invitations (
id SERIAL PRIMARY KEY,
invited_user_id INTEGER NOT NULL REFERENCES users (id),
invited_by INTEGER NOT NULL REFERENCES users (id),
crate_id INTEGER NOT NULL REFERENCES crates (id) ON DELETE CASCADE,
status TEXT NOT NULL,
created TIMESTAMP NOT NULL DEFAULT now()
);

CREATE FUNCTION ensure_single_crate_owner_invitation() RETURNS trigger AS $$
DECLARE
old_id INTEGER;
BEGIN
IF TG_OP = 'UPDATE' THEN
old_id = OLD.id;
ELSE
old_id = -1;
END IF;

-- If a pending invitation already exists for this same invited user and crate
IF EXISTS (
SELECT 1 FROM crate_owner_invitations
WHERE id != old_id AND
invited_user_id = NEW.invited_user_id AND
crate_id = NEW.crate_id AND
status = 'pending'
) THEN
RAISE EXCEPTION 'cannot invite a user to the same crate more than once';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_ensure_single_crate_owner_invitation
BEFORE INSERT OR UPDATE ON crate_owner_invitations
FOR EACH ROW EXECUTE PROCEDURE ensure_single_crate_owner_invitation();
11 changes: 11 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ table! {
}
}

table! {
crate_owner_invitations (id) {
id -> Int4,
invited_user_id -> Int4,
invited_by -> Int4,
crate_id -> Int4,
status -> Text,
created -> Timestamp,
}
}

table! {
crate_owners (crate_id, owner_id, owner_kind) {
crate_id -> Int4,
Expand Down