~ihabunek/triglav

447e9869e2e0c07f789d2c1dbe68c0b1891e72ba — Ivan Habunek 8 months ago 90eeef6
Ask confirmation before nuking osmosis schema
M lib/mix/tasks/triglav/osmosis_init.ex => lib/mix/tasks/triglav/osmosis_init.ex +16 -2
@@ 1,6 1,8 @@
defmodule Mix.Tasks.Triglav.OsmosisInit do
  use Mix.Task

  alias Triglav.Import.Osmosis

  @shortdoc "(re)Creates the Osmosis schema and loads the initial data set"

  @impl Mix.Task


@@ 8,7 10,19 @@ defmodule Mix.Tasks.Triglav.OsmosisInit do
    Application.put_env(:triglav, :minimal, true)
    {:ok, _} = Application.ensure_all_started(:triglav)

    Triglav.Import.Osmosis.create_schema()
    Triglav.Import.Osmosis.load_initial()
    if not Osmosis.schema_exists?() or confirm() do
      Osmosis.create_schema()
      Osmosis.load_initial()
    else
      IO.puts("Aborted")
    end
  end

  defp confirm() do
    response =
      IO.gets("\nOsmosis schema already exists. This will overwrite it. Are you sure? [y/N]: ")
      |> String.trim()

    String.downcase(response) == "y"
  end
end

M lib/mix/tasks/triglav/osmosis_load.ex => lib/mix/tasks/triglav/osmosis_load.ex +16 -2
@@ 1,6 1,8 @@
defmodule Mix.Tasks.Triglav.OsmosisLoad do
  use Mix.Task

  alias Triglav.Import.Osmosis

  @shortdoc "(re)Creates the Osmosis schema and loads a given pbf file"

  @impl Mix.Task


@@ 8,7 10,19 @@ defmodule Mix.Tasks.Triglav.OsmosisLoad do
    Application.put_env(:triglav, :minimal, true)
    {:ok, _} = Application.ensure_all_started(:triglav)

    Triglav.Import.Osmosis.create_schema()
    Triglav.Import.Osmosis.load_file(pbf_path)
    if not Osmosis.schema_exists?() or confirm() do
      Osmosis.create_schema()
      Osmosis.load_file(pbf_path)
    else
      IO.puts("Aborted")
    end
  end

  defp confirm() do
    response =
      IO.gets("\nOsmosis schema already exists. This will overwrite it. Are you sure? [y/N]: ")
      |> String.trim()

    String.downcase(response) == "y"
  end
end

M lib/triglav/release.ex => lib/triglav/release.ex +10 -6
@@ 25,18 25,22 @@ defmodule Triglav.Release do
  end

  def osmosis_init() do
    start_repo()
    Triglav.Import.Osmosis.create_schema()
    Triglav.Import.Osmosis.load_initial()
    load_app()
    Mix.Tasks.Triglav.OsmosisInit.run([])
  end

  def osmosis_load(pbf_path) do
    load_app()
    Mix.Tasks.Triglav.OsmosisLoad.run([pbf_path])
  end

  def osmosis_update() do
    start_repo()
    Triglav.Import.Osmosis.update()
    load_app()
    Mix.Tasks.Triglav.OsmosisUpdate.run([])
  end

  def validate_routes do
    start_repo()
    load_app()
    Mix.Tasks.Triglav.ValidateRoutes.validate()
  end