sqlite_db_migration.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "sqlite_db_migration.h"
  3. static int return_int_cb(void *data, int argc, char **argv, char **column)
  4. {
  5. int *status = data;
  6. UNUSED(argc);
  7. UNUSED(column);
  8. *status = str2uint32_t(argv[0], NULL);
  9. return 0;
  10. }
  11. int table_exists_in_database(const char *table)
  12. {
  13. char *err_msg = NULL;
  14. char sql[128];
  15. int exists = 0;
  16. snprintf(sql, 127, "select 1 from sqlite_schema where type = 'table' and name = '%s';", table);
  17. int rc = sqlite3_exec_monitored(db_meta, sql, return_int_cb, (void *) &exists, &err_msg);
  18. if (rc != SQLITE_OK) {
  19. info("Error checking table existence; %s", err_msg);
  20. sqlite3_free(err_msg);
  21. }
  22. return exists;
  23. }
  24. static int column_exists_in_table(const char *table, const char *column)
  25. {
  26. char *err_msg = NULL;
  27. char sql[128];
  28. int exists = 0;
  29. snprintf(sql, 127, "SELECT 1 FROM pragma_table_info('%s') where name = '%s';", table, column);
  30. int rc = sqlite3_exec_monitored(db_meta, sql, return_int_cb, (void *) &exists, &err_msg);
  31. if (rc != SQLITE_OK) {
  32. info("Error checking column existence; %s", err_msg);
  33. sqlite3_free(err_msg);
  34. }
  35. return exists;
  36. }
  37. const char *database_migrate_v1_v2[] = {
  38. "ALTER TABLE host ADD hops INTEGER NOT NULL DEFAULT 0;",
  39. NULL
  40. };
  41. const char *database_migrate_v2_v3[] = {
  42. "ALTER TABLE host ADD memory_mode INT NOT NULL DEFAULT 0;",
  43. "ALTER TABLE host ADD abbrev_timezone TEXT NOT NULL DEFAULT '';",
  44. "ALTER TABLE host ADD utc_offset INT NOT NULL DEFAULT 0;",
  45. "ALTER TABLE host ADD program_name TEXT NOT NULL DEFAULT 'unknown';",
  46. "ALTER TABLE host ADD program_version TEXT NOT NULL DEFAULT 'unknown';",
  47. "ALTER TABLE host ADD entries INT NOT NULL DEFAULT 0;",
  48. "ALTER TABLE host ADD health_enabled INT NOT NULL DEFAULT 0;",
  49. NULL
  50. };
  51. const char *database_migrate_v4_v5[] = {
  52. "DROP TABLE IF EXISTS chart_active;",
  53. "DROP TABLE IF EXISTS dimension_active;",
  54. "DROP TABLE IF EXISTS chart_hash;",
  55. "DROP TABLE IF EXISTS chart_hash_map;",
  56. "DROP VIEW IF EXISTS v_chart_hash;",
  57. NULL
  58. };
  59. const char *database_migrate_v5_v6[] = {
  60. "DROP TRIGGER IF EXISTS tr_dim_del;",
  61. "DROP TABLE IF EXISTS dimension_delete;",
  62. NULL
  63. };
  64. static int do_migration_v1_v2(sqlite3 *database, const char *name)
  65. {
  66. UNUSED(name);
  67. info("Running \"%s\" database migration", name);
  68. if (table_exists_in_database("host") && !column_exists_in_table("host", "hops"))
  69. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v1_v2[0]);
  70. return 0;
  71. }
  72. static int do_migration_v2_v3(sqlite3 *database, const char *name)
  73. {
  74. UNUSED(name);
  75. info("Running \"%s\" database migration", name);
  76. if (table_exists_in_database("host") && !column_exists_in_table("host", "memory_mode"))
  77. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v2_v3[0]);
  78. return 0;
  79. }
  80. static int do_migration_v3_v4(sqlite3 *database, const char *name)
  81. {
  82. UNUSED(name);
  83. info("Running database migration %s", name);
  84. char sql[256];
  85. int rc;
  86. sqlite3_stmt *res = NULL;
  87. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%';");
  88. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  89. if (rc != SQLITE_OK) {
  90. error_report("Failed to prepare statement to alter health_log tables");
  91. return 1;
  92. }
  93. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  94. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  95. if (!column_exists_in_table(table, "chart_context")) {
  96. snprintfz(sql, 255, "ALTER TABLE %s ADD chart_context text", table);
  97. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  98. }
  99. freez(table);
  100. }
  101. rc = sqlite3_finalize(res);
  102. if (unlikely(rc != SQLITE_OK))
  103. error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
  104. return 0;
  105. }
  106. static int do_migration_v4_v5(sqlite3 *database, const char *name)
  107. {
  108. UNUSED(name);
  109. info("Running \"%s\" database migration", name);
  110. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v4_v5[0]);
  111. }
  112. static int do_migration_v5_v6(sqlite3 *database, const char *name)
  113. {
  114. UNUSED(name);
  115. info("Running \"%s\" database migration", name);
  116. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v5_v6[0]);
  117. }
  118. static int do_migration_v6_v7(sqlite3 *database, const char *name)
  119. {
  120. UNUSED(name);
  121. info("Running \"%s\" database migration", name);
  122. char sql[256];
  123. int rc;
  124. sqlite3_stmt *res = NULL;
  125. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'aclk_alert_%%';");
  126. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  127. if (rc != SQLITE_OK) {
  128. error_report("Failed to prepare statement to alter aclk_alert tables");
  129. return 1;
  130. }
  131. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  132. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  133. if (!column_exists_in_table(table, "filtered_alert_unique_id")) {
  134. snprintfz(sql, 255, "ALTER TABLE %s ADD filtered_alert_unique_id", table);
  135. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  136. snprintfz(sql, 255, "UPDATE %s SET filtered_alert_unique_id = alert_unique_id", table);
  137. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  138. }
  139. freez(table);
  140. }
  141. rc = sqlite3_finalize(res);
  142. if (unlikely(rc != SQLITE_OK))
  143. error_report("Failed to finalize statement when altering aclk_alert tables, rc = %d", rc);
  144. return 0;
  145. }
  146. static int do_migration_v7_v8(sqlite3 *database, const char *name)
  147. {
  148. UNUSED(name);
  149. info("Running database migration %s", name);
  150. char sql[256];
  151. int rc;
  152. sqlite3_stmt *res = NULL;
  153. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%';");
  154. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  155. if (rc != SQLITE_OK) {
  156. error_report("Failed to prepare statement to alter health_log tables");
  157. return 1;
  158. }
  159. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  160. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  161. if (!column_exists_in_table(table, "transition_id")) {
  162. snprintfz(sql, 255, "ALTER TABLE %s ADD transition_id blob", table);
  163. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  164. }
  165. freez(table);
  166. }
  167. rc = sqlite3_finalize(res);
  168. if (unlikely(rc != SQLITE_OK))
  169. error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
  170. return 0;
  171. }
  172. static int do_migration_noop(sqlite3 *database, const char *name)
  173. {
  174. UNUSED(database);
  175. UNUSED(name);
  176. info("Running database migration %s", name);
  177. return 0;
  178. }
  179. typedef struct database_func_migration_list {
  180. char *name;
  181. int (*func)(sqlite3 *database, const char *name);
  182. } DATABASE_FUNC_MIGRATION_LIST;
  183. static int migrate_database(sqlite3 *database, int target_version, char *db_name, DATABASE_FUNC_MIGRATION_LIST *migration_list)
  184. {
  185. int user_version = 0;
  186. char *err_msg = NULL;
  187. int rc = sqlite3_exec_monitored(database, "PRAGMA user_version;", return_int_cb, (void *) &user_version, &err_msg);
  188. if (rc != SQLITE_OK) {
  189. info("Error checking the %s database version; %s", db_name, err_msg);
  190. sqlite3_free(err_msg);
  191. }
  192. if (likely(user_version == target_version)) {
  193. info("%s database version is %d (no migration needed)", db_name, target_version);
  194. return target_version;
  195. }
  196. info("Database version is %d, current version is %d. Running migration for %s ...", user_version, target_version, db_name);
  197. for (int i = user_version; i < target_version && migration_list[i].func; i++) {
  198. rc = (migration_list[i].func)(database, migration_list[i].name);
  199. if (unlikely(rc)) {
  200. error_report("Database %s migration from version %d to version %d failed", db_name, i, i + 1);
  201. return i;
  202. }
  203. }
  204. return target_version;
  205. }
  206. DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
  207. {.name = "v0 to v1", .func = do_migration_noop},
  208. {.name = "v1 to v2", .func = do_migration_v1_v2},
  209. {.name = "v2 to v3", .func = do_migration_v2_v3},
  210. {.name = "v3 to v4", .func = do_migration_v3_v4},
  211. {.name = "v4 to v5", .func = do_migration_v4_v5},
  212. {.name = "v5 to v6", .func = do_migration_v5_v6},
  213. {.name = "v6 to v7", .func = do_migration_v6_v7},
  214. {.name = "v7 to v8", .func = do_migration_v7_v8},
  215. // the terminator of this array
  216. {.name = NULL, .func = NULL}
  217. };
  218. DATABASE_FUNC_MIGRATION_LIST context_migration_action[] = {
  219. {.name = "v0 to v1", .func = do_migration_noop},
  220. // the terminator of this array
  221. {.name = NULL, .func = NULL}
  222. };
  223. int perform_database_migration(sqlite3 *database, int target_version)
  224. {
  225. return migrate_database(database, target_version, "metadata", migration_action);
  226. }
  227. int perform_context_database_migration(sqlite3 *database, int target_version)
  228. {
  229. return migrate_database(database, target_version, "context", context_migration_action);
  230. }