testing tg bot library and set the db connection for dev and production

This commit is contained in:
Hosein
2025-12-28 10:49:50 +03:30
parent b16ed91822
commit 67a76250ab
4 changed files with 150 additions and 33 deletions
+9 -6
View File
@@ -9,15 +9,18 @@ logger = logging.getLogger(__name__)
_connection_pool = None
def get_connection_pool():
"""Create a connection pool"""
global _connection_pool
if _connection_pool is None:
try:
DATABASE_URL = os.environ.get("DATABASE_URL")
_connection_pool = psycopg2.pool.SimpleConnectionPool(
1, # minimum connections
5, # maximum connections
DATABASE_URL
)
# --- Production settings
# DATABASE_URL = os.environ.get("DATABASE_URL")
# _connection_pool = psycopg2.pool.SimpleConnectionPool(1, 5, DATABASE_URL);
# --- Development settings
dev_url = "postgresql://postgres:123@localhost:5432/OLP"
_connection_pool = psycopg2.pool.SimpleConnectionPool(1, 5, dev_url);
logger.info("Database connection pool created")
except Exception as e:
logger.error(f"Failed to create connection pool: {e}")
+24 -4
View File
@@ -4,8 +4,8 @@ import database.connection as connection
logger = logging.getLogger(__name__)
class testModel:
def getAllUsers():
class Students:
def getAllStudents():
conn = None
try:
conn = connection.get_db_connection()
@@ -13,13 +13,33 @@ class testModel:
return None
cursor = conn.cursor()
cursor.execute("SELECT * FROM students");
cursor.execute("SELECT id,name FROM students");
result = cursor.fetchall()
cursor.close()
return result if result else None
except Exception as e:
logger.error(f"Database query error: {e}")
logger.error(f"Database query error in getAllStudents: {e}")
return None
finally:
if conn:
connection.release_db_connection(conn)
def getStudentById(student_id):
conn = None
try:
conn = connection.get_db_connection()
if not conn:
return None
cursor = conn.cursor()
cursor.execute("SELECT id,username, created_at, email, phone_number, last_seen, is_verfied, birthday FROM students WHERE id = %s", (student_id,))
result = cursor.fetchall()
cursor.close()
return result[0] if result else None
except Exception as e:
logger.error(f"Database query error in getStudentById: {e}")
return None
finally:
if conn: