Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Aug 27, 2024
1 parent be5074a commit 8f7243a
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 190 deletions.
225 changes: 38 additions & 187 deletions lib/plausible_release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,204 +25,55 @@ defmodule Plausible.Release do
IO.puts("Migrations successful!")
end

@doc """
Unlike `migrate/0` above this function:
- lists all pending migrations across repos,
- sorts them into a single list,
- groups consequent migration into "streaks" by repo,
- migrates each repo in "streak" consequently
For examples, assuming wi have the following migrations across two repos:
priv/repo/migrations/
- 20230530161856_add_enable_feature_fields_for_site.exs
- 20230724131709_change_allowed_event_props_type.exs
- 20230802081520_cascade_delete_user.exs
- 20230914071244_fix_broken_goals.exs
- 20230914071245_goals_unique.exs
- 20230925072840_plugins_api_tokens.exs
- 20231003081927_add_user_previous_email.exs
- 20231010074900_add_unique_index_on_site_memberships_site_id_when_owner.exs
- 20231011101825_add_email_activation_codes.exs
- 20231018081657_add_last_used_at_to_plugins_api_tokens.exs
- 20231109090334_add_site_user_preferences.exs
- 20231115131025_add_limits_to_enterprise_plans.exs
- 20231115140646_add_totp_user_fields_and_recovery_codes.exs
- 20231121131602_create_plans_table.exs
- 20231127132321_remove_custom_domains.exs
- 20231129103158_add_allow_next_upgrade_override_to_users.exs
- 20231129161022_add_totp_token_to_users.exs
- 20231204151831_backfill_last_bill_date_to_subscriptions.exs
- 20231208125624_add_data_retention_in_years_to_plans.exs
- 20231211092344_add_accept_traffic_until_to_sites.exs
- 20231219083050_track_accept_traffic_until_notifcations.exs
- 20231220072829_add_accept_traffic_until_to_user.exs
- 20231220101920_backfill_accept_traffic_until.exs
- 20240103090304_upgrade_oban_jobs_to_v12.exs
- 20240123085318_add_ip_block_list_table.exs
- 20240123095646_remove_google_analytics_imports_jobs.exs
- 20240123144308_add_site_imports.exs
- 20240129102900_migrate_accepted_traffic_until.exs
- 20240129113531_backfill_accept_traffic_until_for_users_missing_notifications.exs
- 20240214114158_add_legacy_flag_to_site_imports.exs
- 20240220144655_cascade_delete_ip_rules.exs
- 20240221122626_shield_country_rules.exs
- 20240307083402_shield_page_rules.exs
- 20240319094940_add_label_to_site_imports.exs
- 20240407104659_shield_hostname_rules.exs
- 20240528115149_migrate_site_imports.exs
- 20240702055817_traffic_drop_notifications.exs
- 20240708120453_create_help_scout_credentials.exs
- 20240722143005_create_helpscout_mappings.exs
- 20240801052902_add_goal_display_name.exs
- 20240801052903_make_goal_display_names_unique.exs
- 20240809100853_turn_google_auth_tokens_into_text.exs
priv/ingest_repo/migrations/
- 20231017073642_disable_deduplication_window_for_imports.exs
- 20240123142959_add_import_id_to_imported_tables.exs
- 20240209085338_minmax_index_session_timestamp.exs
- 20240220123656_create_sessions_events_compression_options.exs
- 20240222082911_sessions_v2_versioned_collapsing_merge_tree.exs
- 20240305085310_events_sessions_columns_improved.exs
- 20240326134840_add_metrics_to_imported_tables.exs
- 20240327085855_hostnames_in_sessions.exs
- 20240419133926_add_active_visitors_to_imported_pages.exs
- 20240423094014_add_imported_custom_events.exs
- 20240502115822_alias_api_prop_names.exs
- 20240709181437_populate_location_data.exs
The migrations would happen in the following order:
priv/repo/migrations/
- 20230530161856_add_enable_feature_fields_for_site.exs
- 20230724131709_change_allowed_event_props_type.exs
- 20230802081520_cascade_delete_user.exs
- 20230914071244_fix_broken_goals.exs
- 20230914071245_goals_unique.exs
- 20230925072840_plugins_api_tokens.exs
- 20231003081927_add_user_previous_email.exs
- 20231010074900_add_unique_index_on_site_memberships_site_id_when_owner.exs
- 20231011101825_add_email_activation_codes.exs
priv/ingest_repo/migrations/
- 20231017073642_disable_deduplication_window_for_imports.exs
priv/repo/migrations/
- 20231018081657_add_last_used_at_to_plugins_api_tokens.exs
- 20231109090334_add_site_user_preferences.exs
- 20231115131025_add_limits_to_enterprise_plans.exs
- 20231115140646_add_totp_user_fields_and_recovery_codes.exs
- 20231121131602_create_plans_table.exs
- 20231127132321_remove_custom_domains.exs
- 20231129103158_add_allow_next_upgrade_override_to_users.exs
- 20231129161022_add_totp_token_to_users.exs
- 20231204151831_backfill_last_bill_date_to_subscriptions.exs
- 20231208125624_add_data_retention_in_years_to_plans.exs
- 20231211092344_add_accept_traffic_until_to_sites.exs
- 20231219083050_track_accept_traffic_until_notifcations.exs
- 20231220072829_add_accept_traffic_until_to_user.exs
- 20231220101920_backfill_accept_traffic_until.exs
- 20240103090304_upgrade_oban_jobs_to_v12.exs
- 20240123085318_add_ip_block_list_table.exs
- 20240123095646_remove_google_analytics_imports_jobs.exs
priv/ingest_repo/migrations/
- 20240123142959_add_import_id_to_imported_tables.exs
priv/repo/migrations/
- 20240123144308_add_site_imports.exs
- 20240129102900_migrate_accepted_traffic_until.exs
- 20240129113531_backfill_accept_traffic_until_for_users_missing_notifications.exs
priv/ingest_repo/migrations/
- 20240209085338_minmax_index_session_timestamp.exs
priv/repo/migrations/
- 20240214114158_add_legacy_flag_to_site_imports.exs
priv/ingest_repo/migrations/
- 20240220123656_create_sessions_events_compression_options.exs
priv/repo/migrations/
- 20240220144655_cascade_delete_ip_rules.exs
- 20240221122626_shield_country_rules.exs
priv/ingest_repo/migrations/
- 20240222082911_sessions_v2_versioned_collapsing_merge_tree.exs
- 20240305085310_events_sessions_columns_improved.exs
priv/repo/migrations/
- 20240307083402_shield_page_rules.exs
- 20240319094940_add_label_to_site_imports.exs
priv/ingest_repo/migrations/
- 20240326134840_add_metrics_to_imported_tables.exs
- 20240327085855_hostnames_in_sessions.exs
priv/repo/migrations/
- 20240407104659_shield_hostname_rules.exs
priv/ingest_repo/migrations/
- 20240419133926_add_active_visitors_to_imported_pages.exs
- 20240423094014_add_imported_custom_events.exs
- 20240502115822_alias_api_prop_names.exs
priv/repo/migrations/
- 20240528115149_migrate_site_imports.exs
- 20240702055817_traffic_drop_notifications.exs
- 20240708120453_create_help_scout_credentials.exs
priv/ingest_repo/migrations/
- 20240709181437_populate_location_data.exs
priv/repo/migrations/
- 20240722143005_create_helpscout_mappings.exs
- 20240801052902_add_goal_display_name.exs
- 20240801052903_make_goal_display_names_unique.exs
- 20240809100853_turn_google_auth_tokens_into_text.exs
This approach helps resolve dependencies between migrations across repos.
"""
def interweave_migrate do
# interweave
all_pending =
Enum.flat_map(repos(), fn repo ->
Ecto.Migrator.migrations(repo)
|> Enum.filter(fn {status, _version, _name} -> status == :down end)
|> Enum.map(fn {_status, version, _name} -> {repo, version} end)
end)

