PhoenixKit.Modules.Sync.SchemaInspector (phoenix_kit v1.7.33)

Copy Markdown View Source

Inspects database schema for DB Sync module.

Discovers available tables, their columns, and metadata. Uses PostgreSQL information_schema for introspection.

Security Considerations

  • Only returns tables in the public schema by default
  • System tables (pg_*, information_schema) are excluded
  • Admin can configure allowed/blocked tables (future feature)

Example

iex> SchemaInspector.list_tables()
{:ok, [
  %{name: "users", estimated_count: 150},
  %{name: "posts", estimated_count: 1200},
  ...
]}

iex> SchemaInspector.get_schema("users")
{:ok, %{
  table: "users",
  columns: [
    %{name: "id", type: "bigint", nullable: false, primary_key: true},
    %{name: "email", type: "character varying", nullable: false},
    ...
  ],
  primary_key: ["id"]
}}

Summary

Functions

Creates a table based on a schema definition from another database.

Gets the exact count of records in a local table.

Gets the primary key columns for a table.

Gets the schema (columns, types, constraints) for a specific table.

Lists all available tables with row counts.

Checks if a table exists.

Functions

create_table(table_name, schema_def, opts \\ [])

@spec create_table(String.t(), map(), keyword()) :: :ok | {:error, any()}

Creates a table based on a schema definition from another database.

Used by DB Sync to create tables that exist on sender but not on receiver.

Parameters

  • table_name - Name of the table to create
  • schema_def - Schema definition map with columns and primary_key

Example

schema_def = %{
  "columns" => [
    %{"name" => "id", "type" => "bigint", "nullable" => false, "primary_key" => true},
    %{"name" => "name", "type" => "character varying", "nullable" => true}
  ],
  "primary_key" => ["id"]
}
SchemaInspector.create_table("users", schema_def)

get_local_count(table_name, opts \\ [])

@spec get_local_count(
  String.t(),
  keyword()
) :: {:ok, non_neg_integer()} | {:error, any()}

Gets the exact count of records in a local table.

This is used by the receiver to compare local vs sender record counts.

get_primary_key(table_name, opts \\ [])

@spec get_primary_key(
  String.t(),
  keyword()
) :: {:ok, [String.t()]} | {:error, any()}

Gets the primary key columns for a table.

get_schema(table_name, opts \\ [])

@spec get_schema(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, any()}

Gets the schema (columns, types, constraints) for a specific table.

Returns

  • {:ok, schema} - Map with table info and columns
  • {:error, :not_found} - Table doesn't exist
  • {:error, reason} - Database error

list_tables(opts \\ [])

@spec list_tables(keyword()) :: {:ok, [map()]} | {:error, any()}

Lists all available tables with row counts.

Returns tables from the public schema, excluding system tables and security-sensitive tables.

Options

  • :include_phoenix_kit - Include phoenixkit* tables (default: true)
  • :schema - Database schema to inspect (default: "public")
  • :exact_counts - Use exact COUNT(*) instead of pg_stat estimates (default: true)

table_exists?(table_name, opts \\ [])

@spec table_exists?(
  String.t(),
  keyword()
) :: boolean()

Checks if a table exists.