# Compile flow to SQL

import { Aside, Steps } from "@astrojs/starlight/components";

Now that we've defined our flow, we need to register it in the database. pgflow provides a CLI tool that compiles your TypeScript flow definition into SQL migrations that can be applied to your Supabase database.

<Aside type="caution" title="Prerequisites">
Before continuing, make sure you have:
- Completed the [flow definition](/get-started/flows/create-flow/) from the previous step
- The Supabase CLI installed and configured
- [Deno version **1.45.2 or higher**](https://github.com/denoland/deno/releases/tag/v1.45.2) (2.x also supported) - required for flow compilation
</Aside>

## What is compilation?

pgflow compiles your TypeScript flow definition into SQL migrations that register the flow in your database.

<Steps>

1. Analyze TypeScript flow definition
2. Extract steps, dependencies, and options
3. Generate SQL registration commands
4. Create migration file

</Steps>

The runtime executes flows based on their database representation, not the TypeScript code directly.

<Steps>

1. ### Compile the flow to SQL

   Run the pgflow compile command, pointing to your flow definition file:

   ```bash frame="none"
   npx pgflow@latest compile supabase/functions/_flows/greet_user.ts
   ```

   This will:
   - Parse your TypeScript flow definition
   - Extract the flow structure, step dependencies, and configuration
   - Generate SQL commands to register the flow in the database
   - Create a timestamped migration file in your Supabase migrations folder

   You should see output like this:

   ```
   ✓ Successfully compiled flow to SQL
   ✓ Migration file created: supabase/migrations/20250505120000_create_greet_user_flow.sql
   ```

2. ### Examine the generated SQL

   Let's look at what got generated. Open the migration file in your editor:

   ```bash frame="none"
   cat supabase/migrations/*_create_greet_user_flow.sql
   ```

   The migration file contains SQL commands that:

   1. Create the flow definition
   2. Add each step with its configuration
   3. Define dependencies between steps

   The generated SQL looks like this:

   ```sql
   SELECT pgflow.create_flow('greet_user');
   SELECT pgflow.add_step('greet_user', 'full_name');
   SELECT pgflow.add_step('greet_user', 'greeting', ARRAY['full_name']);
   ```

   This SQL representation is what the pgflow runtime system uses to execute your workflow.

   :::tip[Step Configuration]
   This is a simple example showing basic step registration. Steps can also be configured with retry behavior, timeouts, and delays. See [Step Execution Options](/reference/configuration/step-execution/) for all available configuration options.
   :::

3. ### Apply the migration

   Now that we have the SQL migration, we need to apply it to our database:

   ```bash frame="none"
   npx supabase migrations up
   ```

   This will execute the SQL migration and register your flow definition in the database.

   If successful, you should see output like this:

   ```
   Applying migration 20250505120000_create_greet_user_flow.sql...done
   ```

</Steps>

:::note[Flow definitions are immutable]
Once a flow is registered in the database, its structure cannot be modified. To change a flow, you can either [delete it](/build/delete-flows/) (development only) or use [versioning](/build/version-flows/).

<details>
<summary>Why immutability matters</summary>

<br/>

**What does immutability mean?**

Once a flow is registered, you cannot modify its structure - no adding/removing steps, changing dependencies, or renaming steps.

**Why this protects production:**

- Running workflows continue safely through deployments
- Historical runs remain intact with their original structure
- Flows behave consistently from start to finish
- Complete audit trails are preserved

This ensures your production workflows are stable, predictable, and never break mid-execution.
</details>
:::