mirror of
https://github.com/Xevion/contest-assistant.git
synced 2025-12-06 21:14:41 -06:00
fix async issues, add logging, get database to be created on startup
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
|||||||
# Repository specific
|
# Repository specific
|
||||||
.idea
|
.idea
|
||||||
token.dat
|
token.dat
|
||||||
|
database.db
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
DATABASE = 'database.db'
|
# Path Constants
|
||||||
|
BASE_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__, '..')))
|
||||||
|
TOKEN = os.path.join(BASE_DIR, 'token.dat')
|
||||||
|
DATABASE = os.path.join(BASE_DIR, 'database.db')
|
||||||
|
|
||||||
|
# Other constants
|
||||||
LOGGING_LEVEL = logging.DEBUG
|
LOGGING_LEVEL = logging.DEBUG
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
import aiosqlite
|
import aiosqlite
|
||||||
@@ -16,7 +17,6 @@ class ContestDatabase(object):
|
|||||||
|
|
||||||
def __init__(self, conn: aiosqlite.Connection) -> None:
|
def __init__(self, conn: aiosqlite.Connection) -> None:
|
||||||
self.conn = conn
|
self.conn = conn
|
||||||
self.setup()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def create(cls) -> 'ContestDatabase':
|
async def create(cls) -> 'ContestDatabase':
|
||||||
@@ -24,17 +24,27 @@ class ContestDatabase(object):
|
|||||||
Constructs a ContestDatabase object connecting to the default database location with the proper connection settings.
|
Constructs a ContestDatabase object connecting to the default database location with the proper connection settings.
|
||||||
:return: A fully realized ContestDatabase object.
|
:return: A fully realized ContestDatabase object.
|
||||||
"""
|
"""
|
||||||
conn = await aiosqlite.connect(constants.DATABASE, detect_types=sqlite3.PARSE_DELCTYPES)
|
conn = await aiosqlite.connect(constants.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES)
|
||||||
|
logger.info(f'Asynchronous SQLite3 connection made to ./{os.path.relpath(constants.DATABASE)}')
|
||||||
db = ContestDatabase(conn)
|
db = ContestDatabase(conn)
|
||||||
|
await db.setup()
|
||||||
await conn.commit()
|
await conn.commit()
|
||||||
|
logger.info('ContestDatabase instance created.')
|
||||||
return db
|
return db
|
||||||
|
|
||||||
async def setup(self) -> None:
|
async def setup(self) -> None:
|
||||||
"""Sets up the tables for initial database creation"""
|
"""Sets up the tables for initial database creation"""
|
||||||
await self.conn.execute('''CREATE TABLE IF NOT EXISTS guild
|
cur = await self.conn.cursor()
|
||||||
(id INTEGER PRIMARY KEY,
|
try:
|
||||||
prefix TEXT DEFAULT '$',
|
await cur.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name = ?;''', ['guild'])
|
||||||
submission INTEGER NULLABLE)''')
|
if await cur.fetchone() is None:
|
||||||
|
await self.conn.execute('''CREATE TABLE IF NOT EXISTS guild
|
||||||
|
(id INTEGER PRIMARY KEY,
|
||||||
|
prefix TEXT DEFAULT '$',
|
||||||
|
submission INTEGER NULLABLE)''')
|
||||||
|
logger.info(f"'guild' table created.")
|
||||||
|
finally:
|
||||||
|
await cur.close()
|
||||||
|
|
||||||
async def setup_guild(self, guild_id: int) -> None:
|
async def setup_guild(self, guild_id: int) -> None:
|
||||||
"""Sets up a guild in the database."""
|
"""Sets up a guild in the database."""
|
||||||
@@ -60,6 +70,6 @@ class ContestDatabase(object):
|
|||||||
cur = await self.conn.cursor()
|
cur = await self.conn.cursor()
|
||||||
try:
|
try:
|
||||||
await cur.execute('''SELECT prefix FROM guild WHERE id = ?''', [guild_id])
|
await cur.execute('''SELECT prefix FROM guild WHERE id = ?''', [guild_id])
|
||||||
return (await cur.fetchone())['prefix']
|
return await (await cur.fetchone())['prefix']
|
||||||
finally:
|
finally:
|
||||||
await cur.close()
|
await cur.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user