version: 2
jobs:
  build:
    docker:
      - image: circleci/node:10.15
        environment:
          C_DB_URL: postgres://livemap@localhost:5432/livemapdb

      - image: circleci/postgres:9.6
        environment:
          POSTGRES_USER: postgres
    working_directory: ~/repo

    steps:
      - checkout

      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m

      - run:
          name: Install PostgreSQL client
          command: |
            sudo apt-get update && sudo apt-get install postgresql-client

      - run:
          name: Initialise DB
          command: |
            psql -h localhost -p 5432 -U postgres < initdb/10_init-user-db.sql

      - run:
          name: Create tables
          command: |
            psql -h localhost -p 5432 -U livemap -d livemapdb < setup/schema.sql

      - run:
          name: Install packages
          command: npm install

      - run:
          name: Run tests
          command: npm test

version: 2
jobs:
  build:
    docker:
      - image: circleci/node:10.15
        environment:
          C_DB_URL: postgres://livemap@localhost:5432/livemapdb

      - image: circleci/postgres:9.6
        environment:
          POSTGRES_USER: postgres
    working_directory: ~/repo

    steps:
      - checkout

      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m

      - run:
          name: Install PostgreSQL client
          command: |
            sudo apt-get update && sudo apt-get install postgresql-client

      - run:
          name: Initialise DB
          command: |
            psql -h localhost -p 5432 -U postgres < initdb/10_init-user-db.sql

      - run:
          name: Create tables
          command: |
            psql -h localhost -p 5432 -U livemap -d livemapdb < setup/schema.sql

      - run:
          name: Install packages
          command: npm install

      - run:
          name: Run tests
          command: npm test

  deploy:
    docker:
      - image: buildpack-deps:trusty
    steps:
      - checkout
      - run:
          name: Deploy Development to Heroku
          command: |
            git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME_TEST.git development

workflows:
  version: 2
  build-deploy:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: development

Deploy to Heroku after build
Deploy to Heroku after build
