summaryrefslogtreecommitdiff
path: root/libpurple/purplesqlite3.c
blob: c0775214d25f4623a9c2e586ffaebd2382b80737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>

#include "purplesqlite3.h"

/******************************************************************************
 * Helpers
 *****************************************************************************/
static gboolean
purple_sqlite3_run_migration(sqlite3 *db, int version, const char *migration,
                             GError **error)
{
	char *errmsg = NULL;
	char *str = NULL;
	gboolean success = TRUE;

	str = g_strdup_printf("BEGIN;%s;PRAGMA user_version=%d;COMMIT;", migration,
	                      version);

	sqlite3_exec(db, str, NULL, NULL, &errmsg);
	if(errmsg != NULL) {
		g_set_error(error, PURPLE_SQLITE3_DOMAIN, 0,
		            "failed to run migration: %s", errmsg);

		sqlite3_free(errmsg);

		sqlite3_exec(db, "ROLLBACK", NULL, NULL, &errmsg);
		if(errmsg != NULL) {
			g_error("failed to rollback transaction: %s", errmsg);

			sqlite3_free(errmsg);
		}

		success = FALSE;
	}

	g_free(str);

	return success;
}

/******************************************************************************
 * Public API
 *****************************************************************************/
int
purple_sqlite3_get_schema_version(sqlite3 *db, GError **error) {
	sqlite3_stmt *stmt = NULL;
	int version = -1;

	g_return_val_if_fail(db != NULL, -1);

	sqlite3_prepare_v2(db, "PRAGMA user_version", -1, &stmt, NULL);

	if(stmt == NULL) {
		g_set_error(error, PURPLE_SQLITE3_DOMAIN, 0,
		            "error while creating prepared statement: %s",
		            sqlite3_errmsg(db));

		return -1;
	}

	if(sqlite3_step(stmt) == SQLITE_ROW) {
		version = sqlite3_column_int(stmt, 0);
	} else {
		g_set_error_literal(error, PURPLE_SQLITE3_DOMAIN, 0,
		                    "'PRAGMA user_version' didn't return a row");

		sqlite3_finalize(stmt);

		return -1;
	}

	sqlite3_finalize(stmt);

	return version;
}

gboolean
purple_sqlite3_run_migrations_from_strings(sqlite3 *db,
                                           const char *migrations[],
                                           GError **error)
{
	int current_version = 0;
	guint n_migrations = 0;

	g_return_val_if_fail(db != NULL, FALSE);
	g_return_val_if_fail(migrations != NULL, FALSE);

	/* Get the current version or bail if it failed. */
	current_version = purple_sqlite3_get_schema_version(db, error);
	if(current_version == -1) {
		return FALSE;
	}

	n_migrations = g_strv_length((char **)migrations);
	if((guint)current_version > n_migrations) {
		g_set_error(error, PURPLE_SQLITE3_DOMAIN, 0,
		            "schema version %u is higher than known migrations %u",
		            (guint)current_version, n_migrations);

		return FALSE;
	}

	for(int i = current_version; migrations[i] != NULL; i++) {
		int version = i + 1;

		if(!purple_sqlite3_run_migration(db, version, migrations[i], error)) {
			return FALSE;
		}
	}

	return TRUE;
}

gboolean
purple_sqlite3_run_migrations_from_resources(sqlite3 *db, const char *path,
                                             const char *migrations[],
                                             GError **error)
{
	GError *local_error = NULL;
	int current_version = 0;
	guint n_migrations = 0;

	g_return_val_if_fail(db != NULL, FALSE);
	g_return_val_if_fail(path != NULL, FALSE);
	g_return_val_if_fail(migrations != NULL, FALSE);

	/* Get the current version or bail if it failed. */
	current_version = purple_sqlite3_get_schema_version(db, error);
	if(current_version == -1) {
		return FALSE;
	}

	n_migrations = g_strv_length((char **)migrations);
	if((guint)current_version > n_migrations) {
		g_set_error(error, PURPLE_SQLITE3_DOMAIN, 0,
		            "schema version %u is higher than known migrations %u",
		            (guint)current_version, n_migrations);

		return FALSE;
	}

	/* `PRAGMA user_version` starts at 0, so write our version as i + 1. We
	 * start iterating the list of migrations at the current version of the
	 * database. If the database is already up to date, then current_version
	 * will point us at the null terminator in the list of migrations, which
	 * will short circuit the for loop.
	 */
	for(int i = current_version; migrations[i] != NULL; i++) {
		GBytes *data = NULL;
		char *full_path = NULL;
		const gchar *migration = NULL;
		int version = i + 1;

		/* Get the data from the resource */
		full_path = g_build_path("/", path, migrations[i], NULL);

		data = g_resources_lookup_data(full_path, G_RESOURCE_LOOKUP_FLAGS_NONE,
		                               &local_error);
		if(data == NULL || local_error != NULL) {
			if(local_error == NULL) {
				local_error = g_error_new(PURPLE_SQLITE3_DOMAIN, 0,
				                          "failed to load resource %s",
				                          full_path);
			}

			g_propagate_error(error, local_error);

			g_clear_pointer(&data, g_bytes_unref);
			g_free(full_path);

			return FALSE;
		}

		g_free(full_path);

		migration = (const char *)g_bytes_get_data(data, NULL);
		if(!purple_sqlite3_run_migration(db, version, migration, error)) {
			g_bytes_unref(data);

			return FALSE;
		}

		g_bytes_unref(data);
	}

	return TRUE;
}