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)
6 # "One does not simply 'upgrade the schema'..."
8 # When changing Quassel's database schema, you need to follow several steps to
9 # handle all cases (upgrade, Postgres migration, etc).
11 # 1. Run this script on -both- the PostgreSQL and SQLite directory
13 # Make sure you're on the Git branch you want to modify
14 # > ./upgradeSchema.sh "PostgreSQL"
15 # > ./upgradeSchema.sh "SQLite"
17 # 2. Modify queries and setup scripts to handle your change
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.
23 # 3. Create an upgrade script for -both- PostgreSQL and SQLite
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]
30 # 4. Create a pair of migration scripts for moving from SQLite to PostgreSQL
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
36 # 5. Add the new SQL queries to 'src/core/sql.qrc', update all existing files
38 # You may need to re-run CMake to detect these changes.
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>
49 # 6. Update the migration logic in 'src/core/abstractsqlstorage.h', and the
50 # storage backends 'postgresqlstorage.cpp' and 'sqlitestorage.cpp'
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
62 # 7. Update any affected queries in storage backends 'postgresqlstorage.cpp'
63 # and 'sqlitestorage.cpp', and any related synchronized 'src/common' classes.
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'
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'
79 # 8. If protocol changed (add a setting, etc), add a new "Feature" flag
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) { ... }'
85 # 9. Test everything! Upgrade, migrate, new setups, new client/old core,
86 # old client/new core, etc.
89 # If not specified, assume current directory
90 if [ ! "$TARGET_DIR" ]; then
94 if [[ ! -d "$TARGET_DIR" ]]; then
95 echo "No such directory '$TARGET_DIR'"
101 # Grab the current schema version
102 CURRENT_VERSION=$(ls | sort -n | tail -n1)
104 if [ ! $CURRENT_VERSION ]; then
105 echo "No previous schema found to upgrade from"
110 ((NEW_VERSION=$CURRENT_VERSION + 1))
112 # Create the new schema directory, add the directory, and move all files over...
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" \;