mix pgflow.gen.flow_migration (PgFlow v0.1.0)

Copy Markdown View Source

Generates an Ecto migration that registers a PgFlow flow in the database.

Usage

mix pgflow.gen.flow_migration MyApp.Flows.ArticleFlow
mix pgflow.gen.flow_migration MyApp.Flows.ArticleFlow --migrations-path priv/repo/migrations

Options

  • --migrations-path - Path to the migrations directory. Defaults to priv/repo/migrations.

Generated SQL

The migration executes SQL statements that:

  1. Create the flow record and PGMQ queue
  2. Add each step with its dependencies and configuration

Example generated migration:

defmodule MyApp.Repo.Migrations.CompileArticleFlow do
  use Ecto.Migration

  def up do
    execute "SELECT pgflow.create_flow('article_flow', 3, 5, 120)"
    execute "SELECT pgflow.add_step('article_flow', 'fetch_article', ARRAY[]::text[], NULL, NULL, NULL, NULL, 'single')"
    execute "SELECT pgflow.add_step('article_flow', 'summarize', ARRAY['fetch_article']::text[], NULL, NULL, NULL, NULL, 'single')"
  end

  def down do
    execute "DELETE FROM pgflow.deps WHERE flow_slug = 'article_flow'"
    execute "DELETE FROM pgflow.steps WHERE flow_slug = 'article_flow'"
    execute "DELETE FROM pgflow.flows WHERE flow_slug = 'article_flow'"
    execute "SELECT pgmq.drop_queue('article_flow')"
  end
end

Requirements

The flow module must:

  1. Use PgFlow.Flow
  2. Define a valid flow with @flow and at least one step
  3. Be compilable (no syntax errors)

Example

# Define a flow
defmodule MyApp.Flows.ArticleFlow do
  use PgFlow.Flow

  @flow slug: :article_flow, max_attempts: 3

  step :fetch do
    fn input, _ctx -> %{data: input} end
  end

  step :process, depends_on: [:fetch] do
    fn deps, _ctx -> %{result: deps.fetch} end
  end
end

# Generate the migration
$ mix pgflow.gen.flow_migration MyApp.Flows.ArticleFlow

# Run the migration
$ mix ecto.migrate