reconnect if database connection was closed...
This commit is contained in:
parent
8b07505672
commit
eb263227fd
2 changed files with 18 additions and 6 deletions
|
@ -1,5 +1,7 @@
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
def __init__(self, host, user, password, db):
|
def __init__(self, host, user, password, db):
|
||||||
self.con = mysql.connector.connect(
|
self.con = mysql.connector.connect(
|
||||||
|
@ -11,12 +13,17 @@ class Database:
|
||||||
|
|
||||||
def init_test_database(self):
|
def init_test_database(self):
|
||||||
cursor = self.con.cursor()
|
cursor = self.con.cursor()
|
||||||
cursor.execute("CREATE TABLE discord_link ( admin_id int(10) UNSIGNED NOT NULL, discord_id bigint(20) UNSIGNED NOT NULL, steamid varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL);")
|
try:
|
||||||
cursor.execute("CREATE TABLE sm_admins ( id int(10) UNSIGNED NOT NULL, authtype enum('steam', 'name', 'ip') COLLATE utf8mb4_unicode_ci NOT NULL, identity varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, password varchar(65) COLLATE utf8mb4_unicode_ci DEFAULT NULL, flags varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, name varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, immunity int(10) UNSIGNED NOT NULL);")
|
cursor.execute("CREATE TABLE discord_link ( admin_id int(10) UNSIGNED NOT NULL, discord_id bigint(20) UNSIGNED NOT NULL, steamid varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL);")
|
||||||
cursor.execute("ALTER TABLE discord_link ADD PRIMARY KEY (admin_id), ADD UNIQUE KEY admin_id (admin_id), ADD UNIQUE KEY discord_id (discord_id), ADD UNIQUE KEY steamid (steamid);")
|
cursor.execute("CREATE TABLE sm_admins ( id int(10) UNSIGNED NOT NULL, authtype enum('steam', 'name', 'ip') COLLATE utf8mb4_unicode_ci NOT NULL, identity varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, password varchar(65) COLLATE utf8mb4_unicode_ci DEFAULT NULL, flags varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, name varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, immunity int(10) UNSIGNED NOT NULL);")
|
||||||
cursor.execute("ALTER TABLE sm_admins ADD PRIMARY KEY (id), ADD UNIQUE KEY identity (identity), ADD UNIQUE KEY id (id);")
|
cursor.execute("ALTER TABLE discord_link ADD PRIMARY KEY (admin_id), ADD UNIQUE KEY admin_id (admin_id), ADD UNIQUE KEY discord_id (discord_id), ADD UNIQUE KEY steamid (steamid);")
|
||||||
cursor.execute("ALTER TABLE sm_admins MODIFY id int(10) UNSIGNED NOT NULL AUTO_INCREMENT;")
|
cursor.execute("ALTER TABLE sm_admins ADD PRIMARY KEY (id), ADD UNIQUE KEY identity (identity), ADD UNIQUE KEY id (id);")
|
||||||
cursor.execute("ALTER TABLE discord_link ADD CONSTRAINT discord_link_ibfk_1 FOREIGN KEY (admin_id) REFERENCES sm_admins (id);")
|
cursor.execute("ALTER TABLE sm_admins MODIFY id int(10) UNSIGNED NOT NULL AUTO_INCREMENT;")
|
||||||
|
cursor.execute("ALTER TABLE discord_link ADD CONSTRAINT discord_link_ibfk_1 FOREIGN KEY (admin_id) REFERENCES sm_admins (id);")
|
||||||
|
except mysql.connector.InterfaceError:
|
||||||
|
logger.info(f"Connection to mysql got interrupted, trying to reconnect...")
|
||||||
|
self.con.reconnect()
|
||||||
|
self.init_test_database()
|
||||||
|
|
||||||
def create_reserved_slot(self, discord_user, steam_id):
|
def create_reserved_slot(self, discord_user, steam_id):
|
||||||
cursor = self.con.cursor()
|
cursor = self.con.cursor()
|
||||||
|
@ -33,5 +40,9 @@ class Database:
|
||||||
val_discord = (id, str(discord_user.id), steam_id)
|
val_discord = (id, str(discord_user.id), steam_id)
|
||||||
cursor.execute(sql_discord, val_discord)
|
cursor.execute(sql_discord, val_discord)
|
||||||
self.con.commit()
|
self.con.commit()
|
||||||
|
except mysql.connector.InterfaceError:
|
||||||
|
logger.info(f"Connection to mysql got interrupted, trying to reconnect...")
|
||||||
|
self.con.reconnect()
|
||||||
|
self.create_reserved_slot(discord_user, steam_id)
|
||||||
except mysql.connector.IntegrityError:
|
except mysql.connector.IntegrityError:
|
||||||
raise Exception("Duplicate entrys")
|
raise Exception("Duplicate entrys")
|
||||||
|
|
|
@ -41,6 +41,7 @@ def main(args):
|
||||||
bot = interactions.Client(token=token)
|
bot = interactions.Client(token=token)
|
||||||
|
|
||||||
con = Database(host, user, password, db)
|
con = Database(host, user, password, db)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if args[0] == "init_database":
|
if args[0] == "init_database":
|
||||||
con.init_test_database()
|
con.init_test_database()
|
||||||
|
|
Loading…
Reference in a new issue