Improve performance of PostgreSQL for large databases.
[quassel.git] / src / core / SQL / upgradeSchema.sh
1 #!/bin/bash
2 # Copyright (C) 2005-2016 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 # "One does not simply 'upgrade the schema'..."
7 #
8 # When changing Quassel's database schema, you need to follow several steps to
9 # handle all cases (upgrade, Postgres migration, etc).
10 #
11 # 1.  Run this script on -both- the PostgreSQL and SQLite directory
12 #
13 # Make sure you're on the Git branch you want to modify
14 # > ./upgradeSchema.sh "PostgreSQL"
15 # > ./upgradeSchema.sh "SQLite"
16 #
17 # 2.  Modify queries and setup scripts to handle your change
18 #
19 # [Example] Modifying the 'ircserver' table to add column 'test'
20 # Modify all query/setup .sql files that touch the 'ircserver' table for
21 # -both- PostgreSQL and SQLite.
22 #
23 # 3.  Create an upgrade script for -both- PostgreSQL and SQLite
24 #
25 # [Example] Modifying the 'ircserver' table to add column 'test'
26 # Add the file 'upgrade_000_alter_ircserver_add_test.sql' with contents:
27 # > ALTER TABLE ircserver
28 # > ADD COLUMN test [additional column-specific details]
29 #
30 # 4.  Create a pair of migration scripts for moving from SQLite to PostgreSQL
31 #
32 # [Example] Modifying the 'ircserver' table to add column 'test'
33 # > Modify 'SQLite/##/migrate_read_ircserver.sql' to select from new column
34 # > Modify 'PostgreSQL/##/migrate_write_ircserver.sql' to insert to new column
35 #
36 # 5.  Add the new SQL queries to 'src/core/sql.qrc', update all existing files
37 #
38 # You may need to re-run CMake to detect these changes.
39 #
40 # [Example] Modifying the 'ircserver' table to add column 'test'
41 # > Add the new upgrade script...
42 #   <file>./SQL/SQLite/19/upgrade_000_alter_ircserver_add_test.sql</file>
43 #   <file>./SQL/PostgreSQL/18/upgrade_000_alter_ircserver_add_test.sql</file>
44 # > Find/replace all non-upgrade scripts from the old schema number to new one
45 #   <file>./SQL/SQLite/[18->19]/update_buffer_persistent_channel.sql</file>
46 #   <file>./SQL/PostgreSQL/[17->18]/update_buffer_persistent_channel.sql</file>
47 #   (etc)
48 #
49 # 6.  Update the migration logic in 'src/core/abstractsqlstorage.h', and the
50 # storage backends 'postgresqlstorage.cpp' and 'sqlitestorage.cpp'
51 #
52 # [Example] Modifying the 'ircserver' table to add column 'test'
53 # > Modify struct 'IrcServerMO' in 'abstractsqlstorage.h', adding an entry for
54 #   'test' of the appropriate data-type.
55 # > Modify 'SqliteMigrationReader::readMo(IrcServerMO &ircserver)' in
56 #   'sqlitestorage.cpp' to read from the new column and store it in the
57 #   migration object.  You may need to convert from SQLite's looser types.
58 # > Modify 'PostgreSqlMigrationWriter::writeMo(const IrcServerMO &ircserver)'
59 #   in 'postgresqlstorage.cpp' to write to the new column from the data in the
60 #   migration object.
61 #
62 # 7.  Update any affected queries in storage backends 'postgresqlstorage.cpp'
63 # and 'sqlitestorage.cpp', and any related synchronized 'src/common' classes.
64 #
65 # [Example] Modifying the 'ircserver' table to add column 'test'
66 # > Update 'network.h' to add new column to Server structure
67 #   QString proxyPass;                                     // Existing code
68 #   Typename test;                                         // New column 'test'
69 #   [...]
70 #   Server() : port(6667), ..., proxyPort(8080), test("defaultValue") {}
71 # > Modify reading data in ____Storage::networks(...)
72 #   server.proxyPass = serversQuery.value(10).toString();  // Existing code
73 #   server.test = serversQuery.value(11).toType();         // New column 'test'
74 #   servers << server;                                     // Existing code
75 # > Modify writing data in ____Storage::bindServerInfo(...)
76 #   query.bindValue(":proxypass", server.proxyPass);       // Existing code
77 #   query.bindValue(":test", server.test);                 // New column 'test'
78 #
79 # 8.  If protocol changed (add a setting, etc), add a new "Feature" flag
80 #
81 # Newer clients need to detect when they're on an older core to disable the
82 # feature.  Use 'enum Feature' in 'quassel.h'.  In client-side code, test with
83 # 'if (Client::coreFeatures() & Quassel::FeatureName) { ... }'
84 #
85 # 9.  Test everything!  Upgrade, migrate, new setups, new client/old core,
86 # old client/new core, etc.
87
88 TARGET_DIR="$1"
89 # If not specified, assume current directory
90 if [ ! "$TARGET_DIR" ]; then
91     TARGET_DIR="$(pwd)"
92 fi
93
94 if [[ ! -d "$TARGET_DIR" ]]; then
95     echo "No such directory '$TARGET_DIR'"
96     exit 1
97 fi
98
99 cd "$TARGET_DIR"
100
101 # Grab the current schema version
102 CURRENT_VERSION=$(ls | sort -n | tail -n1)
103
104 if [ ! $CURRENT_VERSION ]; then
105     echo "No previous schema found to upgrade from"
106     exit 2
107 fi
108
109 # Increment by one
110 ((NEW_VERSION=$CURRENT_VERSION + 1))
111
112 # Create the new schema directory, add the directory, and move all files over...
113 mkdir "$NEW_VERSION"
114 git add "$NEW_VERSION"
115 # ...except for 'upgrade_' scripts.
116 find "$CURRENT_VERSION" -maxdepth 1 -type f \! -name "upgrade_*" \! -name ".*" -exec git mv {} "$NEW_VERSION" \;