version: 2.1

executors:
  node-executor:
    docker:
      - image: circleci/node:lts

jobs:
  unit-test:
    executor: node-executor
    working_directory: ~/repo
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Test
          command: |
            set +e
            yarn install
            yarn test

  integration-test-postgres:
    working_directory: ~/repo
    machine: true
    steps:
      - checkout
      - run:
          name: Install node@v12.18.2
          command: |
            set +e
            curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
            export NVM_DIR="/opt/circleci/.nvm"
            [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
            nvm install v12.18.2
            nvm alias default v12.18.2
            rm -rf ~./node-gyp
            node -v
      - run:
          name: Install yarn
          command: |
            set +e
            export NVM_DIR="/opt/circleci/.nvm"
            [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
            curl -o- -L https://yarnpkg.com/install.sh | bash
      - run:
          name: Setup Database
          command: |
            set +e
            export NVM_DIR="/opt/circleci/.nvm"
            [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
            export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
            nvm use 12.18.2
            yarn install
            yarn start:postgres
          background: true
      - run:
          name: Wait for DB
          command: |
            ./wait-for-it.sh -h localhost -p 5432 -t 500
      - run:
          name: Run integration tests
          command: |
            set +e
            export NVM_DIR="/opt/circleci/.nvm"
            [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
            export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
            nvm use 12.18.2
            yarn install
            yarn test:using-pg-dal
      - run:
          name: Cleanup
          command: |
            set +e
            export NVM_DIR="/opt/circleci/.nvm"
            [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
            export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
            nvm use 12.18.2
            yarn install
            yarn stop:postgres

workflows:
  test:
    jobs:
      - unit-test
      - integration-test-postgres
