File size: 718 Bytes
3e21b19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
defmodule Mix.Tasks.CleanPostgres do
  @moduledoc false

  use Mix.Task

  alias Plausible.Repo

  def run(_) do
    case Plausible.Repo.start_link(pool_size: 1, log: false) do
      {:ok, _} -> :pass
      {:error, {:already_started, _pid}} -> :pass
      {:error, _} = error -> throw(error)
    end

    query = """
    SELECT tablename
    FROM pg_catalog.pg_tables
    WHERE schemaname != 'pg_catalog' AND
        schemaname != 'information_schema';
    """

    %{rows: rows} = Repo.query!(query)
    tables = Enum.map(rows, fn [table] -> table end)

    Enum.each(tables -- ["schema_migrations"], fn table ->
      Repo.query!("truncate #{table} cascade")
    end)
  after
    Plausible.Repo.stop(500)
  end
end