sqlite_db_migration.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 = (int) str2uint32_t(argv[0], NULL);
  9. return 0;
  10. }
  11. static int get_auto_vaccum(sqlite3 *database)
  12. {
  13. char *err_msg = NULL;
  14. char sql[128];
  15. int exists = 0;
  16. snprintf(sql, sizeof(sql) - 1, "PRAGMA auto_vacuum");
  17. int rc = sqlite3_exec_monitored(database, sql, return_int_cb, (void *) &exists, &err_msg);
  18. if (rc != SQLITE_OK) {
  19. netdata_log_info("Error checking database auto vacuum setting; %s", err_msg);
  20. sqlite3_free(err_msg);
  21. }
  22. return exists;
  23. }
  24. int db_table_count(sqlite3 *database)
  25. {
  26. char *err_msg = NULL;
  27. char sql[128];
  28. int count = 0;
  29. snprintf(sql, sizeof(sql) - 1, "select count(1) from sqlite_schema where type = 'table'");
  30. int rc = sqlite3_exec_monitored(database, sql, return_int_cb, (void *) &count, &err_msg);
  31. if (rc != SQLITE_OK) {
  32. netdata_log_info("Error checking database table count; %s", err_msg);
  33. sqlite3_free(err_msg);
  34. }
  35. return count;
  36. }
  37. int table_exists_in_database(sqlite3 *database, const char *table)
  38. {
  39. char *err_msg = NULL;
  40. char sql[128];
  41. int exists = 0;
  42. snprintf(sql, sizeof(sql) - 1, "select 1 from sqlite_schema where type = 'table' and name = '%s'", table);
  43. int rc = sqlite3_exec_monitored(database, sql, return_int_cb, (void *) &exists, &err_msg);
  44. if (rc != SQLITE_OK) {
  45. netdata_log_info("Error checking table existence; %s", err_msg);
  46. sqlite3_free(err_msg);
  47. }
  48. return exists;
  49. }
  50. static int column_exists_in_table(sqlite3 *database, const char *table, const char *column)
  51. {
  52. char *err_msg = NULL;
  53. char sql[128];
  54. int exists = 0;
  55. snprintf(sql, sizeof(sql) - 1, "SELECT 1 FROM pragma_table_info('%s') where name = '%s'", table, column);
  56. int rc = sqlite3_exec_monitored(database, sql, return_int_cb, (void *) &exists, &err_msg);
  57. if (rc != SQLITE_OK) {
  58. netdata_log_info("Error checking column existence; %s", err_msg);
  59. sqlite3_free(err_msg);
  60. }
  61. return exists;
  62. }
  63. static int get_database_user_version(sqlite3 *database)
  64. {
  65. int user_version = 0;
  66. int rc = sqlite3_exec_monitored(database, "PRAGMA user_version", return_int_cb, (void *)&user_version, NULL);
  67. if (rc != SQLITE_OK)
  68. netdata_log_error("Failed to get user version for database");
  69. return user_version;
  70. }
  71. const char *database_migrate_v1_v2[] = {
  72. "ALTER TABLE host ADD hops INTEGER NOT NULL DEFAULT 0",
  73. NULL
  74. };
  75. const char *database_migrate_v2_v3[] = {
  76. "ALTER TABLE host ADD memory_mode INT NOT NULL DEFAULT 0",
  77. "ALTER TABLE host ADD abbrev_timezone TEXT NOT NULL DEFAULT ''",
  78. "ALTER TABLE host ADD utc_offset INT NOT NULL DEFAULT 0",
  79. "ALTER TABLE host ADD program_name TEXT NOT NULL DEFAULT 'unknown'",
  80. "ALTER TABLE host ADD program_version TEXT NOT NULL DEFAULT 'unknown'",
  81. "ALTER TABLE host ADD entries INT NOT NULL DEFAULT 0",
  82. "ALTER TABLE host ADD health_enabled INT NOT NULL DEFAULT 0",
  83. NULL
  84. };
  85. const char *database_migrate_v4_v5[] = {
  86. "DROP TABLE IF EXISTS chart_active",
  87. "DROP TABLE IF EXISTS dimension_active",
  88. "DROP TABLE IF EXISTS chart_hash",
  89. "DROP TABLE IF EXISTS chart_hash_map",
  90. "DROP VIEW IF EXISTS v_chart_hash",
  91. NULL
  92. };
  93. const char *database_migrate_v5_v6[] = {
  94. "DROP TRIGGER IF EXISTS tr_dim_del",
  95. "DROP TABLE IF EXISTS dimension_delete",
  96. NULL
  97. };
  98. const char *database_migrate_v9_v10[] = {
  99. "ALTER TABLE alert_hash ADD chart_labels TEXT",
  100. NULL
  101. };
  102. const char *database_migrate_v10_v11[] = {
  103. "ALTER TABLE health_log ADD chart_name TEXT",
  104. NULL
  105. };
  106. const char *database_migrate_v11_v12[] = {
  107. "ALTER TABLE health_log_detail ADD summary TEXT",
  108. "ALTER TABLE alert_hash ADD summary TEXT",
  109. NULL
  110. };
  111. const char *database_migrate_v12_v13_detail[] = {
  112. "ALTER TABLE health_log_detail ADD summary TEXT",
  113. NULL
  114. };
  115. const char *database_migrate_v12_v13_hash[] = {
  116. "ALTER TABLE alert_hash ADD summary TEXT",
  117. NULL
  118. };
  119. const char *database_migrate_v13_v14[] = {
  120. "ALTER TABLE host ADD last_connected INT NOT NULL DEFAULT 0",
  121. NULL
  122. };
  123. static int do_migration_v1_v2(sqlite3 *database)
  124. {
  125. if (table_exists_in_database(database, "host") && !column_exists_in_table(database, "host", "hops"))
  126. return init_database_batch(database, &database_migrate_v1_v2[0], "meta_migrate");
  127. return 0;
  128. }
  129. static int do_migration_v2_v3(sqlite3 *database)
  130. {
  131. if (table_exists_in_database(database, "host") && !column_exists_in_table(database, "host", "memory_mode"))
  132. return init_database_batch(database, &database_migrate_v2_v3[0], "meta_migrate");
  133. return 0;
  134. }
  135. static int do_migration_v3_v4(sqlite3 *database)
  136. {
  137. char sql[256];
  138. int rc;
  139. sqlite3_stmt *res = NULL;
  140. snprintfz(sql, sizeof(sql) - 1, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%'");
  141. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  142. if (rc != SQLITE_OK) {
  143. error_report("Failed to prepare statement to alter health_log tables");
  144. return 1;
  145. }
  146. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  147. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  148. if (!column_exists_in_table(database, table, "chart_context")) {
  149. snprintfz(sql, sizeof(sql) - 1, "ALTER TABLE %s ADD chart_context text", table);
  150. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  151. }
  152. freez(table);
  153. }
  154. rc = sqlite3_finalize(res);
  155. if (unlikely(rc != SQLITE_OK))
  156. error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
  157. return 0;
  158. }
  159. static int do_migration_v4_v5(sqlite3 *database)
  160. {
  161. return init_database_batch(database, &database_migrate_v4_v5[0], "meta_migrate");
  162. }
  163. static int do_migration_v5_v6(sqlite3 *database)
  164. {
  165. return init_database_batch(database, &database_migrate_v5_v6[0], "meta_migrate");
  166. }
  167. static int do_migration_v6_v7(sqlite3 *database)
  168. {
  169. char sql[256];
  170. int rc;
  171. sqlite3_stmt *res = NULL;
  172. snprintfz(sql, sizeof(sql) - 1, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'aclk_alert_%%'");
  173. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  174. if (rc != SQLITE_OK) {
  175. error_report("Failed to prepare statement to alter aclk_alert tables");
  176. return 1;
  177. }
  178. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  179. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  180. if (!column_exists_in_table(database, table, "filtered_alert_unique_id")) {
  181. snprintfz(sql, sizeof(sql) - 1, "ALTER TABLE %s ADD filtered_alert_unique_id", table);
  182. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  183. snprintfz(sql, sizeof(sql) - 1, "UPDATE %s SET filtered_alert_unique_id = alert_unique_id", table);
  184. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  185. }
  186. freez(table);
  187. }
  188. rc = sqlite3_finalize(res);
  189. if (unlikely(rc != SQLITE_OK))
  190. error_report("Failed to finalize statement when altering aclk_alert tables, rc = %d", rc);
  191. return 0;
  192. }
  193. static int do_migration_v7_v8(sqlite3 *database)
  194. {
  195. char sql[256];
  196. int rc;
  197. sqlite3_stmt *res = NULL;
  198. snprintfz(sql, sizeof(sql) - 1, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%'");
  199. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  200. if (rc != SQLITE_OK) {
  201. error_report("Failed to prepare statement to alter health_log tables");
  202. return 1;
  203. }
  204. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  205. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  206. if (!column_exists_in_table(database, table, "transition_id")) {
  207. snprintfz(sql, sizeof(sql) - 1, "ALTER TABLE %s ADD transition_id blob", table);
  208. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  209. }
  210. freez(table);
  211. }
  212. rc = sqlite3_finalize(res);
  213. if (unlikely(rc != SQLITE_OK))
  214. error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
  215. return 0;
  216. }
  217. static int do_migration_v8_v9(sqlite3 *database)
  218. {
  219. char sql[2048];
  220. int rc;
  221. sqlite3_stmt *res = NULL;
  222. //create the health_log table and it's index
  223. snprintfz(sql, sizeof(sql) - 1, "CREATE TABLE IF NOT EXISTS health_log (health_log_id INTEGER PRIMARY KEY, host_id blob, alarm_id int, " \
  224. "config_hash_id blob, name text, chart text, family text, recipient text, units text, exec text, " \
  225. "chart_context text, last_transition_id blob, UNIQUE (host_id, alarm_id))");
  226. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  227. //TODO indexes
  228. snprintfz(sql, sizeof(sql) - 1, "CREATE INDEX IF NOT EXISTS health_log_ind_1 ON health_log (host_id)");
  229. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  230. snprintfz(sql, sizeof(sql) - 1, "CREATE TABLE IF NOT EXISTS health_log_detail (health_log_id int, unique_id int, alarm_id int, alarm_event_id int, " \
  231. "updated_by_id int, updates_id int, when_key int, duration int, non_clear_duration int, " \
  232. "flags int, exec_run_timestamp int, delay_up_to_timestamp int, " \
  233. "info text, exec_code int, new_status real, old_status real, delay int, " \
  234. "new_value double, old_value double, last_repeat int, transition_id blob, global_id int, summary text, host_id blob)");
  235. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  236. snprintfz(sql, sizeof(sql) - 1, "CREATE INDEX IF NOT EXISTS health_log_d_ind_1 ON health_log_detail (unique_id)");
  237. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  238. snprintfz(sql, sizeof(sql) - 1, "CREATE INDEX IF NOT EXISTS health_log_d_ind_2 ON health_log_detail (global_id)");
  239. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  240. snprintfz(sql, sizeof(sql) - 1, "CREATE INDEX IF NOT EXISTS health_log_d_ind_3 ON health_log_detail (transition_id)");
  241. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  242. snprintfz(sql, sizeof(sql) - 1, "CREATE INDEX IF NOT EXISTS health_log_d_ind_4 ON health_log_detail (health_log_id)");
  243. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  244. snprintfz(sql, sizeof(sql) - 1, "ALTER TABLE alert_hash ADD source text");
  245. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  246. snprintfz(sql, sizeof(sql) - 1, "CREATE INDEX IF NOT EXISTS alert_hash_index ON alert_hash (hash_id)");
  247. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  248. snprintfz(sql, sizeof(sql) - 1, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%' AND name <> 'health_log_detail'");
  249. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  250. if (rc != SQLITE_OK) {
  251. error_report("Failed to prepare statement to alter health_log tables");
  252. return 1;
  253. }
  254. DICTIONARY *dict_tables = dictionary_create(DICT_OPTION_NONE);
  255. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  256. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  257. if (health_migrate_old_health_log_table(table)) {
  258. dictionary_set(dict_tables, table, NULL, 0);
  259. }
  260. freez(table);
  261. }
  262. rc = sqlite3_finalize(res);
  263. if (unlikely(rc != SQLITE_OK))
  264. error_report("Failed to finalize statement when copying health_log tables, rc = %d", rc);
  265. char *table = NULL;
  266. dfe_start_read(dict_tables, table) {
  267. sql_drop_table(table_dfe.name);
  268. }
  269. dfe_done(table);
  270. dictionary_destroy(dict_tables);
  271. snprintfz(sql, sizeof(sql) - 1, "ALTER TABLE health_log_detail DROP COLUMN host_id");
  272. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  273. return 0;
  274. }
  275. static int do_migration_v9_v10(sqlite3 *database)
  276. {
  277. if (table_exists_in_database(database, "alert_hash") && !column_exists_in_table(database, "alert_hash", "chart_labels"))
  278. return init_database_batch(database, &database_migrate_v9_v10[0], "meta_migrate");
  279. return 0;
  280. }
  281. static int do_migration_v10_v11(sqlite3 *database)
  282. {
  283. if (table_exists_in_database(database, "health_log") && !column_exists_in_table(database, "health_log", "chart_name"))
  284. return init_database_batch(database, &database_migrate_v10_v11[0], "meta_migrate");
  285. return 0;
  286. }
  287. #define MIGR_11_12_UPD_HEALTH_LOG_DETAIL "UPDATE health_log_detail SET summary = (select name from health_log where health_log_id = health_log_detail.health_log_id)"
  288. static int do_migration_v11_v12(sqlite3 *database)
  289. {
  290. int rc = 0;
  291. if (table_exists_in_database(database, "health_log_detail") && !column_exists_in_table(database, "health_log_detail", "summary") &&
  292. table_exists_in_database(database, "alert_hash") && !column_exists_in_table(database, "alert_hash", "summary"))
  293. rc = init_database_batch(database, &database_migrate_v11_v12[0], "meta_migrate");
  294. if (!rc)
  295. sqlite3_exec_monitored(database, MIGR_11_12_UPD_HEALTH_LOG_DETAIL, 0, 0, NULL);
  296. return rc;
  297. }
  298. static int do_migration_v14_v15(sqlite3 *database)
  299. {
  300. char sql[256];
  301. int rc;
  302. sqlite3_stmt *res = NULL;
  303. snprintfz(sql, sizeof(sql) - 1, "SELECT name FROM sqlite_schema WHERE type = \"index\" AND name LIKE \"aclk_alert_index@_%%\" ESCAPE \"@\"");
  304. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  305. if (rc != SQLITE_OK) {
  306. error_report("Failed to prepare statement to drop unused indices");
  307. return 1;
  308. }
  309. BUFFER *wb = buffer_create(128, NULL);
  310. while (sqlite3_step_monitored(res) == SQLITE_ROW)
  311. buffer_sprintf(wb, "DROP INDEX IF EXISTS %s", (char *) sqlite3_column_text(res, 0));
  312. rc = sqlite3_finalize(res);
  313. if (unlikely(rc != SQLITE_OK))
  314. error_report("Failed to finalize statement when dropping unused indices, rc = %d", rc);
  315. (void) db_execute(database, buffer_tostring(wb));
  316. buffer_free(wb);
  317. return 0;
  318. }
  319. static int do_migration_v12_v13(sqlite3 *database)
  320. {
  321. int rc = 0;
  322. if (table_exists_in_database(database, "health_log_detail") && !column_exists_in_table(database, "health_log_detail", "summary")) {
  323. rc = init_database_batch(database, &database_migrate_v12_v13_detail[0], "meta_migrate");
  324. sqlite3_exec_monitored(database, MIGR_11_12_UPD_HEALTH_LOG_DETAIL, 0, 0, NULL);
  325. }
  326. if (table_exists_in_database(database, "alert_hash") && !column_exists_in_table(database, "alert_hash", "summary"))
  327. rc = init_database_batch(database, &database_migrate_v12_v13_hash[0], "meta_migrate");
  328. return rc;
  329. }
  330. static int do_migration_v13_v14(sqlite3 *database)
  331. {
  332. if (table_exists_in_database(database, "host") && !column_exists_in_table(database, "host", "last_connected"))
  333. return init_database_batch(database, &database_migrate_v13_v14[0], "meta_migrate");
  334. return 0;
  335. }
  336. // Actions for ML migration
  337. const char *database_ml_migrate_v1_v2[] = {
  338. "PRAGMA journal_mode=delete",
  339. "PRAGMA journal_mode=WAL",
  340. "PRAGMA auto_vacuum=2",
  341. "VACUUM",
  342. NULL
  343. };
  344. static int do_ml_migration_v1_v2(sqlite3 *database)
  345. {
  346. if (get_auto_vaccum(database) != 2)
  347. return init_database_batch(database, &database_ml_migrate_v1_v2[0], "ml_migrate");
  348. return 0;
  349. }
  350. static int do_migration_noop(sqlite3 *database)
  351. {
  352. UNUSED(database);
  353. return 0;
  354. }
  355. typedef struct database_func_migration_list {
  356. char *name;
  357. int (*func)(sqlite3 *database);
  358. } DATABASE_FUNC_MIGRATION_LIST;
  359. static int migrate_database(sqlite3 *database, int target_version, char *db_name, DATABASE_FUNC_MIGRATION_LIST *migration_list)
  360. {
  361. int user_version = 0;
  362. char *err_msg = NULL;
  363. int rc = sqlite3_exec_monitored(database, "PRAGMA user_version", return_int_cb, (void *) &user_version, &err_msg);
  364. if (rc != SQLITE_OK) {
  365. netdata_log_info("Error checking the %s database version; %s", db_name, err_msg);
  366. sqlite3_free(err_msg);
  367. }
  368. if (likely(user_version == target_version)) {
  369. netdata_log_info("%s database version is %d (no migration needed)", db_name, target_version);
  370. return target_version;
  371. }
  372. netdata_log_info("Database version is %d, current version is %d. Running migration for %s ...", user_version, target_version, db_name);
  373. for (int i = user_version; i < target_version && migration_list[i].func; i++) {
  374. netdata_log_info("Running database \"%s\" migration %s", db_name, migration_list[i].name);
  375. rc = (migration_list[i].func)(database);
  376. if (unlikely(rc)) {
  377. error_report("Database %s migration from version %d to version %d failed", db_name, i, i + 1);
  378. return i;
  379. }
  380. }
  381. return target_version;
  382. }
  383. DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
  384. {.name = "v0 to v1", .func = do_migration_noop},
  385. {.name = "v1 to v2", .func = do_migration_v1_v2},
  386. {.name = "v2 to v3", .func = do_migration_v2_v3},
  387. {.name = "v3 to v4", .func = do_migration_v3_v4},
  388. {.name = "v4 to v5", .func = do_migration_v4_v5},
  389. {.name = "v5 to v6", .func = do_migration_v5_v6},
  390. {.name = "v6 to v7", .func = do_migration_v6_v7},
  391. {.name = "v7 to v8", .func = do_migration_v7_v8},
  392. {.name = "v8 to v9", .func = do_migration_v8_v9},
  393. {.name = "v9 to v10", .func = do_migration_v9_v10},
  394. {.name = "v10 to v11", .func = do_migration_v10_v11},
  395. {.name = "v11 to v12", .func = do_migration_v11_v12},
  396. {.name = "v12 to v13", .func = do_migration_v12_v13},
  397. {.name = "v13 to v14", .func = do_migration_v13_v14},
  398. {.name = "v14 to v15", .func = do_migration_v14_v15},
  399. // the terminator of this array
  400. {.name = NULL, .func = NULL}
  401. };
  402. DATABASE_FUNC_MIGRATION_LIST context_migration_action[] = {
  403. {.name = "v0 to v1", .func = do_migration_noop},
  404. // the terminator of this array
  405. {.name = NULL, .func = NULL}
  406. };
  407. DATABASE_FUNC_MIGRATION_LIST ml_migration_action[] = {
  408. {.name = "v0 to v1", .func = do_migration_noop},
  409. {.name = "v1 to v2", .func = do_ml_migration_v1_v2},
  410. // the terminator of this array
  411. {.name = NULL, .func = NULL}
  412. };
  413. int perform_database_migration(sqlite3 *database, int target_version)
  414. {
  415. int user_version = get_database_user_version(database);
  416. if (!user_version && !db_table_count(database))
  417. return target_version;
  418. return migrate_database(database, target_version, "metadata", migration_action);
  419. }
  420. int perform_context_database_migration(sqlite3 *database, int target_version)
  421. {
  422. int user_version = get_database_user_version(database);
  423. if (!user_version && !table_exists_in_database(database, "context"))
  424. return target_version;
  425. return migrate_database(database, target_version, "context", context_migration_action);
  426. }
  427. int perform_ml_database_migration(sqlite3 *database, int target_version)
  428. {
  429. return migrate_database(database, target_version, "ml", ml_migration_action);
  430. }