docs: Move SQL schema to README.md, add more help
[quassel.git] / src / core / SQL / upgradeSchema.sh
1 #!/bin/bash
2 # Copyright (C) 2005-2018 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, then regenerate sql.qrc with
13 # 'updateSQLResource'.  See 'README.md' for details.
14
15 TARGET_DIR="$1"
16 # If not specified, assume current directory
17 if [ ! "$TARGET_DIR" ]; then
18     TARGET_DIR="$(pwd)"
19 fi
20
21 if [[ ! -d "$TARGET_DIR" ]]; then
22     echo "No such directory '$TARGET_DIR'" >&2
23     exit 1
24 fi
25
26 # Find out the name of the target directory to offer some guidance later.
27 TARGET_DB_NAME=$(basename "$TARGET_DIR")
28
29 # Upgrade scripts are stored in the 'version' subdirectory, e.g.
30 # 'SQL/[database]/version/##'.
31 cd "$TARGET_DIR/version"
32
33 # Grab the current schema version
34 CURRENT_VERSION=$(ls | sort -n | tail -n1)
35
36 if [ ! $CURRENT_VERSION ]; then
37     echo "No previous schema found to upgrade from" >&2
38     exit 2
39 fi
40
41 # Increment by one
42 ((NEW_VERSION=$CURRENT_VERSION + 1))
43
44 # Create the new schema directory.
45 mkdir "$NEW_VERSION"
46 # Git doesn't track empty folders, no need for 'git add "$NEW_VERSION"'.
47
48 echo "New schema version '$TARGET_DB_NAME/version/$NEW_VERSION' created." >&2
49 echo "Create any needed 'upgrade_[...].sql' scripts in this folder." >&2
50
51 # Don't move any files over.  Schema version upgrade scripts are now stored
52 # independently of the main SQL files in order to make the repository history
53 # more useful and easier to work with.
54
55 # Granted, this script doesn't do anything one couldn't easily manually do.
56 # I'd argue that's a good thing.  Though as this script offers documentation
57 # and guidance for contributors new to the database schema system as well as
58 # helping migrate those used to the older method, it seems worthwhile keeping.