sqlite_db_migration.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_noop(sqlite3 *database, const char *name)
  68. {
  69. UNUSED(database);
  70. UNUSED(name);
  71. info("Running database migration %s", name);
  72. return 0;
  73. }
  74. typedef struct database_func_migration_list {
  75. char *name;
  76. int (*func)(sqlite3 *database, const char *name);
  77. } DATABASE_FUNC_MIGRATION_LIST;
  78. static int migrate_database(sqlite3 *database, int target_version, char *db_name, DATABASE_FUNC_MIGRATION_LIST *migration_list)
  79. {
  80. int user_version = 0;
  81. char *err_msg = NULL;
  82. int rc = sqlite3_exec(database, "PRAGMA user_version;", return_int_cb, (void *) &user_version, &err_msg);
  83. if (rc != SQLITE_OK) {
  84. info("Error checking the %s database version; %s", db_name, err_msg);
  85. sqlite3_free(err_msg);
  86. }
  87. if (likely(user_version == target_version)) {
  88. info("%s database version is %d (no migration needed)", db_name, target_version);
  89. return target_version;
  90. }
  91. info("Database version is %d, current version is %d. Running migration for %s ...", user_version, target_version, db_name);
  92. for (int i = user_version; i < target_version && migration_list[i].func; i++) {
  93. rc = (migration_list[i].func)(database, migration_list[i].name);
  94. if (unlikely(rc)) {
  95. error_report("Database %s migration from version %d to version %d failed", db_name, i, i + 1);
  96. return i;
  97. }
  98. }
  99. return target_version;
  100. }
  101. DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
  102. {.name = "v0 to v1", .func = do_migration_noop},
  103. {.name = "v1 to v2", .func = do_migration_v1_v2},
  104. {.name = "v2 to v3", .func = do_migration_v2_v3},
  105. // the terminator of this array
  106. {.name = NULL, .func = NULL}
  107. };
  108. DATABASE_FUNC_MIGRATION_LIST context_migration_action[] = {
  109. {.name = "v0 to v1", .func = do_migration_noop},
  110. // the terminator of this array
  111. {.name = NULL, .func = NULL}
  112. };
  113. int perform_database_migration(sqlite3 *database, int target_version)
  114. {
  115. return migrate_database(database, target_version, "metadata", migration_action);
  116. }
  117. int perform_context_database_migration(sqlite3 *database, int target_version)
  118. {
  119. return migrate_database(database, target_version, "context", context_migration_action);
  120. }