DBML (dbml v0.3.2)

Summary

Functions

Generate Ecto migration files from parsed DBML tokens.

Generate Ecto schema files from parsed DBML tokens.

Parse a given string with a DBML schema definition

Parse a file containing a DBML schema definition.

Generate a DBML schema file from existing Ecto schema files.

Functions

generate_ecto_migrations(tokens, output_dir, repo_module, opts \\ [])

Generate Ecto migration files from parsed DBML tokens.

Options

  • :base_timestamp - integer timestamp prefix for file names (default: 20000101000000). Each table gets base + index.
  • :update - if false (default), returns an error if any migration file already exists. If true, compares schema with existing migrations and creates new ones for changed tables.

Returns {:ok, paths} on success, or {:error, message} if a file already exists (when update: false).

generate_ecto_schemas(tokens, output_dir, opts \\ [])

Generate Ecto schema files from parsed DBML tokens.

Options

  • :namespace - module namespace prefix (e.g. "MyApp.Schema"). Defaults to the project name from the DBML tokens, or "" if absent.
  • :singularize - whether to singularize table names for module names (default: true). Set to false to use table names as-is (e.g., usersUsers instead of usersUser).
  • :update - if false (default), returns an error if any schema file already exists. If true, overwrites existing files with the newly-generated content.

Returns {:ok, paths} on success, or {:error, message} if a file already exists (when update: false).

parse(doc, options \\ [])

Parse a given string with a DBML schema definition

Example:

iex> DBML.parse("table a { c1 [pk] }")
{:ok, [table: %{name: "a", fields: [%{name: "c1", type: "[pk]"}]}]}

parse_file(file, options \\ [])

Parse a file containing a DBML schema definition.

Options

  • :inline - when true, inlines clauses that work as redirection for other clauses. Settings this may improve runtime performance at the cost of increased compilation time and bytecode size

  • :debug - when true, writes generated clauses to :stderr for debugging

  • :export_combinator - make the underlying combinator function public so it can be used as part of parsec/1 from other modules

  • :export_metadata - export metadata necessary to use this parser combinator to generate inputs

schemas_to_dbml(input_dir, output_path, opts \\ [])

Generate a DBML schema file from existing Ecto schema files.

Reads all *.ex files in input_dir that contain use Ecto.Schema, parses their structure, and writes a single .dbml file to output_path.

Options

  • :project_name - name for the DBML project block (optional).
  • :database_type - database type string (default: "PostgreSQL").

Returns {:ok, output_path} or {:error, reason}.