Psycopg3 returns None randomly - python

So i have a flask app, that connects to a Postgres12 DB using a user that just has select privileges. Often times i see my apis return 400 error, and api does this when the sql query executed returns None.
I have built a small wrapper class over execute and executemany function for some error handling.
import time
from functools import wraps
import psycopg
from psycopg import InterfaceError, OperationalError
logger = logging.getLogger(__name__)
def retry(fn):
#wraps(fn)
def wrapper(*args, **kw):
cls = args[0]
exec = None
for x in range(cls._reconnectTries):
try:
return fn(*args, **kw)
except (InterfaceError, OperationalError) as e:
logger.warning(f"Database Connection {e} exception type: {type(e)}")
logger.info(f"Idle for {cls._reconnectIdle} seconds")
time.sleep(cls._reconnectIdle)
cls._connect()
exec = e
import sys
logger.exception(f"Exiting the system, {exec} ")
sys.exit(exec)
return wrapper
class Connection:
_reconnectTries = 5
_reconnectIdle = 2
def __init__(self, conn_string):
self._conn_string = conn_string
self.conn = None
self.cursor = None
self._connect()
def _connect(self):
self.conn = psycopg.connect(self._conn_string)
self.conn.autocommit = True
self.cursor = self.conn.cursor()
#retry
def execute(self, **kwargs):
# self.conn.commit()
if "query" in kwargs:
"""
this is done to ensure postgres logs multi line queries sent by client in single line for easier
log collection and debugging.
"""
kwargs["query"] = kwargs["query"].replace("\n", " ")
kwargs["query"] = " ".join(kwargs["query"].split())
return self.cursor.execute(**kwargs)
#retry
def executemany(self, **kwargs):
# self.conn.commit()
return self.cursor.executemany(**kwargs)
For the sake of simplicity, the query code looks somewhat like
store_detail_by_link_query = """
SELECT
json_build_object('id', store.id, 'uuid', store.uuid)
FROM
optimus_store store
WHERE
store.link= %(store_link_or_domain)s and store.is_active = TRUE and store.is_deleted = FALSE;
"""
optimus_connection = Connection(conn_string=CONN_STRING)
params = {
"store_link_or_domain": "dipen28",
}
row = optimus_connection.execute(
query=store_detail_by_link_query,
params=params,
).fetchone()
The problem is, in the same api call if i just do a check that result is None, then rerun the query, at that time the result comes.
I know the data is there in database, this is coming to us in our production system and i am unable to reproduce it at local.
Any help is much appreciated.

Related

SQLAlchemy: execute() got an unexpected keyword argument

I have database.py which contains the database exec functions
import sqlachemy
class DB():
def __init__(self, **kwargs):
self.connection = None
self.connect(**kwargs)
def connect(self, **kwargs):
if 'url' in kwargs and kwargs.get('url') is not None:
return self.connectUrl(kwargs.get('url'), kwargs.get('username'),
kwargs.get('password'), kwargs.get('database'))
else :
return self.connectHost(kwargs.get('host'), kwargs.get('port'), kwargs.get('username'),
kwargs.get('password'), kwargs.get('database'))
def execute(self, query):
if self.connect is None:
raise Exception('No connection')
try:
with self.connection.connect() as conn:
return conn.execute(query)
except (pymysql.err.OperationalError, sqlalchemy.exc.OperationalError) as e:
print(e)
except (Exception) as e:
print(e)
else:
return None
Also I've a main.py. Im importing the DB class from the above file and using it here.
from database import db
# db connection
def connect():
global db
db = DB(
database="DATABASE_NAME",
username="DATABASE_USER",
password="XXXXXXXXX",
url="XXXXXXX",
host="XXXXXXXXXXX",
port="XXXXXXX",
)
SQL_QUERY = """ select * from sample_table where country = :country and age = :age """
_age = 5
_country = 'US'
connect()
query = text(SQL_QUERY).bindparams(bindparam("country", String), bindparam("age", String))
db.execute(query, age=_age, country=_country)
When I'm trying to execute this script, I'm getting an error
db.execute(query,age=_age, country=_country) TypeError: execute() got an unexpected keyword argument 'age'
Can anyone help me with this?
you can simply pass variables as tuple :
query = 'select * from sample_table where country = ? and age = ?'
_age = 5
_country = 'US'
connect()
db.execute(query, (_age,_country,))

