Skip to content

User must always belong to at least one organization

What does this MR do and why?

In Cells 1.0 every user needs to belong to at least one organization. The user's first organization is added when the user is created.

This MR will:

  • Spec that the user is assigned to the default organization (spec link)
    • The callback itself has specs. My intention with this MR is to describe the relationship between user creation and associated organization specifically. In future MRs we are going to change this relationship such that a user without an organization would be invalid.
  • Prevent removal of user's last organization with model callback (spec link)
  • Remove redundant query that breaks an unrelated query counter spec and creates a red pipeline.
Database migration console dump
WARNING: This version of GitLab depends on gitlab-shell 14.34.0, but you're running 14.33.0. Please update gitlab-shell.
main: == [advisory_lock_connection] object_id: 119740, pg_backend_pid: 57378
main: == 20240318032920 EnsureUserHasOrganizationTrigger: migrating =================
main: -- current_schema(nil)
main:    -> 0.0004s
main: -- execute("CREATE OR REPLACE FUNCTION ensure_user_has_organization()\nRETURNS TRIGGER AS\n$$\nBEGIN\nIF (SELECT NOT EXISTS(SELECT 1 AS one FROM organization_users WHERE user_id = OLD.user_id)) THEN\n  RAISE EXCEPTION 'User % must belong to an organization', OLD.user_id;\nEND IF;\n\nRETURN OLD;\n\nEND\n$$ LANGUAGE PLPGSQL\n")
main:    -> 0.0051s
main: -- execute("CREATE TRIGGER ensure_user_has_organization_trigger\nAFTER DELETE ON organization_users\nFOR EACH ROW\n\nEXECUTE FUNCTION ensure_user_has_organization()\n")
main:    -> 0.0008s
main: == 20240318032920 EnsureUserHasOrganizationTrigger: migrated (0.0266s) ========

main: == [advisory_lock_connection] object_id: 119740, pg_backend_pid: 57378
ci: == [advisory_lock_connection] object_id: 119940, pg_backend_pid: 57380
ci: == 20240318032920 EnsureUserHasOrganizationTrigger: migrating =================
ci: -- current_schema(nil)
ci:    -> 0.0002s
ci: -- execute("CREATE OR REPLACE FUNCTION ensure_user_has_organization()\nRETURNS TRIGGER AS\n$$\nBEGIN\nIF (SELECT NOT EXISTS(SELECT 1 AS one FROM organization_users WHERE user_id = OLD.user_id)) THEN\n  RAISE EXCEPTION 'User % must belong to an organization', OLD.user_id;\nEND IF;\n\nRETURN OLD;\n\nEND\n$$ LANGUAGE PLPGSQL\n")
ci:    -> 0.0013s
ci: -- execute("CREATE TRIGGER ensure_user_has_organization_trigger\nAFTER DELETE ON organization_users\nFOR EACH ROW\n\nEXECUTE FUNCTION ensure_user_has_organization()\n")
ci:    -> 0.0005s
ci: == 20240318032920 EnsureUserHasOrganizationTrigger: migrated (0.0119s) ========

ci: == [advisory_lock_connection] object_id: 119940, pg_backend_pid: 57380

Closes #443873 (closed)

Edited by Alex Pooley

Merge request reports