1## This Source Code Form is subject to the terms of the Mozilla Public
2## License, v. 2.0. If a copy of the MPL was not distributed with this
3## file, You can obtain one at https://mozilla.org/MPL/2.0/.
4##
5## Copyright (c) 2007-2021 VMware, Inc. or its affiliates.  All rights reserved.
6
7defmodule RabbitMQ.CLI.Diagnostics.Commands.SchemaInfoCommand do
8  @moduledoc """
9  Lists all tables on the mnesia schema
10  """
11
12  alias RabbitMQ.CLI.Ctl.InfoKeys
13
14  @behaviour RabbitMQ.CLI.CommandBehaviour
15
16  @info_keys ~w(name snmp load_order active_replicas all_nodes attributes checkpoints disc_copies
17    disc_only_copies external_copies frag_properties master_nodes ram_copies
18    storage_properties subscribers user_properties cstruct local_content
19    where_to_commit where_to_read name access_mode cookie load_by_force
20    load_node record_name size storage_type type where_to_write index arity
21    majority memory commit_work where_to_wlock load_reason record_validation
22   version wild_pattern index_info)a
23
24  def info_keys(), do: @info_keys
25
26  def scopes(), do: [:ctl, :diagnostics]
27
28  use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout
29  use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
30
31  def merge_defaults([], opts) do
32    merge_defaults(
33      ~w(name cookie active_replicas user_properties),
34      opts
35    )
36  end
37
38  def merge_defaults(args, opts) do
39    {args, Map.merge(%{table_headers: true}, opts)}
40  end
41
42  def validate(args, _) do
43    case InfoKeys.validate_info_keys(args, @info_keys) do
44      {:ok, _} -> :ok
45      err -> err
46    end
47  end
48
49  def run([_ | _] = args, %{node: node_name, timeout: timeout}) do
50    info_keys = InfoKeys.prepare_info_keys(args)
51    :rabbit_misc.rpc_call(node_name, :rabbit_mnesia, :schema_info, [info_keys], timeout)
52  end
53
54  use RabbitMQ.CLI.DefaultOutput
55
56  def formatter(), do: RabbitMQ.CLI.Formatters.Table
57
58  def usage() do
59    "schema_info [--no-table-headers] [<column> ...]"
60  end
61
62  def usage_additional() do
63    [
64      ["<column>", "must be one of " <> Enum.join(Enum.sort(@info_keys), ", ")]
65    ]
66  end
67
68  def help_section(), do: :observability_and_health_checks
69
70  def description(), do: "Lists schema database tables and their properties"
71
72  def banner(_, %{node: node_name}), do: "Asking node #{node_name} to report its schema..."
73end
74