Backfill placeholder users that miss an organization

What does this MR do and why?

We found about 50k users of type placeholder that are not related to the Default Organization.

After fixing the root cause, this migration will fix the data.

It retrieves all the users with user_type = placeholder . For each user, it will check if an organization has been assigned. If not, the migration will assign one.

Database Queries

I see results that are pretty good (2 ms).

SELECT "users"."id" 
  FROM "users" 
  WHERE "users"."user_type" = 15 AND "users"."id" > 96880 
ORDER BY "users"."id" ASC 
LIMIT 100

References

Create a background migration for fixing the pl... (#546659 - closed)

How to set up and validate locally

Tested by creating test users.

  • Generate test users
  • Check number of good and bad records
  • Run migration
    load 'db/post_migrate/20250604090226_assign_organization_to_placeholder_users.rb'
    AssignOrganizationToPlaceholderUsers.new.up
  • Check number of good and bad records again

Test query. Output should be two lines that both have a true value for column has_org

WITH generated_users AS (
	SELECT u.id, u.username, u.user_type, u.created_at, ou.id IS NOT NULL AS has_org
	FROM users u 
	LEFT JOIN organization_users ou ON ou.user_id = u.id AND ou.organization_id  = 1
	WHERE u.username LIKE 'generated-user%'
)
SELECT has_org, count(*) FROM generated_users GROUP BY 1 ORDER BY 1

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #546659 (closed)

Edited by Rutger Wessels

Merge request reports

Loading