X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/793728a58d8f97daf7de1dde7a02a5c4a30e30d4..0c80dbc4ce66a278eb5235420d34a469323efc6b:/projects/stargazer/plugins/store/mysql/mysql_store.cpp diff --git a/projects/stargazer/plugins/store/mysql/mysql_store.cpp b/projects/stargazer/plugins/store/mysql/mysql_store.cpp index b4ac8715..11c2def3 100644 --- a/projects/stargazer/plugins/store/mysql/mysql_store.cpp +++ b/projects/stargazer/plugins/store/mysql/mysql_store.cpp @@ -12,6 +12,7 @@ #include "stg/user_stat.h" #include "stg/blowfish.h" #include "stg/plugin_creator.h" +#include "stg/logger.h" #include "mysql_store.h" #define adm_enc_passwd "cjeifY8m3" @@ -235,12 +236,18 @@ else } ret = CheckAllTables(sock); } - } - else - ret = CheckAllTables(sock); - mysql_close(sock); + } + else + { + ret = CheckAllTables(sock); + } + if (!ret) + { + WriteServLog("MYSQL_STORE: Current DB schema version: %d", schemaVersion); + MakeUpdates(sock); + } + mysql_close(sock); } - } return ret; } @@ -267,6 +274,43 @@ return num_rows == 1; //----------------------------------------------------------------------------- int MYSQL_STORE::CheckAllTables(MYSQL * sock) { +//info------------------------------------------------------------------------- +if(!IsTablePresent("info",sock)) +{ + sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)"); + + if(MysqlQuery(qbuf,sock)) + { + errorStr = "Couldn't create info table With error:\n"; + errorStr += mysql_error(sock); + mysql_close(sock); + return -1; + } + + sprintf(qbuf,"INSERT INTO info SET version=0"); + + if(MysqlQuery(qbuf,sock)) + { + errorStr = "Couldn't write default version. With error:\n"; + errorStr += mysql_error(sock); + mysql_close(sock); + return -1; + } + schemaVersion = 0; +} +else +{ + std::vector info; + if (GetAllParams(&info, "info", "version")) + schemaVersion = 0; + else + { + if (info.empty()) + schemaVersion = 0; + else + GetInt(info.front(), &schemaVersion, 0); + } +} //admins----------------------------------------------------------------------- if(!IsTablePresent("admins",sock)) { @@ -330,8 +374,9 @@ if(!IsTablePresent("tariffs",sock)) res += param; } - res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\ - "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')"; + res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0," + "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT ''," + "period VARCHAR(32) NOT NULL DEFAULT 'month')"; if(MysqlQuery(res.c_str(),sock)) { @@ -385,7 +430,7 @@ if(!IsTablePresent("tariffs",sock)) res += "PassiveCost=0.0, Fee=10.0, Free=0,"\ "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\ - "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'"; + "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month'"; if(MysqlQuery(res.c_str(),sock)) { @@ -394,6 +439,17 @@ if(!IsTablePresent("tariffs",sock)) mysql_close(sock); return -1; } + + sprintf(qbuf,"UPDATE info SET version=1"); + + if(MysqlQuery(qbuf,sock)) + { + errorStr = "Couldn't write default version. With error:\n"; + errorStr += mysql_error(sock); + mysql_close(sock); + return -1; + } + schemaVersion = 1; } //users----------------------------------------------------------------------- @@ -525,6 +581,29 @@ if(!IsTablePresent("stat",sock)) return 0; } //----------------------------------------------------------------------------- +int MYSQL_STORE::MakeUpdates(MYSQL * sock) +{ +if (schemaVersion < 1) + { + if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock)) + { + errorStr = "Couldn't update tariffs table to version 1. With error:\n"; + errorStr += mysql_error(sock); + mysql_close(sock); + return -1; + } + if (MysqlQuery("UPDATE info SET version = 1", sock)) + { + errorStr = "Couldn't update DB schema version to 1. With error:\n"; + errorStr += mysql_error(sock); + mysql_close(sock); + return -1; + } + schemaVersion = 1; + WriteServLog("MYSQL_STORE: Updated DB schema to version %d", schemaVersion); + } +return 0; +} //----------------------------------------------------------------------------- int MYSQL_STORE::GetAllParams(std::vector * ParamList, @@ -1561,6 +1640,26 @@ else return -1; } +if (schemaVersion > 0) +{ + str = row[5+8*DIR_NUM]; + param = "Period"; + + if (str.length() == 0) + { + mysql_free_result(res); + errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + mysql_close(sock); + return -1; + } + + td->tariffConf.period = TARIFF::StringToPeriod(str); + } +else + { + td->tariffConf.period = TARIFF::MONTH; + } + mysql_free_result(res); mysql_close(sock); return 0; @@ -1638,12 +1737,16 @@ switch (td.tariffConf.traffType) res += " TraffType='max'"; break; } + +if (schemaVersion > 0) + res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'"; + strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str()); res += param; if(MysqlSetQuery(res.c_str())) { - errorStr = "Couldn't save admin:\n"; + errorStr = "Couldn't save tariff:\n"; //errorStr += mysql_error(sock); return -1; }