Python unitest mock to check that a method returns a method call

I'm trying to figure out how I can simply check that the execute_db returns
a method call with the name cursor.fetchone?
I'm not interested to see if the db works, that will be done in a integration test later on.
I've written a small unittest already, but here I'm only mocking the return value.. I want to find a way to test that the method with the given name is being called as well.
class DataChecker:
def __init__(self):
# Initialize class
self.conn = sqlite3.connect("pos.db")
self.cursor = self.conn.cursor()
def execute_db(self, query, params=None):
# Execute SQL query with parameters and return data
self.cursor.execute(query, [params])
self.conn.commit()
return self.cursor.fetchone()
Test:
def test_execute_db():
mock_datachecker = Mock()
mock_datachecker.cursor.fetchone.return_value = "one"
assert DataChecker.execute_db(mock_datachecker, "SELECT * FROM Customers;", 1) == "one"
You would mock the method fetchone from sqlite3 that is imported in DataChecker module.
db.py
import sqlite3
class DataChecker:
def __init__(self):
# Initialize class
self.conn = sqlite3.connect("pos.db")
self.cursor = self.conn.cursor()
def execute_db(self, query, params=None):
# Execute SQL query with parameters and return data
if params:
self.cursor.execute(query, params)
else:
self.cursor.execute(query)
self.conn.commit()
return self.cursor.fetchone()
Then you could use the db.sqlite3 to mock the connect().cursor().fetchone method.
def test_execute_db():
with patch('db.sqlite3') as mock_db:
mock_db.connect().cursor().fetchone.return_value = "one"
assert DataChecker().execute_db("SELECT * FROM Customers") == "one"

Library program in Python 3 (beginner) with psycopg2

I need help with this program. I am making a program for a library in Python 3 (psycopg2) and I am making my modules to handle my tables, I have already done the "create" module and I am doing the "delete" module, I need help to do it, the code used is the following:
This is my class ConnectioDB: (it works to connect to my database in postgresql)
class ConnectionDB:
"""Connection class."""
bd = None
cursor = None
def __init__(self, **param):
"""Connection constructor."""
try:
self.db = connect(
host = '127.0.0.1', # localhost
user = 'postgres'
password = #$#!#*
database = 'national-library'
)
self.cursor = self.db.cursor()
except Error as e:
write_errors(e, 'Failed to connect to database')
with the following lines are used to execute sql code:
def execute_sql(
self,
sentence_sql,
param=None,
write_in_db=True
):
"""Execute SQL code."""
try:
execute = self.cursor.execute(sentence_sql, param)
if write_in_db:
result = self.db.commit()
except Exception as e:
write_errors(e, f"An error occurred while executing the SQL statement:\n\n{sentence_sql}\n")
if write_in_db:
self.db.rollback()
now this is my Model class that has the "create" module
class Model():
"""Generic model class."""
table_name = None
connection = ConnectionDB()
def create(self):
"""Save in database."""
table_name = self.table_name
keys = ", ".join(self.__dict__.keys())
values_placeholders = ", ".join(["%s" for i in range(len(self.__dict__.keys()))])
values = self.__dict__.values()
sql = f"INSERT INTO {table_name} ({keys}) VALUES ({values_placeholders})"
self.connection.execute_sql(sql, tuple(values))
I was trying to do the following code for my "delete" module but I'm not sure if it's ok:
def delete(self, column_id):
"""Delete an item in the database."""
table_name = self.table_name
sql = f"DELETE FROM {table_name} WHERE id = %s"
self.connection.execute_sql(sql, (column_id, ))
I hope you can help me. Thank you!

Use decorator to connect to a postgres database

