sqlite_db_migration.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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]);
  9. return 0;
  10. }
  11. static 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(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(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;",
  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. static int do_migration_v1_v2(sqlite3 *database, const char *name)
  52. {
  53. UNUSED(name);
  54. info("Running \"%s\" database migration", name);
  55. if (table_exists_in_database("host") && !column_exists_in_table("host", "hops"))
  56. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v1_v2[0]);
  57. return 0;
  58. }
  59. static int do_migration_v2_v3(sqlite3 *database, const char *name)
  60. {
  61. UNUSED(name);
  62. info("Running \"%s\" database migration", name);
  63. if (table_exists_in_database("host") && !column_exists_in_table("host", "memory_mode"))
  64. return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v2_v3[0]);
  65. return 0;
  66. }
  67. static int do_migration_v3_v4(sqlite3 *database, const char *name)
  68. {
  69. UNUSED(name);
  70. info("Running database migration %s", name);
  71. char sql[256];
  72. int rc;
  73. sqlite3_stmt *res = NULL;
  74. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%';");
  75. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  76. if (rc != SQLITE_OK) {
  77. error_report("Failed to prepare statement to alter health_log tables");
  78. return 1;
  79. }
  80. while (sqlite3_step(res) == SQLITE_ROW) {
  81. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  82. if (!column_exists_in_table(table, "chart_context")) {
  83. snprintfz(sql, 255, "ALTER TABLE %s ADD chart_context text", table);
  84. sqlite3_exec(database, sql, 0, 0, NULL);
  85. }
  86. freez(table);
  87. }
  88. rc = sqlite3_finalize(res);
  89. if (unlikely(rc != SQLITE_OK))
  90. error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
  91. return 0;
  92. }
  93. static int do_migration_noop(sqlite3 *database, const char *name)
  94. {
  95. UNUSED(database);
  96. UNUSED(name);
  97. info("Running database migration %s", name);
  98. return 0;
  99. }
  100. typedef struct database_func_migration_list {
  101. char *name;
  102. int (*func)(sqlite3 *database, const char *name);
  103. } DATABASE_FUNC_MIGRATION_LIST;
  104. static int migrate_database(sqlite3 *database, int target_version, char *db_name, DATABASE_FUNC_MIGRATION_LIST *migration_list)
  105. {
  106. int user_version = 0;
  107. char *err_msg = NULL;
  108. int rc = sqlite3_exec(database, "PRAGMA user_version;", return_int_cb, (void *) &user_version, &err_msg);
  109. if (rc != SQLITE_OK) {
  110. info("Error checking the %s database version; %s", db_name, err_msg);
  111. sqlite3_free(err_msg);
  112. }
  113. if (likely(user_version == target_version)) {
  114. info("%s database version is %d (no migration needed)", db_name, target_version);
  115. return target_version;
  116. }
  117. info("Database version is %d, current version is %d. Running migration for %s ...", user_version, target_version, db_name);
  118. for (int i = user_version; i < target_version && migration_list[i].func; i++) {
  119. rc = (migration_list[i].func)(database, migration_list[i].name);
  120. if (unlikely(rc)) {
  121. error_report("Database %s migration from version %d to version %d failed", db_name, i, i + 1);
  122. return i;
  123. }
  124. }
  125. return target_version;
  126. }
  127. DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
  128. {.name = "v0 to v1", .func = do_migration_noop},
  129. {.name = "v1 to v2", .func = do_migration_v1_v2},
  130. {.name = "v2 to v3", .func = do_migration_v2_v3},
  131. {.name = "v3 to v4", .func = do_migration_v3_v4},
  132. // the terminator of this array
  133. {.name = NULL, .func = NULL}
  134. };
  135. DATABASE_FUNC_MIGRATION_LIST context_migration_action[] = {
  136. {.name = "v0 to v1", .func = do_migration_noop},
  137. // the terminator of this array
  138. {.name = NULL, .func = NULL}
  139. };
  140. int perform_database_migration(sqlite3 *database, int target_version)
  141. {
  142. return migrate_database(database, target_version, "metadata", migration_action);
  143. }
  144. int perform_context_database_migration(sqlite3 *database, int target_version)
  145. {
  146. return migrate_database(database, target_version, "context", context_migration_action);
  147. }