View Source ExcellentMigrations.DangersDetector (excellent_migrations v0.1.5)

This module finds potentially dangerous or destructive database operations in a given migration AST.

Link to this section Summary

Functions

Traverses ast and finds potentially dangerous database operations. Returns keyword list containing danger types and lines where they were detected.

Link to this section Types

@type ast() :: list() | tuple() | atom() | String.t()
@type danger_type() ::
  :column_added_with_default
  | :column_removed
  | :column_renamed
  | :column_type_changed
  | :index_not_concurrently
  | :many_columns_index
  | :not_null_added
  | :operation_delete
  | :operation_insert
  | :operation_update
  | :raw_sql_executed
  | :table_dropped
  | :table_renamed
  | :index_concurrently_without_disable_ddl_transaction
  | :index_concurrently_without_disable_migration_lock
@type line() :: integer()

Link to this section Functions

Link to this function

detect_dangers(ast, source_code)

View Source
@spec detect_dangers(ast(), String.t()) :: [{danger_type(), line()}]

Traverses ast and finds potentially dangerous database operations. Returns keyword list containing danger types and lines where they were detected.

parameters

Parameters

examples

Examples

    iex> source_code = """
    ...>   alter table("dumplings") do
    ...>     remove(:taste, :string)
    ...>     remove(:stuffing, :string)
    ...>   end
    ...> """
    iex> ast = Code.string_to_quoted!(source_code)
    {:ok,
    {:alter, [line: 1],
    [
      {:table, [line: 1], ["dumplings"]},
      [
        do: {:__block__, [],
         [
           {:remove, [line: 2], [:taste, :string]},
           {:remove, [line: 3], [:stuffing, :string]}
         ]}
      ]
    ]}}

    iex> ExcellentMigrations.DangersDetector.detect_dangers(ast, source_code)
    [column_removed: 2, column_removed: 3]