summaryrefslogtreecommitdiff
path: root/scripts/travis_prepare.sh
blob: f6f761113bd67054a53fce86472d8afcd05d7864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash

set -e -x

# Prepare the test databases in Travis CI.
#
# The script should be run with sudo.
# The script is not idempotent: it assumes the machine in a clean state
# and is designed for a sudo-enabled Trusty environment.
#
# The variables TEST_PAST, TEST_FUTURE, DONT_TEST_PRESENT can be used to test
# against unsupported Postgres versions and skip tests with supported ones.
#
# The variables can be set in the travis configuration
# (https://travis-ci.org/psycopg/psycopg2/settings)
export TEST_PAST=${TEST_PAST:-0}
export TEST_FUTURE=${TEST_FUTURE:-0}
export TEST_VERBOSE=${TEST_VERBOSE:-0}
export PSYCOPG2_TEST_FAST=${PSYCOPG2_TEST_FAST:-0}
export TEST_PRESENT=${TEST_PRESENT:-1}

set_param () {
    # Set a parameter in a postgresql.conf file
    param=$1
    value=$2

    sed -i "s/^\s*#\?\s*$param.*/$param = $value/" "$DATADIR/postgresql.conf"
}

create () {
    export VERSION=$1
    export PACKAGE=${2:-$VERSION}

    # Version as number: 9.6 -> 906; 11 -> 1100
    export VERNUM=$(echo $VERSION \
        | sed 's/\([0-9]\+\)\(\.\([0-9]\+\)\)\?/100 * \1 + 0\3/' | bc)

    # Port number: 9.6 -> 50906
    export PORT=$(( 50000 + $VERNUM ))

    export DATADIR="/var/lib/postgresql/$PACKAGE/psycopg"
    export PGDIR="/usr/lib/postgresql/$PACKAGE"
    export PGBIN="$PGDIR/bin"

    # install postgres versions not available on the image
    if [[ ! -d "${PGDIR}" ]]; then
        if (( "$VERNUM" >= 904 )); then
            # A versiou supported by postgres
            if [[ ! "${apt_updated:-}" ]]; then
                apt_updated="yeah"
                sudo apt-get update -y
            fi
            sudo apt-get install -y \
                postgresql-server-dev-${VERSION} postgresql-${VERSION}
        else
            # A dinosaur
            wget -O - \
                https://upload.psycopg.org/postgresql/postgresql-${PACKAGE}-$(lsb_release -cs).tar.bz2 \
                | sudo tar xjf - -C /usr/lib/postgresql
        fi
    fi

    sudo -u postgres "$PGBIN/initdb" -D "$DATADIR"

    set_param port "$PORT"
    if (( "$VERNUM" >= 800 )); then
        set_param listen_addresses "'*'"
    else
        set_param tcpip_socket true
    fi

    # for two-phase commit testing
    if (( "$VERNUM" >= 801 )); then set_param max_prepared_transactions 10; fi

    # for replication testing
    if (( "$VERNUM" >= 900 )); then set_param max_wal_senders 5; fi
    if (( "$VERNUM" >= 904 )); then set_param max_replication_slots 5; fi

    if (( "$VERNUM" >= 904 )); then
        set_param wal_level logical
    elif (( "$VERNUM" >= 900 )); then
        set_param wal_level hot_standby
    fi

    if (( "$VERNUM" >= 900 )); then
        echo "host replication travis 0.0.0.0/0 trust" >> "$DATADIR/pg_hba.conf"
    fi

    # start the server, wait for start
    sudo -u postgres "$PGBIN/pg_ctl" -w -l /dev/null -D "$DATADIR" start

    # create the test database
    DBNAME=psycopg2_test
    CONNINFO="user=postgres host=localhost port=$PORT dbname=template1"

    if (( "$VERNUM" >= 901 )); then
        psql -c "create user travis createdb createrole replication" "$CONNINFO"
    elif (( "$VERNUM" >= 801 )); then
        psql -c "create user travis createdb createrole" "$CONNINFO"
    else
        psql -c "create user travis createdb createuser" "$CONNINFO"
    fi

    psql -c "create database $DBNAME with owner travis" "$CONNINFO"

    # configure global objects on the test database
    CONNINFO="user=postgres host=localhost port=$PORT dbname=$DBNAME"

    if (( "$VERNUM" >= 901 )); then
        psql -c "create extension hstore" "$CONNINFO"
    elif (( "$VERNUM" >= 803 )); then
        psql -f "$PGDIR/share/contrib/hstore.sql" "$CONNINFO"
    fi

    if (( "$VERNUM" == 901 )); then
        psql -c "create extension json" "$CONNINFO"
    fi
}

# Would give a permission denied error in the travis build dir
cd /

if (( "$TEST_PRESENT" )); then
    if [[ ${TRAVIS_CPU_ARCH} == "arm64" ]]; then
    # Postgres versions supported by ARM64
        create 10
    else
        create 13
        create 12
        create 11
        create 10
        create 9.6
        create 9.5
        create 9.4
    fi
fi
# Unsupported postgres versions that we still support
# Images built by https://github.com/psycopg/psycopg2-wheels/tree/build-dinosaurs
if (( "$TEST_PAST" )); then
    create 7.4
    create 8.0
    create 8.1
    create 8.2
    create 8.3
    create 8.4
    create 9.0
    create 9.1
    create 9.2
    create 9.3
fi

# Postgres built from master
if (( "$TEST_FUTURE" )); then
    # create 14 14-master
    true
fi