db fix
This commit is contained in:
1 parent
d53fd4790b
commit
887fdbf25c
3 files changed
+59
-30
No files matched your search
+57
-21
@@ -6,27 +6,9 @@ DB_PATH = "SQLite/FemBot.db"
|
|||||||
class Database:
|
class Database:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.conn = None
|
self.conn = None
|
||||||
|
self._lock = None
|
||||||
|
|
||||||
async def connect(self):
|
async def _ensure_schema(self):
|
||||||
if self.conn is None:
|
|
||||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
|
||||||
|
|
||||||
db_existed = os.path.exists(DB_PATH)
|
|
||||||
if db_existed:
|
|
||||||
print(f"Using existing database file at {DB_PATH}")
|
|
||||||
else:
|
|
||||||
print(f"Database file not found at {DB_PATH}, creating a new one...")
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.conn = await aiosqlite.connect(DB_PATH)
|
|
||||||
except aiosqlite.Error as e:
|
|
||||||
print(f"Failed to open database at {DB_PATH}: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
if not db_existed and not os.path.exists(DB_PATH):
|
|
||||||
print(f"Warning: database file was expected at {DB_PATH} but still missing after connect.")
|
|
||||||
|
|
||||||
try:
|
|
||||||
await self.conn.executescript("""
|
await self.conn.executescript("""
|
||||||
CREATE TABLE IF NOT EXISTS test (
|
CREATE TABLE IF NOT EXISTS test (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -57,13 +39,67 @@ class Database:
|
|||||||
""")
|
""")
|
||||||
|
|
||||||
await self.conn.commit()
|
await self.conn.commit()
|
||||||
print("Table check/creation completed successfully.")
|
|
||||||
|
|
||||||
|
async def connect(self):
|
||||||
|
if self._lock is None:
|
||||||
|
import asyncio
|
||||||
|
self._lock = asyncio.Lock()
|
||||||
|
|
||||||
|
async with self._lock:
|
||||||
|
if self.conn is not None:
|
||||||
|
try:
|
||||||
|
await self.conn.execute("SELECT 1")
|
||||||
|
return self.conn
|
||||||
|
except Exception:
|
||||||
|
try:
|
||||||
|
await self.conn.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
self.conn = None
|
||||||
|
|
||||||
|
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||||
|
|
||||||
|
db_existed = os.path.exists(DB_PATH)
|
||||||
|
if db_existed:
|
||||||
|
print(f"Using existing database file at {DB_PATH}")
|
||||||
|
else:
|
||||||
|
print(f"Database file not found at {DB_PATH}, creating a new one...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.conn = await aiosqlite.connect(DB_PATH)
|
||||||
|
except aiosqlite.Error as e:
|
||||||
|
print(f"Failed to open database at {DB_PATH}: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
if not db_existed and not os.path.exists(DB_PATH):
|
||||||
|
print(f"Warning: database file was expected at {DB_PATH} but still missing after connect.")
|
||||||
|
|
||||||
|
try:
|
||||||
|
await self._ensure_schema()
|
||||||
|
print("Table check/creation completed successfully.")
|
||||||
except aiosqlite.Error as e:
|
except aiosqlite.Error as e:
|
||||||
print(f"Failed to check/creation table: {e}")
|
print(f"Failed to check/creation table: {e}")
|
||||||
|
|
||||||
return self.conn
|
return self.conn
|
||||||
|
|
||||||
|
async def get_conn(self):
|
||||||
|
if self.conn is not None:
|
||||||
|
try:
|
||||||
|
await self.conn.execute("SELECT 1")
|
||||||
|
return self.conn
|
||||||
|
except Exception:
|
||||||
|
try:
|
||||||
|
await self.conn.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
self.conn = None
|
||||||
|
await self.connect()
|
||||||
|
return self.conn
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
if self.conn:
|
if self.conn:
|
||||||
|
try:
|
||||||
await self.conn.close()
|
await self.conn.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
self.conn = None
|
||||||
@@ -46,14 +46,7 @@ class Counting(commands.Cog):
|
|||||||
if message.channel.id != COUNTING_CHANNEL_ID:
|
if message.channel.id != COUNTING_CHANNEL_ID:
|
||||||
return
|
return
|
||||||
|
|
||||||
db = self.bot.database.conn
|
db = await self.bot.database.get_conn()
|
||||||
if db is None:
|
|
||||||
try:
|
|
||||||
await message.delete()
|
|
||||||
except discord.NotFound:
|
|
||||||
pass
|
|
||||||
await message.channel.send(f"DB is Down :c <@1073620716152434830>")
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with db.execute(
|
async with db.execute(
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ async def CreateTicketChannel(interaction: discord.Interaction, chosen_language,
|
|||||||
print("Error: Category not found")
|
print("Error: Category not found")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
db = bot.database.conn
|
db = await bot.database.get_conn()
|
||||||
|
|
||||||
cursor = await db.execute(
|
cursor = await db.execute(
|
||||||
"INSERT INTO tickets (user_id, language, reason) VALUES (?, ?, ?)",
|
"INSERT INTO tickets (user_id, language, reason) VALUES (?, ?, ?)",
|
||||||
|
|||||||
Reference in new issue
Block a user