v0.2.9 - Embedded sql schema into app
This commit is contained in:
61
src/main.c
61
src/main.c
@@ -16,6 +16,7 @@
|
||||
#include "../nostr_core_lib/nostr_core/nostr_core.h"
|
||||
#include "../nostr_core_lib/nostr_core/nip013.h" // NIP-13: Proof of Work
|
||||
#include "config.h" // Configuration management system
|
||||
#include "sql_schema.h" // Embedded database schema
|
||||
|
||||
// Color constants for logging
|
||||
#define RED "\033[31m"
|
||||
@@ -1938,7 +1939,7 @@ int validate_event_expiration(cJSON* event, char* error_message, size_t error_si
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Initialize database connection
|
||||
// Initialize database connection and schema
|
||||
int init_database() {
|
||||
// Use configurable database path, falling back to default
|
||||
const char* db_path = get_config_value("database_path");
|
||||
@@ -1955,6 +1956,64 @@ int init_database() {
|
||||
char success_msg[256];
|
||||
snprintf(success_msg, sizeof(success_msg), "Database connection established: %s", db_path);
|
||||
log_success(success_msg);
|
||||
|
||||
// Check if database is already initialized by looking for the events table
|
||||
const char* check_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='events'";
|
||||
sqlite3_stmt* check_stmt;
|
||||
rc = sqlite3_prepare_v2(g_db, check_sql, -1, &check_stmt, NULL);
|
||||
if (rc == SQLITE_OK) {
|
||||
int has_events_table = (sqlite3_step(check_stmt) == SQLITE_ROW);
|
||||
sqlite3_finalize(check_stmt);
|
||||
|
||||
if (has_events_table) {
|
||||
log_info("Database schema already exists, skipping initialization");
|
||||
|
||||
// Log existing schema version if available
|
||||
const char* version_sql = "SELECT value FROM schema_info WHERE key = 'version'";
|
||||
sqlite3_stmt* version_stmt;
|
||||
if (sqlite3_prepare_v2(g_db, version_sql, -1, &version_stmt, NULL) == SQLITE_OK) {
|
||||
if (sqlite3_step(version_stmt) == SQLITE_ROW) {
|
||||
const char* db_version = (char*)sqlite3_column_text(version_stmt, 0);
|
||||
char version_msg[256];
|
||||
snprintf(version_msg, sizeof(version_msg), "Existing database schema version: %s",
|
||||
db_version ? db_version : "unknown");
|
||||
log_info(version_msg);
|
||||
} else {
|
||||
log_info("Database exists but no version information found");
|
||||
}
|
||||
sqlite3_finalize(version_stmt);
|
||||
}
|
||||
} else {
|
||||
// Initialize database schema using embedded SQL
|
||||
log_info("Initializing database schema from embedded SQL");
|
||||
|
||||
// Execute the embedded schema SQL
|
||||
char* error_msg = NULL;
|
||||
rc = sqlite3_exec(g_db, EMBEDDED_SCHEMA_SQL, NULL, NULL, &error_msg);
|
||||
if (rc != SQLITE_OK) {
|
||||
char error_log[512];
|
||||
snprintf(error_log, sizeof(error_log), "Failed to initialize database schema: %s",
|
||||
error_msg ? error_msg : "unknown error");
|
||||
log_error(error_log);
|
||||
if (error_msg) {
|
||||
sqlite3_free(error_msg);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
log_success("Database schema initialized successfully");
|
||||
|
||||
// Log schema version information
|
||||
char version_msg[256];
|
||||
snprintf(version_msg, sizeof(version_msg), "Database schema version: %s",
|
||||
EMBEDDED_SCHEMA_VERSION);
|
||||
log_info(version_msg);
|
||||
}
|
||||
} else {
|
||||
log_error("Failed to check existing database schema");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user