I am working on a program to store my picture meta data and thumbnails into a postgres database using python and psycopg2. In the example I have defined a class MyDbase with methods to create a table, store a value and load a value. Each of these methods needs to connect to the database and a cursor object to execute sql commands. To avoid repetition of code to make the connection and get the cursor I have made a sub class DbDecorators with a decorator connect.
My question: is this a proper way to handle this and specifically using the with statement and passing the cursor to the Dbase method (func) inside the wrapper?
from functools import wraps
import psycopg2
class MyDbase:
''' example using a decorator to connect to a dbase
'''
table_name = 'my_table'
class DbDecorators:
host = 'localhost'
db_user = 'db_tester'
db_user_pw = 'db_tester_pw'
database = 'my_database'
#classmethod
def connect(cls, func):
#wraps(func)
def wrapper(*args, **kwargs):
connect_string = f'host=\'{cls.host}\' dbname=\'{cls.database}\''\
f'user=\'{cls.db_user}\' password=\'{cls.db_user_pw}\''
result = None
try:
with psycopg2.connect(connect_string) as connection:
cursor = connection.cursor()
result = func(*args, cursor, **kwargs)
except psycopg2.Error as error:
print(f'error while connect to PostgreSQL {cls.database}: '
f'{error}')
finally:
if connection:
cursor.close()
connection.close()
print(f'PostgreSQL connection to {cls.database} is closed')
return result
return wrapper
#staticmethod
def get_cursor(cursor):
if cursor:
return cursor
else:
print(f'no connection to database')
raise()
#classmethod
#DbDecorators.connect
def create_table(cls, *args):
cursor = cls.DbDecorators().get_cursor(*args)
sql_string = f'CREATE TABLE {cls.table_name} '\
f'(id SERIAL PRIMARY KEY, name VARCHAR(30));'
print(sql_string)
cursor.execute(sql_string)
#classmethod
#DbDecorators.connect
def store_value(cls, name, *args):
cursor = cls.DbDecorators().get_cursor(*args)
sql_string = f'INSERT INTO {cls.table_name} (name) VALUES (%s);'
print(sql_string)
cursor.execute(sql_string, (name,))
#classmethod
#DbDecorators.connect
def load_value(cls, _id, *args):
cursor = cls.DbDecorators().get_cursor(*args)
sql_string = f'SELECT * FROM {cls.table_name} where id = \'{_id}\';'
print(sql_string)
cursor.execute(sql_string)
db_row = cursor.fetchone()
return db_row
def test():
my_db = MyDbase()
my_db.create_table()
my_db.store_value('John Dean')
db_row = my_db.load_value(1)
print(f'id: {db_row[0]}, name: {db_row[1]}')
if __name__ == '__main__':
test()
probably I did not get your request correctly. Why you need decorator but don't use context manager? Like define db client in any file where from you can import it later and then use it in context manager –
from psycopg2 import SomeDataBase
db = SomeDataBase(credentials)
def create_table(table_name):
with db:
sql_string = f'CREATE TABLE {table_name} '\
f'(id SERIAL PRIMARY KEY, name VARCHAR(30));'
db.cursor.execute(sql_string)
Using a context manager will not close the connection, only the cursor. So using the decorator pattern actually makes more sense here. More info on the context manager: https://www.psycopg.org/docs/usage.html (scroll down to the "with statement" section.)

sqlite3.Connection class inheritance, commit not working

This is my first time using sqlite3 through class inheritance, and I've run into a problem where I get no traceback errors, but the queries I execute won't commit. I simplified my code
import sqlite3 as lite
class BaseModel(lite.Connection):
def __init__(self, **args):
lite.Connection.__init__(self, **args)
self.cur = self.cursor()
def execute(self, query):
self.cur.execute(query)
class Model(BaseModel):
def __init__(self, **args):
BaseModel.__init__(self, **args)
def _new_(self):
queries = []
queries.append(' '.join(['CREATE TABLE IF NOT EXISTS tb1',
'(id INTEGER PRIMARY KEY,',
'column1 TEXT,',
'column2 INT)']))
for q in queries:
self.execute(q) # execute the queries
self.commit() # write changes to db
def tables(self):
query = 'SELECT name FROM sqlite_master WHERE type="table" ORDER BY name'
results = self.execute(query)
return results#.fetchall()
if __name__ == '__main__':
model = Model(database='test.db')
model._new_()
# Test Fails because the queries aren't being saved in the db
# see Model.__new__ for details
tables = model.tables() # get all tables
print 'Tables Created:'
if tables:
for t in model.tables():
print '\t%s' % str(t[0])
else: print tables
You need to call self.commit():
self.commit() # write changes to db
Without the () you are merely referencing the method, not invoking it.
Next, your execute() function doesn't return anything:
def execute(self, query):
return self.cur.execute(query)

Categories

Resources