sudo: false
language: java
dist: trusty

branches:
  except:
    - /^tmp\/.*/
    - /^REL.*/

stages:
  - name: test
    if: not branch =~ ^release\/.*$
  - name: release
    # releases pgjdbc for Java 8
    # It does set release tag and update pom.xml versions
    if: branch =~ ^release\/.*$
  - name: release_prev
    # releases pgjdbc for Java 6, Java 7
    # pgjdbc-jreX requires release pom.xml versions of the main project, so pgjdbc-jreX should be
    # released after the release of the main version, thus second stage
    if: branch =~ ^release\/.*$

before_script:
  - test $(grep "after_n_builds" codecov.yml | tr -d '[:space:]' | cut -d":" -f2) -eq $(grep -e "COVERAGE=Y$" .travis.yml | wc -l) || exit 1
  - export PG_DATADIR="/etc/postgresql/${PG_VERSION}/main"
  - export PG_SLAVE1_DATADIR="/etc/postgresql/${PG_VERSION}/slave1"
  - export PG_SLAVE2_DATADIR="/etc/postgresql/${PG_VERSION}/slave2"
  -  ./.travis/travis_install_postgres.sh
  - test "x$XA" == 'x' || ./.travis/travis_configure_xa.sh
  - test "x$REPLICATION" == 'x' || ./.travis/travis_configure_replication.sh
  - test "x$SSLTEST" == 'x' || ./.travis/travis_configure_ssl.sh
  - ./.travis/travis_start_postgres.sh
  - test "x$PG_VERSION" != 'xHEAD' || psql -U postgres -c "set password_encryption='scram-sha-256'; create user test with password 'test';"
  - test "x$PG_VERSION" = 'xHEAD' || psql -U postgres -c "create user test with password 'test';"
  - test "x$REPLICATION" == 'x' || psql -U postgres -c "alter user test with replication;"
  - psql -c 'create database test owner test;' -U postgres
  - test "x$SSLTEST" == 'x' || ./.travis/travis_ssl_users.sh
  - test "x$REPLICATION" == 'x' || ./.travis/travis_create_slaves.sh
  - if [[ $TRAVIS_BRANCH == release/* ]]; then echo "MAVEN_OPTS='-Xmx1g'" > ~/.mavenrc; else echo "MAVEN_OPTS='-Xmx1g -Dgpg.skip=true'" > ~/.mavenrc; fi
  - test "x$PG_VERSION" == 'x' || test "x$NO_HSTORE" == 'xY' || psql test -c 'CREATE EXTENSION hstore;' -U postgres
  - test "x$PG_VERSION" == 'x' || test "x$CREATE_PLPGSQL" == 'x' || createlang -U postgres plpgsql test
  - test "x$PG_VERSION" == 'x' || test "x$PLPGSQL_EXTENSION" == 'x' || psql test -c 'CREATE EXTENSION IF NOT EXISTS plpgsql;' -U postgres

env:
  global:
    - secure: "NI+aqwRLLVt2feJdk/2ZEZnsaPyu+vOx8MahVxjz0UUVvRHVqxM5O1M0R53NJfEeIjflOzgZJwRLqgyl6dkdfjytRhaHWGptQdehV4cwNb+4epnn8WlpRzMac65zTQqnbGVtw9jissDQv6/Zl/+D+DMcU65BbFZkix40whILXG0="

before_install:
  - ./.travis/travis_install_zulu.sh
  - test -z "${ZULU_JDK}" || export JDK${ZULU_JDK}_HOME=/usr/lib/jvm/zulu-${ZULU_JDK}-amd64
  - unset _JAVA_OPTIONS # see https://github.com/travis-ci/travis-ci/issues/8408

script:
  # make sure previous build artifacts are not used for subsequent builds
  - rm -rf $HOME/.m2/repository/org/postgresql || true
  - test -d "${JDK6_HOME}" || export JDK6_HOME=$(jdk_switcher home openjdk6)
  - test -d "${JDK7_HOME}" || export JDK7_HOME=$(jdk_switcher home openjdk7)
  - test -d "${JDK8_HOME}" || export JDK8_HOME=$(jdk_switcher home oraclejdk8)
  - test -d "${JDK9_HOME}" || export JDK9_HOME=$(jdk_switcher home oraclejdk9)
  - test -d "${JDK9_HOME}" || export JDK9_HOME=$(jdk_switcher home oraclejdk8) # JDK9 missing on precise, fallback to JDK8
  - envsubst < toolchains.xml > ~/.m2/toolchains.xml
  - test ${JDK} -eq 9 || jdk_switcher use oraclejdk8 # Run Maven with Java 8, build with Toolchains.
  - test -z "${ZULU_JDK}" || export TRAVIS_JDK_VERSION=zulujdk${ZULU_JDK} # trick codecov to use correct jdk version
  - if [[ $TRAVIS_BRANCH == release/* ]]; then .travis/travis_release.sh; else ./.travis/travis_build.sh; fi
  - ./.travis/travis_check_postgres_health.sh
  # To avoid useless S3 cache updates (https://github.com/travis-ci/travis-ci/issues/1441#issuecomment-67607074)
  #- mkdir /tmp/cache-trick
  #- mv $HOME/.m2/repository/org/postgresql /tmp/cache-trick/

before_cache:
  # No sense in caching current build artifacts
  - rm -rf $HOME/.m2/repository/org/postgresql
  - find $HOME/.ivy2 -name "ivydata-*.properties" -delete
  - find $HOME/.sbt -name "*.lock" -delete

# Skip default "mvn install" issued by Travis
# Root project cannot be compiled with older JDKs, so it makes sense to just skip the step
install: true

cache:
  directories:
    - $HOME/.m2/repository
    - $HOME/.ivy2/cache
    - $HOME/.sbt/boot/

matrix:
  fast_finish: true
  include:
    - stage: test
      jdk: oraclejdk8
      env: RUN_CHECKSTYLE=true
      script: mvn checkstyle:check
    - env:
        - FEDORA_CI=Y
      services:
        - docker
      if: type != pull_request
    - jdk: openjdk-ea
      sudo: required
      addons:
        postgresql: "10"
      env:
      - PG_VERSION=10
      - JDK=12
      - TZ=America/New_York # flips between −05:00 and −04:00
    - jdk: openjdk-ea
      sudo: required
      addons:
        postgresql: "11"
      env:
        - PG_VERSION=11
        - XA=true
        - COVERAGE=Y
        - NO_HSTORE=Y
        - PLPGSQL_EXTENSION=Y
    - jdk: oraclejdk9
      sudo: required
      addons:
        postgresql: "11"
      env:
        - PG_VERSION=11
        - XA=true
        - COVERAGE=Y
        - NO_HSTORE=Y
        - PLPGSQL_EXTENSION=Y
        - JDK=9
    - jdk: oraclejdk8
      sudo: required
      addons:
        postgresql: "11"
      env:
        - PG_VERSION=11
        - XA=true
        - COVERAGE=Y
        - NO_HSTORE=Y
        - PLPGSQL_EXTENSION=Y
        - JDK=8
    - jdk: oraclejdk11
      sudo: required
      addons:
        postgresql: "10"
      env:
      - PG_VERSION=10
      - JDK=11
      - TZ=America/New_York # flips between −05:00 and −04:00
    - jdk: oraclejdk9
      addons:
        postgresql: "9.6"
      env:
        - PG_VERSION=9.6
        - JDK=9
        - TZ=America/New_York # flips between −05:00 and −04:00
    - jdk: oraclejdk8
      sudo: required
      env:
        - PG_VERSION=HEAD
        - XA=true
        - REPLICATION=Y
        - COVERAGE=Y
        - TEST_CLIENTS=Y
        - TZ=Pacific/Chatham # flips between +12:45 and +13:45
        - JDK=8
    - jdk: oraclejdk8
      sudo: required
      addons:
        postgresql: "9.6"
      env:
        - PG_VERSION=9.6
        - ZULU_JDK=8
        - XA=true
        - REPLICATION=Y
        - COVERAGE=Y
        - MCENTRAL=Y
        - JDOC=Y
    - jdk: oraclejdk8
      sudo: required
      addons:
        postgresql: "9.5"
      env:
        - PG_VERSION=9.5
        - XA=true
        - REPLICATION=Y
        - JDK=8
    - jdk: oraclejdk8
      addons:
        postgresql: "9.4"
      env:
        - PG_VERSION=9.4
        - QUERY_MODE=extendedCacheEverything
        - COVERAGE=Y
        - TZ=Europe/Moscow # +03:00, no DST
        - JDK=8
    - jdk: oraclejdk8
      sudo: required
      addons:
        postgresql: "10"
      env:
        - PG_VERSION=10
        - SSLTEST=Y
        - COVERAGE=Y
        - JDK=8
    - jdk: openjdk7
      sudo: required
      addons:
        postgresql: "9.2"
      env:
        - PG_VERSION=9.2
        - ZULU_JDK=7
        - MCENTRAL=Y
        - COVERAGE=Y
        - TZ=UTC
    - jdk: openjdk7
      sudo: required
      addons:
        postgresql: "9.1"
      env:
        - PG_VERSION=9.1
        - ZULU_JDK=6
        - MCENTRAL=Y
    - jdk: oraclejdk8
      sudo: required
      env:
        - PG_VERSION=8.4
        - XA=true
        - COVERAGE=Y
        - NO_HSTORE=Y
        - CREATE_PLPGSQL=Y
    - jdk: oraclejdk8
      addons:
        postgresql: "9.6"
      env:
        - PG_VERSION=9.6
        - QUERY_MODE=simple
        - COVERAGE=Y
        - TEST_CLIENTS=Y
        - JDK=8
    - jdk: oraclejdk8
      addons:
        postgresql: "9.3"
      env:
        - PG_VERSION=9.3
        - QUERY_MODE=extendedForPrepared
        - COVERAGE=Y
        - JDK=8
    - jdk: oraclejdk8
      addons:
        postgresql: "9.4"
      env:
        - PG_VERSION=9.4
        - NO_WAFFLE_NO_OSGI=Y
        - JDK=8
    - stage: release
      jdk: oraclejdk8
      env:
        - PG_VERSION=9.6
    - stage: release_prev
      jdk: openjdk7
      sudo: required
      addons:
        postgresql: "9.2"
      env:
        - PG_VERSION=9.2
        - ZULU_JDK=7
    - stage: release_prev
      jdk: openjdk7
      sudo: required
      addons:
        postgresql: "9.1"
      env:
        - PG_VERSION=9.1
        - ZULU_JDK=6

# Deploy snapshots to Maven Central
after_success:
  - "test $TRAVIS_PULL_REQUEST == 'false' && test $TRAVIS_BRANCH == 'master' && test $MCENTRAL == 'Y' && ./.travis/travis_deploy.sh"
