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

Copy Markdown View Source

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

Jobs are single-step flows under the hood. This task generates migration SQL that creates the flow, adds the step, and marks it as flow_type = 'job'.

Usage

mix pgflow.gen.job_migration MyApp.Jobs.SendEmail
mix pgflow.gen.job_migration MyApp.Jobs.SendEmail --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 the step (slug defaults to the job queue name, or an explicit name from perform :name do)
  3. Mark the flow as flow_type = 'job'

Example generated migration:

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

  def up do
    execute "SELECT pgflow.create_flow('send_email', 3, 5, 60)"
    execute "SELECT pgflow.add_step('send_email', 'send_email', ARRAY[]::text[], NULL, NULL, NULL, NULL, 'single')"
    execute "UPDATE pgflow.flows SET flow_type = 'job' WHERE flow_slug = 'send_email'"
  end

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

Requirements

The job module must:

  1. Use PgFlow.Job
  2. Define a valid job with @job and a perform block
  3. Be compilable (no syntax errors)