-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
flavours/classic/repo/migrations/20231119004947_boundaries_users_fixtures.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Bonfire.Boundaries.Repo.Migrations.BoundariesUsersFixturesUp do | ||
alias EctoSparkles.DataMigration | ||
use DataMigration | ||
|
||
@impl DataMigration | ||
def base_query do | ||
# NOTE: This works in cases where: | ||
# 1. The data can be queried with a condition that not longer applies after the migration ran, so you can repeatedly query the data and update the data until the query result is empty. For example, if a column is currently null and will be updated to not be null, then you can query for the null records and pick up where you left off. | ||
# 2. The migration is written in such a way that it can be ran several times on the same data without causing data loss or duplication (or crashing). | ||
|
||
# Notice how we do not use Ecto schemas here. | ||
from(u in Bonfire.Data.Identity.User, | ||
select: %{id: u.id} | ||
) | ||
end | ||
|
||
@impl DataMigration | ||
def config do | ||
%DataMigration.Config{ | ||
# do not block app startup in auto-migrations | ||
async: true, | ||
# users at a time | ||
batch_size: 1000, | ||
# wait a sec | ||
throttle_ms: 1_000, | ||
repo: Bonfire.Common.Repo, | ||
first_id: "00000000000000000000000000" | ||
} | ||
end | ||
|
||
@impl DataMigration | ||
def migrate(results) do | ||
Enum.each(results, fn user -> | ||
# hooks into a context module, which is more likely to be kept up to date as the app evolves, to avoid having to update old migrations | ||
Bonfire.Boundaries.Users.create_missing_boundaries(user) | ||
# can hae some extra throttling here | ||
Process.sleep(100) | ||
end) | ||
end | ||
end |