Add database reconnect

This commit is contained in:
Cole Deck
2025-02-18 19:16:38 +00:00
parent e154645972
commit d9454f44d8
5 changed files with 29 additions and 9 deletions

View File

@@ -1,7 +1,12 @@
from peewee import *
from search import InventorySearch as ivs
from playhouse.shortcuts import ReconnectMixin
db = MySQLDatabase('inventory', thread_safe=True, user='inventory', password='nfrwnfprifbwef', host='database', port=3306)
class ReconnectMySQLDatabase(ReconnectMixin, MySQLDatabase):
pass
# auto-reconnect if mysql disconnects
db = ReconnectMySQLDatabase('inventory', thread_safe=True, user='inventory', password='nfrwnfprifbwef', host='database', port=3306)
search = None
class user(Model):
@@ -144,7 +149,8 @@ def create_item(fullname, serial, officename, barcode, locationid=None, descript
if loc == False:
loc = None
else:
print("Found location: " + loc.name)
pass
#print("Found location: " + loc.name)
off = office.select().where(office.name == officename)[0]
itm = item(office=off, barcode=barcode, fullname=fullname, description=description, loc=loc, serial=serial, mac=mac, fwver=fwver, manufacturer=manufacturer)
itm.save(force_insert=True)
@@ -156,7 +162,7 @@ def create_item(fullname, serial, officename, barcode, locationid=None, descript
except:
pass
search.add_document(itmdict)
print("item: " + itm.fullname)
print("added item: " + itm.fullname)
return itm
except IntegrityError:
print("Duplicate item " + fullname)
@@ -174,7 +180,8 @@ def update_item(fullname, serial, officename, barcode, locationid=None, descript
if loc == False:
loc = None
else:
print("Found location: " + loc.name)
pass
#print("Found location: " + loc.name)
off = office.select().where(office.name == officename)[0]
itm = item(office=off, barcode=barcode, fullname=fullname, description=description, loc=loc, serial=serial, mac=mac, fwver=fwver, manufacturer=manufacturer)
itm.save()
@@ -186,7 +193,7 @@ def update_item(fullname, serial, officename, barcode, locationid=None, descript
except:
pass
search.add_document(itmdict)
print("item: " + itm.fullname)
print("updated item: " + itm.fullname)
return itm
except IntegrityError:
print("Duplicate item " + fullname)
@@ -215,7 +222,7 @@ def create_component(parentitem, name, barcode, serial=None, description=None):
try:
cmp = component(owner=itm, name=name, barcode=barcode, description=description, serial=serial)
cmp.save(force_insert=True)
print("component: " + cmp.name)
print("added component: " + cmp.name)
return cmp
except IntegrityError:
print("Duplicate component " + name)
@@ -347,6 +354,12 @@ def user_login(username, password):
return user.password == password
else:
return False
def check_db_connection():
query = office.select()
if len(query) > 0:
# require the query to fire by checking it's return length
pass
def test():
costa = create_user("Costa Aralis", "caralpwtwfpis", "12345")