Files
fizzy/script/migrations/20250924-populate-identities.rb
T
Mike Dalessio d41d50d52b Account.sole → Current.account
and some other de-tenant changes, including removing the controller
tenanting concerns
2025-11-17 09:11:40 -05:00

30 lines
1.2 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require_relative "../../config/environment"
ApplicationRecord.with_each_tenant do |tenant|
puts "# #{tenant}"
User.find_each do |user|
next if user.system? || !user.active?
if user.membership.present?
puts "Found identity #{user.identity.id} for user #{user.id} (#{user.email_address})"
else
memberships = Membership.where(email_address: user.email_address)
if memberships.empty?
# Create a new Identity
Identity.transaction do
identity = Identity.create!
user.membership = identity.memberships.create!(user_id: user.id, user_tenant: user.tenant, email_address: user.email_address, account_name: Current.account.name)
puts "Created identity #{identity.id} for user #{user.id} (#{user.email_address})"
end
else
# Merge this User's Membership into the existing Identity
identity = memberships.first.identity
user.membership = identity.memberships.create!(user_id: user.id, user_tenant: user.tenant, email_address: user.email_address, account_name: Current.account.name)
puts "Merged membership for user #{user.id} (#{user.email_address}) into identity #{identity.id}"
end
end
end
end