sqlite_db_migration.c 14 KB

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