name: 'Util: Data Tooling'

# TODO: Uncomment this after it works on a manual invocation
# on:
#   pull_request:
#     branches:
#       - '**'
#       - '!release/*'

on:
  workflow_dispatch:

jobs:
  sqlite-export-sqlite-import:
    name: sqlite database export -> sqlite database import
    runs-on: blacksmith-2vcpu-ubuntu-2204
    outputs:
      db_changed: ${{ fromJSON(steps.paths-filter.outputs.results).db }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Check for db changes
        uses: ./.github/actions/ci-filter
        id: paths-filter
        with:
          mode: filter
          base-ref: master
          filters: |
            db:
              - packages/@n8n/db/**
              - packages/cli/**
      - name: Setup, build and export sqlite
        if: fromJSON(steps.paths-filter.outputs.results).db
        uses: ./.github/actions/setup-nodejs
        with:
          build-command: |
            pnpm build
            ./packages/cli/bin/n8n export:entities --outputDir packages/cli/commands/export/outputs 
            ./packages/cli/bin/n8n import:entities --inputDir packages/cli/commands/export/outputs --truncateTables
  postgres-export-postgres-import:
    name: postgres export -> postgres import
    runs-on: blacksmith-2vcpu-ubuntu-2204
    outputs:
      db_changed: ${{ fromJSON(steps.paths-filter.outputs.results).db }}
    env:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_DATABASE: n8n
      DB_POSTGRESDB_HOST: localhost
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_USER: postgres
      DB_POSTGRESDB_PASSWORD: password
      DB_POSTGRESDB_POOL_SIZE: 1 # Detect connection pooling deadlocks
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Check for db changes
        uses: ./.github/actions/ci-filter
        id: paths-filter
        with:
          mode: filter
          base-ref: master
          filters: |
            db:
              - packages/@n8n/db/**
              - packages/cli/**

      - name: Setup and Build
        if: fromJSON(steps.paths-filter.outputs.results).db
        uses: ./.github/actions/setup-nodejs

      - name: Start Postgres
        if: fromJSON(steps.paths-filter.outputs.results).db
        uses: isbang/compose-action@4894d2492015c1774ee5a13a95b1072093087ec3 # v2.5.0
        with:
          compose-file: ./.github/docker-compose.yml
          services: |
            postgres

      - name: Export postgres
        if: fromJSON(steps.paths-filter.outputs.results).db
        run: ./packages/cli/bin/n8n export:entities --outputDir packages/cli/commands/export/outputs
      - name: Import postgres
        if: fromJSON(steps.paths-filter.outputs.results).db
        run: ./packages/cli/bin/n8n import:entities --inputDir packages/cli/commands/export/outputs --truncateTables
