Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/725
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Nov 19, 2023
1 parent c5be2d9 commit eb5aa40
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
- name: Compile deps & app
run: mix compile
- name: Set up database
run: mix ecto.setup
run: mix ecto.create && mix ecto.migrate
- name: Run tests
run: mix test
- name: Check database down migrations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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: 100,
# 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)
end)
end
end
6 changes: 5 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ dev-bg: init
# Run latest database migrations (eg. after adding/upgrading an app/extension)
db-migrate:
just mix "ecto.migrate"
# just mix "excellent_migrations.migrate"

db-migration-checks:
just mix "excellent_migrations.migrate"

# Run latest database seeds (eg. inserting required data after adding/upgrading an app/extension)
db-seeds: db-migrate
Expand Down Expand Up @@ -234,7 +238,7 @@ update: init update-repo
just update-deps
just mix deps.get
just deps-post-get
just mix "ecto.migrate"
just mix "excellent_migrations.migrate"
just js-deps-get
just mix compile

Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Bonfire.Umbrella.MixProject do
# {:mix_unused, "~> 0.4", only: :dev}, # find unused public functions
{:ex_doc, "~> 0.30.3", only: [:dev, :test], runtime: false},
{:ecto_erd, "~> 0.4", only: :dev},
{:excellent_migrations, "~> 0.1", only: [:dev, :test], runtime: false},
# {:ecto_dev_logger, "~> 0.7", only: :dev},
# flame graphs in live_dashboard
# {:flame_on, "~> 0.5", only: :dev},
Expand Down
Loading

0 comments on commit eb5aa40

Please sign in to comment.