- @doc "Approximate count for current iteration (including processed records count)"
- def count(force \\ false, timeout \\ :infinity) do
- stored_count = get_stat(:count)
-
- if stored_count && !force do
- stored_count
- else
- processed_count = get_stat(:processed_count, 0)
- max_processed_id = get_stat(:max_processed_id, 0)
- query = where(query(), [object], object.id > ^max_processed_id)
-
- count = Repo.aggregate(query, :count, :id, timeout: timeout) + processed_count
- put_stat(:count, count)
- persist_state()
-
- count
- end
- end
-
- defp on_complete(data_migration) do
- cond do
- data_migration.feature_lock ->
- :noop
-
- not is_nil(feature_state()) ->
- :noop
-
- true ->
- Config.put(@feature_config_path, true)
- :ok
- end
- end
-
- def failed_objects_query do
- from(o in Object)
- |> join(:inner, [o], dmf in fragment("SELECT * FROM data_migration_failed_ids"),
- on: dmf.record_id == o.id
- )
- |> where([_o, dmf], dmf.data_migration_id == ^data_migration_id())
- |> order_by([o], asc: o.id)
- end
-
- def failures_count do
- with {:ok, %{rows: [[count]]}} <-
- Repo.query(
- "SELECT COUNT(record_id) FROM data_migration_failed_ids WHERE data_migration_id = $1;",
- [data_migration_id()]
- ) do
- count
- end
- end
-