summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/console.c16
-rw-r--r--src/common/console.h4
-rw-r--r--src/common/sql.c52
-rw-r--r--src/common/sql.h1
4 files changed, 69 insertions, 4 deletions
diff --git a/src/common/console.c b/src/common/console.c
index a9ea0386f..66f8d914a 100644
--- a/src/common/console.c
+++ b/src/common/console.c
@@ -17,6 +17,7 @@
#include "../common/mutex.h"
#include "../common/timer.h"
#include "../common/strlib.h"
+ #include "../common/sql.h"
#endif
#include <stdio.h>
@@ -106,6 +107,13 @@ CPCMD(malloc_usage) {
unsigned int val = (unsigned int)iMalloc->usage();
ShowInfo("malloc_usage: %.2f MB\n",(double)(val)/1024);
}
+CPCMD(skip) {
+ if( !line ) {
+ ShowDebug("usage example: sql update skip 2013-02-14--16-15.sql\n");
+ return;
+ }
+ Sql_HerculesUpdateSkip(console->SQL, line);
+}
#define CP_DEF_C(x) { #x , NULL , NULL, NULL }
#define CP_DEF_C2(x,y) { #x , NULL , #y, NULL }
#define CP_DEF_S(x,y) { #x , console_parse_ ## x , #y, NULL }
@@ -123,6 +131,9 @@ void console_load_defaults(void) {
CP_DEF_S(mem_report,server),
CP_DEF_S(malloc_usage,server),
CP_DEF_S(exit,server),
+ CP_DEF_C(sql),
+ CP_DEF_C2(update,sql),
+ CP_DEF_S(skip,update),
};
unsigned int i, len = ARRAYLENGTH(default_list);
struct CParseEntry *cmd;
@@ -375,6 +386,9 @@ void console_parse_init(void) {
iTimer->add_timer_interval(iTimer->gettick() + 1000, console->parse_timer, 0, 0, 500);/* start listening in 1s; re-try every 0.5s */
}
+void console_setSQL(Sql *SQL) {
+ console->SQL = SQL;
+}
#endif /* CONSOLE_INPUT */
void console_init (void) {
@@ -413,5 +427,7 @@ void console_defaults(void) {
console->load_defaults = console_load_defaults;
console->parse_list_subs = console_parse_list_subs;
console->addCommand = console_parse_create;
+ console->setSQL = console_setSQL;
+ console->SQL = NULL;
#endif
}
diff --git a/src/common/console.h b/src/common/console.h
index ebce013f7..214a41175 100644
--- a/src/common/console.h
+++ b/src/common/console.h
@@ -7,6 +7,7 @@
#include "../common/thread.h"
#include "../common/mutex.h"
#include "../common/spinlock.h"
+#include "../common/sql.h"
#include "../config/core.h"
/**
@@ -51,6 +52,8 @@ struct console_interface {
unsigned int cmd_count;
unsigned int cmd_list_count;
/* */
+ Sql *SQL;
+ /* */
void (*parse_init) (void);
void (*parse_final) (void);
int (*parse_timer) (int tid, unsigned int tick, int id, intptr_t data);
@@ -61,6 +64,7 @@ struct console_interface {
void (*load_defaults) (void);
void (*parse_list_subs) (struct CParseEntry *cmd, unsigned char depth);
void (*addCommand) (char *name, CParseFunc func);
+ void (*setSQL) (Sql *SQL);
#endif
};
diff --git a/src/common/sql.c b/src/common/sql.c
index 441b860da..99482072c 100644
--- a/src/common/sql.c
+++ b/src/common/sql.c
@@ -1003,18 +1003,21 @@ void Sql_HerculesUpdateCheck(Sql* self) {
char line[22];// "yyyy-mm-dd--hh-mm" (17) + ".sql" (4) + 1
FILE* ifp;/* index fp */
unsigned int performed = 0;
-
+ StringBuf buf;
+
if( !( ifp = fopen("sql-files/upgrades/index.txt", "r") ) ) {
ShowError("SQL upgrade index was not found!\n");
return;
}
+ StrBuf->Init(&buf);
+
while(fgets(line, sizeof(line), ifp)) {
char path[41];// "sql-files/upgrades/" (19) + "yyyy-mm-dd--hh-mm" (17) + ".sql" (4) + 1
char timestamp[11];// "1360186680" (10) + 1
FILE* ufp;/* upgrade fp */
- if( line[0] == '\n' || ( line[0] == '/' && line[1] == '/' ) )/* skip \n and "//" comments */
+ if( line[0] == '\n' || line[0] == '\r' || ( line[0] == '/' && line[1] == '/' ) )/* skip \n, \r and "//" comments */
continue;
sprintf(path,"sql-files/upgrades/%s",line);
@@ -1034,7 +1037,7 @@ void Sql_HerculesUpdateCheck(Sql* self) {
if( SQL_ERROR == SQL->Query(self, "SELECT 1 FROM `sql_updates` WHERE `timestamp` = '%u' LIMIT 1", timestampui) )
Sql_ShowDebug(self);
if( Sql_NumRows(self) != 1 ) {
- ShowSQL("'"CL_WHITE"%s"CL_RESET"' wasn't applied to the database\n",path);
+ StrBuf->Printf(&buf,CL_MAGENTA"[SQL]"CL_RESET": -- '"CL_WHITE"%s"CL_RESET"'\n", path);
performed++;
}
}
@@ -1045,8 +1048,49 @@ void Sql_HerculesUpdateCheck(Sql* self) {
fclose(ifp);
if( performed ) {
- ShowSQL("If you did apply these updates or would like to be skip, insert a new entry in your sql_updates table with the timestamp of each file\n");
+ ShowSQL("- dected %d new "CL_WHITE"SQL updates"CL_RESET"\n",performed);
+ ShowMessage("%s",StrBuf->Value(&buf));
+ ShowSQL("To manually skip, type: 'sql update skip <file name>'\n");
+ }
+
+ StrBuf->Destroy(&buf);
+}
+
+void Sql_HerculesUpdateSkip(Sql* self,const char *filename) {
+ char path[41];// "sql-files/upgrades/" (19) + "yyyy-mm-dd--hh-mm" (17) + ".sql" (4) + 1
+ char timestamp[11];// "1360186680" (10) + 1
+ FILE* ifp;/* index fp */
+
+ if( !self ) {
+ ShowError("SQL not hooked!\n");
+ return;
}
+
+ snprintf(path,41,"sql-files/upgrades/%s",filename);
+
+ if( !( ifp = fopen(path, "r") ) ) {
+ ShowError("Upgrade file '%s' was not found!\n",filename);
+ return;
+ }
+
+ fseek (ifp,1,SEEK_SET);/* woo. skip the # */
+
+ if( fgets(timestamp,sizeof(timestamp),ifp) ) {
+ unsigned int timestampui = atol(timestamp);
+ if( SQL_ERROR == SQL->Query(self, "SELECT 1 FROM `sql_updates` WHERE `timestamp` = '%u' LIMIT 1", timestampui) )
+ Sql_ShowDebug(self);
+ else if( Sql_NumRows(self) == 1 ) {
+ ShowError("Upgrade '%s' has already been skipped\n",filename);
+ } else {
+ if( SQL_ERROR == SQL->Query(self, "INSERT INTO `sql_updates` (`timestamp`,`ignored`) VALUES ('%u','Yes') ", timestampui) )
+ Sql_ShowDebug(self);
+ else {
+ ShowInfo("SQL Upgrade '%s' successfully skipped\n",filename);
+ }
+ }
+ }
+ fclose(ifp);
+ return;
}
void Sql_Init(void) {
diff --git a/src/common/sql.h b/src/common/sql.h
index 535990649..da00edf2d 100644
--- a/src/common/sql.h
+++ b/src/common/sql.h
@@ -281,6 +281,7 @@ void sql_defaults(void);
#endif
void Sql_HerculesUpdateCheck(Sql* self);
+void Sql_HerculesUpdateSkip(Sql* self,const char *filename);
#if defined(SQL_REMOVE_SHOWDEBUG)
#define SqlStmt_ShowDebug(self) (void)0