From 6a1d616396bdd899571bb947e9a63ecdf93777bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Fri, 30 Oct 2020 13:01:36 +0100 Subject: [PATCH] Renamed 'interval_ms' from 'restapi_routes' table into 'timeout_ms' --- include/proxysql_admin.h | 1 + include/proxysql_restapi.h | 2 +- lib/ProxySQL_Admin.cpp | 34 ++++++++++++++++++++++++++++++++-- lib/ProxySQL_Config.cpp | 14 ++++++++------ lib/ProxySQL_Restapi.cpp | 8 ++++---- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/include/proxysql_admin.h b/include/proxysql_admin.h index 987a55a74..9663fd052 100644 --- a/include/proxysql_admin.h +++ b/include/proxysql_admin.h @@ -223,6 +223,7 @@ class ProxySQL_Admin { void disk_upgrade_mysql_servers(); void disk_upgrade_mysql_users(); void disk_upgrade_scheduler(); + void disk_upgrade_rest_api_routes(); #ifdef DEBUG void add_credentials(char *type, char *credentials, int hostgroup_id); diff --git a/include/proxysql_restapi.h b/include/proxysql_restapi.h index b1de2bd82..cf9967784 100644 --- a/include/proxysql_restapi.h +++ b/include/proxysql_restapi.h @@ -12,7 +12,7 @@ class Restapi_Row { public: unsigned int id; bool is_active; - unsigned int interval_ms; + unsigned int timeout_ms; std::string method; std::string uri; std::string script; diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 8d7802f2c..1a317b502 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -380,9 +380,13 @@ static int http_handler(void *cls, struct MHD_Connection *connection, const char #define ADMIN_SQLITE_TABLE_MYSQL_COLLATIONS "CREATE TABLE mysql_collations (Id INTEGER NOT NULL PRIMARY KEY , Collation VARCHAR NOT NULL , Charset VARCHAR NOT NULL , `Default` VARCHAR NOT NULL)" -#define ADMIN_SQLITE_TABLE_RESTAPI_ROUTES "CREATE TABLE restapi_routes (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , interval_ms INTEGER CHECK (interval_ms>=100 AND interval_ms<=100000000) NOT NULL , method VARCHAR NOT NULL CHECK (UPPER(method) IN ('GET','POST')) , uri VARCHAR NOT NULL , script VARCHAR NOT NULL , comment VARCHAR NOT NULL DEFAULT '')" +#define ADMIN_SQLITE_TABLE_RESTAPI_ROUTES_V2_0_15 "CREATE TABLE restapi_routes (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , interval_ms INTEGER CHECK (interval_ms>=100 AND interval_ms<=100000000) NOT NULL , method VARCHAR NOT NULL CHECK (UPPER(method) IN ('GET','POST')) , uri VARCHAR NOT NULL , script VARCHAR NOT NULL , comment VARCHAR NOT NULL DEFAULT '')" -#define ADMIN_SQLITE_TABLE_RUNTIME_RESTAPI_ROUTES "CREATE TABLE runtime_restapi_routes (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , interval_ms INTEGER CHECK (interval_ms>=100 AND interval_ms<=100000000) NOT NULL , method VARCHAR NOT NULL CHECK (UPPER(method) IN ('GET','POST')) , uri VARCHAR NOT NULL , script VARCHAR NOT NULL , comment VARCHAR NOT NULL DEFAULT '')" +#define ADMIN_SQLITE_TABLE_RESTAPI_ROUTES_v2_1_0 "CREATE TABLE restapi_routes (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , timeout_ms INTEGER CHECK (timeout_ms>=100 AND timeout_ms<=100000000) NOT NULL , method VARCHAR NOT NULL CHECK (UPPER(method) IN ('GET','POST')) , uri VARCHAR NOT NULL , script VARCHAR NOT NULL , comment VARCHAR NOT NULL DEFAULT '')" + +#define ADMIN_SQLITE_TABLE_RESTAPI_ROUTES ADMIN_SQLITE_TABLE_RESTAPI_ROUTES_v2_1_0 + +#define ADMIN_SQLITE_TABLE_RUNTIME_RESTAPI_ROUTES "CREATE TABLE runtime_restapi_routes (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , timeout_ms INTEGER CHECK (timeout_ms>=100 AND timeout_ms<=100000000) NOT NULL , method VARCHAR NOT NULL CHECK (UPPER(method) IN ('GET','POST')) , uri VARCHAR NOT NULL , script VARCHAR NOT NULL , comment VARCHAR NOT NULL DEFAULT '')" #define ADMIN_SQLITE_TABLE_SCHEDULER "CREATE TABLE scheduler (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , interval_ms INTEGER CHECK (interval_ms>=100 AND interval_ms<=100000000) NOT NULL , filename VARCHAR NOT NULL , arg1 VARCHAR , arg2 VARCHAR , arg3 VARCHAR , arg4 VARCHAR , arg5 VARCHAR , comment VARCHAR NOT NULL DEFAULT '')" @@ -5404,6 +5408,9 @@ bool ProxySQL_Admin::init() { // upgrade scheduler if needed (upgrade from previous version) disk_upgrade_scheduler(); + // upgrade restapi_routes if needed (upgrade from previous version) + disk_upgrade_rest_api_routes(); + check_and_build_standard_tables(admindb, tables_defs_admin); check_and_build_standard_tables(configdb, tables_defs_config); check_and_build_standard_tables(statsdb, tables_defs_stats); @@ -11624,6 +11631,29 @@ void ProxySQL_Admin::disk_upgrade_mysql_users() { configdb->execute("PRAGMA foreign_keys = ON"); } + +void ProxySQL_Admin::disk_upgrade_rest_api_routes() { + int rci; + configdb->execute("PRAGMA foreign_keys = OFF"); + + rci=configdb->check_table_structure((char *)"restapi_routes",(char *)ADMIN_SQLITE_TABLE_RESTAPI_ROUTES_V2_0_15); + if (rci) { + // upgrade is required + proxy_warning("Detected version pre-2.1.0 of table restapi_routes\n"); + proxy_warning("ONLINE UPGRADE of table restapi_routes in progress\n"); + // drop any existing table with suffix _v210 + configdb->execute("DROP TABLE IF EXISTS restapi_routes_v2015"); + // rename current table to add suffix _v210 + configdb->execute("ALTER TABLE restapi_routes RENAME TO restapi_routes_v2015"); + // create new table + configdb->build_table((char *)"restapi_routes",(char *)ADMIN_SQLITE_TABLE_RESTAPI_ROUTES,false); + // copy fields from old table + configdb->execute("INSERT INTO restapi_routes(id,active,timeout_ms,method,uri,script,comment) SELECT id,active,interval_ms,method,uri,script,comment FROM restapi_routes_v2015"); + } + + configdb->execute("PRAGMA foreign_keys = ON"); +} + Scheduler_Row::Scheduler_Row(unsigned int _id, bool _is_active, unsigned int _in, char *_f, char *a1, char *a2, char *a3, char *a4, char *a5, char *_comment) { int i; id=_id; diff --git a/lib/ProxySQL_Config.cpp b/lib/ProxySQL_Config.cpp index 265bcdf0a..79dee6c1e 100644 --- a/lib/ProxySQL_Config.cpp +++ b/lib/ProxySQL_Config.cpp @@ -368,7 +368,7 @@ int ProxySQL_Config::Write_Restapi_to_configfile(std::string& data) { data += "\t{\n"; addField(data, "id", r->fields[0], ""); addField(data, "active", r->fields[1], ""); - addField(data, "interval_ms", r->fields[2], ""); + addField(data, "timeout_ms", r->fields[2], ""); addField(data, "method", r->fields[3], ""); addField(data, "uri", r->fields[4]); addField(data, "script", r->fields[5]); @@ -401,8 +401,8 @@ int ProxySQL_Config::Read_Restapi_from_configfile() { const Setting &route = routes[i]; int id; int active=1; - // variable for parsing interval_ms - int interval_ms=0; + // variable for parsing timeout_ms + int timeout_ms=0; std::string method; std::string uri; @@ -415,7 +415,9 @@ int ProxySQL_Config::Read_Restapi_from_configfile() { continue; } route.lookupValue("active", active); - route.lookupValue("interval_ms", interval_ms); + if (route.lookupValue("interval_ms", timeout_ms) == false) { + route.lookupValue("timeout_ms", timeout_ms); + } if (route.lookupValue("method", method)==false) { proxy_error("Admin: detected a restapi route in config file without a mandatory method\n"); continue; @@ -434,7 +436,7 @@ int ProxySQL_Config::Read_Restapi_from_configfile() { query_len+=strlen(q) + strlen(std::to_string(id).c_str()) + strlen(std::to_string(active).c_str()) + - strlen(std::to_string(interval_ms).c_str()) + + strlen(std::to_string(timeout_ms).c_str()) + strlen(method.c_str()) + strlen(uri.c_str()) + strlen(script.c_str()) + @@ -443,7 +445,7 @@ int ProxySQL_Config::Read_Restapi_from_configfile() { char *query=(char *)malloc(query_len); sprintf(query, q, id, active, - interval_ms, + timeout_ms, method.c_str(), uri.c_str(), script.c_str(), diff --git a/lib/ProxySQL_Restapi.cpp b/lib/ProxySQL_Restapi.cpp index 2c851c377..f4e6d24a8 100644 --- a/lib/ProxySQL_Restapi.cpp +++ b/lib/ProxySQL_Restapi.cpp @@ -6,7 +6,7 @@ #include Restapi_Row::Restapi_Row(unsigned int _id, bool _is_active, unsigned int _in, const std::string& _method, const std::string& _uri, const std::string& _script, const std::string& _comment) : - id(_id), is_active(_is_active), interval_ms(_in), method(_method), uri(_uri), script(_script), comment(_comment) {} + id(_id), is_active(_is_active), timeout_ms(_in), method(_method), uri(_uri), script(_script), comment(_comment) {} ProxySQL_Restapi::ProxySQL_Restapi(SQLite3DB* db) { assert(db); @@ -37,8 +37,8 @@ void ProxySQL_Restapi::update_restapi_table(SQLite3_result *resultset) { if (atoi(r->fields[1])) { is_active=true; } - unsigned int interval_ms=strtoul(r->fields[2], NULL, 10); - Restapi_Rows.push_back({id, is_active, interval_ms, r->fields[3], r->fields[4], r->fields[5], r->fields[6]}); + unsigned int timeout_ms=strtoul(r->fields[2], NULL, 10); + Restapi_Rows.push_back({id, is_active, timeout_ms, r->fields[3], r->fields[4], r->fields[5], r->fields[6]}); } // increase version @@ -79,7 +79,7 @@ void ProxySQL_Restapi::save_restapi_runtime_to_database(bool _runtime) { for (auto r : Restapi_Rows) { std::stringstream ss; ss << "INSERT INTO " << table << " VALUES(" << r.id << "," << r.is_active << "," - << r.interval_ms << ",'" << r.method << "','" << r.uri << "','" << r.script << "','" << r.comment << "')"; + << r.timeout_ms << ",'" << r.method << "','" << r.uri << "','" << r.script << "','" << r.comment << "')"; admindb->execute(ss.str().c_str()); }