version: 2.1
orbs:
  codecov: codecov/codecov@1.0.5

jobs:
  build:
    docker:
      - image: 'citus/extbuilder:latest'
    steps:
      - checkout
      - run:
          name: 'Configure, Build, and Install'
          command: build-ext
      - persist_to_workspace:
          root: .
          paths: [.]
  check-style:
    docker:
      - image: 'citusdata/stylechecker:latest'
    steps:
      - checkout
      - run:
          name: 'Check Style'
          command: citus_indent --check
      - run:
          name: 'Fix whitespace'
          command: ci/editorconfig.sh
      - run:
          name: 'Check if whitespace fixing changed anything, install editorconfig if it did'
          command: git diff --exit-code
      - run:
          name: 'Remove useless declarations'
          command: ci/remove_useless_declarations.sh
      - run:
          name: 'Check if changed'
          command: git diff --cached --exit-code
      - run:
          name: 'Normalize test output'
          command: ci/normalize_expected.sh
      - run:
          name: 'Check if changed'
          command: git diff --exit-code
  check-sql-snapshots:
    docker:
      - image: 'citus/extbuilder:latest'
    steps:
      - checkout
      - run:
          name: 'Check Snapshots'
          command: "./configure && make check-sql-snapshots -C src/backend/distributed"
  test-11_check-multi:
    docker:
      - image: 'citus/exttester-11:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Install and Test (check-multi)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-multi'
      - codecov/upload:
          flags: 'test_11,multi'

  test-11_check-tt-van-mx:
    docker:
      - image: 'citus/exttester-11:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Install and Test (check-tt-van-mx)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-multi-task-tracker-extra check-vanilla check-multi-mx'
      - codecov/upload:
          flags: 'test_11,tracker,vanilla,mx'
  test-11_check-iso-work-fol:
    docker:
      - image: 'citus/exttester-11:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Install and Test (check-iso-work-fol)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-isolation check-worker'
      - codecov/upload:
          flags: 'test_11,isolation,worker'
  test-11_check-fol:
    docker:
      - image: 'citus/exttester-11:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Enable core dumps'
          command: 'ulimit -c unlimited'
      - run:
          name: 'Install and Test (fol)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-follower-cluster'
      - run:
          command: |
            mkdir -p /tmp/core_dumps
            cp core.* /tmp/core_dumps
          when: on_fail
      - codecov/upload:
          flags: 'test_11,follower'
      - store_artifacts:
          path: '/tmp/core_dumps'
  test-11_check-failure:
    docker:
      - image: 'citus/failtester-11:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Install and Test (check-failure)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-failure'

  test-11-12_check-pg-upgrade:
    docker:
      - image: 'citus/pgupgradetester:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Install and test postgres upgrade'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext --target check-pg-upgrade --old-pg-version 11 --new-pg-version 12'

  test-12_check-multi:
    docker:
      - image: 'citus/exttester-12:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Install and Test (check-multi)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-multi'
      - codecov/upload:
          flags: 'test_12,multi'
  test-12_check-tt-van-mx:
    docker:
      - image: 'citus/exttester-12:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Install and Test (check-tt-van-mx)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-multi-task-tracker-extra check-vanilla check-multi-mx'
      - codecov/upload:
          flags: 'test_12,tracker,vanilla,mx'
  test-12_check-iso-work-fol:
    docker:
      - image: 'citus/exttester-12:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
           at: .
      - run:
          name: 'Install and Test (check-iso-work-fol)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-isolation check-worker'
      - codecov/upload:
          flags: 'test_12,isolation,worker'
  test-12_check-fol:
    docker:
      - image: 'citus/exttester-12:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Enable core dumps'
          command: 'ulimit -c unlimited'
      - run:
          name: 'Install and Test (fol)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-follower-cluster'
      - run:
          command: |
            mkdir -p /tmp/core_dumps
            cp core.* /tmp/core_dumps
          when: on_fail
      - codecov/upload:
          flags: 'test_12,follower'
      - store_artifacts:
          path: '/tmp/core_dumps'

  test-12_check-failure:
    docker:
      - image: 'citus/failtester-12:latest'
    working_directory: /home/circleci/project
    steps:
      - attach_workspace:
          at: .
      - run:
          name: 'Install and Test (check-failure)'
          command: 'chown -R circleci:circleci /home/circleci && install-and-test-ext check-failure'

  test-11_check-citus-upgrade:
    docker:
      - image: 'citus/citusupgradetester-11:latest'
    working_directory: /home/circleci/project
    steps:
      - {attach_workspace: {at: .}}
      - run:
          name: 'Install and test citus upgrade'
          command: |
           chown -R circleci:circleci /home/circleci
           install-and-test-ext --target check-citus-upgrade --citus-pre-tar /install-pg11-citusv8.0.0.tar
           install-and-test-ext --target check-citus-upgrade --citus-pre-tar /install-pg11-citusv8.1.0.tar
           install-and-test-ext --target check-citus-upgrade --citus-pre-tar /install-pg11-citusv8.2.0.tar
           install-and-test-ext --target check-citus-upgrade --citus-pre-tar /install-pg11-citusv8.3.0.tar

           install-and-test-ext --target check-citus-upgrade-mixed --citus-pre-tar /install-pg11-citusv8.0.0.tar
           install-and-test-ext --target check-citus-upgrade-mixed --citus-pre-tar /install-pg11-citusv8.1.0.tar
           install-and-test-ext --target check-citus-upgrade-mixed --citus-pre-tar /install-pg11-citusv8.2.0.tar
           install-and-test-ext --target check-citus-upgrade-mixed --citus-pre-tar /install-pg11-citusv8.3.0.tar





workflows:
  version: 2
  build_and_test:
    jobs:
      - build
      - check-style
      - check-sql-snapshots

      - test-11_check-multi:
          requires: [build]
      - test-11_check-tt-van-mx:
          requires: [build]
      - test-11_check-iso-work-fol:
          requires: [build]
      - test-11_check-fol:
          requires: [build]
      - test-11_check-failure:
          requires: [build]

      - test-12_check-multi:
          requires: [build]
      - test-12_check-tt-van-mx:
          requires: [build]
      - test-12_check-iso-work-fol:
          requires: [build]
      - test-12_check-fol:
          requires: [build]
      - test-12_check-failure:
          requires: [build]

      - test-11-12_check-pg-upgrade:
          requires: [build]

      - test-11_check-citus-upgrade:
          requires: [build]