# sort
all_sorted = Enum.sort_by(all_pending, fn {_repo, version} -> version end, :asc)
def interweave_migrate(repos \\ repos()) do
prepare()

# group into streaks
streaks = migration_streaks(all_sorted)
streaks = migration_streaks(repos)

# migrate the streaks
Enum.each(streaks, fn {repo, version} ->
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, to: version))
Enum.each(streaks, fn {repo, up_to_version} ->
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, to: up_to_version))
end)

IO.puts("Migrations successful!")
end

@doc false
def migration_streaks([{repo, version} | streaks]) do
migration_streaks(streaks, repo, version)
end

def migration_streaks([] = empty), do: empty
def migration_streaks(repos) do
all_pending =
Enum.flat_map(repos, fn repo ->
# credo:disable-for-lines:6 Credo.Check.Refactor.Nesting
{:ok, pending, _started} =
Ecto.Migrator.with_repo(repo, fn repo ->
Ecto.Migrator.migrations(repo)
|> Enum.filter(fn {status, _version, _name} -> status == :down end)
|> Enum.map(fn {_status, version, _name} -> {repo, version} end)
end)

pending
end)

# extend the streak
defp migration_streaks([{repo, version} | rest], repo, _prev_version) do
migration_streaks(rest, repo, version)
group_migration_streaks(all_pending)
end

# end the streak
defp migration_streaks([{repo, version} | rest], prev_repo, prev_version) do
[{prev_repo, prev_version} | migration_streaks(rest, repo, version)]
end
@doc false
def group_migration_streaks(all_pending) do
all_sorted = Enum.sort_by(all_pending, fn {_repo, version} -> version end, :asc)

streaks_reversed =
Enum.reduce(all_sorted, [], fn {repo, _version} = latest_migration, streaks_acc ->
case streaks_acc do
# start the streak for repo
[] -> [latest_migration]
# extend the streak
[{^repo, _prev_version} | rest] -> [latest_migration | rest]
# end the streak for prev_repo, start the streak for repo
[{_prev_repo, _prev_version} | _rest] -> [latest_migration | streaks_acc]
end
end)

defp migration_streaks([], repo, version), do: [{repo, version}]
:lists.reverse(streaks_reversed)
end

def pending_migrations do
prepare()
Expand Down
2 changes: 1 addition & 1 deletion rel/overlays/migrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

BIN_DIR=$(dirname "$0")

"${BIN_DIR}"/bin/plausible eval Plausible.Release.migrate
"${BIN_DIR}"/bin/plausible eval Plausible.Release.interweave_migrate
Loading

0 comments on commit 8f7243a

Please sign in to comment.