Show More
@@ -0,0 +1,122 b'' | |||||
|
1 | #!/bin/sh -e | |||
|
2 | ||||
|
3 | if [ $# -lt 2 ] || [ $# -gt 3 ]; then | |||
|
4 | cat >&2 <<EOD | |||
|
5 | usage: $0 CONFIG_FILE FROM_REV [TO_REV] | |||
|
6 | ||||
|
7 | Runs a database migration from FROM_REV to TO_REV (default: current | |||
|
8 | working directory parent), using the specified CONFIG_FILE (.ini file). | |||
|
9 | ||||
|
10 | Test is run using a clean Kallithea install, in a temporary virtual | |||
|
11 | environment. FROM_REV and (optional) TO_REV should be Mercurial revision | |||
|
12 | identifiers (e.g. changeset hash or a version number tag). The working | |||
|
13 | directory is not touched, but the database referenced in the config file | |||
|
14 | will be (re)created. | |||
|
15 | ||||
|
16 | Only SQLite is available out of the box; for MySQL or PostgreSQL, set | |||
|
17 | the EXTRA environment variable to the required package(s), and it'll | |||
|
18 | be installed in the virtual environment. (E.g. EXTRA=MySQL-python or | |||
|
19 | EXTRA=psycopg2.) | |||
|
20 | ||||
|
21 | The temporary directory is not removed, allowing follow-up examination | |||
|
22 | of the upgrade results. It is, however, created in /tmp by default, | |||
|
23 | which many Linux distributions automatically clean at regular intervals. | |||
|
24 | EOD | |||
|
25 | exit 1 | |||
|
26 | fi | |||
|
27 | ||||
|
28 | config_file=$(readlink -f "$1") | |||
|
29 | from_rev=$2 | |||
|
30 | to_rev=$3 | |||
|
31 | source_repo=$(dirname "$(dirname "$(readlink -f "$0")")") | |||
|
32 | ||||
|
33 | announce() { | |||
|
34 | echo | |||
|
35 | echo "$1" | |||
|
36 | echo | |||
|
37 | } | |||
|
38 | ||||
|
39 | quiet_if_ok() ( | |||
|
40 | local output | |||
|
41 | local st | |||
|
42 | set +e | |||
|
43 | output=$("$@" < /dev/null 2>&1) | |||
|
44 | st=$? | |||
|
45 | if [ $st -ne 0 ]; then | |||
|
46 | echo "$output" >&2 | |||
|
47 | echo "Command $@ returned exit status $st." >&2 | |||
|
48 | exit 1 | |||
|
49 | fi | |||
|
50 | ) | |||
|
51 | ||||
|
52 | HG() { | |||
|
53 | "${HG:-hg}" --repository "$source_repo" "$@" | |||
|
54 | } | |||
|
55 | ||||
|
56 | # If upgrading to "current revision", warn if working directory is dirty. | |||
|
57 | if [ ! "$to_rev" ] && [ "$(HG status -mard)" ]; then | |||
|
58 | announce "Warning: Uncommitted changes in working directory will be ignored!" | |||
|
59 | fi | |||
|
60 | ||||
|
61 | from_rev_hash=$(HG id --id --rev "${from_rev:-.}") | |||
|
62 | to_rev_hash=$(HG id --id --rev "${to_rev:-.}") | |||
|
63 | temp=$(readlink -f "$(mktemp --tmpdir -d 'dbmigrate-test.XXXXXX')") | |||
|
64 | ||||
|
65 | cat <<EOD | |||
|
66 | Config file: $config_file | |||
|
67 | EOD | |||
|
68 | sed -n -e 's/^sqlalchemy\.db1\.url *= */Database URL: /p' "$config_file" | |||
|
69 | cat <<EOD | |||
|
70 | Working dir: $temp | |||
|
71 | Repository: $source_repo | |||
|
72 | Upgrade from: $from_rev_hash (${from_rev:-current}) | |||
|
73 | Upgrade to: $to_rev_hash (${to_rev:-current}) | |||
|
74 | Extra packages: ${EXTRA:-(none)} | |||
|
75 | EOD | |||
|
76 | ||||
|
77 | mkdir "$temp/repos" # empty | |||
|
78 | ||||
|
79 | # Enable caching for old pip versions (this will cache the pip upgrade) | |||
|
80 | # Newer pip versions cache automatically, and don't use this variable. | |||
|
81 | if [ ! "$PIP_DOWNLOAD_CACHE" ]; then | |||
|
82 | export PIP_DOWNLOAD_CACHE=$HOME/.cache/pip/legacy | |||
|
83 | fi | |||
|
84 | ||||
|
85 | install_kallithea() { | |||
|
86 | local prefix=$1 | |||
|
87 | local rev=$2 | |||
|
88 | ||||
|
89 | announce "Installing Kallithea $rev in $prefix..." | |||
|
90 | ||||
|
91 | "${VIRTUALENV:-virtualenv}" --quiet "$prefix-env" | |||
|
92 | HG archive --rev "$rev" "$prefix" | |||
|
93 | ||||
|
94 | ( | |||
|
95 | cd "$prefix" | |||
|
96 | . "$prefix-env/bin/activate" | |||
|
97 | pip install --quiet --upgrade pip setuptools mercurial $EXTRA | |||
|
98 | pip install --quiet -e . | |||
|
99 | ) | |||
|
100 | } | |||
|
101 | ||||
|
102 | install_kallithea "$temp/from" "$from_rev_hash" | |||
|
103 | ( | |||
|
104 | cd "$temp/from" | |||
|
105 | . "$temp/from-env/bin/activate" | |||
|
106 | announce "Initializing database..." | |||
|
107 | quiet_if_ok paster setup-db "$config_file" --repos="$temp/repos" --user=doe --email=doe@example.com --password=123456 --no-public-access --force-yes | |||
|
108 | alembic -c "$config_file" current -v | |||
|
109 | ) | |||
|
110 | ||||
|
111 | install_kallithea "$temp/to" "$to_rev_hash" | |||
|
112 | ( | |||
|
113 | cd "$temp/to" | |||
|
114 | . "$temp/to-env/bin/activate" | |||
|
115 | ||||
|
116 | announce "Commencing database upgrade from shown Alembic revision to head..." | |||
|
117 | alembic -c "$config_file" current -v | |||
|
118 | alembic -c "$config_file" upgrade head | |||
|
119 | announce "Upgrade complete, now at the shown Alembic revision:" | |||
|
120 | alembic -c "$config_file" current -v | |||
|
121 | ) | |||
|
122 |
General Comments 0
You need to be logged in to leave comments.
Login now