d31d6137f4aba104736b29cbc8e211eec52af4d1
[quassel.git] / src / core / SQL / upgradeSchema.sh
1 #!/bin/bash
2 # Copyright (C) 2005-2019 by the Quassel Project - devel@quassel-irc.org
3 # Licensed under GNU General Public License version 2, or (at your option)
4 # version 3.
5 #
6 # Call this script whenever you need to add a new database schema version.
7 # You can also simply create a new folder in '[SQLite/PostgreSQL]/version/##',
8 # where '##' represents your new version number, incremented by one from
9 # whatever the current latest version is (e.g. 21 -> 22).
10 #
11 # NOTE: If you upgrade the database schema version, you must add upgrade
12 # scripts and modify the existing setup queries.  See 'README.md' for details.
13
14 TARGET_DIR="$1"
15 # If not specified, assume current directory
16 if [ ! "$TARGET_DIR" ]; then
17     TARGET_DIR="$(pwd)"
18 fi
19
20 if [[ ! -d "$TARGET_DIR" ]]; then
21     echo "No such directory '$TARGET_DIR'" >&2
22     exit 1
23 fi
24
25 # Find out the name of the target directory to offer some guidance later.
26 TARGET_DB_NAME=$(basename "$TARGET_DIR")
27
28 # Upgrade scripts are stored in the 'version' subdirectory, e.g.
29 # 'SQL/[database]/version/##'.
30 cd "$TARGET_DIR/version"
31
32 # Grab the current schema version
33 CURRENT_VERSION=$(ls | sort -n | tail -n1)
34
35 if [ ! $CURRENT_VERSION ]; then
36     echo "No previous schema found to upgrade from" >&2
37     exit 2
38 fi
39
40 # Increment by one
41 ((NEW_VERSION=$CURRENT_VERSION + 1))
42
43 # Create the new schema directory.
44 mkdir "$NEW_VERSION"
45 # Git doesn't track empty folders, no need for 'git add "$NEW_VERSION"'.
46
47 echo "New schema version '$TARGET_DB_NAME/version/$NEW_VERSION' created." >&2
48 echo "Create any needed 'upgrade_[...].sql' scripts in this folder." >&2
49
50 # Don't move any files over.  Schema version upgrade scripts are now stored
51 # independently of the main SQL files in order to make the repository history
52 # more useful and easier to work with.
53
54 # Granted, this script doesn't do anything one couldn't easily manually do.
55 # I'd argue that's a good thing.  Though as this script offers documentation
56 # and guidance for contributors new to the database schema system as well as
57 # helping migrate those used to the older method, it seems worthwhile keeping.