Branching Workflows
This guide demonstrates practical workflows for using database branches in your development process.
Development Workflow Overview
Section titled “Development Workflow Overview”Database branches let you:
- Test migrations safely - Apply schema changes to a branch before production
- Isolate feature development - Each feature gets its own database copy
- Create PR previews - Automatic preview environments for pull requests
- Debug production issues - Clone production data to investigate bugs
Workflow 1: Feature Development
Section titled “Workflow 1: Feature Development”Use branches to develop and test new features in isolation.
Create a Feature Branch
Section titled “Create a Feature Branch”# Create a branch for your featurefluxbase branch create add-user-profiles
# Check the branch was createdfluxbase branch status add-user-profilesOutput:
Branch: add-user-profiles (add-user-profiles)Status: readyConnect to Your Branch
Section titled “Connect to Your Branch”Configure your application to use the branch:
// TypeScript SDKimport { createClient } from '@fluxbase/sdk'
const fluxbase = createClient( 'http://localhost:8080', 'your-client-key', { branch: 'add-user-profiles' } // Connect to branch)Or use HTTP headers:
curl http://localhost:8080/api/v1/tables/public/users \ -H "X-Fluxbase-Branch: add-user-profiles" \ -H "Authorization: Bearer $TOKEN"Make Schema Changes
Section titled “Make Schema Changes”Create a migration for your feature:
-- migrations/003_add_user_profiles.up.sqlCREATE TABLE public.profiles ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id UUID NOT NULL REFERENCES auth.users(id), bio TEXT, avatar_url TEXT, created_at TIMESTAMPTZ DEFAULT now());
CREATE INDEX idx_profiles_user_id ON public.profiles(user_id);Apply to your branch:
fluxbase migrations syncTest Your Changes
Section titled “Test Your Changes”Run your application tests against the branch:
# Your tests automatically use the branch if configurednpm test
# Or specify the branch in your test setupFLUXBASE_BRANCH=add-user-profiles npm testReview and Merge
Section titled “Review and Merge”Once satisfied:
- Commit your migration files
- Create a pull request
- After review, merge to main
- Apply migrations to production:
fluxbase --profile prod migrations apply-pendingClean Up
Section titled “Clean Up”Delete the branch when done:
fluxbase branch delete add-user-profilesWorkflow 2: PR Preview Environments
Section titled “Workflow 2: PR Preview Environments”Automatically create database branches for each pull request.
GitHub Actions Setup
Section titled “GitHub Actions Setup”Add this workflow to .github/workflows/pr-preview.yml:
name: PR Preview
on: pull_request: types: [opened, synchronize, reopened, closed]
jobs: preview: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4
- name: Install Fluxbase CLI run: | curl -fsSL https://raw.githubusercontent.com/fluxbase-eu/fluxbase/main/install-cli.sh | bash
- name: Create/Update Preview Branch if: github.event.action != 'closed' env: FLUXBASE_SERVER: ${{ secrets.FLUXBASE_SERVER }} FLUXBASE_TOKEN: ${{ secrets.FLUXBASE_TOKEN }} run: | # Create or reset the preview branch fluxbase branch create "pr-${{ github.event.number }}" \ --pr ${{ github.event.number }} \ --repo ${{ github.repository }} \ --expires-in 7d 2>/dev/null || \ fluxbase branch reset "pr-${{ github.event.number }}" --force
# Apply migrations fluxbase migrations sync
# Deploy functions fluxbase functions sync
- name: Comment Preview Info if: github.event.action == 'opened' uses: actions/github-script@v7 with: script: | github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: `## Preview Environment Ready
Branch: \`pr-${{ github.event.number }}\`
**Connect via SDK:** \`\`\`typescript const client = createClient(url, key, { branch: 'pr-${{ github.event.number }}' }) \`\`\`
**Connect via Header:** \`\`\` X-Fluxbase-Branch: pr-${{ github.event.number }} \`\`\`
This preview will be automatically deleted when the PR is closed.` })
- name: Delete Preview Branch if: github.event.action == 'closed' env: FLUXBASE_SERVER: ${{ secrets.FLUXBASE_SERVER }} FLUXBASE_TOKEN: ${{ secrets.FLUXBASE_TOKEN }} run: | fluxbase branch delete "pr-${{ github.event.number }}" --force || trueHow It Works
Section titled “How It Works”- PR Opened: Creates a new branch named
pr-{number}, applies migrations and functions - PR Updated: Resets the branch and reapplies changes
- PR Closed: Deletes the branch and database
Testing Against PR Preview
Section titled “Testing Against PR Preview”In your CI tests:
test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4
- name: Run Tests env: FLUXBASE_SERVER: ${{ secrets.FLUXBASE_SERVER }} FLUXBASE_TOKEN: ${{ secrets.FLUXBASE_TOKEN }} FLUXBASE_BRANCH: pr-${{ github.event.number }} run: npm testWorkflow 3: Migration Testing
Section titled “Workflow 3: Migration Testing”Test database migrations before applying to production.
Create a Test Branch
Section titled “Create a Test Branch”# Create branch with full data clone (for realistic testing)fluxbase branch create test-migrations --clone-data full_cloneApply and Test Migrations
Section titled “Apply and Test Migrations”# Apply pending migrations to the test branchfluxbase migrations sync
# Run migration testsfluxbase tables query users --limit 10
# Check for any data issuesfluxbase graphql query '{ users { id email } }'Verify Rollback Works
Section titled “Verify Rollback Works”# Rollback the migrationfluxbase migrations rollback --step 1
# Verify data is intactfluxbase tables query users --limit 10
# Re-applyfluxbase migrations apply-pendingApply to Production
Section titled “Apply to Production”Once testing passes:
# Switch to production profilefluxbase --profile prod migrations apply-pending
# Verifyfluxbase --profile prod migrations listClean Up
Section titled “Clean Up”fluxbase branch delete test-migrationsWorkflow 4: Debugging Production Issues
Section titled “Workflow 4: Debugging Production Issues”Clone production data to investigate issues safely.
Create a Debug Branch
Section titled “Create a Debug Branch”# Clone with full data (be careful with sensitive data!)fluxbase branch create debug-issue-123 --clone-data full_clone
# Or schema only if data isn't neededfluxbase branch create debug-issue-123 --clone-data schema_onlyInvestigate the Issue
Section titled “Investigate the Issue”# Query problematic datafluxbase tables query orders \ --where "status=eq.stuck" \ --select "id,user_id,status,created_at"
# Run custom SQLfluxbase graphql query '{ orders(filter: { status_eq: "stuck" }) { id user { email } items { product_id quantity } }}'Test a Fix
Section titled “Test a Fix”Make your fix, then test:
# Apply your fix migrationfluxbase migrations sync
# Verify the fixfluxbase tables query orders --where "status=eq.stuck"Apply Fix to Production
Section titled “Apply Fix to Production”fluxbase --profile prod migrations apply-pendingClean Up
Section titled “Clean Up”fluxbase branch delete debug-issue-123Workflow 5: Using Seed Data
Section titled “Workflow 5: Using Seed Data”Populate branches with test data using seed files for consistent development environments.
What is Seed Data?
Section titled “What is Seed Data?”The seed_data clone mode creates a branch with the database schema and automatically executes SQL seed files to populate it with test data. This is perfect for:
- Development environments with realistic sample data
- Demo environments with consistent data
- Integration testing with known test datasets
- Onboarding new developers with pre-populated data
Create Seed Files
Section titled “Create Seed Files”Create numbered SQL files in the seeds/ directory:
# Create seeds directorymkdir -p seeds
# Create seed filescat > seeds/001_users.sql <<'EOF'-- Seed file: Initial test usersINSERT INTO auth.users (id, email, email_confirmed_at, role)VALUES ('00000000-0000-0000-0000-000000000001', 'admin@test.local', NOW(), 'admin'), ('00000000-0000-0000-0000-000000000002', 'user@test.local', NOW(), 'authenticated')ON CONFLICT (email) DO NOTHING;EOF
cat > seeds/002_data.sql <<'EOF'-- Seed file: Sample dataINSERT INTO public.posts (user_id, title, content)VALUES ('00000000-0000-0000-0000-000000000001', 'Welcome Post', 'This is a test post'), ('00000000-0000-0000-0000-000000000002', 'Another Post', 'More test content')ON CONFLICT DO NOTHING;EOFCreate Branch with Seeds
Section titled “Create Branch with Seeds”# Use default seeds from ./seeds directoryfluxbase branch create dev --clone-data seed_data
# Use custom seeds directoryfluxbase branch create demo --clone-data seed_data --seeds-dir ./test-fixtures
# Use production-like seedsfluxbase branch create staging --clone-data seed_data --seeds-dir /shared/prod-seedsSeed File Best Practices
Section titled “Seed File Best Practices”- Use numeric prefixes - Files execute in lexicographic order:
001_,002_, etc. - Make seeds idempotent - Use
ON CONFLICT DO NOTHINGorWHERE NOT EXISTS - Use deterministic UUIDs - Makes data predictable across environments
- Keep files focused - One file per logical group (users, posts, settings, etc.)
- Document dependencies - Add comments if one seed depends on another
Example idempotent seed:
-- 001_admin_user.sqlINSERT INTO auth.users (id, email, role)VALUES ('00000000-0000-0000-0000-000000000001', 'admin@test.local', 'admin')ON CONFLICT (email) DO NOTHING;Troubleshooting Seeds
Section titled “Troubleshooting Seeds”If seed execution fails:
# Check branch statusfluxbase branch get my-branch
# View activity log for errorsfluxbase branch activity my-branch
# Fix the seed file, then reset and retryfluxbase branch reset my-branch --forceConfiguration
Section titled “Configuration”Set default seeds path in fluxbase.yaml:
branching: enabled: true default_data_clone_mode: seed_data seeds_path: ./seedsBest Practices
Section titled “Best Practices”Branch Naming Conventions
Section titled “Branch Naming Conventions”Use consistent naming for easy identification:
# Feature branchesfluxbase branch create feature/add-authfluxbase branch create feature/update-billing
# PR previewsfluxbase branch create pr-123
# Bug investigationfluxbase branch create debug/order-stuck-issue
# Testingfluxbase branch create test/migration-v2Set Expiration for Temporary Branches
Section titled “Set Expiration for Temporary Branches”Prevent abandoned branches from accumulating:
# Auto-delete after 24 hoursfluxbase branch create temp-test --expires-in 24h
# Auto-delete after 7 daysfluxbase branch create pr-preview --expires-in 7dUse Appropriate Clone Modes
Section titled “Use Appropriate Clone Modes”| Mode | Use Case |
|---|---|
schema_only | Most development work, migrations |
full_clone | Bug investigation, data-dependent tests |
seed_data | Development with sample test data |
Monitor Branch Usage
Section titled “Monitor Branch Usage”Keep track of active branches:
# List all branchesfluxbase branch list
# See only your branchesfluxbase branch list --mine
# Check connection pool statsfluxbase branch statsClean Up Regularly
Section titled “Clean Up Regularly”Remove branches you no longer need:
# Delete a specific branchfluxbase branch delete my-old-branch
# Force delete (skip confirmation)fluxbase branch delete abandoned-branch --forceTroubleshooting
Section titled “Troubleshooting”Branch Creation Fails
Section titled “Branch Creation Fails”Error: “Maximum branches limit reached”
# List branches to find ones to deletefluxbase branch list --mine
# Delete unused branchesfluxbase branch delete old-feature --forceError: “Failed to create database”
Check that:
- The admin database URL is configured
- The database user has CREATE DATABASE privileges
- There’s enough disk space
Branch Not Found
Section titled “Branch Not Found”# Check if branch existsfluxbase branch list | grep my-branch
# Get full branch detailsfluxbase branch get my-branchMigrations Fail on Branch
Section titled “Migrations Fail on Branch”# Check migration statusfluxbase migrations list
# View branch activity for errorsfluxbase branch activity my-branch --limit 10
# Reset and retryfluxbase branch reset my-branch --forcefluxbase migrations syncNext Steps
Section titled “Next Steps”- Database Branching Overview - Core concepts
- TypeScript SDK Branching - SDK documentation
- GitHub Integration - Automated PR branches
- CLI Commands Reference - All branch commands