Skip to content

Instantly share code, notes, and snippets.

@halfdan
Last active August 14, 2023 21:17
Show Gist options
  • Select an option

  • Save halfdan/6853a8cc8994eca4c9311ecad04a0eb0 to your computer and use it in GitHub Desktop.

Select an option

Save halfdan/6853a8cc8994eca4c9311ecad04a0eb0 to your computer and use it in GitHub Desktop.

Revisions

  1. halfdan revised this gist Apr 28, 2023. No changes.
  2. halfdan created this gist Apr 28, 2023.
    31 changes: 31 additions & 0 deletions schema_checker.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    defmodule SchemaChecker do
    def find_field_discrepancies do
    schema_modules()
    |> Enum.map(fn mod ->
    {mod.__schema__(:source), check_module(mod)}
    end)
    end

    defp schema_modules do
    {:ok, modules} = :application.get_key(:core, :modules)

    modules
    |> Enum.filter(&({:__schema__, 1} in &1.__info__(:functions)))
    |> Enum.filter(&(:__meta__ in Map.keys(&1.__schema__(:loaded))))
    end

    defp check_module(module) do
    fields =
    module.__schema__(:fields)
    |> Enum.map(&module.__schema__(:field_source, &1))
    |> Enum.map(&Atom.to_string/1)

    table_name = module.__schema__(:source)

    {:ok, %{columns: columns}} = DB.Repo.query("SELECT * FROM #{table_name} LIMIT 0")

    columns -- fields
    end
    end

    SchemaChecker.find_field_discrepancies