sqlite_db_migration.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. netdata_log_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. netdata_log_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. const char *database_migrate_v9_v10[] = {
  65. "ALTER TABLE alert_hash ADD chart_labels TEXT;",
  66. NULL
  67. };
  68. static int do_migration_v1_v2(sqlite3 *database, const char *name)
  69. {
  70. UNUSED(name);
  71. netdata_log_info("Running \"%s\" database migration", name);
  72. if (table_exists_in_database("host") && !column_exists_in_table("host", "hops"))
  73. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v1_v2[0]);
  74. return 0;
  75. }
  76. static int do_migration_v2_v3(sqlite3 *database, const char *name)
  77. {
  78. UNUSED(name);
  79. netdata_log_info("Running \"%s\" database migration", name);
  80. if (table_exists_in_database("host") && !column_exists_in_table("host", "memory_mode"))
  81. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v2_v3[0]);
  82. return 0;
  83. }
  84. static int do_migration_v3_v4(sqlite3 *database, const char *name)
  85. {
  86. UNUSED(name);
  87. netdata_log_info("Running database migration %s", name);
  88. char sql[256];
  89. int rc;
  90. sqlite3_stmt *res = NULL;
  91. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%';");
  92. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  93. if (rc != SQLITE_OK) {
  94. error_report("Failed to prepare statement to alter health_log tables");
  95. return 1;
  96. }
  97. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  98. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  99. if (!column_exists_in_table(table, "chart_context")) {
  100. snprintfz(sql, 255, "ALTER TABLE %s ADD chart_context text", table);
  101. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  102. }
  103. freez(table);
  104. }
  105. rc = sqlite3_finalize(res);
  106. if (unlikely(rc != SQLITE_OK))
  107. error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
  108. return 0;
  109. }
  110. static int do_migration_v4_v5(sqlite3 *database, const char *name)
  111. {
  112. UNUSED(name);
  113. netdata_log_info("Running \"%s\" database migration", name);
  114. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v4_v5[0]);
  115. }
  116. static int do_migration_v5_v6(sqlite3 *database, const char *name)
  117. {
  118. UNUSED(name);
  119. netdata_log_info("Running \"%s\" database migration", name);
  120. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v5_v6[0]);
  121. }
  122. static int do_migration_v6_v7(sqlite3 *database, const char *name)
  123. {
  124. UNUSED(name);
  125. netdata_log_info("Running \"%s\" database migration", name);
  126. char sql[256];
  127. int rc;
  128. sqlite3_stmt *res = NULL;
  129. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'aclk_alert_%%';");
  130. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  131. if (rc != SQLITE_OK) {
  132. error_report("Failed to prepare statement to alter aclk_alert tables");
  133. return 1;
  134. }
  135. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  136. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  137. if (!column_exists_in_table(table, "filtered_alert_unique_id")) {
  138. snprintfz(sql, 255, "ALTER TABLE %s ADD filtered_alert_unique_id", table);
  139. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  140. snprintfz(sql, 255, "UPDATE %s SET filtered_alert_unique_id = alert_unique_id", table);
  141. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  142. }
  143. freez(table);
  144. }
  145. rc = sqlite3_finalize(res);
  146. if (unlikely(rc != SQLITE_OK))
  147. error_report("Failed to finalize statement when altering aclk_alert tables, rc = %d", rc);
  148. return 0;
  149. }
  150. static int do_migration_v7_v8(sqlite3 *database, const char *name)
  151. {
  152. UNUSED(name);
  153. netdata_log_info("Running database migration %s", name);
  154. char sql[256];
  155. int rc;
  156. sqlite3_stmt *res = NULL;
  157. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%';");
  158. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  159. if (rc != SQLITE_OK) {
  160. error_report("Failed to prepare statement to alter health_log tables");
  161. return 1;
  162. }
  163. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  164. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  165. if (!column_exists_in_table(table, "transition_id")) {
  166. snprintfz(sql, 255, "ALTER TABLE %s ADD transition_id blob", table);
  167. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  168. }
  169. freez(table);
  170. }
  171. rc = sqlite3_finalize(res);
  172. if (unlikely(rc != SQLITE_OK))
  173. error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
  174. return 0;
  175. }
  176. static int do_migration_v8_v9(sqlite3 *database, const char *name)
  177. {
  178. netdata_log_info("Running database migration %s", name);
  179. char sql[2048];
  180. int rc;
  181. sqlite3_stmt *res = NULL;
  182. //create the health_log table and it's index
  183. snprintfz(sql, 2047, "CREATE TABLE IF NOT EXISTS health_log (health_log_id INTEGER PRIMARY KEY, host_id blob, alarm_id int, " \
  184. "config_hash_id blob, name text, chart text, family text, recipient text, units text, exec text, " \
  185. "chart_context text, last_transition_id blob, UNIQUE (host_id, alarm_id)) ;");
  186. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  187. //TODO indexes
  188. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_ind_1 ON health_log (host_id);");
  189. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  190. snprintfz(sql, 2047, "CREATE TABLE IF NOT EXISTS health_log_detail (health_log_id int, unique_id int, alarm_id int, alarm_event_id int, " \
  191. "updated_by_id int, updates_id int, when_key int, duration int, non_clear_duration int, " \
  192. "flags int, exec_run_timestamp int, delay_up_to_timestamp int, " \
  193. "info text, exec_code int, new_status real, old_status real, delay int, " \
  194. "new_value double, old_value double, last_repeat int, transition_id blob, global_id int, host_id blob);");
  195. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  196. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_d_ind_1 ON health_log_detail (unique_id);");
  197. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  198. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_d_ind_2 ON health_log_detail (global_id);");
  199. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  200. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_d_ind_3 ON health_log_detail (transition_id);");
  201. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  202. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_d_ind_4 ON health_log_detail (health_log_id);");
  203. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  204. snprintfz(sql, 2047, "ALTER TABLE alert_hash ADD source text;");
  205. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  206. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS alert_hash_index ON alert_hash (hash_id);");
  207. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  208. snprintfz(sql, 2047, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%' AND name <> 'health_log_detail';");
  209. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  210. if (rc != SQLITE_OK) {
  211. error_report("Failed to prepare statement to alter health_log tables");
  212. return 1;
  213. }
  214. DICTIONARY *dict_tables = dictionary_create(DICT_OPTION_NONE);
  215. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  216. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  217. if (health_migrate_old_health_log_table(table)) {
  218. dictionary_set(dict_tables, table, NULL, 0);
  219. }
  220. freez(table);
  221. }
  222. rc = sqlite3_finalize(res);
  223. if (unlikely(rc != SQLITE_OK))
  224. error_report("Failed to finalize statement when copying health_log tables, rc = %d", rc);
  225. char *table = NULL;
  226. dfe_start_read(dict_tables, table) {
  227. sql_drop_table(table_dfe.name);
  228. }
  229. dfe_done(table);
  230. dictionary_destroy(dict_tables);
  231. snprintfz(sql, 2047, "ALTER TABLE health_log_detail DROP COLUMN host_id;");
  232. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  233. return 0;
  234. }
  235. static int do_migration_v9_v10(sqlite3 *database, const char *name)
  236. {
  237. UNUSED(name);
  238. netdata_log_info("Running \"%s\" database migration", name);
  239. if (table_exists_in_database("alert_hash") && !column_exists_in_table("alert_hash", "chart_labels"))
  240. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v9_v10[0]);
  241. return 0;
  242. }
  243. static int do_migration_noop(sqlite3 *database, const char *name)
  244. {
  245. UNUSED(database);
  246. UNUSED(name);
  247. netdata_log_info("Running database migration %s", name);
  248. return 0;
  249. }
  250. typedef struct database_func_migration_list {
  251. char *name;
  252. int (*func)(sqlite3 *database, const char *name);
  253. } DATABASE_FUNC_MIGRATION_LIST;
  254. static int migrate_database(sqlite3 *database, int target_version, char *db_name, DATABASE_FUNC_MIGRATION_LIST *migration_list)
  255. {
  256. int user_version = 0;
  257. char *err_msg = NULL;
  258. int rc = sqlite3_exec_monitored(database, "PRAGMA user_version;", return_int_cb, (void *) &user_version, &err_msg);
  259. if (rc != SQLITE_OK) {
  260. netdata_log_info("Error checking the %s database version; %s", db_name, err_msg);
  261. sqlite3_free(err_msg);
  262. }
  263. if (likely(user_version == target_version)) {
  264. netdata_log_info("%s database version is %d (no migration needed)", db_name, target_version);
  265. return target_version;
  266. }
  267. netdata_log_info("Database version is %d, current version is %d. Running migration for %s ...", user_version, target_version, db_name);
  268. for (int i = user_version; i < target_version && migration_list[i].func; i++) {
  269. rc = (migration_list[i].func)(database, migration_list[i].name);
  270. if (unlikely(rc)) {
  271. error_report("Database %s migration from version %d to version %d failed", db_name, i, i + 1);
  272. return i;
  273. }
  274. }
  275. return target_version;
  276. }
  277. DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
  278. {.name = "v0 to v1", .func = do_migration_noop},
  279. {.name = "v1 to v2", .func = do_migration_v1_v2},
  280. {.name = "v2 to v3", .func = do_migration_v2_v3},
  281. {.name = "v3 to v4", .func = do_migration_v3_v4},
  282. {.name = "v4 to v5", .func = do_migration_v4_v5},
  283. {.name = "v5 to v6", .func = do_migration_v5_v6},
  284. {.name = "v6 to v7", .func = do_migration_v6_v7},
  285. {.name = "v7 to v8", .func = do_migration_v7_v8},
  286. {.name = "v8 to v9", .func = do_migration_v8_v9},
  287. {.name = "v9 to v10", .func = do_migration_v9_v10},
  288. // the terminator of this array
  289. {.name = NULL, .func = NULL}
  290. };
  291. DATABASE_FUNC_MIGRATION_LIST context_migration_action[] = {
  292. {.name = "v0 to v1", .func = do_migration_noop},
  293. // the terminator of this array
  294. {.name = NULL, .func = NULL}
  295. };
  296. int perform_database_migration(sqlite3 *database, int target_version)
  297. {
  298. return migrate_database(database, target_version, "metadata", migration_action);
  299. }
  300. int perform_context_database_migration(sqlite3 *database, int target_version)
  301. {
  302. return migrate_database(database, target_version, "context", context_migration_action);
  303. }