Skip to content

Commit

Permalink
DataMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Nov 19, 2023
1 parent 45d838e commit bb7da69
Showing 1 changed file with 40 additions and 0 deletions.
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

0 comments on commit bb7da69

Please sign in to comment.